2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
:- object(term,
|
|
|
|
implements(termp)).
|
|
|
|
|
|
|
|
:- info([
|
2007-06-12 11:39:47 +01:00
|
|
|
version is 1.1,
|
2003-02-05 00:15:28 +00:00
|
|
|
author is 'Paulo Moura',
|
2007-06-12 11:39:47 +01:00
|
|
|
date is 2007/4/3,
|
|
|
|
comment is 'Prolog term utility predicates.']).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2007-06-12 11:39:47 +01:00
|
|
|
depth(Term, Depth) :-
|
|
|
|
depth(Term, 0, 0, Depth).
|
|
|
|
|
|
|
|
depth(Var, Acc, MaxSoFar, Depth) :-
|
|
|
|
var(Var),
|
|
|
|
!,
|
|
|
|
( Acc > MaxSoFar ->
|
|
|
|
Depth = Acc
|
|
|
|
; Depth = MaxSoFar
|
|
|
|
).
|
|
|
|
depth(Atomic, Acc, MaxSoFar, Depth) :-
|
|
|
|
atomic(Atomic),
|
|
|
|
!,
|
|
|
|
( Acc > MaxSoFar ->
|
|
|
|
Depth = Acc
|
|
|
|
; Depth = MaxSoFar
|
|
|
|
).
|
|
|
|
depth([Arg| Args], Acc, MaxSoFar, Depth) :-
|
|
|
|
!,
|
|
|
|
depth(Arg, Acc, MaxSoFar, ArgDepth),
|
|
|
|
depth(Args, Acc, ArgDepth, Depth).
|
|
|
|
depth(Term, Acc, MaxSoFar, Depth) :-
|
|
|
|
Acc2 is Acc + 1,
|
|
|
|
Term =.. [_| Args],
|
|
|
|
depth(Args, Acc2, MaxSoFar, Depth).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
ground(Term) :-
|
|
|
|
nonvar(Term),
|
|
|
|
functor(Term, _, Arity),
|
|
|
|
ground(Arity, Term).
|
|
|
|
|
|
|
|
ground(0, _) :-
|
|
|
|
!.
|
|
|
|
ground(N, Term) :-
|
|
|
|
N > 0,
|
|
|
|
arg(N, Term, Arg),
|
|
|
|
ground(Arg),
|
|
|
|
N2 is N - 1,
|
|
|
|
ground(N2, Term).
|
|
|
|
|
|
|
|
occurs(Var, Term) :-
|
2007-06-12 11:39:47 +01:00
|
|
|
( var(Term) ->
|
2001-06-06 20:40:57 +01:00
|
|
|
Var == Term
|
2007-06-12 11:39:47 +01:00
|
|
|
; functor(Term, _, Arity),
|
|
|
|
occurs(Arity, Var, Term)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
occurs(N, Var, Term) :-
|
|
|
|
compound(Term),
|
|
|
|
arg(N, Term, Arg),
|
|
|
|
occurs(Var, Arg),
|
|
|
|
!.
|
|
|
|
occurs(N, Var, Term) :-
|
|
|
|
N > 1,
|
|
|
|
N2 is N - 1,
|
|
|
|
occurs(N2, Var, Term).
|
|
|
|
|
|
|
|
subsumes(General, Specific) :-
|
|
|
|
vars(Specific, Vars),
|
|
|
|
subsumes(General, Specific, Vars).
|
|
|
|
|
|
|
|
subsumes(General, Specific, Vars) :-
|
|
|
|
var(General),
|
|
|
|
!,
|
2007-06-12 11:39:47 +01:00
|
|
|
( var_member_chk(General, Vars) ->
|
2001-06-06 20:40:57 +01:00
|
|
|
General == Specific
|
2007-06-12 11:39:47 +01:00
|
|
|
; General = Specific
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
subsumes(General, Specific, Vars) :-
|
|
|
|
nonvar(Specific),
|
|
|
|
functor(General, Functor, Arity),
|
|
|
|
functor(Specific, Functor, Arity),
|
|
|
|
subsumes(Arity, General, Specific, Vars).
|
|
|
|
|
2007-06-12 11:39:47 +01:00
|
|
|
subsumes(0, _, _, _) :-
|
|
|
|
!.
|
2001-06-06 20:40:57 +01:00
|
|
|
subsumes(N, General, Specific, Vars) :-
|
|
|
|
arg(N, General, GenArg),
|
|
|
|
arg(N, Specific, SpeArg),
|
|
|
|
subsumes(GenArg, SpeArg, Vars),
|
|
|
|
M is N-1, !,
|
|
|
|
subsumes(M, General, Specific, Vars).
|
|
|
|
|
|
|
|
var_member_chk(Var, [Head| Tail]) :-
|
2007-06-12 11:39:47 +01:00
|
|
|
( Var == Head ->
|
2001-06-06 20:40:57 +01:00
|
|
|
true
|
2007-06-12 11:39:47 +01:00
|
|
|
; var_member_chk(Var, Tail)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
subterm(Term, Term).
|
|
|
|
subterm(Sub, Term) :-
|
|
|
|
nonvar(Term),
|
|
|
|
functor(Term, _, N),
|
|
|
|
subterm(N, Sub, Term).
|
|
|
|
|
|
|
|
subterm(N, Sub, Term) :-
|
|
|
|
compound(Term),
|
|
|
|
arg(N, Term, Arg),
|
|
|
|
subterm(Sub, Arg).
|
|
|
|
subterm(N, Sub, Term) :-
|
|
|
|
N > 1,
|
|
|
|
M is N-1,
|
|
|
|
subterm(M, Sub, Term).
|
|
|
|
|
|
|
|
valid(_).
|
|
|
|
|
2007-06-12 11:39:47 +01:00
|
|
|
variant(Term1, Term2) :-
|
|
|
|
\+ \+ subsumes(Term1, Term2),
|
|
|
|
\+ \+ subsumes(Term2, Term1).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
vars(Term, Vars) :-
|
|
|
|
vars(Term, [], Vars).
|
|
|
|
|
|
|
|
vars(Term, Acc, Vars) :-
|
2007-06-12 11:39:47 +01:00
|
|
|
( var(Term) ->
|
|
|
|
( var_member_chk(Term, Acc) ->
|
2001-06-06 20:40:57 +01:00
|
|
|
Vars = Acc
|
2007-06-12 11:39:47 +01:00
|
|
|
; Vars = [Term| Acc]
|
|
|
|
)
|
|
|
|
; Term =.. [_| Args],
|
|
|
|
var_list(Args, Acc, Vars)
|
|
|
|
).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
var_list([], Vars, Vars).
|
|
|
|
var_list([Term| Terms], Acc, Vars) :-
|
|
|
|
vars(Term, Acc, Acc2),
|
|
|
|
var_list(Terms, Acc2, Vars).
|
|
|
|
|
|
|
|
:- end_object.
|