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).
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)
39 ; Clause = '$source_location'(File,Line) : ActualClause ->
40 NClause = '$source_location'(File,Line) : NActualClause,
41 clean_clause(ActualClause,NActualClause)
46 clean_goal(Goal,NGoal) :-
49 clean_goal((G1,G2),NGoal) :-
60 clean_goal((If -> Then ; Else),NGoal) :-
64 clean_goal(Then,NThen),
67 clean_goal(Else,NElse),
70 clean_goal(Then,NThen),
71 clean_goal(Else,NElse),
72 NGoal = (NIf -> NThen; NElse)
74 clean_goal((G1 ; G2),NGoal) :-
85 clean_goal(once(G),NGoal) :-
95 clean_goal((G1 -> G2),NGoal) :-
106 clean_goal(Goal,Goal).
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 move_unification_into_head(Head,Body,NHead,NBody) :-
109 conj2list(Body,BodyList),
110 move_unification_into_head_(BodyList,Head,NHead,NBody).
112 move_unification_into_head_([],Head,Head,true).
113 move_unification_into_head_([G|Gs],Head,NHead,NBody) :-
114 ( nonvar(G), G = (X = Y) ->
115 term_variables(Gs,GsVars),
116 ( var(X), ( \+ memberchk_eq(X,GsVars) ; atomic(Y)) ->
118 move_unification_into_head_(Gs,Head,NHead,NBody)
119 ; var(Y), (\+ memberchk_eq(Y,GsVars) ; atomic(X)) ->
121 move_unification_into_head_(Gs,Head,NHead,NBody)
124 list2conj([G|Gs],NBody)
128 list2conj([G|Gs],NBody)
131 % move_unification_into_head(Head,Body,NHead,NBody) :-
132 % ( Body = (X = Y, More) ; Body = (X = Y), More = true), !,
133 % ( var(X), term_variables(More,MoreVars), \+ memberchk_eq(X,MoreVars) ->
135 % move_unification_into_head(Head,More,NHead,NBody)
137 % move_unification_into_head(Head,(Y = X,More),NHead,NBody)
143 % move_unification_into_head(Head,Body,Head,Body).
146 conj2list(Conj,L) :- %% transform conjunctions to list
147 conj2list(Conj,L,[]).
152 conj2list(true,L,L) :- !.
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