root/tags/lgt2210/library/meta.lgt

Revision 365, 1.3 KB (checked in by pmoura, 6 years ago)

Changed "authors" key in info/1 directive to "author".

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2:- object(meta,
3    implements(metap)).
4
5
6    :- info([
7        version is 1,
8        date is 2000/7/24,
9        author is 'Paulo Moura',
10        comment is 'Useful meta-predicates.']).
11
12
13    :- private(apply/3).
14    :- metapredicate(apply(*, *, ::)).
15
16    :- mode(apply(+callable, +list, -callable), zero_or_more).
17
18    :- info(apply/3, [
19        comment is 'Applies a predicate to list of arguments.',
20        argnames is ['Predicate', 'Arguments', 'Goal']]).
21
22
23    apply(Pred, Args) :-
24        apply(Pred, Args, _).
25
26
27    apply(Pred, Args, Goal) :-
28        (atom(Pred) ->
29            Goal =.. [Pred| Args]
30            ;
31            Pred =.. Old,
32            append(Old, Args, New),
33            Goal =.. New),
34        call(Goal).
35
36
37    append([], List, List).
38
39    append([Head| Tail], List, [Head| Tail2]) :-
40        append(Tail, List, Tail2).
41
42
43    callable(Term) :-
44        nonvar(Term),
45        functor(Term, Functor, _),
46        atom(Functor).
47
48
49    filter(Pred, In, Out) :-
50        filter2(In, Pred, Out).
51
52
53    filter2([], _, []).
54
55    filter2([Arg| Args], Pred, List) :-
56        (apply(Pred, [Arg], _) ->
57            List = [Arg| Args2]
58            ;
59            List = Args2),
60        filter2(Args, Pred, Args2).
61
62
63    map(Pred, In, Out) :-
64        map2(In, Pred, Out).
65
66
67    map2([], _, []).
68
69    map2([Old| Olds], Pred, [New| News]) :-
70        apply(Pred, [Old, New], _),
71        map2(Olds, Pred, News).
72
73
74
75    succeeds(Pred, List) :-
76        succeeds2(List, Pred).
77
78
79    succeeds2([], _).
80
81    succeeds2([Head| Tail], Pred) :-
82        apply(Pred, [Head], _),
83        succeeds2(Tail, Pred).
84
85
86:- end_object.
Note: See TracBrowser for help on using the browser.