Changeset 4594 for trunk/examples

Show
Ignore:
Timestamp:
11/19/08 09:18:27 (7 weeks ago)
Author:
pmoura
Message:

Updated the "metapredicates" example with Paul Crocker's contributions to illustrate the use of some of the meta-predicates defined in the Logtalk library.

Location:
trunk/examples/metapredicates
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/metapredicates/closures.lgt

    r4522 r4594  
    55 
    66 
    7 :- object(meta). 
     7:- object(metapreds). 
    88 
    99    % the meta_predicate/1 directive below changes the interpretation of meta-calls on apply/2 
     
    1212 
    1313    :- public(apply/2). 
     14    :- meta_predicate(apply(1, *)). 
    1415    :- mode(apply(+callable, ?term), zero_or_more). 
    15     :- meta_predicate(apply(1, *)). 
    1616 
    1717    apply(Closure, Arg) :-      % the Logtalk compiler verifies that any closure which is a 
     
    2323    test_this :- 
    2424        apply(foo(X), Y), 
    25         write((X, Y)), nl. 
     25        writeq((X, Y)), nl. 
    2626 
    27     foo(1, meta). 
     27    foo(1, metapreds). 
    2828 
    2929:- end_object. 
    3030 
    3131 
    32 :- object(desc, 
    33     extends(meta)).  
     32:- object(descendant, 
     33    extends(metapreds)).     
    3434 
    3535    :- public(test_self/0).     % simple predicate for testing calls to a meta-predicate 
     
    3737    test_self :- 
    3838        ::apply(foo(X), Y), 
    39         write((X, Y)), nl. 
     39        writeq((X, Y)), nl. 
    4040 
    41     foo(2, desc). 
     41    foo(2, descendant). 
    4242 
    4343:- end_object. 
     
    4949                                % defined in another object 
    5050    test_obj :- 
    51         meta::apply(foo(X), Y), 
    52         write((X, Y)), nl. 
     51        metapreds::apply(foo(X), Y), 
     52        writeq((X, Y)), nl. 
    5353 
    5454    foo(3, test). 
  • trunk/examples/metapredicates/loader.lgt

    r3853 r4594  
    11 
    22:- initialization(( 
    3     logtalk_load(library(types_loader), [reload(skip)]),    % allow for static binding 
    4     logtalk_load([closures, metapredicates]))). 
     3    logtalk_load([library(types_loader), library(metapredicates_loader)], [reload(skip)]),  % allow for static binding 
     4    logtalk_load([predicates, closures, metapredicates]))). 
  • trunk/examples/metapredicates/NOTES.txt

    r4572 r4594  
    1616body of the predicate definition. 
    1717 
    18 This example defines two objects: 
     18This example defines the following objects: 
    1919 
    2020    sort(_) 
     
    2626        this object implements a meta-predicate that is used by sort(_) to  
    2727        trace the sorting algorithm steps 
     28 
     29    metapreds 
     30    descendant 
     31    test 
     32        objects used for illustrating the use of clusures as meta-arguments 
     33 
     34    predicates 
     35        object defining some predicates for testing meta-predicates defined  
     36        in the Logtalk library 
  • trunk/examples/metapredicates/SCRIPT.txt

    r4572 r4594  
    1616 
    1717 
    18 % note that "user" is a pseudo-object representing the Prolog database 
    19 % this implies that the integer comparisons are done using the standard 
     18% note that "user" is a pseudo-object representing the Prolog database; 
     19% therefore, the integer comparisons are performed using the standard 
    2020% Prolog built-in predicates 
    2121 
     
    5353exit: sort([4,9],[4,9]) 
    5454 
    55 Sorted = [1,2,3,4,9] ?  
    56  
     55Sorted = [1,2,3,4,9] 
    5756yes 
    5857 
     
    6059% call the meta-predicate apply/2 directly: 
    6160 
    62 | ?- meta::test_this. 
     61| ?- metapreds::test_this. 
    6362 
    64 1, meta 
    65  
     631, metapreds 
    6664yes 
    6765 
     
    6967% send an apply/2 message to self: 
    7068 
    71 | ?- desc::test_self. 
     69| ?- descendant::test_self. 
    7270 
    73 2, desc 
    74  
     712, descendant 
    7572yes 
    7673 
     
    8178 
    82793, test 
     80yes 
     81 
     82 
     83% use the partition/4 predicate with a predicate defined in the 
     84% pseudo-object "user": 
     85 
     86| ?- meta::partition(even_integer, [1,2,3,4,5], Included, Excluded). 
     87 
     88Included = [2, 4] 
     89Excluded = [1, 3, 5] 
     90yes 
     91 
     92 
     93% use the fold_left/4 predicate with a predicate defined in the 
     94% pseudo-object "user": 
     95 
     96| ?- meta::fold_left(sum_squares, 0, [1,2,3], Result). 
     97 
     98Result = 34 
     99yes 
     100 
     101 
     102% use the fold_left/4 predicate with the atom_concat/3 Prolog built-in 
     103% predicate: 
     104 
     105| ?- meta::fold_left(atom_concat, 'PREFIX', [abc,def,ghi], Result). 
     106 
     107Result = 'PREFIXabcdefghi' 
     108yes 
     109 
     110| ?- meta::fold_right(atom_concat, 'SUFIX', [abc,def,ghi], Result). 
     111 
     112Result = abcdefghiSUFIX 
     113yes 
     114 
     115 
     116% use the fold_left/4 predicate with predicates defined in the object 
     117% "predicates": 
     118 
     119| ?- meta::fold_left(predicates::sum, 0, [1,2,3,4,5], Result). 
     120 
     121Result = 15 
     122yes 
     123 
     124| ?- meta::fold_left(predicates::product, 1, [1,2,3,4,5], Result). 
     125 
     126Result = 120 
     127yes 
     128 
     129| ?- meta::fold_left(predicates::tuple, (0,0), [(1,2), (3,4), (6,4)], Result). 
     130 
     131Result = (10, 10) 
     132yes 
     133 
     134 
     135% use the map/2-3 predicates with some Prolog built-in predicates: 
     136 
     137| ?- meta::map(integer, [1,2,3,4,5]). 
    83138 
    84139yes 
     140 
     141| ?- meta::map(char_code, [a,b,c,d,e], Codes). 
     142 
     143Codes = [97, 98, 99, 100, 101] 
     144yes