Changeset 4594 for trunk/examples/metapredicates
- Timestamp:
- 11/19/08 09:18:27 (7 weeks ago)
- Location:
- trunk/examples/metapredicates
- Files:
-
- 1 added
- 4 modified
-
closures.lgt (modified) (5 diffs)
-
loader.lgt (modified) (1 diff)
-
NOTES.txt (modified) (2 diffs)
-
predicates.lgt (added)
-
SCRIPT.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/metapredicates/closures.lgt
r4522 r4594 5 5 6 6 7 :- object(meta ).7 :- object(metapreds). 8 8 9 9 % the meta_predicate/1 directive below changes the interpretation of meta-calls on apply/2 … … 12 12 13 13 :- public(apply/2). 14 :- meta_predicate(apply(1, *)). 14 15 :- mode(apply(+callable, ?term), zero_or_more). 15 :- meta_predicate(apply(1, *)).16 16 17 17 apply(Closure, Arg) :- % the Logtalk compiler verifies that any closure which is a … … 23 23 test_this :- 24 24 apply(foo(X), Y), 25 write ((X, Y)), nl.25 writeq((X, Y)), nl. 26 26 27 foo(1, meta ).27 foo(1, metapreds). 28 28 29 29 :- end_object. 30 30 31 31 32 :- object(desc ,33 extends(meta )).32 :- object(descendant, 33 extends(metapreds)). 34 34 35 35 :- public(test_self/0). % simple predicate for testing calls to a meta-predicate … … 37 37 test_self :- 38 38 ::apply(foo(X), Y), 39 write ((X, Y)), nl.39 writeq((X, Y)), nl. 40 40 41 foo(2, desc ).41 foo(2, descendant). 42 42 43 43 :- end_object. … … 49 49 % defined in another object 50 50 test_obj :- 51 meta ::apply(foo(X), Y),52 write ((X, Y)), nl.51 metapreds::apply(foo(X), Y), 52 writeq((X, Y)), nl. 53 53 54 54 foo(3, test). -
trunk/examples/metapredicates/loader.lgt
r3853 r4594 1 1 2 2 :- initialization(( 3 logtalk_load( library(types_loader), [reload(skip)]), % allow for static binding4 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 16 16 body of the predicate definition. 17 17 18 This example defines t woobjects:18 This example defines the following objects: 19 19 20 20 sort(_) … … 26 26 this object implements a meta-predicate that is used by sort(_) to 27 27 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 16 16 17 17 18 % note that "user" is a pseudo-object representing the Prolog database 19 % th is implies that the integer comparisons are doneusing the standard18 % note that "user" is a pseudo-object representing the Prolog database; 19 % therefore, the integer comparisons are performed using the standard 20 20 % Prolog built-in predicates 21 21 … … 53 53 exit: sort([4,9],[4,9]) 54 54 55 Sorted = [1,2,3,4,9] ? 56 55 Sorted = [1,2,3,4,9] 57 56 yes 58 57 … … 60 59 % call the meta-predicate apply/2 directly: 61 60 62 | ?- meta ::test_this.61 | ?- metapreds::test_this. 63 62 64 1, meta 65 63 1, metapreds 66 64 yes 67 65 … … 69 67 % send an apply/2 message to self: 70 68 71 | ?- desc ::test_self.69 | ?- descendant::test_self. 72 70 73 2, desc 74 71 2, descendant 75 72 yes 76 73 … … 81 78 82 79 3, test 80 yes 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 88 Included = [2, 4] 89 Excluded = [1, 3, 5] 90 yes 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 98 Result = 34 99 yes 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 107 Result = 'PREFIXabcdefghi' 108 yes 109 110 | ?- meta::fold_right(atom_concat, 'SUFIX', [abc,def,ghi], Result). 111 112 Result = abcdefghiSUFIX 113 yes 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 121 Result = 15 122 yes 123 124 | ?- meta::fold_left(predicates::product, 1, [1,2,3,4,5], Result). 125 126 Result = 120 127 yes 128 129 | ?- meta::fold_left(predicates::tuple, (0,0), [(1,2), (3,4), (6,4)], Result). 130 131 Result = (10, 10) 132 yes 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]). 83 138 84 139 yes 140 141 | ?- meta::map(char_code, [a,b,c,d,e], Codes). 142 143 Codes = [97, 98, 99, 100, 101] 144 yes
