| 1 | ================================================================ |
|---|
| 2 | Logtalk - Open source object-oriented logic programming language |
|---|
| 3 | Release 2.35.0 |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 1998-2009 Paulo Moura. All Rights Reserved. |
|---|
| 6 | Logtalk is free software. You can redistribute it and/or modify |
|---|
| 7 | it under the terms of the "Artistic License 2.0" as published by |
|---|
| 8 | The Perl Foundation. Consult the "LICENSE.txt" file for details. |
|---|
| 9 | ================================================================ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | % start by loading the example and the required library files: |
|---|
| 13 | |
|---|
| 14 | | ?- logtalk_load(metapredicates(loader)). |
|---|
| 15 | ... |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | % note that "user" is a pseudo-object representing the Prolog database; |
|---|
| 19 | % therefore, the integer comparisons are performed using the standard |
|---|
| 20 | % Prolog built-in predicates |
|---|
| 21 | |
|---|
| 22 | | ?- sort(user)::sort([3, 1, 4, 2, 9], Sorted). |
|---|
| 23 | |
|---|
| 24 | call: partition([1,4,2,9],3,_358,_359) |
|---|
| 25 | exit: partition([1,4,2,9],3,[1,2],[4,9]) |
|---|
| 26 | call: sort([1,2],_740) |
|---|
| 27 | call: partition([2],1,_967,_968) |
|---|
| 28 | exit: partition([2],1,[],[2]) |
|---|
| 29 | call: sort([],_1300) |
|---|
| 30 | exit: sort([],[]) |
|---|
| 31 | call: sort([2],_1539) |
|---|
| 32 | call: partition([],2,_1765,_1766) |
|---|
| 33 | exit: partition([],2,[],[]) |
|---|
| 34 | call: sort([],_2093) |
|---|
| 35 | exit: sort([],[]) |
|---|
| 36 | call: sort([],_2332) |
|---|
| 37 | exit: sort([],[]) |
|---|
| 38 | exit: sort([2],[2]) |
|---|
| 39 | exit: sort([1,2],[1,2]) |
|---|
| 40 | call: sort([4,9],_2831) |
|---|
| 41 | call: partition([9],4,_3058,_3059) |
|---|
| 42 | exit: partition([9],4,[],[9]) |
|---|
| 43 | call: sort([],_3391) |
|---|
| 44 | exit: sort([],[]) |
|---|
| 45 | call: sort([9],_3630) |
|---|
| 46 | call: partition([],9,_3856,_3857) |
|---|
| 47 | exit: partition([],9,[],[]) |
|---|
| 48 | call: sort([],_4184) |
|---|
| 49 | exit: sort([],[]) |
|---|
| 50 | call: sort([],_4423) |
|---|
| 51 | exit: sort([],[]) |
|---|
| 52 | exit: sort([9],[9]) |
|---|
| 53 | exit: sort([4,9],[4,9]) |
|---|
| 54 | |
|---|
| 55 | Sorted = [1,2,3,4,9] |
|---|
| 56 | yes |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | % call the meta-predicate apply/2 directly: |
|---|
| 60 | |
|---|
| 61 | | ?- metapreds::test_this. |
|---|
| 62 | |
|---|
| 63 | 1, metapreds |
|---|
| 64 | yes |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | % send an apply/2 message to self: |
|---|
| 68 | |
|---|
| 69 | | ?- descendant::test_self. |
|---|
| 70 | |
|---|
| 71 | 2, descendant |
|---|
| 72 | yes |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | % send an apply/2 message from another object: |
|---|
| 76 | |
|---|
| 77 | | ?- test::test_obj. |
|---|
| 78 | |
|---|
| 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 and fold_right/4 predicates with the atom_concat/3 |
|---|
| 103 | % Prolog built-in 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]). |
|---|
| 138 | |
|---|
| 139 | yes |
|---|
| 140 | |
|---|
| 141 | | ?- meta::map(char_code, [a,b,c,d,e], Codes). |
|---|
| 142 | |
|---|
| 143 | Codes = [97, 98, 99, 100, 101] |
|---|
| 144 | yes |
|---|