This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/library/meta.lgt
pmoura 9fe4d26c59 Logtalk 2.27.0 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1539 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2006-02-10 17:44:05 +00:00

87 lines
1.3 KiB
Plaintext

:- object(meta,
implements(metap)).
:- info([
version is 1.0,
date is 2000/7/24,
author is 'Paulo Moura',
comment is 'Useful meta-predicates.']).
:- private(apply/3).
:- metapredicate(apply(*, *, ::)).
:- mode(apply(+callable, +list, -callable), zero_or_more).
:- info(apply/3, [
comment is 'Applies a predicate to list of arguments.',
argnames is ['Predicate', 'Arguments', 'Goal']]).
apply(Pred, Args) :-
apply(Pred, Args, _).
apply(Pred, Args, Goal) :-
(atom(Pred) ->
Goal =.. [Pred| Args]
;
Pred =.. Old,
append(Old, Args, New),
Goal =.. New),
call(Goal).
append([], List, List).
append([Head| Tail], List, [Head| Tail2]) :-
append(Tail, List, Tail2).
callable(Term) :-
nonvar(Term),
functor(Term, Functor, _),
atom(Functor).
filter(Pred, In, Out) :-
filter2(In, Pred, Out).
filter2([], _, []).
filter2([Arg| Args], Pred, List) :-
(apply(Pred, [Arg], _) ->
List = [Arg| Args2]
;
List = Args2),
filter2(Args, Pred, Args2).
map(Pred, In, Out) :-
map2(In, Pred, Out).
map2([], _, []).
map2([Old| Olds], Pred, [New| News]) :-
apply(Pred, [Old, New], _),
map2(Olds, Pred, News).
succeeds(Pred, List) :-
succeeds2(List, Pred).
succeeds2([], _).
succeeds2([Head| Tail], Pred) :-
apply(Pred, [Head], _),
succeeds2(Tail, Pred).
:- end_object.