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).
25 clean_clauses(Clauses,NClauses) :-
26 clean_clauses1(Clauses,Clauses1),
27 merge_clauses(Clauses1,NClauses).
30 clean_clauses1([],[]).
31 clean_clauses1([C|Cs],[NC|NCs]) :-
33 clean_clauses1(Cs,NCs).
35 clean_clause(Clause,NClause) :-
36 ( Clause = (Head :- Body) ->
37 clean_goal(Body,Body1),
38 move_unification_into_head(Head,Body1,NHead,NBody),
42 NClause = (NHead :- NBody)
44 ; Clause = '$source_location'(File,Line) : ActualClause ->
45 NClause = '$source_location'(File,Line) : NActualClause,
46 clean_clause(ActualClause,NActualClause)
51 clean_goal(Goal,NGoal) :-
54 clean_goal((G1,G2),NGoal) :-
65 clean_goal((If -> Then ; Else),NGoal) :-
69 clean_goal(Then,NThen),
72 clean_goal(Else,NElse),
75 clean_goal(Then,NThen),
76 clean_goal(Else,NElse),
77 NGoal = (NIf -> NThen; NElse)
79 clean_goal((G1 ; G2),NGoal) :-
90 clean_goal(once(G),NGoal) :-
100 clean_goal((G1 -> G2),NGoal) :-
111 clean_goal(Goal,Goal).
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 move_unification_into_head(Head,Body,NHead,NBody) :-
114 conj2list(Body,BodyList),
115 move_unification_into_head_(BodyList,Head,NHead,NBody).
117 move_unification_into_head_([],Head,Head,true).
118 move_unification_into_head_([G|Gs],Head,NHead,NBody) :-
119 ( nonvar(G), G = (X = Y) ->
120 term_variables(Gs,GsVars),
121 ( var(X), ( \+ memberchk_eq(X,GsVars) ; atomic(Y)) ->
123 move_unification_into_head_(Gs,Head,NHead,NBody)
124 ; var(Y), (\+ memberchk_eq(Y,GsVars) ; atomic(X)) ->
126 move_unification_into_head_(Gs,Head,NHead,NBody)
129 list2conj([G|Gs],NBody)
133 list2conj([G|Gs],NBody)
136 % move_unification_into_head(Head,Body,NHead,NBody) :-
137 % ( Body = (X = Y, More) ; Body = (X = Y), More = true), !,
138 % ( var(X), term_variables(More,MoreVars), \+ memberchk_eq(X,MoreVars) ->
140 % move_unification_into_head(Head,More,NHead,NBody)
142 % move_unification_into_head(Head,(Y = X,More),NHead,NBody)
148 % move_unification_into_head(Head,Body,Head,Body).
151 conj2list(Conj,L) :- %% transform conjunctions to list
152 conj2list(Conj,L,[]).
157 conj2list(true,L,L) :- !.
158 conj2list(Conj,L,T) :-
162 conj2list(G,[G | T],T).
165 list2conj([G],X) :- !, X = G.
166 list2conj([G|Gs],C) :-
167 ( G == true -> %% remove some redundant trues
175 merge_clauses([],[]).
176 merge_clauses([C],[C]).
177 merge_clauses([X,Y|Clauses],NClauses) :-
178 ( merge_two_clauses(X,Y,Clause) ->
179 merge_clauses([Clause|Clauses],NClauses)
181 NClauses = [X|RClauses],
182 merge_clauses([Y|Clauses],RClauses)
186 merge_two_clauses(H1 :- B1, H2 :- B2, H :- B) :-
191 merge_lists(List1,List2,H1,H2,Unifier,List,NList1,NList2),
195 list2conj(List,Prefix),
196 list2conj(NList1,NB1),
200 list2conj(NList2,NB2),
201 B = (Prefix,(NB1 ; NB2))
204 merge_lists([],[],_,_,true,[],[],[]).
205 merge_lists([],L2,_,_,true,[],[],L2).
206 merge_lists([!|Xs],_,_,_,true,[!|Xs],[],!) :- !.
207 merge_lists([X|Xs],[],_,_,true,[],[X|Xs],[]).
208 merge_lists([X|Xs],[Y|Ys],H1,H2,Unifier,Common,N1,N2) :-
210 Unifier = (X = Y, RUnifier),
211 Common = [X|NCommon],
212 merge_lists(Xs,Ys,H1/X,H2/Y,RUnifier,NCommon,N1,N2)