root/trunk/examples/metapredicates/closures.lgt

Revision 4601, 1.4 KB (checked in by pmoura, 7 weeks ago)

Added svn:mime-type property to source files (set to text/x-logtalk).

  • Property svn:mime-type set to text/x-logtalk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2/*  Logtalk meta-predicates accept both goals and closures
3    as meta-arguments as illustrated in this example
4*/
5
6
7:- object(metapreds).
8
9    % the meta_predicate/1 directive below changes the interpretation of meta-calls on apply/2
10    % clauses; the integer argument ("1") implies that the first argument is a closure that will
11    % be used to construct a goal by appending exactly one additional argument
12
13    :- public(apply/2).
14    :- meta_predicate(apply(1, *)).
15    :- mode(apply(+callable, ?term), zero_or_more).
16
17    apply(Closure, Arg) :-      % the Logtalk compiler verifies that any closure which is a
18        call(Closure, Arg).     % meta-argument is used within a call/N method that complies with
19                                % the meta-predicate directive (in this case, apply(1, *) => call/2)
20
21    :- public(test_this/0).     % simple predicate for testing calls to a local meta-predicate
22
23    test_this :-
24        apply(foo(X), Y),
25        writeq((X, Y)), nl.
26
27    foo(1, metapreds).
28
29:- end_object.
30
31
32:- object(descendant,
33    extends(metapreds)).   
34
35    :- public(test_self/0).     % simple predicate for testing calls to a meta-predicate
36                                % defined in an ancestor object
37    test_self :-
38        ::apply(foo(X), Y),
39        writeq((X, Y)), nl.
40
41    foo(2, descendant).
42
43:- end_object.
44
45
46:- object(test).
47
48    :- public(test_obj/0).      % simple predicate for testing calls to a meta-predicate
49                                % defined in another object
50    test_obj :-
51        metapreds::apply(foo(X), Y),
52        writeq((X, Y)), nl.
53
54    foo(3, test).
55
56:- end_object.
Note: See TracBrowser for help on using the browser.