1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Author
: Tom Schrijvers
3 % Email
: Tom
.Schrijvers
@cs.kuleuven
.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 %% :- use_module(library(terms),[term_variables/2]).
30 clean_clauses([C|Cs],[NC|NCs]) :-
32 clean_clauses(Cs,NCs).
34 clean_clause(Clause,NClause) :-
35 ( Clause = (Head :- Body) ->
36 clean_goal(Body,Body1),
37 move_unification_into_head(Head,Body1,NHead,NBody),
41 NClause = (NHead :- NBody)
47 clean_goal(Goal,NGoal) :-
50 clean_goal((G1,G2),NGoal) :-
61 clean_goal((If -> Then ; Else),NGoal) :-
65 clean_goal(Then,NThen),
68 clean_goal(Else,NElse),
71 clean_goal(Then,NThen),
72 clean_goal(Else,NElse),
73 NGoal = (NIf -> NThen; NElse)
75 clean_goal((G1 ; G2),NGoal) :-
86 clean_goal(once(G),NGoal) :-
96 clean_goal((G1 -> G2),NGoal) :-
107 clean_goal(Goal,Goal).
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 move_unification_into_head(Head,Body,NHead,NBody) :-
110 conj2list(Body,BodyList),
111 move_unification_into_head_(BodyList,Head,NHead,NBody).
113 move_unification_into_head_([],Head,Head,true).
114 move_unification_into_head_([G|Gs],Head,NHead,NBody) :-
115 ( nonvar(G), G = (X = Y) ->
116 term_variables(Gs,GsVars),
117 ( var(X), ( \+ memberchk_eq(X,GsVars) ; atomic(Y)) ->
119 move_unification_into_head_(Gs,Head,NHead,NBody)
120 ; var(Y), (\+ memberchk_eq(Y,GsVars) ; atomic(X)) ->
122 move_unification_into_head_(Gs,Head,NHead,NBody)
125 list2conj([G|Gs],NBody)
129 list2conj([G|Gs],NBody)
132 % move_unification_into_head(Head,Body,NHead,NBody) :-
133 % ( Body = (X = Y, More) ; Body = (X = Y), More = true), !,
134 % ( var(X), term_variables(More,MoreVars), \+ memberchk_eq(X,MoreVars) ->
136 % move_unification_into_head(Head,More,NHead,NBody)
138 % move_unification_into_head(Head,(Y = X,More),NHead,NBody)
144 % move_unification_into_head(Head,Body,Head,Body).
147 conj2list(Conj,L) :- %% transform conjunctions to list
148 conj2list(Conj,L,[]).
150 conj2list(Conj,L,T) :-
153 conj2list(Conj,L,T) :-
157 conj2list(G,[G | T],T).
160 list2conj([G],X) :- !, X = G.
161 list2conj([G|Gs],C) :-
162 ( G == true -> %% remove some redundant trues