3 Part of CHR
(Constraint Handling Rules
)
5 Author
: Tom Schrijvers
and Jan Wielemaker
6 E
-mail
: Tom
.Schrijvers
@cs.kuleuven
.be
7 WWW
: http
://www
.swi
-prolog
.org
8 Copyright
(C
): 2003-2004, 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
.
36 op
(1150, fx
, constraints
),
37 op
(1150, fx
, chr_constraint
),
38 op
(1150, fx
, handler
),
42 op
(1190, xfx
, pragma
),
44 op
(1150, fx
, chr_type
),
47 chr_show_store
/1, % +Module
48 find_chr_constraint
/1, % +Pattern
54 :- set_prolog_flag
(generate_debug_info
, false
).
56 :- multifile user
:file_search_path
/2.
57 :- dynamic user
:file_search_path
/2.
58 :- dynamic chr_translated_program
/1.
60 user
:file_search_path
(chr, library
(chr)).
62 :- load_files
([ library
(gensym
),
66 chr(chr_hashtable_store
),
67 chr(chr_compiler_errors
)
73 :- use_module
(library
(lists
),[member
/2]).
85 %% :- op
(1180, xfx
, ==>),
86 %% op
(1180, xfx
, <=>),
87 %% op
(1150, fx
, constraints
),
88 %% op
(1150, fx
, handler
),
89 %% op
(1150, fx
, rules
),
92 %% op
(1190, xfx
, pragma
),
94 %% op
(1150, fx
, chr_type
),
95 %% op
(1130, xfx
, --->),
98 %% :- multifile user
:file_search_path
/2.
99 %% :- dynamic chr_translated_program
/1.
101 %% user
:file_search_path
(chr, library
(chr)).
104 %% :- use_module
('chr/chr_translate').
105 %% :- use_module
('chr/chr_runtime').
106 %% :- use_module
('chr/chr_hashtable_store').
107 %% :- use_module
('chr/hprolog').
110 :- multifile
chr:'$chr_module'/1.
113 chr_term
/2. % File
, Term
115 % chr_expandable
(+Term
)
117 % Succeeds
if Term is a rule that must be handled by the CHR
118 % compiler
. Ideally CHR definitions should be between
122 % :- end_constraints
.
124 % As they are
not we have to
use some heuristics
. We assume any
125 % file is a CHR after we
've seen :- constraints ...
127 chr_expandable((:- constraints _)).
128 chr_expandable((constraints _)).
129 chr_expandable((:- chr_constraint _)).
130 chr_expandable((:- chr_type _)).
131 chr_expandable((chr_type _)).
132 chr_expandable(option(_, _)).
133 chr_expandable((:- chr_option(_, _))).
134 chr_expandable((handler _)).
135 chr_expandable((rules _)).
136 chr_expandable((_ <=> _)).
137 chr_expandable((_ @ _)).
138 chr_expandable((_ ==> _)).
139 chr_expandable((_ pragma _)).
141 % chr_expand(+Term, -Expansion)
143 % Extract CHR declarations and rules from the file and run the
144 % CHR compiler when reaching end-of-file.
147 extra_declarations([(:- use_module(chr(chr_runtime))),
148 (:- style_check(-discontiguous)), % no need to restore; file ends
149 (:- set_prolog_flag(generate_debug_info, false))
154 %% extra_declarations([(:-use_module(chr(chr_runtime)))
155 %% , (:- use_module(chr(hprolog),[term_variables/2,term_variables/3]))
156 %% , (:-use_module(chr(hpattvars)))
160 chr_expand(Term, []) :-
161 chr_expandable(Term), !,
162 prolog_load_context(file,File),
163 assert(chr_term(File, Term)).
164 chr_expand(end_of_file, FinalProgram) :-
165 extra_declarations(FinalProgram,Program),
166 prolog_load_context(file,File),
167 findall(T, retract(chr_term(File, T)), CHR0),
169 prolog_load_context(module, Module),
170 add_debug_decl(CHR0, CHR1),
171 add_optimise_decl(CHR1, CHR),
172 catch(call_chr_translate(File,
173 [ (:- module(Module, []))
178 ( chr_compiler_errors:print_chr_error(Error),
182 delete_header(Program0, Program).
185 delete_header([(:- module(_,_))|T0], T) :- !,
186 delete_header(T0, T).
189 add_debug_decl(CHR, CHR) :-
190 member(option(Name, _), CHR), Name == debug, !.
191 add_debug_decl(CHR, CHR) :-
192 member((:- chr_option(Name, _)), CHR), Name == debug, !.
193 add_debug_decl(CHR, [(:- chr_option(debug, Debug))|CHR]) :-
194 ( chr_current_prolog_flag(generate_debug_info, true)
200 chr_current_prolog_flag(Flag,Val) :- current_prolog_flag(Flag,Val).
203 add_optimise_decl(CHR, CHR) :-
204 \+(\+(memberchk((:- chr_option(optimize, _)), CHR))), !.
205 add_optimise_decl(CHR, [(:- chr_option(optimize, full))|CHR]) :-
206 chr_current_prolog_flag(optimize, full), !.
207 add_optimise_decl(CHR, CHR).
210 % call_chr_translate(+File, +In, -Out)
212 % The entire chr_translate/2 translation may fail, in which case we'd
213 % better issue a warning rather than simply ignoring the CHR
216 call_chr_translate
(_
, In
, _Out
) :-
217 ( chr_translate
(In
, Out0
) ->
218 nb_setval
(chr_translated_program
,Out0
),
221 call_chr_translate
(_
, _In
, Out
) :-
222 nb_current
(chr_translated_program
,Out
), !,
223 nb_delete
(chr_translated_program
).
225 call_chr_translate
(File
, _
, []) :-
226 print_message
(error
, chr(compilation_failed
(File
))).
230 /*******************************
231 * SYNCHRONISE TRACER
*
232 *******************************/
237 chr:debug_interact
/3.
241 user
:message_hook
(trace_mode
(OnOff
), _
, _
) :-
246 fail
. % backtrack to other handlers
248 % chr:debug_event
(+State
, +Event
)
250 % Hook into the CHR debugger
. At this moment we will discard CHR
251 % events
if we are
in a Prolog
`skip' and we ignore the
253 chr:debug_event(_State, _Event) :-
254 tracing, % are we tracing?
255 prolog_skip_level(Skip, Skip),
257 prolog_current_frame(Me),
258 prolog_frame_attribute(Me, level, Level),
261 % chr:debug_interact(+Event, +Depth, -Command)
263 % Hook into the CHR debugger to display Event and ask for the next
264 % command to execute. This definition causes the normal Prolog
265 % debugger to be used for the standard ports.
267 chr:debug_interact(Event, _Depth, creep) :-
271 prolog_event(call(_)).
272 prolog_event(exit(_)).
273 prolog_event(fail(_)).
278 /*******************************
280 *******************************/
285 prolog:message(chr(CHR)) -->
288 /*******************************
289 * TOPLEVEL PRINTING *
290 *******************************/
292 :- set_prolog_flag(chr_toplevel_show_store,true).
294 prolog:message(query(YesNo)) --> !,
295 ['~@'-[chr:print_all_stores]],
296 '$messages':prolog_message(query(YesNo)).
298 prolog:message(query(YesNo,Bindings)) --> !,
299 ['~@'-[chr:print_all_stores]],
300 '$messages':prolog_message(query(YesNo,Bindings)).
303 ( chr_current_prolog_flag(chr_toplevel_show_store,true),
304 catch(nb_getval(chr_global, _), _, fail),
305 chr:'$chr_module'(Mod),
312 /*******************************
314 *******************************/
316 :- multifile user:term_expansion/2.
317 :- dynamic user:term_expansion/2.
319 user:term_expansion(In, Out) :-
326 % current_toplevel_show_store/1,
327 % current_generate_debug_info/1,
328 % current_optimize/1.
330 % current_toplevel_show_store(on).
332 % current_generate_debug_info(false).
334 % current_optimize(off).
336 % chr_current_prolog_flag(generate_debug_info, X) :-
337 % chr_flag(generate_debug_info, X, X).
338 % chr_current_prolog_flag(optimize, X) :-
339 % chr_flag(optimize, X, X).
341 % chr_flag(Flag, Old, New) :-
342 % Goal = chr_flag(Flag,Old,New),
343 % g must_be(Flag, oneof([toplevel_show_store,generate_debug_info,optimize]), Goal, 1),
344 % chr_flag(Flag, Old, New, Goal).
346 % chr_flag(toplevel_show_store, Old, New, Goal) :-
347 % clause(current_toplevel_show_store(Old), true, Ref),
349 % ; must_be(New, oneof([on,off]), Goal, 3),
351 % assertz(current_toplevel_show_store(New))
353 % chr_flag(generate_debug_info, Old, New, Goal) :-
354 % clause(current_generate_debug_info(Old), true, Ref),
356 % ; must_be(New, oneof([false,true]), Goal, 3),
358 % assertz(current_generate_debug_info(New))
360 % chr_flag(optimize, Old, New, Goal) :-
361 % clause(current_optimize(Old), true, Ref),
363 % ; must_be(New, oneof([full,off]), Goal, 3),
365 % assertz(current_optimize(New))
369 % all_stores_goal(Goal, CVAs) :-
370 % chr_flag(toplevel_show_store, on, on), !,
371 % findall(C-CVAs, find_chr_constraint(C), Pairs),
372 % andify(Pairs, Goal, CVAs).
373 % all_stores_goal(true, _).
375 % andify([], true, _).
376 % andify([X-Vs|L], Conj, Vs) :- andify(L, X, Conj, Vs).
378 % andify([], X, X, _).
379 % andify([Y-Vs|L], X, (X,Conj), Vs) :- andify(L, Y, Conj, Vs).
381 % :- multifile user:term_expansion/6.
383 % user:term_expansion(In, _, Ids, Out, [], [chr|Ids]) :-
385 % nonmember(chr, Ids),
386 % chr_expand(In, Out), !.