* Deleted copy_term_nat/2 definition
[chr.git] / chr_translate_bootstrap1.pl
blobbe808ac8dde42c2ebae2992efa13fe27d6c58f32
1 /* Generated by CHR bootstrap compiler
2 From: chr_translate_bootstrap1.chr
3 Date: Fri Feb 18 13:54:42 2005
5 DO NOT EDIT. EDIT THE CHR FILE INSTEAD
6 */
8 :-module(chr_translate_bootstrap1, [chr_translate/2]).
9 :-use_module(chr_runtime).
10 :-style_check(-singleton).
11 :-style_check(- (discontiguous)).
12 :-use_module(library(lists)).
13 :-use_module(hprolog).
14 :-use_module(library(assoc)).
15 :-use_module(pairlist).
16 :-use_module(library(ordsets)).
17 :-include(chr_op2).
18 chr_translate(A, B) :-
19 init_chr_pp_flags,
20 partition_clauses(A, C, D, E),
21 ( C==[]
22 -> B=E
23 ; check_rules(D, C),
24 unique_analyse_optimise(D, F),
25 check_attachments(F),
26 set_constraint_indices(C, 1),
27 store_management_preds(C, G),
28 constraints_code(C, F, H),
29 append_lists([E, G, H], B)
31 chr_clear.
32 store_management_preds(A, B) :-
33 generate_attach_detach_a_constraint_all(A, C),
34 generate_attach_increment(D),
35 generate_attr_unify_hook(E),
36 append_lists([C, D, E], B).
37 partition_clauses([], [], [], []).
38 partition_clauses([A|B], C, D, E) :-
39 ( rule(A, F)
40 -> C=G,
41 D=[F|H],
42 E=I
43 ; is_declaration(A, J)
44 -> append(J, G, C),
45 D=H,
46 E=I
47 ; is_module_declaration(A, K)
48 -> target_module(K),
49 C=G,
50 D=H,
51 E=[A|I]
52 ; A=handler(L)
53 -> format('CHR compiler WARNING: ~w.\n', [A]),
54 format(' `--> SICStus compatibility: ignoring handler/1 declaration.\n', []),
55 C=G,
56 D=H,
57 E=I
58 ; A=rules(M)
59 -> format('CHR compiler WARNING: ~w.\n', [A]),
60 format(' `--> SICStus compatibility: ignoring rules/1 declaration.\n', []),
61 C=G,
62 D=H,
63 E=I
64 ; A=option(N, O)
65 -> handle_option(N, O),
66 C=G,
67 D=H,
68 E=I
69 ; C=G,
70 D=H,
71 E=[A|I]
73 partition_clauses(B, G, H, I).
74 is_declaration(A, B) :-
75 ( A= (:-C)
76 -> true
77 ; A=C
79 C=..[constraints, D],
80 conj2list(D, B).
81 rule(A, B) :-
82 A= @(C, D), !,
83 rule(D, yes(C), B).
84 rule(A, B) :-
85 rule(A, no, B).
86 rule(A, B, C) :-
87 A=pragma(D, E), !,
88 is_rule(D, F, G),
89 conj2list(E, H),
90 C=pragma(F, G, H, B).
91 rule(A, B, C) :-
92 is_rule(A, D, E),
93 C=pragma(D, E, [], B).
94 is_rule(A, B, C) :-
95 A= ==>(D, E), !,
96 conj2list(D, F),
97 get_ids(F, G, H),
98 C=ids([], G),
99 ( E= (I|J)
100 -> B=rule([], H, I, J)
101 ; B=rule([], H, true, E)
103 is_rule(A, B, C) :-
104 A= <=>(D, E), !,
105 ( E= (F|G)
106 -> H=F,
108 ; H=true,
111 ( D= \(J, K)
112 -> conj2list(J, L),
113 conj2list(K, M),
114 get_ids(L, N, O, 0, P),
115 get_ids(M, Q, R, P, S),
116 C=ids(Q, N)
117 ; conj2list(D, M),
118 O=[],
119 get_ids(M, Q, R),
120 C=ids(Q, [])
122 B=rule(R, O, H, I).
123 get_ids(A, B, C) :-
124 get_ids(A, B, C, 0, D).
125 get_ids([], [], [], A, A).
126 get_ids([A|B], [C|D], [E|F], C, G) :-
127 ( A= #(E, C)
128 -> true
129 ; E=A
131 H is C+1,
132 get_ids(B, D, F, H, G).
133 is_module_declaration((:-module(A)), A).
134 is_module_declaration((:-module(A, B)), A).
135 check_rules(A, B) :-
136 check_rules(A, B, 1).
137 check_rules([], A, B).
138 check_rules([A|B], C, D) :-
139 check_rule(A, C, D),
140 E is D+1,
141 check_rules(B, C, E).
142 check_rule(A, B, C) :-
143 A=pragma(D, E, F, G),
144 D=rule(H, I, J, K),
145 append(H, I, L),
146 check_head_constraints(L, B, A, C),
147 check_pragmas(F, A, C).
148 check_head_constraints([], A, B, C).
149 check_head_constraints([A|B], C, D, E) :-
150 functor(A, F, G),
151 ( member(F/G, C)
152 -> check_head_constraints(B, C, D, E)
153 ; format('CHR compiler ERROR: Undeclared constraint ~w in head of ~@.\n', [F/G, format_rule(D, E)]),
154 format(' `--> Constraint should be on of ~w.\n', [C]),
155 fail
157 check_pragmas([], A, B).
158 check_pragmas([A|B], C, D) :-
159 check_pragma(A, C, D),
160 check_pragmas(B, C, D).
161 check_pragma(A, B, C) :-
162 var(A), !,
163 format('CHR compiler ERROR: invalid pragma ~w in ~@.\n', [A, format_rule(B, C)]),
164 format(' `--> Pragma should not be a variable!\n', []),
165 fail.
166 check_pragma(passive(A), B, C) :- !,
167 B=pragma(D, ids(E, F), G, H),
168 ( memberchk_eq(A, E)
169 -> true
170 ; memberchk_eq(A, F)
171 -> true
172 ; format('CHR compiler ERROR: invalid identifier ~w in pragma passive in ~@.\n', [A, format_rule(B, C)]),
173 fail
175 check_pragma(A, B, C) :-
176 A=unique(D, E), !,
177 format('CHR compiler WARNING: undocument pragma ~w in ~@.\n', [A, format_rule(B, C)]),
178 format(' `--> Only use this pragma if you know what you are doing.\n', []).
179 check_pragma(A, B, C) :-
180 A=already_in_heads, !,
181 format('CHR compiler WARNING: currently unsupported pragma ~w in ~@.\n', [A, format_rule(B, C)]),
182 format(' `--> Pragma is ignored. Termination and correctness may be affected \n', []).
183 check_pragma(A, B, C) :-
184 A=already_in_head(D), !,
185 format('CHR compiler WARNING: currently unsupported pragma ~w in ~@.\n', [A, format_rule(B, C)]),
186 format(' `--> Pragma is ignored. Termination and correctness may be affected \n', []).
187 check_pragma(A, B, C) :-
188 format('CHR compiler ERROR: invalid pragma ~w in ~@.\n', [A, format_rule(B, C)]),
189 format(' `--> Pragma should be one of passive/1!\n', []),
190 fail.
191 format_rule(A, B) :-
192 A=pragma(C, D, E, F),
193 ( F=yes(G)
194 -> write('rule '),
195 write(G)
196 ; write('rule number '),
197 write(B)
199 handle_option(A, B) :-
200 var(A), !,
201 format('CHR compiler ERROR: ~w.\n', [option(A, B)]),
202 format(' `--> First argument should be an atom, not a variable.\n', []),
203 fail.
204 handle_option(A, B) :-
205 var(B), !,
206 format('CHR compiler ERROR: ~w.\n', [option(A, B)]),
207 format(' `--> Second argument should be a nonvariable.\n', []),
208 fail.
209 handle_option(A, B) :-
210 option_definition(A, B, C), !,
211 set_chr_pp_flags(C).
212 handle_option(A, B) :-
213 \+option_definition(A, C, D), !.
214 handle_option(A, B) :-
215 findall(C, option_definition(A, C, D), E),
216 format('CHR compiler ERROR: ~w.\n', [option(A, B)]),
217 format(' `--> Invalid value ~w: should be one of ~w.\n', [B, E]),
218 fail.
219 option_definition(optimize, experimental, A) :-
220 A=[unique_analyse_optimise-on, check_unnecessary_active-full, reorder_heads-on, set_semantics_rule-on, check_attachments-on, guard_via_reschedule-on].
221 option_definition(optimize, full, A) :-
222 A=[unique_analyse_optimise-on, check_unnecessary_active-full, reorder_heads-on, set_semantics_rule-on, check_attachments-on, guard_via_reschedule-on].
223 option_definition(optimize, sicstus, A) :-
224 A=[unique_analyse_optimise-off, check_unnecessary_active-simplification, reorder_heads-off, set_semantics_rule-off, check_attachments-off, guard_via_reschedule-off].
225 option_definition(optimize, off, A) :-
226 A=[unique_analyse_optimise-off, check_unnecessary_active-off, reorder_heads-off, set_semantics_rule-off, check_attachments-off, guard_via_reschedule-off].
227 option_definition(debug, off, A) :-
228 A=[debugable-off].
229 option_definition(debug, on, A) :-
230 A=[debugable-on].
231 option_definition(check_guard_bindings, on, A) :-
232 A=[guard_locks-on].
233 option_definition(check_guard_bindings, off, A) :-
234 A=[guard_locks-off].
235 init_chr_pp_flags :-
236 chr_pp_flag_definition(A, [B|C]),
237 set_chr_pp_flag(A, B),
238 fail.
239 init_chr_pp_flags.
240 set_chr_pp_flags([]).
241 set_chr_pp_flags([A-B|C]) :-
242 set_chr_pp_flag(A, B),
243 set_chr_pp_flags(C).
244 set_chr_pp_flag(A, B) :-
245 atom_concat('$chr_pp_', A, C),
246 nb_setval(C, B).
247 chr_pp_flag_definition(unique_analyse_optimise, [on, off]).
248 chr_pp_flag_definition(check_unnecessary_active, [full, simplification, off]).
249 chr_pp_flag_definition(reorder_heads, [on, off]).
250 chr_pp_flag_definition(set_semantics_rule, [on, off]).
251 chr_pp_flag_definition(guard_via_reschedule, [on, off]).
252 chr_pp_flag_definition(guard_locks, [on, off]).
253 chr_pp_flag_definition(check_attachments, [on, off]).
254 chr_pp_flag_definition(debugable, [off, on]).
255 chr_pp_flag(A, B) :-
256 atom_concat('$chr_pp_', A, C),
257 nb_getval(C, D),
258 ( D==[]
259 -> chr_pp_flag_definition(A, [B|E])
260 ; D=B
262 generate_attach_detach_a_constraint_all([], []).
263 generate_attach_detach_a_constraint_all([A|B], C) :-
264 ( is_attached(A)
265 -> generate_attach_a_constraint(A, D),
266 generate_detach_a_constraint(A, E)
267 ; D=[],
268 E=[]
270 generate_attach_detach_a_constraint_all(B, F),
271 append_lists([D, E, F], C).
272 generate_attach_a_constraint(A, [B, C]) :-
273 generate_attach_a_constraint_empty_list(A, B),
274 get_max_constraint_index(D),
275 ( D==1
276 -> generate_attach_a_constraint_1_1(A, C)
277 ; generate_attach_a_constraint_t_p(A, C)
279 generate_attach_a_constraint_empty_list(A/B, C) :-
280 atom_concat_list([attach_, A, /, B], D),
281 E=[[], F],
282 G=..[D|E],
283 C= (G:-true).
284 generate_attach_a_constraint_1_1(A/B, C) :-
285 atom_concat_list([attach_, A, /, B], D),
286 E=[[F|G], H],
287 I=..[D|E],
288 J=..[D, G, H],
289 get_target_module(K),
290 L= ((get_attr(F, K, M)->N=[H|M], put_attr(F, K, N);put_attr(F, K, [H])), J),
291 C= (I:-L).
292 generate_attach_a_constraint_t_p(A/B, C) :-
293 atom_concat_list([attach_, A, /, B], D),
294 E=[[F|G], H],
295 I=..[D|E],
296 J=..[D, G, H],
297 get_constraint_index(A/B, K),
298 or_pattern(K, L),
299 get_max_constraint_index(M),
300 make_attr(M, N, O, P),
301 nth(K, O, Q),
302 substitute(Q, O, [H|Q], R),
303 make_attr(M, N, R, S),
304 substitute(Q, O, [H], T),
305 make_attr(M, U, T, V),
306 copy_term(O, W),
307 nth(K, W, [H]),
308 chr_delete(W, [H], X),
309 set_elems(X, []),
310 make_attr(M, L, W, Y),
311 get_target_module(Z),
312 A1= ((get_attr(F, Z, B1)->B1=P, (N/\L=:=L->put_attr(F, Z, S);U is N\/L, put_attr(F, Z, V));put_attr(F, Z, Y)), J),
313 C= (I:-A1).
314 generate_detach_a_constraint(A, [B, C]) :-
315 generate_detach_a_constraint_empty_list(A, B),
316 get_max_constraint_index(D),
317 ( D==1
318 -> generate_detach_a_constraint_1_1(A, C)
319 ; generate_detach_a_constraint_t_p(A, C)
321 generate_detach_a_constraint_empty_list(A/B, C) :-
322 atom_concat_list([detach_, A, /, B], D),
323 E=[[], F],
324 G=..[D|E],
325 C= (G:-true).
326 generate_detach_a_constraint_1_1(A/B, C) :-
327 atom_concat_list([detach_, A, /, B], D),
328 E=[[F|G], H],
329 I=..[D|E],
330 J=..[D, G, H],
331 get_target_module(K),
332 L= ((get_attr(F, K, M)->'chr sbag_del_element'(M, H, N), (N==[]->del_attr(F, K);put_attr(F, K, N));true), J),
333 C= (I:-L).
334 generate_detach_a_constraint_t_p(A/B, C) :-
335 atom_concat_list([detach_, A, /, B], D),
336 E=[[F|G], H],
337 I=..[D|E],
338 J=..[D, G, H],
339 get_constraint_index(A/B, K),
340 or_pattern(K, L),
341 and_pattern(K, M),
342 get_max_constraint_index(N),
343 make_attr(N, O, P, Q),
344 nth(K, P, R),
345 substitute(R, P, [], S),
346 make_attr(N, T, S, U),
347 substitute(R, P, V, W),
348 make_attr(N, O, W, X),
349 get_target_module(Y),
350 Z= ((get_attr(F, Y, A1)->A1=Q, (O/\L=:=L->'chr sbag_del_element'(R, H, V), (V==[]->T is O/\M, (T==0->del_attr(F, Y);put_attr(F, Y, U));put_attr(F, Y, X));true);true), J),
351 C= (I:-Z).
352 generate_attach_increment([A, B]) :-
353 generate_attach_increment_empty(A),
354 get_max_constraint_index(C),
355 ( C==1
356 -> generate_attach_increment_one(B)
357 ; generate_attach_increment_many(C, B)
359 generate_attach_increment_empty((attach_increment([], A):-true)).
360 generate_attach_increment_one(A) :-
361 B=attach_increment([C|D], E),
362 get_target_module(F),
363 G= ('chr not_locked'(C), (get_attr(C, F, H)->sort(H, I), merge(E, I, J), put_attr(C, F, J);put_attr(C, F, E)), attach_increment(D, E)),
364 A= (B:-G).
365 generate_attach_increment_many(A, B) :-
366 make_attr(A, C, D, E),
367 make_attr(A, F, G, H),
368 I=attach_increment([J|K], E),
369 bagof(L, M^N^O^P^ (member2(D, G, M-N), L= (sort(N, O), 'chr merge_attributes'(M, O, P))), Q),
370 list2conj(Q, R),
371 bagof(S, T^U^V^member((T, 'chr merge_attributes'(U, V, S)), Q), W),
372 make_attr(A, X, W, Y),
373 get_target_module(Z),
374 A1= ('chr not_locked'(J), (get_attr(J, Z, B1)->B1=H, R, X is C\/F, put_attr(J, Z, Y);put_attr(J, Z, E)), attach_increment(K, E)),
375 B= (I:-A1).
376 generate_attr_unify_hook([A]) :-
377 get_max_constraint_index(B),
378 ( B==1
379 -> generate_attr_unify_hook_one(A)
380 ; generate_attr_unify_hook_many(B, A)
382 generate_attr_unify_hook_one(A) :-
383 B=C:attr_unify_hook(D, E),
384 get_target_module(C),
385 make_run_suspensions(F, G),
386 make_run_suspensions(D, H),
387 I= (sort(D, J), (var(E)-> (get_attr(E, C, K)->true;K=[]), sort(K, L), 'chr merge_attributes'(J, L, F), put_attr(E, C, F), G; (compound(E)->term_variables(E, M), attach_increment(M, J);true), H)),
388 A= (B:-I).
389 generate_attr_unify_hook_many(A, B) :-
390 make_attr(A, C, D, E),
391 make_attr(A, F, G, H),
392 bagof(I, J^K^ (member(J, D), I=sort(J, K)), L),
393 list2conj(L, M),
394 bagof(K, J^member(sort(J, K), L), N),
395 bagof(O, P^Q^R^S^ (member2(N, G, P-Q), O= (sort(Q, R), 'chr merge_attributes'(P, R, S))), T),
396 bagof(S, P^R^U^member((U, 'chr merge_attributes'(P, R, S)), T), V),
397 list2conj(T, W),
398 make_attr(A, X, V, Y),
399 make_attr(A, C, N, Z),
400 A1=B1:attr_unify_hook(E, C1),
401 get_target_module(B1),
402 make_run_suspensions_loop(V, D1),
403 make_run_suspensions_loop(N, E1),
404 F1= (M, (var(C1)-> (get_attr(C1, B1, G1)->G1=H, W, X is C\/F, put_attr(C1, B1, Y), D1;put_attr(C1, B1, Z), E1); (compound(C1)->term_variables(C1, H1), attach_increment(H1, Z);true), E1)),
405 B= (A1:-F1).
406 make_run_suspensions(A, B) :-
407 ( chr_pp_flag(debugable, on)
408 -> B='chr run_suspensions_d'(A)
409 ; B='chr run_suspensions'(A)
411 make_run_suspensions_loop(A, B) :-
412 ( chr_pp_flag(debugable, on)
413 -> B='chr run_suspensions_loop_d'(A)
414 ; B='chr run_suspensions_loop'(A)
416 check_attachments(A) :-
417 ( chr_pp_flag(check_attachments, on)
418 -> check_attachments_(A)
419 ; true
421 check_attachments_([]).
422 check_attachments_([A|B]) :-
423 check_attachment(A),
424 check_attachments_(B).
425 check_attachment(A) :-
426 A=pragma(B, C, D, E),
427 B=rule(F, G, H, I),
428 check_attachment_heads1(F, F, G, H),
429 check_attachment_heads2(G, F, I).
430 check_attachment_heads1([], A, B, C).
431 check_attachment_heads1([A|B], C, D, E) :-
432 functor(A, F, G),
433 ( C==[A],
434 D==[],
435 E==true,
436 A=..[H|I],
437 no_matching(I, [])
438 -> attached(F/G, no)
439 ; attached(F/G, maybe)
441 check_attachment_heads1(B, C, D, E).
442 no_matching([], A).
443 no_matching([A|B], C) :-
444 var(A),
445 \+memberchk_eq(A, C),
446 no_matching(B, [A|C]).
447 check_attachment_heads2([], A, B).
448 check_attachment_heads2([A|B], C, D) :-
449 functor(A, E, F),
450 ( C\==[],
451 D==true
452 -> attached(E/F, maybe)
453 ; attached(E/F, yes)
455 check_attachment_heads2(B, C, D).
456 all_attached([]).
457 all_attached([A|B]) :-
458 functor(A, C, D),
459 is_attached(C/D),
460 all_attached(B).
461 set_constraint_indices([], A) :-
462 B is A-1,
463 max_constraint_index(B).
464 set_constraint_indices([A|B], C) :-
465 ( is_attached(A)
466 -> constraint_index(A, C),
467 D is C+1,
468 set_constraint_indices(B, D)
469 ; set_constraint_indices(B, C)
471 constraints_code(A, B, C) :-
472 post_constraints(A, 1),
473 constraints_code1(1, B, D, []),
474 clean_clauses(D, C).
475 post_constraints([], A) :-
476 B is A-1,
477 constraint_count(B).
478 post_constraints([A/B|C], D) :-
479 constraint(A/B, D),
480 E is D+1,
481 post_constraints(C, E).
482 constraints_code1(A, B, C, D) :-
483 constraint_count(E),
484 ( A>E
485 -> D=C
486 ; constraint_code(A, B, C, F),
487 G is A+1,
488 constraints_code1(G, B, F, D)
490 constraint_code(A, B, C, D) :-
491 constraint(E, A),
492 constraint_prelude(E, F),
493 C=[F|G],
494 H=[0],
495 rules_code(B, 1, A, H, I, G, J),
496 gen_cond_attach_clause(E, I, J, D).
497 constraint_prelude(A/B, C) :-
498 vars_susp(B, D, E, F),
499 G=..[A|D],
500 build_head(A, B, [0], F, H),
501 get_target_module(I),
502 ( chr_pp_flag(debugable, on)
503 -> C= (G:-'chr allocate_constraint'(I:H, E, A, D), ('chr debug_event'(call(E)), H;'chr debug_event'(fail(E)), !, fail), ('chr debug_event'(exit(E));'chr debug_event'(redo(E)), fail))
504 ; C= (G:-H)
506 gen_cond_attach_clause(A/B, C, D, E) :-
507 ( is_attached(A/B)
508 -> ( C==[0]
509 -> gen_cond_attach_goal(A/B, F, G, H, I)
510 ; vars_susp(B, H, I, G),
511 gen_uncond_attach_goal(A/B, I, F, J)
513 ( chr_pp_flag(debugable, on)
514 -> K=..[A|H],
515 L='chr debug_event'(insert(#(K, I)))
516 ; L=true
518 build_head(A, B, C, G, M),
519 N= (M:-L, F),
520 D=[N|E]
521 ; D=E
523 gen_cond_attach_goal(A/B, C, D, E, F) :-
524 vars_susp(B, E, F, D),
525 build_head(A, B, [0], D, G),
526 atom_concat_list([attach_, A, /, B], H),
527 I=..[H, J, F],
528 get_target_module(K),
529 C= ((var(F)->'chr insert_constraint_internal'(J, F, K:G, A, E);'chr activate_constraint'(J, F, L)), I).
530 gen_uncond_attach_goal(A/B, C, D, E) :-
531 atom_concat_list([attach_, A, /, B], F),
532 G=..[F, H, C],
533 D= ('chr activate_constraint'(H, C, E), G).
534 rules_code([], A, B, C, C, D, D).
535 rules_code([A|B], C, D, E, F, G, H) :-
536 rule_code(A, C, D, E, I, G, J),
537 K is C+1,
538 rules_code(B, K, D, I, F, J, H).
539 rule_code(A, B, C, D, E, F, G) :-
540 A=pragma(H, I, J, K),
541 I=ids(L, M),
542 H=rule(N, O, P, Q),
543 heads1_code(N, [], L, [], A, C, D, F, R),
544 heads2_code(O, [], M, [], A, B, C, D, E, R, G).
545 heads1_code([], A, B, C, D, E, F, G, G).
546 heads1_code([A|B], C, [D|E], F, G, H, I, J, K) :-
547 G=pragma(L, M, N, O),
548 constraint(P/Q, H),
549 ( functor(A, P, Q),
550 \+check_unnecessary_active(A, C, L),
551 \+memberchk_eq(passive(D), N),
552 all_attached(B),
553 all_attached(C),
554 L=rule(R, S, T, U),
555 all_attached(S)
556 -> append(B, C, V),
557 append(E, F, W),
558 head1_code(A, V, W, G, P/Q, H, I, J, X)
559 ; J=X
561 heads1_code(B, [A|C], E, [D|F], G, H, I, X, K).
562 head1_code(A, B, C, D, E, F, G, H, I) :-
563 D=pragma(J, K, L, M),
564 J=rule(N, O, P, Q),
565 ( O==[]
566 -> reorder_heads(A, B, C, R, S),
567 simplification_code(A, R, S, D, E, G, H, I)
568 ; simpagation_head1_code(A, B, C, D, E, G, H, I)
570 heads2_code([], A, B, C, D, E, F, G, G, H, H).
571 heads2_code([A|B], C, [D|E], F, G, H, I, J, K, L, M) :-
572 G=pragma(N, O, P, Q),
573 constraint(R/S, I),
574 ( functor(A, R, S),
575 \+check_unnecessary_active(A, C, N),
576 \+memberchk_eq(passive(D), P),
577 \+set_semantics_rule(G),
578 all_attached(B),
579 all_attached(C),
580 N=rule(T, U, V, W),
581 all_attached(T)
582 -> append(B, C, X),
583 append(E, F, Y),
584 length(B, Z),
585 head2_code(A, X, Y, G, H, Z, R/S, J, L, A1),
586 inc_id(J, B1),
587 gen_alloc_inc_clause(R/S, J, A1, C1)
588 ; L=C1,
589 B1=J
591 heads2_code(B, [A|C], E, [D|F], G, H, I, B1, K, C1, M).
592 head2_code(A, B, C, D, E, F, G, H, I, J) :-
593 D=pragma(K, L, M, N),
594 K=rule(O, P, Q, R),
595 ( O==[]
596 -> reorder_heads(A, B, S),
597 propagation_code(A, S, K, E, F, G, H, I, J)
598 ; simpagation_head2_code(A, B, C, D, G, H, I, J)
600 gen_alloc_inc_clause(A/B, C, D, E) :-
601 vars_susp(B, F, G, H),
602 build_head(A, B, C, H, I),
603 inc_id(C, J),
604 build_head(A, B, J, H, K),
605 ( C==[0]
606 -> gen_cond_allocation(F, G, A/B, H, L)
607 ; L=true
609 M= (I:-L, K),
610 D=[M|E].
611 gen_cond_allocation(A, B, C/D, E, F) :-
612 build_head(C, D, [0], E, G),
613 get_target_module(H),
614 F= (var(B)->'chr allocate_constraint'(H:G, B, C, A);true).
615 guard_via_reschedule(A, B, C, D) :-
616 ( chr_pp_flag(guard_via_reschedule, on)
617 -> guard_via_reschedule_main(A, B, C, D)
618 ; append(A, B, E),
619 list2conj(E, D)
621 guard_via_reschedule_main(A, B, C, D) :-
622 initialize_unit_dictionary(C, E),
623 build_units(A, B, E, F),
624 dependency_reorder(F, G),
625 units2goal(G, D).
626 units2goal([], true).
627 units2goal([unit(A, B, C, D)|E], (B, F)) :-
628 units2goal(E, F).
629 dependency_reorder(A, B) :-
630 dependency_reorder(A, [], B).
631 dependency_reorder([], A, B) :-
632 reverse(A, B).
633 dependency_reorder([A|B], C, D) :-
634 A=unit(E, F, G, H),
635 ( G==fixed
636 -> I=[A|C]
637 ; dependency_insert(C, A, H, I)
639 dependency_reorder(B, I, D).
640 dependency_insert([], A, B, [A]).
641 dependency_insert([A|B], C, D, E) :-
642 A=unit(F, G, H, I),
643 ( memberchk(F, D)
644 -> E=[C, A|B]
645 ; E=[A|J],
646 dependency_insert(B, C, D, J)
648 build_units(A, B, C, D) :-
649 build_retrieval_units(A, 1, E, C, F, D, G),
650 build_guard_units(B, E, F, G).
651 build_retrieval_units([], A, A, B, B, C, C).
652 build_retrieval_units([A|B], C, D, E, F, G, H) :-
653 term_variables(A, I),
654 update_unit_dictionary(I, C, E, J, [], K),
655 G=[unit(C, A, movable, K)|L],
656 M is C+1,
657 build_retrieval_units2(B, M, D, J, F, L, H).
658 build_retrieval_units2([], A, A, B, B, C, C).
659 build_retrieval_units2([A|B], C, D, E, F, G, H) :-
660 term_variables(A, I),
661 update_unit_dictionary(I, C, E, J, [], K),
662 G=[unit(C, A, fixed, K)|L],
663 M is C+1,
664 build_retrieval_units(B, M, D, J, F, L, H).
665 initialize_unit_dictionary(A, B) :-
666 term_variables(A, C),
667 pair_all_with(C, 0, B).
668 update_unit_dictionary([], A, B, B, C, C).
669 update_unit_dictionary([A|B], C, D, E, F, G) :-
670 ( lookup_eq(D, A, H)
671 -> ( ( H==C
672 ; memberchk(H, F)
674 -> I=F
675 ; I=[H|F]
678 ; J=[A-C|D],
681 update_unit_dictionary(B, C, J, E, I, G).
682 build_guard_units(A, B, C, D) :-
683 ( A=[E]
684 -> D=[unit(B, E, fixed, [])]
685 ; A=[E|F]
686 -> term_variables(E, G),
687 update_unit_dictionary2(G, B, C, H, [], I),
688 D=[unit(B, E, movable, I)|J],
689 K is B+1,
690 build_guard_units(F, K, H, J)
692 update_unit_dictionary2([], A, B, B, C, C).
693 update_unit_dictionary2([A|B], C, D, E, F, G) :-
694 ( lookup_eq(D, A, H)
695 -> ( ( H==C
696 ; memberchk(H, F)
698 -> I=F
699 ; I=[H|F]
701 J=[A-C|D]
702 ; J=[A-C|D],
705 update_unit_dictionary2(B, C, J, E, I, G).
706 unique_analyse_optimise(A, B) :-
707 ( chr_pp_flag(unique_analyse_optimise, on)
708 -> unique_analyse_optimise_main(A, 1, [], B)
709 ; B=A
711 unique_analyse_optimise_main([], A, B, []).
712 unique_analyse_optimise_main([A|B], C, D, [E|F]) :-
713 ( discover_unique_pattern(A, C, G)
714 -> H=[G|D]
715 ; H=D
717 A=pragma(I, J, K, L),
718 I=rule(M, N, O, P),
719 J=ids(Q, R),
720 apply_unique_patterns_to_constraints(M, Q, H, S),
721 apply_unique_patterns_to_constraints(N, R, H, T),
722 append_lists([S, T, K], U),
723 E=pragma(I, J, U, L),
724 V is C+1,
725 unique_analyse_optimise_main(B, V, H, F).
726 apply_unique_patterns_to_constraints([], A, B, []).
727 apply_unique_patterns_to_constraints([A|B], [C|D], E, F) :-
728 ( member(G, E),
729 apply_unique_pattern(A, C, G, H)
730 -> F=[H|I]
731 ; F=I
733 apply_unique_patterns_to_constraints(B, D, E, I).
734 apply_unique_pattern(A, B, C, D) :-
735 C=unique(E, F),
736 subsumes(A, E, G),
737 ( setof(H, I^J^K^ (member(I, F), lookup_eq(G, I, J), term_variables(J, K), member(H, K)), L)
738 -> true
739 ; L=[]
741 D=unique(B, L).
742 subsumes(A, B, C) :-
743 empty_assoc(D),
744 subsumes_aux(A, B, D, E),
745 assoc_to_list(E, F),
746 build_unifier(F, C).
747 subsumes_aux(A, B, C, D) :-
748 ( compound(B),
749 functor(B, E, F)
750 -> compound(A),
751 functor(A, E, F),
752 subsumes_aux(F, A, B, C, D)
753 ; A==B
754 -> D=C
755 ; var(B),
756 get_assoc(A, C, G)
757 -> G==B,
759 ; var(B),
760 put_assoc(A, C, B, D)
762 subsumes_aux(0, A, B, C, C) :- !.
763 subsumes_aux(A, B, C, D, E) :-
764 arg(A, B, F),
765 arg(A, C, G),
766 subsumes_aux(F, G, D, H),
767 I is A-1,
768 subsumes_aux(I, B, C, H, E).
769 build_unifier([], []).
770 build_unifier([A-B|C], [B-A|D]) :-
771 build_unifier(C, D).
772 discover_unique_pattern(A, B, C) :-
773 A=pragma(D, E, F, G),
774 ( D=rule([H], [I], J, K)
775 -> true
776 ; D=rule([H, I], [], J, K)
778 check_unique_constraints(H, I, J, K, F, L),
779 term_variables(H, M),
780 select_pragma_unique_variables(L, M, N),
781 O=unique(H, N),
782 copy_term(O, C),
783 ( prolog_flag(verbose, P),
784 P==yes
785 -> format('Found unique pattern ~w in rule ~d~@\n', [C, B, (G=yes(Q)->write([58, 32]), write(Q);true)])
786 ; true
788 select_pragma_unique_variables([], A, []).
789 select_pragma_unique_variables([A-B|C], D, E) :-
790 ( A==B
791 -> E=[A|F]
792 ; once((
793 ( \+memberchk_eq(A, D)
794 ; \+memberchk_eq(B, D)
799 select_pragma_unique_variables(C, D, F).
800 check_unique_constraints(A, B, C, D, E, F) :-
801 \+member(passive(G), E),
802 variable_replacement(A-B, B-A, F),
803 copy_with_variable_replacement(C, H, F),
804 negate(C, I),
805 once(entails(I, H)).
806 negate(true, fail).
807 negate(fail, true).
808 negate(A=<B, B<A).
809 negate(A>B, B>=A).
810 negate(A>=B, B>A).
811 negate(A<B, B=<A).
812 negate(var(A), nonvar(A)).
813 negate(nonvar(A), var(A)).
814 entails(A, B) :-
815 B==A.
816 entails(fail, A).
817 entails(A>B, C>=D) :-
818 C==A,
819 D==B.
820 entails(A<B, C=<D) :-
821 C==A,
822 D==B.
823 entails(ground(A), nonvar(B)) :-
824 B==A.
825 entails(compound(A), nonvar(B)) :-
826 B==A.
827 entails(atomic(A), nonvar(B)) :-
828 B==A.
829 entails(number(A), nonvar(B)) :-
830 B==A.
831 entails(atom(A), nonvar(B)) :-
832 B==A.
833 check_unnecessary_active(A, B, C) :-
834 ( chr_pp_flag(check_unnecessary_active, full)
835 -> check_unnecessary_active_main(A, B, C)
836 ; chr_pp_flag(check_unnecessary_active, simplification),
837 C=rule(D, [], E, F)
838 -> check_unnecessary_active_main(A, B, C)
839 ; fail
841 check_unnecessary_active_main(A, B, C) :-
842 member(D, B),
843 variable_replacement(D, A, E),
844 copy_with_variable_replacement(C, F, E),
845 identical_rules(C, F), !.
846 set_semantics_rule(A) :-
847 ( chr_pp_flag(set_semantics_rule, on)
848 -> set_semantics_rule_main(A)
849 ; fail
851 set_semantics_rule_main(A) :-
852 A=pragma(B, C, D, E),
853 B=rule([F], [G], true, H),
854 C=ids([I], [J]),
855 once(member(unique(I, K), D)),
856 once(member(unique(J, L), D)),
857 K==L,
858 \+memberchk_eq(passive(I), D).
859 identical_rules(rule(A, B, C, D), rule(E, F, G, H)) :-
860 C==G,
861 identical_bodies(D, H),
862 permutation(A, I),
863 I==E,
864 permutation(B, J),
865 J==F.
866 identical_bodies(A, B) :-
867 ( A= (C=D),
868 B= (E=F)
869 -> ( C==E,
870 D==F
871 ; C==F,
872 E==D
873 ), !
874 ; A==B
876 copy_with_variable_replacement(A, B, C) :-
877 ( var(A)
878 -> ( lookup_eq(C, A, B)
879 -> true
880 ; A=B
882 ; functor(A, D, E),
883 functor(B, D, E),
884 A=..[F|G],
885 B=..[H|I],
886 copy_with_variable_replacement_l(G, I, C)
888 copy_with_variable_replacement_l([], [], A).
889 copy_with_variable_replacement_l([A|B], [C|D], E) :-
890 copy_with_variable_replacement(A, C, E),
891 copy_with_variable_replacement_l(B, D, E).
892 variable_replacement(A, B, C) :-
893 variable_replacement(A, B, [], C).
894 variable_replacement(A, B, C, D) :-
895 ( var(A)
896 -> var(B),
897 ( lookup_eq(C, A, E)
898 -> E==B,
900 ; D=[A-B|C]
902 ; A=..[F|G],
903 nonvar(B),
904 B=..[F|H],
905 variable_replacement_l(G, H, C, D)
907 variable_replacement_l([], [], A, A).
908 variable_replacement_l([A|B], [C|D], E, F) :-
909 variable_replacement(A, C, E, G),
910 variable_replacement_l(B, D, G, F).
911 simplification_code(A, B, C, D, E/F, G, H, I) :-
912 D=pragma(J, K, L, M),
913 head_info(A, F, N, O, P, Q),
914 build_head(E, F, G, P, R),
915 head_arg_matches(Q, [], S, T),
916 ( B==[]
917 -> U=[],
918 V=T,
919 W=[]
920 ; rest_heads_retrieval_and_matching(B, C, L, A, W, U, T, V)
922 guard_body_copies2(J, V, X, Y),
923 guard_via_reschedule(W, X, R-S, Z),
924 gen_uncond_susps_detachments(U, B, A1),
925 gen_cond_susp_detachment(O, E/F, B1),
926 ( chr_pp_flag(debugable, on)
927 -> J=rule(C1, D1, E1, F1),
928 my_term_copy(E1-F1, V, G1, H1-I1),
929 J1='chr debug_event'(try([O|K1], [], H1, I1)),
930 L1='chr debug_event'(apply([O|K1], [], H1, I1))
931 ; J1=true,
932 L1=true
934 M1= (R:-S, Z, J1, !, L1, A1, B1, Y),
935 H=[M1|I].
936 head_arg_matches(A, B, C, D) :-
937 head_arg_matches_(A, B, E, D),
938 list2conj(E, C).
939 head_arg_matches_([], A, [], A).
940 head_arg_matches_([A-B|C], D, E, F) :-
941 ( var(A)
942 -> ( lookup_eq(D, A, G)
943 -> E=[B==G|H],
945 ; I=[A-B|D],
949 ; atomic(A)
950 -> E=[B==A|H],
951 D=I,
953 ; A=..[K|L],
954 functor(A, M, N),
955 functor(O, M, N),
956 O=..[P|Q],
957 E=[nonvar(B), B=O|H],
958 pairup(L, Q, R),
959 append(R, C, J),
962 head_arg_matches_(J, I, H, F).
963 rest_heads_retrieval_and_matching(A, B, C, D, E, F, G, H) :-
964 rest_heads_retrieval_and_matching(A, B, C, D, E, F, G, H, [], [], []).
965 rest_heads_retrieval_and_matching(A, B, C, D, E, F, G, H, I, J, K) :-
966 ( A=[L|M]
967 -> rest_heads_retrieval_and_matching_n(A, B, C, I, J, D, E, F, G, H, K)
968 ; E=[],
969 F=[],
972 rest_heads_retrieval_and_matching_n([], A, B, C, D, E, [], [], F, F, G) :-
973 instantiate_pattern_goals(G).
974 rest_heads_retrieval_and_matching_n([A|B], [C|D], E, F, G, H, [I, J|K], [L|M], N, O, P) :-
975 passive_head_via(A, [H|F], P, N, I, Q, R),
976 functor(A, S, T),
977 head_info(A, T, U, V, W, X),
978 head_arg_matches(X, N, Y, Z),
979 A1=..[suspension, B1, C1, D1, E1, F1, G1|U],
980 get_max_constraint_index(H1),
981 ( H1==1
982 -> I1=Q
983 ; get_constraint_index(S/T, J1),
984 make_attr(H1, K1, L1, Q),
985 nth(J1, L1, I1)
987 different_from_other_susps(A, L, F, G, M1),
988 create_get_mutable(active, C1, N1),
989 O1= ('chr sbag_member'(L, I1), L=A1, N1, M1, Y),
990 ( member(unique(C, P1), E),
991 check_unique_keys(P1, N)
992 -> J= (O1->true)
993 ; J=O1
995 rest_heads_retrieval_and_matching_n(B, D, E, [A|F], [L|G], H, K, M, Z, O, R).
996 instantiate_pattern_goals([]).
997 instantiate_pattern_goals([A-attr(B, C, D)|E]) :-
998 get_max_constraint_index(F),
999 ( F==1
1000 -> D=true
1001 ; make_attr(F, G, H, B),
1002 or_list(C, I), !,
1003 D= (G/\I=:=I)
1005 instantiate_pattern_goals(E).
1006 check_unique_keys([], A).
1007 check_unique_keys([A|B], C) :-
1008 lookup_eq(C, A, D),
1009 check_unique_keys(B, C).
1010 different_from_other_susps(A, B, C, D, E) :-
1011 ( bagof(F, G^ (nth(G, C, H), \+A\=H, nth(G, D, I), F= (B\==I)), J)
1012 -> list2conj(J, E)
1013 ; E=true
1015 passive_head_via(A, B, C, D, E, F, G) :-
1016 functor(A, H, I),
1017 get_constraint_index(H/I, J),
1018 common_variables(A, B, K),
1019 translate(K, D, L),
1020 or_pattern(J, M),
1021 ( permutation(L, N),
1022 lookup_eq(C, N, attr(F, O, P))
1023 -> member(M, O), !,
1024 G=C,
1025 E=true
1026 ; E= (Q, R),
1027 gen_get_mod_constraints(L, Q, F),
1028 G=[L-attr(F, [M|S], R)|C]
1030 common_variables(A, B, C) :-
1031 term_variables(A, D),
1032 term_variables(B, E),
1033 intersect_eq(D, E, C).
1034 gen_get_mod_constraints(A, B, C) :-
1035 get_target_module(D),
1036 ( A==[]
1037 -> B= ('chr global_term_ref_1'(E), get_attr(E, D, F), F=C)
1038 ; ( A=[G]
1039 -> H='chr via_1'(G, I)
1040 ; A=[G, J]
1041 -> H='chr via_2'(G, J, I)
1042 ; H='chr via'(A, I)
1044 B= (H, get_attr(I, D, F), F=C)
1046 guard_body_copies(A, B, C, D) :-
1047 guard_body_copies2(A, B, E, D),
1048 list2conj(E, C).
1049 guard_body_copies2(A, B, C, D) :-
1050 A=rule(E, F, G, H),
1051 conj2list(G, I),
1052 split_off_simple_guard(I, B, J, K),
1053 my_term_copy(J-K, B, L, M-N),
1054 append(M, [O], C),
1055 term_variables(K, P),
1056 term_variables(N, Q),
1057 ( chr_pp_flag(guard_locks, on),
1058 bagof('chr lock'(R)-'chr unlock'(R), S^ (member(S, P), lookup_eq(B, S, R), memberchk_eq(R, Q)), T)
1059 -> once(pairup(U, V, T))
1060 ; U=[],
1061 V=[]
1063 list2conj(U, W),
1064 list2conj(V, X),
1065 list2conj(N, Y),
1066 O= (W, Y, X),
1067 my_term_copy(H, L, D).
1068 split_off_simple_guard([], A, [], []).
1069 split_off_simple_guard([A|B], C, D, E) :-
1070 ( simple_guard(A, C)
1071 -> D=[A|F],
1072 split_off_simple_guard(B, C, F, E)
1073 ; D=[],
1074 E=[A|B]
1076 simple_guard(var(A), B).
1077 simple_guard(nonvar(A), B).
1078 simple_guard(ground(A), B).
1079 simple_guard(number(A), B).
1080 simple_guard(atom(A), B).
1081 simple_guard(integer(A), B).
1082 simple_guard(float(A), B).
1083 simple_guard(A>B, C).
1084 simple_guard(A<B, C).
1085 simple_guard(A=<B, C).
1086 simple_guard(A>=B, C).
1087 simple_guard(A=:=B, C).
1088 simple_guard(A==B, C).
1089 simple_guard(A is B, C) :-
1090 \+lookup_eq(C, A, D).
1091 simple_guard((A, B), C) :-
1092 simple_guard(A, C),
1093 simple_guard(B, C).
1094 simple_guard(\+A, B) :-
1095 simple_guard(A, B).
1096 my_term_copy(A, B, C) :-
1097 my_term_copy(A, B, D, C).
1098 my_term_copy(A, B, C, D) :-
1099 ( var(A)
1100 -> ( lookup_eq(B, A, D)
1101 -> C=B
1102 ; C=[A-D|B]
1104 ; functor(A, E, F),
1105 functor(D, E, F),
1106 A=..[G|H],
1107 D=..[I|J],
1108 my_term_copy_list(H, B, C, J)
1110 my_term_copy_list([], A, A, []).
1111 my_term_copy_list([A|B], C, D, [E|F]) :-
1112 my_term_copy(A, C, G, E),
1113 my_term_copy_list(B, G, D, F).
1114 gen_cond_susp_detachment(A, B, C) :-
1115 ( is_attached(B)
1116 -> gen_uncond_susp_detachment(A, B, D),
1117 C= (var(A)->true;D)
1118 ; C=true
1120 gen_uncond_susp_detachment(A, B/C, D) :-
1121 ( is_attached(B/C)
1122 -> atom_concat_list([detach_, B, /, C], E),
1123 F=..[E, G, A],
1124 ( chr_pp_flag(debugable, on)
1125 -> H='chr debug_event'(remove(A))
1126 ; H=true
1128 D= (H, 'chr remove_constraint_internal'(A, G), F)
1129 ; D=true
1131 gen_uncond_susps_detachments([], [], true).
1132 gen_uncond_susps_detachments([A|B], [C|D], (E, F)) :-
1133 functor(C, G, H),
1134 gen_uncond_susp_detachment(A, G/H, E),
1135 gen_uncond_susps_detachments(B, D, F).
1136 simpagation_head1_code(A, B, C, D, E/F, G, H, I) :-
1137 D=pragma(J, ids(K, L), M, N),
1138 J=rule(O, P, Q, R),
1139 head_info(A, F, S, T, U, V),
1140 head_arg_matches(V, [], W, X),
1141 build_head(E, F, G, U, Y),
1142 append(B, P, Z),
1143 append(C, L, A1),
1144 reorder_heads(A, Z, A1, B1, C1),
1145 rest_heads_retrieval_and_matching(B1, C1, M, A, D1, E1, X, F1),
1146 split_by_ids(C1, E1, C, G1, H1),
1147 guard_body_copies2(J, F1, I1, J1),
1148 guard_via_reschedule(D1, I1, Y-W, K1),
1149 gen_uncond_susps_detachments(G1, B, L1),
1150 gen_cond_susp_detachment(T, E/F, M1),
1151 ( chr_pp_flag(debugable, on)
1152 -> my_term_copy(Q-R, F1, N1, O1-P1),
1153 Q1='chr debug_event'(try([T|G1], H1, O1, P1)),
1154 R1='chr debug_event'(apply([T|G1], H1, O1, P1))
1155 ; Q1=true,
1156 R1=true
1158 S1= (Y:-W, K1, Q1, !, R1, L1, M1, J1),
1159 H=[S1|I].
1160 split_by_ids([], [], A, [], []).
1161 split_by_ids([A|B], [C|D], E, F, G) :-
1162 ( memberchk_eq(A, E)
1163 -> F=[C|H],
1165 ; F=H,
1166 G=[C|I]
1168 split_by_ids(B, D, E, H, I).
1169 simpagation_head2_code(A, B, C, D, E, F, G, H) :-
1170 D=pragma(I, ids(J, K), L, M),
1171 I=rule(N, O, P, Q),
1172 reorder_heads(A, N, J, [R|S], [T|U]),
1173 simpagation_head2_prelude(A, R, [B, N, P, Q], E, F, G, V),
1174 extend_id(F, W),
1175 simpagation_head2_worker(A, R, T, S, U, B, C, I, L, E, W, V, H).
1176 simpagation_head2_prelude(A, B, C, D/E, F, G, H) :-
1177 head_info(A, E, I, J, K, L),
1178 build_head(D, E, F, K, M),
1179 head_arg_matches(L, [], N, O),
1180 passive_head_via(B, [A], [], O, P, Q, R),
1181 instantiate_pattern_goals(R),
1182 get_max_constraint_index(S),
1183 ( S==1
1184 -> T=Q
1185 ; functor(B, U, V),
1186 get_constraint_index(U/V, W),
1187 make_attr(S, X, Y, Q),
1188 nth(W, Y, T)
1190 ( F==[0]
1191 -> gen_cond_allocation(I, J, D/E, K, Z)
1192 ; Z=true
1194 extend_id(F, A1),
1195 extra_active_delegate_variables(A, C, O, B1),
1196 append([T|K], B1, C1),
1197 build_head(D, E, A1, C1, D1),
1198 E1= (M:-N, P, !, Z, D1),
1199 G=[E1|H].
1200 extra_active_delegate_variables(A, B, C, D) :-
1201 A=..[E|F],
1202 delegate_variables(A, B, C, F, D).
1203 passive_delegate_variables(A, B, C, D, E) :-
1204 term_variables(B, F),
1205 delegate_variables(A, C, D, F, E).
1206 delegate_variables(A, B, C, D, E) :-
1207 term_variables(A, F),
1208 term_variables(B, G),
1209 intersect_eq(F, G, H),
1210 list_difference_eq(H, D, I),
1211 translate(I, C, E).
1212 simpagation_head2_worker(A, B, C, D, E, F, G, H, I, J, K, L, M) :-
1213 H=rule(N, O, P, Q),
1214 simpagation_head2_worker_end(A, [B, D, F, P, Q], J, K, L, R),
1215 simpagation_head2_worker_body(A, B, C, D, E, F, G, H, I, J, K, R, M).
1216 simpagation_head2_worker_body(A, B, C, D, E, F, G, H, I, J/K, L, M, N) :-
1217 gen_var(O),
1218 gen_var(P),
1219 head_info(A, K, Q, R, S, T),
1220 head_arg_matches(T, [], U, V),
1221 H=rule(W, X, Y, Z),
1222 extra_active_delegate_variables(A, [B, D, F, Y, Z], V, A1),
1223 append([[O|P]|S], A1, B1),
1224 build_head(J, K, L, B1, C1),
1225 functor(B, D1, E1),
1226 head_info(B, E1, F1, G1, H1, I1),
1227 head_arg_matches(I1, V, J1, K1),
1228 L1=..[suspension, M1, N1, O1, P1, Q1, R1|F1],
1229 create_get_mutable(active, N1, S1),
1230 T1= (O=L1, S1),
1231 ( ( D\==[]
1232 ; F\==[]
1234 -> append(D, F, U1),
1235 append(E, G, V1),
1236 reorder_heads(B-A, U1, V1, W1, X1),
1237 rest_heads_retrieval_and_matching(W1, X1, I, [B, A], Y1, Z1, K1, A2, [B], [O], []),
1238 split_by_ids(X1, Z1, E, B2, C2)
1239 ; Y1=[],
1240 B2=[],
1241 C2=[],
1242 A2=K1
1244 gen_uncond_susps_detachments([O|B2], [B|D], D2),
1245 append([P|S], A1, E2),
1246 build_head(J, K, L, E2, F2),
1247 append([[]|S], A1, G2),
1248 build_head(J, K, L, G2, H2),
1249 guard_body_copies2(H, A2, I2, J2),
1250 guard_via_reschedule(Y1, I2, v(C1, T1, J1), K2),
1251 ( J2\==true
1252 -> gen_uncond_attach_goal(J/K, R, L2, M2),
1253 gen_state_cond_call(R, K, F2, M2, N2),
1254 gen_state_cond_call(R, K, H2, M2, O2)
1255 ; L2=true,
1256 N2=F2,
1257 O2=H2
1259 ( chr_pp_flag(debugable, on)
1260 -> my_term_copy(Y-Z, A2, P2, Q2-R2),
1261 S2='chr debug_event'(try([O|B2], [R|C2], Q2, R2)),
1262 T2='chr debug_event'(apply([O|B2], [R|C2], Q2, R2))
1263 ; S2=true,
1264 T2=true
1266 ( member(unique(C, U2), I),
1267 check_unique_keys(U2, V)
1268 -> V2= (C1:-T1, J1-> (K2, S2->T2, D2, L2, J2, O2;H2);F2)
1269 ; V2= (C1:-T1, J1, K2, S2->T2, D2, L2, J2, N2;F2)
1271 M=[V2|N].
1272 gen_state_cond_call(A, B, C, D, E) :-
1273 length(F, B),
1274 G=..[suspension, H, I, J, K, L, M|F],
1275 create_get_mutable(active, I, N),
1276 create_get_mutable(D, K, O),
1277 E= (A=G, N, O->'chr update_mutable'(inactive, I), C;true).
1278 simpagation_head2_worker_end(A, B, C/D, E, F, G) :-
1279 head_info(A, D, H, I, J, K),
1280 head_arg_matches(K, [], L, M),
1281 extra_active_delegate_variables(A, B, M, N),
1282 append([[]|J], N, O),
1283 build_head(C, D, E, O, P),
1284 next_id(E, Q),
1285 build_head(C, D, Q, J, R),
1286 S= (P:-R),
1287 F=[S|G].
1288 propagation_code(A, B, C, D, E, F, G, H, I) :-
1289 ( B==[]
1290 -> propagation_single_headed(A, C, D, F, G, H, I)
1291 ; propagation_multi_headed(A, B, C, D, E, F, G, H, I)
1293 propagation_single_headed(A, B, C, D/E, F, G, H) :-
1294 head_info(A, E, I, J, K, L),
1295 build_head(D, E, F, K, M),
1296 inc_id(F, N),
1297 build_head(D, E, N, K, O),
1298 P=O,
1299 head_arg_matches(L, [], Q, R),
1300 guard_body_copies(B, R, S, T),
1301 ( F==[0]
1302 -> gen_cond_allocation(I, J, D/E, K, U),
1304 ; V=true
1306 gen_uncond_attach_goal(D/E, J, W, X),
1307 gen_state_cond_call(J, E, P, X, Y),
1308 ( chr_pp_flag(debugable, on)
1309 -> B=rule(Z, A1, B1, C1),
1310 my_term_copy(B1-C1, R, D1, E1-F1),
1311 G1='chr debug_event'(try([], [J], E1, F1)),
1312 H1='chr debug_event'(apply([], [J], E1, F1))
1313 ; G1=true,
1314 H1=true
1316 I1= (M:-Q, V, 'chr novel_production'(J, C), S, G1, !, H1, 'chr extend_history'(J, C), W, T, Y),
1317 G=[I1|H].
1318 propagation_multi_headed(A, B, C, D, E, F, G, H, I) :-
1319 B=[J|K],
1320 propagation_prelude(A, B, C, F, G, H, L),
1321 extend_id(G, M),
1322 propagation_nested_code(K, [J, A], C, D, E, F, M, L, I).
1323 propagation_prelude(A, [B|C], D, E/F, G, H, I) :-
1324 head_info(A, F, J, K, L, M),
1325 build_head(E, F, G, L, N),
1326 head_arg_matches(M, [], O, P),
1327 D=rule(Q, R, S, T),
1328 extra_active_delegate_variables(A, [B, C, S, T], P, U),
1329 passive_head_via(B, [A], [], P, V, W, X),
1330 instantiate_pattern_goals(X),
1331 get_max_constraint_index(Y),
1332 ( Y==1
1333 -> Z=W
1334 ; functor(B, A1, B1),
1335 make_attr(Y, C1, D1, W),
1336 get_constraint_index(A1/B1, E1),
1337 nth(E1, D1, Z)
1339 ( G==[0]
1340 -> gen_cond_allocation(J, K, E/F, L, F1)
1341 ; F1=true
1343 extend_id(G, G1),
1344 append([Z|L], U, H1),
1345 build_head(E, F, G1, H1, I1),
1346 J1=I1,
1347 K1= (N:-O, V, !, F1, J1),
1348 H=[K1|I].
1349 propagation_nested_code([], [A|B], C, D, E, F, G, H, I) :-
1350 propagation_end([A|B], [], C, F, G, H, J),
1351 propagation_body(A, B, C, D, E, F, G, J, I).
1352 propagation_nested_code([A|B], C, D, E, F, G, H, I, J) :-
1353 propagation_end(C, [A|B], D, G, H, I, K),
1354 propagation_accumulator([A|B], C, D, G, H, K, L),
1355 inc_id(H, M),
1356 propagation_nested_code(B, [A|C], D, E, F, G, M, L, J).
1357 propagation_body(A, B, C, D, E, F/G, H, I, J) :-
1358 C=rule(K, L, M, N),
1359 get_prop_inner_loop_vars(B, [A, M, N], O, P, Q, R),
1360 gen_var(S),
1361 gen_var(T),
1362 functor(A, U, V),
1363 gen_vars(V, W),
1364 X=..[suspension, Y, Z, A1, B1, C1, D1|W],
1365 create_get_mutable(active, Z, E1),
1366 F1= (S=X, E1),
1367 G1=[[S|T]|O],
1368 build_head(F, G, H, G1, H1),
1369 I1=[T|O],
1370 build_head(F, G, H, I1, J1),
1371 K1=J1,
1372 A=..[L1|M1],
1373 pairup(M1, W, N1),
1374 head_arg_matches(N1, P, O1, P1),
1375 different_from_other_susps(A, S, B, R, Q1),
1376 guard_body_copies(C, P1, R1, S1),
1377 gen_uncond_attach_goal(F/G, Q, T1, U1),
1378 gen_state_cond_call(Q, G, K1, U1, V1),
1379 history_susps(E, [S|R], Q, [], W1),
1380 bagof('chr novel_production'(X1, Y1), (member(X1, W1), Y1=Z1), A2),
1381 list2conj(A2, B2),
1382 C2=..[t, D|W1],
1383 ( chr_pp_flag(debugable, on)
1384 -> C=rule(D2, E2, M, N),
1385 my_term_copy(M-N, P1, F2, G2-H2),
1386 I2='chr debug_event'(try([], [Q, S|R], G2, H2)),
1387 J2='chr debug_event'(apply([], [Q, S|R], G2, H2))
1388 ; I2=true,
1389 J2=true
1391 K2= (H1:-F1, Q1, O1, Z1=C2, B2, R1, I2->J2, 'chr extend_history'(Q, Z1), T1, S1, V1;K1),
1392 I=[K2|J].
1393 history_susps(A, B, C, D, E) :-
1394 ( A==0
1395 -> reverse(B, F),
1396 append(F, [C|D], E)
1397 ; B=[G|H],
1398 I is A-1,
1399 history_susps(I, H, C, [G|D], E)
1401 get_prop_inner_loop_vars([A], B, C, D, E, []) :- !,
1402 functor(A, F, G),
1403 head_info(A, G, H, E, I, J),
1404 head_arg_matches(J, [], K, D),
1405 extra_active_delegate_variables(A, B, D, L),
1406 append(I, L, C).
1407 get_prop_inner_loop_vars([A|B], C, D, E, F, [G|H]) :-
1408 get_prop_inner_loop_vars(B, [A|C], I, J, F, H),
1409 functor(A, K, L),
1410 gen_var(M),
1411 head_info(A, L, N, G, O, P),
1412 head_arg_matches(P, J, Q, E),
1413 passive_delegate_variables(A, B, C, E, R),
1414 append(R, [G, M|I], D).
1415 propagation_end([A|B], C, D, E/F, G, H, I) :-
1416 D=rule(J, K, L, M),
1417 gen_var_susp_list_for(B, [A, C, L, M], N, O, P, Q),
1418 R=[[]|O],
1419 build_head(E, F, G, R, S),
1420 ( G=[0|T]
1421 -> next_id(G, U),
1423 ; dec_id(G, U),
1424 V=[Q|P]
1426 build_head(E, F, U, V, W),
1427 X=W,
1428 Y= (S:-X),
1429 H=[Y|I].
1430 gen_var_susp_list_for([A], B, C, D, E, F) :- !,
1431 functor(A, G, H),
1432 head_info(A, H, I, F, E, J),
1433 head_arg_matches(J, [], K, C),
1434 extra_active_delegate_variables(A, B, C, L),
1435 append(E, L, D).
1436 gen_var_susp_list_for([A|B], C, D, E, F, G) :-
1437 gen_var_susp_list_for(B, [A|C], H, F, I, J),
1438 functor(A, K, L),
1439 gen_var(G),
1440 head_info(A, L, M, N, O, P),
1441 head_arg_matches(P, H, Q, D),
1442 passive_delegate_variables(A, B, C, D, R),
1443 append(R, [N, G|F], E).
1444 propagation_accumulator([A|B], [C|D], E, F/G, H, I, J) :-
1445 E=rule(K, L, M, N),
1446 pre_vars_and_susps(D, [C, A, B, M, N], O, P, Q),
1447 gen_var(R),
1448 functor(C, S, T),
1449 gen_vars(T, U),
1450 head_info(C, T, U, V, W, X),
1451 head_arg_matches(X, P, Y, Z),
1452 A1=..[suspension, B1, C1, D1, E1, F1, G1|U],
1453 different_from_other_susps(C, V, D, Q, H1),
1454 create_get_mutable(active, C1, I1),
1455 J1= (V=A1, I1, H1, Y),
1456 functor(A, K1, L1),
1457 passive_head_via(A, [C|D], [], Z, M1, N1, O1),
1458 instantiate_pattern_goals(O1),
1459 get_max_constraint_index(P1),
1460 ( P1==1
1461 -> Q1=N1
1462 ; get_constraint_index(K1/L1, R1),
1463 make_attr(P1, S1, T1, N1),
1464 nth(R1, T1, Q1)
1466 inc_id(H, U1),
1467 V1=[[V|R]|O],
1468 build_head(F, G, H, V1, W1),
1469 passive_delegate_variables(C, D, [A, B, M, N], Z, X1),
1470 append([Q1|X1], [V, R|O], Y1),
1471 build_head(F, G, U1, Y1, Z1),
1472 A2=[R|O],
1473 build_head(F, G, H, A2, B2),
1474 C2= (W1:-J1, M1->Z1;B2),
1475 I=[C2|J].
1476 pre_vars_and_susps([A], B, C, D, []) :- !,
1477 functor(A, E, F),
1478 head_info(A, F, G, H, I, J),
1479 head_arg_matches(J, [], K, D),
1480 extra_active_delegate_variables(A, B, D, L),
1481 append(I, L, C).
1482 pre_vars_and_susps([A|B], C, D, E, [F|G]) :-
1483 pre_vars_and_susps(B, [A|C], H, I, G),
1484 functor(A, J, K),
1485 gen_var(L),
1486 head_info(A, K, M, F, N, O),
1487 head_arg_matches(O, I, P, E),
1488 passive_delegate_variables(A, B, C, E, Q),
1489 append(Q, [F, L|H], D).
1490 reorder_heads(A, B, C, D, E) :-
1491 ( chr_pp_flag(reorder_heads, on)
1492 -> reorder_heads_main(A, B, C, D, E)
1493 ; D=B,
1496 reorder_heads_main(A, B, C, D, E) :-
1497 term_variables(A, F),
1498 reorder_heads1(B, C, F, D, E).
1499 reorder_heads1(A, B, C, D, E) :-
1500 ( A==[]
1501 -> D=[],
1502 E=[]
1503 ; D=[F|G],
1504 E=[H|I],
1505 select_best_head(A, B, C, F, H, J, K, L),
1506 reorder_heads1(J, K, L, G, I)
1508 select_best_head(A, B, C, D, E, F, G, H) :-
1509 ( bagof(tuple(I, J, K, L, M), (select2(J, K, A, B, L, M), order_score(J, C, L, I)), N)
1510 -> true
1511 ; N=[]
1513 max_go_list(N, tuple(O, D, E, F, G)),
1514 term_variables(D, P),
1515 ( setof(Q, (member(Q, P), \+memberchk_eq(Q, C)), R)
1516 -> true
1517 ; R=[]
1519 append(R, C, H).
1520 reorder_heads(A, B, C) :-
1521 term_variables(A, D),
1522 reorder_heads1(B, D, C).
1523 reorder_heads1(A, B, C) :-
1524 ( A==[]
1525 -> C=[]
1526 ; C=[D|E],
1527 select_best_head(A, B, D, F, G),
1528 reorder_heads1(F, G, E)
1530 select_best_head(A, B, C, D, E) :-
1531 ( bagof(tuple(F, G, H), (select(G, A, H), order_score(G, B, H, F)), I)
1532 -> true
1533 ; I=[]
1535 max_go_list(I, tuple(J, C, D)),
1536 term_variables(C, K),
1537 ( setof(L, (member(L, K), \+memberchk_eq(L, B)), M)
1538 -> true
1539 ; M=[]
1541 append(M, B, E).
1542 order_score(A, B, C, D) :-
1543 term_variables(A, E),
1544 term_variables(C, F),
1545 order_score_vars(E, B, F, 0, D).
1546 order_score_vars([], A, B, C, D) :-
1547 ( C==0
1548 -> D=99999
1549 ; D=C
1551 order_score_vars([A|B], C, D, E, F) :-
1552 ( memberchk_eq(A, C)
1553 -> G is E+1
1554 ; memberchk_eq(A, D)
1555 -> G is E+1
1556 ; G=E
1558 order_score_vars(B, C, D, G, F).
1559 create_get_mutable(A, B, C) :-
1560 C= (B=mutable(A)).
1561 clean_clauses([], []).
1562 clean_clauses([A|B], [C|D]) :-
1563 clean_clause(A, C),
1564 clean_clauses(B, D).
1565 clean_clause(A, B) :-
1566 ( A= (C:-D)
1567 -> clean_goal(D, E),
1568 ( E==true
1569 -> B=C
1570 ; B= (C:-E)
1572 ; B=A
1574 clean_goal(A, B) :-
1575 var(A), !,
1576 B=A.
1577 clean_goal((A, B), C) :- !,
1578 clean_goal(A, D),
1579 clean_goal(B, E),
1580 ( D==true
1581 -> C=E
1582 ; E==true
1583 -> C=D
1584 ; C= (D, E)
1586 clean_goal((A->B;C), D) :- !,
1587 clean_goal(A, E),
1588 ( E==true
1589 -> clean_goal(B, F),
1591 ; E==fail
1592 -> clean_goal(C, G),
1594 ; clean_goal(B, F),
1595 clean_goal(C, G),
1596 D= (E->F;G)
1598 clean_goal((A;B), C) :- !,
1599 clean_goal(A, D),
1600 clean_goal(B, E),
1601 ( D==fail
1602 -> C=E
1603 ; E==fail
1604 -> C=D
1605 ; C= (D;E)
1607 clean_goal(once(A), B) :- !,
1608 clean_goal(A, C),
1609 ( C==true
1610 -> B=true
1611 ; C==fail
1612 -> B=fail
1613 ; B=once(C)
1615 clean_goal((A->B), C) :- !,
1616 clean_goal(A, D),
1617 ( D==true
1618 -> clean_goal(B, C)
1619 ; D==fail
1620 -> C=fail
1621 ; clean_goal(B, E),
1622 C= (D->E)
1624 clean_goal(A, A).
1625 gen_var(A).
1626 gen_vars(A, B) :-
1627 length(B, A).
1628 head_info(A, B, C, D, E, F) :-
1629 vars_susp(B, C, D, E),
1630 A=..[G|H],
1631 pairup(H, C, F).
1632 inc_id([A|B], [C|B]) :-
1633 C is A+1.
1634 dec_id([A|B], [C|B]) :-
1635 C is A-1.
1636 extend_id(A, [0|A]).
1637 next_id([A, B|C], [D|C]) :-
1638 D is B+1.
1639 build_head(A, B, C, D, E) :-
1640 buildName(A, B, C, F),
1641 E=..[F|D].
1642 buildName(A, B, C, D) :-
1643 atom_concat(A, /, E),
1644 atom_concat(E, B, F),
1645 buildName_(C, F, D).
1646 buildName_([], A, A).
1647 buildName_([A|B], C, D) :-
1648 buildName_(B, C, E),
1649 atom_concat(E, '__', F),
1650 atom_concat(F, A, D).
1651 vars_susp(A, B, C, D) :-
1652 length(B, A),
1653 append(B, [C], D).
1654 make_attr(A, B, C, D) :-
1655 length(C, A),
1656 D=..[v, B|C].
1657 or_pattern(A, B) :-
1658 C is A-1,
1659 B is 1<<C.
1660 and_pattern(A, B) :-
1661 C is A-1,
1662 D is 1<<C,
1663 B is-1* (D+1).
1664 conj2list(A, B) :-
1665 conj2list(A, B, []).
1666 conj2list(A, B, C) :-
1667 A= (D, E), !,
1668 conj2list(D, B, F),
1669 conj2list(E, F, C).
1670 conj2list(A, [A|B], B).
1671 list2conj([], true).
1672 list2conj([A], B) :- !,
1673 B=A.
1674 list2conj([A|B], C) :-
1675 ( A==true
1676 -> list2conj(B, C)
1677 ; C= (A, D),
1678 list2conj(B, D)
1680 atom_concat_list([A], A) :- !.
1681 atom_concat_list([A|B], C) :-
1682 atom_concat_list(B, D),
1683 atom_concat(A, D, C).
1684 set_elems([], A).
1685 set_elems([A|B], A) :-
1686 set_elems(B, A).
1687 member2([A|B], [C|D], A-C).
1688 member2([A|B], [C|D], E) :-
1689 member2(B, D, E).
1690 select2(A, B, [A|C], [B|D], C, D).
1691 select2(A, B, [C|D], [E|F], [C|G], [E|H]) :-
1692 select2(A, B, D, F, G, H).
1693 pair_all_with([], A, []).
1694 pair_all_with([A|B], C, [A-C|D]) :-
1695 pair_all_with(B, C, D).
1696 'attach_constraint/2'([], A).
1697 'attach_constraint/2'([A|B], C) :-
1698 ( get_attr(A, chr_translate_bootstrap1, D)
1699 -> D=v(E, F, G, H, I, J, K),
1700 ( E/\1=:=1
1701 -> put_attr(A, chr_translate_bootstrap1, v(E, [C|F], G, H, I, J, K))
1702 ; L is E\/1,
1703 put_attr(A, chr_translate_bootstrap1, v(L, [C], G, H, I, J, K))
1705 ; put_attr(A, chr_translate_bootstrap1, v(1, [C], [], [], [], [], []))
1707 'attach_constraint/2'(B, C).
1708 'detach_constraint/2'([], A).
1709 'detach_constraint/2'([A|B], C) :-
1710 ( get_attr(A, chr_translate_bootstrap1, D)
1711 -> D=v(E, F, G, H, I, J, K),
1712 ( E/\1=:=1
1713 -> 'chr sbag_del_element'(F, C, L),
1714 ( L==[]
1715 -> M is E/\ -2,
1716 ( M==0
1717 -> del_attr(A, chr_translate_bootstrap1)
1718 ; put_attr(A, chr_translate_bootstrap1, v(M, [], G, H, I, J, K))
1720 ; put_attr(A, chr_translate_bootstrap1, v(E, L, G, H, I, J, K))
1722 ; true
1724 ; true
1726 'detach_constraint/2'(B, C).
1727 'attach_constraint_count/1'([], A).
1728 'attach_constraint_count/1'([A|B], C) :-
1729 ( get_attr(A, chr_translate_bootstrap1, D)
1730 -> D=v(E, F, G, H, I, J, K),
1731 ( E/\2=:=2
1732 -> put_attr(A, chr_translate_bootstrap1, v(E, F, [C|G], H, I, J, K))
1733 ; L is E\/2,
1734 put_attr(A, chr_translate_bootstrap1, v(L, F, [C], H, I, J, K))
1736 ; put_attr(A, chr_translate_bootstrap1, v(2, [], [C], [], [], [], []))
1738 'attach_constraint_count/1'(B, C).
1739 'detach_constraint_count/1'([], A).
1740 'detach_constraint_count/1'([A|B], C) :-
1741 ( get_attr(A, chr_translate_bootstrap1, D)
1742 -> D=v(E, F, G, H, I, J, K),
1743 ( E/\2=:=2
1744 -> 'chr sbag_del_element'(G, C, L),
1745 ( L==[]
1746 -> M is E/\ -3,
1747 ( M==0
1748 -> del_attr(A, chr_translate_bootstrap1)
1749 ; put_attr(A, chr_translate_bootstrap1, v(M, F, [], H, I, J, K))
1751 ; put_attr(A, chr_translate_bootstrap1, v(E, F, L, H, I, J, K))
1753 ; true
1755 ; true
1757 'detach_constraint_count/1'(B, C).
1758 'attach_constraint_index/2'([], A).
1759 'attach_constraint_index/2'([A|B], C) :-
1760 ( get_attr(A, chr_translate_bootstrap1, D)
1761 -> D=v(E, F, G, H, I, J, K),
1762 ( E/\4=:=4
1763 -> put_attr(A, chr_translate_bootstrap1, v(E, F, G, [C|H], I, J, K))
1764 ; L is E\/4,
1765 put_attr(A, chr_translate_bootstrap1, v(L, F, G, [C], I, J, K))
1767 ; put_attr(A, chr_translate_bootstrap1, v(4, [], [], [C], [], [], []))
1769 'attach_constraint_index/2'(B, C).
1770 'detach_constraint_index/2'([], A).
1771 'detach_constraint_index/2'([A|B], C) :-
1772 ( get_attr(A, chr_translate_bootstrap1, D)
1773 -> D=v(E, F, G, H, I, J, K),
1774 ( E/\4=:=4
1775 -> 'chr sbag_del_element'(H, C, L),
1776 ( L==[]
1777 -> M is E/\ -5,
1778 ( M==0
1779 -> del_attr(A, chr_translate_bootstrap1)
1780 ; put_attr(A, chr_translate_bootstrap1, v(M, F, G, [], I, J, K))
1782 ; put_attr(A, chr_translate_bootstrap1, v(E, F, G, L, I, J, K))
1784 ; true
1786 ; true
1788 'detach_constraint_index/2'(B, C).
1789 'attach_max_constraint_index/1'([], A).
1790 'attach_max_constraint_index/1'([A|B], C) :-
1791 ( get_attr(A, chr_translate_bootstrap1, D)
1792 -> D=v(E, F, G, H, I, J, K),
1793 ( E/\8=:=8
1794 -> put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, [C|I], J, K))
1795 ; L is E\/8,
1796 put_attr(A, chr_translate_bootstrap1, v(L, F, G, H, [C], J, K))
1798 ; put_attr(A, chr_translate_bootstrap1, v(8, [], [], [], [C], [], []))
1800 'attach_max_constraint_index/1'(B, C).
1801 'detach_max_constraint_index/1'([], A).
1802 'detach_max_constraint_index/1'([A|B], C) :-
1803 ( get_attr(A, chr_translate_bootstrap1, D)
1804 -> D=v(E, F, G, H, I, J, K),
1805 ( E/\8=:=8
1806 -> 'chr sbag_del_element'(I, C, L),
1807 ( L==[]
1808 -> M is E/\ -9,
1809 ( M==0
1810 -> del_attr(A, chr_translate_bootstrap1)
1811 ; put_attr(A, chr_translate_bootstrap1, v(M, F, G, H, [], J, K))
1813 ; put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, L, J, K))
1815 ; true
1817 ; true
1819 'detach_max_constraint_index/1'(B, C).
1820 'attach_target_module/1'([], A).
1821 'attach_target_module/1'([A|B], C) :-
1822 ( get_attr(A, chr_translate_bootstrap1, D)
1823 -> D=v(E, F, G, H, I, J, K),
1824 ( E/\16=:=16
1825 -> put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, I, [C|J], K))
1826 ; L is E\/16,
1827 put_attr(A, chr_translate_bootstrap1, v(L, F, G, H, I, [C], K))
1829 ; put_attr(A, chr_translate_bootstrap1, v(16, [], [], [], [], [C], []))
1831 'attach_target_module/1'(B, C).
1832 'detach_target_module/1'([], A).
1833 'detach_target_module/1'([A|B], C) :-
1834 ( get_attr(A, chr_translate_bootstrap1, D)
1835 -> D=v(E, F, G, H, I, J, K),
1836 ( E/\16=:=16
1837 -> 'chr sbag_del_element'(J, C, L),
1838 ( L==[]
1839 -> M is E/\ -17,
1840 ( M==0
1841 -> del_attr(A, chr_translate_bootstrap1)
1842 ; put_attr(A, chr_translate_bootstrap1, v(M, F, G, H, I, [], K))
1844 ; put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, I, L, K))
1846 ; true
1848 ; true
1850 'detach_target_module/1'(B, C).
1851 'attach_attached/2'([], A).
1852 'attach_attached/2'([A|B], C) :-
1853 ( get_attr(A, chr_translate_bootstrap1, D)
1854 -> D=v(E, F, G, H, I, J, K),
1855 ( E/\32=:=32
1856 -> put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, I, J, [C|K]))
1857 ; L is E\/32,
1858 put_attr(A, chr_translate_bootstrap1, v(L, F, G, H, I, J, [C]))
1860 ; put_attr(A, chr_translate_bootstrap1, v(32, [], [], [], [], [], [C]))
1862 'attach_attached/2'(B, C).
1863 'detach_attached/2'([], A).
1864 'detach_attached/2'([A|B], C) :-
1865 ( get_attr(A, chr_translate_bootstrap1, D)
1866 -> D=v(E, F, G, H, I, J, K),
1867 ( E/\32=:=32
1868 -> 'chr sbag_del_element'(K, C, L),
1869 ( L==[]
1870 -> M is E/\ -33,
1871 ( M==0
1872 -> del_attr(A, chr_translate_bootstrap1)
1873 ; put_attr(A, chr_translate_bootstrap1, v(M, F, G, H, I, J, []))
1875 ; put_attr(A, chr_translate_bootstrap1, v(E, F, G, H, I, J, L))
1877 ; true
1879 ; true
1881 'detach_attached/2'(B, C).
1882 attach_increment([], A).
1883 attach_increment([A|B], v(C, D, E, F, G, H, I)) :-
1884 'chr not_locked'(A),
1885 ( get_attr(A, chr_translate_bootstrap1, J)
1886 -> J=v(K, L, M, N, O, P, Q),
1887 sort(L, R),
1888 'chr merge_attributes'(D, R, S),
1889 sort(M, T),
1890 'chr merge_attributes'(E, T, U),
1891 sort(N, V),
1892 'chr merge_attributes'(F, V, W),
1893 sort(O, X),
1894 'chr merge_attributes'(G, X, Y),
1895 sort(P, Z),
1896 'chr merge_attributes'(H, Z, A1),
1897 sort(Q, B1),
1898 'chr merge_attributes'(I, B1, C1),
1899 D1 is C\/K,
1900 put_attr(A, chr_translate_bootstrap1, v(D1, S, U, W, Y, A1, C1))
1901 ; put_attr(A, chr_translate_bootstrap1, v(C, D, E, F, G, H, I))
1903 attach_increment(B, v(C, D, E, F, G, H, I)).
1904 chr_translate_bootstrap1:attr_unify_hook(v(A, B, C, D, E, F, G), H) :-
1905 sort(B, I),
1906 sort(C, J),
1907 sort(D, K),
1908 sort(E, L),
1909 sort(F, M),
1910 sort(G, N),
1911 ( var(H)
1912 -> ( get_attr(H, chr_translate_bootstrap1, O)
1913 -> O=v(P, Q, R, S, T, U, V),
1914 sort(Q, W),
1915 'chr merge_attributes'(I, W, X),
1916 sort(R, Y),
1917 'chr merge_attributes'(J, Y, Z),
1918 sort(S, A1),
1919 'chr merge_attributes'(K, A1, B1),
1920 sort(T, C1),
1921 'chr merge_attributes'(L, C1, D1),
1922 sort(U, E1),
1923 'chr merge_attributes'(M, E1, F1),
1924 sort(V, G1),
1925 'chr merge_attributes'(N, G1, H1),
1926 I1 is A\/P,
1927 put_attr(H, chr_translate_bootstrap1, v(I1, X, Z, B1, D1, F1, H1)),
1928 'chr run_suspensions_loop'([X, Z, B1, D1, F1, H1])
1929 ; put_attr(H, chr_translate_bootstrap1, v(A, I, J, K, L, M, N)),
1930 'chr run_suspensions_loop'([I, J, K, L, M, N])
1932 ; ( compound(H)
1933 -> term_variables(H, J1),
1934 attach_increment(J1, v(A, I, J, K, L, M, N))
1935 ; true
1937 'chr run_suspensions_loop'([I, J, K, L, M, N])
1939 constraint(A, B) :-
1940 'constraint/2__0'(A, B, C).
1941 'constraint/2__0'(A, B, C) :-
1942 'chr via_1'(A, D),
1943 get_attr(D, chr_translate_bootstrap1, E),
1944 E=v(F, G, H, I, J, K, L),
1945 F/\1=:=1,
1946 ( 'chr sbag_member'(M, G),
1947 M=suspension(N, O, P, Q, R, S, T, U),
1948 O=mutable(active),
1949 T==A
1950 -> true
1951 ), !,
1952 ( var(C)
1953 -> true
1954 ; 'chr remove_constraint_internal'(C, V),
1955 'detach_constraint/2'(V, C)
1957 B=U.
1958 'constraint/2__0'(A, B, C) :-
1959 'chr via_1'(B, D),
1960 get_attr(D, chr_translate_bootstrap1, E),
1961 E=v(F, G, H, I, J, K, L),
1962 F/\1=:=1,
1963 'chr sbag_member'(M, G),
1964 M=suspension(N, O, P, Q, R, S, T, U),
1965 O=mutable(active),
1966 U==B, !,
1967 ( var(C)
1968 -> true
1969 ; 'chr remove_constraint_internal'(C, V),
1970 'detach_constraint/2'(V, C)
1972 A=T.
1973 'constraint/2__0'(A, B, C) :-
1974 ( var(C)
1975 -> 'chr insert_constraint_internal'(D, C, chr_translate_bootstrap1:'constraint/2__0'(A, B, C), constraint, [A, B])
1976 ; 'chr activate_constraint'(D, C, E)
1978 'attach_constraint/2'(D, C).
1979 constraint_count(A) :-
1980 'constraint_count/1__0'(A, B).
1981 'constraint_count/1__0'(A, B) :-
1982 'chr global_term_ref_1'(C),
1983 get_attr(C, chr_translate_bootstrap1, D),
1984 D=v(E, F, G, H, I, J, K),
1985 E/\2=:=2,
1986 'chr sbag_member'(L, G),
1987 L=suspension(M, N, O, P, Q, R, S),
1988 N=mutable(active), !,
1989 ( var(B)
1990 -> true
1991 ; 'chr remove_constraint_internal'(B, T),
1992 'detach_constraint_count/1'(T, B)
1994 A=S.
1995 'constraint_count/1__0'(A, B) :-
1996 ( var(B)
1997 -> 'chr insert_constraint_internal'(C, B, chr_translate_bootstrap1:'constraint_count/1__0'(A, B), constraint_count, [A])
1998 ; 'chr activate_constraint'(C, B, D)
2000 'attach_constraint_count/1'(C, B).
2001 constraint_index(A, B) :-
2002 'constraint_index/2__0'(A, B, C).
2003 'constraint_index/2__0'(A, B, C) :-
2004 ( var(C)
2005 -> 'chr insert_constraint_internal'(D, C, chr_translate_bootstrap1:'constraint_index/2__0'(A, B, C), constraint_index, [A, B])
2006 ; 'chr activate_constraint'(D, C, E)
2008 'attach_constraint_index/2'(D, C).
2009 get_constraint_index(A, B) :-
2010 'get_constraint_index/2__0'(A, B, C).
2011 'get_constraint_index/2__0'(A, B, C) :-
2012 'chr via_1'(A, D),
2013 get_attr(D, chr_translate_bootstrap1, E),
2014 E=v(F, G, H, I, J, K, L),
2015 F/\4=:=4,
2016 'chr sbag_member'(M, I),
2017 M=suspension(N, O, P, Q, R, S, T, U),
2018 O=mutable(active),
2019 T==A, !,
2020 B=U.
2021 'get_constraint_index/2__0'(A, B, C) :- !,
2022 fail.
2023 max_constraint_index(A) :-
2024 'max_constraint_index/1__0'(A, B).
2025 'max_constraint_index/1__0'(A, B) :-
2026 ( var(B)
2027 -> 'chr insert_constraint_internal'(C, B, chr_translate_bootstrap1:'max_constraint_index/1__0'(A, B), max_constraint_index, [A])
2028 ; 'chr activate_constraint'(C, B, D)
2030 'attach_max_constraint_index/1'(C, B).
2031 get_max_constraint_index(A) :-
2032 'get_max_constraint_index/1__0'(A, B).
2033 'get_max_constraint_index/1__0'(A, B) :-
2034 'chr global_term_ref_1'(C),
2035 get_attr(C, chr_translate_bootstrap1, D),
2036 D=v(E, F, G, H, I, J, K),
2037 E/\8=:=8,
2038 'chr sbag_member'(L, I),
2039 L=suspension(M, N, O, P, Q, R, S),
2040 N=mutable(active), !,
2041 A=S.
2042 'get_max_constraint_index/1__0'(A, B) :- !,
2043 fail.
2044 target_module(A) :-
2045 'target_module/1__0'(A, B).
2046 'target_module/1__0'(A, B) :-
2047 ( var(B)
2048 -> 'chr insert_constraint_internal'(C, B, chr_translate_bootstrap1:'target_module/1__0'(A, B), target_module, [A])
2049 ; 'chr activate_constraint'(C, B, D)
2051 'attach_target_module/1'(C, B).
2052 get_target_module(A) :-
2053 'get_target_module/1__0'(A, B).
2054 'get_target_module/1__0'(A, B) :-
2055 'chr global_term_ref_1'(C),
2056 get_attr(C, chr_translate_bootstrap1, D),
2057 D=v(E, F, G, H, I, J, K),
2058 E/\16=:=16,
2059 'chr sbag_member'(L, J),
2060 L=suspension(M, N, O, P, Q, R, S),
2061 N=mutable(active), !,
2062 A=S.
2063 'get_target_module/1__0'(A, B) :- !,
2064 A=user.
2065 attached(A, B) :-
2066 'attached/2__0'(A, B, C).
2067 'attached/2__0'(A, B, C) :-
2068 'chr via_1'(A, D),
2069 get_attr(D, chr_translate_bootstrap1, E),
2070 E=v(F, G, H, I, J, K, L),
2071 F/\32=:=32,
2072 'chr sbag_member'(M, L),
2073 M=suspension(N, O, P, Q, R, S, T, U),
2074 O=mutable(active),
2075 T==A,
2076 U==yes, !,
2077 ( var(C)
2078 -> true
2079 ; 'chr remove_constraint_internal'(C, V),
2080 'detach_attached/2'(V, C)
2082 'attached/2__0'(A, B, C) :-
2083 B==yes,
2084 'chr via_1'(A, D),
2085 get_attr(D, chr_translate_bootstrap1, E),
2086 E=v(F, G, H, I, J, K, L),
2087 F/\32=:=32, !,
2088 ( var(C)
2089 -> 'chr allocate_constraint'(chr_translate_bootstrap1:'attached/2__0'(A, B, C), C, attached, [A, B])
2090 ; true
2092 'attached/2__0__0'(L, A, B, C).
2093 'attached/2__0__0'([], A, B, C) :-
2094 'attached/2__1'(A, B, C).
2095 'attached/2__0__0'([A|B], C, D, E) :-
2096 ( A=suspension(F, G, H, I, J, K, L, M),
2097 G=mutable(active),
2098 L==C
2099 -> 'chr remove_constraint_internal'(A, N),
2100 'detach_attached/2'(N, A),
2101 'attached/2__0__0'(B, C, D, E)
2102 ; 'attached/2__0__0'(B, C, D, E)
2104 'attached/2__0'(A, B, C) :-
2105 ( var(C)
2106 -> 'chr allocate_constraint'(chr_translate_bootstrap1:'attached/2__0'(A, B, C), C, attached, [A, B])
2107 ; true
2109 'attached/2__1'(A, B, C).
2110 'attached/2__1'(A, B, C) :-
2111 'chr via_1'(A, D),
2112 get_attr(D, chr_translate_bootstrap1, E),
2113 E=v(F, G, H, I, J, K, L),
2114 F/\32=:=32,
2115 'chr sbag_member'(M, L),
2116 M=suspension(N, O, P, Q, R, S, T, U),
2117 O=mutable(active),
2118 T==A,
2119 U==no, !,
2120 ( var(C)
2121 -> true
2122 ; 'chr remove_constraint_internal'(C, V),
2123 'detach_attached/2'(V, C)
2125 'attached/2__1'(A, B, C) :-
2126 B==no,
2127 'chr via_1'(A, D),
2128 get_attr(D, chr_translate_bootstrap1, E),
2129 E=v(F, G, H, I, J, K, L),
2130 F/\32=:=32, !,
2131 'attached/2__1__0'(L, A, B, C).
2132 'attached/2__1__0'([], A, B, C) :-
2133 'attached/2__2'(A, B, C).
2134 'attached/2__1__0'([A|B], C, D, E) :-
2135 ( A=suspension(F, G, H, I, J, K, L, M),
2136 G=mutable(active),
2137 L==C
2138 -> 'chr remove_constraint_internal'(A, N),
2139 'detach_attached/2'(N, A),
2140 'attached/2__1__0'(B, C, D, E)
2141 ; 'attached/2__1__0'(B, C, D, E)
2143 'attached/2__1'(A, B, C) :-
2144 'attached/2__2'(A, B, C).
2145 'attached/2__2'(A, B, C) :-
2146 B==maybe,
2147 'chr via_1'(A, D),
2148 get_attr(D, chr_translate_bootstrap1, E),
2149 E=v(F, G, H, I, J, K, L),
2150 F/\32=:=32,
2151 ( 'chr sbag_member'(M, L),
2152 M=suspension(N, O, P, Q, R, S, T, U),
2153 O=mutable(active),
2154 T==A,
2155 U==maybe
2156 -> true
2157 ), !,
2158 ( var(C)
2159 -> true
2160 ; 'chr remove_constraint_internal'(C, V),
2161 'detach_attached/2'(V, C)
2163 'attached/2__2'(A, B, C) :-
2164 'chr activate_constraint'(D, C, E),
2165 'attach_attached/2'(D, C).
2166 is_attached(A) :-
2167 'is_attached/1__0'(A, B).
2168 'is_attached/1__0'(A, B) :-
2169 'chr via_1'(A, C),
2170 get_attr(C, chr_translate_bootstrap1, D),
2171 D=v(E, F, G, H, I, J, K),
2172 E/\32=:=32,
2173 'chr sbag_member'(L, K),
2174 L=suspension(M, N, O, P, Q, R, S, T),
2175 N=mutable(active),
2176 S==A, !,
2177 ( T==no
2178 -> fail
2179 ; true
2181 'is_attached/1__0'(A, B) :- !.
2182 chr_clear :-
2183 'chr_clear/0__0'(A).
2184 'chr_clear/0__0'(A) :-
2185 'chr global_term_ref_1'(B),
2186 get_attr(B, chr_translate_bootstrap1, C),
2187 C=v(D, E, F, G, H, I, J),
2188 D/\1=:=1, !,
2189 ( var(A)
2190 -> 'chr allocate_constraint'(chr_translate_bootstrap1:'chr_clear/0__0'(A), A, chr_clear, [])
2191 ; true
2193 'chr_clear/0__0__0'(E, A).
2194 'chr_clear/0__0__0'([], A) :-
2195 'chr_clear/0__1'(A).
2196 'chr_clear/0__0__0'([A|B], C) :-
2197 ( A=suspension(D, E, F, G, H, I, J, K),
2198 E=mutable(active)
2199 -> 'chr remove_constraint_internal'(A, L),
2200 'detach_constraint/2'(L, A),
2201 'chr_clear/0__0__0'(B, C)
2202 ; 'chr_clear/0__0__0'(B, C)
2204 'chr_clear/0__0'(A) :-
2205 ( var(A)
2206 -> 'chr allocate_constraint'(chr_translate_bootstrap1:'chr_clear/0__0'(A), A, chr_clear, [])
2207 ; true
2209 'chr_clear/0__1'(A).
2210 'chr_clear/0__1'(A) :-
2211 'chr global_term_ref_1'(B),
2212 get_attr(B, chr_translate_bootstrap1, C),
2213 C=v(D, E, F, G, H, I, J),
2214 D/\2=:=2, !,
2215 'chr_clear/0__1__0'(F, A).
2216 'chr_clear/0__1__0'([], A) :-
2217 'chr_clear/0__2'(A).
2218 'chr_clear/0__1__0'([A|B], C) :-
2219 ( A=suspension(D, E, F, G, H, I, J),
2220 E=mutable(active)
2221 -> 'chr remove_constraint_internal'(A, K),
2222 'detach_constraint_count/1'(K, A),
2223 'chr_clear/0__1__0'(B, C)
2224 ; 'chr_clear/0__1__0'(B, C)
2226 'chr_clear/0__1'(A) :-
2227 'chr_clear/0__2'(A).
2228 'chr_clear/0__2'(A) :-
2229 'chr global_term_ref_1'(B),
2230 get_attr(B, chr_translate_bootstrap1, C),
2231 C=v(D, E, F, G, H, I, J),
2232 D/\4=:=4, !,
2233 'chr_clear/0__2__0'(G, A).
2234 'chr_clear/0__2__0'([], A) :-
2235 'chr_clear/0__3'(A).
2236 'chr_clear/0__2__0'([A|B], C) :-
2237 ( A=suspension(D, E, F, G, H, I, J, K),
2238 E=mutable(active)
2239 -> 'chr remove_constraint_internal'(A, L),
2240 'detach_constraint_index/2'(L, A),
2241 'chr_clear/0__2__0'(B, C)
2242 ; 'chr_clear/0__2__0'(B, C)
2244 'chr_clear/0__2'(A) :-
2245 'chr_clear/0__3'(A).
2246 'chr_clear/0__3'(A) :-
2247 'chr global_term_ref_1'(B),
2248 get_attr(B, chr_translate_bootstrap1, C),
2249 C=v(D, E, F, G, H, I, J),
2250 D/\8=:=8, !,
2251 'chr_clear/0__3__0'(H, A).
2252 'chr_clear/0__3__0'([], A) :-
2253 'chr_clear/0__4'(A).
2254 'chr_clear/0__3__0'([A|B], C) :-
2255 ( A=suspension(D, E, F, G, H, I, J),
2256 E=mutable(active)
2257 -> 'chr remove_constraint_internal'(A, K),
2258 'detach_max_constraint_index/1'(K, A),
2259 'chr_clear/0__3__0'(B, C)
2260 ; 'chr_clear/0__3__0'(B, C)
2262 'chr_clear/0__3'(A) :-
2263 'chr_clear/0__4'(A).
2264 'chr_clear/0__4'(A) :-
2265 'chr global_term_ref_1'(B),
2266 get_attr(B, chr_translate_bootstrap1, C),
2267 C=v(D, E, F, G, H, I, J),
2268 D/\16=:=16, !,
2269 'chr_clear/0__4__0'(I, A).
2270 'chr_clear/0__4__0'([], A) :-
2271 'chr_clear/0__5'(A).
2272 'chr_clear/0__4__0'([A|B], C) :-
2273 ( A=suspension(D, E, F, G, H, I, J),
2274 E=mutable(active)
2275 -> 'chr remove_constraint_internal'(A, K),
2276 'detach_target_module/1'(K, A),
2277 'chr_clear/0__4__0'(B, C)
2278 ; 'chr_clear/0__4__0'(B, C)
2280 'chr_clear/0__4'(A) :-
2281 'chr_clear/0__5'(A).
2282 'chr_clear/0__5'(A) :-
2283 'chr global_term_ref_1'(B),
2284 get_attr(B, chr_translate_bootstrap1, C),
2285 C=v(D, E, F, G, H, I, J),
2286 D/\32=:=32, !,
2287 'chr_clear/0__5__0'(J, A).
2288 'chr_clear/0__5__0'([], A) :-
2289 'chr_clear/0__6'(A).
2290 'chr_clear/0__5__0'([A|B], C) :-
2291 ( A=suspension(D, E, F, G, H, I, J, K),
2292 E=mutable(active)
2293 -> 'chr remove_constraint_internal'(A, L),
2294 'detach_attached/2'(L, A),
2295 'chr_clear/0__5__0'(B, C)
2296 ; 'chr_clear/0__5__0'(B, C)
2298 'chr_clear/0__5'(A) :-
2299 'chr_clear/0__6'(A).
2300 'chr_clear/0__6'(A) :- !.