1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Author
: Tom Schrijvers
3 % Email
: Tom
.Schrijvers
@cs.kuleuven
.ac
.be
4 % Copyright
: K
.U
.Leuven
2004
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %% / ___|___ __| | ___ / ___
| | ___ __ _ _ __
(_
)_ __ __ _
8 %% | | / _ \ / _
` |/ _ \ | | | |/ _ \/ _` | '_ \| | '_ \
/ _
` |
9 %% | |__| (_) | (_| | __/ | |___| | __/ (_| | | | | | | | | (_| |
10 %% \____\___/ \__,_|\___| \____|_|\___|\__,_|_| |_|_|_| |_|\__, |
13 %% removes redundant 'true's and other trivial but potentially non-free constructs
16 % Remove last clause with Body = fail
23 :- use_module(hprolog, [memberchk_eq/2]).
26 clean_clauses([C|Cs],[NC|NCs]) :-
28 clean_clauses(Cs,NCs).
30 clean_clause(Clause,NClause) :-
31 ( Clause = (Head :- Body) ->
32 clean_goal(Body,Body1),
33 move_unification_into_head(Head,Body1,NHead,NBody),
37 NClause = (NHead :- NBody)
43 clean_goal(Goal,NGoal) :-
46 clean_goal((G1,G2),NGoal) :-
57 clean_goal((If -> Then ; Else),NGoal) :-
61 clean_goal(Then,NThen),
64 clean_goal(Else,NElse),
67 clean_goal(Then,NThen),
68 clean_goal(Else,NElse),
69 NGoal = (NIf -> NThen; NElse)
71 clean_goal((G1 ; G2),NGoal) :-
82 clean_goal(once(G),NGoal) :-
92 clean_goal((G1 -> G2),NGoal) :-
103 clean_goal(Goal,Goal).
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 move_unification_into_head(Head,Body,NHead,NBody) :-
106 conj2list(Body,BodyList),
107 move_unification_into_head_(BodyList,Head,NHead,NBody).
109 move_unification_into_head_([],Head,Head,true).
110 move_unification_into_head_([G|Gs],Head,NHead,NBody) :-
111 ( nonvar(G), G = (X = Y) ->
112 term_variables(Gs,GsVars),
113 ( var(X), ( \+ memberchk_eq(X,GsVars) ; atomic(Y)) ->
115 move_unification_into_head_(Gs,Head,NHead,NBody)
116 ; var(Y), (\+ memberchk_eq(Y,GsVars) ; atomic(X)) ->
118 move_unification_into_head_(Gs,Head,NHead,NBody)
121 list2conj([G|Gs],NBody)
125 list2conj([G|Gs],NBody)
128 % move_unification_into_head(Head,Body,NHead,NBody) :-
129 % ( Body = (X = Y, More) ; Body = (X = Y), More = true), !,
130 % ( var(X), term_variables(More,MoreVars), \+ memberchk_eq(X,MoreVars) ->
132 % move_unification_into_head(Head,More,NHead,NBody)
134 % move_unification_into_head(Head,(Y = X,More),NHead,NBody)
140 % move_unification_into_head(Head,Body,Head,Body).
143 conj2list(Conj,L) :- %% transform conjunctions to list
144 conj2list(Conj,L,[]).
146 conj2list(Conj,L,T) :-
149 conj2list(Conj,L,T) :-
153 conj2list(G,[G | T],T).
156 list2conj([G],X) :- !, X = G.
157 list2conj([G|Gs],C) :-
158 ( G == true -> %% remove some redundant trues