meta programming, include, filter

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

meta programming, include, filter

Post by joerg » Wed Jan 21, 2009 12:13 pm

Hello,

I have not been using Logtalk for quite a long time, so maybe the following question is naive.

How do I have to refer to predicates that are an argument of a meta-predicate?

The example given below is supposed to be used with Swi: test::get_good_elements([a, b, 1, c, 3], [1, 3]) should be true. Yet, if you use the method get_good_elements, you will get this error message:

Code: Select all

ERROR: include_/3: Undefined procedure: good_element/1
Jörg


Code: Select all


:- object(swi).

    :- public(include/3).
    :- meta_predicate(include(::, *, *)).

    include(A, B, C) :-
        user::include(A, B, C).

:- end_object.

:- object(test).

    :- public(get_good_elements/2).
    :- private(good_element/1).

    get_good_elements(ListIn, ListOut) :-
        swi::include(good_element, ListIn, ListOut).

    good_element(1).
    good_element(2).
    good_element(3).

:- end_object.


Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: meta programming, include, filter

Post by Paulo Moura » Wed Jan 21, 2009 3:33 pm

Prolog proprietary built-in meta-predicates must be declared in the corresponding config file (in this case, "configs/swi.pl"). However, include/3 is not a built-in predicates but a library predicate. You could use the use_module/2 directive but that will not work in the current Logtalk version for meta-predicates. For the include/3 and filter/3 meta-predicates, why not use instead the Logtalk library object "meta"?

Cheers,

Paulo

P.S. I'm traveling today from the USA back to Portugal. I will give you more detailed feedback on this subject when I'm back at home.
Paulo Moura
Logtalk developer

joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

Re: meta programming, include, filter

Post by joerg » Wed Jan 21, 2009 3:55 pm

Hello Paulo,

thanks for your answer.
why not use instead the Logtalk library object "meta"
Yes, that is, of course, the best solution. I didn't think about the Logtalk library.

Your post caused me to have a closer look at the library. I really liked it. It inspired me to formulate some wishes in the wish list.
However, include/3 is not a built-in predicates but a library predicate. You could use the use_module/2 directive but that will not work in the current Logtalk version for meta-predicates.
I think I should better use the source code of Prolog-compiler-specific predicates in such cases, anyway.

Jörg

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: meta programming, include, filter

Post by Paulo Moura » Thu Jan 22, 2009 11:53 pm

The current Logtalk development version adds limited support for calling Prolog library meta-predicates. Check the following change set:

http://trac.logtalk.org/changeset/4681

Closures, unfortunately, are not supported as Prolog have no idea of how Logtalk compiles object predicates, preventing the correct construction of goals from closures when calling Prolog library meta-predicates from within objects. Follows a simple example, adopted from your code:

Code: Select all

:- object(swi).

    :- public(foreach/2).
    :- meta_predicate(foreach(::, ::)).

    :- use_module(aggregate, [foreach/2:swi_foreach/2]).

    foreach(A, B) :-
        swi_foreach(A, B).

:- end_object.

:- object(test).

    :- public(gb/0).

    gb :-
        swi::foreach(good(A), bad(A)).

    good(1).
    good(2).
    good(3).

    bad(1).
    bad(2).
    bad(3).

:- end_object.
Compiling and loading this code can be used to illustrate the new features in the current Logtalk development version:

Code: Select all

?- {joerg}.
%  <<< loading source file joerg... 
%  >>> compiling source file joerg...
%      compiling object swi... compiled
%      compiling object test... compiled
%  >>> joerg source file compiled
% joerg.pl compiled 0.00 sec, 4,680 bytes
%  <<< joerg source file loaded
%  (0 warnings)
true.

?- test::gb.
true.
Hope this helps. Cheers,

Paulo
Paulo Moura
Logtalk developer

joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

Re: meta programming, include, filter

Post by joerg » Fri Jan 23, 2009 12:12 pm

Thanks a lot for this code. It might help me to make some of my predicates more elegant.

Jörg

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: meta programming, include, filter

Post by Paulo Moura » Fri Jan 23, 2009 2:35 pm

Let me know if you need me to add to the Logtalk "meta" library object some missing meta-predicate (such as the foreach/2 predicate in the above example).

A few Logtalk releases back, I added support for using the use_module/2 directive within objects essentially to support migration of existing code that uses modules and to make it a bit easier to use constraints. Note that the specification of predicate aliases in use_module/2 directives is, of course, optional but handy sometimes to make code easier to read.
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest