3 Part of CHR
(Constraint Handling Rules
)
6 E
-mail
: Tom
.Schrijvers
@cs.kuleuven
.be
7 WWW
: http
://www
.swi
-prolog
.org
8 Copyright
(C
): 2005-2006, K
.U
. Leuven
10 This program is free software
; you can redistribute it
and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation
; either version
2
13 of the License
, or (at your option
) any later version
.
15 This program is distributed
in the hope that it will be useful
,
16 but WITHOUT ANY WARRANTY
; without even the implied warranty of
17 MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
. See the
18 GNU General Public License
for more details
.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library
; if not, write to the Free Software
22 Foundation
, Inc
., 59 Temple Place
, Suite
330, Boston
, MA
02111-1307 USA
24 As a special exception
, if you
link this library with other files
,
25 compiled with a Free Software compiler
, to produce an executable
, this
26 library does
not by itself cause the resulting executable to be covered
27 by the GNU General Public License
. This exception does
not however
28 invalidate any other reasons why the executable file might be covered by
29 the GNU General Public License
.
31 :- module
(chr_compiler_utility
,
40 , variable_replacement
/3
41 , variable_replacement
/4
43 , copy_with_variable_replacement
/3
55 :- use_module
(pairlist
).
56 :- use_module
(library
(lists
), [permutation
/2]).
59 %% use_module
(library
(terms
),[term_variables
/2]).
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 term_variables
(AC
,AVars
),
68 term_variables
(BC
,BVars
),
74 is_variant1
([X
|Xs
]) :-
80 is_variant2
([X
|Xs
]) :-
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 statistics
(runtime
,[T1
|_
]),
88 statistics
(runtime
,[T2
|_
]),
90 format
(' ~w:\t\t~w ms\n',[Phase
,T
]).
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 pair_all_with
([],_
,[]).
104 pair_all_with
([X
|Xs
],Y
,[X
-Y
|Rest
]) :-
105 pair_all_with
(Xs
,Y
,Rest
).
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 conj2list
(Conj
,L
) :- %% transform conjunctions to list
109 conj2list
(Conj
,L
,[]).
111 conj2list
(Var
,L
,T
) :-
114 conj2list
(Conj
,L
,T
) :-
117 conj2list
(Conj
,L
,T
) :-
121 conj2list
(G
,[G
| T
],T
).
123 disj2list
(Conj
,L
) :- %% transform disjunctions to list
124 disj2list
(Conj
,L
,[]).
125 disj2list
(Conj
,L
,T
) :-
128 disj2list
(Conj
,L
,T
) :-
132 disj2list
(G
,[G
| T
],T
).
135 list2conj
([G
],X
) :- !, X
= G
.
136 list2conj
([G
|Gs
],C
) :-
137 ( G
== true
-> %% remove some redundant trues
145 list2disj
([G
],X
) :- !, X
= G
.
146 list2disj
([G
|Gs
],C
) :-
147 ( G
== fail
-> %% remove some redundant fails
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % check wether two rules are identical
157 identical_rules
(rule
(H11
,H21
,G1
,B1
),rule
(H12
,H22
,G2
,B2
)) :-
159 identical_bodies
(B1
,B2
),
165 identical_bodies
(B1
,B2
) :-
177 % replace variables
in list
179 copy_with_variable_replacement
(X
,Y
,L
) :-
181 ( lookup_eq
(L
,X
,Y
) ->
189 copy_with_variable_replacement_l
(XArgs
,YArgs
,L
)
192 copy_with_variable_replacement_l
([],[],_
).
193 copy_with_variable_replacement_l
([X
|Xs
],[Y
|Ys
],L
) :-
194 copy_with_variable_replacement
(X
,Y
,L
),
195 copy_with_variable_replacement_l
(Xs
,Ys
,L
).
197 %% build variable replacement list
199 variable_replacement
(X
,Y
,L
) :-
200 variable_replacement
(X
,Y
,[],L
).
202 variable_replacement
(X
,Y
,L1
,L2
) :-
205 ( lookup_eq
(L1
,X
,Z
) ->
208 ; ( X
== Y
-> L2
=L1
; L2
= [X
-Y
,Y
-X
|L1
])
213 variable_replacement_l
(XArgs
,YArgs
,L1
,L2
)
216 variable_replacement_l
([],[],L
,L
).
217 variable_replacement_l
([X
|Xs
],[Y
|Ys
],L1
,L3
) :-
218 variable_replacement
(X
,Y
,L1
,L2
),
219 variable_replacement_l
(Xs
,Ys
,L2
,L3
).
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 my_term_copy
(X
,Dict
,Y
) :-
223 my_term_copy
(X
,Dict
,_
,Y
).
225 my_term_copy
(X
,Dict1
,Dict2
,Y
) :-
227 ( lookup_eq
(Dict1
,X
,Y
) ->
229 ; Dict2
= [X
-Y
|Dict1
]
235 my_term_copy_list
(XArgs
,Dict1
,Dict2
,YArgs
)
238 my_term_copy_list
([],Dict
,Dict
,[]).
239 my_term_copy_list
([X
|Xs
],Dict1
,Dict3
,[Y
|Ys
]) :-
240 my_term_copy
(X
,Dict1
,Dict2
,Y
),
241 my_term_copy_list
(Xs
,Dict2
,Dict3
,Ys
).
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 atom_concat_list
([X
],X
) :- ! .
245 atom_concat_list
([X
|Xs
],A
) :-
246 atom_concat_list
(Xs
,B
),
247 atomic_concat
(X
,B
,A
).
249 atomic_concat
(A
,B
,C
) :-
252 atom_concat
(AA
,BB
,C
).
267 set_elems
([X
|Xs
],X
) :-
272 init
([X
|Xs
],[X
|R
]) :-
275 member2
([X
|_
],[Y
|_
],X
-Y
).
276 member2
([_
|Xs
],[_
|Ys
],P
) :-
279 select2
(X
, Y
, [X
|Xs
], [Y
|Ys
], Xs
, Ys
).
280 select2
(X
, Y
, [X1
|Xs
], [Y1
|Ys
], [X1
|NXs
], [Y1
|NYs
]) :-
281 select2
(X
, Y
, Xs
, Ys
, NXs
, NYs
).
283 instrument_goal
(Goal
,Pre
,Post
,(Pre
,Goal
,Post
)).