/*  Generated by CHR bootstrap compiler
    From: D:/temp/2026_05_20_01_50_01_6433/Rtmp80q20F/R.INSTALLe2f846f524afd/rswipl/src/swipl-devel/packages/chr/chr_translate.chr
    Date: Wed May 20 03:35:48 2026

    DO NOT EDIT.  EDIT THE CHR FILE INSTEAD
*/

:- module(chr_translate,
          [ chr_translate/2,
            chr_translate_line_info/3
          ]).
:- use_module(chr(chr_runtime)).
:- style_check(- (discontiguous)).
:- style_check(-singleton).
:- style_check(-no_effect).
:- use_module(library(lists)).
:- use_module(library(sort)).
:- use_module(library(ordsets)).
:- use_module(library(aggregate)).
:- use_module(library(apply)).
:- use_module(library(apply_macros)).
:- use_module(library(occurs)).
:- use_module(library(assoc)).
:- use_module(library(dialect/hprolog)).
:- use_module(chr(pairlist)).
:- use_module(chr(a_star)).
:- use_module(chr(listmap)).
:- use_module(chr(clean_code)).
:- use_module(chr(builtins)).
:- use_module(chr(find)).
:- use_module(chr(binomialheap)).
:- use_module(chr(guard_entailment)).
:- use_module(chr(chr_compiler_options)).
:- use_module(chr(chr_compiler_utility)).
:- use_module(chr(chr_compiler_errors)).
:- include(chr(chr_op)).
:- op(1150, fx, chr_type).
:- op(1150, fx, chr_declaration).
:- op(1130, xfx, --->).
:- op(980, fx, +).
:- op(980, fx, -).
:- op(980, fx, ?).
:- op(1150, fx, constraints).
:- op(1150, fx, chr_constraint).
format_storetype(multi_store(A)) :-
    !,
    maplist(format_storetype, A).
format_storetype(atomic_constants(A, B, _)) :-
    format('\t* a trie index on the argument(s) ~w for the ground terms ~w\n', [A, B]).
format_storetype(ground_constants(A, B, _)) :-
    format('\t* a trie index on the argument(s) ~w for the ground terms ~w\n', [A, B]).
format_storetype(A) :-
    format('\t* ~w\n', [A]).
get_constraint_arg_type(A, B, C) :-
    get_constraint_type(A, D),
    nth1(B, D, E),
    unalias_type(E, C).
partial_store(ground_constants(_, _, incomplete)).
partial_store(atomic_constants(_, _, incomplete)).
late_allocation_analysis(A) :-
    (   chr_pp_flag(late_allocation, on)
    ->  maplist(late_allocation, A)
    ;   true
    ).
late_allocation(A) :-
    late_allocation(A, 0).
late_allocation(A, B) :-
    allocation_occurrence(A, B),
    !.
late_allocation(A, B) :-
    C is B+1,
    late_allocation(A, C).
stored_in_guard_before_next_kept_occurrence(A, B) :-
    chr_pp_flag(store_in_guards, on),
    C is B+1,
    stored_in_guard_lookahead(A, C).
set_constraint_indices(A) :-
    set_constraint_indices(A, 1).
set_constraint_indices([], A) :-
    B is A-1,
    max_constraint_index(B).
set_constraint_indices([A|B], C) :-
    (   (   chr_pp_flag(debugable, on)
        ;   \+ only_ground_indexed_arguments(A),
            is_stored(A)
        ;   is_stored(A),
            get_store_type(A, default)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  constraint_index(A, C),
        D is C+1,
        set_constraint_indices(B, D)
    ;   set_constraint_indices(B, C)
    ).
type_indexed_identifier_structure(A, B) :-
    type_indexed_identifier_name(A,
                                 type_indexed_identifier_struct,
                                 C),
    get_type_indexed_identifier_size(A, D),
    functor(B, C, D).
type_indexed_identifier_name(A, B, C) :-
    (   atom(A)
    ->  D=A
    ;   term_to_atom(A, D)
    ),
    atom_concat_list([B, '_', D], C).
chr_translate(A, B) :-
    chr_translate_line_info(A, bootstrap, B).
chr_translate_line_info(A, B, C) :-
    chr_banner,
    restart_after_flattening(A, D),
    init_chr_pp_flags,
    chr_source_file(B),
    partition_clauses(D, E, F, G),
    chr_compiler_options:sanity_check,
    dump_code(D),
    check_declared_constraints(E),
    generate_show_constraint(E, H, F, I),
    add_constraints(H),
    add_rules(I),
    generate_never_stored_rules(H, J),
    add_rules(J),
    append(I, J, K),
    chr_analysis(K, H, D),
    time('constraint code generation',
         chr_translate:constraints_code(H, L)),
    time('validate store assumptions',
         chr_translate:validate_store_type_assumptions(H)),
    phase_end(validate_store_type_assumptions),
    used_states_known,
    time('store code generation',
         chr_translate:store_management_preds(H, M)),
    insert_declarations(G, N),
    chr_module_declaration(O),
    append([M, L, O, [end_of_file]], P),
    clean_clauses(P, Q),
    append([N, Q], C),
    dump_code(C),
    !.
chr_analysis(A, B, C) :-
    maplist(pragma_rule_to_ast_rule, A, D),
    check_rules(A, D, B),
    time('type checking', chr_translate:static_type_check(A, D)),
    collect_constants(A, D, B, C),
    add_occurrences(A, D),
    time('functional dependency',
         chr_translate:functional_dependency_analysis(A)),
    time('set semantics', chr_translate:set_semantics_rules(A)),
    time('symmetry analysis', chr_translate:symmetry_analysis(A)),
    time('guard simplification', chr_translate:guard_simplification),
    time('late storage', chr_translate:storage_analysis(B)),
    time(observation, chr_translate:observation_analysis(B)),
    time('ai observation', chr_translate:ai_observation_analysis(B)),
    time('late allocation', chr_translate:late_allocation_analysis(B)),
    partial_wake_analysis,
    time('assume constraint stores',
         chr_translate:assume_constraint_stores(B)),
    time('default constraint indices',
         chr_translate:set_constraint_indices(B)),
    time('check storedness assertions',
         chr_translate:check_storedness_assertions(B)),
    time('continuation analysis', chr_translate:continuation_analysis(B)).
store_management_preds(A, B) :-
    generate_attach_detach_a_constraint_all(A, C),
    generate_attr_unify_hook(D),
    generate_attach_increment(E),
    generate_extra_clauses(A, F),
    generate_insert_delete_constraints(A, G),
    generate_attach_code(A, H),
    generate_counter_code(I),
    generate_dynamic_type_check_clauses(J),
    append([C, E, D, F, G, H, I, J],
           B).
insert_declarations(A, B) :-
    findall((:-use_module(chr(C))),
            ( auxiliary_module(C),
              is_used_auxiliary_module(C)
            ),
            D),
    append(A, [(:-use_module(chr(chr_runtime)))|D], B).
auxiliary_module(chr_hashtable_store).
auxiliary_module(chr_integertable_store).
auxiliary_module(chr_assoc_store).
generate_counter_code(A) :-
    (   chr_pp_flag(store_counter, on)
    ->  A=[('$counter_init'(B):-nb_setval(B, 0)), ('$counter'(C, D):-nb_getval(C, D)), ('$counter_inc'(E):-nb_getval(E, F), G is F+1, nb_setval(E, G)), (:-'$counter_init'('$insert_counter')), (:-'$counter_init'('$delete_counter')), ('$insert_counter_inc':-'$counter_inc'('$insert_counter')), ('$delete_counter_inc':-'$counter_inc'('$delete_counter')), (counter_stats(H, I):-'$counter'('$insert_counter', H), '$counter'('$delete_counter', I))]
    ;   A=[]
    ).
chr_module_declaration([]) :-
    tmp_module,
    !.
chr_module_declaration(A) :-
    get_target_module(B),
    (   B\==chr_translate,
        chr_pp_flag(toplevel_show_store, on)
    ->  A=[(:-multifile chr:'$chr_module'/1), chr:'$chr_module'(B)]
    ;   A=[]
    ).
partition_clauses([], [], [], []).
partition_clauses([A|B], C, D, E) :-
    (   parse_rule(A, F)
    ->  C=G,
        D=[F|H],
        E=I
    ;   is_declaration(A, J)
    ->  append(J, G, C),
        D=H,
        E=I
    ;   is_module_declaration(A, K)
    ->  target_module(K),
        C=G,
        D=H,
        E=[A|I]
    ;   is_type_definition(A)
    ->  C=G,
        D=H,
        E=I
    ;   is_chr_declaration(A)
    ->  C=G,
        D=H,
        E=I
    ;   A=handler(_)
    ->  chr_warning(deprecated(A),
                    'Backward compatibility: ignoring handler/1 declaration.\n',
                    []),
        C=G,
        D=H,
        E=I
    ;   A=rules(_)
    ->  chr_warning(deprecated(A),
                    'Backward compatibility: ignoring rules/1 declaration.\n',
                    []),
        C=G,
        D=H,
        E=I
    ;   A=option(L, M)
    ->  chr_warning(deprecated(A), 'Instead use `:-chr_option(~w,~w).\'\n', [L, M]),
        handle_option(L, M),
        C=G,
        D=H,
        E=I
    ;   A=(:-chr_option(L, M))
    ->  handle_option(L, M),
        C=G,
        D=H,
        E=I
    ;   A='$chr_compiled_with_version'(_)
    ->  C=G,
        D=H,
        E=['$chr_compiled_with_version'(3)|I]
    ;   C=G,
        D=H,
        E=[A|I]
    ),
    partition_clauses(B, G, H, I).
'$chr_compiled_with_version'(3).
is_declaration(A, B) :-
    (   A=(:-C),
        C=..[D, E],
        D==(chr_constraint)
    ->  conj2list(E, F)
    ;   (   A=(:-C)
        ->  C=..[constraints, E]
        ;   A=..[constraints, E]
        ),
        conj2list(E, F),
        chr_warning(deprecated(A), 'Instead use :- chr_constraint ~w.\n', [E])
    ),
    extract_type_mode(F, B).
extract_type_mode([], []).
extract_type_mode([A/B|C], [A/B|D]) :-
    !,
    extract_type_mode(C, D).
extract_type_mode([A|B], [C|D]) :-
    (   A= #(E, F)
    ->  functor(E, G, H),
        extract_annotation(F, G/H)
    ;   A=E,
        functor(E, G, H)
    ),
    C=G/H,
    E=..[_|I],
    extract_types_and_modes(I, J, K),
    assert_constraint_type(C, J),
    constraint_mode(C, K),
    extract_type_mode(B, D).
extract_annotation(stored, A) :-
    stored_assertion(A).
extract_annotation(default(A), B) :-
    never_stored_default(B, A).
extract_types_and_modes([], [], []).
extract_types_and_modes([A|B], [C|D], [E|F]) :-
    extract_type_and_mode(A, C, E),
    extract_types_and_modes(B, D, F).
extract_type_and_mode(+A, A, +) :-
    !.
extract_type_and_mode(?A, A, ?) :-
    !.
extract_type_and_mode(-A, A, -) :-
    !.
extract_type_and_mode(+, any, +) :-
    !.
extract_type_and_mode(?, any, ?) :-
    !.
extract_type_and_mode(-, any, -) :-
    !.
extract_type_and_mode(A, _, _) :-
    chr_error(syntax(A), 'Illegal mode/type declaration.\n\tCorrect syntax is +type, -type or ?type\n\tor +, - or ?.\n', []).
is_chr_declaration(A) :-
    A=(:-chr_declaration B),
    (   B=(C--->D)
    ->  background_info(C, D)
    ;   B=D
    ->  background_info([D])
    ).
is_type_definition(A) :-
    is_type_definition(A, B),
    assert_type_definition(B).
assert_type_definition(typedef(A, B)) :-
    type_definition(A, B).
assert_type_definition(alias(A, B)) :-
    type_alias(A, B).
is_type_definition(A, B) :-
    (   A=(:-C)
    ->  true
    ;   A=C
    ),
    C=..[chr_type, D],
    (   D=(E--->F)
    ->  tdisj2list(F, G),
        B=typedef(E, G)
    ;   D=(H==E)
    ->  B=alias(H, E)
    ;   B=typedef(D, []),
        chr_warning(syntax, 'Empty type definition `~w\'.\nAre you sure you want to declare a phantom type?\n', [A])
    ).
tdisj2list(A, B) :-
    tdisj2list(A, B, []).
tdisj2list(A, B, C) :-
    A=(D;E),
    !,
    tdisj2list(D, B, F),
    tdisj2list(E, F, C).
tdisj2list(A, [A|B], B).
parse_rule(A, B) :-
    A= @(C, D),
    !,
    rule(D, yes(C), B).
parse_rule(A, B) :-
    rule(A, no, B).
rule(A, B, C) :-
    A=pragma(D, E),
    !,
    (   var(E)
    ->  F=[_]
    ;   conj2list(E, F)
    ),
    inc_rule_count(G),
    C=pragma(H, I, F, B, G),
    is_rule(D, H, I, C).
rule(A, B, C) :-
    inc_rule_count(D),
    C=pragma(E, F, [], B, D),
    is_rule(A, E, F, C).
is_rule(A, B, C, D) :-
    A=(E==>F),
    !,
    conj2list(E, G),
    get_ids(G, H, I, D),
    C=ids([], H),
    (   F=(J| K)
    ->  B=rule([], I, J, K)
    ;   B=rule([], I, true, F)
    ).
is_rule(A, B, C, D) :-
    A= <=>(E, F),
    !,
    (   F=(G| H)
    ->  I=G,
        J=H
    ;   I=true,
        J=F
    ),
    (   E= \(K, L)
    ->  conj2list(K, M),
        conj2list(L, N),
        get_ids(M, O, P, 0, Q, D),
        get_ids(N, R, S, Q, _, D),
        C=ids(R, O)
    ;   conj2list(E, N),
        P=[],
        get_ids(N, R, S, D),
        C=ids(R, [])
    ),
    B=rule(S, P, I, J).
get_ids(A, B, C, D) :-
    get_ids(A, B, C, 0, _, D).
get_ids([], [], [], A, A, _).
get_ids([A|B], [C|D], [E|F], C, G, H) :-
    (   A= #(E, I)
    ->  (   var(I)
        ->  I=C
        ;   check_direct_pragma(I, C, H)
        )
    ;   E=A
    ),
    J is C+1,
    get_ids(B, D, F, J, G, H).
check_direct_pragma(passive, A, B) :-
    !,
    B=pragma(_, _, _, _, C),
    passive(C, A).
check_direct_pragma(A, _, B) :-
    (   direct_pragma(C),
        atom_concat(A, _, C)
    ->  chr_warning(problem_pragma(A, B), 'completed `~w\' to `~w\'\n', [A, C])
    ;   chr_warning(unsupported_pragma(A, B), '', [])
    ).
direct_pragma(passive).
is_module_declaration((:-module(A)), A).
is_module_declaration((:-module(A, _)), A).
add_constraints([]).
add_constraints([A|B]) :-
    max_occurrence(A, 0),
    A=_/C,
    length(D, C),
    set_elems(D, ?),
    constraint_mode(A, D),
    add_constraints(B).
add_rules([]).
add_rules([A|B]) :-
    A=pragma(_, _, _, _, C),
    rule(C, A),
    add_rules(B).
check_declared_constraints(A) :-
    tree_set_empty(B),
    check_declared_constraints(A, B).
check_declared_constraints([], _).
check_declared_constraints([A|B], C) :-
    (   tree_set_memberchk(A, C)
    ->  chr_error(syntax(A), 'Constraint multiply defined: ~w.\n\tRemove redundant declaration!\n', [A])
    ;   true
    ),
    tree_set_add(C, A, D),
    check_declared_constraints(B, D).
check_rules(A, B, C) :-
    maplist(check_rule(C), A, B).
check_rule(A, B, C) :-
    B=pragma(_, _, D, _, _),
    check_ast_rule_indexing(C, B),
    check_ast_trivial_propagation_rule(C, B),
    check_ast_head_constraints(C, A, B),
    check_pragmas(D, B).
check_ast_trivial_propagation_rule(A, B) :-
    A=ast_rule(C, _, _, D, _),
    (   C=propagation(_),
        D==[]
    ->  chr_warning(weird_program,
                    'Ignoring propagation rule with empty body: ~@.\n\t\n',
                    [format_rule(B)]),
        set_rule_passive(B)
    ;   true
    ).
set_rule_passive(A) :-
    A=pragma(_, _, _, _, B),
    set_all_passive(B).
check_trivial_propagation_rule(A) :-
    A=pragma(B, _, _, _, C),
    (   B=rule([], _, _, true)
    ->  chr_warning(weird_program,
                    'Ignoring propagation rule with empty body: ~@.\n\t\n',
                    [format_rule(A)]),
        set_all_passive(C)
    ;   true
    ).
check_ast_head_constraints(ast_rule(A, _, _, _, _), B, C) :-
    check_ast_head_constraints_(A, B, C).
check_ast_head_constraints_(simplification(A), B, C) :-
    maplist(check_ast_head_constraint(B, C), A).
check_ast_head_constraints_(propagation(A), B, C) :-
    maplist(check_ast_head_constraint(B, C), A).
check_ast_head_constraints_(simpagation(A, B), C, D) :-
    maplist(check_ast_head_constraint(C, D), A),
    maplist(check_ast_head_constraint(C, D), B).
check_ast_head_constraint(A, B, chr_constraint(C, _, D)) :-
    (   memberchk(C, A)
    ->  true
    ;   chr_error(syntax(D), 'Undeclared constraint ~w in head of ~@.\n\tConstraint should be one of ~w.\n', [_/_, format_rule(B), A])
    ).
check_head_constraints([], _, _).
check_head_constraints([A|B], C, D) :-
    functor(A, E, F),
    (   memberchk(E/F, C)
    ->  check_head_constraints(B, C, D)
    ;   chr_error(syntax(A), 'Undeclared constraint ~w in head of ~@.\n\tConstraint should be one of ~w.\n', [E/F, format_rule(D), C])
    ).
check_pragmas([], _).
check_pragmas([A|B], C) :-
    check_pragma(A, C),
    check_pragmas(B, C).
check_pragma(A, B) :-
    var(A),
    !,
    chr_error(syntax(A), 'Invalid pragma ~w in ~@.\n\tPragma should not be a variable!\n', [A, format_rule(B)]).
check_pragma(passive(A), B) :-
    !,
    B=pragma(_, ids(C, D), _, _, E),
    (   memberchk_eq(A, C)
    ->  true
    ;   memberchk_eq(A, D)
    ->  true
    ;   chr_error(syntax(A),
                  'Invalid identifier ~w in pragma passive in ~@.\n',
                  [A, format_rule(B)])
    ),
    passive(E, A).
check_pragma(mpassive(A), B) :-
    !,
    B=pragma(_, _, _, _, C),
    chr_warning(experimental, 'Experimental pragma ~w. Use with care!\n', [mpassive(A)]),
    maplist(passive(C), A).
check_pragma(A, B) :-
    A=already_in_heads,
    !,
    chr_warning(unsupported_pragma(A, B),
                'Termination and correctness may be affected.\n',
                []).
check_pragma(A, B) :-
    A=already_in_head(_),
    !,
    chr_warning(unsupported_pragma(A, B),
                'Termination and correctness may be affected.\n',
                []).
check_pragma(A, B) :-
    A=no_history,
    !,
    chr_warning(experimental, 'Experimental pragma no_history. Use with care!\n', []),
    B=pragma(_, _, _, _, C),
    no_history(C).
check_pragma(A, B) :-
    A=history(C, D),
    !,
    B=pragma(_, ids(E, F), _, _, G),
    chr_warning(experimental, 'Experimental pragma ~w. Use with care!\n', [A]),
    (   E\==[]
    ->  chr_error(syntax(A),
                  'Pragma history only implemented for propagation rules.\n',
                  [])
    ;   \+ atom(C)
    ->  chr_error(syntax(A),
                  'Illegal argument for pragma history: ~w is not an atom (rule number ~w).\n',
                  [C, G])
    ;   \+ is_set(D)
    ->  chr_error(syntax(A),
                  'Illegal argument for pragma history: ~w is not a set (rule number ~w).\n',
                  [D, G])
    ;   check_history_pragma_ids(D, E, F)
    ->  history(G, C, D)
    ;   chr_error(syntax(A),
                  'Invalid identifier(s) in pragma ~w of rule number ~w.\n',
                  [A, G])
    ).
check_pragma(A, B) :-
    A=source_location(C),
    !,
    B=pragma(_, _, _, _, D),
    source_location(D, C).
check_history_pragma_ids([], _, _).
check_history_pragma_ids([A|B], C, D) :-
    (   memberchk_eq(A, D)
    ;   memberchk_eq(A, C)
    ),
    check_history_pragma_ids(B, C, D).
check_pragma(A, B) :-
    chr_error(syntax(A), 'Unknown pragma ~w in ~@.\n', [A, format_rule(B)]).
test_named_history_id_pairs(_, [], _, []).
test_named_history_id_pairs(A, [B|C], D, [E|F]) :-
    test_named_history_id_pair(A, B, D, E),
    test_named_history_id_pairs(A, C, D, F).
format_rule(A) :-
    A=pragma(_, _, _, B, C),
    (   B=yes(D)
    ->  write('rule '),
        write(D)
    ;   write('rule number '),
        write(C)
    ),
    get_line_number(C, E),
    write(' at '),
    write(E).
check_ast_rule_indexing(A, B) :-
    A=ast_rule(C, D, _, _, _),
    tree_set_empty(E),
    ast_head_variables(C, E, F),
    ast_remove_anti_monotonic_guards(D, F, G),
    ast_term_list_variables(G, E, H),
    check_ast_head_indexing(C, H),
    (   chr_pp_flag(term_indexing, on)
    ->  B=pragma(I, _, _, _, _),
        I=rule(J, K, L, _),
        term_variables(J-K, M),
        remove_anti_monotonic_guards(L, M, N),
        term_variables(N, O),
        append(J, K, P),
        check_specs_indexing(P, O, _)
    ;   true
    ).
check_ast_head_indexing(simplification(A), B) :-
    check_ast_indexing(A, B).
check_ast_head_indexing(propagation(A), B) :-
    check_ast_indexing(A, B).
check_ast_head_indexing(simpagation(A, B), C) :-
    ast_constraint_list_variables(B, C, D),
    check_ast_indexing(A, D),
    ast_constraint_list_variables(A, C, E),
    check_ast_indexing(B, E).
check_rule_indexing(A) :-
    A=pragma(B, _, _, _, _),
    B=rule(C, D, E, _),
    term_variables(C-D, F),
    remove_anti_monotonic_guards(E, F, G),
    check_indexing(C, G-D),
    check_indexing(D, G-C),
    (   chr_pp_flag(term_indexing, on)
    ->  term_variables(G, H),
        append(C, D, I),
        check_specs_indexing(I, H, _)
    ;   true
    ).
remove_anti_monotonic_guards(A, B, C) :-
    conj2list(A, D),
    remove_anti_monotonic_guard_list(D, B, E),
    list2conj(E, C).
remove_anti_monotonic_guard_list([], _, []).
remove_anti_monotonic_guard_list([A|B], C, D) :-
    (   A=var(E),
        memberchk_eq(E, C)
    ->  D=F
    ;   D=[A|F]
    ),
    remove_anti_monotonic_guard_list(B, C, F).
ast_remove_anti_monotonic_guards([], _, []).
ast_remove_anti_monotonic_guards([A|B], C, D) :-
    (   A=compound(var, 1, [E], _),
        ast_var_memberchk(E, C)
    ->  D=F
    ;   D=[A|F]
    ),
    ast_remove_anti_monotonic_guards(B, C, F).
check_ast_indexing([], _).
check_ast_indexing([A|B], C) :-
    A=chr_constraint(D, E, _),
    ast_constraint_list_variables(B, C, F),
    check_ast_indexing(E, 1, D, F),
    ast_constraint_variables(A, C, G),
    check_ast_indexing(B, G).
check_ast_indexing([], _, _, _).
check_ast_indexing([A|B], C, D, E) :-
    (   is_indexed_argument(D, C)
    ->  true
    ;   ast_nonvar(A)
    ->  indexed_argument(D, C)
    ;   ast_term_list_variables(B, E, F),
        (   ast_var_memberchk(A, F)
        ->  indexed_argument(D, C)
        ;   true
        )
    ),
    G is C+1,
    ast_term_variables(A, E, H),
    check_ast_indexing(B, G, D, H).
check_indexing([], _).
check_indexing([A|B], C) :-
    functor(A, D, E),
    A=..[_|F],
    term_variables(B-C, G),
    check_indexing(F, 1, D/E, G),
    check_indexing(B, [A|C]).
check_indexing([], _, _, _).
check_indexing([A|B], C, D, E) :-
    (   is_indexed_argument(D, C)
    ->  true
    ;   nonvar(A)
    ->  indexed_argument(D, C)
    ;   term_variables(B, F),
        append(F, E, G),
        (   memberchk_eq(A, G)
        ->  indexed_argument(D, C)
        ;   true
        )
    ),
    H is C+1,
    term_variables(A, I),
    append(I, E, J),
    check_indexing(B, H, D, J).
check_specs_indexing([], _, []).
check_specs_indexing([A|B], C, D) :-
    D=[E|F],
    term_variables(B, G, C),
    check_spec_indexing(A, G, E),
    term_variables(A, H, C),
    check_specs_indexing(B, H, F).
check_spec_indexing(A, B, C) :-
    functor(A, D, E),
    C=spec(D, E, F),
    A=..[_|G],
    check_args_spec_indexing(G, 1, B, F),
    indexing_spec(D/E, [F]).
check_args_spec_indexing([], _, _, []).
check_args_spec_indexing([A|B], C, D, E) :-
    term_variables(B, F, D),
    (   check_arg_spec_indexing(A, C, F, G)
    ->  E=[G|H]
    ;   E=H
    ),
    I is C+1,
    term_variables(A, J, D),
    check_args_spec_indexing(B, I, J, H).
check_arg_spec_indexing(A, B, C, D) :-
    (   var(A)
    ->  memberchk_eq(A, C),
        D=specinfo(B, any, [])
    ;   functor(A, E, F),
        D=specinfo(B, E/F, [G]),
        A=..[_|H],
        check_args_spec_indexing(H, 1, C, G)
    ).
add_occurrences(A, B) :-
    maplist(add_rule_occurrences, A, B).
add_rule_occurrences(A, B) :-
    A=pragma(_, C, _, _, D),
    B=ast_rule(E, _, _, _, _),
    add_head_occurrences(E, C, D).
add_head_occurrences(simplification(A), ids(B, _), C) :-
    maplist(add_constraint_occurrence(C, simplification), A, B).
add_head_occurrences(propagation(A), ids(_, B), C) :-
    maplist(add_constraint_occurrence(C, propagation), A, B).
add_head_occurrences(simpagation(A, B), ids(C, D), E) :-
    maplist(add_constraint_occurrence(E, simplification), A, C),
    maplist(add_constraint_occurrence(E, propagation), B, D).
add_constraint_occurrence(A, B, C, D) :-
    C=chr_constraint(E, _, _),
    new_occurrence(E, A, D, B).
observation_analysis(A, B, C, D) :-
    (   all_spawned(A, C)
    ->  true
    ;   var(B)
    ->  spawns_all(A, C)
    ;   B=true
    ->  true
    ;   B=fail
    ->  true
    ;   B=!
    ->  true
    ;   B=(E, F)
    ->  observation_analysis(A, E, C, D),
        observation_analysis(A, F, C, D)
    ;   B=(E;F)
    ->  observation_analysis(A, E, C, D),
        observation_analysis(A, F, C, D)
    ;   B=(E->F)
    ->  observation_analysis(A, E, C, D),
        observation_analysis(A, F, C, D)
    ;   B=(\+G)
    ->  observation_analysis(A, G, C, D)
    ;   functor(B, H, I),
        memberchk(H/I, D)
    ->  spawns(A, C, H/I)
    ;   B=(_=_)
    ->  spawns_all_triggers(A, C)
    ;   B=(_ is _)
    ->  spawns_all_triggers(A, C)
    ;   builtin_binds_b(B, J)
    ->  (   J==[]
        ->  true
        ;   spawns_all_triggers(A, C)
        )
    ;   spawns_all(A, C)
    ).
is_observed(A, B) :-
    is_observed(A, B, _),
    ai_is_observed(A, B).
is_stored_in_guard(A, B) :-
    chr_pp_flag(store_in_guards, on),
    do_is_observed(A, B, guard).
generate_attach_detach_a_constraint_all([], []).
generate_attach_detach_a_constraint_all([A|B], C) :-
    (   (   chr_pp_flag(debugable, on)
        ;   is_stored(A),
            \+ only_ground_indexed_arguments(A),
            \+ get_store_type(A, var_assoc_store(_, _))
        )
    ->  generate_attach_a_constraint(A, D),
        generate_detach_a_constraint(A, E)
    ;   D=[],
        E=[]
    ),
    generate_attach_detach_a_constraint_all(B, F),
    append([D, E, F], C).
generate_attach_a_constraint(A, [B, C]) :-
    generate_attach_a_constraint_nil(A, B),
    generate_attach_a_constraint_cons(A, C).
attach_constraint_atom(A, B, C, D) :-
    make_name(attach_, A, E),
    D=..[E, B, C].
generate_attach_a_constraint_nil(A, B) :-
    B=(C:-true),
    attach_constraint_atom(A, [], _, C).
generate_attach_a_constraint_cons(A, B) :-
    B=(C:-D),
    attach_constraint_atom(A, [E|F], G, C),
    attach_constraint_atom(A, F, G, H),
    D=(I, J, H),
    get_max_constraint_index(K),
    (   K==1
    ->  generate_attach_body_1(A, E, G, I)
    ;   generate_attach_body_n(A, E, G, I)
    ),
    chr_pp_flag(solver_events, L),
    (   L\==none
    ->  _=[[E|_], G],
        get_target_module(M),
        use_auxiliary_predicate(run_suspensions),
        J=clp_events:subscribe(E, L, M, M:'$run_suspensions'([G]))
    ;   J=true
    ).
generate_attach_body_1(_, A, B, C) :-
    get_target_module(D),
    C=(get_attr(A, D, E)->put_attr(A, D, [B|E]);put_attr(A, D, [B])).
generate_attach_body_n(A/B, C, D, E) :-
    chr_pp_flag(experiment, off),
    !,
    get_constraint_index(A/B, F),
    get_max_constraint_index(G),
    get_target_module(H),
    add_attr(G, D, F, I, J, K),
    singleton_attr(G, D, F, L),
    E=(get_attr(C, H, I)->J, put_attr(C, H, K);put_attr(C, H, L)),
    !.
generate_attach_body_n(A/B, C, D, E) :-
    chr_pp_flag(experiment, on),
    !,
    get_constraint_index(A/B, F),
    or_pattern(F, G),
    H is F+1,
    get_max_constraint_index(I),
    get_target_module(J),
    singleton_attr(I, D, F, K),
    E=(get_attr(C, J, L)->arg(1, L, M), arg(H, L, N), O is M\/G, setarg(1, L, O), setarg(H, L, [D|N]);put_attr(C, J, K)),
    !.
generate_detach_a_constraint(A, [B, C]) :-
    generate_detach_a_constraint_nil(A, B),
    generate_detach_a_constraint_cons(A, C).
detach_constraint_atom(A, B, C, D) :-
    make_name(detach_, A, E),
    D=..[E, B, C].
generate_detach_a_constraint_nil(A, B) :-
    B=(C:-true),
    detach_constraint_atom(A, [], _, C).
generate_detach_a_constraint_cons(A, B) :-
    B=(C:-D),
    detach_constraint_atom(A, [E|F], G, C),
    detach_constraint_atom(A, F, G, H),
    D=(I, H),
    get_max_constraint_index(J),
    (   J==1
    ->  generate_detach_body_1(A, E, G, I)
    ;   generate_detach_body_n(A, E, G, I)
    ).
generate_detach_body_1(_, A, B, C) :-
    get_target_module(D),
    C=(get_attr(A, D, E)->'chr sbag_del_element'(E, B, F), (F==[]->del_attr(A, D);put_attr(A, D, F));true).
generate_detach_body_n(A/B, C, D, E) :-
    get_constraint_index(A/B, F),
    get_max_constraint_index(G),
    rem_attr(G, C, D, F, H, I),
    get_target_module(J),
    E=(get_attr(C, J, H)->I;true),
    !.
create_indexed_variables_body([], [], [], _, _, _, empty, 0).
create_indexed_variables_body([A|B], [C|D], [E|F], G, H, I, J, K) :-
    L is H+1,
    create_indexed_variables_body(B,
                                  D,
                                  F,
                                  M,
                                  L,
                                  I,
                                  N,
                                  O),
    (   C==(?),
        is_indexed_argument(I, H)
    ->  (   atomic_type(E)
        ->  J=((var(A)->G=[A|M];G=M), P),
            (   N==empty
            ->  P=true,
                M=[]
            ;   P=N
            )
        ;   N==empty
        ->  J=term_variables(A, G)
        ;   J=(term_variables(A, G, M), N)
        ),
        K=O
    ;   C==(-),
        is_indexed_argument(I, H)
    ->  (   N==empty
        ->  J=(G=[A])
        ;   J=(G=[A|M], N)
        ),
        K is O+1
    ;   G=M,
        J=N,
        K is O+1
    ).
spectermvars(A, B, C, D, E, F) :-
    spectermvars(B, 1, A, C, D, F, [], E).
spectermvars([], A, _, _, B, C, C, true) :-
    A>B,
    !.
spectermvars([A|B], C, D, E, F, G, H, I) :-
    I=(J, K),
    argspecs(D, C, L, M),
    merge_argspecs(L, N),
    arggoal(N, A, J, G, O),
    P is C+1,
    spectermvars(B,
                 P,
                 M,
                 E,
                 F,
                 O,
                 H,
                 K).
argspecs([], _, [], []).
argspecs([[]|A], B, C, D) :-
    argspecs(A, B, C, D).
argspecs([[specinfo(A, B, C)|D]|E], F, G, H) :-
    (   F==A
    ->  G=[specinfo(A, B, C)|I],
        (   D=[]
        ->  J=H
        ;   H=[D|J]
        )
    ;   G=I,
        H=[[specinfo(A, B, C)|D]|J]
    ),
    argspecs(E, F, I, J).
merge_argspecs(A, B) :-
    sort(A, C),
    merge_argspecs_(C, B).
merge_argspecs_([], []).
merge_argspecs_([A], B) :-
    !,
    B=[A].
merge_argspecs_([specinfo(A, B, C), specinfo(A, D, E)|F], G) :-
    (   (   B==any
        ;   D==any
        )
    ->  merge_argspecs_([specinfo(A, any, [])|F], G)
    ;   B==D
    ->  append(C, E, H),
        merge_argspecs_([specinfo(A, B, H)|F], G)
    ;   G=[specinfo(A, B, C)|I],
        merge_argspecs_([specinfo(A, D, E)|F], I)
    ).
arggoal(A, B, C, D, E) :-
    (   A==[]
    ->  D=E,
        C=true
    ;   A=[specinfo(_, any, _)]
    ->  C=term_variables(B, D, E)
    ;   C=(var(B)->D=[B|E];F),
        arggoal_cases(A, B, D, E, F)
    ).
arggoal_cases([], _, A, B, A=B).
arggoal_cases([specinfo(_, A, B)|C], D, E, F, G) :-
    (   B==[]
    ->  G=H
    ;   B==[[]]
    ->  G=H
    ;   A=I/J
    ->  G=(K;H),
        functor(L, I, J),
        L=..[_|M],
        K=(D=L->N),
        spectermvars(M,
                     1,
                     B,
                     I,
                     J,
                     E,
                     F,
                     N)
    ),
    arggoal_cases(C, D, E, F, H).
generate_extra_clauses(A, B) :-
    generate_activate_clauses(A, B, C),
    generate_remove_clauses(A, C, D),
    generate_allocate_clauses(A, D, E),
    generate_insert_constraint_internal_clauses(A, E, F),
    generate_novel_production(F, G),
    generate_extend_history(G, H),
    generate_run_suspensions_clauses(A, H, I),
    generate_empty_named_history_initialisations(I, J),
    J=[].
generate_remove_clauses([], A, A).
generate_remove_clauses([A|B], C, D) :-
    generate_remove_clause(A, C, E),
    generate_remove_clauses(B, E, D).
remove_constraint_goal(A, B, C, D, E, F, G) :-
    uses_state(A, removed),
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(remove_constraint_internal, A),
        G=(H, (I==yes->E;D)),
        remove_constraint_atom(A, B, C, I, H)
    ;   delay_phase_end(validate_store_type_assumptions,
                        generate_remove_body(A,
                                             B,
                                             C,
                                             D,
                                             E,
                                             F,
                                             G))
    ).
remove_constraint_atom(A, B, C, D, E) :-
    make_name('$remove_constraint_internal_', A, F),
    (   chr_pp_flag(debugable, off),
        (   only_ground_indexed_arguments(A)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  E=..[F, B, D]
    ;   E=..[F, B, C, D]
    ).
generate_remove_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(remove_constraint_internal, A)
    ->  B=[D|C],
        D=(E:-F),
        remove_constraint_atom(A, G, H, I, E),
        generate_remove_body(A,
                             G,
                             H,
                             I=no,
                             I=yes,
                             active,
                             F)
    ;   B=C
    ).
generate_remove_body(A, B, C, D, E, F, G) :-
    (   chr_pp_flag(debugable, off),
        (   only_ground_indexed_arguments(A)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  (   F==active
        ->  get_update_suspension_field(A,
                                        B,
                                        state,
                                        H,
                                        removed,
                                        I,
                                        J,
                                        K),
            if_used_state(A, not_stored_yet, J, true, L),
            if_used_state(A,
                          not_stored_yet,
                          (H==not_stored_yet->D;E),
                          E,
                          M)
        ;   F==partner
        ->  get_update_suspension_field(A,
                                        B,
                                        state,
                                        H,
                                        removed,
                                        I,
                                        _,
                                        K),
            L=true,
            M=E
        ),
        G=(I, L, K, M)
    ;   static_suspension_term(A, N),
        get_static_suspension_term_field(arguments, A, N, O),
        generate_indexed_variables_body(A, O, P, C),
        (   chr_pp_flag(debugable, on)
        ->  A=Q/_,
            get_static_suspension_term_field(functor,
                                             A,
                                             N,
                                             Q)
        ;   true
        ),
        (   F==active
        ->  get_update_static_suspension_field(A,
                                               B,
                                               N,
                                               state,
                                               H,
                                               removed,
                                               J,
                                               K),
            if_used_state(A, not_stored_yet, J, true, L),
            if_used_state(A,
                          not_stored_yet,
                          (H==not_stored_yet->C=[], D;P, E),
                          (P, E),
                          M)
        ;   F==partner
        ->  get_update_static_suspension_field(A,
                                               B,
                                               N,
                                               state,
                                               H,
                                               removed,
                                               _,
                                               K),
            L=true,
            M=(P, E)
        ),
        G=(B=N, L, K, M)
    ).
generate_activate_clauses([], A, A).
generate_activate_clauses([A|B], C, D) :-
    generate_activate_clause(A, C, E),
    generate_activate_clauses(B, E, D).
activate_constraint_goal(A, B, C, D, E, F) :-
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(activate_constraint, A),
        F=(G, (H==yes->B;true)),
        activate_constraint_atom(A,
                                 H,
                                 C,
                                 D,
                                 E,
                                 G)
    ;   delay_phase_end(validate_store_type_assumptions,
                        activate_constraint_body(A,
                                                 B,
                                                 true,
                                                 C,
                                                 D,
                                                 E,
                                                 F))
    ).
activate_constraint_atom(A, B, C, D, E, F) :-
    make_name('$activate_constraint_', A, G),
    (   chr_pp_flag(debugable, off),
        only_ground_indexed_arguments(A)
    ->  F=..[G, B, D]
    ;   chr_pp_flag(debugable, off),
        may_trigger(A),
        get_store_type(A, var_assoc_store(_, _))
    ->  F=..[G, B, D, E]
    ;   chr_pp_flag(debugable, off),
        may_trigger(A),
        get_store_type(A, var_assoc_store(_, _))
    ->  F=..[G, B, C, D, E]
    ;   F=..[G, B, C, D]
    ).
generate_activate_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(activate_constraint, A)
    ->  B=[D|C],
        D=(E:-F),
        activate_constraint_atom(A,
                                 G,
                                 H,
                                 I,
                                 J,
                                 E),
        activate_constraint_body(A,
                                 G=yes,
                                 G=no,
                                 H,
                                 I,
                                 J,
                                 F)
    ;   B=C
    ).
activate_constraint_body(A, B, C, D, E, F, G) :-
    (   chr_pp_flag(debugable, off),
        may_trigger(A),
        uses_field(A, generation)
    ->  get_update_suspension_field(A,
                                    E,
                                    generation,
                                    H,
                                    F,
                                    I,
                                    J,
                                    K),
        L=(I, J, F is H+1, K)
    ;   L=true
    ),
    get_update_suspension_field(A,
                                E,
                                state,
                                M,
                                active,
                                N,
                                O,
                                P),
    if_used_state(A, not_stored_yet, O, true, Q),
    (   chr_pp_flag(debugable, off),
        (   only_ground_indexed_arguments(A)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  if_used_state(A,
                      not_stored_yet,
                      (M==not_stored_yet->B;C),
                      C,
                      R)
    ;   get_dynamic_suspension_term_field(arguments,
                                          A,
                                          E,
                                          S,
                                          T),
        generate_indexed_variables_body(A, S, U, D),
        chr_none_locked(D, V),
        if_used_state(A,
                      not_stored_yet,
                      (M==not_stored_yet->T, U, V, B;C),
                      C,
                      R)
    ),
    G=(N, Q, P, L, R).
generate_allocate_clauses([], A, A).
generate_allocate_clauses([A|B], C, D) :-
    generate_allocate_clause(A, C, E),
    generate_allocate_clauses(B, E, D).
allocate_constraint_goal(A, B, C, D) :-
    uses_state(A, not_stored_yet),
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(allocate_constraint, A),
        allocate_constraint_atom(A, B, C, D)
    ;   D=(B=E, F),
        delay_phase_end(validate_store_type_assumptions,
                        allocate_constraint_body(A,
                                                 E,
                                                 C,
                                                 F))
    ).
allocate_constraint_atom(A, B, C, D) :-
    make_name('$allocate_constraint_', A, E),
    D=..[E, B|C].
generate_allocate_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(allocate_constraint, A)
    ->  B=[D|C],
        D=(E:-F),
        A=_/G,
        length(H, G),
        allocate_constraint_atom(A, I, H, E),
        allocate_constraint_body(A, I, H, F)
    ;   B=C
    ).
allocate_constraint_body(A, B, C, D) :-
    static_suspension_term(A, E),
    get_static_suspension_term_field(arguments, A, E, C),
    (   chr_pp_flag(debugable, on)
    ->  A=F/_,
        get_static_suspension_term_field(functor, A, E, F)
    ;   true
    ),
    (   chr_pp_flag(debugable, on)
    ->  (   may_trigger(A)
        ->  append(C, [B], G),
            build_head(_, _, [0], G, H),
            get_target_module(I),
            J=I:H
        ;   J=true
        ),
        K=(B=E),
        create_static_suspension_field(A,
                                       E,
                                       continuation,
                                       J,
                                       L),
        create_static_suspension_field(A, E, generation, 0, M)
    ;   may_trigger(A),
        uses_field(A, generation)
    ->  create_static_suspension_field(A, E, generation, 0, M),
        B=E,
        K=true,
        L=true
    ;   M=true,
        B=E,
        K=true,
        L=true
    ),
    (   uses_history(A)
    ->  create_static_suspension_field(A, E, history, t, N)
    ;   N=true
    ),
    create_static_suspension_field(A,
                                   E,
                                   state,
                                   not_stored_yet,
                                   O),
    (   has_suspension_field(A, id)
    ->  get_static_suspension_term_field(id, A, E, P),
        gen_id(P, Q)
    ;   Q=true
    ),
    D=(K, L, M, N, O, Q).
gen_id(A, 'chr gen_id'(A)).
generate_insert_constraint_internal_clauses([], A, A).
generate_insert_constraint_internal_clauses([A|B], C, D) :-
    generate_insert_constraint_internal_clause(A, C, E),
    generate_insert_constraint_internal_clauses(B, E, D).
insert_constraint_internal_constraint_goal(A, B, C, D, E, F) :-
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(remove_constraint_internal, A),
        insert_constraint_internal_constraint_atom(A,
                                                   B,
                                                   C,
                                                   D,
                                                   E,
                                                   F)
    ;   delay_phase_end(validate_store_type_assumptions,
                        generate_insert_constraint_internal_body(A,
                                                                 C,
                                                                 D,
                                                                 E,
                                                                 B,
                                                                 F))
    ).
insert_constraint_internal_constraint_atom(A, B, C, D, E, F) :-
    insert_constraint_internal_constraint_name(A, G),
    (   chr_pp_flag(debugable, on)
    ->  F=..[G, B, C, D|E]
    ;   (   only_ground_indexed_arguments(A)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  F=..[G, C|E]
    ;   F=..[G, B, C|E]
    ).
insert_constraint_internal_constraint_name(A, B) :-
    make_name('$insert_constraint_internal_', A, B).
generate_insert_constraint_internal_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(insert_constraint_internal, A)
    ->  B=[D|C],
        D=(E:-F),
        A=_/G,
        length(H, G),
        insert_constraint_internal_constraint_atom(A,
                                                   I,
                                                   J,
                                                   K,
                                                   H,
                                                   E),
        generate_insert_constraint_internal_body(A,
                                                 J,
                                                 K,
                                                 H,
                                                 I,
                                                 F)
    ;   B=C
    ).
generate_insert_constraint_internal_body(A, B, C, D, E, F) :-
    static_suspension_term(A, G),
    create_static_suspension_field(A, G, state, active, H),
    (   chr_pp_flag(debugable, on)
    ->  get_static_suspension_term_field(continuation, A, G, C),
        create_static_suspension_field(A, G, generation, 0, I)
    ;   may_trigger(A),
        uses_field(A, generation)
    ->  create_static_suspension_field(A, G, generation, 0, I)
    ;   I=true
    ),
    (   chr_pp_flag(debugable, on)
    ->  A=J/_,
        get_static_suspension_term_field(functor, A, G, J)
    ;   true
    ),
    (   uses_history(A)
    ->  create_static_suspension_field(A, G, history, t, K)
    ;   K=true
    ),
    get_static_suspension_term_field(arguments, A, G, D),
    _=[_|_],
    (   chr_pp_flag(debugable, off),
        (   only_ground_indexed_arguments(A)
        ;   get_store_type(A, var_assoc_store(_, _))
        )
    ->  suspension_term_base_fields(A, _),
        (   has_suspension_field(A, id)
        ->  get_static_suspension_term_field(id, A, G, L),
            gen_id(L, M)
        ;   M=true
        ),
        F=(B=G, H, I, K, M)
    ;   (   has_suspension_field(A, id)
        ->  get_static_suspension_term_field(id, A, G, L),
            gen_id(L, M)
        ;   M=true
        ),
        generate_indexed_variables_body(A, D, N, E),
        chr_none_locked(E, O),
        F=(B=G, N, O, H, I, K, M)
    ).
generate_novel_production(A, B) :-
    (   is_used_auxiliary_predicate(novel_production)
    ->  A=[C|B],
        C=('$novel_production'(D, E):-arg(3, D, F), (hprolog:get_ds(E, F, _)->fail;true))
    ;   A=B
    ).
generate_extend_history(A, B) :-
    (   is_used_auxiliary_predicate(extend_history)
    ->  A=[C|B],
        C=('$extend_history'(D, E):-arg(3, D, F), hprolog:put_ds(E, F, x, G), setarg(3, D, G))
    ;   A=B
    ).
generate_empty_named_history_initialisations(A, B) :-
    empty_named_history_initialisations(A, B),
    find_empty_named_histories.
empty_named_history_global_variable(A, B) :-
    atom_concat('chr empty named history ', A, B).
empty_named_history_novel_production(A, nb_getval(B, 0)) :-
    empty_named_history_global_variable(A, B).
empty_named_history_extend_history(A, b_setval(B, 1)) :-
    empty_named_history_global_variable(A, B).
generate_run_suspensions_clauses([], A, A).
generate_run_suspensions_clauses([A|B], C, D) :-
    generate_run_suspensions_clause(A, C, E),
    generate_run_suspensions_clauses(B, E, D).
run_suspensions_goal(A, B, C) :-
    make_name('$run_suspensions_', A, D),
    C=..[D, B].
generate_run_suspensions_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(run_suspensions, A)
    ->  B=[D, E|C],
        run_suspensions_goal(A, [], D),
        (   chr_pp_flag(debugable, on)
        ->  run_suspensions_goal(A, [F|G], H),
            get_update_suspension_field(A,
                                        F,
                                        state,
                                        I,
                                        triggered,
                                        J,
                                        K,
                                        L),
            get_update_suspension_field(A,
                                        F,
                                        state,
                                        M,
                                        active,
                                        N,
                                        O,
                                        P),
            get_update_suspension_field(A,
                                        F,
                                        generation,
                                        Q,
                                        R,
                                        S,
                                        T,
                                        U),
            get_dynamic_suspension_term_field(continuation,
                                              A,
                                              F,
                                              V,
                                              W),
            run_suspensions_goal(A, G, X),
            E=(H:-J, K, (I==active->L, S, T, R is Q+1, U, W, ('chr debugging'->('chr debug_event'(wake(F)), call(V);'chr debug_event'(fail(F)), !, fail), ('chr debug_event'(exit(F));'chr debug_event'(redo(F)), fail);call(V)), N, O, (M==triggered->P;true);true), X)
        ;   run_suspensions_goal(A, [F|G], H),
            static_suspension_term(A, Y),
            get_static_suspension_term_field(arguments,
                                             A,
                                             Y,
                                             Z),
            append(Z, [F], A1),
            make_suspension_continuation_goal(A, A1, V),
            run_suspensions_goal(A, G, X),
            (   uses_field(A, generation)
            ->  get_update_static_suspension_field(A,
                                                   F,
                                                   Y,
                                                   generation,
                                                   Q,
                                                   R,
                                                   S,
                                                   U),
                B1=(S, R is Q+1, U)
            ;   B1=true
            ),
            get_update_static_suspension_field(A,
                                               F,
                                               Y,
                                               state,
                                               I,
                                               triggered,
                                               J,
                                               L),
            get_update_static_suspension_field(A,
                                               F,
                                               Y,
                                               state,
                                               M,
                                               active,
                                               C1,
                                               D1),
            if_used_state(A,
                          removed,
                          (J, (I==active->E1;true)),
                          E1,
                          F1),
            E1=(L, B1, V, C1, (M==triggered->D1;true)),
            E=(H:-F=Y, F1, X)
        )
    ;   B=C
    ).
generate_attach_increment(A) :-
    get_max_constraint_index(B),
    (   is_used_auxiliary_predicate(attach_increment),
        B>0
    ->  A=[C, D],
        generate_attach_increment_empty(C),
        (   B==1
        ->  generate_attach_increment_one(D)
        ;   generate_attach_increment_many(B, D)
        )
    ;   A=[]
    ).
generate_attach_increment_empty((attach_increment([], _):-true)).
generate_attach_increment_one(A) :-
    B=attach_increment([C|D], E),
    get_target_module(F),
    chr_not_locked(C, G),
    H=(G, (get_attr(C, F, I)->sort(I, J), 'chr merge_attributes'(E, J, K), put_attr(C, F, K);put_attr(C, F, E)), attach_increment(D, E)),
    A=(B:-H).
generate_attach_increment_many(A, B) :-
    C=attach_increment([D|E], F),
    merge_attributes(A, F, G, H, I),
    get_target_module(J),
    chr_not_locked(D, K),
    L=(K, (get_attr(D, J, G)->H, put_attr(D, J, I);put_attr(D, J, F)), attach_increment(E, F)),
    B=(C:-L).
generate_attr_unify_hook(A) :-
    get_max_constraint_index(B),
    (   B==0
    ->  A=[]
    ;   A=[C|D],
        C=attribute_goals(_, E, E),
        (   B==1
        ->  generate_attr_unify_hook_one(D)
        ;   generate_attr_unify_hook_many(B, D)
        )
    ).
generate_attr_unify_hook_one([A]) :-
    B=attr_unify_hook(C, D),
    get_target_module(E),
    get_indexed_constraint(1, F),
    (   get_store_type(F, G),
        (   G=default
        ;   G=multi_store(H),
            memberchk(default, H)
        )
    ->  make_run_suspensions(I, J, K),
        make_run_suspensions(J, J, L),
        (   atomic_types_suspended_constraint(F)
        ->  M=true,
            J=C,
            N=true,
            O=P,
            Q=(append(C, P, R), sort(R, I)),
            S=true
        ;   M=sort(C, J),
            N=sort(P, O),
            Q='chr merge_attributes'(J, O, I),
            use_auxiliary_predicate(attach_increment),
            S=(compound(D)->term_variables(D, T), attach_increment(T, J);true)
        ),
        U=(M, (var(D)->(get_attr(D, E, P)->N, Q, put_attr(D, E, I), K;put_attr(D, E, J), L);S, L)),
        A=(B:-U)
    ;   get_store_type(F, var_assoc_store(_, _))
    ->  make_run_suspensions(R, R, K),
        Q=merge_into_assoc_store(C, P, R),
        U=(get_attr(D, E, P)->Q, K;put_attr(D, E, C)),
        A=(B:-U)
    ).
generate_attr_unify_hook_many(A, [B]) :-
    chr_pp_flag(dynattr, off),
    !,
    C=attr_unify_hook(D, E),
    get_target_module(F),
    make_attr(A, G, H, D),
    bagof(I,
          J^K^(member(J, H), I=sort(J, K)),
          L),
    list2conj(L, M),
    bagof(K, J^member(sort(J, K), L), N),
    merge_attributes2(A, G, N, O, P, Q),
    get_all_suspensions2(A, Q, R),
    make_attr(A, G, N, S),
    make_run_suspensions_loop(R, N, T),
    make_run_suspensions_loop(N, N, U),
    (   forall(( between(1, A, V),
                 get_indexed_constraint(V, W)
               ),
               atomic_types_suspended_constraint(W))
    ->  X=true
    ;   use_auxiliary_predicate(attach_increment),
        X=(compound(E)->term_variables(E, Y), attach_increment(Y, S);true)
    ),
    Z=(M, (var(E)->(get_attr(E, F, O)->P, put_attr(E, F, Q), T;put_attr(E, F, S), U);X, U)),
    B=(C:-Z).
generate_attr_unify_hook_many(A, B) :-
    C=attr_unify_hook(D, E),
    get_target_module(F),
    normalize_attr(D, G, H),
    normalize_attr(I, J, K),
    merge_attributes(A, H, K, L, M),
    make_run_suspensions(A),
    (   forall(( between(1, A, N),
                 get_indexed_constraint(N, O)
               ),
               atomic_types_suspended_constraint(O))
    ->  P=true
    ;   use_auxiliary_predicate(attach_increment),
        P=(compound(E)->term_variables(E, Q), attach_increment(Q, H);true)
    ),
    R=(G, (var(E)->(get_attr(E, F, I)->J, L, put_attr(E, F, M), '$dispatch_run_suspensions'(M);put_attr(E, F, H), '$dispatch_run_suspensions'(H));P, '$dispatch_run_suspensions'(H))),
    S=(C:-R),
    B=[S, T, U|V],
    T='$dispatch_run_suspensions'([]),
    U=('$dispatch_run_suspensions'([W-X|Y]):-'$dispatch_run_suspensions'(W, X), '$dispatch_run_suspensions'(Y)),
    run_suspensions_dispatchers(A, [], V).
run_suspensions_dispatchers(A, B, C) :-
    (   A>0
    ->  get_indexed_constraint(A, D),
        E=[('$dispatch_run_suspensions'(A, F):-G)|B],
        (   may_trigger(D)
        ->  run_suspensions_goal(D, F, G)
        ;   G=true
        ),
        H is A-1,
        run_suspensions_dispatchers(H, E, C)
    ;   C=B
    ).
make_run_suspensions(A) :-
    (   A>0
    ->  (   get_indexed_constraint(A, B),
            may_trigger(B)
        ->  use_auxiliary_predicate(run_suspensions, B)
        ;   true
        ),
        C is A-1,
        make_run_suspensions(C)
    ;   true
    ).
make_run_suspensions(A, B, C) :-
    make_run_suspensions(1, A, B, C).
make_run_suspensions(A, B, C, D) :-
    (   get_indexed_constraint(A, E),
        may_trigger(E)
    ->  use_auxiliary_predicate(run_suspensions, E),
        (   wakes_partially(E)
        ->  run_suspensions_goal(E, C, D)
        ;   run_suspensions_goal(E, B, D)
        )
    ;   D=true
    ).
make_run_suspensions_loop(A, B, C) :-
    make_run_suspensions_loop(A, B, 1, C).
make_run_suspensions_loop([], [], _, true).
make_run_suspensions_loop([A|B], [C|D], E, (F, G)) :-
    make_run_suspensions(E, A, C, F),
    H is E+1,
    make_run_suspensions_loop(B, D, H, G).
generate_insert_delete_constraints([], []).
generate_insert_delete_constraints([A|B], C) :-
    (   is_stored(A)
    ->  generate_insert_delete_constraint(A, C, D)
    ;   C=D
    ),
    generate_insert_delete_constraints(B, D).
generate_insert_delete_constraint(A, B, C) :-
    insert_constraint_clause(A, B, D),
    delete_constraint_clause(A, D, C).
insert_constraint_goal(A, B, C, D) :-
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(insert_in_store, A),
        insert_constraint_atom(A, B, D)
    ;   delay_phase_end(validate_store_type_assumptions,
                        (insert_constraint_body(A, B, E, D), insert_constraint_direct_used_vars(E, C)))
    ).
insert_constraint_direct_used_vars([], _).
insert_constraint_direct_used_vars([A-B|C], D) :-
    nth1(A, D, B),
    insert_constraint_direct_used_vars(C, D).
insert_constraint_atom(A, B, C) :-
    make_name('$insert_in_store_', A, D),
    C=..[D, B].
insert_constraint_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(insert_in_store, A)
    ->  B=[D|C],
        D=(E:-F, G, H),
        insert_constraint_atom(A, I, E),
        insert_constraint_body(A, I, J, H),
        insert_constraint_used_vars(J, A, I, G),
        (   chr_pp_flag(store_counter, on)
        ->  F='$insert_counter_inc'
        ;   F=true
        )
    ;   B=C
    ).
insert_constraint_used_vars([], _, _, true).
insert_constraint_used_vars([A-B|C], D, E, (F, G)) :-
    get_dynamic_suspension_term_field(argument(A),
                                      D,
                                      E,
                                      B,
                                      F),
    insert_constraint_used_vars(C, D, E, G).
insert_constraint_body(A, B, C, D) :-
    get_store_type(A, E),
    insert_constraint_body(E, A, B, C, D).
insert_constraint_body(default, A, B, [], C) :-
    global_list_store_name(A, D),
    make_get_store_goal(D, E, F),
    make_update_store_goal(D, G, H),
    (   chr_pp_flag(debugable, on)
    ->  G=[B|E],
        C=(F, H)
    ;   set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          I,
                                          G,
                                          J),
        C=(F, G=[B|E], H, (E=[I|_]->J;true))
    ).
insert_constraint_body(multi_inthash(A), B, C, [], D) :-
    generate_multi_inthash_insert_constraint_bodies(A,
                                                    B,
                                                    C,
                                                    D).
insert_constraint_body(multi_hash(A), B, C, D, E) :-
    generate_multi_hash_insert_constraint_bodies(A,
                                                 B,
                                                 C,
                                                 E,
                                                 D),
    sort_out_used_vars(D, _).
insert_constraint_body(atomic_constants(A, _, _), B, C, D, E) :-
    multi_hash_key_direct(B, A, C, F, D),
    constants_store_index_name(B, A, G),
    H=..[G, F, I],
    E=(H->nb_getval(I, J), b_setval(I, [C|J]);true).
insert_constraint_body(ground_constants(A, _, _), B, C, D, E) :-
    multi_hash_key_direct(B, A, C, F, D),
    constants_store_index_name(B, A, G),
    H=..[G, F, I],
    E=(H->nb_getval(I, J), b_setval(I, [C|J]);true).
insert_constraint_body(global_ground, A, B, [], C) :-
    global_ground_store_name(A, D),
    make_get_store_goal(D, E, F),
    make_update_store_goal(D, G, H),
    (   chr_pp_flag(debugable, on)
    ->  G=[B|E],
        C=(F, H)
    ;   set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          I,
                                          G,
                                          J),
        C=(F, G=[B|E], H, (E=[I|_]->J;true))
    ).
insert_constraint_body(var_assoc_store(A, B), _, C, [A-D, B-E], F) :-
    get_target_module(G),
    F=(get_attr(D, G, H)->insert_assoc_store(H, E, C);new_assoc_store(H), put_attr(D, G, H), insert_assoc_store(H, E, C)).
insert_constraint_body(global_singleton, A, B, [], C) :-
    global_singleton_store_name(A, D),
    make_update_store_goal(D, B, E),
    C=E.
insert_constraint_body(multi_store(A), B, C, D, E) :-
    maplist(insert_constraint_body1(B, C), A, F, G),
    list2conj(G, E),
    sort_out_used_vars(F, D).
insert_constraint_body1(A, B, C, D, E) :-
    insert_constraint_body(C, A, B, D, E).
insert_constraint_body(identifier_store(A), B, C, D, E) :-
    D=[A-F],
    get_identifier_size(G),
    functor(H, struct, G),
    get_identifier_index(B, A, I),
    arg(I, H, J),
    E=(F=H, setarg(I, F, [C|J])).
insert_constraint_body(type_indexed_identifier_store(A, B), C, D, E, F) :-
    E=[A-G],
    type_indexed_identifier_structure(B, H),
    get_type_indexed_identifier_index(B, C, A, I),
    arg(I, H, J),
    F=(G=H, setarg(I, G, [D|J])).
sort_out_used_vars(A, B) :-
    flatten(A, C),
    sort(C, D),
    sort_out_used_vars1(D, B).
sort_out_used_vars1([], []).
sort_out_used_vars1([A-B], C) :-
    !,
    C=[A-B].
sort_out_used_vars1([A-B, C-D|E], F) :-
    (   A==C
    ->  B=D,
        sort_out_used_vars1([A-B|E], F)
    ;   F=[A-B|G],
        sort_out_used_vars1([C-D|E], G)
    ).
generate_multi_inthash_insert_constraint_bodies([], _, _, true).
generate_multi_inthash_insert_constraint_bodies([A|B], C, D, (E, F)) :-
    multi_hash_store_name(C, A, G),
    multi_hash_key(C, A, D, H, I),
    E=(H, nb_getval(G, J), insert_iht(J, I, D)),
    generate_multi_inthash_insert_constraint_bodies(B,
                                                    C,
                                                    D,
                                                    F).
generate_multi_hash_insert_constraint_bodies([], _, _, true, []).
generate_multi_hash_insert_constraint_bodies([A|B], C, D, (E, F), [G|H]) :-
    multi_hash_store_name(C, A, I),
    multi_hash_key_direct(C, A, D, J, G),
    make_get_store_goal(I, K, L),
    (   chr_pp_flag(ht_removal, on)
    ->  ht_prev_field(A, M),
        set_dynamic_suspension_term_field(M,
                                          C,
                                          N,
                                          O,
                                          P),
        E=(L, insert_ht(K, J, D, O), (O=[_, N|_]->P;true))
    ;   E=(L, insert_ht(K, J, D))
    ),
    generate_multi_hash_insert_constraint_bodies(B,
                                                 C,
                                                 D,
                                                 F,
                                                 H).
delete_constraint_clause(A, B, C) :-
    (   is_used_auxiliary_predicate(delete_from_store, A)
    ->  B=[D|C],
        D=(E:-F),
        delete_constraint_atom(A, G, E),
        A=H/I,
        functor(E, H, I),
        delete_constraint_body(A, E, G, [], F)
    ;   B=C
    ).
delete_constraint_goal(A, B, C, D) :-
    functor(A, E, F),
    G=E/F,
    (   chr_pp_flag(inline_insertremove, off)
    ->  use_auxiliary_predicate(delete_from_store, G),
        delete_constraint_atom(G, B, D)
    ;   delay_phase_end(validate_store_type_assumptions,
                        delete_constraint_body(G,
                                               A,
                                               B,
                                               C,
                                               D))
    ).
delete_constraint_atom(A, B, C) :-
    make_name('$delete_from_store_', A, D),
    C=..[D, B].
delete_constraint_body(A, B, C, D, E) :-
    E=(F, G),
    (   chr_pp_flag(store_counter, on)
    ->  F='$delete_counter_inc'
    ;   F=true
    ),
    get_store_type(A, H),
    delete_constraint_body(H, A, B, C, D, G).
delete_constraint_body(default, A, _, B, _, C) :-
    (   chr_pp_flag(debugable, on)
    ->  global_list_store_name(A, D),
        make_get_store_goal(D, E, F),
        make_update_store_goal(D, G, H),
        C=(F, 'chr sbag_del_element'(E, B, G), H)
    ;   get_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          B,
                                          I,
                                          J),
        global_list_store_name(A, D),
        make_get_store_goal(D, E, F),
        make_update_store_goal(D, K, H),
        set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          L,
                                          _,
                                          M),
        set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          L,
                                          I,
                                          N),
        C=(J, (var(I)->F, E=[_|K], H, (K=[L|_]->M;true);I=[_, _|K], setarg(2, I, K), (K=[L|_]->N;true)))
    ).
delete_constraint_body(multi_inthash(A), B, _, C, _, D) :-
    generate_multi_inthash_delete_constraint_bodies(A,
                                                    B,
                                                    C,
                                                    D).
delete_constraint_body(multi_hash(A), B, C, D, E, F) :-
    generate_multi_hash_delete_constraint_bodies(A,
                                                 B,
                                                 C,
                                                 D,
                                                 E,
                                                 F).
delete_constraint_body(atomic_constants(A, _, _), B, C, D, E, F) :-
    multi_hash_key(B, C, A, D, E, G, H),
    constants_store_index_name(B, A, I),
    J=..[I, H, K],
    F=(G, (J->nb_getval(K, L), 'chr sbag_del_element'(L, D, M), b_setval(K, M);true)).
delete_constraint_body(ground_constants(A, _, _), B, C, D, E, F) :-
    multi_hash_key(B, C, A, D, E, G, H),
    constants_store_index_name(B, A, I),
    J=..[I, H, K],
    F=(G, (J->nb_getval(K, L), 'chr sbag_del_element'(L, D, M), b_setval(K, M);true)).
delete_constraint_body(global_ground, A, _, B, _, C) :-
    (   chr_pp_flag(debugable, on)
    ->  global_ground_store_name(A, D),
        make_get_store_goal(D, E, F),
        make_update_store_goal(D, G, H),
        C=(F, 'chr sbag_del_element'(E, B, G), H)
    ;   get_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          B,
                                          I,
                                          J),
        global_ground_store_name(A, D),
        make_get_store_goal(D, E, F),
        make_update_store_goal(D, K, H),
        set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          L,
                                          _,
                                          M),
        set_dynamic_suspension_term_field(global_list_prev,
                                          A,
                                          L,
                                          I,
                                          N),
        C=(J, (var(I)->F, E=[_|K], H, (K=[L|_]->M;true);I=[_, _|K], setarg(2, I, K), (K=[L|_]->N;true)))
    ).
delete_constraint_body(var_assoc_store(A, B), C, _, D, _, E) :-
    get_target_module(F),
    get_dynamic_suspension_term_field(argument(A),
                                      C,
                                      D,
                                      G,
                                      H),
    get_dynamic_suspension_term_field(argument(B),
                                      C,
                                      D,
                                      I,
                                      J),
    E=(H, get_attr(G, F, K), J, delete_assoc_store(K, I, D)).
delete_constraint_body(global_singleton, A, _, _, _, B) :-
    global_singleton_store_name(A, C),
    make_update_store_goal(C, [], D),
    B=D.
delete_constraint_body(multi_store(A), B, C, D, E, F) :-
    maplist(delete_constraint_body1(B, C, D, E),
            A,
            G),
    list2conj(G, F).
delete_constraint_body1(A, B, C, D, E, F) :-
    delete_constraint_body(E, A, B, C, D, F).
delete_constraint_body(identifier_store(A), B, C, D, E, F) :-
    get_suspension_argument_possibly_in_scope(C,
                                              E,
                                              D,
                                              A,
                                              G,
                                              H),
    get_identifier_size(I),
    functor(J, struct, I),
    get_identifier_index(B, A, K),
    arg(K, J, L),
    F=(H, G=J, 'chr sbag_del_element'(L, D, M), setarg(K, G, M)).
delete_constraint_body(type_indexed_identifier_store(A, B), C, D, E, F, G) :-
    get_suspension_argument_possibly_in_scope(D,
                                              F,
                                              E,
                                              A,
                                              H,
                                              I),
    type_indexed_identifier_structure(B, J),
    get_type_indexed_identifier_index(B, C, A, K),
    arg(K, J, L),
    G=(I, H=J, 'chr sbag_del_element'(L, E, M), setarg(K, H, M)).
generate_multi_inthash_delete_constraint_bodies([], _, _, true).
generate_multi_inthash_delete_constraint_bodies([A|B], C, D, (E, F)) :-
    multi_hash_store_name(C, A, G),
    multi_hash_key(C, A, D, H, I),
    E=(H, nb_getval(G, J), delete_iht(J, I, D)),
    generate_multi_inthash_delete_constraint_bodies(B,
                                                    C,
                                                    D,
                                                    F).
generate_multi_hash_delete_constraint_bodies([], _, _, _, _, true).
generate_multi_hash_delete_constraint_bodies([A|B], C, D, E, F, (G, H)) :-
    multi_hash_store_name(C, A, I),
    multi_hash_key(C, D, A, E, F, J, K),
    make_get_store_goal(I, L, M),
    (   chr_pp_flag(ht_removal, on)
    ->  ht_prev_field(A, N),
        get_dynamic_suspension_term_field(N,
                                          C,
                                          E,
                                          O,
                                          P),
        set_dynamic_suspension_term_field(N,
                                          C,
                                          Q,
                                          _,
                                          R),
        set_dynamic_suspension_term_field(N,
                                          C,
                                          Q,
                                          O,
                                          S),
        G=(P, (var(O)->M, J, delete_first_ht(L, K, T), (T=[Q|_]->R;true);O=[_, _|T], setarg(2, O, T), (T=[Q|_]->S;true)))
    ;   G=(J, M, delete_ht(L, K, E))
    ),
    generate_multi_hash_delete_constraint_bodies(B,
                                                 _,
                                                 D,
                                                 E,
                                                 F,
                                                 H).
generate_attach_code(A, B) :-
    enumerate_stores_code(A, C),
    append(C, D, B),
    generate_attach_code(A, D, E),
    module_initializers(F),
    prolog_global_variables_code(G),
    E=[('$chr_initialization':-F), (:-initialization'$chr_initialization')|G].
generate_attach_code([], A, A).
generate_attach_code([A|B], C, D) :-
    get_store_type(A, E),
    generate_attach_code(E, A, C, F),
    generate_attach_code(B, F, D).
generate_attach_code(default, A, B, C) :-
    global_list_store_initialisation(A, B, C).
generate_attach_code(multi_inthash(A), B, C, D) :-
    multi_inthash_store_initialisations(A, B, C, E),
    multi_inthash_via_lookups(A, B, E, D).
generate_attach_code(multi_hash(A), B, C, D) :-
    multi_hash_store_initialisations(A, B, C, E),
    multi_hash_lookups(A, B, E, D).
generate_attach_code(atomic_constants(A, B, _), C, D, E) :-
    constants_initializers(C, A, B),
    atomic_constants_code(C, A, B, D, E).
generate_attach_code(ground_constants(A, B, _), C, D, E) :-
    constants_initializers(C, A, B),
    ground_constants_code(C, A, B, D, E).
generate_attach_code(global_ground, A, B, C) :-
    global_ground_store_initialisation(A, B, C).
generate_attach_code(var_assoc_store(_, _), _, A, A) :-
    use_auxiliary_module(chr_assoc_store).
generate_attach_code(global_singleton, A, B, C) :-
    global_singleton_store_initialisation(A, B, C).
generate_attach_code(multi_store(A), B, C, D) :-
    multi_store_generate_attach_code(A, B, C, D).
generate_attach_code(identifier_store(A), B, C, D) :-
    get_identifier_index(B, A, E),
    (   E==2
    ->  get_identifier_size(F),
        functor(G, struct, F),
        G=..[_, H|I],
        set_elems(I, []),
        J=new_identifier(H, G),
        functor(K, struct, F),
        arg(1, K, L),
        M=(user:portray(K):-write('<id:'), print(L), write(>)),
        functor(N, struct, F),
        arg(1, N, O),
        P=identifier_label(N, O),
        C=[J, M, P|D]
    ;   C=D
    ).
generate_attach_code(type_indexed_identifier_store(A, B), C, D, E) :-
    get_type_indexed_identifier_index(B, C, A, F),
    (   F==2
    ->  identifier_store_initialization(B, D, G),
        get_type_indexed_identifier_size(B, _),
        type_indexed_identifier_structure(B, H),
        H=..[_, I|J],
        set_elems(J, []),
        type_indexed_identifier_name(B, new_identifier, K),
        L=..[K, I, H],
        M=..[K, N, O],
        type_indexed_identifier_structure(B, P),
        P=..[_, N|Q],
        set_elems(Q, []),
        R=(O=P),
        S=user:goal_expansion(M, R),
        type_indexed_identifier_structure(B, T),
        arg(1, T, U),
        V=(user:portray(T):-write('<id:'), print(U), write(>)),
        type_indexed_identifier_structure(B, W),
        arg(1, W, X),
        type_indexed_identifier_name(B, identifier_label, Y),
        Z=..[Y, W, X],
        A1=..[Y, B1, C1],
        type_indexed_identifier_structure(B, D1),
        arg(1, D1, C1),
        E1=(B1=D1),
        F1=(user:goal_expansion(A1, E1):-writeln(expanding)),
        identifier_store_name(B, G1),
        lookup_identifier_atom(B, H1, I1, J1),
        type_indexed_identifier_name(B, new_identifier, K1),
        L1=..[K1, H1, I1],
        M1=(J1:-nb_getval(G1, N1), (lookup_ht(N1, H1, [I1])->true;L1, insert_ht(N1, H1, I1))),
        lookup_only_identifier_atom(B, O1, P1, Q1),
        R1=(Q1:-nb_getval(G1, S1), lookup_ht(S1, O1, [P1])),
        G=[(:-multifile goal_expansion/2), (:-dynamic goal_expansion/2), L, S, V, Z, F1, M1, R1|E]
    ;   D=E
    ).
constants_initializers(A, B, C) :-
    maplist(constant_initializer(A, B), C).
constant_initializer(A, B, C) :-
    constants_store_name(A, B, C, D),
    prolog_global_variable(D),
    module_initializer(nb_setval(D, [])).
lookup_identifier_atom(A, B, C, D) :-
    atom_concat(lookup_identifier_, A, E),
    D=..[E, B, C].
lookup_only_identifier_atom(A, B, C, D) :-
    atom_concat(lookup_only_identifier_, A, E),
    D=..[E, B, C].
identifier_label_atom(A, B, C, D) :-
    type_indexed_identifier_name(A, identifier_label, E),
    D=..[E, B, C].
multi_store_generate_attach_code([], _, A, A).
multi_store_generate_attach_code([A|B], C, D, E) :-
    generate_attach_code(A, C, D, F),
    multi_store_generate_attach_code(B, C, F, E).
multi_inthash_store_initialisations([], _, A, A).
multi_inthash_store_initialisations([A|B], C, D, E) :-
    use_auxiliary_module(chr_integertable_store),
    multi_hash_store_name(C, A, F),
    module_initializer((new_iht(G), nb_setval(F, G))),
    H=D,
    multi_inthash_store_initialisations(B, C, H, E).
multi_hash_store_initialisations([], _, A, A).
multi_hash_store_initialisations([A|B], C, D, E) :-
    use_auxiliary_module(chr_hashtable_store),
    multi_hash_store_name(C, A, F),
    prolog_global_variable(F),
    make_init_store_goal(F, G, H),
    module_initializer((new_ht(G), H)),
    I=D,
    multi_hash_store_initialisations(B, C, I, E).
global_list_store_initialisation(A, B, C) :-
    (   is_stored(A)
    ->  global_list_store_name(A, D),
        prolog_global_variable(D),
        make_init_store_goal(D, [], E),
        module_initializer(E)
    ;   true
    ),
    B=C.
global_ground_store_initialisation(A, B, C) :-
    global_ground_store_name(A, D),
    prolog_global_variable(D),
    make_init_store_goal(D, [], E),
    module_initializer(E),
    B=C.
global_singleton_store_initialisation(A, B, C) :-
    global_singleton_store_name(A, D),
    prolog_global_variable(D),
    make_init_store_goal(D, [], E),
    module_initializer(E),
    B=C.
identifier_store_initialization(A, B, C) :-
    use_auxiliary_module(chr_hashtable_store),
    identifier_store_name(A, D),
    prolog_global_variable(D),
    make_init_store_goal(D, E, F),
    module_initializer((new_ht(E), F)),
    B=C.
multi_inthash_via_lookups([], _, A, A).
multi_inthash_via_lookups([A|B], C, D, E) :-
    multi_hash_lookup_head(C, A, F, G, H),
    multi_hash_lookup_body(C, inthash, A, F, G, I),
    D=[(H:-I)|J],
    multi_inthash_via_lookups(B, C, J, E).
multi_hash_lookups([], _, A, A).
multi_hash_lookups([A|B], C, D, E) :-
    multi_hash_lookup_head(C, A, F, G, H),
    multi_hash_lookup_body(C, hash, A, F, G, I),
    D=[(H:-I)|J],
    multi_hash_lookups(B, C, J, E).
multi_hash_lookup_head(A, B, C, D, E) :-
    multi_hash_lookup_name(A, B, F),
    E=..[F, C, D].
multi_hash_lookup_body(A, B, C, D, E, F) :-
    get_store_type(A, multi_store(G)),
    (   memberchk(atomic_constants(C, H, _), G)
    ->  (   ground(D)
        ->  constants_store_name(A, C, D, I),
            F=nb_getval(I, E)
        ;   constants_store_index_name(A, C, J),
            K=..[J, D, I],
            F=(K, nb_getval(I, E))
        )
    ;   memberchk(ground_constants(C, H, _), G)
    ->  (   ground(D)
        ->  constants_store_name(A, C, D, I),
            F=nb_getval(I, E)
        ;   constants_store_index_name(A, C, J),
            K=..[J, D, I],
            F=(K, nb_getval(I, E))
        )
    ;   memberchk(multi_hash([C]), G)
    ->  multi_hash_store_name(A, C, I),
        make_get_store_goal(I, L, M),
        (   B==hash,
            specialized_hash_term_call(A,
                                       C,
                                       D,
                                       N,
                                       O)
        ->  F=(M, O, lookup_ht1(L, N, D, E))
        ;   lookup_hash_call(B, L, D, E, K),
            F=(M, K)
        )
    ;   B==inthash
    ->  multi_hash_store_name(A, C, I),
        make_get_store_goal(I, L, M),
        lookup_hash_call(B, L, D, E, K),
        F=(M, K)
    ).
lookup_hash_call(hash, A, B, C, lookup_ht(A, B, C)).
lookup_hash_call(inthash, A, B, C, lookup_iht(A, B, C)).
specialized_hash_term_call(_, _, A, B, C) :-
    (   ground(A)
    ->  term_hash(A, B),
        C=true
    ).
multi_hash_lookup_goal(A, B, C, D, E, F) :-
    (   atomic(D)
    ->  actual_atomic_multi_hash_keys(A, C, [D])
    ;   ground(D)
    ->  actual_ground_multi_hash_keys(A, C, [D])
    ;   C=[G],
        get_constraint_arg_type(A, G, H),
        is_chr_constants_type(H, _, _)
    ->  true
    ;   actual_non_ground_multi_hash_key(A, C)
    ),
    delay_phase_end(validate_store_type_assumptions,
                    multi_hash_lookup_body(A,
                                           B,
                                           C,
                                           D,
                                           E,
                                           F)).
multi_hash_lookup_name(A/B, C, D) :-
    atom_concat_list(C, E),
    atom_concat_list(['$via1_multi_hash_', A, '___', B, -, E],
                     D).
multi_hash_store_name(A/B, C, D) :-
    get_target_module(E),
    atom_concat_list(C, F),
    atom_concat_list([ '$chr_store_multi_hash_',
                       E,
                       '____',
                       A,
                       '___',
                       B,
                       -,
                       F
                     ],
                     D).
multi_hash_key(A, B, C, D, E) :-
    (   B=[F]
    ->  get_dynamic_suspension_term_field(argument(F),
                                          A,
                                          C,
                                          E,
                                          D)
    ;   maplist(get_dynamic_suspension_term_field1(A, C),
                B,
                G,
                H),
        E=..[k|G],
        list2conj(H, D)
    ).
get_dynamic_suspension_term_field1(A, B, C, D, E) :-
    get_dynamic_suspension_term_field(argument(C),
                                      A,
                                      B,
                                      D,
                                      E).
multi_hash_key(_, A, B, C, D, E, F) :-
    (   B=[G]
    ->  get_suspension_argument_possibly_in_scope(A,
                                                  D,
                                                  C,
                                                  G,
                                                  F,
                                                  E)
    ;   maplist(get_suspension_argument_possibly_in_scope(A,
                                                          D,
                                                          C),
                B,
                H,
                I),
        F=..[k|H],
        list2conj(I, E)
    ).
get_suspension_argument_possibly_in_scope(A, B, C, D, E, F) :-
    arg(D, A, G),
    (   ground(G),
        G='$chr_identifier_match'(H, I)
    ->  functor(A, J, K),
        lookup_identifier_atom(I, H, E, F)
    ;   term_variables(G, L),
        copy_term_nat(G-L, E-M),
        translate(L, B, M)
    ->  F=true
    ;   functor(A, J, K),
        N=J/K,
        get_dynamic_suspension_term_field(argument(D),
                                          N,
                                          C,
                                          E,
                                          F)
    ).
multi_hash_key_direct(_, A, _, B, C) :-
    (   A=[D]
    ->  C=[D-B]
    ;   pairup(A, E, C),
        B=..[k|E]
    ).
args(A, B, C) :-
    maplist(arg1(B), A, C).
split_args(A, B, C, D) :-
    split_args(A, B, 1, C, D).
split_args([], A, _, [], A).
split_args([A|B], [C|D], E, F, G) :-
    H is E+1,
    (   A==E
    ->  F=[C|I],
        split_args(B, D, H, I, G)
    ;   G=[C|I],
        split_args([A|B], D, H, F, I)
    ).
atomic_constants_code(A, B, C, D, E) :-
    constants_store_index_name(A, B, F),
    maplist(atomic_constant_code(A, B, F), C, G),
    append(G, E, D).
atomic_constant_code(A, B, C, D, E) :-
    constants_store_name(A, B, D, F),
    E=..[C, D, F].
ground_constants_code(A, B, C, D, E) :-
    constants_store_index_name(A, B, F),
    maplist(constants_store_name(A, B), C, G),
    length(C, H),
    replicate(H, [], I),
    trie_index([C|I], G, F, D, E).
constants_store_name(A/B, C, D, E) :-
    get_target_module(F),
    term_to_atom(D, G),
    term_to_atom(C, H),
    atom_concat_list([ '$chr_store_constants_',
                       F,
                       '____',
                       A,
                       '___',
                       B,
                       '___',
                       H,
                       '___',
                       G
                     ],
                     E).
constants_store_index_name(A/B, C, D) :-
    get_target_module(E),
    term_to_atom(C, F),
    atom_concat_list([ '$chr_store_constants_',
                       E,
                       '____',
                       A,
                       '___',
                       B,
                       '___',
                       F
                     ],
                     D).
trie_index([A|B], C, D, E, F) :-
    trie_step(A, D, D, B, C, E, F).
trie_step([], _, _, [], [], A, A) :-
    !.
trie_step(A, B, C, D, E, F, G) :-
    D=[H|_],
    length(H, I),
    aggregate_all(set(J/K),
                  ( member(L, A),
                    functor(L, J, K)
                  ),
                  M),
    N is I+1,
    trie_step_cases(M,
                    N,
                    A,
                    D,
                    E,
                    B,
                    C,
                    F,
                    G).
trie_step_cases([], _, _, _, _, _, _, A, A).
trie_step_cases([A|B], C, D, E, F, G, H, I, J) :-
    trie_step_case(A,
                   C,
                   D,
                   E,
                   F,
                   G,
                   H,
                   I,
                   K),
    trie_step_cases(B,
                    C,
                    D,
                    E,
                    F,
                    G,
                    H,
                    K,
                    J).
trie_step_case(A/B, C, D, E, F, G, H, [I|J], K) :-
    I=(L:-M),
    N is C+1,
    functor(L, G, N),
    arg(1, L, O),
    L=..[_, _|P],
    once(append(Q, [R], P)),
    functor(O, A, B),
    O=..[_|S],
    append(S, P, T),
    (   T==[R]
    ->  J=K,
        M=true,
        rec_cases(D,
                  _,
                  F,
                  A/B,
                  _,
                  _,
                  U),
        U=[R]
    ;   rec_cases(D,
                  E,
                  F,
                  A/B,
                  V,
                  W,
                  U),
        (   W=[X]
        ->  J=K,
            M=true,
            append([V, X, U], T)
        ;   pairup(V, W, Y),
            common_pattern(Y, Z, A1, B1),
            append(S, Q, [C1|D1]),
            C1-D1=Z,
            fresh_symbol(H, E1),
            append(A1, [R], F1),
            M=..[E1|F1],
            maplist(head_tail, B1, G1, H1),
            trie_step(G1,
                      E1,
                      H,
                      H1,
                      U,
                      J,
                      K)
        )
    ).
head_tail([A|B], A, B).
rec_cases([], [], [], _, [], [], []).
rec_cases([A|B], [C|D], [E|F], G/H, I, J, K) :-
    (   functor(A, G, H),
        A=..[_|L],
        append(L, C, [M|N])
    ->  I=[M|O],
        J=[N|P],
        K=[E|Q],
        rec_cases(B,
                  D,
                  F,
                  G/H,
                  O,
                  P,
                  Q)
    ;   rec_cases(B,
                  D,
                  F,
                  G/H,
                  I,
                  J,
                  K)
    ).
common_pattern(A, B, C, D) :-
    fold1(chr_translate:gct, A, B),
    term_variables(B, C),
    findall(C, member(B, A), D).
gct(A, B, C) :-
    gct_(A, B, C, [], _).
gct_(A, B, C, D, E) :-
    (   nonvar(A),
        nonvar(B),
        functor(A, F, G),
        functor(B, H, I),
        F==H,
        G==I
    ->  functor(C, F, G),
        A=..[_|J],
        B=..[_|K],
        C=..[_|L],
        maplist_dcg(chr_translate:gct_,
                    J,
                    K,
                    L,
                    D,
                    E)
    ;   lookup_eq(D, A+B, C)
    ->  E=D
    ;   E=[A+B-C|D]
    ).
global_list_store_name(A/B, C) :-
    get_target_module(D),
    atom_concat_list([ '$chr_store_global_list_',
                       D,
                       '____',
                       A,
                       '___',
                       B
                     ],
                     C).
global_ground_store_name(A/B, C) :-
    get_target_module(D),
    atom_concat_list([ '$chr_store_global_ground_',
                       D,
                       '____',
                       A,
                       '___',
                       B
                     ],
                     C).
global_singleton_store_name(A/B, C) :-
    get_target_module(D),
    atom_concat_list([ '$chr_store_global_singleton_',
                       D,
                       '____',
                       A,
                       '___',
                       B
                     ],
                     C).
identifier_store_name(A, B) :-
    get_target_module(C),
    atom_concat_list(['$chr_identifier_lookup_', C, '____', A], B).
:- if(current_predicate(user:exception/3)).
prolog_global_variables_code([]) :-
    tmp_module,
    !.
prolog_global_variables_code(A) :-
    prolog_global_variables(B),
    (   B==[]
    ->  A=[]
    ;   maplist(wrap_in_functor('$chr_prolog_global_variable'), B, C),
        A=[(:-dynamic user:exception/3), (:-multifile user:exception/3), (user:exception(undefined_global_variable, D, retry):-'$chr_prolog_global_variable'(D), '$chr_initialization')|C]
    ).
tmp_module :-
    prolog_load_context(module, A),
    module_property(A, class(temporary)).
:- else.
prolog_global_variables_code([]).
:- endif.
sbag_member_call(A, B, member(A, B)).
update_mutable_call(A, B, 'chr update_mutable'(A, B)).
create_mutable_call(A, B, true) :-
    B=mutable(A).
get_suspension_field(A, B, C, D, E) :-
    get_dynamic_suspension_term_field(C, A, B, D, E).
update_suspension_field(A, B, C, D, E) :-
    set_dynamic_suspension_term_field(C, A, B, D, E).
get_update_suspension_field(A, B, C, D, E, true, F, G) :-
    get_dynamic_suspension_term_field(C, A, B, D, F),
    set_dynamic_suspension_term_field(C, A, B, E, G).
create_static_suspension_field(A, B, C, D, true) :-
    get_static_suspension_term_field(C, A, B, D).
get_static_suspension_field(A, B, C, D, true) :-
    get_static_suspension_term_field(C, A, B, D).
get_update_static_suspension_field(A, B, C, D, E, F, true, G) :-
    get_static_suspension_term_field(D, A, C, E),
    set_dynamic_suspension_term_field(D, A, B, F, G).
enumerate_stores_code(A, [B|C]) :-
    D='$enumerate_constraints'(E),
    B=(D:-F),
    enumerate_store_bodies(A, E, C),
    (   C=[]
    ->  F=fail
    ;   F=(nonvar(E)->functor(E, G, _), '$enumerate_constraints'(G, E);'$enumerate_constraints'(_, E))
    ).
enumerate_store_bodies([], _, []).
enumerate_store_bodies([A|B], C, D) :-
    (   is_stored(A)
    ->  get_store_type(A, E),
        (   enumerate_store_body(E, A, F, G)
        ->  true
        ;   chr_error(internal,
                      'Could not generate enumeration code for constraint ~w.\n',
                      [A])
        ),
        get_dynamic_suspension_term_field(arguments,
                                          A,
                                          F,
                                          H,
                                          I),
        A=J/_,
        K=..[J|H],
        L='$enumerate_constraints'(J, C),
        M=(G, I, C=K),
        D=[(L:-M)|N]
    ;   D=N
    ),
    enumerate_store_bodies(B, C, N).
enumerate_store_body(default, A, B, C) :-
    global_list_store_name(A, D),
    sbag_member_call(B, E, F),
    make_get_store_goal_no_error(D, E, G),
    C=(G, F).
enumerate_store_body(multi_inthash([A|_]), B, C, D) :-
    multi_inthash_enumerate_store_body(A, B, C, D).
enumerate_store_body(multi_hash([A|_]), B, C, D) :-
    multi_hash_enumerate_store_body(A, B, C, D).
enumerate_store_body(atomic_constants(A, B, C), D, E, F) :-
    C==complete,
    maplist(enumerate_constant_store_body(D, A, G),
            B,
            H),
    list2disj(H, I),
    F=(I, member(E, G)).
enumerate_constant_store_body(A, B, C, D, nb_getval(E, C)) :-
    constants_store_name(A, B, D, E).
enumerate_store_body(ground_constants(A, B, C), D, E, F) :-
    enumerate_store_body(atomic_constants(A, B, C),
                         D,
                         E,
                         F).
enumerate_store_body(global_ground, A, B, C) :-
    global_ground_store_name(A, D),
    sbag_member_call(B, E, F),
    make_get_store_goal(D, E, G),
    C=(G, F).
enumerate_store_body(var_assoc_store(_, _), _, _, A) :-
    A=fail.
enumerate_store_body(global_singleton, A, B, C) :-
    global_singleton_store_name(A, D),
    make_get_store_goal(D, B, E),
    C=(E, B\==[]).
enumerate_store_body(multi_store(A), B, C, D) :-
    (   memberchk(global_ground, A)
    ->  enumerate_store_body(global_ground, B, C, D)
    ;   once(( member(E, A),
               enumerate_store_body(E, B, C, D)
             ))
    ).
enumerate_store_body(identifier_store(_), _, _, A) :-
    A=fail.
enumerate_store_body(type_indexed_identifier_store(_, _), _, _, A) :-
    A=fail.
multi_inthash_enumerate_store_body(A, B, C, D) :-
    multi_hash_store_name(B, A, E),
    D=(nb_getval(E, F), value_iht(F, C)).
multi_hash_enumerate_store_body(A, B, C, D) :-
    multi_hash_store_name(B, A, E),
    make_get_store_goal(E, F, G),
    D=(G, value_ht(F, C)).
guard_simplification :-
    (   chr_pp_flag(guard_simplification, on)
    ->  precompute_head_matchings,
        simplify_guards(1)
    ;   true
    ).
next_prev_rule(A, B, C) :-
    (   find_min_q(A, _-D)
    ->  D=(-B),
        normalize_heap(A, D, C)
    ;   B=0,
        C=A
    ).
normalize_heap(A, B, C) :-
    (   find_min_q(A, _-B)
    ->  delete_min_q(A, D, tuple(E, F, _)-_),
        (   F>1
        ->  G is F-1,
            get_occurrence(E, G, H, _),
            insert_q(D,
                     tuple(E, G, H)-(-H),
                     I)
        ;   I=D
        ),
        normalize_heap(I, B, C)
    ;   C=A
    ).
head_types_modes_condition([], _, true).
head_types_modes_condition([A|B], C, (D, E)) :-
    types_modes_condition(C, A, D),
    head_types_modes_condition(B, C, E).
add_background_info(A, B) :-
    get_bg_info(C),
    add_background_info2(A, D),
    append(C, D, B).
add_background_info2(A, []) :-
    var(A),
    !.
add_background_info2([], []) :-
    !.
add_background_info2([A|B], C) :-
    !,
    add_background_info2(A, D),
    add_background_info2(B, E),
    append(D, E, C).
add_background_info2(A, B) :-
    (   functor(A, _, C),
        C>0
    ->  A=..[_|D],
        add_background_info2(D, E)
    ;   E=[]
    ),
    get_bg_info(A, F),
    append(F, E, B).
normalize_conj_list(A, B) :-
    list2conj(A, C),
    conj2list(C, B).
compute_derived_info([], _, _, _, _, _, _, _, [], []).
compute_derived_info([A|B], C, D, E, F, G, H, I, [J|K], [L|M]) :-
    copy_term(C-F, N),
    variable_replacement(C-F, N, O),
    append(A, O, P),
    list2conj(C, Q),
    negate_b(Q, R),
    make_head_matchings_explicit_not_negated2(E, D, S),
    list2conj(S, T),
    term_variables(P, U),
    term_variables(C-F-S, V),
    new_vars(V, U, W),
    append(P, W, X),
    (   F==true
    ->  Y=R
    ;   negate_b(F, Z),
        Y=(R;T, Z)
    ),
    copy_with_variable_replacement(Y, J, X),
    copy_with_variable_replacement(F, A1, X),
    copy_with_variable_replacement(C, B1, X),
    list2conj(B1, C1),
    apply_guard_wrt_term(H, A1, D1),
    apply_guard_wrt_term(D1, C1, L),
    compute_derived_info(B,
                         C,
                         D,
                         E,
                         F,
                         G,
                         H,
                         I,
                         K,
                         M).
simplify_guard(A, B, C, D, E) :-
    conj2list(A, F),
    guard_entailment:simplify_guards(C, B, F, G, E),
    list2conj(G, D).
new_vars([], _, []).
new_vars([A|B], C, D) :-
    (   memberchk_eq(A, C)
    ->  new_vars(B, C, D)
    ;   D=[A-E, E-A|F],
        new_vars(B, C, F)
    ).
head_subset(A, B, C) :-
    head_subset(A, B, C, [], _).
head_subset([], A, B, B, A).
head_subset([A|B], C, D, E, F) :-
    head_member(C, A, G, E, H),
    head_subset(B, H, D, G, F).
head_member([A|B], C, D, E, F) :-
    (   variable_replacement(C, A, E, D),
        F=B
    ;   F=[A|G],
        head_member(B, C, D, E, G)
    ).
make_head_matchings_explicit(A, B, C) :-
    make_head_matchings_explicit_memo_lookup(A, D, E),
    copy_term_nat(D-E, B-C).
make_head_matchings_explicit_(A, B, C) :-
    extract_arguments(A, D),
    make_matchings_explicit(D, E, [], [], _, C),
    substitute_arguments(A, E, B).
make_head_matchings_explicit_not_negated(A, B, C) :-
    extract_arguments(A, D),
    make_matchings_explicit_not_negated(D, E, C),
    substitute_arguments(A, E, B).
make_head_matchings_explicit_not_negated2(A, B, C) :-
    extract_arguments(A, D),
    extract_arguments(B, E),
    make_matchings_explicit_not_negated(D, E, C).
extract_arguments([], []).
extract_arguments([A|B], C) :-
    A=..[_|D],
    append(D, E, C),
    extract_arguments(B, E).
substitute_arguments([], [], []).
substitute_arguments([A|B], C, [D|E]) :-
    functor(A, F, G),
    split_at(G, C, H, I),
    D=..[F|H],
    substitute_arguments(B, I, E).
make_matchings_explicit([], [], _, A, A, []).
make_matchings_explicit([A|B], [C|D], E, F, G, H) :-
    (   var(A)
    ->  (   memberchk_eq(A, E)
        ->  list2disj(F, I),
            H=[(I;C==A)|J],
            K=E
        ;   H=J,
            C=A,
            K=[A|E]
        ),
        L=F
    ;   functor(A, M, N),
        A=..[M|O],
        make_matchings_explicit(O,
                                P,
                                E,
                                F,
                                Q,
                                R),
        S=..[M|P],
        (   R==[]
        ->  H=[functor(C, M, N)|J]
        ;   list2conj(R, T),
            list2disj(F, I),
            U=(C\=S;I;T),
            H=[functor(C, M, N), U|J]
        ),
        L=[C\=S|Q],
        term_variables(B, V),
        append(V, E, K)
    ),
    make_matchings_explicit(B, D, K, L, G, J).
make_matchings_explicit_not_negated([], [], []).
make_matchings_explicit_not_negated([A|B], [C|D], E) :-
    E=[C=A|F],
    make_matchings_explicit_not_negated(B, D, F).
apply_guard_wrt_term([], _, []).
apply_guard_wrt_term([A|B], C, [D|E]) :-
    (   var(A)
    ->  apply_guard_wrt_variable(C, A, D)
    ;   A=..[F|G],
        apply_guard_wrt_term(G, C, H),
        D=..[F|H]
    ),
    apply_guard_wrt_term(B, C, E).
apply_guard_wrt_variable((A, B), C, D) :-
    !,
    apply_guard_wrt_variable(A, C, E),
    apply_guard_wrt_variable(B, E, D).
apply_guard_wrt_variable(A, B, C) :-
    (   A=(D=E),
        B==D
    ->  C=E
    ;   A=functor(F, G, H),
        B==F,
        ground(G),
        ground(H)
    ->  functor(C, G, H)
    ;   C=B
    ).
replace_some_heads(A, B, _, [], A, B, _, _, []) :-
    !.
replace_some_heads([], [A|B], [C|D], [E|F], [], [G|H], I, J, K) :-
    !,
    (   C==E
    ->  G=E,
        replace_some_heads([],
                           B,
                           D,
                           F,
                           [],
                           H,
                           I,
                           J,
                           K)
    ;   E=functor(L, M, N),
        C==L
    ->  length(O, N),
        (   var(A)
        ->  P=[],
            G=..[M|O]
        ;   A=..[M|Q],
            use_same_args(Q, O, R, I, J, P),
            G=..[M|R]
        ),
        replace_some_heads([],
                           B,
                           D,
                           F,
                           [],
                           H,
                           I,
                           J,
                           S),
        append(P, S, K)
    ;   G=A,
        replace_some_heads([],
                           B,
                           D,
                           [E|F],
                           [],
                           H,
                           I,
                           J,
                           K)
    ).
replace_some_heads([A|B], C, [D|E], [F|G], [H|I], J, K, L, M) :-
    !,
    (   D==F
    ->  H=F,
        replace_some_heads(B,
                           C,
                           E,
                           G,
                           I,
                           J,
                           K,
                           L,
                           M)
    ;   F=functor(N, O, P),
        D==N
    ->  length(Q, P),
        (   var(A)
        ->  R=[],
            H=..[O|Q]
        ;   A=..[O|S],
            use_same_args(S,
                          Q,
                          T,
                          K,
                          L,
                          R),
            H=..[O|T]
        ),
        replace_some_heads(B,
                           C,
                           E,
                           G,
                           I,
                           J,
                           K,
                           L,
                           U),
        append(R, U, M)
    ;   H=A,
        replace_some_heads(B,
                           C,
                           E,
                           [F|G],
                           I,
                           J,
                           K,
                           L,
                           M)
    ).
use_same_args([], [], [], _, _, []).
use_same_args([A|B], [_|C], [D|E], F, G, H) :-
    var(A),
    !,
    D=A,
    use_same_args(B, C, E, F, G, H).
use_same_args([A|B], [C|D], [E|F], G, H, I) :-
    nonvar(A),
    !,
    (   common_variables(A, H)
    ->  I=[C=A|J]
    ;   I=J
    ),
    E=C,
    use_same_args(B, D, F, G, H, J).
simplify_heads([], _, _, _, [], []).
simplify_heads([A|B], C, D, E, F, G) :-
    A=(H=I),
    (   (   nonvar(I)
        ;   common_variables(I, B-C)
        ),
        guard_entailment:entails_guard(C, H=I)
    ->  (   common_variables(I, D-B-C)
        ->  G=J,
            F=K
        ;   (   common_variables(I, E)
            ->  G=[H=I|J]
            ;   G=J
            ),
            F=[H|K]
        )
    ;   nonvar(I),
        functor(I, L, M),
        guard_entailment:entails_guard([functor(H, L, M)|C], H=I)
    ->  G=J,
        (   common_variables(I, D-B-C)
        ->  F=K
        ;   F=[functor(H, L, M)|K]
        )
    ;   F=K,
        G=J
    ),
    simplify_heads(B, [A|C], D, E, K, J).
common_variables(A, B) :-
    term_variables(A, C),
    term_variables(B, D),
    intersect_eq(C, D, E),
    E\==[].
add_failing_occ(A, B, C, D, E, F, _, G, H) :-
    A=pragma(rule(_, _, I, _), ids(J, K), _, _, _),
    append(K, J, L),
    missing_partner_cond(B,
                         C,
                         L,
                         D,
                         M,
                         N,
                         G),
    copy_term((N, B, C), (O, P, Q)),
    variable_replacement((N, B, C),
                         (O, P, Q),
                         H),
    copy_with_variable_replacement(I, R, H),
    extract_explicit_matchings(R, S),
    negate_b(S, T),
    copy_with_variable_replacement(M, U, H),
    (   subsumes_term(F, O),
        F=O
    ->  E=[(T;U)]
    ;   E=[chr_pp_void_info]
    ).
missing_partner_cond([], [], [], _, fail, _, _).
missing_partner_cond([A|B], [_|C], [D|E], D, F, A, G) :-
    !,
    missing_partner_cond(B,
                         C,
                         E,
                         D,
                         F,
                         A,
                         G).
missing_partner_cond([A|B], [_|C], [_|D], E, F, G, H/I) :-
    F=(chr_pp_not_in_store(A);J),
    missing_partner_cond(B,
                         C,
                         D,
                         E,
                         J,
                         G,
                         H/I).
extract_explicit_matchings((A, B), C) :-
    !,
    (   extract_explicit_matchings(A)
    ->  extract_explicit_matchings(B, C)
    ;   C=(A, D),
        extract_explicit_matchings(B, D)
    ).
extract_explicit_matchings(A, B) :-
    !,
    (   extract_explicit_matchings(A)
    ->  B=true
    ;   B=A
    ).
extract_explicit_matchings(A=B) :-
    var(A),
    var(B),
    !,
    A=B.
extract_explicit_matchings(A==B) :-
    var(A),
    var(B),
    !,
    A=B.
assert_constraint_type(A, B) :-
    (   ground(B)
    ->  constraint_type(A, B)
    ;   chr_error(type_error,
                  'Declared argument types "~w" for constraint "~w" must be ground!\n',
                  [B, A])
    ).
get_type_definition_det(A, B) :-
    (   get_type_definition(A, B)
    ->  true
    ;   chr_error(type, 'Could not find type definition for type `~w\'.\n', [A])
    ).
get_constraint_type_det(A, B) :-
    (   get_constraint_type(A, B)
    ->  true
    ;   A=_/C,
        replicate(C, any, B)
    ).
modes_condition([], [], true).
modes_condition([A|B], [C|D], E) :-
    (   A==(+)
    ->  E=(ground(C), F)
    ;   A==(-)
    ->  E=(var(C), F)
    ;   E=F
    ),
    modes_condition(B, D, F).
types_condition([], [], [], [], true).
types_condition([A|B], [C|D], [E|F], [G|H], (I, J)) :-
    (   G==(-)
    ->  K=[true]
    ;   get_type_definition_det(A, L),
        type_condition(L, C, E, G, M),
        (   G==(+)
        ->  K=M
        ;   K=[\+ground(C)|M]
        )
    ),
    list2disj(K, I),
    types_condition(B, D, F, H, J).
type_condition([], _, _, _, []).
type_condition([A|B], C, D, E, [F|G]) :-
    (   var(A)
    ->  chr_error(type, 'Unexpected variable type in type definition!\n', [])
    ;   atomic_builtin_type(A, C, F)
    ->  true
    ;   compound_builtin_type(A, C, F, _)
    ->  true
    ;   type_def_case_condition(A, C, D, E, F)
    ),
    type_condition(B, C, D, E, G).
ast_atomic_builtin_type(A, B, C) :-
    ast_term_to_term(B, D),
    atomic_builtin_type(A, D, C).
ast_compound_builtin_type(A, B, C) :-
    ast_term_to_term(B, D),
    compound_builtin_type(A, D, _, C).
atomic_builtin_type(any, _, true).
atomic_builtin_type(dense_int, A, (integer(A), A>=0)).
atomic_builtin_type(int, A, integer(A)).
atomic_builtin_type(number, A, number(A)).
atomic_builtin_type(float, A, float(A)).
atomic_builtin_type(natural, A, (integer(A), A>=0)).
atomic_builtin_type(chr_identifier, _, true).
compound_builtin_type(chr_constants(_), _, true, true).
compound_builtin_type(chr_constants(_, _), _, true, true).
compound_builtin_type(chr_identifier(_), _, true, true).
compound_builtin_type(chr_enum(A), B, (ground(B), memberchk(B, A)), once((member(C, A), unifiable(B, C, _)))).
compound_builtin_type(chr_enum(_, _), _, true, true).
is_chr_constants_type(chr_constants(A), A, no).
is_chr_constants_type(chr_constants(A, B), A, yes(B)).
is_chr_enum_type(chr_enum(A), A, no).
is_chr_enum_type(chr_enum(A, B), A, yes(B)).
type_def_case_condition(A, B, C, D, E) :-
    (   nonvar(A)
    ->  functor(A, F, G),
        (   G==0
        ->  E=(B=A)
        ;   var(C)
        ->  E=functor(B, F, G)
        ;   functor(C, F, G)
        ->  E=(functor(B, F, G), B=H, I),
            A=..[_|J],
            C=..[_|K],
            functor(H, F, G),
            H=..[_|L],
            replicate(G, D, M),
            types_condition(J, L, K, M, I)
        ;   E=functor(B, F, G)
        )
    ;   chr_error(internal, 'Illegal type definition (must be nonvar).\n', [])
    ).
static_type_check_rule(A, B) :-
    B=ast_rule(C, _, _, D, _),
    (   catch(( ast_static_type_check_head(C),
                ast_static_type_check_body(D)
              ),
              type_error(E),
              (   E=invalid_functor(F, G, H)
              ->  chr_error(type_error,
                            'Invalid functor in ~@ of ~@:\n\t\tfound `~w\',\n\t\texpected type `~w\'!\n',
                            [ chr_translate:format_src(F),
                              format_rule(A),
                              G,
                              H
                            ])
              ;   E=type_clash(I, J, K, L, M)
              ->  chr_error(type_error,
                            'Type clash for variable ~w in ~@:\n\t\texpected type ~w in ~@\n\t\texpected type ~w in ~@\n',
                            [ I,
                              format_rule(A),
                              L,
                              chr_translate:format_src(J),
                              M,
                              chr_translate:format_src(K)
                            ])
              )),
        fail
    ;   true
    ).
ast_static_type_check_head(simplification(A)) :-
    maplist(ast_static_type_check_head_constraint, A).
ast_static_type_check_head(propagation(A)) :-
    maplist(ast_static_type_check_head_constraint, A).
ast_static_type_check_head(simpagation(A, B)) :-
    maplist(ast_static_type_check_head_constraint, A),
    maplist(ast_static_type_check_head_constraint, B).
ast_static_type_check_head_constraint(A) :-
    A=chr_constraint(B, C, _),
    get_constraint_type_det(B, D),
    maplist(ast_static_type_check_term(head(_)), C, D).
ast_static_type_check_body([]).
ast_static_type_check_body([A|B]) :-
    ast_symbol(A, C),
    get_constraint_type_det(C, D),
    ast_args(A, E),
    maplist(ast_static_type_check_term(body(A)), E, D),
    ast_static_type_check_body(B).
format_src(head(A)) :-
    format('head ~w', [A]).
format_src(body(A)) :-
    format('body goal ~w', [A]).
generate_dynamic_type_check_clauses(A) :-
    (   chr_pp_flag(debugable, on)
    ->  dynamic_type_check,
        get_dynamic_type_check_clauses(B),
        append(B,
               [ ('$dynamic_type_check'(C, D):-throw(error(type_error(C, D), context(_, 'CHR Runtime Type Error'))))
               ],
               A)
    ;   A=[]
    ).
dynamic_type_check_clause(A, B, C) :-
    copy_term(A-B, D-E),
    functor(E, F, G),
    functor(H, F, G),
    E=..[_|I],
    H=..[_|J],
    maplist(dynamic_type_check_call, I, J, K),
    list2conj(K, L),
    C=('$dynamic_type_check'(D, H):-L).
dynamic_type_check_alias_clause(A, B, C) :-
    C=('$dynamic_type_check'(A, D):-'$dynamic_type_check'(B, D)).
dynamic_type_check_call(A, B, C) :-
    (   A==any
    ->  C=true
    ;   C=when(nonvar(B), once('$dynamic_type_check'(A, B)))
    ).
atomic_types_suspended_constraint(A) :-
    A=_/B,
    get_constraint_type(A, C),
    get_constraint_mode(A, D),
    numlist(1, B, E),
    maplist(atomic_types_suspended_constraint(A),
            C,
            D,
            E).
atomic_types_suspended_constraint(A, B, C, D) :-
    (   is_indexed_argument(A, D)
    ->  (   C==(?)
        ->  atomic_type(B)
        ;   true
        )
    ;   true
    ).
storage_analysis(A) :-
    (   chr_pp_flag(storage_analysis, on)
    ->  check_constraint_storages(A)
    ;   true
    ).
check_constraint_storages(A) :-
    maplist(check_constraint_storage, A).
check_constraint_storage(A) :-
    get_max_occurrence(A, B),
    check_occurrences_storage(A, 1, B).
check_occurrences_storage(A, B, C) :-
    (   B>C
    ->  stored_completing(A, 1, 0)
    ;   check_occurrence_storage(A, B),
        D is B+1,
        check_occurrences_storage(A, D, C)
    ).
check_occurrence_storage(A, B) :-
    get_occurrence(A, B, C, D, E),
    (   is_passive(C, D)
    ->  stored(A, B, maybe)
    ;   get_rule(C, F),
        F=pragma(rule(G, H, I, J), ids(K, L), _, _, _),
        (   E==simplification,
            select2(D, M, K, G, _, _)
        ->  check_storage_head1(M, B, G, H, I)
        ;   E==propagation,
            select2(D, N, L, H, _, _)
        ->  check_storage_head2(N, B, G, J)
        )
    ).
check_storage_head1(A, B, C, D, E) :-
    functor(A, F, G),
    H=F/G,
    (   C==[A],
        D==[],
        guard_entailment:entails_guard([chr_pp_headvariables(A)], E),
        A=..[_|I],
        no_matching(I, [])
    ->  stored(H, B, no)
    ;   stored(H, B, maybe)
    ).
no_matching([], _).
no_matching([A|B], C) :-
    var(A),
    \+ memberchk_eq(A, C),
    no_matching(B, [A|C]).
check_storage_head2(A, B, C, D) :-
    functor(A, E, F),
    G=E/F,
    (   C\==[],
        D==true
    ->  stored(G, B, maybe)
    ;   stored(G, B, yes)
    ).
constraints_code(A, B) :-
    (   chr_pp_flag(reduced_indexing, on),
        forall(C,
               A,
               chr_translate:only_ground_indexed_arguments(C))
    ->  none_suspended_on_variables
    ;   true
    ),
    constraints_code1(A, B, []).
constraint_prelude(A/B, C) :-
    vars_susp(B, D, E, F),
    G=..[A|D],
    make_suspension_continuation_goal(A/B, F, H),
    build_head(A, B, [0], F, I),
    (   chr_pp_flag(debugable, on)
    ->  insert_constraint_goal(A/B, E, D, J),
        attach_constraint_atom(A/B, K, E, L),
        delay_phase_end(validate_store_type_assumptions,
                        chr_translate:update_suspension_field(A/B, E, state, inactive, M)),
        insert_constraint_internal_constraint_goal(A/B,
                                                   K,
                                                   E,
                                                   H,
                                                   D,
                                                   N),
        (   get_constraint_type(A/B, O)
        ->  maplist(dynamic_type_check_call, O, D, P),
            list2conj(P, Q)
        ;   Q=true
        ),
        C=(G:-Q, N, J, L, M, 'chr debug_event'(insert(#(G, E))), ('chr debugging'->('chr debug_event'(call(E)), I;'chr debug_event'(fail(E)), !, fail), ('chr debug_event'(exit(E));'chr debug_event'(redo(E)), fail);I))
    ;   get_allocation_occurrence(A/B, 0)
    ->  gen_insert_constraint_internal_goal(A/B,
                                            R,
                                            F,
                                            D,
                                            E),
        delay_phase_end(validate_store_type_assumptions,
                        chr_translate:update_suspension_field(A/B, E, state, inactive, M)),
        C=(G:-R, M, I)
    ;   C=(G:-I)
    ).
make_suspension_continuation_goal(A/B, C, D) :-
    (   may_trigger(A/B)
    ->  build_head(A, B, [0], C, E),
        (   chr_pp_flag(debugable, off)
        ->  D=E
        ;   get_target_module(F),
            D=F:E
        )
    ;   D=true
    ).
gen_cond_attach_clause(A/B, C, D, E) :-
    (   is_finally_stored(A/B)
    ->  get_allocation_occurrence(A/B, F),
        get_max_occurrence(A/B, G),
        (   chr_pp_flag(debugable, off),
            G<F
        ->  (   only_ground_indexed_arguments(A/B)
            ->  gen_insert_constraint_internal_goal(A/B,
                                                    H,
                                                    I,
                                                    J,
                                                    K)
            ;   gen_cond_attach_goal(A/B,
                                     H,
                                     I,
                                     J,
                                     K)
            )
        ;   vars_susp(B, J, K, I),
            gen_uncond_attach_goal(A/B,
                                   K,
                                   J,
                                   H,
                                   _)
        ),
        build_head(A, B, C, I, L),
        M=(L:-H),
        add_dummy_location(M, N),
        D=[N|E]
    ;   D=E
    ).
gen_cond_attach_goal(A/B, C, D, E, F) :-
    vars_susp(B, E, F, D),
    make_suspension_continuation_goal(A/B, D, G),
    (   get_store_type(A/B, var_assoc_store(_, _))
    ->  H=true
    ;   attach_constraint_atom(A/B, I, F, H)
    ),
    _=..[A|E],
    insert_constraint_goal(A/B, F, E, J),
    insert_constraint_internal_constraint_goal(A/B,
                                               I,
                                               F,
                                               G,
                                               E,
                                               K),
    (   may_trigger(A/B)
    ->  activate_constraint_goal(A/B,
                                 (J, H),
                                 I,
                                 F,
                                 _,
                                 L),
        C=(var(F)->K, J, H;L)
    ;   C=(K, J, H)
    ).
gen_insert_constraint_internal_goal(A/B, C, D, E, F) :-
    vars_susp(B, E, F, D),
    make_suspension_continuation_goal(A/B, D, G),
    (   \+ only_ground_indexed_arguments(A/B),
        \+ get_store_type(A/B, var_assoc_store(_, _))
    ->  attach_constraint_atom(A/B, H, F, I)
    ;   I=true
    ),
    _=..[A|E],
    insert_constraint_goal(A/B, F, E, J),
    insert_constraint_internal_constraint_goal(A/B,
                                               H,
                                               F,
                                               G,
                                               E,
                                               K),
    (   only_ground_indexed_arguments(A/B),
        chr_pp_flag(debugable, off)
    ->  C=(K, J)
    ;   C=(K, J, I)
    ).
gen_uncond_attach_goal(A, B, C, D, E) :-
    (   \+ only_ground_indexed_arguments(A),
        \+ get_store_type(A, var_assoc_store(_, _))
    ->  attach_constraint_atom(A, F, B, G)
    ;   G=true
    ),
    insert_constraint_goal(A, B, C, H),
    (   chr_pp_flag(late_allocation, on)
    ->  activate_constraint_goal(A,
                                 (H, G),
                                 F,
                                 B,
                                 E,
                                 D)
    ;   activate_constraint_goal(A, true, F, B, E, D)
    ).
head1_code(A, B, C, D, E, F, G, H, I, J) :-
    E=pragma(K, _, _, _, L),
    K=rule(_, M, _, _),
    (   M==[]
    ->  reorder_heads(L, A, C, D, N, O),
        simplification_code(A,
                            B,
                            N,
                            O,
                            E,
                            F,
                            G,
                            H,
                            I,
                            J)
    ;   simpagation_head1_code(A,
                               B,
                               C,
                               D,
                               E,
                               F,
                               G,
                               H,
                               I,
                               J)
    ).
head2_code(A, B, C, D, E, F, G, H, I, J) :-
    E=pragma(K, _, _, _, L),
    K=rule(M, _, _, _),
    (   M==[]
    ->  reorder_heads(L, A, C, D, N, O),
        propagation_code(A,
                         B,
                         N,
                         O,
                         K,
                         L,
                         F,
                         G,
                         H,
                         I,
                         J)
    ;   simpagation_head2_code(A,
                               B,
                               C,
                               D,
                               E,
                               F,
                               G,
                               H,
                               I,
                               J)
    ).
gen_alloc_inc_clause(A/B, C, D, E, F) :-
    vars_susp(B, G, H, I),
    build_head(A, B, D, I, J),
    inc_id(D, K),
    build_head(A, B, K, I, L),
    gen_occ_allocation(A/B, C, G, H, M),
    N=(J:-M, L),
    add_dummy_location(N, O),
    E=[O|F].
gen_occ_allocation(A, B, C, D, E) :-
    get_allocation_occurrence(A, F),
    get_occurrence_code_id(A, F, G),
    get_occurrence_code_id(A, B, H),
    (   chr_pp_flag(debugable, off),
        H==G
    ->  allocate_constraint_goal(A, D, C, I),
        (   may_trigger(A)
        ->  E=(var(D)->I;true)
        ;   E=I
        )
    ;   E=true
    ).
gen_occ_allocation_in_guard(A, B, C, D, E) :-
    get_allocation_occurrence(A, F),
    (   chr_pp_flag(debugable, off),
        B<F
    ->  allocate_constraint_goal(A, D, C, G),
        (   may_trigger(A)
        ->  E=(var(D)->G;true)
        ;   E=G
        )
    ;   E=true
    ).
guard_via_reschedule_new(A, B, C, D, E, F) :-
    (   chr_pp_flag(guard_via_reschedule, on)
    ->  guard_via_reschedule_main_new(A,
                                      B,
                                      C,
                                      D,
                                      E,
                                      G),
        list2conj(G, F)
    ;   length(A, H),
        length(E, H),
        length(B, I),
        length(D, I),
        append(E, D, J),
        list2conj(J, F)
    ).
guard_via_reschedule_main_new(A, B, C, D, E, F) :-
    initialize_unit_dictionary(C, G),
    maplist(wrap_in_functor(lookup), A, H),
    maplist(wrap_in_functor(guard), B, I),
    build_units(H, I, G, J),
    dependency_reorder(J, K),
    wrappedunits2lists(K, L, E, F),
    sort(L, M),
    snd_of_pairs(M, D).
wrappedunits2lists([], [], [], []).
wrappedunits2lists([unit(A, B, _, _)|C], D, E, F) :-
    F=[G|H],
    (   B=lookup(I)
    ->  E=[G|J],
        D=K
    ;   B=guard(I)
    ->  D=[A-G|K],
        E=J
    ),
    wrappedunits2lists(C, K, J, H).
guard_splitting(A, B) :-
    A=rule(C, D, E, _),
    append(C, D, F),
    conj2list(E, G),
    term_variables(F, H),
    split_off_simple_guard_new(G, H, I, J),
    append(I, [K], B),
    term_variables(J, L),
    ground_vars(F, M),
    list_difference_eq(H, M, N),
    intersect_eq(N, L, O),
    maplist(chr_lock, O, P),
    maplist(chr_unlock, O, Q),
    list2conj(P, R),
    list2conj(Q, S),
    list2conj(J, T),
    K=(R, T, S).
guard_body_copies3(A, B, C, D, E) :-
    A=rule(_, _, _, F),
    my_term_copy(B, C, G, D),
    my_term_copy(F, G, E).
split_off_simple_guard_new([], _, [], []).
split_off_simple_guard_new([A|B], C, D, E) :-
    (   simple_guard_new(A, C)
    ->  D=[A|F],
        split_off_simple_guard_new(B, C, F, E)
    ;   D=[],
        E=[A|B]
    ).
simple_guard_new(A, B) :-
    builtin_binds_b(A, C),
    not(( member(D, C),
          memberchk_eq(D, B)
        )).
dependency_reorder(A, B) :-
    dependency_reorder(A, [], B).
dependency_reorder([], A, B) :-
    reverse(A, B).
dependency_reorder([A|B], C, D) :-
    A=unit(_, _, E, F),
    (   E==fixed
    ->  G=[A|C]
    ;   dependency_insert(C, A, F, G)
    ),
    dependency_reorder(B, G, D).
dependency_insert([], A, _, [A]).
dependency_insert([A|B], C, D, E) :-
    A=unit(F, _, _, _),
    (   memberchk(F, D)
    ->  E=[C, A|B]
    ;   E=[A|G],
        dependency_insert(B, C, D, G)
    ).
build_units(A, B, C, D) :-
    build_retrieval_units(A, 1, E, C, F, D, G),
    build_guard_units(B, E, F, G).
build_retrieval_units([], A, A, B, B, C, C).
build_retrieval_units([A|B], C, D, E, F, G, H) :-
    term_variables(A, I),
    update_unit_dictionary(I, C, E, J, [], K),
    G=[unit(C, A, fixed, K)|L],
    M is C+1,
    build_retrieval_units(B,
                          M,
                          D,
                          J,
                          F,
                          L,
                          H).
initialize_unit_dictionary(A, B) :-
    term_variables(A, C),
    pair_all_with(C, 0, B).
update_unit_dictionary([], _, A, A, B, B).
update_unit_dictionary([A|B], C, D, E, F, G) :-
    (   lookup_eq(D, A, H)
    ->  (   (   H==C
            ;   memberchk(H, F)
            )
        ->  I=F
        ;   I=[H|F]
        ),
        J=D
    ;   J=[A-C|D],
        I=F
    ),
    update_unit_dictionary(B, C, J, E, I, G).
build_guard_units(A, B, C, D) :-
    (   A=[E]
    ->  D=[unit(B, E, fixed, [])]
    ;   A=[E|F]
    ->  term_variables(E, G),
        update_unit_dictionary2(G, B, C, H, [], I),
        D=[unit(B, E, movable, I)|J],
        K is B+1,
        build_guard_units(F, K, H, J)
    ).
update_unit_dictionary2([], _, A, A, B, B).
update_unit_dictionary2([A|B], C, D, E, F, G) :-
    (   lookup_eq(D, A, H)
    ->  (   (   H==C
            ;   memberchk(H, F)
            )
        ->  I=F
        ;   I=[H|F]
        ),
        J=[A-C|D]
    ;   J=[A-C|D],
        I=F
    ),
    update_unit_dictionary2(B, C, J, E, I, G).
functional_dependency_analysis(A) :-
    (   fail,
        chr_pp_flag(functional_dependency_analysis, on)
    ->  functional_dependency_analysis_main(A)
    ;   true
    ).
functional_dependency_analysis_main([]).
functional_dependency_analysis_main([A|B]) :-
    (   discover_unique_pattern(A, C, D, E, F)
    ->  functional_dependency(C, D, E, F)
    ;   true
    ),
    functional_dependency_analysis_main(B).
discover_unique_pattern(A, B/C, D, E, F) :-
    A=pragma(G, _, _, _, D),
    G=rule(H, I, J, _),
    (   H=[K],
        I=[L]
    ->  true
    ;   H=[K, L],
        I==[]
    ->  true
    ),
    check_unique_constraints(K, L, J, D, M),
    term_variables(K, N),
    \+ ( member(O, N),
         lookup_eq(M, O, P),
         memberchk_eq(P, N)
       ),
    select_pragma_unique_variables(N, M, Q),
    copy_term_nat(K-Q, E-F),
    functor(K, B, C).
select_pragma_unique_variables([], _, []).
select_pragma_unique_variables([A|B], C, D) :-
    (   lookup_eq(C, A, _)
    ->  D=E
    ;   D=[A|E]
    ),
    select_pragma_unique_variables(B, C, E).
set_semantics_rules(A) :-
    (   fail,
        chr_pp_flag(set_semantics_rule, on)
    ->  set_semantics_rules_main(A)
    ;   true
    ).
set_semantics_rules_main([]).
set_semantics_rules_main([A|B]) :-
    set_semantics_rule_main(A),
    set_semantics_rules_main(B).
set_semantics_rule_main(A) :-
    A=pragma(B, C, _, _, D),
    (   B=rule([E], [F], true, _),
        C=ids([G], [H]),
        \+ is_passive(D, G),
        functor(E, I, J),
        get_functional_dependency(I/J, D, K, L),
        copy_term_nat(K-L, E-M),
        copy_term_nat(K-L, F-N),
        M==N
    ->  passive(D, H)
    ;   true
    ).
check_unique_constraints(A, B, C, D, E) :-
    \+ any_passive_head(D),
    variable_replacement(A-B, B-A, E),
    copy_with_variable_replacement(C, F, E),
    negate_b(C, G),
    once(entails_b(G, F)).
symmetry_analysis(A) :-
    (   chr_pp_flag(check_unnecessary_active, off)
    ->  true
    ;   symmetry_analysis_main(A)
    ).
symmetry_analysis_main([]).
symmetry_analysis_main([A|B]) :-
    A=pragma(C, ids(D, E), _, _, F),
    C=rule(G, H, _, _),
    (   (   \+ chr_pp_flag(check_unnecessary_active, simplification)
        ;   H==[]
        ),
        G\==[]
    ->  symmetry_analysis_heads_simplification(G,
                                               D,
                                               [],
                                               [],
                                               C,
                                               F),
        symmetry_analysis_heads_propagation(H,
                                            E,
                                            [],
                                            [],
                                            C,
                                            F)
    ;   true
    ),
    symmetry_analysis_main(B).
symmetry_analysis_heads_simplification([], [], _, _, _, _).
symmetry_analysis_heads_simplification([A|B], [C|D], E, F, G, H) :-
    (   \+ is_passive(H, C),
        member2(E, F, I-J),
        \+ is_passive(H, J),
        variable_replacement(I, A, K),
        copy_with_variable_replacement(G, L, K),
        identical_guarded_rules(G, L)
    ->  passive(H, C)
    ;   true
    ),
    symmetry_analysis_heads_simplification(B,
                                           D,
                                           [A|E],
                                           [C|F],
                                           G,
                                           H).
symmetry_analysis_heads_propagation([], [], _, _, _, _).
symmetry_analysis_heads_propagation([A|B], [C|D], E, F, G, H) :-
    (   \+ is_passive(H, C),
        member2(E, F, I-J),
        \+ is_passive(H, J),
        variable_replacement(I, A, K),
        copy_with_variable_replacement(G, L, K),
        identical_rules(G, L)
    ->  passive(H, C)
    ;   true
    ),
    symmetry_analysis_heads_propagation(B,
                                        D,
                                        [A|E],
                                        [C|F],
                                        G,
                                        H).
simplification_code(A, B, C, D, E, F, G, H, I, J) :-
    E=pragma(K, _, _, _, L),
    head_info1(A, F, _, M, N, O),
    build_head(F, H, N, P),
    get_constraint_mode(F, Q),
    head_arg_matches(O, Q, [], R, S, [], T),
    guard_splitting(K, U),
    (   is_stored_in_guard(F, L)
    ->  V=[_|U]
    ;   V=U
    ),
    guard_via_reschedule_new(C,
                             V,
                             A,
                             W,
                             X,
                             Y),
    rest_heads_retrieval_and_matching(C,
                                      D,
                                      A,
                                      X,
                                      Z,
                                      S,
                                      A1,
                                      [],
                                      [],
                                      [],
                                      T,
                                      _),
    guard_body_copies3(K, V, A1, W, B1),
    (   is_stored_in_guard(F, L)
    ->  gen_occ_allocation_in_guard(F,
                                    G,
                                    C1,
                                    M,
                                    D1),
        gen_uncond_attach_goal(F, M, C1, E1, _),
        W=[F1|_],
        F1=(D1, E1)
    ;   true
    ),
    partner_constraint_detachments(Z, C, A1, G1),
    active_constraint_detachment(H, M, A, A1, H1),
    (   chr_pp_flag(debugable, on)
    ->  K=rule(_, _, I1, J1),
        my_term_copy(I1-J1, A1, K1-L1),
        sort_by_key([M|Z], [B|D], M1),
        N1='chr debug_event'(try(M1, [], K1, L1)),
        O1='chr debug_event'(apply(M1, [], K1, L1)),
        instrument_goal(P1, N1, O1, Q1)
    ;   Q1=P1
    ),
    actual_cut(F, G, P1),
    R1=(P:-R, Y, Q1, G1, H1, B1),
    add_location(R1, L, S1),
    I=[S1|J].
actual_cut(A, B, C) :-
    (   unconditional_occurrence(A, B),
        chr_pp_flag(late_allocation, on)
    ->  C=true
    ;   C=!
    ).
add_location(A, B, C) :-
    (   chr_pp_flag(line_numbers, on)
    ->  get_line_number(B, D:E),
        C='$source_location'(D, E):A
    ;   C=A
    ).
add_dummy_location(A, B) :-
    (   chr_pp_flag(line_numbers, on)
    ->  get_chr_source_file(C),
        B='$source_location'(C, 1):A
    ;   B=A
    ).
head_arg_matches(A, B, C, D, E) :-
    head_arg_matches(A, B, C, D, E, [], _).
head_arg_matches(A, B, C, D, E, F, G) :-
    head_arg_matches_(A, B, C, F, H, E, G),
    list2conj(H, D).
head_arg_matches_([], [], A, B, [], A, B).
head_arg_matches_([silent(A-_)|B], [C|D], E, F, G, H, I) :-
    !,
    (   C==(+)
    ->  term_variables(A, J, F),
        head_arg_matches_(B,
                          D,
                          E,
                          J,
                          G,
                          H,
                          I)
    ;   head_arg_matches_(B,
                          D,
                          E,
                          F,
                          G,
                          H,
                          I)
    ).
head_arg_matches_([A-B|C], [D|E], F, G, H, I, J) :-
    (   var(A)
    ->  (   lookup_eq(F, A, K)
        ->  (   D=(+)
            ->  (   memberchk_eq(A, G)
                ->  H=[B=K|L],
                    M=G
                ;   H=[B==K|L],
                    M=[A|G]
                )
            ;   H=[B==K|L],
                M=G
            ),
            N=F
        ;   N=[A-B|F],
            H=L,
            (   D=(+)
            ->  M=[A|G]
            ;   M=G
            )
        ),
        O=C,
        P=E
    ;   ground(A),
        A='$chr_identifier_match'(Q, R)
    ->  identifier_label_atom(R, B, Q, S),
        H=[S|L],
        F=N,
        M=G,
        O=C,
        P=E
    ;   atomic(A)
    ->  (   D=(+)
        ->  H=[B=A|L]
        ;   H=[B==A|L]
        ),
        F=N,
        M=G,
        O=C,
        P=E
    ;   D==(+),
        is_ground(G, A)
    ->  copy_with_variable_replacement(A, T, F),
        H=[B=T|L],
        F=N,
        M=G,
        O=C,
        P=E
    ;   D==(?),
        is_ground(G, A)
    ->  copy_with_variable_replacement(A, T, F),
        H=[B==T|L],
        F=N,
        M=G,
        O=C,
        P=E
    ;   A=..[_|U],
        functor(A, V, W),
        functor(X, V, W),
        X=..[_|Y],
        (   D=(+)
        ->  H=[B=X|L]
        ;   H=[nonvar(B), B=X|L]
        ),
        pairup(U, Y, Z),
        append(Z, C, O),
        replicate(W, D, A1),
        append(A1, E, P),
        N=F,
        M=G
    ),
    head_arg_matches_(O,
                      P,
                      N,
                      M,
                      L,
                      I,
                      J).
add_heads_types([], A, A).
add_heads_types([A|B], C, D) :-
    add_head_types(A, C, E),
    add_heads_types(B, E, D).
add_head_types(A, B, C) :-
    functor(A, D, E),
    get_constraint_type_det(D/E, F),
    A=..[_|G],
    add_args_types(G, F, B, C).
add_args_types([], [], A, A).
add_args_types([A|B], [C|D], E, F) :-
    add_arg_types(A, C, E, G),
    add_args_types(B, D, G, F).
add_arg_types(A, B, C, D) :-
    (   B==any
    ->  D=C
    ;   var(A)
    ->  (   lookup_eq(C, A, _)
        ->  D=C
        ;   D=[A-B|C]
        )
    ;   D=C
    ).
add_heads_ground_variables([], A, A).
add_heads_ground_variables([A|B], C, D) :-
    add_head_ground_variables(A, C, E),
    add_heads_ground_variables(B, E, D).
add_head_ground_variables(A, B, C) :-
    functor(A, D, E),
    get_constraint_mode(D/E, F),
    A=..[_|G],
    add_arg_ground_variables(G, F, B, C).
add_arg_ground_variables([], [], A, A).
add_arg_ground_variables([A|B], [C|D], E, F) :-
    (   C==(+)
    ->  term_variables(A, G),
        add_var_ground_variables(G, E, H)
    ;   E=H
    ),
    add_arg_ground_variables(B, D, H, F).
add_var_ground_variables([], A, A).
add_var_ground_variables([A|B], C, D) :-
    (   memberchk_eq(A, C)
    ->  E=C
    ;   E=[A|C]
    ),
    add_var_ground_variables(B, E, D).
is_ground(A, B) :-
    (   ground(B)
    ->  true
    ;   compound(B)
    ->  B=..[_|C],
        maplist(is_ground(A), C)
    ;   memberchk_eq(B, A)
    ).
check_ground(A, B, C) :-
    term_variables(B, D),
    check_ground_variables(D, A, C).
check_ground_variables([], _, true).
check_ground_variables([A|B], C, D) :-
    (   memberchk_eq(A, C)
    ->  check_ground_variables(B, C, D)
    ;   D=(ground(A), E),
        check_ground_variables(B, C, E)
    ).
rest_heads_retrieval_and_matching(A, B, C, D, E, F, G, H, I, J) :-
    rest_heads_retrieval_and_matching(A,
                                      B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H,
                                      I,
                                      J,
                                      [],
                                      _).
rest_heads_retrieval_and_matching(A, B, C, D, E, F, G, H, I, J, K, L) :-
    (   A=[_|_]
    ->  rest_heads_retrieval_and_matching_n(A,
                                            B,
                                            H,
                                            I,
                                            C,
                                            D,
                                            E,
                                            F,
                                            G,
                                            J,
                                            K,
                                            L)
    ;   D=[],
        E=[],
        F=G,
        K=L
    ).
rest_heads_retrieval_and_matching_n([], _, _, _, _, [], [], A, A, _, B, B).
rest_heads_retrieval_and_matching_n([A|B], [_|C], D, E, F, [G|H], [I|J], K, L, _, M, N) :-
    functor(A, O, P),
    head_info(A, P, Q, _, _, R),
    get_store_type(O/P, S),
    (   S==default
    ->  passive_head_via(A, [F|D], K, T, U),
        delay_phase_end(validate_store_type_assumptions,
                        (static_suspension_term(O/P, V), get_static_suspension_term_field(arguments, O/P, V, Q), get_static_suspension_field(O/P, V, state, active, W))),
        get_constraint_mode(O/P, X),
        head_arg_matches(R,
                         X,
                         K,
                         Y,
                         Z,
                         M,
                         A1),
        B1=R,
        sbag_member_call(I, U, C1),
        D1=(T, C1, I=V, W),
        inline_matching_goal(Y, E1)
    ;   delay_phase_end(validate_store_type_assumptions,
                        (static_suspension_term(O/P, V), get_static_suspension_term_field(arguments, O/P, V, Q))),
        existential_lookup(S,
                           A,
                           [F|D],
                           K,
                           M,
                           V,
                           D1,
                           I,
                           R,
                           B1),
        get_constraint_mode(O/P, X),
        F1=X,
        head_arg_matches(B1,
                         F1,
                         K,
                         Y,
                         Z,
                         M,
                         A1),
        filter_append(B1, Z, G1),
        translate(A1, G1, H1),
        translate(A1, Z, I1),
        inline_matching_goal(Y, E1, H1, I1)
    ),
    different_from_other_susps(A, I, D, E, J1),
    G=(D1, J1, E1),
    rest_heads_retrieval_and_matching_n(B,
                                        C,
                                        [A|D],
                                        [I|E],
                                        F,
                                        H,
                                        J,
                                        Z,
                                        L,
                                        _,
                                        A1,
                                        N).
inline_matching_goal(A, B) :-
    inline_matching_goal(A, B, [], []).
inline_matching_goal(A==B, true, C, D) :-
    memberchk_eq(A, C),
    memberchk_eq(B, D),
    A=B,
    !.
inline_matching_goal((A, B), (C, D), E, F) :-
    !,
    inline_matching_goal(A, C, E, F),
    inline_matching_goal(B, D, E, F).
inline_matching_goal(A, A, _, _).
filter_mode([], _, _, []).
filter_mode([A-B|C], [_-D|E], [F|G], H) :-
    (   B==D
    ->  H=[F|I],
        filter_mode(C, E, G, I)
    ;   filter_mode([A-B|C], E, G, H)
    ).
filter_append([], A, A).
filter_append([A|B], C, D) :-
    (   A=silent(_)
    ->  filter_append(B, C, D)
    ;   D=[A|E],
        filter_append(B, C, E)
    ).
check_unique_keys([], _).
check_unique_keys([A|B], C) :-
    lookup_eq(C, A, _),
    check_unique_keys(B, C).
different_from_other_susps(A, B, C, D, E) :-
    different_from_other_susps_(C, D, A, B, F),
    list2conj(F, E).
different_from_other_susps_(_, [], _, _, []) :-
    !.
different_from_other_susps_([A|B], [C|D], E, F, G) :-
    (   functor(E, H, I),
        functor(A, H, I),
        copy_term_nat(A-E, J-K),
        \+ \+ J=K
    ->  G=[F\==C|L]
    ;   G=L
    ),
    different_from_other_susps_(B, D, E, F, L).
passive_head_via(A, B, C, D, E) :-
    functor(A, F, G),
    get_constraint_index(F/G, _),
    common_variables(A, B, H),
    ground_vars([A], I),
    list_difference_eq(H, I, J),
    global_list_store_name(F/G, K),
    L=nb_getval(K, E),
    get_constraint_mode(F/G, M),
    (   N==[]
    ->  D=L
    ;   member(O, J),
        nth1(P, M, -),
        arg(P, A, Q),
        Q==O
    ->  translate([O], C, [R]),
        gen_get_mod_constraints(F/G, R, S, E),
        D=S
    ;   translate(J, C, N),
        add_heads_types(B, [], T),
        my_term_copy(T, C, U),
        gen_get_mod_constraints(F/G,
                                N,
                                U,
                                V,
                                S,
                                E),
        D=(V->S;L)
    ).
common_variables(A, B, C) :-
    term_variables(A, D),
    term_variables(B, E),
    intersect_eq(D, E, C).
gen_get_mod_constraints(A, B, C, D, E, F) :-
    via_goal(B, C, D, G),
    get_target_module(H),
    E=(get_attr(G, H, I), J),
    get_max_constraint_index(K),
    (   K==1
    ->  J=true,
        F=I
    ;   get_constraint_index(A, L),
        get_suspensions(K, L, I, J, F)
    ).
via_goal(A, B, C, D) :-
    (   A=[]
    ->  C=fail
    ;   A=[E]
    ->  lookup_type(B, E, F),
        (   atomic_type(F)
        ->  C=var(E),
            E=D
        ;   C='chr newvia_1'(E, D)
        )
    ;   A=[E, G]
    ->  C='chr newvia_2'(E, G, D)
    ;   C='chr newvia'(A, D)
    ).
lookup_type(A, B, C) :-
    (   lookup_eq(A, B, C)
    ->  true
    ;   C=any
    ).
gen_get_mod_constraints(A, B, C, D) :-
    get_target_module(E),
    C=(get_attr(B, E, F), G),
    get_max_constraint_index(H),
    (   H==1
    ->  G=true,
        D=F
    ;   get_constraint_index(A, I),
        get_suspensions(H, I, F, G, D)
    ).
guard_body_copies(A, B, C, D) :-
    guard_body_copies2(A, B, E, D),
    list2conj(E, C).
guard_body_copies2(A, B, C, D) :-
    A=rule(_, E, F, G),
    conj2list(F, H),
    split_off_simple_guard(H, B, I, J),
    my_term_copy(I-J, B, K, L-M),
    append(L, [N], C),
    term_variables(J, O),
    term_variables(M, P),
    ground_vars(E, Q),
    list_difference_eq(O, Q, R),
    (   chr_pp_flag(guard_locks, off)
    ->  S=[],
        T=[]
    ;   bagof(U-V,
              W^X^(lists:member(W, R), pairlist:lookup_eq(B, W, X), memberchk_eq(X, P), chr_lock(X, U), chr_unlock(X, V)),
              Y)
    ->  once(pairup(S, T, Y))
    ;   S=[],
        T=[]
    ),
    list2conj(S, Z),
    list2conj(T, A1),
    list2conj(M, B1),
    N=(Z, B1, A1),
    my_term_copy(G, K, D).
split_off_simple_guard([], _, [], []).
split_off_simple_guard([A|B], C, D, E) :-
    (   simple_guard(A, C)
    ->  D=[A|F],
        split_off_simple_guard(B, C, F, E)
    ;   D=[],
        E=[A|B]
    ).
simple_guard(A, B) :-
    binds_b(A, C),
    \+ ( member(D, C),
         lookup_eq(B, D, _)
       ).
active_constraint_detachment(A, B, C, D, E) :-
    functor(C, F, G),
    H=F/G,
    (   is_stored(H)
    ->  (   (   A==[0],
                chr_pp_flag(store_in_guards, off)
            ;   get_allocation_occurrence(H, I),
                get_max_occurrence(H, J),
                J<I
            ),
            only_ground_indexed_arguments(H),
            chr_pp_flag(late_allocation, on)
        ->  E=true
        ;   gen_uncond_susp_detachment(C,
                                       B,
                                       active,
                                       D,
                                       K),
            (   chr_pp_flag(late_allocation, on)
            ->  E=(var(B)->true;K)
            ;   E=K
            )
        )
    ;   E=true
    ).
partner_constraint_detachments([], [], _, true).
partner_constraint_detachments([A|B], [C|D], E, (F, G)) :-
    gen_uncond_susp_detachment(C, A, partner, E, F),
    partner_constraint_detachments(B, D, E, G).
gen_uncond_susp_detachment(A, B, C, D, E) :-
    functor(A, F, G),
    H=F/G,
    (   is_stored(H)
    ->  E=(I, J),
        (   chr_pp_flag(debugable, on)
        ->  I='chr debug_event'(remove(B))
        ;   I=true
        ),
        remove_constraint_goal(H,
                               B,
                               K,
                               true,
                               (L, M),
                               C,
                               J),
        delete_constraint_goal(A, B, D, L),
        (   \+ only_ground_indexed_arguments(H),
            \+ get_store_type(H, var_assoc_store(_, _))
        ->  detach_constraint_atom(H, K, B, M)
        ;   M=true
        )
    ;   E=true
    ).
simpagation_head1_code(A, B, C, D, E, F/G, _, H, I, J) :-
    E=pragma(K, ids(_, L), _, _, M),
    K=rule(_, N, O, P),
    head_info(A, G, Q, R, S, T),
    get_constraint_mode(F/G, U),
    head_arg_matches(T, U, [], V, W, [], X),
    build_head(F, G, H, S, Y),
    append(C, N, Z),
    append(D, L, A1),
    reorder_heads(M, A, Z, A1, B1, C1),
    guard_splitting(K, D1),
    (   is_stored_in_guard(F/G, M)
    ->  E1=[_|D1]
    ;   E1=D1
    ),
    guard_via_reschedule_new(B1,
                             E1,
                             A,
                             F1,
                             G1,
                             H1),
    rest_heads_retrieval_and_matching(B1,
                                      C1,
                                      A,
                                      G1,
                                      I1,
                                      W,
                                      J1,
                                      [],
                                      [],
                                      [],
                                      X,
                                      _),
    split_by_ids(C1,
                 I1,
                 D,
                 K1,
                 L1,
                 M1,
                 N1),
    guard_body_copies3(K, E1, J1, F1, O1),
    (   is_stored_in_guard(F/G, M)
    ->  gen_uncond_attach_goal(F/G,
                               R,
                               Q,
                               P1,
                               _),
        F1=[Q1|_],
        Q1=P1
    ;   true
    ),
    sort_by_key(K1, L1, R1),
    partner_constraint_detachments(R1, C, J1, S1),
    active_constraint_detachment(H, R, A, J1, T1),
    (   chr_pp_flag(debugable, on)
    ->  my_term_copy(O-P, J1, U1-V1),
        sort_by_key([R|K1], [B|L1], W1),
        sort_by_key(M1, N1, X1),
        Y1='chr debug_event'(try(W1, X1, U1, V1)),
        Z1='chr debug_event'(apply(W1, X1, U1, V1)),
        instrument_goal(!, Y1, Z1, A2)
    ;   A2=!
    ),
    B2=(Y:-V, H1, A2, S1, T1, O1),
    add_location(B2, M, C2),
    I=[C2|J].
split_by_ids([], [], _, [], []).
split_by_ids([A|B], [C|D], E, F, G) :-
    (   memberchk_eq(A, E)
    ->  F=[C|H],
        G=I
    ;   F=H,
        G=[C|I]
    ),
    split_by_ids(B, D, E, H, I).
split_by_ids([], [], _, [], [], [], []).
split_by_ids([A|B], [C|D], E, F, G, H, I) :-
    (   memberchk_eq(A, E)
    ->  F=[C|J],
        G=[A|K],
        H=L,
        I=M
    ;   F=J,
        G=K,
        H=[C|L],
        I=[A|M]
    ),
    split_by_ids(B, D, E, J, K, L, M).
simpagation_head2_code(A, B, C, D, E, F, G, H, I, J) :-
    E=pragma(K, ids(L, M), _, _, N),
    K=rule(O, _, P, Q),
    append(O, C, R),
    append(L, D, S),
    reorder_heads(N,
                  A,
                  R,
                  S,
                  [T|U],
                  [V|W]),
    simpagation_head2_prelude(A,
                              T,
                              [U, P, Q],
                              F,
                              G,
                              H,
                              I,
                              X),
    extend_id(H, Y),
    (   memberchk_eq(V, M)
    ->  simpagation_universal_searches(U,
                                       W,
                                       M,
                                       [T, A],
                                       K,
                                       F,
                                       G,
                                       Z,
                                       A1,
                                       B1,
                                       Y,
                                       C1,
                                       X,
                                       D1)
    ;   X=D1,
        Y=C1,
        Z=U,
        A1=[T, A],
        B1=W
    ),
    universal_search_iterator_end(A1,
                                  Z,
                                  K,
                                  F,
                                  G,
                                  C1,
                                  D1,
                                  E1),
    simpagation_head2_worker(A1,
                             Z,
                             B1,
                             B,
                             E,
                             F,
                             G,
                             C1,
                             E1,
                             J).
simpagation_universal_searches([], [], _, A, _, _, _, [], A, [], B, B, C, C).
simpagation_universal_searches(A, [B|C], D, E, F, G, H, I, J, K, L, M, N, O) :-
    A=[P|Q],
    inc_id(L, R),
    universal_search_iterator_end(E,
                                  A,
                                  F,
                                  G,
                                  H,
                                  L,
                                  N,
                                  S),
    universal_search_iterator(A,
                              E,
                              F,
                              G,
                              H,
                              L,
                              S,
                              T),
    (   memberchk_eq(B, D)
    ->  simpagation_universal_searches(Q,
                                       C,
                                       D,
                                       [P|E],
                                       F,
                                       G,
                                       H,
                                       I,
                                       J,
                                       K,
                                       R,
                                       M,
                                       T,
                                       O)
    ;   M=R,
        T=O,
        I=Q,
        J=[P|E],
        C=K
    ).
simpagation_head2_prelude(A, B, C, D/E, F, G, H, I) :-
    head_info(A, E, J, K, L, M),
    build_head(D, E, G, L, N),
    get_constraint_mode(D/E, O),
    head_arg_matches(M, O, [], P, Q, [], R),
    lookup_passive_head(B, [A], Q, R, S, T),
    gen_occ_allocation(D/E, F, J, K, U),
    extend_id(G, V),
    extra_active_delegate_variables(A,
                                    [B|C],
                                    Q,
                                    W),
    append([T|L], W, X),
    build_head(D, E, [F|V], X, Y),
    Z=(N:-P, S, !, U, Y),
    add_dummy_location(Z, A1),
    H=[A1|I].
extra_active_delegate_variables(A, B, C, D) :-
    A=..[_|E],
    delegate_variables(A, B, C, E, D).
passive_delegate_variables(A, B, C, D, E) :-
    term_variables(B, F),
    delegate_variables(A, C, D, F, E).
delegate_variables(A, B, C, D, E) :-
    term_variables(A, F),
    term_variables(B, G),
    intersect_eq(F, G, H),
    list_difference_eq(H, D, I),
    translate(I, C, E).
simpagation_head2_worker([A|B], C, D, _, E, F/G, H, I, J, K) :-
    E=pragma(L, ids(M, _), N, _, O),
    L=rule(_, _, P, Q),
    get_prop_inner_loop_vars(B,
                             [A, C, P, Q],
                             R,
                             S,
                             T,
                             U,
                             V),
    gen_var(W),
    gen_var(X),
    functor(A, Y, Z),
    gen_vars(Z, A1),
    head_info(A, Z, A1, W, _, B1),
    get_constraint_mode(Y/Z, C1),
    head_arg_matches(B1, C1, S, D1, E1, [], _),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(Y/Z, F1), get_static_suspension_field(Y/Z, F1, state, active, G1), get_static_suspension_term_field(arguments, Y/Z, F1, A1))),
    different_from_other_susps(A, W, B, V, H1),
    I1=(W=F1, G1, H1, D1),
    J1=[[W|X]|R],
    build_head(F, G, [H|I], J1, K1),
    guard_splitting(L, L1),
    (   is_stored_in_guard(F/G, O)
    ->  M1=[_|L1]
    ;   M1=L1
    ),
    guard_via_reschedule_new(C,
                             M1,
                             [A|B],
                             N1,
                             O1,
                             P1),
    rest_heads_retrieval_and_matching(C,
                                      D,
                                      [A|B],
                                      O1,
                                      Q1,
                                      E1,
                                      R1,
                                      [A|B],
                                      [W|V],
                                      []),
    split_by_ids(D, Q1, M, S1, T1),
    split_by_ids(D, C, M, U1, _),
    partner_constraint_detachments([W|S1],
                                   [A|U1],
                                   R1,
                                   V1),
    W1=[X|R],
    build_head(F, G, [H|I], W1, X1),
    Y1=[[]|R],
    build_head(F, G, [H|I], Y1, Z1),
    guard_body_copies3(L, M1, R1, N1, A2),
    (   is_stored_in_guard(F/G, O)
    ->  N1=[B2|_]
    ;   true
    ),
    (   is_observed(F/G, H)
    ->  gen_uncond_attach_goal(F/G,
                               T,
                               U,
                               C2,
                               D2),
        gen_state_cond_call(T,
                            F/G,
                            X1,
                            D2,
                            E2),
        gen_state_cond_call(T,
                            F/G,
                            Z1,
                            D2,
                            F2)
    ;   C2=true,
        E2=X1,
        F2=Z1
    ),
    (   chr_pp_flag(debugable, on)
    ->  my_term_copy(P-Q, S, G2-H2),
        I2='chr debug_event'(try([W|S1], [T|T1], G2, H2)),
        J2='chr debug_event'(apply([W|S1], [T|T1], G2, H2))
    ;   I2=true,
        J2=true
    ),
    (   is_stored_in_guard(F/G, O)
    ->  B2=C2,
        K2=true
    ;   B2=true,
        K2=C2
    ),
    (   member(unique(_, L2), N),
        check_unique_keys(L2, S)
    ->  M2=(K1:-I1->(P1, I2->J2, V1, K2, A2, F2;Z1);X1)
    ;   M2=(K1:-I1, P1, I2->J2, V1, K2, A2, E2;X1)
    ),
    add_location(M2, O, N2),
    J=[N2|K].
gen_state_cond_call(A, B, C, D, E) :-
    (   may_trigger(B)
    ->  does_use_field(B, generation),
        delay_phase_end(validate_store_type_assumptions,
                        (static_suspension_term(B, F), get_update_static_suspension_field(B, A, F, state, active, inactive, G, H), get_static_suspension_field(B, F, generation, D, I), get_static_suspension_term_field(arguments, B, F, J)))
    ;   delay_phase_end(validate_store_type_assumptions,
                        (static_suspension_term(B, F), get_update_static_suspension_field(B, A, F, state, active, inactive, G, H), get_static_suspension_term_field(arguments, B, F, J))),
        I=true
    ),
    E=(A=F, G, I->H, C;true).
propagation_code(A, B, C, D, E, F, G, H, I, J, K) :-
    (   C==[]
    ->  propagation_single_headed(A,
                                  B,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I,
                                  J,
                                  K)
    ;   propagation_multi_headed(A,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ).
propagation_single_headed(A, _, B, C, D/E, F, G, H, I) :-
    head_info(A, E, J, K, L, M),
    build_head(D, E, G, L, N),
    inc_id(G, O),
    build_head(D, E, O, L, P),
    get_constraint_mode(D/E, Q),
    head_arg_matches(M, Q, [], R, S, [], _),
    guard_body_copies(B, S, T, U),
    V=P,
    actual_cut(D/E, F, W),
    B=rule(_, _, X, Y),
    (   chr_pp_flag(debugable, on)
    ->  my_term_copy(X-Y, S, Z-A1),
        B1='chr debug_event'(try([], [K], Z, A1)),
        C1='chr debug_event'(apply([], [K], Z, A1)),
        instrument_goal(W, B1, C1, D1)
    ;   D1=W
    ),
    (   may_trigger(D/E),
        \+ has_no_history(C)
    ->  use_auxiliary_predicate(novel_production),
        use_auxiliary_predicate(extend_history),
        does_use_history(D/E, F),
        gen_occ_allocation(D/E, F, J, K, E1),
        (   named_history(C, F1, G1)
        ->  (   G1==[]
            ->  empty_named_history_novel_production(F1, H1),
                empty_named_history_extend_history(F1, I1)
            ;   J1=F1
            )
        ;   J1=C
        ),
        (   var(H1)
        ->  H1='$novel_production'(K, J1),
            I1='$extend_history'(K, J1)
        ;   true
        ),
        (   is_observed(D/E, F)
        ->  gen_uncond_attach_goal(D/E,
                                   K,
                                   J,
                                   K1,
                                   L1),
            gen_state_cond_call(K,
                                D/E,
                                V,
                                L1,
                                M1)
        ;   K1=true,
            M1=V
        )
    ;   E1=true,
        H1=true,
        I1=true,
        (   is_observed(D/E, F)
        ->  get_allocation_occurrence(D/E, N1),
            (   F==N1
            ->  gen_insert_constraint_internal_goal(D/E,
                                                    K1,
                                                    L,
                                                    J,
                                                    K),
                L1=0
            ;   K1=(O1, P1),
                gen_occ_allocation(D/E,
                                   F,
                                   J,
                                   K,
                                   O1),
                gen_uncond_attach_goal(D/E,
                                       K,
                                       J,
                                       P1,
                                       L1)
            ),
            gen_state_cond_call(K,
                                D/E,
                                V,
                                L1,
                                M1)
        ;   gen_occ_allocation(D/E,
                               F,
                               J,
                               K,
                               K1),
            M1=V
        )
    ),
    (   is_stored_in_guard(D/E, C)
    ->  Q1=K1,
        R1=true
    ;   Q1=true,
        R1=K1
    ),
    S1=(N:-R, E1, H1, Q1, T, D1, I1, R1, U, M1),
    add_location(S1, C, T1),
    H=[T1|I].
propagation_multi_headed(A, B, C, D, E, F, G, H, I, J) :-
    B=[K|L],
    propagation_prelude(A,
                        B,
                        D,
                        F,
                        G,
                        H,
                        I,
                        M),
    extend_id(H, N),
    propagation_nested_code(L,
                            [K, A],
                            C,
                            D,
                            E,
                            F,
                            G,
                            N,
                            M,
                            J).
propagation_prelude(A, [B|C], D, E/F, G, H, I, J) :-
    head_info(A, F, K, L, M, N),
    build_head(E, F, H, M, O),
    get_constraint_mode(E/F, P),
    head_arg_matches(N, P, [], Q, R, [], S),
    D=rule(_, _, T, U),
    extra_active_delegate_variables(A,
                                    [B, C, T, U],
                                    R,
                                    V),
    lookup_passive_head(B, [A], R, S, W, X),
    gen_occ_allocation(E/F, G, K, L, Y),
    extend_id(H, Z),
    append([X|M], V, A1),
    build_head(E, F, [G|Z], A1, B1),
    C1=B1,
    D1=(O:-Q, W, !, Y, C1),
    add_dummy_location(D1, E1),
    I=[E1|J].
propagation_nested_code([], [A|B], C, D, E, F, G, H, I, J) :-
    universal_search_iterator_end([A|B],
                                  [],
                                  D,
                                  F,
                                  G,
                                  H,
                                  I,
                                  K),
    propagation_body(A,
                     B,
                     C,
                     D,
                     E,
                     F,
                     G,
                     H,
                     K,
                     J).
propagation_nested_code([A|B], C, D, E, F, G, H, I, J, K) :-
    universal_search_iterator_end(C,
                                  [A|B],
                                  E,
                                  G,
                                  H,
                                  I,
                                  J,
                                  L),
    universal_search_iterator([A|B],
                              C,
                              E,
                              G,
                              H,
                              I,
                              L,
                              M),
    inc_id(I, N),
    propagation_nested_code(B,
                            [A|C],
                            D,
                            E,
                            F,
                            G,
                            H,
                            N,
                            M,
                            K).
check_fd_lookup_condition(A, B, _, _) :-
    get_store_type(A/B, global_singleton),
    !.
check_fd_lookup_condition(A, B, C, D) :-
    \+ may_trigger(A/B),
    get_functional_dependency(A/B, 1, E, F),
    copy_term(E-F, C-G),
    term_variables(D, H),
    intersect_eq(G, H, G),
    !.
propagation_body(A, B, C, D, E, F/G, H, I, J, K) :-
    D=rule(_, L, M, N),
    gen_var_susp_list_for_b(B,
                            [A, M, N],
                            O,
                            P,
                            Q,
                            R,
                            S),
    flatten(P, T),
    init(R, U),
    last(R, V),
    gen_var(W),
    gen_var(X),
    functor(A, Y, Z),
    gen_vars(Z, A1),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(Y/Z, B1), get_static_suspension_field(Y/Z, B1, state, active, C1), get_static_suspension_term_field(arguments, Y/Z, B1, A1))),
    D1=(W=B1, C1),
    E1=[[W|X]|T],
    build_head(F, G, [H|I], E1, F1),
    (   check_fd_lookup_condition(Y, Z, A, B)
    ->  universal_search_iterator_failure_vars(B,
                                               I,
                                               P,
                                               Q,
                                               S,
                                               G1,
                                               H1),
        I1=G1
    ;   I1=[X|T],
        H1=I
    ),
    (   H1=[_]
    ->  J1=H1
    ;   J1=[H|H1]
    ),
    build_head(F, G, J1, I1, K1),
    L1=K1,
    A=..[_|M1],
    pairup(M1, A1, N1),
    get_constraint_mode(Y/Z, O1),
    head_arg_matches(N1, O1, O, P1, Q1),
    different_from_other_susps(A, W, B, U, R1),
    guard_body_copies(D, Q1, S1, T1),
    get_occurrence(F/G, H, _, U1),
    (   is_observed(F/G, H)
    ->  init(Q, V1),
        gen_uncond_attach_goal(F/G,
                               V,
                               V1,
                               W1,
                               X1),
        gen_state_cond_call(V,
                            F/G,
                            L1,
                            X1,
                            Y1)
    ;   W1=true,
        Y1=L1
    ),
    (   (   is_least_occurrence(E)
        ;   has_no_history(E)
        )
    ->  Z1=true,
        A2=true
    ;   \+ may_trigger(F/G),
        maplist(is_passive(E), C)
    ->  Z1=true,
        A2=true
    ;   get_occurrence(F/G, H, _, U1),
        use_auxiliary_predicate(novel_production),
        use_auxiliary_predicate(extend_history),
        does_use_history(F/G, H),
        (   named_history(E, B2, C2)
        ->  (   C2==[]
            ->  empty_named_history_novel_production(B2, Z1),
                empty_named_history_extend_history(B2, A2)
            ;   reverse([W|U], D2),
                named_history_susps(C2,
                                    [U1|C],
                                    [V|D2],
                                    E2),
                E2=[F2|_],
                (   length(C2, 1)
                ->  A2='$extend_history'(F2, B2),
                    Z1='$novel_production'(F2, B2)
                ;   findall(G2,
                            ( member(H2, C2),
                              get_occurrence_from_id(G2,
                                                     _,
                                                     E,
                                                     H2)
                            ),
                            I2),
                    J2=..[t, B2|E2]
                )
            )
        ;   F2=V,
            maplist(extract_symbol, L, I2),
            sort([U1|C], C2),
            history_susps(C,
                          [W|U],
                          V,
                          U1,
                          E2),
            J2=..[t, E|E2]
        ),
        (   var(Z1)
        ->  novel_production_calls(I2,
                                   C2,
                                   E2,
                                   E,
                                   K2,
                                   L2),
            A2='$extend_history'(F2, K2),
            Z1=(K2=J2, L2)
        ;   true
        )
    ),
    (   chr_pp_flag(debugable, on)
    ->  D=rule(_, _, M, N),
        my_term_copy(M-N, Q1, M2-N2),
        get_occurrence(F/G, H, _, U1),
        sort_by_key([V, W|U], [U1|C], O2),
        P2='chr debug_event'(try([], O2, M2, N2)),
        Q2='chr debug_event'(apply([], O2, M2, N2))
    ;   P2=true,
        Q2=true
    ),
    (   is_stored_in_guard(F/G, E)
    ->  R2=W1,
        S2=true
    ;   R2=true,
        S2=W1
    ),
    T2=(F1:-D1, R1, P1, Z1, R2, S1, P2->Q2, A2, S2, T1, Y1;L1),
    add_location(T2, E, U2),
    J=[U2|K].
extract_symbol(A, B/C) :-
    functor(A, B, C).
novel_production_calls([], [], [], _, _, true).
novel_production_calls([A|B], [C|D], [E|F], G, H, (I, J)) :-
    get_occurrence_from_id(A, K, G, C),
    delay_phase_end(validate_store_type_assumptions,
                    novel_production_call(A,
                                          K,
                                          '$novel_production'(E, H),
                                          I)),
    novel_production_calls(B, D, F, G, H, J).
history_susps(A, B, C, D, E) :-
    reverse(B, F),
    sort_by_key([C|F], [D|A], E).
named_history_susps([], _, _, []).
named_history_susps([A|B], C, D, [E|F]) :-
    select2(A, E, C, D, G, H),
    !,
    named_history_susps(B, G, H, F).
gen_var_susp_list_for([A], B, C, D, E, F) :-
    !,
    functor(A, G, H),
    head_info(A, H, _, F, E, I),
    get_constraint_mode(G/H, J),
    head_arg_matches(I, J, [], _, C),
    extra_active_delegate_variables(A, B, C, K),
    append(E, K, D).
gen_var_susp_list_for([A|B], C, D, E, F, G) :-
    gen_var_susp_list_for(B,
                          [A|C],
                          H,
                          F,
                          _,
                          _),
    functor(A, I, J),
    gen_var(G),
    head_info(A, J, _, K, _, L),
    get_constraint_mode(I/J, M),
    head_arg_matches(L, M, H, _, D),
    passive_delegate_variables(A, B, C, D, N),
    append(N, [K, G|F], E).
gen_var_susp_list_for_b([A], B, C, [D], E, [F], []) :-
    !,
    functor(A, G, H),
    head_info(A, H, _, F, E, I),
    get_constraint_mode(G/H, J),
    head_arg_matches(I, J, [], _, C),
    extra_active_delegate_variables(A, B, C, K),
    append(E, K, D).
gen_var_susp_list_for_b([A|B], C, D, [E|F], G, [H|I], [J|K]) :-
    gen_var_susp_list_for_b(B,
                            [A|C],
                            L,
                            F,
                            G,
                            I,
                            K),
    functor(A, M, N),
    gen_var(J),
    head_info(A, N, _, H, _, O),
    get_constraint_mode(M/N, P),
    head_arg_matches(O, P, L, _, D),
    passive_delegate_variables(A, B, C, D, Q),
    append(Q, [H, J], E).
get_prop_inner_loop_vars([A], B, C, D, E, F, []) :-
    !,
    functor(A, G, H),
    head_info(A, H, F, E, I, J),
    get_constraint_mode(G/H, K),
    head_arg_matches(J, K, [], _, D),
    extra_active_delegate_variables(A, B, D, L),
    append(I, L, C).
get_prop_inner_loop_vars([A|B], C, D, E, F, G, [H|I]) :-
    get_prop_inner_loop_vars(B,
                             [A|C],
                             J,
                             K,
                             F,
                             G,
                             I),
    functor(A, L, M),
    gen_var(N),
    head_info(A, M, _, H, _, O),
    get_constraint_mode(L/M, P),
    head_arg_matches(O, P, K, _, E),
    passive_delegate_variables(A, B, C, E, Q),
    append(Q, [H, N|J], D).
reorder_heads(A, B, C, D, E, F) :-
    (   chr_pp_flag(reorder_heads, on),
        length(C, G),
        G=<6
    ->  reorder_heads_main(A, B, C, D, E, F)
    ;   E=C,
        F=D
    ).
reorder_heads_main(A, B, C, D, E, F) :-
    term_variables(B, G),
    H=entry([], [], G, C, D, A),
    copy_term_nat(H, I),
    a_star(I,
           J^(chr_translate:final_data(J)),
           K^L^M^(chr_translate:expand_data(K, L, M)),
           N),
    I=H,
    N=entry(O, P, _, _, _, _),
    reverse(O, E),
    reverse(P, F).
final_data(A) :-
    A=entry(_, _, _, _, [], _).
expand_data(A, B, C) :-
    A=entry(D, E, F, G, H, I),
    select2(J, K, G, H, L, M),
    term_variables([J|F], N),
    B=entry([J|D], [K|E], N, L, M, I),
    order_score(J, K, F, L, I, C).
order_score(A, B, C, D, E, F) :-
    functor(A, G, H),
    get_store_type(G/H, I),
    order_score(I, A, B, C, D, E, 99999, F).
order_score(default, A, _, B, C, _, D, E) :-
    term_variables(A, F),
    term_variables(C, G),
    ground_vars([A], H),
    list_difference_eq(F, H, I),
    order_score_vars(I, B, G, J),
    E is min(D, J).
order_score(multi_inthash(A), B, _, C, _, _, D, E) :-
    (   D=<100
    ->  E=D
    ;   order_score_indexes(A, B, C, E)
    ).
order_score(multi_hash(A), B, _, C, _, _, D, E) :-
    (   D=<100
    ->  E=D
    ;   order_score_indexes(A, B, C, E)
    ).
order_score(global_ground, A, _, B, C, _, D, E) :-
    term_variables(A, F),
    term_variables(C, G),
    order_score_vars(F, B, G, H),
    I is H*200,
    E is min(D, I).
order_score(var_assoc_store(_, _), _, _, _, _, _, _, 1).
order_score(global_singleton, _, _, _, _, _, _, A) :-
    A=1.
order_score(multi_store(A), B, C, D, E, F, G, H) :-
    multi_order_score(A,
                      B,
                      C,
                      D,
                      E,
                      F,
                      G,
                      H).
multi_order_score([], _, _, _, _, _, A, A).
multi_order_score([A|B], C, D, E, F, G, H, I) :-
    (   order_score(A,
                    C,
                    D,
                    E,
                    F,
                    G,
                    H,
                    J)
    ->  true
    ;   J=H
    ),
    multi_order_score(B,
                      C,
                      D,
                      E,
                      F,
                      G,
                      J,
                      I).
order_score(identifier_store(A), B, _, C, _, _, D, E) :-
    arg(A, B, F),
    memberchk_eq(F, C),
    E is min(D, 10).
order_score(type_indexed_identifier_store(A, _), B, _, C, _, _, D, E) :-
    arg(A, B, F),
    memberchk_eq(F, C),
    E is min(D, 10).
order_score_indexes(A, B, C, D) :-
    copy_term_nat(B+C, E+F),
    numbervars(F, 0, _),
    order_score_indexes(A, E, D).
order_score_indexes([A|B], C, D) :-
    args(A, C, E),
    (   maplist(ground, E)
    ->  D=100
    ;   order_score_indexes(B, C, D)
    ).
memberchk_eq_flip(A, B) :-
    memberchk_eq(B, A).
order_score_vars(A, B, C, D) :-
    order_score_count_vars(A, B, C, E-F-G),
    (   E-F-G==0-0-0
    ->  D=0
    ;   E>0
    ->  D is max(10-E, 0)
    ;   F>0
    ->  D is max(10-F, 1)*100
    ;   D is max(10-G, 1)*1000
    ).
order_score_count_vars([], _, _, 0-0-0).
order_score_count_vars([A|B], C, D, E-F-G) :-
    order_score_count_vars(B, C, D, H-I-J),
    (   memberchk_eq(A, C)
    ->  E is H+1,
        F=I,
        G=J
    ;   memberchk_eq(A, D)
    ->  F is I+1,
        E=H,
        G=J
    ;   G is J+1,
        E=H,
        F=I
    ).
create_get_mutable_ref(A, B, C) :-
    C=(B=mutable(A)).
create_get_mutable(A, B, C) :-
    B=mutable(A),
    C=true.
gen_var(_).
gen_vars(A, B) :-
    length(B, A).
ast_head_info1(A, B, C, D, E) :-
    A=chr_constraint(_/F, G, _),
    vars_susp(F, B, C, D),
    pairup(G, B, E).
head_info1(A, _/B, C, D, E, F) :-
    vars_susp(B, C, D, E),
    A=..[_|G],
    pairup(G, C, F).
head_info(A, B, C, D, E, F) :-
    vars_susp(B, C, D, E),
    A=..[_|G],
    pairup(G, C, F).
inc_id([A|B], [C|B]) :-
    C is A+1.
dec_id([A|B], [C|B]) :-
    C is A-1.
extend_id(A, [0|A]).
next_id([_, A|B], [C|B]) :-
    C is A+1.
build_head(A/B, C, D, E) :-
    build_head(A, B, C, D, E).
build_head(A, B, C, D, E) :-
    buildName(A, B, C, F),
    (   (   chr_pp_flag(debugable, on)
        ;   is_stored(A/B),
            (   has_active_occurrence(A/B)
            ;   chr_pp_flag(late_allocation, off)
            ),
            (   may_trigger(A/B)
            ;   get_allocation_occurrence(A/B, G),
                get_max_occurrence(A/B, H),
                H>=G
            )
        )
    ->  E=..[F|D]
    ;   init(D, I),
        E=..[F|I]
    ).
buildName(A, B, C, D) :-
    (   (   chr_pp_flag(debugable, on)
        ;   once((   is_stored(A/B),
                     (   has_active_occurrence(A/B)
                     ;   chr_pp_flag(late_allocation, off)
                     ),
                     (   may_trigger(A/B)
                     ;   get_allocation_occurrence(A/B, E),
                         get_max_occurrence(A/B, F),
                         F>=E
                     )
                 ;   C\=[0]
                 ))
        )
    ->  atom_concat(A, '___', G),
        atomic_concat(G, B, H),
        buildName_(C, H, D)
    ;   D=A
    ).
buildName_([], A, A).
buildName_([A|B], C, D) :-
    buildName_(B, C, E),
    atom_concat(E, '__', F),
    atomic_concat(F, A, D).
vars_susp(A, B, C, D) :-
    length(B, A),
    append(B, [C], D).
or_pattern(A, B) :-
    C is A-1,
    B is 1<<C.
and_pattern(A, B) :-
    C is A-1,
    D is 1<<C,
    B is -1*(D+1).
make_name(A, B/C, D) :-
    atom_concat_list([A, B, '___', C], D).
make_attr(A, B, C, D) :-
    length(C, A),
    D=..[v, B|C].
get_all_suspensions2(A, B, C) :-
    chr_pp_flag(dynattr, off),
    !,
    make_attr(A, _, C, B).
get_all_suspensions2(A, B, C, D) :-
    length(D, A),
    C='chr all_suspensions'(D, 1, B).
normalize_attr(A, B, C) :-
    B='chr normalize_attr'(A, C).
get_suspensions(A, B, C, C=D, E) :-
    chr_pp_flag(dynattr, off),
    !,
    make_attr(A, _, F, D),
    nth1(B, F, E).
get_suspensions(_, A, B, C, D) :-
    C=(memberchk(A-D, B)->true;D=[]).
singleton_attr(A, B, C, D) :-
    chr_pp_flag(dynattr, off),
    !,
    or_pattern(C, E),
    make_attr(A, E, F, D),
    nth1(C, F, [B], G),
    set_elems(G, []).
singleton_attr(_, A, B, C) :-
    C=[B-[A]].
add_attr(A, B, C, D, E, F) :-
    chr_pp_flag(dynattr, off),
    !,
    make_attr(A, G, H, I),
    or_pattern(C, J),
    nth1(C, H, K),
    substitute_eq(K, H, [B|K], L),
    make_attr(A, G, L, M),
    substitute_eq(K, H, [B], N),
    make_attr(A, O, N, P),
    E=(D=I, (G/\J=:=J->F=M;O is G\/J, F=P)),
    !.
add_attr(_, A, B, C, D, E) :-
    D=('chr select'(C, B-F, G)->E=[B-[A|F]|G];E=[B-[A]|C]).
rem_attr(A, B, C, D, E, F) :-
    chr_pp_flag(dynattr, off),
    chr_pp_flag(experiment, off),
    !,
    or_pattern(D, G),
    and_pattern(D, H),
    make_attr(A, I, J, K),
    nth1(D, J, L),
    substitute_eq(L, J, [], M),
    make_attr(A, N, M, O),
    substitute_eq(L, J, P, Q),
    make_attr(A, I, Q, R),
    get_target_module(S),
    F=(E=K, (I/\G=:=G->'chr sbag_del_element'(L, C, P), (P==[]->N is I/\H, (N==0->del_attr(B, S);put_attr(B, S, O));put_attr(B, S, R));true)),
    !.
rem_attr(_, A, B, C, D, E) :-
    chr_pp_flag(dynattr, off),
    chr_pp_flag(experiment, on),
    !,
    or_pattern(C, F),
    and_pattern(C, G),
    H is C+1,
    get_target_module(I),
    E=(arg(1, D, J), (J/\F=:=F->arg(H, D, K), 'chr sbag_del_element'(K, B, L), (L==[]->M is J/\G, (M==0->del_attr(A, I);setarg(1, D, M), setarg(H, D, L));setarg(H, D, L));true)),
    !.
rem_attr(_, A, B, C, D, E) :-
    get_target_module(F),
    E=('chr select'(D, C-G, H)->'chr sbag_del_element'(G, B, I), (I==[]->(H==[]->del_attr(A, F);put_attr(A, F, H));put_attr(A, F, [C-I|H]));true).
merge_attributes(A, B, C, D, E) :-
    chr_pp_flag(dynattr, off),
    !,
    make_attr(A, F, G, H),
    merge_attributes2(A, F, G, C, I, E),
    D=(B=H, I).
merge_attributes(_, A, B, C, D) :-
    C=(sort(A, E), sort(B, F), 'chr new_merge_attributes'(E, F, D)).
merge_attributes2(A, B, C, D, E, F) :-
    make_attr(A, G, H, I),
    bagof(J,
          K^L^M^N^(member2(C, H, K-L), J=(sort(L, M), 'chr merge_attributes'(K, M, N))),
          O),
    list2conj(O, P),
    bagof(Q,
          R^S^T^member((R, 'chr merge_attributes'(S, T, Q)), O),
          U),
    make_attr(A, V, U, F),
    E=(D=I, P, V is B\/G).
lookup_passive_head(A, B, C, D, E) :-
    functor(A, F, G),
    get_store_type(F/G, H),
    lookup_passive_head(H, A, B, C, [], D, E).
lookup_passive_head(A, B, C, D, E, F) :-
    functor(A, G, H),
    get_store_type(G/H, I),
    lookup_passive_head(I,
                        A,
                        B,
                        C,
                        D,
                        E,
                        F).
lookup_passive_head(default, A, B, C, _, D, E) :-
    functor(A, F, G),
    passive_head_via(A, B, C, D, E),
    update_store_type(F/G, default).
lookup_passive_head(multi_inthash(A), B, _, C, D, E, F) :-
    hash_lookup_passive_head(inthash,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             _).
lookup_passive_head(multi_hash(A), B, _, C, D, E, F) :-
    hash_lookup_passive_head(hash,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             _).
lookup_passive_head(global_ground, A, _, _, _, B, C) :-
    functor(A, D, E),
    global_ground_store_name(D/E, F),
    make_get_store_goal(F, C, B),
    update_store_type(D/E, global_ground).
lookup_passive_head(var_assoc_store(A, B), C, _, D, _, E, F) :-
    arg(A, C, G),
    arg(B, C, H),
    translate([G, H], D, [I, J]),
    get_target_module(K),
    E=(get_attr(I, K, L), lookup_assoc_store(L, J, F)).
lookup_passive_head(global_singleton, A, _, _, _, B, C) :-
    functor(A, D, E),
    global_singleton_store_name(D/E, F),
    make_get_store_goal(F, G, H),
    B=(H, G\==[], C=[G]),
    update_store_type(D/E, global_singleton).
lookup_passive_head(multi_store(A), B, C, D, E, F, G) :-
    once(( member(H, A),
           lookup_passive_head(H,
                               B,
                               C,
                               D,
                               E,
                               F,
                               G)
         )).
lookup_passive_head(identifier_store(A), B, _, C, _, D, E) :-
    functor(B, F, G),
    arg(A, B, H),
    translate([H], C, [I]),
    delay_phase_end(validate_store_type_assumptions,
                    identifier_lookup(F/G,
                                      A,
                                      E,
                                      I,
                                      D)),
    update_store_type(F/G, identifier_store(A)),
    get_identifier_index(F/G, A, _).
lookup_passive_head(type_indexed_identifier_store(A, B), C, _, D, _, E, F) :-
    functor(C, G, H),
    arg(A, C, I),
    (   var(I)
    ->  translate([I], D, [J]),
        E=K
    ;   ground(I),
        I='$chr_identifier_match'(L, _)
    ->  lookup_only_identifier_atom(B, L, J, M),
        E=(M, K)
    ),
    delay_phase_end(validate_store_type_assumptions,
                    type_indexed_identifier_lookup(G/H,
                                                   A,
                                                   B,
                                                   F,
                                                   J,
                                                   K)),
    update_store_type(G/H,
                      type_indexed_identifier_store(A, B)),
    get_type_indexed_identifier_index(B,
                                      G/H,
                                      A,
                                      _).
identifier_lookup(A, B, C, D, E) :-
    get_identifier_size(F),
    functor(G, struct, F),
    get_identifier_index(A, B, H),
    arg(H, G, C),
    E=(D=G).
type_indexed_identifier_lookup(A, B, C, D, E, F) :-
    type_indexed_identifier_structure(C, G),
    get_type_indexed_identifier_index(C, A, B, H),
    arg(H, G, D),
    F=(E=G).
hash_lookup_passive_head(A, B, C, D, E, F, G, H) :-
    pick_hash_index(B, C, D, H, I, J),
    (   J=[K]
    ->  true
    ;   K=..[k|J]
    ),
    functor(C, L, M),
    multi_hash_lookup_goal(L/M,
                           A,
                           H,
                           K,
                           G,
                           N),
    check_ground(E, I, O),
    my_term_copy(O, D, P),
    F=(P, N),
    (   A==inthash
    ->  update_store_type(L/M, multi_inthash([H]))
    ;   update_store_type(L/M, multi_hash([H]))
    ).
pick_hash_index(A, B, C, D, E, F) :-
    member(D, A),
    args(D, B, E),
    key_in_scope(E, C, F),
    !.
key_in_scope([], _, []).
key_in_scope([A|B], C, [D|E]) :-
    term_variables(A, F),
    translate(F, C, G),
    copy_term(A/F, D/G),
    key_in_scope(B, C, E).
existential_lookup(var_assoc_store(A, B), C, D, E, F, G, H, I, J, J) :-
    !,
    lookup_passive_head(var_assoc_store(A, B),
                        C,
                        D,
                        E,
                        F,
                        K,
                        L),
    sbag_member_call(I, L, M),
    functor(C, N, O),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(N/O, G), get_static_suspension_field(N/O, G, state, active, P))),
    H=(K, M, I=G, P).
existential_lookup(global_singleton, A, _, _, _, B, C, D, E, E) :-
    !,
    functor(A, F, G),
    global_singleton_store_name(F/G, H),
    make_get_store_goal(H, D, I),
    C=(I, D\==[], D=B),
    update_store_type(F/G, global_singleton).
existential_lookup(multi_store(A), B, C, D, E, F, G, H, I, J) :-
    !,
    once(( member(K, A),
           existential_lookup(K,
                              B,
                              C,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I,
                              J)
         )).
existential_lookup(multi_inthash(A), B, _, C, D, E, F, G, H, I) :-
    !,
    existential_hash_lookup(inthash,
                            A,
                            B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I).
existential_lookup(multi_hash(A), B, _, C, D, E, F, G, H, I) :-
    !,
    existential_hash_lookup(hash,
                            A,
                            B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I).
existential_lookup(identifier_store(A), B, C, D, E, F, G, H, I, J) :-
    !,
    lookup_passive_head(identifier_store(A),
                        B,
                        C,
                        D,
                        E,
                        K,
                        L),
    hash_index_filter(I, [A], J),
    functor(B, M, N),
    (   check_fd_lookup_condition(M, N, B, _)
    ->  O=(L=[H])
    ;   sbag_member_call(H, L, O)
    ),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(M/N, F), get_static_suspension_field(M/N, F, state, active, P))),
    G=(K, O, H=F, P).
existential_lookup(type_indexed_identifier_store(A, B), C, D, E, F, G, H, I, J, K) :-
    !,
    lookup_passive_head(type_indexed_identifier_store(A, B),
                        C,
                        D,
                        E,
                        F,
                        L,
                        M),
    hash_index_filter(J, [A], K),
    functor(C, N, O),
    (   check_fd_lookup_condition(N, O, C, _)
    ->  P=(M=[I])
    ;   sbag_member_call(I, M, P)
    ),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(N/O, G), get_static_suspension_field(N/O, G, state, active, Q))),
    H=(L, P, I=G, Q).
existential_lookup(A, B, C, D, E, F, G, H, I, I) :-
    lookup_passive_head(A,
                        B,
                        C,
                        D,
                        E,
                        J,
                        K),
    sbag_member_call(H, K, L),
    functor(B, M, N),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(M/N, F), get_static_suspension_field(M/N, F, state, active, O))),
    G=(J, L, H=F, O).
existential_hash_lookup(A, B, C, D, E, F, G, H, I, J) :-
    hash_lookup_passive_head(A,
                             B,
                             C,
                             D,
                             E,
                             K,
                             L,
                             M),
    hash_index_filter(I, M, J),
    functor(C, N, O),
    (   check_fd_lookup_condition(N, O, C, _)
    ->  P=(L=[H])
    ;   sbag_member_call(H, L, P)
    ),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(N/O, F), get_static_suspension_field(N/O, F, state, active, Q))),
    G=(K, P, H=F, Q).
hash_index_filter(A, B, C) :-
    hash_index_filter(A, B, 1, C).
hash_index_filter([], _, _, []).
hash_index_filter([A|B], C, D, E) :-
    (   C=[F|G]
    ->  H is D+1,
        (   F>D
        ->  E=[A|I],
            hash_index_filter(B, [F|G], H, I)
        ;   F==D
        ->  E=[silent(A)|I],
            hash_index_filter(B, G, H, I)
        )
    ;   E=[A|B]
    ).
assume_constraint_stores([]).
assume_constraint_stores([A|B]) :-
    (   chr_pp_flag(debugable, off),
        (   only_ground_indexed_arguments(A)
        ;   chr_pp_flag(mixed_stores, on)
        ),
        is_stored(A),
        get_store_type(A, default)
    ->  get_indexed_arguments(A, C),
        get_constraint_mode(A, D),
        aggregate_all(bag(E)-count,
                      ( member(E, C),
                        nth1(E, D, +)
                      ),
                      F-G),
        (   G>10
        ->  findall([E], member(E, F), H)
        ;   findall(E,
                    ( sublist(E, F),
                      E\==[]
                    ),
                    I),
            predsort(longer_list, I, H)
        ),
        (   get_functional_dependency(A, 1, J, K),
            all_distinct_var_args(J),
            K==[]
        ->  assumed_store_type(A, global_singleton)
        ;   (   only_ground_indexed_arguments(A)
            ;   G>0
            )
        ->  get_constraint_type_det(A, L),
            partition_indexes(H,
                              L,
                              M,
                              N,
                              O,
                              P),
            (   N=[]
            ->  Q=R
            ;   Q=[multi_inthash(N)|R]
            ),
            (   M=[]
            ->  R=S
            ;   R=[multi_hash(M)|S]
            ),
            (   O=[]
            ->  S=T
            ;   maplist(wrap_in_functor(identifier_store), O, U),
                append(U, T, S)
            ),
            append(P, V, T),
            (   only_ground_indexed_arguments(A)
            ->  V=[global_ground]
            ;   V=[default]
            ),
            assumed_store_type(A, multi_store(Q))
        ;   true
        )
    ;   true
    ),
    assume_constraint_stores(B).
partition_indexes([], _, [], [], [], []).
partition_indexes([A|B], C, D, E, F, G) :-
    (   A=[H],
        nth1(H, C, I),
        unalias_type(I, J),
        J==chr_identifier
    ->  F=[H|K],
        E=L,
        D=M,
        G=N
    ;   A=[H],
        nth1(H, C, I),
        unalias_type(I, J),
        nonvar(J),
        J=chr_identifier(O)
    ->  G=[type_indexed_identifier_store(H, O)|N],
        F=K,
        E=L,
        D=M
    ;   A=[H],
        nth1(H, C, I),
        unalias_type(I, J),
        J==dense_int
    ->  E=[A|L],
        D=M,
        F=K,
        G=N
    ;   member(H, A),
        nth1(H, C, I),
        unalias_type(I, J),
        nonvar(J),
        J=chr_identifier(_)
    ->  E=L,
        D=M,
        F=K,
        G=N
    ;   E=L,
        D=[A|M],
        F=K,
        G=N
    ),
    partition_indexes(B, C, M, L, K, N).
longer_list(A, B, C) :-
    length(B, D),
    length(C, E),
    compare(F, E, D),
    (   F==(=)
    ->  compare(A, B, C)
    ;   A=F
    ).
all_distinct_var_args(A) :-
    copy_term_nat(A, B),
    functor(A, C, D),
    functor(E, C, D),
    E=@=B.
get_indexed_arguments(A, B) :-
    A=_/C,
    get_indexed_arguments(1, C, A, B).
get_indexed_arguments(A, B, C, D) :-
    (   A>B
    ->  D=[]
    ;   (   is_indexed_argument(C, A)
        ->  D=[A|E]
        ;   D=E
        ),
        F is A+1,
        get_indexed_arguments(F, B, C, E)
    ).
validate_store_type_assumptions([]).
validate_store_type_assumptions([A|B]) :-
    validate_store_type_assumption(A),
    validate_store_type_assumptions(B).
universal_search_iterator_end([A|B], C, D, E/F, G, H, I, J) :-
    D=rule(_, _, K, L),
    gen_var_susp_list_for_b(B,
                            [A, C, K, L],
                            _,
                            M,
                            N,
                            _,
                            O),
    universal_search_iterator_failure_vars(B,
                                           H,
                                           M,
                                           N,
                                           O,
                                           P,
                                           Q),
    flatten(M, R),
    S=[[]|R],
    build_head(E, F, [G|H], S, T),
    (   Q=[_]
    ->  get_success_continuation_code_id(E/F, G, U),
        V=[U]
    ;   V=[G|Q]
    ),
    build_head(E, F, V, P, W),
    X=(T:-W),
    add_dummy_location(X, Y),
    I=[Y|J].
universal_search_iterator_failure_vars(A, B, C, D, E, F, G) :-
    (   B=[0|_]
    ->  next_id(B, G),
        F=D
    ;   C=[_|H],
        dec_id(B, I),
        (   A=[J|K],
            functor(J, L, M),
            check_fd_lookup_condition(L, M, J, K)
        ->  E=[_|N],
            universal_search_iterator_failure_vars(K,
                                                   I,
                                                   H,
                                                   D,
                                                   N,
                                                   F,
                                                   G)
        ;   G=I,
            flatten(H, O),
            E=[P|_],
            F=[P|O]
        )
    ).
universal_search_iterator([A|B], [C|D], E, F/G, H, I, J, K) :-
    E=rule(_, _, L, M),
    gen_var_susp_list_for_b(D,
                            [C, A, B, L, M],
                            N,
                            O,
                            P,
                            Q,
                            R),
    init(Q, S),
    flatten(O, T),
    gen_var(U),
    functor(C, V, W),
    gen_vars(W, X),
    head_info(C, W, X, Y, _, Z),
    get_constraint_mode(V/W, A1),
    head_arg_matches(Z, A1, N, B1, C1),
    delay_phase_end(validate_store_type_assumptions,
                    (static_suspension_term(V/W, D1), get_static_suspension_field(V/W, D1, state, active, E1), get_static_suspension_term_field(arguments, V/W, D1, X))),
    different_from_other_susps(C, Y, D, S, F1),
    G1=(Y=D1, E1, F1, B1),
    add_heads_ground_variables([C|D], [], H1),
    lookup_passive_head(A,
                        [C|D],
                        C1,
                        H1,
                        I1,
                        J1),
    inc_id(I, K1),
    L1=[[Y|U]|T],
    build_head(F, G, [H|I], L1, M1),
    passive_delegate_variables(C,
                               D,
                               [A, B, L, M],
                               C1,
                               N1),
    append([J1|N1], [Y, U|T], O1),
    build_head(F, G, [H|K1], O1, P1),
    (   check_fd_lookup_condition(V, W, C, D)
    ->  universal_search_iterator_failure_vars(D,
                                               I,
                                               O,
                                               P,
                                               R,
                                               Q1,
                                               R1),
        S1=Q1
    ;   S1=[U|T],
        R1=I
    ),
    (   R1=[_]
    ->  T1=R1
    ;   T1=[H|R1]
    ),
    build_head(F, G, T1, S1, U1),
    V1=(M1:-G1, I1->P1;U1),
    add_dummy_location(V1, W1),
    J=[W1|K].
ai_observation_analysis(A) :-
    (   chr_pp_flag(ai_observation_analysis, on),
        get_target_module(_),
        '$chr_compiled_with_version'(3)
    ->  list_to_ord_set(A, B),
        abstract_constraints(B),
        ai_observation_schedule_initial_calls(B, B),
        ai_observation_gather_results
    ;   true
    ).
ai_observation_schedule_initial_calls([], _).
ai_observation_schedule_initial_calls([A|B], C) :-
    ai_observation_schedule_initial_call(A, C),
    ai_observation_schedule_initial_calls(B, C).
ai_observation_schedule_initial_call(A, _) :-
    ai_observation_top(A, B),
    initial_call_pattern(B).
ai_observation_schedule_new_calls([], _).
ai_observation_schedule_new_calls([A|B], C) :-
    C=odom(_, D),
    initial_call_pattern(odom(A, D)),
    ai_observation_schedule_new_calls(B, C).
ai_observation_lub(odom(A, B), odom(A, C), odom(A, D)) :-
    ord_intersection(B, C, D).
ai_observation_bot(A, B, odom(A, B)).
ai_observation_top(A, odom(A, B)) :-
    ord_empty(B).
ai_observation_leq(odom(A, B), odom(A, C)) :-
    ord_subset(C, B).
ai_observation_observe_set(A, B, C) :-
    ord_subtract(A, B, C).
ai_observation_abstract_constraint(A, B, C) :-
    functor(A, D, E),
    C=D/E,
    memberchk(C, B).
ai_observation_abstract_constraints(A, B, C) :-
    findall(D,
            ( member(E, A),
              ai_observation_abstract_constraint(E, B, D)
            ),
            C).
ai_observation_abstract_goal_(A, B, C, D, E, F) :-
    term_variables((A, B, C), G),
    append(A, B, H),
    ground_vars(H, I),
    list_difference_eq(G, I, J),
    ai_observation_abstract_goal(D, E, F, [], J),
    !.
ground_vars([], []).
ground_vars([A|B], C) :-
    functor(A, D, E),
    get_constraint_mode(D/E, F),
    head_info(A, E, _, _, _, G),
    head_arg_matches(G, F, [], _, _, [], H),
    ground_vars(B, I),
    append(H, I, C).
ai_observation_abstract_goal((A, B), C, D, E, F) :-
    !,
    ai_observation_abstract_goal(A, C, D, G, F),
    ai_observation_abstract_goal(B, C, G, E, F).
ai_observation_abstract_goal((A;B), C, [(D;E)|F], F, G) :-
    !,
    ai_observation_abstract_goal(A, C, D, [], G),
    ai_observation_abstract_goal(B, C, E, [], G).
ai_observation_abstract_goal((A->B), C, D, E, F) :-
    !,
    ai_observation_abstract_goal(A, C, D, G, F),
    ai_observation_abstract_goal(B, C, G, E, F).
ai_observation_abstract_goal(A, B, [C|D], D, _) :-
    ai_observation_abstract_constraint(A, B, C),
    !.
ai_observation_abstract_goal(true, _, A, A, _) :-
    !.
ai_observation_abstract_goal(writeln(_), _, A, A, _) :-
    !.
ai_observation_abstract_goal(A, _, B, B, C) :-
    builtin_binds_b(A, D),
    intersect_eq(D, C, []),
    !.
ai_observation_abstract_goal(_, _, [A|B], B, _) :-
    A=builtin.
ai_observation_is_observed(odom(_, A), B) :-
    \+ ord_memberchk(B, A).
unconditional_occurrence(A, B) :-
    get_occurrence(A, B, C, _),
    get_rule(C, D),
    D=pragma(E, _, _, _, _),
    copy_term_nat(E, F),
    F=rule(G, H, I, _),
    guard_entailment:entails_guard([chr_pp_headvariables(G, H)], I),
    once((   G=[J],
             H==[]
         ;   H=[J],
             G==[],
             \+ may_trigger(A)
         )),
    all_distinct_var_args(J).
partial_wake_args([], _, _, _).
partial_wake_args([A|B], [C|D], E, F) :-
    (   C\==(+)
    ->  (   nonvar(A)
        ->  no_partial_wake(F)
        ;   memberchk_eq(A, E)
        ->  no_partial_wake(F)
        ;   true
        )
    ;   true
    ),
    partial_wake_args(B, D, E, F).
generate_show_constraint(A, B, C, D) :-
    (   chr_pp_flag(show, on)
    ->  B=['$show'/0|A],
        generate_show_rules(A, D, [E|C]),
        inc_rule_count(F),
        E=pragma(rule(['$show'], [], true, true), ids([0], []), [], no, F)
    ;   B=A,
        D=C
    ).
generate_show_rules([], A, A).
generate_show_rules([A/B|C], [D|E], F) :-
    functor(G, A, B),
    inc_rule_count(H),
    D=pragma(rule([], ['$show', G], true, writeln(G)), ids([], [0, 1]), [passive(1)], no, H),
    generate_show_rules(C, E, F).
static_suspension_term(A/B, C) :-
    suspension_term_base(A/B, D),
    E is D+B,
    functor(C, suspension, E).
has_suspension_field(A, B) :-
    suspension_term_base_fields(A, C),
    memberchk(B, C).
suspension_term_base(A, B) :-
    suspension_term_base_fields(A, C),
    length(C, B).
suspension_term_base_fields(A, B) :-
    (   chr_pp_flag(debugable, on)
    ->  B=[id, state, history, generation, continuation, functor]
    ;   (   uses_history(A)
        ->  B=[id, state, history|C]
        ;   only_ground_indexed_arguments(A),
            get_functional_dependency(A, 1, _, _)
        ->  B=[state|C]
        ;   B=[id, state|C]
        ),
        (   only_ground_indexed_arguments(A)
        ->  get_store_type(A, D),
            basic_store_types(D, E),
            (   memberchk(global_ground, E)
            ->  C=[global_list_prev|F]
            ;   C=F
            ),
            (   chr_pp_flag(ht_removal, on)
            ->  ht_prev_fields(E, F)
            ;   F=[]
            )
        ;   may_trigger(A)
        ->  (   uses_field(A, generation)
            ->  C=[generation, global_list_prev|F]
            ;   C=[global_list_prev|F]
            ),
            (   chr_pp_flag(mixed_stores, on),
                chr_pp_flag(ht_removal, on)
            ->  get_store_type(A, D),
                basic_store_types(D, E),
                ht_prev_fields(E, F)
            ;   F=[]
            )
        ;   C=[global_list_prev|F],
            (   chr_pp_flag(mixed_stores, on),
                chr_pp_flag(ht_removal, on)
            ->  get_store_type(A, D),
                basic_store_types(D, E),
                ht_prev_fields(E, F)
            ;   F=[]
            )
        )
    ).
ht_prev_fields(A, B) :-
    ht_prev_fields_int(A, C),
    append(C, B).
ht_prev_fields_int([], []).
ht_prev_fields_int([A|B], C) :-
    (   A=multi_hash(D)
    ->  maplist(ht_prev_field, D, E),
        C=[E|F]
    ;   C=F
    ),
    ht_prev_fields_int(B, F).
ht_prev_field(A, B) :-
    atomic_list_concat(['multi_hash_prev-'|A], B).
get_static_suspension_term_field(A, B, C, D) :-
    suspension_term_base_fields(B, E),
    nth1(F, E, A),
    !,
    arg(F, C, D).
get_static_suspension_term_field(arguments, A, B, C) :-
    !,
    suspension_term_base(A, D),
    B=..[_|E],
    drop(D, E, C).
get_static_suspension_term_field(A, B, _, _) :-
    chr_error(internal,
              'Trying to obtain field ~w of ~w, wich does not have it!',
              [A, B]).
get_dynamic_suspension_term_field(A, B, C, D, E) :-
    suspension_term_base_fields(B, F),
    nth1(G, F, A),
    !,
    E=arg(G, C, D).
get_dynamic_suspension_term_field(arguments, A, B, C, D) :-
    !,
    static_suspension_term(A, E),
    get_static_suspension_term_field(arguments, A, E, C),
    D=(B=E).
get_dynamic_suspension_term_field(argument(A), B, C, D, E) :-
    !,
    suspension_term_base(B, F),
    G is A+F,
    E=arg(G, C, D).
get_dynamic_suspension_term_field(A, B, _, _, _) :-
    chr_error(internal,
              'Dynamic goal to get ~w of ~w, which does not have this field!',
              [A, B]).
set_dynamic_suspension_term_field(A, B, C, D, E) :-
    suspension_term_base_fields(B, F),
    nth1(G, F, A),
    !,
    E=setarg(G, C, D).
set_dynamic_suspension_term_field(A, B, _, _, _) :-
    chr_error(internal,
              'Dynamic goal to set ~w of ~w, which does not have this field!',
              [A, B]).
basic_store_types(multi_store(A), A) :-
    !.
basic_store_types(A, [A]).
generate_never_stored_rules(A, B) :-
    (   chr_pp_flag(declare_stored_constraints, on)
    ->  never_stored_rules(A, B)
    ;   B=[]
    ).
check_storedness_assertions(A) :-
    (   chr_pp_flag(storage_analysis, on),
        chr_pp_flag(declare_stored_constraints, on)
    ->  forall(B, A, check_storedness_assertion(B))
    ;   true
    ).
continuation_analysis(A) :-
    maplist(analyse_continuations, A).
analyse_continuations(A) :-
    continuation_analysis(A, 1),
    get_max_occurrence(A, B),
    C is B+1,
    bulk_propagation(A, 1, C),
    set_occurrence_code_id(A, 1, 0).
continuation_analysis(A, B) :-
    get_max_occurrence(A, C),
    (   B>C
    ->  true
    ;   B==C
    ->  D is B+1,
        continuation_occurrence(A, B, D)
    ;   constraint_continuation(A, B, C, D),
        continuation_occurrence(A, B, D),
        E is B+1,
        continuation_analysis(A, E)
    ).
constraint_continuation(A, B, C, D) :-
    (   get_occurrence_head(A, B, E)
    ->  F is B+1,
        (   between(F, C, D),
            get_occurrence_head(A, D, G),
            unifiable(E, G, _)
        ->  true
        ;   D is C+1
        )
    ;   D=C
    ).
get_occurrence_head(A, B, C) :-
    get_occurrence(A, B, D, E),
    \+ is_passive(D, E),
    get_rule(D, F),
    F=pragma(rule(G, H, _, _), ids(I, J), _, _, _),
    (   select2(E, C, I, G, _, _)
    ->  true
    ;   select2(E, C, J, H, _, _)
    ).
get_success_continuation_code_id(A, B, C) :-
    get_success_continuation_occurrence(A, B, D),
    get_occurrence_code_id(A, D, C).
collect_constants(_, A, B, C) :-
    (   not_restarted,
        chr_pp_flag(experiment, on)
    ->  (   chr_pp_flag(sss, on)
        ->  D=[fep/4-[2, 3]-[[a, b]-fep1, [c, d]-fep2]-no],
            copy_term_nat(C, E),
            flatten_clauses(E, D, F),
            install_new_declarations_and_restart(F)
        ;   maplist(collect_rule_constants(B), A),
            (   chr_pp_flag(verbose, on)
            ->  print_chr_constants
            ;   true
            ),
            (   chr_pp_flag(experiment, on)
            ->  flattening_dictionary(B, D),
                copy_term_nat(C, E),
                flatten_clauses(E, D, F),
                install_new_declarations_and_restart(F)
            ;   true
            )
        )
    ;   true
    ).
collect_rule_constants(A, B) :-
    B=ast_rule(C, _, _, D, _),
    collect_head_constants(C),
    collect_body_constants(D, A).
collect_head_constants(simplification(A)) :-
    maplist(collect_constraint_constants, A).
collect_head_constants(propagation(A)) :-
    maplist(collect_constraint_constants, A).
collect_head_constants(simpagation(A, B)) :-
    maplist(collect_constraint_constants, A),
    maplist(collect_constraint_constants, B).
collect_body_constants(A, B) :-
    maplist(collect_goal_constants(B), A).
collect_goal_constants(A, B) :-
    (   ast_nonvar(B)
    ->  ast_symbol(B, C),
        (   memberchk(C, A)
        ->  ast_term_to_term(B, D),
            ast_args(B, E),
            collect_constraint_constants(chr_constraint(C,
                                                        E,
                                                        D))
        ;   C==(:)/2,
            ast_args(B, [F, G]),
            F=atomic(H),
            get_target_module(I),
            H==I,
            ast_nonvar(G),
            ast_symbol(G, J),
            memberchk(J, A)
        ->  ast_term_to_term(G, K),
            ast_args(G, L),
            collect_constraint_constants(chr_constraint(J,
                                                        L,
                                                        K))
        ;   true
        )
    ;   true
    ).
collect_constraint_constants(A) :-
    A=chr_constraint(B, C, _),
    get_constraint_type_det(B, D),
    collect_all_arg_constants(C, D, []).
collect_all_arg_constants([], [], A) :-
    (   A\==[]
    ->  add_chr_constants(A)
    ;   true
    ).
collect_all_arg_constants([A|B], [C|D], E) :-
    unalias_type(C, F),
    (   is_chr_constants_type(F, G, _)
    ->  (   ast_ground(A)
        ->  ast_term_to_term(A, H),
            collect_all_arg_constants(B,
                                      D,
                                      [G-H|E])
        ;   true
        )
    ;   collect_all_arg_constants(B, D, E)
    ).
add_chr_constants(A) :-
    keysort(A, B),
    add_chr_constants_(B).
flattening_dictionary([], []).
flattening_dictionary([A|B], C) :-
    (   flattening_dictionary_entry(A, D)
    ->  C=[D|E]
    ;   C=E
    ),
    flattening_dictionary(B, E).
flattening_dictionary_entry(A, B) :-
    get_constraint_type_det(A, C),
    constant_positions(C, 1, D, E, F, G),
    (   D\==[]
    ->  pairup(E, H, I),
        keysort(I, J),
        B=A-D-K-F,
        get_chr_constants(L),
        findall(M,
                ( member(J, L),
                  flat_spec(A, D, H, M)
                ),
                K)
    ;   G==yes
    ->  enum_positions(C, 1, N, O, P),
        B=A-N-K-P,
        findall(M,
                ( cartesian_product(Q, O),
                  flat_spec(A, N, Q, M)
                ),
                K)
    ).
constant_positions([], _, [], [], no, no).
constant_positions([A|B], C, D, E, F, G) :-
    unalias_type(A, H),
    (   is_chr_constants_type(H, I, J)
    ->  compose_error_handlers(J, K, F),
        D=[C|L],
        E=[I|M],
        G=N
    ;   (   is_chr_enum_type(H, _, _)
        ->  G=yes
        ;   G=N
        ),
        L=D,
        M=E,
        K=F
    ),
    O is C+1,
    constant_positions(B, O, L, M, K, N).
compose_error_handlers(no, A, A).
compose_error_handlers(yes(A), _, yes(A)).
enum_positions([], _, [], [], no).
enum_positions([A|B], C, D, E, F) :-
    unalias_type(A, G),
    (   is_chr_enum_type(G, H, I)
    ->  compose_error_handlers(I, J, F),
        D=[C|K],
        E=[H|L]
    ;   D=K,
        E=L,
        F=J
    ),
    M is C+1,
    enum_positions(B, M, K, L, J).
cartesian_product([], []).
cartesian_product([A|B], [C|D]) :-
    member(A, C),
    cartesian_product(B, D).
flat_spec(A/B, C, D, E) :-
    E=D-F,
    term_to_atom(D, G),
    term_to_atom(C, H),
    atom_concat_list(['$flat_', A, /, B, '___', H, '___', G],
                     F).
restart_after_flattening(A, A) :-
    nb_setval('$chr_restart_after_flattening', started).
restart_after_flattening(_, A) :-
    nb_getval('$chr_restart_after_flattening', restart(A)),
    nb_setval('$chr_restart_after_flattening', restarted).
not_restarted :-
    nb_getval('$chr_restart_after_flattening', started).
install_new_declarations_and_restart(A) :-
    nb_setval('$chr_restart_after_flattening', restart(A)),
    fail.
flatten_clauses(A, B, C) :-
    flatten_readcontent(A,
                        D,
                        E,
                        F,
                        _,
                        G,
                        H),
    flatten_clauses_(B, D, H, E, F, G, C).
flatten_clauses_(A, B, C, D, E, F, G) :-
    auxiliary_constraints_declarations(A, E, F, H),
    dispatching_rules(A, I),
    declarations(D, A, E, F, J),
    flatten_rules(B, A, K),
    append([C, H, I, J, K], G).
declarations(A, B, C, D, E) :-
    findall(F,
            ( member(F, A),
              \+ memberchk(F-_-_-_, B)
            ),
            G),
    maplist(declaration(C, D), G, H),
    flatten(H, E).
declaration(A, B, C, [(:-chr_constraint C), (:-chr_option(mode, D)), (:-chr_option(type_declaration, E))]) :-
    C=F/G,
    functor(D, F, G),
    (   memberchk(D, A)
    ->  true
    ;   replicate(G, ?, H),
        D=..[_|H]
    ),
    functor(E, F, G),
    (   memberchk(E, B)
    ->  true
    ;   replicate(G, any, I),
        E=..[_|I]
    ).
flatten_readcontent([], [], [], [], [], [], []).
flatten_readcontent([A|B], C, D, E, F, G, H) :-
    (   A==end_of_file
    ->  C=[],
        D=[],
        E=[],
        G=[],
        F=[],
        H=[]
    ;   crude_is_rule(A)
    ->  C=[A|I],
        flatten_readcontent(B,
                            I,
                            D,
                            E,
                            F,
                            G,
                            H)
    ;   pure_is_declaration(A, J, K, L)
    ->  append(J, M, D),
        append(K, N, E),
        append(L, O, G),
        flatten_readcontent(B,
                            C,
                            M,
                            N,
                            F,
                            O,
                            H)
    ;   is_mode_declaration(A, P)
    ->  E=[P|N],
        flatten_readcontent(B,
                            C,
                            D,
                            N,
                            F,
                            G,
                            H)
    ;   is_type_declaration(A, Q)
    ->  G=[Q|O],
        flatten_readcontent(B,
                            C,
                            D,
                            E,
                            F,
                            O,
                            H)
    ;   is_type_definition(A, R)
    ->  H=[A|S],
        F=[R|T],
        flatten_readcontent(B,
                            C,
                            D,
                            E,
                            T,
                            G,
                            S)
    ;   (   A=(:-op(U, V, W))
        ->  op(U, V, W)
        ;   true
        ),
        H=[A|S],
        flatten_readcontent(B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            S)
    ).
crude_is_rule(@(_, _)).
crude_is_rule(pragma(_, _)).
crude_is_rule((_==>_)).
crude_is_rule(<=>(_, _)).
pure_is_declaration(A, B, C, D) :-
    A=(:-E),
    E=..[F, G],
    F==(chr_constraint),
    conj2list(G, H),
    pure_extract_type_mode(H, B, C, D).
pure_extract_type_mode([], [], [], []).
pure_extract_type_mode([A/B|C], [A/B|D], E, F) :-
    !,
    pure_extract_type_mode(C, D, E, F).
pure_extract_type_mode([A|B], [C|D], [E|F], G) :-
    functor(A, H, I),
    C=H/I,
    A=..[_|J],
    extract_types_and_modes(J, K, L),
    E=..[H|L],
    (   forall(member(M, K), M==any)
    ->  G=N
    ;   G=[O|N],
        O=..[H|K]
    ),
    pure_extract_type_mode(B, D, F, N).
is_mode_declaration((:-chr_option(mode, A)), A).
is_type_declaration((:-chr_option(type_declaration, A)), A).
auxiliary_constraints_declarations(A, B, C, D) :-
    findall(E,
            auxiliary_constraints_declaration(A, B, C, E),
            F),
    flatten(F, D).
auxiliary_constraints_declaration(A, B, C, [(:-chr_constraint D), (:-chr_option(mode, E)), (:-chr_option(type_declaration, F))]) :-
    member(G/H-I-J-_, A),
    arg_modes(G, H, B, K),
    specialize_modes(K, I, L),
    arg_types(G, H, C, M),
    specialize_types(M, I, N),
    length(I, O),
    P is H-O,
    member(_-Q, J),
    D=Q/P,
    E=..[Q|L],
    F=..[Q|N].
arg_modes(A, B, C, D) :-
    functor(E, A, B),
    (   memberchk(E, C)
    ->  E=..[_|D]
    ;   replicate(B, ?, D)
    ).
specialize_modes(A, B, C) :-
    split_args(B, A, _, C).
arg_types(A, B, C, D) :-
    functor(E, A, B),
    (   memberchk(E, C)
    ->  E=..[_|D]
    ;   replicate(B, any, D)
    ).
specialize_types(A, B, C) :-
    split_args(B, A, _, C).
dispatching_rules([], []).
dispatching_rules([A-B-C-D|E], F) :-
    constraint_dispatching_rule(C,
                                A,
                                B,
                                D,
                                F,
                                G),
    dispatching_rules(E, G).
constraint_dispatching_rule(A, B/C, D, E, F, G) :-
    (   increasing_numbers(D, 1)
    ->  H=F,
        I=B/C
    ;   functor(J, B, C),
        J=..[_|K],
        split_args(D, K, L, M),
        append(L, M, N),
        atom_concat(B, '_$shuffled', O),
        P=..[O|N],
        [(J:-P)|H]=F,
        I=O/C
    ),
    Q=swap(B, D),
    dispatching_rule_term_cases(A,
                                D,
                                I,
                                E,
                                Q,
                                H,
                                G).
increasing_numbers([], _).
increasing_numbers([A|B], A) :-
    C is A+1,
    increasing_numbers(B, C).
dispatching_rule_term_cases(A, B, C/D, E, F, G, H) :-
    length(B, I),
    once(pairup(J, K, A)),
    maplist(head_tail, J, L, M),
    N is D-I,
    maplist(wrap_in_functor(dispatching_action), K, O),
    dispatch_trie_index(L,
                        M,
                        N,
                        E,
                        F,
                        O,
                        C,
                        G,
                        H).
dispatching_action(A, B, C) :-
    C=..[A|B].
dispatch_trie_index(A, B, C, D, E, F, G, H, I) :-
    dispatch_trie_step(A,
                       G,
                       G,
                       B,
                       C,
                       D,
                       E,
                       F,
                       H,
                       I).
dispatch_trie_step([], _, _, _, [], _, _, [], A, A) :-
    !.
dispatch_trie_step(A, B, C, D, E, F, G, H, I, J) :-
    D=[K|_],
    length(K, L),
    aggregate_all(set(M/N),
                  ( member(O, A),
                    functor(O, M, N)
                  ),
                  P),
    Q is L+1,
    dispatch_trie_step_cases(P,
                             Q,
                             A,
                             D,
                             E,
                             F,
                             G,
                             H,
                             B,
                             C,
                             I,
                             J).
dispatch_trie_step_cases([], A, _, _, B, C, D, _, E, _, F, G) :-
    (   C=yes(H)
    ->  F=[I|G],
        I=(J:-K),
        L is A+B,
        functor(J, E, L),
        reconstruct_original_term(D, J, M),
        K=..[H, M]
    ;   F=G
    ).
dispatch_trie_step_cases([A|B], C, D, E, F, G, H, I, J, K, L, M) :-
    dispatch_trie_step_case(A,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I,
                            J,
                            K,
                            L,
                            N),
    dispatch_trie_step_cases(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J,
                             K,
                             N,
                             M).
dispatch_trie_step_case(A/B, C, D, E, F, G, H, I, J, K, [L|M], N) :-
    L=(O:-P, Q),
    (   G=yes(_)
    ->  P=!
    ;   P=true
    ),
    R is C+F,
    functor(O, J, R),
    arg(1, O, S),
    O=..[_, _|T],
    length(U, F),
    once(append(V, U, T)),
    functor(S, A, B),
    W=index_functor(A, B, H),
    S=..[_|X],
    append(X, T, Y),
    (   Y==U
    ->  M=N,
        rec_cases(D,
                  _,
                  I,
                  A/B,
                  _,
                  _,
                  Z),
        Z=[A1],
        call(A1, U, Q)
    ;   rec_cases(D,
                  E,
                  I,
                  A/B,
                  B1,
                  C1,
                  Z),
        (   Z=[D1]
        ->  C1=[E1],
            append([B1, E1, U], Y),
            M=N,
            call(D1, U, Q)
        ;   pairup(B1, C1, F1),
            common_pattern(F1, G1, H1, I1),
            append(X, V, [J1|K1]),
            J1-K1=G1,
            L1=gct([J1|K1], W),
            fresh_symbol(K, M1),
            append(H1, U, N1),
            Q=..[M1|N1],
            findall(O1-P1,
                    member([O1|P1], I1),
                    Q1),
            once(pairup(R1, S1, Q1)),
            dispatch_trie_step(R1,
                               M1,
                               K,
                               S1,
                               F,
                               G,
                               L1,
                               Z,
                               M,
                               N)
        )
    ).
split([A|B], C, D, E, F) :-
    (   C==1
    ->  D=[],
        E=A,
        F=B
    ;   G is C-1,
        D=[A|H],
        split(B, G, H, E, F)
    ).
reconstruct_original_term(swap(A, B), C, D) :-
    functor(C, _, E),
    functor(D, A, E),
    D=..[_|F],
    split_args(B, F, G, H),
    C=..[_|I],
    append(G, H, I).
reconstruct_original_term(index_functor(A, B, C), D, E) :-
    D=..[F|G],
    split_at(B, G, H, I),
    J=..[A|H],
    K=..[F, J|I],
    reconstruct_original_term(C, K, E).
reconstruct_original_term(gct(A, B), C, D) :-
    copy_term_nat(A, E),
    term_variables(E, F),
    C=..[G|H],
    append(F, I, H),
    append(E, I, J),
    K=..[G|J],
    reconstruct_original_term(B, K, D).
flatten_rules(A, B, C) :-
    flatten_rules1(A, B, D),
    flatten(D, C).
flatten_rules1([], _, []).
flatten_rules1([A|B], C, [D|E]) :-
    findall(F, flatten_rule(A, C, F), D),
    flatten_rules1(B, C, E).
flatten_rule(@(A, B), C, @(A, D)) :-
    !,
    flatten_rule(B, C, D).
flatten_rule(pragma(A, B), C, pragma(D, B)) :-
    !,
    flatten_rule(A, C, D).
flatten_rule((A==>B), C, (D==>E)) :-
    !,
    flatten_heads(A, C, D),
    flatten_body(B, C, E).
flatten_rule(<=>(\(A, B), C), D, <=>(\(E, F), G)) :-
    !,
    flatten_heads((A, B), D, (E, F)),
    flatten_body(C, D, G).
flatten_rule(<=>(A, B), C, <=>(D, E)) :-
    flatten_heads(A, C, D),
    flatten_body(B, C, E).
flatten_heads((A, B), C, (D, E)) :-
    !,
    flatten_heads(A, C, D),
    flatten_heads(B, C, E).
flatten_heads(#(A, B), C, #(D, B)) :-
    !,
    flatten_heads(A, C, D).
flatten_heads(A, B, C) :-
    (   functor(A, D, E),
        memberchk(D/E-F-G-_, B)
    ->  A=..[_|H],
        split_args(F, H, I, J),
        member(I-K, G),
        C=..[K|J]
    ;   C=A
    ).
flatten_body((A| B), C, (D| E)) :-
    !,
    conj2list(A, F),
    maplist(flatten_goal(C), F, G),
    list2conj(G, D),
    conj2list(B, H),
    maplist(flatten_goal(C), H, I),
    list2conj(I, E).
flatten_body(A, B, C) :-
    conj2list(A, D),
    maplist(flatten_goal(B), D, E),
    list2conj(E, C).
flatten_goal(_, A, B) :-
    var(A),
    !,
    B=A.
flatten_goal(A, B, C) :-
    (   is_specializable_goal(B, A, D)
    ->  specialize_goal(B, D, C)
    ;   B=E:F,
        get_target_module(G),
        E==G,
        nonvar(F),
        is_specializable_goal(F, A, D)
    ->  specialize_goal(F, D, H),
        C=E:H
    ;   partial_eval(B, C)
    ->  true
    ;   C=B
    ).
is_specializable_goal(A, B, C) :-
    functor(A, D, E),
    memberchk(D/E-C-_-_, B),
    args(C, A, F),
    ground(F).
specialize_goal(A, B, C) :-
    functor(A, D, E),
    A=..[_|F],
    split_args(B, F, G, H),
    flat_spec(D/E, B, G, _-I),
    C=..[I|H].
partial_eval(append(A, B, C), D) :-
    (   A==[]
    ->  D=(C=B)
    ;   B==[]
    ->  D=(C=A)
    ).
partial_eval(flatten_path(A, B), C) :-
    (   nonvar(A),
        flatten(A, D),
        D\==A
    ->  C=flatten_path(D, B)
    ).
dump_code(A) :-
    (   chr_pp_flag(dump, on)
    ->  maplist(portray_clause, A)
    ;   true
    ).
chr_banner :-
    chr_info(banner, '\tThe K.U.Leuven CHR System\n\t\tMain Developer:\tTom Schrijvers\n\t\tContributors:\tJon Sneyers, Bart Demoen, Jan Wielemaker\n\t\tCopyright:\tK.U.Leuven, Belgium\n\t\tURL:\t\thttp://www.cs.kuleuven.be/~~toms/CHR/\n', []).
chr_none_locked(A, B) :-
    chr_pp_flag(guard_locks, C),
    (   C==off
    ->  B=true
    ;   C==on
    ->  B='chr none_locked'(A)
    ;   C==error
    ->  B='chr none_error_locked'(A)
    ).
chr_not_locked(A, B) :-
    chr_pp_flag(guard_locks, C),
    (   C==off
    ->  B=true
    ;   C==on
    ->  B='chr not_locked'(A)
    ;   C==error
    ->  B='chr not_error_locked'(A)
    ).
chr_lock(A, B) :-
    chr_pp_flag(guard_locks, C),
    (   C==off
    ->  B=true
    ;   C==on
    ->  B='chr lock'(A)
    ;   C==error
    ->  B='chr error_lock'(A)
    ).
chr_unlock(A, B) :-
    chr_pp_flag(guard_locks, C),
    (   C==off
    ->  B=true
    ;   C==on
    ->  B='chr unlock'(A)
    ;   C==error
    ->  B='chr unerror_lock'(A)
    ).
term_to_ast_term(A, B, C, D) :-
    (   atomic(A)
    ->  B=atomic(A),
        D=C
    ;   compound(A)
    ->  functor(A, E, F),
        B=compound(E, F, G, A),
        A=..[_|H],
        maplist_dcg(chr_translate:term_to_ast_term,
                    H,
                    G,
                    C,
                    D)
    ;   var(A)
    ->  var_to_ast_term(A, C, B, D)
    ).
var_to_ast_term(A, B, C, D) :-
    B=E-F,
    (   lookup_eq(E, A, C)
    ->  D=B
    ;   C=var(F, A),
        G is F+1,
        H=[A-C|E],
        D=H-G
    ).
chr_constraint_to_ast_constraint(A, B, C, D) :-
    B=chr_constraint(E/F, G, A),
    functor(A, E, F),
    A=..[_|H],
    maplist_dcg(chr_translate:term_to_ast_term,
                H,
                G,
                C,
                D).
rule_to_ast_rule(A, B) :-
    B=ast_rule(C, D, E, F, G),
    A=rule(H, I, E, G),
    J=[]-1,
    (   H==[]
    ->  C=propagation(K),
        maplist_dcg(chr_translate:chr_constraint_to_ast_constraint,
                    I,
                    K,
                    J,
                    L)
    ;   I==[]
    ->  C=simplification(K),
        maplist_dcg(chr_translate:chr_constraint_to_ast_constraint,
                    H,
                    K,
                    J,
                    L)
    ;   C=simpagation(M, N),
        maplist_dcg(chr_translate:chr_constraint_to_ast_constraint,
                    H,
                    M,
                    J,
                    O),
        maplist_dcg(chr_translate:chr_constraint_to_ast_constraint,
                    I,
                    N,
                    O,
                    L)
    ),
    conj2list(E, P),
    maplist_dcg(chr_translate:term_to_ast_term,
                P,
                D,
                L,
                Q),
    conj2list(G, R),
    maplist_dcg(chr_translate:term_to_ast_term,
                R,
                F,
                Q,
                _).
pragma_rule_to_ast_rule(pragma(A, _, _, _, _), B) :-
    rule_to_ast_rule(A, B).
check_rule_to_ast_rule(A) :-
    (   rule_to_ast_rule(A, B)
    ->  writeln(B)
    ;   writeln(failed(rule_to_ast_rule(A, B)))
    ).
ast_term_to_term(var(_, A), A).
ast_term_to_term(atomic(A), A).
ast_term_to_term(compound(_, _, _, A), A).
ast_nonvar(atomic(_)).
ast_nonvar(compound(_, _, _, _)).
ast_ground(atomic(_)).
ast_ground(compound(_, _, A, _)) :-
    maplist(ast_ground, A).
ast_is_ground(A, B) :-
    ast_is_ground_(B, A).
ast_is_ground_(var(A, _), B) :-
    tree_set_memberchk(A, B).
ast_is_ground_(atomic(_), _).
ast_is_ground_(compound(_, _, A, _), B) :-
    maplist(ast_is_ground(B), A).
ast_functor(atomic(A), A, 0).
ast_functor(compound(A, B, _, _), A, B).
ast_symbol(atomic(A), A/0).
ast_symbol(compound(A, B, _, _), A/B).
ast_args(atomic(_), []).
ast_args(compound(_, _, A, _), A).
ast_term_variables(atomic(_), A, A).
ast_term_variables(compound(_, _, A, _), B, C) :-
    ast_term_list_variables(A, B, C).
ast_term_variables(var(A, _), B, C) :-
    tree_set_add(B, A, C).
ast_term_list_variables(A, B, C) :-
    fold(A, chr_translate:ast_term_variables, B, C).
ast_constraint_variables(chr_constraint(_, A, _), B, C) :-
    ast_term_list_variables(A, B, C).
ast_constraint_list_variables(A, B, C) :-
    fold(A, chr_translate:ast_constraint_variables, B, C).
ast_head_variables(simplification(A), B, C) :-
    ast_constraint_list_variables(A, B, C).
ast_head_variables(propagation(A), B, C) :-
    ast_constraint_list_variables(A, B, C).
ast_head_variables(simpagation(A, B), C, D) :-
    ast_constraint_list_variables(A, C, E),
    ast_constraint_list_variables(B, E, D).
ast_var_memberchk(var(A, _), B) :-
    tree_set_memberchk(A, B).
ast_instantiate(A, B, C) :-
    ast_instantiate_(B, A, C).
ast_instantiate_(var(A, _), B, C) :-
    get_assoc(A, B, C).
ast_instantiate_(atomic(A), _, A).
ast_instantiate_(compound(A, B, C, _), D, E) :-
    functor(E, A, B),
    E=..[_|F],
    maplist(ast_instantiate(D), C, F).
ast_head_arg_matches_([], [], A, B, [], A, B).
ast_head_arg_matches_([silent(A-_)|B], [C|D], E, F, G, H, I) :-
    !,
    (   C==(+)
    ->  ast_term_variables(A, J, F),
        ast_head_arg_matches_(B,
                              D,
                              E,
                              J,
                              G,
                              H,
                              I)
    ;   ast_head_arg_matches_(B,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I)
    ).
ast_head_arg_matches_([A-B|C], [D|E], F, G, H, I, J) :-
    (   A=var(K, _)
    ->  (   get_assoc(K, F, L)
        ->  (   D=(+)
            ->  (   tree_set_memberchk(K, G)
                ->  H=[B=L|M],
                    N=G
                ;   H=[B==L|M],
                    tree_set_add(G, K, N)
                )
            ;   H=[B==L|M],
                N=G
            ),
            O=F
        ;   put_assoc(K, F, B, O),
            H=M,
            (   D=(+)
            ->  tree_set_add(G, K, N)
            ;   N=G
            )
        ),
        P=C,
        Q=E
    ;   ground(A),
        A='$chr_identifier_match'(R, S)
    ->  identifier_label_atom(S, B, R, T),
        H=[T|M],
        F=O,
        N=G,
        P=C,
        Q=E
    ;   A=atomic(U)
    ->  (   D=(+)
        ->  H=[B=U|M]
        ;   H=[B==U|M]
        ),
        F=O,
        N=G,
        P=C,
        Q=E
    ;   D==(+),
        ast_is_ground(G, A)
    ->  ast_instantiate(F, A, V),
        H=[B=V|M],
        F=O,
        N=G,
        P=C,
        Q=E
    ;   D==(?),
        ast_is_ground(G, A)
    ->  ast_instantiate(F, A, V),
        H=[B==V|M],
        F=O,
        N=G,
        P=C,
        Q=E
    ;   A=compound(W, X, Y, _),
        functor(Z, W, X),
        Z=..[_|A1],
        (   D=(+)
        ->  H=[B=Z|M]
        ;   H=[nonvar(B), B=Z|M]
        ),
        pairup(Y, A1, B1),
        append(B1, C, P),
        replicate(_, D, C1),
        append(C1, E, Q),
        O=F,
        N=G
    ),
    ast_head_arg_matches_(P,
                          Q,
                          O,
                          N,
                          M,
                          I,
                          J).
:- use_module(chr(chr_runtime)).
:- use_module(chr(chr_hashtable_store)).
attach_generate_empty_named_history_initialisation___1([], _).
attach_generate_empty_named_history_initialisation___1([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\1=:=1
        ->  P=v(E, [C|F], G, H, I, J, K, L, M, N, O)
        ;   Q is E\/1,
            P=v(Q, [C], G, H, I, J, K, L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(1, [C], [], [], [], [], [], [], [], [], []))
    ),
    attach_generate_empty_named_history_initialisation___1(B, C).
detach_generate_empty_named_history_initialisation___1([], _).
detach_generate_empty_named_history_initialisation___1([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\1=:=1
        ->  'chr sbag_del_element'(F, C, P),
            (   P==[]
            ->  Q is E/\ -2,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               [],
                               G,
                               H,
                               I,
                               J,
                               K,
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           P,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_generate_empty_named_history_initialisation___1(B, C).
attach_symbol_count___2([], _).
attach_symbol_count___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\2=:=2
        ->  P=v(E, F, [C|G], H, I, J, K, L, M, N, O)
        ;   Q is E\/2,
            P=v(Q, F, [C], H, I, J, K, L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(2, [], [C], [], [], [], [], [], [], [], []))
    ),
    attach_symbol_count___2(B, C).
detach_symbol_count___2([], _).
detach_symbol_count___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\2=:=2
        ->  'chr sbag_del_element'(G, C, P),
            (   P==[]
            ->  Q is E/\ -3,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               [],
                               H,
                               I,
                               J,
                               K,
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           P,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_symbol_count___2(B, C).
attach_fresh_symbol___2([], _).
attach_fresh_symbol___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\4=:=4
        ->  P=v(E, F, G, [C|H], I, J, K, L, M, N, O)
        ;   Q is E\/4,
            P=v(Q, F, G, [C], I, J, K, L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(4, [], [], [C], [], [], [], [], [], [], []))
    ),
    attach_fresh_symbol___2(B, C).
detach_fresh_symbol___2([], _).
detach_fresh_symbol___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\4=:=4
        ->  'chr sbag_del_element'(H, C, P),
            (   P==[]
            ->  Q is E/\ -5,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               [],
                               I,
                               J,
                               K,
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           P,
                           I,
                           J,
                           K,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_fresh_symbol___2(B, C).
attach_background_info___2([], _).
attach_background_info___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\8=:=8
        ->  P=v(E, F, G, H, [C|I], J, K, L, M, N, O)
        ;   Q is E\/8,
            P=v(Q, F, G, H, [C], J, K, L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(8, [], [], [], [C], [], [], [], [], [], []))
    ),
    attach_background_info___2(B, C).
detach_background_info___2([], _).
detach_background_info___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\8=:=8
        ->  'chr sbag_del_element'(I, C, P),
            (   P==[]
            ->  Q is E/\ -9,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               [],
                               J,
                               K,
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           P,
                           J,
                           K,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_background_info___2(B, C).
attach_get_bg_info___2([], _).
attach_get_bg_info___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\16=:=16
        ->  P=v(E, F, G, H, I, [C|J], K, L, M, N, O)
        ;   Q is E\/16,
            P=v(Q, F, G, H, I, [C], K, L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(16, [], [], [], [], [C], [], [], [], [], []))
    ),
    attach_get_bg_info___2(B, C).
detach_get_bg_info___2([], _).
detach_get_bg_info___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\16=:=16
        ->  'chr sbag_del_element'(J, C, P),
            (   P==[]
            ->  Q is E/\ -17,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               [],
                               K,
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           P,
                           K,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_get_bg_info___2(B, C).
attach_type_definition___2([], _).
attach_type_definition___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\32=:=32
        ->  P=v(E, F, G, H, I, J, [C|K], L, M, N, O)
        ;   Q is E\/32,
            P=v(Q, F, G, H, I, J, [C], L, M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(32, [], [], [], [], [], [C], [], [], [], []))
    ),
    attach_type_definition___2(B, C).
detach_type_definition___2([], _).
detach_type_definition___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\32=:=32
        ->  'chr sbag_del_element'(K, C, P),
            (   P==[]
            ->  Q is E/\ -33,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               J,
                               [],
                               L,
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           P,
                           L,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_type_definition___2(B, C).
attach_type_alias___2([], _).
attach_type_alias___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\64=:=64
        ->  P=v(E, F, G, H, I, J, K, [C|L], M, N, O)
        ;   Q is E\/64,
            P=v(Q, F, G, H, I, J, K, [C], M, N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(64, [], [], [], [], [], [], [C], [], [], []))
    ),
    attach_type_alias___2(B, C).
detach_type_alias___2([], _).
detach_type_alias___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\64=:=64
        ->  'chr sbag_del_element'(L, C, P),
            (   P==[]
            ->  Q is E/\ -65,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               J,
                               K,
                               [],
                               M,
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           P,
                           M,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_type_alias___2(B, C).
attach_unalias_type___2([], _).
attach_unalias_type___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\128=:=128
        ->  P=v(E, F, G, H, I, J, K, L, [C|M], N, O)
        ;   Q is E\/128,
            P=v(Q, F, G, H, I, J, K, L, [C], N, O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(128, [], [], [], [], [], [], [], [C], [], []))
    ),
    attach_unalias_type___2(B, C).
detach_unalias_type___2([], _).
detach_unalias_type___2([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\128=:=128
        ->  'chr sbag_del_element'(M, C, P),
            (   P==[]
            ->  Q is E/\ -129,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               J,
                               K,
                               L,
                               [],
                               N,
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           P,
                           N,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_unalias_type___2(B, C).
attach_ast_static_type_check_var___4([], _).
attach_ast_static_type_check_var___4([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\256=:=256
        ->  P=v(E, F, G, H, I, J, K, L, M, [C|N], O)
        ;   Q is E\/256,
            P=v(Q, F, G, H, I, J, K, L, M, [C], O)
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(256, [], [], [], [], [], [], [], [], [C], []))
    ),
    attach_ast_static_type_check_var___4(B, C).
detach_ast_static_type_check_var___4([], _).
detach_ast_static_type_check_var___4([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\256=:=256
        ->  'chr sbag_del_element'(N, C, P),
            (   P==[]
            ->  Q is E/\ -257,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               J,
                               K,
                               L,
                               M,
                               [],
                               O))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M,
                           P,
                           O))
            )
        ;   true
        )
    ;   true
    ),
    detach_ast_static_type_check_var___4(B, C).
attach_atomic_type___1([], _).
attach_atomic_type___1([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\512=:=512
        ->  P=v(E, F, G, H, I, J, K, L, M, N, [C|O])
        ;   Q is E\/512,
            P=v(Q, F, G, H, I, J, K, L, M, N, [C])
        ),
        put_attr(A, chr_translate, P)
    ;   put_attr(A,
                 chr_translate,
                 v(512, [], [], [], [], [], [], [], [], [], [C]))
    ),
    attach_atomic_type___1(B, C).
detach_atomic_type___1([], _).
detach_atomic_type___1([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  D=v(E, F, G, H, I, J, K, L, M, N, O),
        (   E/\512=:=512
        ->  'chr sbag_del_element'(O, C, P),
            (   P==[]
            ->  Q is E/\ -513,
                (   Q==0
                ->  del_attr(A, chr_translate)
                ;   put_attr(A,
                             chr_translate,
                             v(Q,
                               F,
                               G,
                               H,
                               I,
                               J,
                               K,
                               L,
                               M,
                               N,
                               []))
                )
            ;   put_attr(A,
                         chr_translate,
                         v(E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M,
                           N,
                           P))
            )
        ;   true
        )
    ;   true
    ),
    detach_atomic_type___1(B, C).
attach_increment([], _).
attach_increment([A|B], C) :-
    (   get_attr(A, chr_translate, D)
    ->  C=v(E, F, G, H, I, J, K, L, M, N, O),
        D=v(P, Q, R, S, T, U, V, W, X, Y, Z),
        ( sort(Q, A1),
          'chr merge_attributes'(F, A1, B1),
          sort(R, C1),
          'chr merge_attributes'(G, C1, D1),
          sort(S, E1),
          'chr merge_attributes'(H, E1, F1),
          sort(T, G1),
          'chr merge_attributes'(I, G1, H1),
          sort(U, I1),
          'chr merge_attributes'(J, I1, J1),
          sort(V, K1),
          'chr merge_attributes'(K, K1, L1),
          sort(W, M1),
          'chr merge_attributes'(L, M1, N1),
          sort(X, O1),
          'chr merge_attributes'(M, O1, P1),
          sort(Y, Q1),
          'chr merge_attributes'(N, Q1, R1),
          sort(Z, S1),
          'chr merge_attributes'(O, S1, T1)
        ),
        U1 is E\/P,
        put_attr(A,
                 chr_translate,
                 v(U1,
                   B1,
                   D1,
                   F1,
                   H1,
                   J1,
                   L1,
                   N1,
                   P1,
                   R1,
                   T1))
    ;   put_attr(A, chr_translate, C)
    ),
    attach_increment(B, C).
attribute_goals(_, A, A).
attr_unify_hook(v(A, B, C, D, E, F, G, H, I, J, K), L) :-
    sort(B, M),
    sort(C, N),
    sort(D, O),
    sort(E, P),
    sort(F, Q),
    sort(G, R),
    sort(H, S),
    sort(I, T),
    sort(J, U),
    sort(K, V),
    (   var(L)
    ->  (   get_attr(L, chr_translate, W)
        ->  W=v(X, Y, Z, A1, B1, C1, D1, E1, F1, G1, H1),
            ( sort(Y, I1),
              'chr merge_attributes'(M, I1, J1),
              sort(Z, K1),
              'chr merge_attributes'(N, K1, L1),
              sort(A1, M1),
              'chr merge_attributes'(O, M1, N1),
              sort(B1, O1),
              'chr merge_attributes'(P, O1, P1),
              sort(C1, Q1),
              'chr merge_attributes'(Q, Q1, R1),
              sort(D1, S1),
              'chr merge_attributes'(R, S1, T1),
              sort(E1, U1),
              'chr merge_attributes'(S, U1, V1),
              sort(F1, W1),
              'chr merge_attributes'(T, W1, X1),
              sort(G1, Y1),
              'chr merge_attributes'(U, Y1, Z1),
              sort(H1, A2),
              'chr merge_attributes'(V, A2, B2)
            ),
            C2 is A\/X,
            put_attr(L,
                     chr_translate,
                     v(C2,
                       J1,
                       L1,
                       N1,
                       P1,
                       R1,
                       T1,
                       V1,
                       X1,
                       Z1,
                       B2)),
            '$run_suspensions_generate_empty_named_history_initialisation___1'(M),
            '$run_suspensions_symbol_count___2'(N),
            '$run_suspensions_fresh_symbol___2'(O),
            '$run_suspensions_background_info___2'(P1),
            '$run_suspensions_get_bg_info___2'(R1),
            '$run_suspensions_type_definition___2'(T1),
            '$run_suspensions_type_alias___2'(V1),
            '$run_suspensions_unalias_type___2'(X1),
            '$run_suspensions_ast_static_type_check_var___4'(Z1),
            '$run_suspensions_atomic_type___1'(B2)
        ;   put_attr(L,
                     chr_translate,
                     v(A,
                       M,
                       N,
                       O,
                       P,
                       Q,
                       R,
                       S,
                       T,
                       U,
                       V)),
            '$run_suspensions_generate_empty_named_history_initialisation___1'(M),
            '$run_suspensions_symbol_count___2'(N),
            '$run_suspensions_fresh_symbol___2'(O),
            '$run_suspensions_background_info___2'(P),
            '$run_suspensions_get_bg_info___2'(Q),
            '$run_suspensions_type_definition___2'(R),
            '$run_suspensions_type_alias___2'(S),
            '$run_suspensions_unalias_type___2'(T),
            '$run_suspensions_ast_static_type_check_var___4'(U),
            '$run_suspensions_atomic_type___1'(V)
        )
    ;   (   compound(L)
        ->  term_variables(L, D2),
            attach_increment(D2,
                             v(A,
                               M,
                               N,
                               O,
                               P,
                               Q,
                               R,
                               S,
                               T,
                               U,
                               V))
        ;   true
        ),
        '$run_suspensions_generate_empty_named_history_initialisation___1'(M),
        '$run_suspensions_symbol_count___2'(N),
        '$run_suspensions_fresh_symbol___2'(O),
        '$run_suspensions_background_info___2'(P),
        '$run_suspensions_get_bg_info___2'(Q),
        '$run_suspensions_type_definition___2'(R),
        '$run_suspensions_type_alias___2'(S),
        '$run_suspensions_unalias_type___2'(T),
        '$run_suspensions_ast_static_type_check_var___4'(U),
        '$run_suspensions_atomic_type___1'(V)
    ).
'$novel_production'(A, B) :-
    arg(3, A, C),
    (   hprolog:get_ds(B, C, _)
    ->  fail
    ;   true
    ).
'$extend_history'(A, B) :-
    arg(3, A, C),
    hprolog:put_ds(B, C, x, D),
    setarg(3, A, D).
'$run_suspensions_generate_empty_named_history_initialisation___1'([]).
'$run_suspensions_generate_empty_named_history_initialisation___1'([A|B]) :-
    A=suspension(_, C, D, _, E),
    (   C==active
    ->  setarg(2, A, triggered),
        F is D+1,
        setarg(3, A, F),
        generate_empty_named_history_initialisation___1__0(E, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_generate_empty_named_history_initialisation___1'(B).
'$run_suspensions_symbol_count___2'([]).
'$run_suspensions_symbol_count___2'([A|B]) :-
    A=suspension(_, C, _, D, E),
    (   C==active
    ->  setarg(2, A, triggered),
        symbol_count___2__0(D, E, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_symbol_count___2'(B).
'$run_suspensions_fresh_symbol___2'([]).
'$run_suspensions_fresh_symbol___2'([A|B]) :-
    A=suspension(_, C, _, D, _, E, F),
    (   C==active
    ->  setarg(2, A, triggered),
        G is D+1,
        setarg(4, A, G),
        fresh_symbol___2__0(E, F, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_fresh_symbol___2'(B).
'$run_suspensions_background_info___2'([]).
'$run_suspensions_background_info___2'([A|B]) :-
    A=suspension(_, C, _, D, _, E, F),
    setarg(2, A, triggered),
    G is D+1,
    setarg(4, A, G),
    background_info___2__0(E, F, A),
    (   C==triggered
    ->  setarg(2, A, active)
    ;   true
    ),
    '$run_suspensions_background_info___2'(B).
'$run_suspensions_get_bg_info___2'([]).
'$run_suspensions_get_bg_info___2'([A|B]) :-
    A=suspension(_, C, _, D, _, E, F),
    (   C==active
    ->  setarg(2, A, triggered),
        G is D+1,
        setarg(4, A, G),
        get_bg_info___2__0(E, F, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_get_bg_info___2'(B).
'$run_suspensions_type_definition___2'([]).
'$run_suspensions_type_definition___2'([A|B]) :-
    A=suspension(_, C, _, D, _, E, F),
    (   C==active
    ->  setarg(2, A, triggered),
        G is D+1,
        setarg(4, A, G),
        type_definition___2__0(E, F, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_type_definition___2'(B).
'$run_suspensions_type_alias___2'([]).
'$run_suspensions_type_alias___2'([A|B]) :-
    A=suspension(_, C, _, D, _, E, F),
    (   C==active
    ->  setarg(2, A, triggered),
        G is D+1,
        setarg(4, A, G),
        type_alias___2__0(E, F, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_type_alias___2'(B).
'$run_suspensions_unalias_type___2'([]).
'$run_suspensions_unalias_type___2'([A|B]) :-
    A=suspension(_, C, _, D, E),
    (   C==active
    ->  setarg(2, A, triggered),
        unalias_type___2__0(D, E, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_unalias_type___2'(B).
'$run_suspensions_ast_static_type_check_var___4'([]).
'$run_suspensions_ast_static_type_check_var___4'([A|B]) :-
    A=suspension(_, C, _, D, E, F, G),
    (   C==active
    ->  setarg(2, A, triggered),
        ast_static_type_check_var___4__0(D,
                                         E,
                                         F,
                                         G,
                                         A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_ast_static_type_check_var___4'(B).
'$run_suspensions_atomic_type___1'([]).
'$run_suspensions_atomic_type___1'([A|B]) :-
    A=suspension(_, C, _, D),
    (   C==active
    ->  setarg(2, A, triggered),
        atomic_type___1__0(D, A),
        (   C==triggered
        ->  setarg(2, A, active)
        ;   true
        )
    ;   true
    ),
    '$run_suspensions_atomic_type___1'(B).
'$enumerate_constraints'(A) :-
    (   nonvar(A)
    ->  functor(A, B, _),
        '$enumerate_constraints'(B, A)
    ;   '$enumerate_constraints'(_, A)
    ).
'$enumerate_constraints'(chr_source_file, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_source_file___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=chr_source_file(D).
'$enumerate_constraints'(target_module, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____target_module___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=target_module(D).
'$enumerate_constraints'(source_location, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=source_location(D, E).
'$enumerate_constraints'(indexed_argument, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=indexed_argument(D, E).
'$enumerate_constraints'(constraint_mode, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
              B),
    member(C, B),
    C=suspension(_, _, _, _, D, E),
    A=constraint_mode(D, E).
'$enumerate_constraints'(none_suspended_on_variables, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
              B),
    member(C, B),
    C=suspension(_, _, _),
    A=none_suspended_on_variables.
'$enumerate_constraints'(store_type, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1', B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=store_type(D, E).
'$enumerate_constraints'(actual_store_types, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=actual_store_types(D, E).
'$enumerate_constraints'(assumed_store_type, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=assumed_store_type(D, E).
'$enumerate_constraints'(validate_store_type_assumption, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=validate_store_type_assumption(D).
'$enumerate_constraints'(rule_count, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=rule_count(D).
'$enumerate_constraints'(passive, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=passive(D, E).
'$enumerate_constraints'(occurrence, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
              B),
    member(C, B),
    C=suspension(_, _, _, _, D, E, F, G, H),
    A=occurrence(D, E, F, G, H).
'$enumerate_constraints'(max_occurrence, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=max_occurrence(D, E).
'$enumerate_constraints'(allocation_occurrence, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=allocation_occurrence(D, E).
'$enumerate_constraints'(rule, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule___2', B),
    member(C, B),
    C=suspension(_, _, _, _, D, E),
    A=rule(D, E).
'$enumerate_constraints'(least_occurrence, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=least_occurrence(D, E).
'$enumerate_constraints'(constraint_index, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-2',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=constraint_index(D, E).
'$enumerate_constraints'(max_constraint_index, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____max_constraint_index___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=max_constraint_index(D).
'$enumerate_constraints'(identifier_size, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=identifier_size(D).
'$enumerate_constraints'(identifier_index, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=identifier_index(D, E, F).
'$enumerate_constraints'(type_indexed_identifier_size, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=type_indexed_identifier_size(D, E).
'$enumerate_constraints'(type_indexed_identifier_index, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F, G),
    A=type_indexed_identifier_index(D, E, F, G).
'$enumerate_constraints'(no_history, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_history___1-1', B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=no_history(D).
'$enumerate_constraints'(history, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-2', B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E, F),
    A=history(D, E, F).
'$enumerate_constraints'(indexing_spec, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=indexing_spec(D, E).
'$enumerate_constraints'(observation_analysis, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____observation_analysis___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=observation_analysis(D).
'$enumerate_constraints'(spawns, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns___3', B),
    member(C, B),
    C=suspension(_, _, _, _, D, E, F),
    A=spawns(D, E, F).
'$enumerate_constraints'(spawns_all, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1', B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=spawns_all(D, E).
'$enumerate_constraints'(spawns_all_triggers, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
              B),
    member(C, B),
    C=suspension(_, _, _, _, D, E),
    A=spawns_all_triggers(D, E).
'$enumerate_constraints'(spawns_all_triggers_implies_spawns_all, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
              B),
    member(C, B),
    C=suspension(_, _, _),
    A=spawns_all_triggers_implies_spawns_all.
'$enumerate_constraints'(empty_named_history_initialisations, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=empty_named_history_initialisations(D, E).
'$enumerate_constraints'(generate_empty_named_history_initialisation, A) :-
    nb_current('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
               B),
    member(C, B),
    C=suspension(_, _, _, _, D),
    A=generate_empty_named_history_initialisation(D).
'$enumerate_constraints'(find_empty_named_histories, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
              B),
    member(C, B),
    C=suspension(_, _, _, _),
    A=find_empty_named_histories.
'$enumerate_constraints'(module_initializer, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____module_initializer___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=module_initializer(D).
'$enumerate_constraints'(actual_atomic_multi_hash_keys, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=actual_atomic_multi_hash_keys(D, E, F).
'$enumerate_constraints'(actual_ground_multi_hash_keys, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=actual_ground_multi_hash_keys(D, E, F).
'$enumerate_constraints'(actual_non_ground_multi_hash_key, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=actual_non_ground_multi_hash_key(D, E).
'$enumerate_constraints'(symbol_count, A) :-
    nb_current('$chr_store_global_list_chr_translate____symbol_count___2',
               B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=symbol_count(D, E).
'$enumerate_constraints'(fresh_symbol, A) :-
    nb_current('$chr_store_global_list_chr_translate____fresh_symbol___2',
               B),
    member(C, B),
    C=suspension(_, _, _, _, _, D, E),
    A=fresh_symbol(D, E).
'$enumerate_constraints'(prolog_global_variable, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=prolog_global_variable(D).
'$enumerate_constraints'(background_info, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____background_info___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=background_info(D).
'$enumerate_constraints'(background_info, A) :-
    nb_current('$chr_store_global_list_chr_translate____background_info___2',
               B),
    member(C, B),
    C=suspension(_, _, _, _, _, D, E),
    A=background_info(D, E).
'$enumerate_constraints'(get_bg_info, A) :-
    nb_current('$chr_store_global_list_chr_translate____get_bg_info___2',
               B),
    member(C, B),
    C=suspension(_, _, _, _, _, D, E),
    A=get_bg_info(D, E).
'$enumerate_constraints'(get_bg_info_answer, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=get_bg_info_answer(D).
'$enumerate_constraints'(prev_guard_list, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
              B),
    member(C, B),
    C=suspension(_, _, _, _, D, E, F, G, H, I),
    A=prev_guard_list(D, E, F, G, H, I).
'$enumerate_constraints'(set_all_passive, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D),
    A=set_all_passive(D).
'$enumerate_constraints'(precompute_head_matchings, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
              B),
    member(C, B),
    C=suspension(_, _, _, _),
    A=precompute_head_matchings.
'$enumerate_constraints'(make_head_matchings_explicit_memo_table, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=make_head_matchings_explicit_memo_table(D, E, F).
'$enumerate_constraints'(multiple_occ_constraints_checked, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=multiple_occ_constraints_checked(D).
'$enumerate_constraints'(type_definition, A) :-
    nb_current('$chr_store_global_list_chr_translate____type_definition___2',
               B),
    member(C, B),
    C=suspension(_, _, _, _, _, D, E),
    A=type_definition(D, E).
'$enumerate_constraints'(type_alias, A) :-
    nb_current('$chr_store_global_list_chr_translate____type_alias___2', B),
    member(C, B),
    C=suspension(_, _, _, _, _, D, E),
    A=type_alias(D, E).
'$enumerate_constraints'(constraint_type, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____constraint_type___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=constraint_type(D, E).
'$enumerate_constraints'(unalias_type, A) :-
    nb_current('$chr_store_global_list_chr_translate____unalias_type___2',
               B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=unalias_type(D, E).
'$enumerate_constraints'(types_modes_condition, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E, F),
    A=types_modes_condition(D, E, F).
'$enumerate_constraints'(static_type_check, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____static_type_check___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=static_type_check(D, E).
'$enumerate_constraints'(ast_static_type_check_var, A) :-
    nb_current('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
               B),
    member(C, B),
    C=suspension(_, _, _, D, E, F, G),
    A=ast_static_type_check_var(D, E, F, G).
'$enumerate_constraints'(ast_static_atomic_builtin_type_check_var, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F, G),
    A=ast_static_atomic_builtin_type_check_var(D, E, F, G).
'$enumerate_constraints'(dynamic_type_check, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
              B),
    member(C, B),
    C=suspension(_, _, _, _),
    A=dynamic_type_check.
'$enumerate_constraints'(dynamic_type_check_clauses, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=dynamic_type_check_clauses(D).
'$enumerate_constraints'(atomic_type, A) :-
    nb_current('$chr_store_global_list_chr_translate____atomic_type___1',
               B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=atomic_type(D).
'$enumerate_constraints'(stored, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=stored(D, E, F).
'$enumerate_constraints'(stored_complete, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E, F),
    A=stored_complete(D, E, F).
'$enumerate_constraints'(check_all_passive, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=check_all_passive(D, E).
'$enumerate_constraints'(constraints_code1, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____constraints_code1___3',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E, F),
    A=constraints_code1(D, E, F).
'$enumerate_constraints'(memo_has_active_occurrence, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=memo_has_active_occurrence(D).
'$enumerate_constraints'(use_auxiliary_predicate, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=use_auxiliary_predicate(D).
'$enumerate_constraints'(use_auxiliary_predicate, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=use_auxiliary_predicate(D, E).
'$enumerate_constraints'(use_auxiliary_module, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=use_auxiliary_module(D).
'$enumerate_constraints'(functional_dependency, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F, G),
    A=functional_dependency(D, E, F, G).
'$enumerate_constraints'(initial_call_pattern, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D),
    A=initial_call_pattern(D).
'$enumerate_constraints'(call_pattern, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____call_pattern___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=call_pattern(D).
'$enumerate_constraints'(final_answer_pattern, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=final_answer_pattern(D, E).
'$enumerate_constraints'(abstract_constraints, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=abstract_constraints(D).
'$enumerate_constraints'(depends_on, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1', B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=depends_on(D, E).
'$enumerate_constraints'(depends_on_ap, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E, F, G),
    A=depends_on_ap(D, E, F, G).
'$enumerate_constraints'(depends_on_goal, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=depends_on_goal(D, E).
'$enumerate_constraints'(ai_observed_internal, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=ai_observed_internal(D, E).
'$enumerate_constraints'(ai_not_observed_internal, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=ai_not_observed_internal(D, E).
'$enumerate_constraints'(ai_not_observed, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=ai_not_observed(D, E).
'$enumerate_constraints'(depends_on_as, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E, F),
    A=depends_on_as(D, E, F).
'$enumerate_constraints'(ai_observation_gather_results, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
              B),
    member(C, B),
    C=suspension(_, _, _),
    A=ai_observation_gather_results.
'$enumerate_constraints'(ai_observation_memoed_simplification_rest_heads, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=ai_observation_memoed_simplification_rest_heads(D, E, F).
'$enumerate_constraints'(ai_observation_memoed_propagation_rest_heads, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=ai_observation_memoed_propagation_rest_heads(D, E, F).
'$enumerate_constraints'(ai_observation_memoed_abstract_goal, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=ai_observation_memoed_abstract_goal(D, E).
'$enumerate_constraints'(ai_observation_memo_abstract_goal, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observation_memo_abstract_goal___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=ai_observation_memo_abstract_goal(D, E).
'$enumerate_constraints'(partial_wake_analysis, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
              B),
    member(C, B),
    C=suspension(_, _, _, _),
    A=partial_wake_analysis.
'$enumerate_constraints'(no_partial_wake, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=no_partial_wake(D).
'$enumerate_constraints'(phase_end, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____phase_end___1-1', B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=phase_end(D).
'$enumerate_constraints'(delay_phase_end, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=delay_phase_end(D, E).
'$enumerate_constraints'(does_use_history, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=does_use_history(D, E).
'$enumerate_constraints'(does_use_field, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=does_use_field(D, E).
'$enumerate_constraints'(uses_state, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=uses_state(D, E).
'$enumerate_constraints'(if_used_state, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E, F, G, H),
    A=if_used_state(D, E, F, G, H).
'$enumerate_constraints'(used_states_known, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
              B),
    member(C, B),
    C=suspension(_, _, _),
    A=used_states_known.
'$enumerate_constraints'(stored_assertion, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D),
    A=stored_assertion(D).
'$enumerate_constraints'(never_stored_default, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E),
    A=never_stored_default(D, E).
'$enumerate_constraints'(never_stored_rules, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
              B),
    member(C, B),
    C=suspension(_, _, _, D, E),
    A=never_stored_rules(D, E).
'$enumerate_constraints'(continuation_occurrence, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=continuation_occurrence(D, E, F).
'$enumerate_constraints'(skip_to_next_id, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, _, D, E),
    A=skip_to_next_id(D, E).
'$enumerate_constraints'(set_occurrence_code_id, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=set_occurrence_code_id(D, E, F).
'$enumerate_constraints'(occurrence_code_id, A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12',
              B),
    value_ht(B, C),
    C=suspension(_, _, D, E, F),
    A=occurrence_code_id(D, E, F).
'$enumerate_constraints'(chr_constants, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
              B),
    member(C, B),
    C=suspension(_, _, _, D),
    A=chr_constants(D).
'$enumerate_constraints'(print_chr_constants, A) :-
    nb_getval('$chr_store_global_ground_chr_translate____print_chr_constants___0',
              B),
    member(C, B),
    C=suspension(_, _, _),
    A=print_chr_constants.
'$via1_multi_hash_source_location___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_indexed_argument___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_indexed_argument___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_constraint_mode___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_store_type___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_store_types___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_assumed_store_type___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_validate_store_type_assumption___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_passive___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_passive___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-15'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-125'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-134'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-3'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-13'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-34'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence___5-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_max_occurrence___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_allocation_occurrence___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_allocation_occurrence___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_rule___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_least_occurrence___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_least_occurrence___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_constraint_index___2-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-2',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_constraint_index___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_identifier_index___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_type_indexed_identifier_size___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_type_indexed_identifier_index___4-123'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_type_indexed_identifier_index___4-23'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_no_history___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_history___1-1', C),
    lookup_ht(C, A, B).
'$chr_store_constants_chr_translate____history___3___[3]'([], '$chr_store_constants_chr_translate____history___3___[3]___[]').
'$via1_multi_hash_history___3-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-2', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_history___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_indexing_spec___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns___3-13'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns___3-3'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns___3-123'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns_all___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns_all___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns_all_triggers___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_spawns_all_triggers___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_atomic_multi_hash_keys___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_atomic_multi_hash_keys___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_ground_multi_hash_keys___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_ground_multi_hash_keys___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_actual_non_ground_multi_hash_key___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_prolog_global_variable___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_prev_guard_list___6-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_set_all_passive___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_make_head_matchings_explicit_memo_table___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_constraint_type___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_type___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ast_static_atomic_builtin_type_check_var___4-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ast_static_atomic_builtin_type_check_var___4-13'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_stored___3-13'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_stored___3-123'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_stored_complete___3-13'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_stored_complete___3-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_check_all_passive___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_check_all_passive___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_memo_has_active_occurrence___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_use_auxiliary_predicate___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_use_auxiliary_predicate___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_use_auxiliary_predicate___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_use_auxiliary_module___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_functional_dependency___4-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_functional_dependency___4-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_initial_call_pattern___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_call_pattern___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____call_pattern___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_final_answer_pattern___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on___2-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on_ap___4-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on_ap___4-3'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on_goal___2-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_observed_internal___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_not_observed_internal___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_not_observed___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on_as___3-3'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_depends_on_as___3-2'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-2',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_observation_memoed_simplification_rest_heads___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_observation_memoed_propagation_rest_heads___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_ai_observation_memoed_abstract_goal___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_no_partial_wake___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_phase_end___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____phase_end___1-1', C),
    lookup_ht(C, A, B).
'$via1_multi_hash_delay_phase_end___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_does_use_history___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_does_use_history___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_does_use_field___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_uses_state___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_if_used_state___5-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_stored_assertion___1-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_never_stored_default___2-1'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_continuation_occurrence___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_skip_to_next_id___2-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_set_occurrence_code_id___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              C),
    lookup_ht(C, A, B).
'$via1_multi_hash_occurrence_code_id___3-12'(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12',
              C),
    lookup_ht(C, A, B).
'$chr_initialization' :-
    nb_setval('$chr_store_global_ground_chr_translate____print_chr_constants___0',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____chr_constants___1', []),
    new_ht(A),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12',
              A),
    new_ht(B),
    nb_setval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              B),
    new_ht(C),
    nb_setval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
              C),
    new_ht(D),
    nb_setval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              D),
    nb_setval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
              []),
    new_ht(E),
    nb_setval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
              E),
    new_ht(F),
    nb_setval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              F),
    nb_setval('$chr_store_global_ground_chr_translate____used_states_known___0',
              []),
    new_ht(G),
    nb_setval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
              G),
    nb_setval('$chr_store_global_ground_chr_translate____if_used_state___5', []),
    new_ht(H),
    nb_setval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
              H),
    new_ht(I),
    nb_setval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              I),
    new_ht(J),
    nb_setval('$chr_store_multi_hash_chr_translate____does_use_history___2-12',
              J),
    new_ht(K),
    nb_setval('$chr_store_multi_hash_chr_translate____does_use_history___2-1',
              K),
    new_ht(L),
    nb_setval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
              L),
    new_ht(M),
    nb_setval('$chr_store_multi_hash_chr_translate____phase_end___1-1', M),
    new_ht(N),
    nb_setval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              N),
    nb_setval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____ai_observation_memo_abstract_goal___2',
              []),
    new_ht(O),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1',
              O),
    new_ht(P),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12',
              P),
    new_ht(Q),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12',
              Q),
    nb_setval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
              []),
    new_ht(R),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on_as___3-2',
              R),
    new_ht(S),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
              S),
    new_ht(T),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              T),
    new_ht(U),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
              U),
    nb_setval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
              []),
    new_ht(V),
    nb_setval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
              V),
    nb_setval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
              []),
    new_ht(W),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
              W),
    new_ht(X),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
              X),
    new_ht(Y),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
              Y),
    new_ht(Z),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on___2-2',
              Z),
    new_ht(A1),
    nb_setval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
              A1),
    nb_setval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              []),
    new_ht(B1),
    nb_setval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              B1),
    new_ht(C1),
    nb_setval('$chr_store_multi_hash_chr_translate____call_pattern___1-1',
              C1),
    new_ht(D1),
    nb_setval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              D1),
    new_ht(E1),
    nb_setval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
              E1),
    new_ht(F1),
    nb_setval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              F1),
    new_ht(G1),
    nb_setval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              G1),
    new_ht(H1),
    nb_setval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12',
              H1),
    new_ht(I1),
    nb_setval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1',
              I1),
    new_ht(J1),
    nb_setval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              J1),
    new_ht(K1),
    nb_setval('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1',
              K1),
    nb_setval('$chr_store_global_ground_chr_translate____constraints_code1___3',
              []),
    new_ht(L1),
    nb_setval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
              L1),
    new_ht(M1),
    nb_setval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
              M1),
    new_ht(N1),
    nb_setval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              N1),
    new_ht(O1),
    nb_setval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              O1),
    new_ht(P1),
    nb_setval('$chr_store_multi_hash_chr_translate____stored___3-123', P1),
    new_ht(Q1),
    nb_setval('$chr_store_multi_hash_chr_translate____stored___3-13', Q1),
    nb_setval('$chr_store_global_list_chr_translate____atomic_type___1', []),
    nb_setval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
              []),
    new_ht(R1),
    nb_setval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              R1),
    new_ht(S1),
    nb_setval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              S1),
    nb_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____static_type_check___2',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
              []),
    nb_setval('$chr_store_global_list_chr_translate____unalias_type___2', []),
    new_ht(T1),
    nb_setval('$chr_store_multi_hash_chr_translate____constraint_type___2-1',
              T1),
    nb_setval('$chr_store_global_ground_chr_translate____constraint_type___2',
              []),
    nb_setval('$chr_store_global_list_chr_translate____type_alias___2', []),
    nb_setval('$chr_store_global_list_chr_translate____type_definition___2', []),
    nb_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
              []),
    new_ht(U1),
    nb_setval('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1',
              U1),
    nb_setval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
              []),
    new_ht(V1),
    nb_setval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
              V1),
    new_ht(W1),
    nb_setval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              W1),
    nb_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
              []),
    nb_setval('$chr_store_global_list_chr_translate____get_bg_info___2', []),
    nb_setval('$chr_store_global_list_chr_translate____background_info___2', []),
    nb_setval('$chr_store_global_ground_chr_translate____background_info___1',
              []),
    new_ht(X1),
    nb_setval('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1',
              X1),
    nb_setval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
              []),
    nb_setval('$chr_store_global_list_chr_translate____fresh_symbol___2', []),
    nb_setval('$chr_store_global_list_chr_translate____symbol_count___2', []),
    new_ht(Y1),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              Y1),
    new_ht(Z1),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              Z1),
    new_ht(A2),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              A2),
    new_ht(B2),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              B2),
    new_ht(C2),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              C2),
    nb_setval('$chr_store_global_ground_chr_translate____module_initializer___1',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
              []),
    nb_setval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
              []),
    new_ht(D2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              D2),
    new_ht(E2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
              E2),
    nb_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
              []),
    new_ht(F2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              F2),
    new_ht(G2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
              G2),
    new_ht(H2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns___3-123', H2),
    new_ht(I2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns___3-3', I2),
    new_ht(J2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns___3-1', J2),
    new_ht(K2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns___3-12', K2),
    nb_setval('$chr_store_global_ground_chr_translate____spawns___3', []),
    new_ht(L2),
    nb_setval('$chr_store_multi_hash_chr_translate____spawns___3-13', L2),
    nb_setval('$chr_store_global_ground_chr_translate____observation_analysis___1',
              []),
    new_ht(M2),
    nb_setval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              M2),
    new_ht(N2),
    nb_setval('$chr_store_multi_hash_chr_translate____history___3-1', N2),
    new_ht(O2),
    nb_setval('$chr_store_multi_hash_chr_translate____history___3-2', O2),
    nb_setval('$chr_store_constants_chr_translate____history___3___[3]___[]', []),
    new_ht(P2),
    nb_setval('$chr_store_multi_hash_chr_translate____no_history___1-1',
              P2),
    new_ht(Q2),
    nb_setval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23',
              Q2),
    new_ht(R2),
    nb_setval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
              R2),
    new_ht(S2),
    nb_setval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              S2),
    new_ht(T2),
    nb_setval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              T2),
    nb_setval('$chr_store_global_ground_chr_translate____identifier_size___1',
              []),
    nb_setval('$chr_store_global_ground_chr_translate____max_constraint_index___1',
              []),
    new_ht(U2),
    nb_setval('$chr_store_multi_hash_chr_translate____constraint_index___2-1',
              U2),
    new_ht(V2),
    nb_setval('$chr_store_multi_hash_chr_translate____constraint_index___2-2',
              V2),
    new_ht(W2),
    nb_setval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              W2),
    new_ht(X2),
    nb_setval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
              X2),
    new_ht(Y2),
    nb_setval('$chr_store_multi_hash_chr_translate____rule___2-1', Y2),
    nb_setval('$chr_store_global_ground_chr_translate____rule___2', []),
    new_ht(Z2),
    nb_setval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
              Z2),
    new_ht(A3),
    nb_setval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              A3),
    new_ht(B3),
    nb_setval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              B3),
    new_ht(C3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
              C3),
    new_ht(D3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              D3),
    new_ht(E3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
              E3),
    new_ht(F3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
              F3),
    new_ht(G3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              G3),
    new_ht(H3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
              H3),
    new_ht(I3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
              I3),
    nb_setval('$chr_store_global_ground_chr_translate____occurrence___5', []),
    new_ht(J3),
    nb_setval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
              J3),
    new_ht(K3),
    nb_setval('$chr_store_multi_hash_chr_translate____passive___2-12', K3),
    new_ht(L3),
    nb_setval('$chr_store_multi_hash_chr_translate____passive___2-1', L3),
    nb_setval('$chr_store_global_ground_chr_translate____rule_count___1', []),
    new_ht(M3),
    nb_setval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              M3),
    new_ht(N3),
    nb_setval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              N3),
    new_ht(O3),
    nb_setval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              O3),
    new_ht(P3),
    nb_setval('$chr_store_multi_hash_chr_translate____store_type___2-1',
              P3),
    nb_setval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
              []),
    new_ht(Q3),
    nb_setval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              Q3),
    nb_setval('$chr_store_global_ground_chr_translate____constraint_mode___2',
              []),
    new_ht(R3),
    nb_setval('$chr_store_multi_hash_chr_translate____indexed_argument___2-12',
              R3),
    new_ht(S3),
    nb_setval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              S3),
    new_ht(T3),
    nb_setval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              T3),
    nb_setval('$chr_store_global_ground_chr_translate____target_module___1', []),
    nb_setval('$chr_store_global_ground_chr_translate____chr_source_file___1',
              []).
:- (initialization'$chr_initialization').
:- (dynamic user:exception/3).
:- (multifile user:exception/3).
user:exception(undefined_global_variable, A, retry) :-
    '$chr_prolog_global_variable'(A),
    '$chr_initialization'.
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____print_chr_constants___0').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____chr_constants___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____never_stored_rules___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____never_stored_default___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____stored_assertion___1-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____used_states_known___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____if_used_state___5-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____if_used_state___5').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____uses_state___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____does_use_field___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____does_use_history___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____does_use_history___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____phase_end___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____partial_wake_analysis___0').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____ai_observation_memo_abstract_goal___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on_as___3-2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on_as___3-3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____ai_observed_internal___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on___2-2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____depends_on___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____abstract_constraints___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____call_pattern___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____functional_dependency___4-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____functional_dependency___4-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____constraints_code1___3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____check_all_passive___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____check_all_passive___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____stored_complete___3-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____stored_complete___3-13').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____stored___3-123').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____stored___3-13').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____atomic_type___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____dynamic_type_check___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____ast_static_type_check_var___4').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____static_type_check___2').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____types_modes_condition___3').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____unalias_type___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____constraint_type___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____constraint_type___2').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____type_alias___2').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____type_definition___2').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____precompute_head_matchings___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____set_all_passive___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____prev_guard_list___6').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____get_bg_info_answer___1').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____get_bg_info___2').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____background_info___2').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____background_info___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____prolog_global_variable___1').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____fresh_symbol___2').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____symbol_count___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____module_initializer___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____find_empty_named_histories___0').
'$chr_prolog_global_variable'('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____spawns_all_triggers___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns_all___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns_all___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns___3-123').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns___3-3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns___3-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns___3-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____spawns___3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____spawns___3-13').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____observation_analysis___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____indexing_spec___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____history___3-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____history___3-2').
'$chr_prolog_global_variable'('$chr_store_constants_chr_translate____history___3___[3]___[]').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____no_history___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____identifier_index___3-12').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____identifier_size___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____max_constraint_index___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____constraint_index___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____constraint_index___2-2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____least_occurrence___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____least_occurrence___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____rule___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____rule___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____max_occurrence___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-34').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-13').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-3').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-134').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-125').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____occurrence___5').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____occurrence___5-15').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____passive___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____passive___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____rule_count___1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____actual_store_types___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____store_type___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____constraint_mode___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____constraint_mode___2').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____indexed_argument___2-12').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____indexed_argument___2-1').
'$chr_prolog_global_variable'('$chr_store_multi_hash_chr_translate____source_location___2-1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____target_module___1').
'$chr_prolog_global_variable'('$chr_store_global_ground_chr_translate____chr_source_file___1').
chr_source_file(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_source_file___1',
              B),
    (   member(C, B),
        C=suspension(_, active, _, _),
        !
    ;   !,
        chr_source_file___1__0__0__2(B, A)
    ).
chr_source_file___1__0__0__2([], A) :-
    chr_source_file___1__1(A).
chr_source_file___1__0__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        arg(3, A, D),
        (   var(D)
        ->  nb_getval('$chr_store_global_ground_chr_translate____chr_source_file___1',
                      E),
            E=[_|F],
            b_setval('$chr_store_global_ground_chr_translate____chr_source_file___1',
                     F),
            (   F=[G|_]
            ->  setarg(3, G, _)
            ;   true
            )
        ;   D=[_, _|F],
            setarg(2, D, F),
            (   F=[G|_]
            ->  setarg(3, G, D)
            ;   true
            )
        ),
        chr_source_file___1__0__0__2(B, C)
    ;   chr_source_file___1__0__0__2(B, C)
    ).
chr_source_file(A) :-
    chr_source_file___1__1(A).
chr_source_file___1__1(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____chr_source_file___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____chr_source_file___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_chr_source_file(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_source_file___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_chr_source_file(user).
target_module(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____target_module___1',
              B),
    (   member(C, B),
        C=suspension(_, active, _, _),
        !
    ;   !,
        target_module___1__0__0__2(B, A)
    ).
target_module___1__0__0__2([], A) :-
    target_module___1__1(A).
target_module___1__0__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        arg(3, A, D),
        (   var(D)
        ->  nb_getval('$chr_store_global_ground_chr_translate____target_module___1',
                      E),
            E=[_|F],
            b_setval('$chr_store_global_ground_chr_translate____target_module___1',
                     F),
            (   F=[G|_]
            ->  setarg(3, G, _)
            ;   true
            )
        ;   D=[_, _|F],
            setarg(2, D, F),
            (   F=[G|_]
            ->  setarg(3, G, D)
            ;   true
            )
        ),
        target_module___1__0__0__2(B, C)
    ;   target_module___1__0__0__2(B, C)
    ).
target_module(A) :-
    target_module___1__1(A).
target_module___1__1(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____target_module___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____target_module___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_target_module(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____target_module___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_target_module(user).
source_location(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              C),
    lookup_ht(C, A, D),
    (   member(E, D),
        E=suspension(_, active, _, _),
        !
    ;   !,
        source_location___2__0__0__2(D, A, B)
    ).
source_location___2__0__0__2([], A, B) :-
    source_location___2__1(A, B).
source_location___2__0__0__2([A|B], C, D) :-
    (   A=suspension(_, active, E, _),
        E==C
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
                  F),
        delete_ht(F, C, A),
        source_location___2__0__0__2(B, C, D)
    ;   source_location___2__0__0__2(B, C, D)
    ).
source_location(A, B) :-
    source_location___2__1(A, B).
source_location___2__1(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              E),
    insert_ht(E, A, C).
get_line_number(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____source_location___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_line_number(_, (?):0).
indexed_argument(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
indexed_argument(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              E),
    insert_ht(E, A, C),
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-12',
              F),
    insert_ht(F, k(A, B), C).
is_indexed_argument(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
is_indexed_argument(_, _) :-
    fail.
constraint_mode(A, B) :-
    constraint_mode___2__0(A, B, _).
constraint_mode___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              D),
    lookup_ht(D, A, E),
    (   member(F, E),
        F=suspension(_, active, _, _, _, _),
        !
    ;   !,
        constraint_mode___2__0__0__2(E, A, B, C)
    ).
constraint_mode___2__0__0__2([], A, B, C) :-
    constraint_mode___2__1(A, B, C).
constraint_mode___2__0__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _),
        F==C
    ->  setarg(2, A, removed),
        ( arg(4, A, G),
          (   var(G)
          ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                        H),
              H=[_|I],
              b_setval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                       I),
              (   I=[J|_]
              ->  setarg(4, J, _)
              ;   true
              )
          ;   G=[_, _|I],
              setarg(2, G, I),
              (   I=[J|_]
              ->  setarg(4, J, G)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                  K),
        delete_ht(K, C, A),
        constraint_mode___2__0__0__2(B, C, D, E)
    ;   constraint_mode___2__0__0__2(B, C, D, E)
    ).
constraint_mode___2__0(A, B, C) :-
    constraint_mode___2__1(A, B, C).
constraint_mode___2__1(A, B, C) :-
    A=D/E,
    nb_getval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
              F),
    !,
    C=suspension(G, not_stored_yet, t, _, A, B),
    'chr gen_id'(G),
    constraint_mode___2__1__0__7(F,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E).
constraint_mode___2__1__0__7([], A, B, C, _, _) :-
    constraint_mode___2__2(A, B, C).
constraint_mode___2__1__0__7([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H, I, J),
        H=[K|L],
        I=[M|N],
        functor(K, F, G)
    ->  setarg(2, A, removed),
        arg(3, A, O),
        (   var(O)
        ->  nb_getval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
                      P),
            P=[_|Q],
            b_setval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
                     Q),
            (   Q=[R|_]
            ->  setarg(3, R, _)
            ;   true
            )
        ;   O=[_, _|Q],
            setarg(2, O, Q),
            (   Q=[R|_]
            ->  setarg(3, R, O)
            ;   true
            )
        ),
        arg(2, E, S),
        setarg(2, E, active),
        (   S==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                      T),
            U=[E|T],
            b_setval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                     U),
            (   T=[V|_]
            ->  setarg(4, V, U)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                      W),
            insert_ht(W, C, E)
        ;   true
        ),
        K=..[_|X],
        J=(Y, Z, A1),
        modes_condition(D, X, Y),
        get_constraint_type_det(F/G, B1),
        M=..[_|C1],
        types_condition(B1, X, C1, D, Z),
        types_modes_condition(L, N, A1),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            constraint_mode___2__1__0__7(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G)
        ;   true
        )
    ;   constraint_mode___2__1__0__7(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G)
    ).
constraint_mode___2__1(A, B, C) :-
    C=suspension(D, not_stored_yet, t, _, A, B),
    'chr gen_id'(D),
    constraint_mode___2__2(A, B, C).
constraint_mode___2__2(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', D),
    lookup_ht(D, A, E),
    !,
    constraint_mode___2__2__0__8(E, A, B, C).
constraint_mode___2__2__0__8([], A, B, C) :-
    constraint_mode___2__3(A, B, C).
constraint_mode___2__2__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, I),
        F==C,
        nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                  J)
    ->  constraint_mode___2__2__1__8(J,
                                     G,
                                     H,
                                     I,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E)
    ;   constraint_mode___2__2__0__8(B, C, D, E)
    ).
constraint_mode___2__2__1__8([], _, _, _, _, A, B, C, D) :-
    constraint_mode___2__2__0__8(A, B, C, D).
constraint_mode___2__2__1__8([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', K),
        lookup_ht(K, C, L)
    ->  constraint_mode___2__2__2__8(L,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J)
    ;   constraint_mode___2__2__1__8(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J)
    ).
constraint_mode___2__2__2__8([], _, A, B, C, D, E, F, G, H, I) :-
    constraint_mode___2__2__1__8(A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I).
constraint_mode___2__2__2__8([A|B], C, D, E, F, G, H, I, J, K, L) :-
    (   A=suspension(_, active, _, _, M, N),
        M==E,
        O=t(330, C, H, A, L),
        '$novel_production'(C, O),
        '$novel_production'(H, O),
        '$novel_production'(A, O),
        '$novel_production'(L, O)
    ->  '$extend_history'(L, O),
        arg(2, L, P),
        setarg(2, L, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                      Q),
            R=[L|Q],
            b_setval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                      T),
            insert_ht(T, J, L)
        ;   true
        ),
        N=pragma(rule(U, V, W, _), _, _, _, _),
        (   is_passive(E, F)
        ->  true
        ;   G==simplification
        ->  select(X, U, _),
            X=..[_|Y],
            term_variables(W, Z),
            partial_wake_args(Y, K, Z, J)
        ;   select(X, V, _),
            X=..[_|Y],
            term_variables(W, Z),
            partial_wake_args(Y, K, Z, J)
        ),
        (   L=suspension(_, active, _, _, _, _)
        ->  setarg(2, L, inactive),
            constraint_mode___2__2__2__8(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I,
                                         J,
                                         K,
                                         L)
        ;   true
        )
    ;   constraint_mode___2__2__2__8(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L)
    ).
constraint_mode___2__2(A, B, C) :-
    constraint_mode___2__3(A, B, C).
constraint_mode___2__3(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                  D),
        E=[B|D],
        b_setval('$chr_store_global_ground_chr_translate____constraint_mode___2',
                 E),
        (   D=[F|_]
        ->  setarg(4, F, E)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                  G),
        insert_ht(G, A, B)
    ;   true
    ).
get_constraint_mode(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, _, F),
    !,
    B=F.
get_constraint_mode(_/A, B) :-
    replicate(A, ?, B).
may_trigger(A) :-
    \+ has_active_occurrence(A),
    !,
    fail.
may_trigger(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, _, E),
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              F),
    lookup_ht(F, A, G),
    member(H, G),
    H=suspension(_, active, _, I),
    nth1(I, E, J),
    J\==(+),
    !,
    is_stored(A).
may_trigger(_) :-
    chr_pp_flag(debugable, on).
only_ground_indexed_arguments(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, _, E),
    nb_getval('$chr_store_multi_hash_chr_translate____indexed_argument___2-1',
              F),
    lookup_ht(F, A, G),
    member(H, G),
    H=suspension(_, active, _, I),
    nth1(I, E, J),
    J\==(+),
    !,
    fail.
only_ground_indexed_arguments(_).
none_suspended_on_variables :-
    nb_getval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
              A),
    member(B, A),
    B=suspension(_, active, _),
    !.
none_suspended_on_variables :-
    A=suspension(B, active, _),
    'chr gen_id'(B),
    nb_getval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
              C),
    D=[A|C],
    b_setval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
             D),
    (   C=[E|_]
    ->  setarg(3, E, D)
    ;   true
    ).
are_none_suspended_on_variables :-
    nb_getval('$chr_store_global_ground_chr_translate____none_suspended_on_variables___0',
              A),
    member(B, A),
    B=suspension(_, active, _),
    !.
are_none_suspended_on_variables :-
    fail.
store_type(A, B) :-
    store_type___2__0(A, B, _).
store_type___2__0(A, B, C) :-
    chr_pp_flag(verbose, on),
    !,
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1', E),
    insert_ht(E, A, C),
    format('The indexes for ~w are:\n', [A]),
    format_storetype(B),
    (   C=suspension(_, active, _, _)
    ->  setarg(2, C, inactive),
        store_type___2__1(A, B, C)
    ;   true
    ).
store_type___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, A, B),
    'chr gen_id'(D),
    store_type___2__1(A, B, C).
store_type___2__1(A, _, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              F),
    lookup_ht(F, A, G),
    member(H, G),
    H=suspension(_, active, _, I),
    !,
    setarg(2, E, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              J),
    delete_ht(J, A, E),
    setarg(2, H, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              K),
    delete_ht(K, A, H),
    (   var(B)
    ->  true
    ;   arg(2, B, L),
        setarg(2, B, removed),
        (   L==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                      M),
            delete_ht(M, A, B)
        )
    ),
    store_type(A, multi_store(I)).
store_type___2__1(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
get_store_type(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1', C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_store_type(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_store_type(_, default).
update_store_type(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    (   memberchk(B, F),
        !
    ;   !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                  G),
        delete_ht(G, A, E),
        actual_store_types(A, [B|F])
    ).
update_store_type(A, B) :-
    actual_store_types(A, [B]).
actual_store_types(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F, G),
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              H),
    lookup_ht(H, A, I),
    member(J, I),
    J=suspension(_, active, _),
    !,
    setarg(2, E, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              K),
    delete_ht(K, k(A, F), E),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              L),
    delete_ht(L, A, E),
    selectchk(multi_hash([F]), B, M),
    F=[N],
    (   get_constraint_arg_type(A, N, O),
        enumerated_atomic_type(O, P)
    ->  Q=P,
        R=complete
    ;   Q=G,
        R=incomplete
    ),
    actual_store_types(A,
                       [atomic_constants(F, Q, R)|M]).
actual_store_types(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F, G),
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              H),
    lookup_ht(H, A, I),
    member(J, I),
    J=suspension(_, active, _),
    !,
    setarg(2, E, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              K),
    delete_ht(K, k(A, F), E),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              L),
    delete_ht(L, A, E),
    (   F=[M],
        get_constraint_arg_type(A, M, N),
        N=chr_enum(O)
    ->  P=complete
    ;   O=G,
        P=incomplete
    ),
    selectchk(multi_hash([F]), B, Q),
    actual_store_types(A,
                       [ground_constants(F, O, P)|Q]).
actual_store_types(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _),
    (   (   memberchk(multi_hash([[F]]), B),
            (   get_constraint_type(A, G),
                nth1(F, G, H),
                enumerated_atomic_type(H, I),
                !,
                selectchk(multi_hash([[F]]), B, J),
                actual_store_types(A,
                                   [ atomic_constants([F],
                                                      I,
                                                      complete)
                                   | J
                                   ])
            ;   get_constraint_arg_type(A, F, K),
                K=chr_enum(L),
                !,
                selectchk(multi_hash([[F]]), B, M),
                actual_store_types(A,
                                   [ ground_constants([F],
                                                      L,
                                                      complete)
                                   | M
                                   ])
            )
        ;   nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                      N),
            lookup_ht(N, A, O),
            member(P, O),
            P=suspension(_, active, _, _),
            !,
            setarg(2, E, removed),
            nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      Q),
            delete_ht(Q, A, E),
            setarg(2, P, removed),
            nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                      R),
            delete_ht(R, A, P),
            (   maplist(partial_store, B)
            ->  S=[global_ground|B]
            ;   S=B
            ),
            store_type(A, multi_store(S))
        )
    ;   nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                  T),
        lookup_ht(T, A, U),
        member(V, U),
        V=suspension(_, active, _, _),
        !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                  W),
        delete_ht(W, A, E),
        setarg(2, V, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                  X),
        delete_ht(X, A, V),
        store_type(A, multi_store(B))
    ).
actual_store_types(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              E),
    insert_ht(E, A, C).
assumed_store_type(A, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    (   (   nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                      E),
            lookup_ht(E, A, F),
            member(G, F),
            G=suspension(_, active, _, H),
            !,
            setarg(2, D, removed),
            nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      I),
            delete_ht(I, A, D),
            setarg(2, G, removed),
            nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                      J),
            delete_ht(J, A, G),
            (   maplist(partial_store, H)
            ->  K=[global_ground|H]
            ;   K=H
            ),
            store_type(A, multi_store(K))
        ;   chr_pp_flag(debugable, on),
            !,
            setarg(2, D, removed),
            nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      L),
            delete_ht(L, A, D),
            store_type(A, default)
        )
    ;   !,
        setarg(2, D, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                  M),
        delete_ht(M, A, D),
        store_type(A, global_ground)
    ).
assumed_store_type(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              E),
    insert_ht(E, A, C).
validate_store_type_assumption(A) :-
    validate_store_type_assumption___1__0(A, _).
validate_store_type_assumption___1__0(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    B=suspension(E, not_stored_yet, A),
    'chr gen_id'(E),
    validate_store_type_assumption___1__0__0__1(D, A, B).
validate_store_type_assumption___1__0__0__1([], A, B) :-
    validate_store_type_assumption___1__1(A, B).
validate_store_type_assumption___1__0__0__1([A|B], C, D) :-
    (   A=suspension(_, active, E, F),
        E==C,
        ground(C),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
                  G),
        lookup_ht(G, C, H),
        member(I, H),
        I=suspension(_, active, _, J, K)
    ->  ( setarg(2, A, removed),
          nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                    L),
          delete_ht(L, C, A)
        ),
        setarg(2, I, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
                    M),
          delete_ht(M, k(C, J), I)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
                  N),
        delete_ht(N, C, I),
        arg(2, D, O),
        setarg(2, D, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      P),
            insert_ht(P, C, D)
        ;   true
        ),
        selectchk(multi_hash([J]), F, Q),
        J=[R],
        (   get_constraint_arg_type(C, R, S),
            enumerated_atomic_type(S, T)
        ->  U=T,
            V=complete
        ;   U=K,
            V=incomplete
        ),
        actual_store_types(C,
                           [ atomic_constants(J, U, V)
                           | Q
                           ]),
        (   D=suspension(_, active, _)
        ->  setarg(2, D, inactive),
            validate_store_type_assumption___1__0__0__1(B,
                                                        C,
                                                        D)
        ;   true
        )
    ;   validate_store_type_assumption___1__0__0__1(B, C, D)
    ).
validate_store_type_assumption___1__0(A, B) :-
    B=suspension(C, not_stored_yet, A),
    'chr gen_id'(C),
    validate_store_type_assumption___1__1(A, B).
validate_store_type_assumption___1__1(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    validate_store_type_assumption___1__1__0__2(D, A, B).
validate_store_type_assumption___1__1__0__2([], A, B) :-
    validate_store_type_assumption___1__2(A, B).
validate_store_type_assumption___1__1__0__2([A|B], C, D) :-
    (   A=suspension(_, active, E, F),
        E==C,
        ground(C),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
                  G),
        lookup_ht(G, C, H),
        member(I, H),
        I=suspension(_, active, _, J, K)
    ->  ( setarg(2, A, removed),
          nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                    L),
          delete_ht(L, C, A)
        ),
        setarg(2, I, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
                    M),
          delete_ht(M, k(C, J), I)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
                  N),
        delete_ht(N, C, I),
        arg(2, D, O),
        setarg(2, D, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      P),
            insert_ht(P, C, D)
        ;   true
        ),
        (   J=[Q],
            get_constraint_arg_type(C, Q, R),
            R=chr_enum(S)
        ->  T=complete
        ;   S=K,
            T=incomplete
        ),
        selectchk(multi_hash([J]), F, U),
        actual_store_types(C,
                           [ ground_constants(J, S, T)
                           | U
                           ]),
        (   D=suspension(_, active, _)
        ->  setarg(2, D, inactive),
            validate_store_type_assumption___1__1__0__2(B,
                                                        C,
                                                        D)
        ;   true
        )
    ;   validate_store_type_assumption___1__1__0__2(B, C, D)
    ).
validate_store_type_assumption___1__1(A, B) :-
    validate_store_type_assumption___1__2(A, B).
validate_store_type_assumption___1__2(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    validate_store_type_assumption___1__2__0__3(D, A, B).
validate_store_type_assumption___1__2__0__3([], A, B) :-
    validate_store_type_assumption___1__3(A, B).
validate_store_type_assumption___1__2__0__3([A|B], C, D) :-
    (   A=suspension(_, active, E, F),
        E==C,
        memberchk(multi_hash([[G]]), F),
        get_constraint_type(C, H),
        nth1(G, H, I),
        enumerated_atomic_type(I, J)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                  K),
        delete_ht(K, C, A),
        arg(2, D, L),
        setarg(2, D, active),
        (   L==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      M),
            insert_ht(M, C, D)
        ;   true
        ),
        selectchk(multi_hash([[G]]), F, N),
        actual_store_types(C,
                           [ atomic_constants([G], J, complete)
                           | N
                           ]),
        (   D=suspension(_, active, _)
        ->  setarg(2, D, inactive),
            validate_store_type_assumption___1__2__0__3(B,
                                                        C,
                                                        D)
        ;   true
        )
    ;   validate_store_type_assumption___1__2__0__3(B, C, D)
    ).
validate_store_type_assumption___1__2(A, B) :-
    validate_store_type_assumption___1__3(A, B).
validate_store_type_assumption___1__3(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    validate_store_type_assumption___1__3__0__4(D, A, B).
validate_store_type_assumption___1__3__0__4([], A, B) :-
    validate_store_type_assumption___1__4(A, B).
validate_store_type_assumption___1__3__0__4([A|B], C, D) :-
    (   A=suspension(_, active, E, F),
        E==C,
        memberchk(multi_hash([[G]]), F),
        get_constraint_arg_type(C, G, H),
        H=chr_enum(I)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                  J),
        delete_ht(J, C, A),
        arg(2, D, K),
        setarg(2, D, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      L),
            insert_ht(L, C, D)
        ;   true
        ),
        selectchk(multi_hash([[G]]), F, M),
        actual_store_types(C,
                           [ ground_constants([G], I, complete)
                           | M
                           ]),
        (   D=suspension(_, active, _)
        ->  setarg(2, D, inactive),
            validate_store_type_assumption___1__3__0__4(B,
                                                        C,
                                                        D)
        ;   true
        )
    ;   validate_store_type_assumption___1__3__0__4(B, C, D)
    ).
validate_store_type_assumption___1__3(A, B) :-
    validate_store_type_assumption___1__4(A, B).
validate_store_type_assumption___1__4(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    (   nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                  G),
        lookup_ht(G, A, H),
        member(I, H),
        I=suspension(_, active, _, _),
        !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                  J),
        delete_ht(J, A, E),
        setarg(2, I, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                  K),
        delete_ht(K, A, I),
        (   var(B)
        ->  true
        ;   arg(2, B, L),
            setarg(2, B, removed),
            (   L==not_stored_yet
            ->  true
            ;   nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                          M),
                delete_ht(M, A, B)
            )
        ),
        (   maplist(partial_store, F)
        ->  N=[global_ground|F]
        ;   N=F
        ),
        store_type(A, multi_store(N))
    ;   nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                  O),
        lookup_ht(O, A, P),
        member(Q, P),
        Q=suspension(_, active, _, _),
        !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
                  R),
        delete_ht(R, A, E),
        setarg(2, Q, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____store_type___2-1',
                  S),
        delete_ht(S, A, Q),
        (   var(B)
        ->  true
        ;   arg(2, B, T),
            setarg(2, B, removed),
            (   T==not_stored_yet
            ->  true
            ;   nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                          U),
                delete_ht(U, A, B)
            )
        ),
        store_type(A, multi_store(F))
    ).
validate_store_type_assumption___1__4(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _),
    (   chr_pp_flag(debugable, on),
        !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                  F),
        delete_ht(F, A, E),
        (   var(B)
        ->  true
        ;   arg(2, B, G),
            setarg(2, B, removed),
            (   G==not_stored_yet
            ->  true
            ;   nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                          H),
                delete_ht(H, A, B)
            )
        ),
        store_type(A, default)
    ;   !,
        setarg(2, E, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____assumed_store_type___2-1',
                  I),
        delete_ht(I, A, E),
        (   var(B)
        ->  true
        ;   arg(2, B, J),
            setarg(2, B, removed),
            (   J==not_stored_yet
            ->  true
            ;   nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                          K),
                delete_ht(K, A, B)
            )
        ),
        store_type(A, global_ground)
    ).
validate_store_type_assumption___1__4(A, B) :-
    (   var(B)
    ->  true
    ;   arg(2, B, C),
        setarg(2, B, removed),
        (   C==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
                      D),
            delete_ht(D, A, B)
        )
    ).
rule_count(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
              B),
    (   member(C, B),
        C=suspension(_, active, _, _),
        !
    ;   !,
        rule_count___1__0__0__2(B, A)
    ).
rule_count___1__0__0__2([], A) :-
    rule_count___1__1(A).
rule_count___1__0__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        arg(3, A, D),
        (   var(D)
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
                      E),
            E=[_|F],
            b_setval('$chr_store_global_ground_chr_translate____rule_count___1',
                     F),
            (   F=[G|_]
            ->  setarg(3, G, _)
            ;   true
            )
        ;   D=[_, _|F],
            setarg(2, D, F),
            (   F=[G|_]
            ->  setarg(3, G, D)
            ;   true
            )
        ),
        rule_count___1__0__0__2(B, C)
    ;   rule_count___1__0__0__2(B, C)
    ).
rule_count(A) :-
    rule_count___1__1(A).
rule_count___1__1(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____rule_count___1', E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
inc_rule_count(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____rule_count___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____rule_count___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    A is D+1,
    rule_count(A).
inc_rule_count(1) :-
    rule_count(1).
passive(A, B) :-
    passive___2__0(A, B, _).
passive___2__0(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
passive___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    passive___2__0__0__6(E, A, B, C).
passive___2__0__0__6([], A, B, C) :-
    passive___2__1(A, B, C).
passive___2__0__0__6([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=[H|I],
        H==D,
        ground(C),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', J),
        lookup_ht(J, C, K),
        member(L, K),
        L=suspension(_, active, _, _, _, _)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
                    M),
          delete_ht(M, k(C, [D|I]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  N),
        delete_ht(N, C, A),
        arg(2, E, O),
        setarg(2, E, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      P),
            insert_ht(P, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      Q),
            insert_ht(Q, k(C, D), E)
        ;   true
        ),
        least_occurrence(C, I),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            passive___2__0__0__6(B, C, D, E)
        ;   true
        )
    ;   passive___2__0__0__6(B, C, D, E)
    ).
passive___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    passive___2__1(A, B, C).
passive___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    passive___2__1__0__8(E, A, B, C).
passive___2__1__0__8([], A, B, C) :-
    passive___2__2(A, B, C).
passive___2__1__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, _),
        H==C,
        I==D,
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                  J),
        lookup_ht(J, F, K)
    ->  passive___2__1__1__8(K,
                             F,
                             G,
                             A,
                             B,
                             C,
                             D,
                             E)
    ;   passive___2__1__0__8(B, C, D, E)
    ).
passive___2__1__1__8([], _, _, _, A, B, C, D) :-
    passive___2__1__0__8(A, B, C, D).
passive___2__1__1__8([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K, L),
        J==C,
        D<K,
        ( ground(C),
          ground(D)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123',
                  M),
        lookup_ht(M, k(C, D, yes), N),
        member(O, N),
        O=suspension(_, active, _, _, _)
    ->  ( setarg(2, A, removed),
          nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                    P),
          delete_ht(P, k(C, L), A),
          nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                    Q),
          delete_ht(Q, C, A)
        ),
        setarg(2, O, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13',
                    R),
          delete_ht(R, k(C, yes), O)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123',
                  S),
        delete_ht(S, k(C, D, yes), O),
        arg(2, I, T),
        setarg(2, I, active),
        (   T==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      U),
            insert_ht(U, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      V),
            insert_ht(V, k(G, H), I)
        ;   true
        ),
        W is L-1,
        stored(C, D, maybe),
        stored_complete(C, K, W),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            passive___2__1__1__8(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I)
        ;   true
        )
    ;   passive___2__1__1__8(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I)
    ).
passive___2__1(A, B, C) :-
    passive___2__2(A, B, C).
passive___2__2(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', D),
    lookup_ht(D, A, E),
    !,
    passive___2__2__0__9(E, A, B, C).
passive___2__2__0__9([], A, B, C) :-
    passive___2__3(A, B, C).
passive___2__2__0__9([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G),
        F==C,
        H=t(251, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H),
        G=pragma(rule(_, _, _, _), ids([D|I], J), _, _, C)
    ->  '$extend_history'(E, H),
        arg(2, E, K),
        setarg(2, E, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      L),
            insert_ht(L, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      M),
            insert_ht(M, k(C, D), E)
        ;   true
        ),
        append(I, J, N),
        check_all_passive(C, N),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            passive___2__2__0__9(B, C, D, E)
        ;   true
        )
    ;   passive___2__2__0__9(B, C, D, E)
    ).
passive___2__2(A, B, C) :-
    passive___2__3(A, B, C).
passive___2__3(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', D),
    lookup_ht(D, A, E),
    !,
    passive___2__3__0__10(E, A, B, C).
passive___2__3__0__10([], A, B, C) :-
    passive___2__4(A, B, C).
passive___2__3__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G),
        F==C,
        H=t(252, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H),
        G=pragma(rule(_, _, _, _), ids([], [D|I]), _, _, C)
    ->  '$extend_history'(E, H),
        arg(2, E, J),
        setarg(2, E, active),
        (   J==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      K),
            insert_ht(K, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      L),
            insert_ht(L, k(C, D), E)
        ;   true
        ),
        check_all_passive(C, I),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            passive___2__3__0__10(B, C, D, E)
        ;   true
        )
    ;   passive___2__3__0__10(B, C, D, E)
    ).
passive___2__3(A, B, C) :-
    passive___2__4(A, B, C).
passive___2__4(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
              D),
    lookup_ht(D, A, E),
    !,
    passive___2__4__0__11(E, A, B, C).
passive___2__4__0__11([], A, B, C) :-
    passive___2__5(A, B, C).
passive___2__4__0__11([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=[H|I],
        H==D
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
                    J),
          delete_ht(J, k(C, [D|I]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
                  K),
        delete_ht(K, C, A),
        arg(2, E, L),
        setarg(2, E, active),
        (   L==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      M),
            insert_ht(M, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      N),
            insert_ht(N, k(C, D), E)
        ;   true
        ),
        check_all_passive(C, I),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            passive___2__4__0__11(B, C, D, E)
        ;   true
        )
    ;   passive___2__4__0__11(B, C, D, E)
    ).
passive___2__4(A, B, C) :-
    passive___2__5(A, B, C).
passive___2__5(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    passive___2__5__0__14(E, A, B, C).
passive___2__5__0__14([], A, B, C) :-
    passive___2__6(A, B, C).
passive___2__5__0__14([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, _),
        H==C,
        I==D,
        nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                  J),
        lookup_ht(J, k(F, G), K)
    ->  passive___2__5__1__14(K,
                              F,
                              G,
                              A,
                              B,
                              C,
                              D,
                              E)
    ;   passive___2__5__0__14(B, C, D, E)
    ).
passive___2__5__1__14([], _, _, _, A, B, C, D) :-
    passive___2__5__0__14(A, B, C, D).
passive___2__5__1__14([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K),
        J==C,
        K==D,
        L=t(362, E, I, A),
        '$novel_production'(E, L),
        '$novel_production'(I, L),
        '$novel_production'(A, L),
        D>1
    ->  '$extend_history'(I, L),
        arg(2, I, M),
        setarg(2, I, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      N),
            insert_ht(N, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      O),
            insert_ht(O, k(G, H), I)
        ;   true
        ),
        P is D-1,
        skip_to_next_id(C, P),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            passive___2__5__1__14(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I)
        ;   true
        )
    ;   passive___2__5__1__14(B,
                              C,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I)
    ).
passive___2__5(A, B, C) :-
    passive___2__6(A, B, C).
passive___2__6(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    passive___2__6__0__15(E, A, B, C).
passive___2__6__0__15([], A, B, C) :-
    passive___2__7(A, B, C).
passive___2__6__0__15([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, _),
        H==C,
        I==D,
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  J),
        lookup_ht(J, k(F, G), K)
    ->  passive___2__6__1__15(K,
                              F,
                              G,
                              A,
                              B,
                              C,
                              D,
                              E)
    ;   passive___2__6__0__15(B, C, D, E)
    ).
passive___2__6__1__15([], _, _, _, A, B, C, D) :-
    passive___2__6__0__15(A, B, C, D).
passive___2__6__1__15([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, J, K, L),
        J==C,
        K==D
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  M),
        delete_ht(M, k(C, D), A),
        arg(2, I, N),
        setarg(2, I, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                      O),
            insert_ht(O, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                      P),
            insert_ht(P, k(G, H), I)
        ;   true
        ),
        occurrence_code_id(C, D, L),
        Q is D+1,
        set_occurrence_code_id(C, Q, L),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            passive___2__6__1__15(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I)
        ;   true
        )
    ;   passive___2__6__1__15(B,
                              C,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I)
    ).
passive___2__6(A, B, C) :-
    passive___2__7(A, B, C).
passive___2__7(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1',
                  E),
        insert_ht(E, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  F),
        insert_ht(F, k(A, B), C)
    ;   true
    ).
is_passive(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
is_passive(_, _) :-
    fail.
any_passive_head(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, _),
    !.
any_passive_head(_) :-
    fail.
new_occurrence(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              E),
    lookup_ht(E, A, F),
    member(G, F),
    G=suspension(_, active, _, _, H),
    !,
    setarg(2, G, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              I),
    delete_ht(I, A, G),
    J is H+1,
    occurrence(A, J, B, C, D),
    max_occurrence(A, J).
new_occurrence(A, B, _, _) :-
    chr_error(internal,
              'new_occurrence: missing max_occurrence for ~w in rule ~w\n',
              [A, B]).
occurrence(A, B, C, D, E) :-
    occurrence___5__0(A, B, C, D, E, _).
occurrence___5__0(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', G),
    lookup_ht(G, C, H),
    !,
    F=suspension(I, not_stored_yet, t, _, A, B, C, D, E),
    'chr gen_id'(I),
    occurrence___5__0__0__4(H,
                            A,
                            B,
                            C,
                            D,
                            E,
                            F).
occurrence___5__0__0__4([], A, B, C, D, E, F) :-
    occurrence___5__1(A, B, C, D, E, F).
occurrence___5__0__0__4([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, J),
        I==E,
        nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                  K),
        lookup_ht(K, k(C, D), L)
    ->  occurrence___5__0__1__4(L,
                                J,
                                A,
                                B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H)
    ;   occurrence___5__0__0__4(B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H)
    ).
occurrence___5__0__1__4([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__0__0__4(A,
                            B,
                            C,
                            D,
                            E,
                            F,
                            G).
occurrence___5__0__1__4([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, L, M),
        L==F,
        M==G,
        N=t(60, D, K, A),
        '$novel_production'(D, N),
        '$novel_production'(K, N),
        '$novel_production'(A, N)
    ->  '$extend_history'(K, N),
        arg(2, K, O),
        setarg(2, K, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      P),
            insert_ht(P, k(F, J), K),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      Q),
            R=[K|Q],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      T),
            insert_ht(T, k(F, G, J), K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      U),
            insert_ht(U, k(F, H, I), K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      V),
            insert_ht(V, k(F, G), K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      W),
            insert_ht(W, H, K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      X),
            insert_ht(X, k(F, H), K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      Y),
            insert_ht(Y, k(H, I), K),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      Z),
            insert_ht(Z, F, K)
        ;   true
        ),
        \+ is_passive(H, I),
        J==propagation,
        (   stored_in_guard_before_next_kept_occurrence(F, G)
        ->  true
        ;   C=pragma(rule([_|_], _, _, _), _, _, _, _)
        ->  is_observed(F, G)
        ;   is_least_occurrence(H)
        ->  is_observed(F, G)
        ;   true
        ),
        (   K=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, K, inactive),
            occurrence___5__0__1__4(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H,
                                    I,
                                    J,
                                    K)
        ;   true
        )
    ;   occurrence___5__0__1__4(B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H,
                                I,
                                J,
                                K)
    ).
occurrence___5__0(A, B, C, D, E, F) :-
    F=suspension(G, not_stored_yet, t, _, A, B, C, D, E),
    'chr gen_id'(G),
    occurrence___5__1(A, B, C, D, E, F).
occurrence___5__1(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__1__0__7(H,
                            A,
                            B,
                            C,
                            D,
                            E,
                            F).
occurrence___5__1__0__7([], A, B, C, D, E, F) :-
    occurrence___5__2(A, B, C, D, E, F).
occurrence___5__1__0__7([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J),
        I==E,
        J=[K|L],
        K==F,
        ( ground(C),
          nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                    M),
          lookup_ht(M, C, N)
        ),
        member(O, N),
        O=suspension(_, active, _, _, P),
        P>=D,
        ( ground(E),
          nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                    Q),
          lookup_ht(Q, E, R)
        ),
        member(S, R),
        S=suspension(_, active, _, _, _, _),
        \+ may_trigger(C)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
                    T),
          delete_ht(T, k(E, [F|L]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  U),
        delete_ht(U, E, A),
        arg(2, H, V),
        setarg(2, H, active),
        (   V==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      W),
            insert_ht(W, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      X),
            Y=[H|X],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     Y),
            (   X=[Z|_]
            ->  setarg(4, Z, Y)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      A1),
            insert_ht(A1, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      B1),
            insert_ht(B1, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      C1),
            insert_ht(C1, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      D1),
            insert_ht(D1, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      E1),
            insert_ht(E1, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      F1),
            insert_ht(F1, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      G1),
            insert_ht(G1, C, H)
        ;   true
        ),
        least_occurrence(E, L),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__1__0__7(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H)
        ;   true
        )
    ;   occurrence___5__1__0__7(B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H)
    ).
occurrence___5__1(A, B, C, D, E, F) :-
    occurrence___5__2(A, B, C, D, E, F).
occurrence___5__2(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
              G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__2__0__29(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__2__0__29([], A, B, C, D, E, F) :-
    occurrence___5__3(A, B, C, D, E, F).
occurrence___5__2__0__29([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, I),
        I==E,
        J=t(180, A, H),
        '$novel_production'(A, J),
        '$novel_production'(H, J)
    ->  '$extend_history'(H, J),
        arg(2, H, K),
        setarg(2, H, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      L),
            insert_ht(L, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      M),
            N=[H|M],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     N),
            (   M=[O|_]
            ->  setarg(4, O, N)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      P),
            insert_ht(P, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      Q),
            insert_ht(Q, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      R),
            insert_ht(R, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      S),
            insert_ht(S, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      T),
            insert_ht(T, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      U),
            insert_ht(U, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      V),
            insert_ht(V, C, H)
        ;   true
        ),
        passive(E, F),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__2__0__29(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H)
        ;   true
        )
    ;   occurrence___5__2__0__29(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__2(A, B, C, D, E, F) :-
    occurrence___5__3(A, B, C, D, E, F).
occurrence___5__3(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__3__0__30(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__3__0__30([], A, B, C, D, E, F) :-
    occurrence___5__4(A, B, C, D, E, F).
occurrence___5__3__0__30([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, J),
        I==E,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  K),
        lookup_ht(K, k(C, E), L)
    ->  occurrence___5__3__1__30(L,
                                 J,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ;   occurrence___5__3__0__30(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__3__1__30([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__3__0__30(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G).
occurrence___5__3__1__30([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, _, L, M, N, _, _),
        L==F,
        N==H,
        nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                  O),
        lookup_ht(O, H, P)
    ->  occurrence___5__3__2__30(P,
                                 M,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ;   occurrence___5__3__1__30(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ).
occurrence___5__3__2__30([], _, _, A, B, C, D, E, F, G, H, I, J) :-
    occurrence___5__3__1__30(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J).
occurrence___5__3__2__30([A|B], C, D, E, F, G, H, I, J, K, L, M, N) :-
    (   A=suspension(_, active, _, _, O, _, _, _, _, _),
        O==K,
        nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                  P)
    ->  occurrence___5__3__3__30(P,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N)
    ;   occurrence___5__3__2__30(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N)
    ).
occurrence___5__3__3__30([], _, A, B, C, D, E, F, G, H, I, J, K, L, M) :-
    occurrence___5__3__2__30(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J,
                             K,
                             L,
                             M).
occurrence___5__3__3__30([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O, P) :-
    (   A=suspension(_, active, _, Q),
        L<E,
        chr_pp_flag(occurrence_subsumption, on),
        H=pragma(rule(R, _, _, _), _, _, _, _),
        R\==[],
        \+ tree_set_memberchk(K, Q)
    ->  setarg(2, A, removed),
        arg(3, A, S),
        (   var(S)
        ->  nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                      T),
            T=[_|U],
            b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                     U),
            (   U=[V|_]
            ->  setarg(3, V, _)
            ;   true
            )
        ;   S=[_, _|U],
            setarg(2, S, U),
            (   U=[V|_]
            ->  setarg(3, V, S)
            ;   true
            )
        ),
        arg(2, P, W),
        setarg(2, P, active),
        (   W==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      X),
            insert_ht(X, k(K, O), P),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      Y),
            Z=[P|Y],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     Z),
            (   Y=[A1|_]
            ->  setarg(4, A1, Z)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      B1),
            insert_ht(B1, k(K, L, O), P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      C1),
            insert_ht(C1, k(K, M, N), P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      D1),
            insert_ht(D1, k(K, L), P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      E1),
            insert_ht(E1, M, P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      F1),
            insert_ht(F1, k(K, M), P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      G1),
            insert_ht(G1, k(M, N), P),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      H1),
            insert_ht(H1, K, P)
        ;   true
        ),
        first_occ_in_rule(M, K, L, N),
        tree_set_add(Q, K, I1),
        multiple_occ_constraints_checked(I1),
        (   P=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, P, inactive),
            occurrence___5__3__3__30(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N,
                                     O,
                                     P)
        ;   true
        )
    ;   occurrence___5__3__3__30(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N,
                                 O,
                                 P)
    ).
occurrence___5__3(A, B, C, D, E, F) :-
    occurrence___5__4(A, B, C, D, E, F).
occurrence___5__4(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__4__0__31(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__4__0__31([], A, B, C, D, E, F) :-
    occurrence___5__5(A, B, C, D, E, F).
occurrence___5__4__0__31([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, J),
        I==E,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  K),
        lookup_ht(K, k(C, E), L)
    ->  occurrence___5__4__1__31(L,
                                 J,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ;   occurrence___5__4__0__31(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__4__1__31([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__4__0__31(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G).
occurrence___5__4__1__31([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, _, L, M, N, O, _),
        L==F,
        N==H,
        nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                  P),
        lookup_ht(P, H, Q)
    ->  occurrence___5__4__2__31(Q,
                                 M,
                                 O,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ;   occurrence___5__4__1__31(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ).
occurrence___5__4__2__31([], _, _, _, A, B, C, D, E, F, G, H, I, J) :-
    occurrence___5__4__1__31(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J).
occurrence___5__4__2__31([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O) :-
    (   A=suspension(_, active, _, _, P, _, _, _, _, _),
        P==L,
        nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                  Q)
    ->  occurrence___5__4__3__31(Q,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N,
                                 O)
    ;   occurrence___5__4__2__31(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N,
                                 O)
    ).
occurrence___5__4__3__31([], _, A, B, C, D, E, F, G, H, I, J, K, L, M, N) :-
    occurrence___5__4__2__31(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J,
                             K,
                             L,
                             M,
                             N).
occurrence___5__4__3__31([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q) :-
    (   A=suspension(_, active, _, R),
        E<M,
        chr_pp_flag(occurrence_subsumption, on),
        I=pragma(rule(S, _, _, _), _, _, _, _),
        S\==[],
        \+ tree_set_memberchk(L, R)
    ->  setarg(2, A, removed),
        arg(3, A, T),
        (   var(T)
        ->  nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                      U),
            U=[_|V],
            b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                     V),
            (   V=[W|_]
            ->  setarg(3, W, _)
            ;   true
            )
        ;   T=[_, _|V],
            setarg(2, T, V),
            (   V=[W|_]
            ->  setarg(3, W, T)
            ;   true
            )
        ),
        arg(2, Q, X),
        setarg(2, Q, active),
        (   X==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      Y),
            insert_ht(Y, k(L, P), Q),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      Z),
            A1=[Q|Z],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     A1),
            (   Z=[B1|_]
            ->  setarg(4, B1, A1)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      C1),
            insert_ht(C1, k(L, M, P), Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      D1),
            insert_ht(D1, k(L, N, O), Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      E1),
            insert_ht(E1, k(L, M), Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      F1),
            insert_ht(F1, N, Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      G1),
            insert_ht(G1, k(L, N), Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      H1),
            insert_ht(H1, k(N, O), Q),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      I1),
            insert_ht(I1, L, Q)
        ;   true
        ),
        first_occ_in_rule(N, L, E, F),
        tree_set_add(R, L, J1),
        multiple_occ_constraints_checked(J1),
        (   Q=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, Q, inactive),
            occurrence___5__4__3__31(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N,
                                     O,
                                     P,
                                     Q)
        ;   true
        )
    ;   occurrence___5__4__3__31(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N,
                                 O,
                                 P,
                                 Q)
    ).
occurrence___5__4(A, B, C, D, E, F) :-
    occurrence___5__5(A, B, C, D, E, F).
occurrence___5__5(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', G),
    lookup_ht(G, k(A, B, yes), H),
    !,
    occurrence___5__5__0__35(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__5__0__35([], A, B, C, D, E, F) :-
    occurrence___5__6(A, B, C, D, E, F).
occurrence___5__5__0__35([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K),
        I==C,
        J==D,
        K=yes,
        ( ground(E),
          ground(F),
          nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                    L),
          lookup_ht(L, k(E, F), M)
        ),
        member(N, M),
        N=suspension(_, active, _, _, _),
        ( ground(C),
          nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                    O),
          lookup_ht(O, C, P)
        ),
        member(Q, P),
        Q=suspension(_, active, _, _, R, S),
        D<R
    ->  ( setarg(2, A, removed),
          nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13',
                    T),
          delete_ht(T, k(C, yes), A),
          nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123',
                    U),
          delete_ht(U, k(C, D, yes), A)
        ),
        setarg(2, Q, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                    V),
          delete_ht(V, k(C, S), Q)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                  W),
        delete_ht(W, C, Q),
        arg(2, H, X),
        setarg(2, H, active),
        (   X==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      Y),
            insert_ht(Y, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      Z),
            A1=[H|Z],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     A1),
            (   Z=[B1|_]
            ->  setarg(4, B1, A1)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      C1),
            insert_ht(C1, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      D1),
            insert_ht(D1, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      E1),
            insert_ht(E1, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      F1),
            insert_ht(F1, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      G1),
            insert_ht(G1, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      H1),
            insert_ht(H1, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      I1),
            insert_ht(I1, C, H)
        ;   true
        ),
        J1 is S-1,
        stored(C, D, maybe),
        stored_complete(C, R, J1),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__5__0__35(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H)
        ;   true
        )
    ;   occurrence___5__5__0__35(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__5(A, B, C, D, E, F) :-
    occurrence___5__6(A, B, C, D, E, F).
occurrence___5__6(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__6__0__36(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__6__0__36([], A, B, C, D, E, F) :-
    occurrence___5__7(A, B, C, D, E, F).
occurrence___5__6__0__36([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, _, J, _, _),
        J==E,
        nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
                  K),
        lookup_ht(K, I, L)
    ->  occurrence___5__6__1__36(L,
                                 I,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ;   occurrence___5__6__0__36(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__6__1__36([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__6__0__36(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G).
occurrence___5__6__1__36([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, L, M),
        L==C,
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                  N),
        lookup_ht(N, k(C, 0), O)
    ->  occurrence___5__6__2__36(O,
                                 M,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ;   occurrence___5__6__1__36(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ).
occurrence___5__6__2__36([], _, _, A, B, C, D, E, F, G, H, I, J) :-
    occurrence___5__6__1__36(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J).
occurrence___5__6__2__36([A|B], C, D, E, F, G, H, I, J, K, L, M, N) :-
    (   A=suspension(_, active, _, O, P, Q),
        O==F,
        Q=0,
        R=t(250, N, G, A, D),
        '$novel_production'(N, R),
        '$novel_production'(G, R),
        '$novel_production'(A, R),
        '$novel_production'(D, R),
        P=<C
    ->  '$extend_history'(N, R),
        arg(2, N, S),
        setarg(2, N, active),
        (   S==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      T),
            insert_ht(T, k(I, M), N),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      U),
            V=[N|U],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     V),
            (   U=[W|_]
            ->  setarg(4, W, V)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      X),
            insert_ht(X, k(I, J, M), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      Y),
            insert_ht(Y, k(I, K, L), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      Z),
            insert_ht(Z, k(I, J), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      A1),
            insert_ht(A1, K, N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      B1),
            insert_ht(B1, k(I, K), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      C1),
            insert_ht(C1, k(K, L), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      D1),
            insert_ht(D1, I, N)
        ;   true
        ),
        passive(K, L),
        (   N=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, N, inactive),
            occurrence___5__6__2__36(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N)
        ;   true
        )
    ;   occurrence___5__6__2__36(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N)
    ).
occurrence___5__6(A, B, C, D, E, F) :-
    occurrence___5__7(A, B, C, D, E, F).
occurrence___5__7(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', G),
    lookup_ht(G, C, H),
    !,
    occurrence___5__7__0__37(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__7__0__37([], A, B, C, D, E, F) :-
    occurrence___5__8(A, B, C, D, E, F).
occurrence___5__7__0__37([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, _, _, I, J, _),
        I==E,
        nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
                  K),
        lookup_ht(K, C, L)
    ->  occurrence___5__7__1__37(L,
                                 J,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ;   occurrence___5__7__0__37(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__7__1__37([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__7__0__37(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G).
occurrence___5__7__1__37([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, L, M),
        L==F,
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                  N),
        lookup_ht(N, k(F, 0), O)
    ->  occurrence___5__7__2__37(O,
                                 M,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ;   occurrence___5__7__1__37(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K)
    ).
occurrence___5__7__2__37([], _, _, A, B, C, D, E, F, G, H, I, J) :-
    occurrence___5__7__1__37(A,
                             B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J).
occurrence___5__7__2__37([A|B], C, D, E, F, G, H, I, J, K, L, M, N) :-
    (   A=suspension(_, active, _, O, P, Q),
        O==I,
        Q=0,
        R=t(250, G, N, A, D),
        '$novel_production'(G, R),
        '$novel_production'(N, R),
        '$novel_production'(A, R),
        '$novel_production'(D, R),
        P=<C
    ->  '$extend_history'(N, R),
        arg(2, N, S),
        setarg(2, N, active),
        (   S==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      T),
            insert_ht(T, k(I, M), N),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      U),
            V=[N|U],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     V),
            (   U=[W|_]
            ->  setarg(4, W, V)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      X),
            insert_ht(X, k(I, J, M), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      Y),
            insert_ht(Y, k(I, K, L), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      Z),
            insert_ht(Z, k(I, J), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      A1),
            insert_ht(A1, K, N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      B1),
            insert_ht(B1, k(I, K), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      C1),
            insert_ht(C1, k(K, L), N),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      D1),
            insert_ht(D1, I, N)
        ;   true
        ),
        passive(K, F),
        (   N=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, N, inactive),
            occurrence___5__7__2__37(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N)
        ;   true
        )
    ;   occurrence___5__7__2__37(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N)
    ).
occurrence___5__7(A, B, C, D, E, F) :-
    occurrence___5__8(A, B, C, D, E, F).
occurrence___5__8(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              G),
    lookup_ht(G, A, H),
    !,
    occurrence___5__8__0__38(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__8__0__38([], A, B, C, D, E, F) :-
    occurrence___5__9(A, B, C, D, E, F).
occurrence___5__8__0__38([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, I, J, _),
        I==C,
        K=t(259, A, H),
        '$novel_production'(A, K),
        '$novel_production'(H, K),
        D>J
    ->  '$extend_history'(H, K),
        arg(2, H, L),
        setarg(2, H, active),
        (   L==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      M),
            insert_ht(M, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      N),
            O=[H|N],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     O),
            (   N=[P|_]
            ->  setarg(4, P, O)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      Q),
            insert_ht(Q, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      R),
            insert_ht(R, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      S),
            insert_ht(S, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      T),
            insert_ht(T, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      U),
            insert_ht(U, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      V),
            insert_ht(V, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      W),
            insert_ht(W, C, H)
        ;   true
        ),
        passive(E, F),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__8__0__38(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H)
        ;   true
        )
    ;   occurrence___5__8__0__38(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__8(A, B, C, D, E, F) :-
    occurrence___5__9(A, B, C, D, E, F).
occurrence___5__9(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
              G),
    lookup_ht(G, k(A, C), H),
    !,
    occurrence___5__9__0__42(H,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F).
occurrence___5__9__0__42([], A, B, C, D, E, F) :-
    occurrence___5__10(A, B, C, D, E, F).
occurrence___5__9__0__42([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K, L),
        I==C,
        J==E,
        E>1,
        ( ground(C),
          nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                    M),
          lookup_ht(M, C, N)
        ),
        member(O, N),
        O=suspension(_, active, _, _, P),
        P>D
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
                    Q),
          delete_ht(Q, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
                  R),
        delete_ht(R, k(C, E), A),
        arg(2, H, S),
        setarg(2, H, active),
        (   S==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      T),
            insert_ht(T, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      U),
            V=[H|U],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     V),
            (   U=[W|_]
            ->  setarg(4, W, V)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      X),
            insert_ht(X, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      Y),
            insert_ht(Y, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      Z),
            insert_ht(Z, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      A1),
            insert_ht(A1, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      B1),
            insert_ht(B1, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      C1),
            insert_ht(C1, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      D1),
            insert_ht(D1, C, H)
        ;   true
        ),
        functional_dependency(C, 1, K, L),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__9__0__42(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H)
        ;   true
        )
    ;   occurrence___5__9__0__42(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H)
    ).
occurrence___5__9(A, B, C, D, E, F) :-
    occurrence___5__10(A, B, C, D, E, F).
occurrence___5__10(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              G),
    lookup_ht(G, A, H),
    !,
    occurrence___5__10__0__48(H,
                              A,
                              B,
                              C,
                              D,
                              E,
                              F).
occurrence___5__10__0__48([], A, B, C, D, E, F) :-
    occurrence___5__11(A, B, C, D, E, F).
occurrence___5__10__0__48([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, J),
        I==C,
        nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                  K)
    ->  occurrence___5__10__1__48(K,
                                  J,
                                  A,
                                  B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ;   occurrence___5__10__0__48(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__10__1__48([], _, _, A, B, C, D, E, F, G) :-
    occurrence___5__10__0__48(A,
                              B,
                              C,
                              D,
                              E,
                              F,
                              G).
occurrence___5__10__1__48([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, _),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', L),
        lookup_ht(L, H, M)
    ->  occurrence___5__10__2__48(M,
                                  A,
                                  B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I,
                                  J,
                                  K)
    ;   occurrence___5__10__1__48(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I,
                                  J,
                                  K)
    ).
occurrence___5__10__2__48([], _, A, B, C, D, E, F, G, H, I, J) :-
    occurrence___5__10__1__48(A,
                              B,
                              C,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I,
                              J).
occurrence___5__10__2__48([A|B], C, D, E, F, G, H, I, J, K, L, M) :-
    (   A=suspension(_, active, _, _, N, O),
        N==J,
        P=t(330, C, M, A, F),
        '$novel_production'(C, P),
        '$novel_production'(M, P),
        '$novel_production'(A, P),
        '$novel_production'(F, P)
    ->  '$extend_history'(M, P),
        arg(2, M, Q),
        setarg(2, M, active),
        (   Q==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      R),
            insert_ht(R, k(H, L), M),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      S),
            T=[M|S],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     T),
            (   S=[U|_]
            ->  setarg(4, U, T)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      V),
            insert_ht(V, k(H, I, L), M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      W),
            insert_ht(W, k(H, J, K), M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      X),
            insert_ht(X, k(H, I), M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      Y),
            insert_ht(Y, J, M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      Z),
            insert_ht(Z, k(H, J), M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      A1),
            insert_ht(A1, k(J, K), M),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      B1),
            insert_ht(B1, H, M)
        ;   true
        ),
        O=pragma(rule(C1, D1, E1, _), _, _, _, _),
        (   is_passive(J, K)
        ->  true
        ;   L==simplification
        ->  select(F1, C1, _),
            F1=..[_|G1],
            term_variables(E1, H1),
            partial_wake_args(G1, E, H1, H)
        ;   select(F1, D1, _),
            F1=..[_|G1],
            term_variables(E1, H1),
            partial_wake_args(G1, E, H1, H)
        ),
        (   M=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, M, inactive),
            occurrence___5__10__2__48(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H,
                                      I,
                                      J,
                                      K,
                                      L,
                                      M)
        ;   true
        )
    ;   occurrence___5__10__2__48(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I,
                                  J,
                                  K,
                                  L,
                                  M)
    ).
occurrence___5__10(A, B, C, D, E, F) :-
    occurrence___5__11(A, B, C, D, E, F).
occurrence___5__11(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', G),
    lookup_ht(G, k(C, D), H),
    !,
    occurrence___5__11__0__50(H,
                              A,
                              B,
                              C,
                              D,
                              E,
                              F).
occurrence___5__11__0__50([], A, B, C, D, E, F) :-
    occurrence___5__12(A, B, C, D, E, F).
occurrence___5__11__0__50([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, I, J),
        I==E,
        J==F,
        nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                  K),
        lookup_ht(K, k(C, D), L)
    ->  occurrence___5__11__1__50(L,
                                  A,
                                  B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ;   occurrence___5__11__0__50(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__11__1__50([], _, A, B, C, D, E, F, G) :-
    occurrence___5__11__0__50(A,
                              B,
                              C,
                              D,
                              E,
                              F,
                              G).
occurrence___5__11__1__50([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, K, L),
        K==E,
        L==F,
        M=t(362, J, C, A),
        '$novel_production'(J, M),
        '$novel_production'(C, M),
        '$novel_production'(A, M),
        F>1
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      O),
            insert_ht(O, k(E, I), J),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      P),
            Q=[J|P],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      S),
            insert_ht(S, k(E, F, I), J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      T),
            insert_ht(T, k(E, G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      U),
            insert_ht(U, k(E, F), J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      V),
            insert_ht(V, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      W),
            insert_ht(W, k(E, G), J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      X),
            insert_ht(X, k(G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      Y),
            insert_ht(Y, E, J)
        ;   true
        ),
        Z is F-1,
        skip_to_next_id(E, Z),
        (   J=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            occurrence___5__11__1__50(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H,
                                      I,
                                      J)
        ;   true
        )
    ;   occurrence___5__11__1__50(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H,
                                  I,
                                  J)
    ).
occurrence___5__11(A, B, C, D, E, F) :-
    occurrence___5__12(A, B, C, D, E, F).
occurrence___5__12(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              G),
    lookup_ht(G, k(A, B), H),
    !,
    occurrence___5__12__0__51(H,
                              A,
                              B,
                              C,
                              D,
                              E,
                              F).
occurrence___5__12__0__51([], A, B, C, D, E, F) :-
    occurrence___5__13(A, B, C, D, E, F).
occurrence___5__12__0__51([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K),
        I==C,
        J==D,
        ( ground(E),
          ground(F)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  L),
        lookup_ht(L, k(E, F), M),
        member(N, M),
        N=suspension(_, active, _, _, _)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  O),
        delete_ht(O, k(C, D), A),
        arg(2, H, P),
        setarg(2, H, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      Q),
            insert_ht(Q, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      R),
            S=[H|R],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     S),
            (   R=[T|_]
            ->  setarg(4, T, S)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      U),
            insert_ht(U, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      V),
            insert_ht(V, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      W),
            insert_ht(W, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      X),
            insert_ht(X, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      Y),
            insert_ht(Y, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      Z),
            insert_ht(Z, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      A1),
            insert_ht(A1, C, H)
        ;   true
        ),
        occurrence_code_id(C, D, K),
        B1 is D+1,
        set_occurrence_code_id(C, B1, K),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__12__0__51(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H)
        ;   true
        )
    ;   occurrence___5__12__0__51(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__12(A, B, C, D, E, F) :-
    occurrence___5__13(A, B, C, D, E, F).
occurrence___5__13(A, B, C, D, simplification, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              F),
    lookup_ht(F, k(A, B), G),
    !,
    occurrence___5__13__0__52(G,
                              A,
                              B,
                              C,
                              D,
                              simplification,
                              E).
occurrence___5__13__0__52([], A, B, C, D, E, F) :-
    occurrence___5__15(A, B, C, D, E, F).
occurrence___5__13__0__52([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K),
        I==C,
        J==D
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  L),
        delete_ht(L, k(C, D), A),
        arg(2, H, M),
        setarg(2, H, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      N),
            insert_ht(N, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      O),
            P=[H|O],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      R),
            insert_ht(R, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      S),
            insert_ht(S, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      T),
            insert_ht(T, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      U),
            insert_ht(U, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      V),
            insert_ht(V, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      W),
            insert_ht(W, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      X),
            insert_ht(X, C, H)
        ;   true
        ),
        occurrence_code_id(C, D, K),
        Y is D+1,
        set_occurrence_code_id(C, Y, K),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__13__0__52(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H)
        ;   true
        )
    ;   occurrence___5__13__0__52(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__13(A, B, C, D, propagation, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              F),
    lookup_ht(F, k(A, B), G),
    !,
    occurrence___5__13__0__53(G,
                              A,
                              B,
                              C,
                              D,
                              propagation,
                              E).
occurrence___5__13__0__53([], A, B, C, D, E, F) :-
    occurrence___5__14(A, B, C, D, E, F).
occurrence___5__13__0__53([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K),
        I==C,
        J==D,
        ( ground(C),
          ground(D)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                  L),
        lookup_ht(L, k(C, D), M),
        member(N, M),
        N=suspension(_, active, _, _, _)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  O),
        delete_ht(O, k(C, D), A),
        arg(2, H, P),
        setarg(2, H, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      Q),
            insert_ht(Q, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      R),
            S=[H|R],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     S),
            (   R=[T|_]
            ->  setarg(4, T, S)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      U),
            insert_ht(U, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      V),
            insert_ht(V, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      W),
            insert_ht(W, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      X),
            insert_ht(X, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      Y),
            insert_ht(Y, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      Z),
            insert_ht(Z, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      A1),
            insert_ht(A1, C, H)
        ;   true
        ),
        occurrence_code_id(C, D, K),
        B1 is D+1,
        C1 is K+1,
        set_occurrence_code_id(C, B1, C1),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__13__0__53(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H)
        ;   true
        )
    ;   occurrence___5__13__0__53(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__13(A, B, C, D, E, F) :-
    occurrence___5__14(A, B, C, D, E, F).
occurrence___5__14(A, B, C, D, propagation, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              F),
    lookup_ht(F, k(A, B), G),
    !,
    occurrence___5__14__0__54(G,
                              A,
                              B,
                              C,
                              D,
                              propagation,
                              E).
occurrence___5__14__0__54([], A, B, C, D, E, F) :-
    occurrence___5__15(A, B, C, D, E, F).
occurrence___5__14__0__54([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, I, J, K),
        I==C,
        J==D
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  L),
        delete_ht(L, k(C, D), A),
        arg(2, H, M),
        setarg(2, H, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                      N),
            insert_ht(N, k(C, G), H),
            nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                      O),
            P=[H|O],
            b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                      R),
            insert_ht(R, k(C, D, G), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                      S),
            insert_ht(S, k(C, E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                      T),
            insert_ht(T, k(C, D), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                      U),
            insert_ht(U, E, H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                      V),
            insert_ht(V, k(C, E), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                      W),
            insert_ht(W, k(E, F), H),
            nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                      X),
            insert_ht(X, C, H)
        ;   true
        ),
        occurrence_code_id(C, D, K),
        Y is D+1,
        set_occurrence_code_id(C, Y, K),
        (   H=suspension(_, active, _, _, _, _, _, _, _)
        ->  setarg(2, H, inactive),
            occurrence___5__14__0__54(B,
                                      C,
                                      D,
                                      E,
                                      F,
                                      G,
                                      H)
        ;   true
        )
    ;   occurrence___5__14__0__54(B,
                                  C,
                                  D,
                                  E,
                                  F,
                                  G,
                                  H)
    ).
occurrence___5__14(A, B, C, D, E, F) :-
    occurrence___5__15(A, B, C, D, E, F).
occurrence___5__15(A, B, C, D, E, F) :-
    arg(2, F, G),
    setarg(2, F, active),
    (   G==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
                  H),
        insert_ht(H, k(A, E), F),
        nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
                  I),
        J=[F|I],
        b_setval('$chr_store_global_ground_chr_translate____occurrence___5',
                 J),
        (   I=[K|_]
        ->  setarg(4, K, J)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                  L),
        insert_ht(L, k(A, B, E), F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
                  M),
        insert_ht(M, k(A, C, D), F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
                  N),
        insert_ht(N, k(A, B), F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  O),
        insert_ht(O, C, F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  P),
        insert_ht(P, k(A, C), F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                  Q),
        insert_ht(Q, k(C, D), F),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                  R),
        insert_ht(R, A, F)
    ;   true
    ).
get_occurrence(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              E),
    lookup_ht(E, k(A, B), F),
    member(G, F),
    G=suspension(_, active, _, _, _, _, H, I, _),
    !,
    H=C,
    I=D.
get_occurrence(A, B, _, _) :-
    chr_error(internal, 'get_occurrence: missing occurrence ~w:~w\n', [A, B]).
get_occurrence(A, B, C, D, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              F),
    lookup_ht(F, k(A, B), G),
    member(H, G),
    H=suspension(_, active, _, _, _, _, I, J, K),
    !,
    I=C,
    J=D,
    K=E.
get_occurrence(A, B, _, _, _) :-
    chr_error(internal, 'get_occurrence: missing occurrence ~w:~w\n', [A, B]).
get_occurrence_from_id(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              E),
    lookup_ht(E, k(C, D), F),
    member(G, F),
    G=suspension(_, active, _, _, H, I, _, _, _),
    !,
    A=H,
    B=I.
get_occurrence_from_id(_, _, _, _) :-
    chr_error(internal, 'get_occurrence_from_id: missing occurrence ~w:~w\n', []).
max_occurrence(A, B) :-
    max_occurrence___2__0(A, B, _).
max_occurrence___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    (   member(F, E),
        F=suspension(_, active, _, _, G),
        G>=B,
        !
    ;   !,
        max_occurrence___2__0__0__2(E, A, B, C)
    ).
max_occurrence___2__0__0__2([], A, B, C) :-
    max_occurrence___2__1(A, B, C).
max_occurrence___2__0__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==C,
        D>=G
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
                  H),
        delete_ht(H, C, A),
        max_occurrence___2__0__0__2(B, C, D, E)
    ;   max_occurrence___2__0__0__2(B, C, D, E)
    ).
max_occurrence___2__0(A, B, C) :-
    max_occurrence___2__1(A, B, C).
max_occurrence___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              D),
    lookup_ht(D, k(A, 0), E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    max_occurrence___2__1__0__5(E, A, B, C).
max_occurrence___2__1__0__5([], A, B, C) :-
    max_occurrence___2__2(A, B, C).
max_occurrence___2__1__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H),
        F==C,
        H=0,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                  I),
        lookup_ht(I, C, J)
    ->  max_occurrence___2__1__1__5(J,
                                    G,
                                    A,
                                    B,
                                    C,
                                    D,
                                    E)
    ;   max_occurrence___2__1__0__5(B, C, D, E)
    ).
max_occurrence___2__1__1__5([], _, _, A, B, C, D) :-
    max_occurrence___2__1__0__5(A, B, C, D).
max_occurrence___2__1__1__5([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, _, I, _, J, _, _),
        I==F,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  K),
        lookup_ht(K, J, L)
    ->  max_occurrence___2__1__2__5(L,
                                    J,
                                    A,
                                    B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H)
    ;   max_occurrence___2__1__1__5(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H)
    ).
max_occurrence___2__1__2__5([], _, _, A, B, C, D, E, F, G) :-
    max_occurrence___2__1__1__5(A,
                                B,
                                C,
                                D,
                                E,
                                F,
                                G).
max_occurrence___2__1__2__5([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, _, _, _, L, M, _),
        A\==D,
        L==C,
        N=t(250, A, D, G, K),
        '$novel_production'(A, N),
        '$novel_production'(D, N),
        '$novel_production'(G, N),
        '$novel_production'(K, N),
        F=<J
    ->  '$extend_history'(K, N),
        arg(2, K, O),
        setarg(2, K, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
                      P),
            insert_ht(P, I, K)
        ;   true
        ),
        passive(C, M),
        (   K=suspension(_, active, _, _, _)
        ->  setarg(2, K, inactive),
            max_occurrence___2__1__2__5(B,
                                        C,
                                        D,
                                        E,
                                        F,
                                        G,
                                        H,
                                        I,
                                        J,
                                        K)
        ;   true
        )
    ;   max_occurrence___2__1__2__5(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H,
                                    I,
                                    J,
                                    K)
    ).
max_occurrence___2__1(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    max_occurrence___2__2(A, B, C).
max_occurrence___2__2(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
get_max_occurrence(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F),
    !,
    B=F.
get_max_occurrence(A, _) :-
    chr_error(internal, 'get_max_occurrence: missing max occurrence for ~w\n', [A]).
allocation_occurrence(A, B) :-
    allocation_occurrence___2__0(A, B, _).
allocation_occurrence___2__0(A, 0, B) :-
    !,
    B=suspension(C, active, t, A, 0),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              D),
    insert_ht(D, A, B),
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
              E),
    insert_ht(E, k(A, 0), B),
    stored_in_guard_before_next_kept_occurrence(A, 0),
    (   B=suspension(_, active, _, _, _)
    ->  setarg(2, B, inactive),
        allocation_occurrence___2__1(A, 0, B)
    ;   true
    ).
allocation_occurrence___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    allocation_occurrence___2__1(A, B, C).
allocation_occurrence___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    allocation_occurrence___2__1__0__2(E, A, B, C).
allocation_occurrence___2__1__0__2([], A, B, C) :-
    allocation_occurrence___2__2(A, B, C).
allocation_occurrence___2__1__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, J),
        F==C,
        G==D,
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', K),
        lookup_ht(K, H, L)
    ->  allocation_occurrence___2__1__1__2(L,
                                           H,
                                           I,
                                           J,
                                           A,
                                           B,
                                           C,
                                           D,
                                           E)
    ;   allocation_occurrence___2__1__0__2(B, C, D, E)
    ).
allocation_occurrence___2__1__1__2([], _, _, _, _, A, B, C, D) :-
    allocation_occurrence___2__1__0__2(A, B, C, D).
allocation_occurrence___2__1__1__2([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _, K, L),
        K==C,
        M=t(60, A, F, J),
        '$novel_production'(A, M),
        '$novel_production'(F, M),
        '$novel_production'(J, M)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                      O),
            insert_ht(O, H, J),
            nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                      P),
            insert_ht(P, k(H, I), J)
        ;   true
        ),
        \+ is_passive(C, D),
        E==propagation,
        (   stored_in_guard_before_next_kept_occurrence(H, I)
        ->  true
        ;   L=pragma(rule([_|_], _, _, _), _, _, _, _)
        ->  is_observed(H, I)
        ;   is_least_occurrence(C)
        ->  is_observed(H, I)
        ;   true
        ),
        (   J=suspension(_, active, _, _, _)
        ->  setarg(2, J, inactive),
            allocation_occurrence___2__1__1__2(B,
                                               C,
                                               D,
                                               E,
                                               F,
                                               G,
                                               H,
                                               I,
                                               J)
        ;   true
        )
    ;   allocation_occurrence___2__1__1__2(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I,
                                           J)
    ).
allocation_occurrence___2__1(A, B, C) :-
    allocation_occurrence___2__2(A, B, C).
allocation_occurrence___2__2(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', D),
    lookup_ht(D, A, E),
    !,
    allocation_occurrence___2__2__0__3(E, A, B, C).
allocation_occurrence___2__2__0__3([], A, B, C) :-
    allocation_occurrence___2__3(A, B, C).
allocation_occurrence___2__2__0__3([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, _),
        F==C,
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  J),
        lookup_ht(J, H, K)
    ->  allocation_occurrence___2__2__1__3(K,
                                           G,
                                           H,
                                           I,
                                           A,
                                           B,
                                           C,
                                           D,
                                           E)
    ;   allocation_occurrence___2__2__0__3(B, C, D, E)
    ).
allocation_occurrence___2__2__1__3([], _, _, _, _, A, B, C, D) :-
    allocation_occurrence___2__2__0__3(A, B, C, D).
allocation_occurrence___2__2__1__3([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, K, L),
        K==D,
        L=[M|N],
        M==E,
        I>=C,
        ( ground(D),
          nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                    O),
          lookup_ht(O, D, P)
        ),
        member(Q, P),
        Q=suspension(_, active, _, _, _, _),
        \+ may_trigger(H)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
                    R),
          delete_ht(R, k(D, [E|N]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  S),
        delete_ht(S, D, A),
        arg(2, J, T),
        setarg(2, J, active),
        (   T==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                      U),
            insert_ht(U, H, J),
            nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                      V),
            insert_ht(V, k(H, I), J)
        ;   true
        ),
        least_occurrence(D, N),
        (   J=suspension(_, active, _, _, _)
        ->  setarg(2, J, inactive),
            allocation_occurrence___2__2__1__3(B,
                                               C,
                                               D,
                                               E,
                                               F,
                                               G,
                                               H,
                                               I,
                                               J)
        ;   true
        )
    ;   allocation_occurrence___2__2__1__3(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I,
                                           J)
    ).
allocation_occurrence___2__2(A, B, C) :-
    allocation_occurrence___2__3(A, B, C).
allocation_occurrence___2__3(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              D),
    lookup_ht(D, A, E),
    !,
    allocation_occurrence___2__3__0__5(E, A, B, C).
allocation_occurrence___2__3__0__5([], A, B, C) :-
    allocation_occurrence___2__4(A, B, C).
allocation_occurrence___2__3__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G, H, I),
        F==C,
        G>1,
        ( ground(C),
          ground(G),
          nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                    J),
          lookup_ht(J, k(C, G), K)
        ),
        member(L, K),
        L=suspension(_, active, _, _, _, M, _, _, _),
        D>M
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
                    N),
          delete_ht(N, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
                  O),
        delete_ht(O, k(C, G), A),
        arg(2, E, P),
        setarg(2, E, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                      Q),
            insert_ht(Q, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                      R),
            insert_ht(R, k(C, D), E)
        ;   true
        ),
        functional_dependency(C, 1, H, I),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            allocation_occurrence___2__3__0__5(B,
                                               C,
                                               D,
                                               E)
        ;   true
        )
    ;   allocation_occurrence___2__3__0__5(B, C, D, E)
    ).
allocation_occurrence___2__3(A, B, C) :-
    allocation_occurrence___2__4(A, B, C).
allocation_occurrence___2__4(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                  E),
        insert_ht(E, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                  F),
        insert_ht(F, k(A, B), C)
    ;   true
    ).
get_allocation_occurrence(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F),
    !,
    B=F.
get_allocation_occurrence(_, A) :-
    chr_pp_flag(late_allocation, off),
    A=0.
rule(A, B) :-
    rule___2__0(A, B, _).
rule___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, t, _, A, B),
    'chr gen_id'(F),
    rule___2__0__0__1(E, A, B, C).
rule___2__0__0__1([], A, B, C) :-
    rule___2__1(A, B, C).
rule___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, J),
        H==C,
        nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-12',
                  K),
        lookup_ht(K, k(F, G), L)
    ->  rule___2__0__1__1(L,
                          F,
                          G,
                          I,
                          J,
                          A,
                          B,
                          C,
                          D,
                          E)
    ;   rule___2__0__0__1(B, C, D, E)
    ).
rule___2__0__1__1([], _, _, _, _, _, A, B, C, D) :-
    rule___2__0__0__1(A, B, C, D).
rule___2__0__1__1([A|B], C, D, E, F, G, H, I, J, K) :-
    (   A=suspension(_, active, _, L, M),
        L==C,
        M==D,
        N=t(60, K, G, A),
        '$novel_production'(K, N),
        '$novel_production'(G, N),
        '$novel_production'(A, N)
    ->  '$extend_history'(K, N),
        arg(2, K, O),
        setarg(2, K, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      P),
            Q=[K|P],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      S),
            insert_ht(S, I, K)
        ;   true
        ),
        \+ is_passive(I, E),
        F==propagation,
        (   stored_in_guard_before_next_kept_occurrence(C, D)
        ->  true
        ;   J=pragma(rule([_|_], _, _, _), _, _, _, _)
        ->  is_observed(C, D)
        ;   is_least_occurrence(I)
        ->  is_observed(C, D)
        ;   true
        ),
        (   K=suspension(_, active, _, _, _, _)
        ->  setarg(2, K, inactive),
            rule___2__0__1__1(B,
                              C,
                              D,
                              E,
                              F,
                              G,
                              H,
                              I,
                              J,
                              K)
        ;   true
        )
    ;   rule___2__0__1__1(B,
                          C,
                          D,
                          E,
                          F,
                          G,
                          H,
                          I,
                          J,
                          K)
    ).
rule___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, t, _, A, B),
    'chr gen_id'(D),
    rule___2__1(A, B, C).
rule___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    !,
    rule___2__1__0__2(E, A, B, C).
rule___2__1__0__2([], A, B, C) :-
    rule___2__2(A, B, C).
rule___2__1__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=[H|I],
        ( ground(C),
          ground(H),
          nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                    J),
          lookup_ht(J, k(C, H), K)
        ),
        member(L, K),
        L=suspension(_, active, _, _, M, N, _, _, _),
        ( nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                    O),
          lookup_ht(O, M, P)
        ),
        member(Q, P),
        Q=suspension(_, active, _, _, R),
        R>=N,
        \+ may_trigger(M)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
                    S),
          delete_ht(S, k(C, [H|I]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  T),
        delete_ht(T, C, A),
        arg(2, E, U),
        setarg(2, E, active),
        (   U==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      V),
            W=[E|V],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     W),
            (   V=[X|_]
            ->  setarg(4, X, W)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      Y),
            insert_ht(Y, C, E)
        ;   true
        ),
        least_occurrence(C, I),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__1__0__2(B, C, D, E)
        ;   true
        )
    ;   rule___2__1__0__2(B, C, D, E)
    ).
rule___2__1(A, B, C) :-
    rule___2__2(A, B, C).
rule___2__2(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    !,
    rule___2__2__0__3(E, A, B, C).
rule___2__2__0__3([], A, B, C) :-
    rule___2__3(A, B, C).
rule___2__2__0__3([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=[H|I],
        ( ground(C),
          ground(H)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  J),
        lookup_ht(J, k(C, H), K),
        member(L, K),
        L=suspension(_, active, _, _, _)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
                    M),
          delete_ht(M, k(C, [H|I]), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
                  N),
        delete_ht(N, C, A),
        arg(2, E, O),
        setarg(2, E, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      P),
            Q=[E|P],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      S),
            insert_ht(S, C, E)
        ;   true
        ),
        least_occurrence(C, I),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__2__0__3(B, C, D, E)
        ;   true
        )
    ;   rule___2__2__0__3(B, C, D, E)
    ).
rule___2__2(A, B, C) :-
    rule___2__3(A, B, C).
rule___2__3(A, B, C) :-
    B=pragma(rule([], _, _, _), ids([], D), _, _, _),
    !,
    arg(2, C, E),
    setarg(2, C, active),
    (   E==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2', F),
        G=[C|F],
        b_setval('$chr_store_global_ground_chr_translate____rule___2', G),
        (   F=[H|_]
        ->  setarg(4, H, G)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', I),
        insert_ht(I, A, C)
    ;   true
    ),
    least_occurrence(A, D),
    (   C=suspension(_, active, _, _, _, _)
    ->  setarg(2, C, inactive),
        rule___2__4(A, B, C)
    ;   true
    ).
rule___2__3(A, B, C) :-
    rule___2__4(A, B, C).
rule___2__4(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, _, G, H, I, J, _),
    B=pragma(rule(K, L, H, M), N, O, P, A),
    H\==true,
    append(J, I, Q),
    (   conj2list(H, R),
        append(Q, R, S),
        guard_entailment:entails_guard(S, fail)
    ->  T=fail
    ;   simplify_guard(H, M, Q, T, U)
    ),
    H\==T,
    !,
    setarg(2, F, removed),
    arg(4, F, V),
    (   var(V)
    ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                  W),
        W=[_|X],
        b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                 X),
        (   X=[Y|_]
        ->  setarg(4, Y, _)
        ;   true
        )
    ;   V=[_, _|X],
        setarg(2, V, X),
        (   X=[Y|_]
        ->  setarg(4, Y, V)
        ;   true
        )
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              Z),
    delete_ht(Z, A, F),
    (   var(C)
    ->  true
    ;   arg(2, C, A1),
        setarg(2, C, removed),
        (   A1==not_stored_yet
        ->  true
        ;   arg(4, C, B1),
            (   var(B1)
            ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                          C1),
                C1=[_|D1],
                b_setval('$chr_store_global_ground_chr_translate____rule___2',
                         D1),
                (   D1=[E1|_]
                ->  setarg(4, E1, _)
                ;   true
                )
            ;   B1=[_, _|D1],
                setarg(2, B1, D1),
                (   D1=[E1|_]
                ->  setarg(4, E1, B1)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      F1),
            delete_ht(F1, A, C)
        )
    ),
    rule(A,
         pragma(rule(K, L, T, U),
                N,
                O,
                P,
                A)),
    prev_guard_list(A, G, T, I, J, []).
rule___2__4(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
              D),
    !,
    rule___2__4__0__10(D, A, B, C).
rule___2__4__0__10([], A, B, C) :-
    rule___2__5(A, B, C).
rule___2__4__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _),
        F=t(174, E, A),
        '$novel_production'(E, F),
        '$novel_production'(A, F)
    ->  '$extend_history'(E, F),
        arg(2, E, G),
        setarg(2, E, active),
        (   G==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      H),
            I=[E|H],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     I),
            (   H=[J|_]
            ->  setarg(4, J, I)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      K),
            insert_ht(K, C, E)
        ;   true
        ),
        D=pragma(rule(L, M, _, _), _, _, _, _),
        append(L, M, N),
        make_head_matchings_explicit_(N, O, P),
        copy_term_nat(O-P, Q-R),
        make_head_matchings_explicit_memo_table(C, Q, R),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__4__0__10(B, C, D, E)
        ;   true
        )
    ;   rule___2__4__0__10(B, C, D, E)
    ).
rule___2__4(A, B, C) :-
    rule___2__5(A, B, C).
rule___2__5(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              D),
    lookup_ht(D, A, E),
    !,
    rule___2__5__0__11(E, A, B, C).
rule___2__5__0__11([], A, B, C) :-
    rule___2__6(A, B, C).
rule___2__5__0__11([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, I, _),
        F==C,
        J=t(178, A, E),
        '$novel_production'(A, J),
        '$novel_production'(E, J),
        chr_pp_flag(check_impossible_rules, on),
        D=pragma(rule(_, _, G, _), _, _, _, C),
        conj2list(G, K),
        append(I, H, L),
        append(L, K, M),
        guard_entailment:entails_guard(M, fail)
    ->  '$extend_history'(E, J),
        arg(2, E, N),
        setarg(2, E, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      O),
            P=[E|O],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      R),
            insert_ht(R, C, E)
        ;   true
        ),
        chr_warning(weird_program,
                    'Heads will never match or guard will always fail in ~@.\n\tThis rule will never fire!\n',
                    [format_rule(D)]),
        set_all_passive(C),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__5__0__11(B, C, D, E)
        ;   true
        )
    ;   rule___2__5__0__11(B, C, D, E)
    ).
rule___2__5(A, B, C) :-
    rule___2__6(A, B, C).
rule___2__6(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, _, G, H, I, J, _),
    B=pragma(rule(K, L, H, M), N, O, P, A),
    simplify_heads(J, I, H, M, Q, R),
    Q\==[],
    extract_arguments(K, S),
    extract_arguments(L, T),
    extract_arguments(G, U),
    replace_some_heads(S,
                       T,
                       U,
                       Q,
                       V,
                       W,
                       H,
                       M,
                       X),
    substitute_arguments(K, V, Y),
    substitute_arguments(L, W, Z),
    append(R, X, A1),
    list2conj(A1, B1),
    C1=pragma(rule(Y, Z, H, (B1, M)), N, O, P, A),
    (   K\==Y
    ;   L\==Z
    ),
    !,
    (   var(C)
    ->  true
    ;   arg(2, C, D1),
        setarg(2, C, removed),
        (   D1==not_stored_yet
        ->  true
        ;   arg(4, C, E1),
            (   var(E1)
            ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                          F1),
                F1=[_|G1],
                b_setval('$chr_store_global_ground_chr_translate____rule___2',
                         G1),
                (   G1=[H1|_]
                ->  setarg(4, H1, _)
                ;   true
                )
            ;   E1=[_, _|G1],
                setarg(2, E1, G1),
                (   G1=[H1|_]
                ->  setarg(4, H1, E1)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      I1),
            delete_ht(I1, A, C)
        )
    ),
    rule(A, C1).
rule___2__6(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    rule___2__6__0__13(E, A, B, C).
rule___2__6__0__13([], A, B, C) :-
    rule___2__7(A, B, C).
rule___2__6__0__13([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, _, _),
        H==C,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  I),
        lookup_ht(I, k(F, C), J)
    ->  rule___2__6__1__13(J,
                           F,
                           G,
                           A,
                           B,
                           C,
                           D,
                           E)
    ;   rule___2__6__0__13(B, C, D, E)
    ).
rule___2__6__1__13([], _, _, _, A, B, C, D) :-
    rule___2__6__0__13(A, B, C, D).
rule___2__6__1__13([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K, L, M, _),
        A\==E,
        J==C,
        L==G,
        nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                  N),
        lookup_ht(N, G, O)
    ->  rule___2__6__2__13(O,
                           K,
                           M,
                           A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G,
                           H,
                           I)
    ;   rule___2__6__1__13(B,
                           C,
                           D,
                           E,
                           F,
                           G,
                           H,
                           I)
    ).
rule___2__6__2__13([], _, _, _, A, B, C, D, E, F, G, H) :-
    rule___2__6__1__13(A,
                       B,
                       C,
                       D,
                       E,
                       F,
                       G,
                       H).
rule___2__6__2__13([A|B], C, D, E, F, G, H, I, J, K, L, M) :-
    (   A=suspension(_, active, _, _, N, _, _, _, _, _),
        N==K,
        nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                  O)
    ->  rule___2__6__3__13(O,
                           A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M)
    ;   rule___2__6__2__13(B,
                           C,
                           D,
                           E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M)
    ).
rule___2__6__3__13([], _, A, B, C, D, E, F, G, H, I, J, K, L) :-
    rule___2__6__2__13(A,
                       B,
                       C,
                       D,
                       E,
                       F,
                       G,
                       H,
                       I,
                       J,
                       K,
                       L).
rule___2__6__3__13([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O) :-
    (   A=suspension(_, active, _, P),
        E<J,
        chr_pp_flag(occurrence_subsumption, on),
        N=pragma(rule(Q, _, _, _), _, _, _, _),
        Q\==[],
        \+ tree_set_memberchk(I, P)
    ->  setarg(2, A, removed),
        arg(3, A, R),
        (   var(R)
        ->  nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                      S),
            S=[_|T],
            b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                     T),
            (   T=[U|_]
            ->  setarg(3, U, _)
            ;   true
            )
        ;   R=[_, _|T],
            setarg(2, R, T),
            (   T=[U|_]
            ->  setarg(3, U, R)
            ;   true
            )
        ),
        arg(2, O, V),
        setarg(2, O, active),
        (   V==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      W),
            X=[O|W],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     X),
            (   W=[Y|_]
            ->  setarg(4, Y, X)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      Z),
            insert_ht(Z, M, O)
        ;   true
        ),
        first_occ_in_rule(M, I, E, F),
        tree_set_add(P, I, A1),
        multiple_occ_constraints_checked(A1),
        (   O=suspension(_, active, _, _, _, _)
        ->  setarg(2, O, inactive),
            rule___2__6__3__13(B,
                               C,
                               D,
                               E,
                               F,
                               G,
                               H,
                               I,
                               J,
                               K,
                               L,
                               M,
                               N,
                               O)
        ;   true
        )
    ;   rule___2__6__3__13(B,
                           C,
                           D,
                           E,
                           F,
                           G,
                           H,
                           I,
                           J,
                           K,
                           L,
                           M,
                           N,
                           O)
    ).
rule___2__6(A, B, C) :-
    rule___2__7(A, B, C).
rule___2__7(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', D),
    lookup_ht(D, A, E),
    !,
    rule___2__7__0__15(E, A, B, C).
rule___2__7__0__15([], A, B, C) :-
    rule___2__8(A, B, C).
rule___2__7__0__15([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==C,
        H=t(251, E, A),
        '$novel_production'(E, H),
        '$novel_production'(A, H),
        D=pragma(rule(_, _, _, _), ids([G|I], J), _, _, C)
    ->  '$extend_history'(E, H),
        arg(2, E, K),
        setarg(2, E, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      L),
            M=[E|L],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     M),
            (   L=[N|_]
            ->  setarg(4, N, M)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      O),
            insert_ht(O, C, E)
        ;   true
        ),
        append(I, J, P),
        check_all_passive(C, P),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__7__0__15(B, C, D, E)
        ;   true
        )
    ;   rule___2__7__0__15(B, C, D, E)
    ).
rule___2__7(A, B, C) :-
    rule___2__8(A, B, C).
rule___2__8(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', D),
    lookup_ht(D, A, E),
    !,
    rule___2__8__0__16(E, A, B, C).
rule___2__8__0__16([], A, B, C) :-
    rule___2__9(A, B, C).
rule___2__8__0__16([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==C,
        H=t(252, E, A),
        '$novel_production'(E, H),
        '$novel_production'(A, H),
        D=pragma(rule(_, _, _, _), ids([], [G|I]), _, _, C)
    ->  '$extend_history'(E, H),
        arg(2, E, J),
        setarg(2, E, active),
        (   J==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      K),
            L=[E|K],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     L),
            (   K=[M|_]
            ->  setarg(4, M, L)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      N),
            insert_ht(N, C, E)
        ;   true
        ),
        check_all_passive(C, I),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__8__0__16(B, C, D, E)
        ;   true
        )
    ;   rule___2__8__0__16(B, C, D, E)
    ).
rule___2__8(A, B, C) :-
    rule___2__9(A, B, C).
rule___2__9(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
              D),
    lookup_ht(D, k(A, []), E),
    !,
    rule___2__9__0__17(E, A, B, C).
rule___2__9__0__17([], A, B, C) :-
    rule___2__10(A, B, C).
rule___2__9__0__17([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=[]
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
                    H),
          delete_ht(H, k(C, []), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
                  I),
        delete_ht(I, C, A),
        arg(2, E, J),
        setarg(2, E, active),
        (   J==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      K),
            L=[E|K],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     L),
            (   K=[M|_]
            ->  setarg(4, M, L)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      N),
            insert_ht(N, C, E)
        ;   true
        ),
        chr_warning(weird_program, 'All heads passive in ~@.\n\tThis rule never fires. Please check your program.\n', [format_rule(D)]),
        (   E=suspension(_, active, _, _, _, _)
        ->  setarg(2, E, inactive),
            rule___2__9__0__17(B, C, D, E)
        ;   true
        )
    ;   rule___2__9__0__17(B, C, D, E)
    ).
rule___2__9(A, B, C) :-
    rule___2__10(A, B, C).
rule___2__10(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    rule___2__10__0__22(E, A, B, C).
rule___2__10__0__22([], A, B, C) :-
    rule___2__11(A, B, C).
rule___2__10__0__22([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, I),
        G==C,
        nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                  J)
    ->  rule___2__10__1__22(J,
                            F,
                            H,
                            I,
                            A,
                            B,
                            C,
                            D,
                            E)
    ;   rule___2__10__0__22(B, C, D, E)
    ).
rule___2__10__1__22([], _, _, _, _, A, B, C, D) :-
    rule___2__10__0__22(A, B, C, D).
rule___2__10__1__22([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _),
        nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                  K),
        lookup_ht(K, C, L)
    ->  rule___2__10__2__22(L,
                            A,
                            B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I,
                            J)
    ;   rule___2__10__1__22(B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I,
                            J)
    ).
rule___2__10__2__22([], _, A, B, C, D, E, F, G, H, I) :-
    rule___2__10__1__22(A,
                        B,
                        C,
                        D,
                        E,
                        F,
                        G,
                        H,
                        I).
rule___2__10__2__22([A|B], C, D, E, F, G, H, I, J, K, L) :-
    (   A=suspension(_, active, _, _, M, N),
        M==E,
        O=t(330, C, H, L, A),
        '$novel_production'(C, O),
        '$novel_production'(H, O),
        '$novel_production'(L, O),
        '$novel_production'(A, O)
    ->  '$extend_history'(L, O),
        arg(2, L, P),
        setarg(2, L, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      Q),
            R=[L|Q],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1',
                      T),
            insert_ht(T, J, L)
        ;   true
        ),
        K=pragma(rule(U, V, W, _), _, _, _, _),
        (   is_passive(J, F)
        ->  true
        ;   G==simplification
        ->  select(X, U, _),
            X=..[_|Y],
            term_variables(W, Z),
            partial_wake_args(Y, N, Z, E)
        ;   select(X, V, _),
            X=..[_|Y],
            term_variables(W, Z),
            partial_wake_args(Y, N, Z, E)
        ),
        (   L=suspension(_, active, _, _, _, _)
        ->  setarg(2, L, inactive),
            rule___2__10__2__22(B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H,
                                I,
                                J,
                                K,
                                L)
        ;   true
        )
    ;   rule___2__10__2__22(B,
                            C,
                            D,
                            E,
                            F,
                            G,
                            H,
                            I,
                            J,
                            K,
                            L)
    ).
rule___2__10(A, B, C) :-
    rule___2__11(A, B, C).
rule___2__11(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2', D),
        E=[B|D],
        b_setval('$chr_store_global_ground_chr_translate____rule___2', E),
        (   D=[F|_]
        ->  setarg(4, F, E)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', G),
        insert_ht(G, A, B)
    ;   true
    ).
get_rule(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, _, F),
    !,
    B=F.
get_rule(_, _) :-
    fail.
least_occurrence(A, [B|C]) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, _, _),
    (   nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
                  G),
        lookup_ht(G, k(A, B), H),
        member(I, H),
        I=suspension(_, active, _, _, J, K, _, _, _),
        nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
                  L),
        lookup_ht(L, J, M),
        member(N, M),
        N=suspension(_, active, _, _, O),
        O>=K,
        \+ may_trigger(J),
        !,
        least_occurrence(A, C)
    ;   nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  P),
        lookup_ht(P, k(A, B), Q),
        member(R, Q),
        R=suspension(_, active, _, _, _),
        !,
        least_occurrence(A, C)
    ).
least_occurrence(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
              E),
    insert_ht(E, k(A, B), C),
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-1',
              F),
    insert_ht(F, A, C).
is_least_occurrence(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____least_occurrence___2-12',
              B),
    lookup_ht(B, k(A, []), C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
is_least_occurrence(_) :-
    fail.
stored_in_guard_lookahead(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _, _, F, G, H),
    (   nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  I),
        lookup_ht(I, k(F, G), J),
        member(K, J),
        K=suspension(_, active, _, _, _),
        !,
        L is B+1,
        stored_in_guard_lookahead(A, L)
    ;   !,
        H==simplification,
        (   is_stored_in_guard(A, F)
        ->  true
        ;   M is B+1,
            stored_in_guard_lookahead(A, M)
        )
    ).
stored_in_guard_lookahead(_, _) :-
    fail.
constraint_index(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-2',
              E),
    insert_ht(E, B, C),
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-1',
              F),
    insert_ht(F, A, C).
get_constraint_index(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_constraint_index(_, _) :-
    fail.
get_indexed_constraint(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_index___2-2',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, F, _),
    !,
    B=F.
get_indexed_constraint(_, _) :-
    fail.
max_constraint_index(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____max_constraint_index___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____max_constraint_index___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_max_constraint_index(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____max_constraint_index___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_max_constraint_index(0).
identifier_size(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
              B),
    (   member(C, B),
        C=suspension(_, active, _, _),
        !
    ;   !,
        identifier_size___1__0__0__2(B, A)
    ).
identifier_size___1__0__0__2([], A) :-
    identifier_size___1__1(A).
identifier_size___1__0__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        arg(3, A, D),
        (   var(D)
        ->  nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
                      E),
            E=[_|F],
            b_setval('$chr_store_global_ground_chr_translate____identifier_size___1',
                     F),
            (   F=[G|_]
            ->  setarg(3, G, _)
            ;   true
            )
        ;   D=[_, _|F],
            setarg(2, D, F),
            (   F=[G|_]
            ->  setarg(3, G, D)
            ;   true
            )
        ),
        identifier_size___1__0__0__2(B, C)
    ;   identifier_size___1__0__0__2(B, C)
    ).
identifier_size(A) :-
    identifier_size___1__1(A).
identifier_size___1__1(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____identifier_size___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_identifier_size(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_identifier_size(1).
identifier_index(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    (   member(F, E),
        F=suspension(_, active, _, _, _),
        !
    ;   !,
        identifier_index___3__0__0__2(E, A, B, C)
    ).
identifier_index___3__0__0__2([], A, B, C) :-
    identifier_index___3__1(A, B, C).
identifier_index___3__0__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G, _),
        F==C,
        G==D
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
                  H),
        delete_ht(H, k(C, D), A),
        identifier_index___3__0__0__2(B, C, D, E)
    ;   identifier_index___3__0__0__2(B, C, D, E)
    ).
identifier_index(A, B, C) :-
    identifier_index___3__1(A, B, C).
identifier_index___3__1(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              F),
    insert_ht(F, k(A, B), D).
get_identifier_index(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____identifier_index___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    C=G.
get_identifier_index(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
              D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    setarg(2, E, removed),
    arg(3, E, G),
    (   var(G)
    ->  nb_getval('$chr_store_global_ground_chr_translate____identifier_size___1',
                  H),
        H=[_|I],
        b_setval('$chr_store_global_ground_chr_translate____identifier_size___1',
                 I),
        (   I=[J|_]
        ->  setarg(3, J, _)
        ;   true
        )
    ;   G=[_, _|I],
        setarg(2, G, I),
        (   I=[J|_]
        ->  setarg(3, J, G)
        ;   true
        )
    ),
    K is F+1,
    identifier_index(A, B, K),
    identifier_size(K),
    C=K.
get_identifier_index(A, B, C) :-
    identifier_index(A, B, 2),
    identifier_size(2),
    C=2.
type_indexed_identifier_size(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              C),
    lookup_ht(C, A, D),
    (   member(E, D),
        E=suspension(_, active, _, _),
        !
    ;   !,
        type_indexed_identifier_size___2__0__0__2(D, A, B)
    ).
type_indexed_identifier_size___2__0__0__2([], A, B) :-
    type_indexed_identifier_size___2__1(A, B).
type_indexed_identifier_size___2__0__0__2([A|B], C, D) :-
    (   A=suspension(_, active, E, _),
        E==C
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
                  F),
        delete_ht(F, C, A),
        type_indexed_identifier_size___2__0__0__2(B, C, D)
    ;   type_indexed_identifier_size___2__0__0__2(B, C, D)
    ).
type_indexed_identifier_size(A, B) :-
    type_indexed_identifier_size___2__1(A, B).
type_indexed_identifier_size___2__1(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              E),
    insert_ht(E, A, C).
get_type_indexed_identifier_size(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_type_indexed_identifier_size(_, 1).
type_indexed_identifier_index(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23',
              E),
    lookup_ht(E, k(B, C), F),
    (   member(G, F),
        G=suspension(_, active, _, _, _, _),
        !
    ;   !,
        type_indexed_identifier_index___4__0__0__2(F,
                                                   A,
                                                   B,
                                                   C,
                                                   D)
    ).
type_indexed_identifier_index___4__0__0__2([], A, B, C, D) :-
    type_indexed_identifier_index___4__1(A, B, C, D).
type_indexed_identifier_index___4__0__0__2([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, H, I, _),
        H==D,
        I==E
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
                    J),
          delete_ht(J, k(G, D, E), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23',
                  K),
        delete_ht(K, k(D, E), A),
        type_indexed_identifier_index___4__0__0__2(B,
                                                   C,
                                                   D,
                                                   E,
                                                   F)
    ;   type_indexed_identifier_index___4__0__0__2(B,
                                                   C,
                                                   D,
                                                   E,
                                                   F)
    ).
type_indexed_identifier_index(A, B, C, D) :-
    type_indexed_identifier_index___4__1(A, B, C, D).
type_indexed_identifier_index___4__1(A, B, C, D) :-
    E=suspension(F, active, A, B, C, D),
    'chr gen_id'(F),
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
              G),
    insert_ht(G, k(A, B, C), E),
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-23',
              H),
    insert_ht(H, k(B, C), E).
get_type_indexed_identifier_index(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_index___4-123',
              E),
    lookup_ht(E, k(A, B, C), F),
    member(G, F),
    G=suspension(_, active, _, _, _, H),
    !,
    D=H.
get_type_indexed_identifier_index(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              E),
    lookup_ht(E, A, F),
    member(G, F),
    G=suspension(_, active, _, H),
    !,
    setarg(2, G, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____type_indexed_identifier_size___2-1',
              I),
    delete_ht(I, A, G),
    J is H+1,
    type_indexed_identifier_index(A, B, C, J),
    type_indexed_identifier_size(A, J),
    D=J.
get_type_indexed_identifier_index(A, B, C, D) :-
    type_indexed_identifier_index(A, B, C, 2),
    type_indexed_identifier_size(A, 2),
    D=2.
no_history(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____no_history___1-1', D),
    insert_ht(D, A, B).
has_no_history(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_history___1-1', B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
has_no_history(_) :-
    fail.
history(A, B, C) :-
    history___3__0(A, B, C, _).
history___3__0(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-1', E),
    lookup_ht(E, A, F),
    !,
    D=suspension(G, not_stored_yet, t, A, B, C),
    'chr gen_id'(G),
    history___3__0__0__1(F, A, B, C, D).
history___3__0__0__1([], A, B, C, D) :-
    history___3__1(A, B, C, D).
history___3__0__0__1([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, _, _),
        G==C,
        H=t(95, F, A),
        '$novel_production'(F, H),
        '$novel_production'(A, H)
    ->  '$extend_history'(F, H),
        arg(2, F, I),
        setarg(2, F, active),
        (   I==not_stored_yet
        ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(E,
                                                                          J)
            ->  nb_getval(J, K),
                b_setval(J, [F|K])
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                      L),
            insert_ht(L, D, F),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                      M),
            insert_ht(M, C, F)
        ;   true
        ),
        chr_error(syntax,
                  'Only one pragma history allowed per rule (rule number ~w)\n',
                  [C]),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            history___3__0__0__1(B, C, D, E, F)
        ;   true
        )
    ;   history___3__0__0__1(B, C, D, E, F)
    ).
history___3__0(A, B, C, D) :-
    D=suspension(E, not_stored_yet, t, A, B, C),
    'chr gen_id'(E),
    history___3__1(A, B, C, D).
history___3__1(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-1', E),
    lookup_ht(E, A, F),
    !,
    history___3__1__0__2(F, A, B, C, D).
history___3__1__0__2([], A, B, C, D) :-
    history___3__2(A, B, C, D).
history___3__1__0__2([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, _, _),
        G==C,
        H=t(95, A, F),
        '$novel_production'(A, H),
        '$novel_production'(F, H)
    ->  '$extend_history'(F, H),
        arg(2, F, I),
        setarg(2, F, active),
        (   I==not_stored_yet
        ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(E,
                                                                          J)
            ->  nb_getval(J, K),
                b_setval(J, [F|K])
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                      L),
            insert_ht(L, D, F),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                      M),
            insert_ht(M, C, F)
        ;   true
        ),
        chr_error(syntax,
                  'Only one pragma history allowed per rule (rule number ~w)\n',
                  [C]),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            history___3__1__0__2(B, C, D, E, F)
        ;   true
        )
    ;   history___3__1__0__2(B, C, D, E, F)
    ).
history___3__1(A, B, C, D) :-
    history___3__2(A, B, C, D).
history___3__2(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-2', E),
    lookup_ht(E, B, F),
    !,
    history___3__2__0__3(F, A, B, C, D).
history___3__2__0__3([], A, B, C, D) :-
    history___3__3(A, B, C, D).
history___3__2__0__3([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, H, I),
        H==D,
        J=t(96, F, A),
        '$novel_production'(F, J),
        '$novel_production'(A, J)
    ->  '$extend_history'(F, J),
        arg(2, F, K),
        setarg(2, F, active),
        (   K==not_stored_yet
        ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(E,
                                                                          L)
            ->  nb_getval(L, M),
                b_setval(L, [F|M])
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                      N),
            insert_ht(N, D, F),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                      O),
            insert_ht(O, C, F)
        ;   true
        ),
        length(E, P),
        length(I, Q),
        (   P\==Q
        ->  chr_error(syntax,
                      'The history named ~w does not always range over an equal amount of occurrences.\n',
                      [D])
        ;   test_named_history_id_pairs(C, E, G, I)
        ),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            history___3__2__0__3(B, C, D, E, F)
        ;   true
        )
    ;   history___3__2__0__3(B, C, D, E, F)
    ).
history___3__2(A, B, C, D) :-
    history___3__3(A, B, C, D).
history___3__3(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-2', E),
    lookup_ht(E, B, F),
    !,
    history___3__3__0__4(F, A, B, C, D).
history___3__3__0__4([], A, B, C, D) :-
    history___3__4(A, B, C, D).
history___3__3__0__4([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, H, I),
        H==D,
        J=t(96, A, F),
        '$novel_production'(A, J),
        '$novel_production'(F, J)
    ->  '$extend_history'(F, J),
        arg(2, F, K),
        setarg(2, F, active),
        (   K==not_stored_yet
        ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(E,
                                                                          L)
            ->  nb_getval(L, M),
                b_setval(L, [F|M])
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                      N),
            insert_ht(N, D, F),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                      O),
            insert_ht(O, C, F)
        ;   true
        ),
        length(I, P),
        length(E, Q),
        (   P\==Q
        ->  chr_error(syntax,
                      'The history named ~w does not always range over an equal amount of occurrences.\n',
                      [D])
        ;   test_named_history_id_pairs(G, I, C, E)
        ),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            history___3__3__0__4(B, C, D, E, F)
        ;   true
        )
    ;   history___3__3__0__4(B, C, D, E, F)
    ).
history___3__3(A, B, C, D) :-
    history___3__4(A, B, C, D).
history___3__4(A, B, [], C) :-
    nb_getval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
              D),
    !,
    history___3__4__0__6(D, A, B, [], C).
history___3__4__0__6([], A, B, C, D) :-
    history___3__5(A, B, C, D).
history___3__4__0__6([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _),
        G=t(142, A, F),
        '$novel_production'(A, G),
        '$novel_production'(F, G)
    ->  '$extend_history'(F, G),
        arg(2, F, H),
        setarg(2, F, active),
        (   H==not_stored_yet
        ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(E,
                                                                          I)
            ->  nb_getval(I, J),
                b_setval(I, [F|J])
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                      K),
            insert_ht(K, D, F),
            nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                      L),
            insert_ht(L, C, F)
        ;   true
        ),
        generate_empty_named_history_initialisation(D),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            history___3__4__0__6(B, C, D, E, F)
        ;   true
        )
    ;   history___3__4__0__6(B, C, D, E, F)
    ).
history___3__4(A, B, C, D) :-
    history___3__5(A, B, C, D).
history___3__5(A, B, C, D) :-
    arg(2, D, E),
    setarg(2, D, active),
    (   E==not_stored_yet
    ->  (   '$chr_store_constants_chr_translate____history___3___[3]'(C,
                                                                      F)
        ->  nb_getval(F, G),
            b_setval(F, [D|G])
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____history___3-2',
                  H),
        insert_ht(H, B, D),
        nb_getval('$chr_store_multi_hash_chr_translate____history___3-1',
                  I),
        insert_ht(I, A, D)
    ;   true
    ).
named_history(A, B, C) :-
    ground(A),
    nb_getval('$chr_store_multi_hash_chr_translate____history___3-1', D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, G, H),
    !,
    B=G,
    C=H.
named_history(_, _, _) :-
    fail.
test_named_history_id_pair(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-34',
              E),
    lookup_ht(E, k(A, B), F),
    member(G, F),
    G=suspension(_, active, _, _, H, _, _, _, _),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
              I),
    lookup_ht(I, k(H, C, D), J),
    member(K, J),
    K=suspension(_, active, _, _, _, _, _, _, _),
    K\==G,
    !.
test_named_history_id_pair(A, _, B, _) :-
    chr_error(syntax,
              'Occurrences of shared history in rules number ~w and ~w do not correspond\n',
              [B, A]).
indexing_spec(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    setarg(2, E, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              G),
    delete_ht(G, A, E),
    append(B, F, H),
    indexing_spec(A, H).
indexing_spec(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              E),
    insert_ht(E, A, C).
get_indexing_spec(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____indexing_spec___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
get_indexing_spec(_, []).
observation_analysis(A) :-
    observation_analysis___1__0(A, _).
observation_analysis___1__0(A, B) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule___2', C),
    !,
    B=suspension(D, not_stored_yet, _, A),
    'chr gen_id'(D),
    observation_analysis___1__0__0__1(C, A, B).
observation_analysis___1__0__0__1([], A, B) :-
    observation_analysis___1__1(A, B).
observation_analysis___1__0__0__1([A|B], C, D) :-
    (   A=suspension(_, active, _, _, E, F)
    ->  F=pragma(rule(_, _, G, H), _, _, _, _),
        (   chr_pp_flag(store_in_guards, on)
        ->  observation_analysis(E, G, guard, C)
        ;   true
        ),
        observation_analysis(E, H, body, C),
        observation_analysis___1__0__0__1(B, C, D)
    ;   observation_analysis___1__0__0__1(B, C, D)
    ).
observation_analysis___1__0(A, B) :-
    B=suspension(C, not_stored_yet, _, A),
    'chr gen_id'(C),
    observation_analysis___1__1(A, B).
observation_analysis___1__1(_, A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(3, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____observation_analysis___1',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____observation_analysis___1',
                         E),
                (   E=[F|_]
                ->  setarg(3, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(3, F, C)
                ;   true
                )
            )
        )
    ).
spawns(A, B, C) :-
    spawns___3__0(A, B, C, _).
spawns___3__0(A, B, C, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123', D),
    lookup_ht(D, k(A, B, C), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, _),
    !.
spawns___3__0(A, body, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123', C),
    lookup_ht(C, k(A, guard, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _, _, _),
    !.
spawns___3__0(A, guard, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123', D),
    lookup_ht(D, k(A, body, B), E),
    !,
    spawns___3__0__0__4(E, A, guard, B, C).
spawns___3__0__0__4([], A, B, C, D) :-
    spawns___3__1(A, B, C, D).
spawns___3__0__0__4([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, H, I),
        G==C,
        H=body,
        I==E
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    J),
          delete_ht(J, k(C, E), A)
        ),
        ( arg(4, A, K),
          (   var(K)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        L),
              L=[_|M],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       M),
              (   M=[N|_]
              ->  setarg(4, N, _)
              ;   true
              )
          ;   K=[_, _|M],
              setarg(2, K, M),
              (   M=[N|_]
              ->  setarg(4, N, K)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    O),
          delete_ht(O, k(C, body), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    P),
          delete_ht(P, C, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    Q),
          delete_ht(Q, E, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  R),
        delete_ht(R, k(C, body, E), A),
        spawns___3__0__0__4(B, C, D, E, F)
    ;   spawns___3__0__0__4(B, C, D, E, F)
    ).
spawns___3__0(A, B, C, D) :-
    spawns___3__1(A, B, C, D).
spawns___3__1(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              E),
    lookup_ht(E, k(A, B), F),
    member(G, F),
    G=suspension(_, active, _, _),
    !,
    (   var(D)
    ->  true
    ;   arg(2, D, H),
        setarg(2, D, removed),
        (   H==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      I),
            delete_ht(I, k(A, C), D),
            arg(4, D, J),
            (   var(J)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                          K),
                K=[_|L],
                b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                         L),
                (   L=[M|_]
                ->  setarg(4, M, _)
                ;   true
                )
            ;   J=[_, _|L],
                setarg(2, J, L),
                (   L=[M|_]
                ->  setarg(4, M, J)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      N),
            delete_ht(N, k(A, B), D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      O),
            delete_ht(O, A, D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      P),
            delete_ht(P, C, D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      Q),
            delete_ht(Q, k(A, B, C), D)
        )
    ).
spawns___3__1(A, body, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              D),
    lookup_ht(D, k(A, guard), E),
    member(F, E),
    F=suspension(_, active, _, _),
    !,
    (   var(C)
    ->  true
    ;   arg(2, C, G),
        setarg(2, C, removed),
        (   G==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      H),
            delete_ht(H, k(A, B), C),
            arg(4, C, I),
            (   var(I)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                          J),
                J=[_|K],
                b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                         K),
                (   K=[L|_]
                ->  setarg(4, L, _)
                ;   true
                )
            ;   I=[_, _|K],
                setarg(2, I, K),
                (   K=[L|_]
                ->  setarg(4, L, I)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      M),
            delete_ht(M, k(A, body), C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      N),
            delete_ht(N, A, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      O),
            delete_ht(O, B, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      P),
            delete_ht(P, k(A, body, B), C)
        )
    ).
spawns___3__1(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              E),
    lookup_ht(E, k(A, B), F),
    member(G, F),
    G=suspension(_, active, _, _, _, _),
    may_trigger(C),
    !,
    (   var(D)
    ->  true
    ;   arg(2, D, H),
        setarg(2, D, removed),
        (   H==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      I),
            delete_ht(I, k(A, C), D),
            arg(4, D, J),
            (   var(J)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                          K),
                K=[_|L],
                b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                         L),
                (   L=[M|_]
                ->  setarg(4, M, _)
                ;   true
                )
            ;   J=[_, _|L],
                setarg(2, J, L),
                (   L=[M|_]
                ->  setarg(4, M, J)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      N),
            delete_ht(N, k(A, B), D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      O),
            delete_ht(O, A, D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      P),
            delete_ht(P, C, D),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      Q),
            delete_ht(Q, k(A, B, C), D)
        )
    ).
spawns___3__1(A, body, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              D),
    lookup_ht(D, k(A, guard), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _),
    may_trigger(B),
    !,
    (   var(C)
    ->  true
    ;   arg(2, C, G),
        setarg(2, C, removed),
        (   G==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      H),
            delete_ht(H, k(A, B), C),
            arg(4, C, I),
            (   var(I)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                          J),
                J=[_|K],
                b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                         K),
                (   K=[L|_]
                ->  setarg(4, L, _)
                ;   true
                )
            ;   I=[_, _|K],
                setarg(2, I, K),
                (   K=[L|_]
                ->  setarg(4, L, I)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      M),
            delete_ht(M, k(A, body), C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      N),
            delete_ht(N, A, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      O),
            delete_ht(O, B, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      P),
            delete_ht(P, k(A, body, B), C)
        )
    ).
spawns___3__1(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', E),
    lookup_ht(E, C, F),
    (   member(G, F),
        G=suspension(_, active, _, _, _, _, H, I, _),
        (   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                      J),
            lookup_ht(J, H, K),
            member(L, K),
            L=suspension(_, active, _, _),
            \+ is_passive(H, I),
            !,
            (   var(D)
            ->  true
            ;   arg(2, D, M),
                setarg(2, D, removed),
                (   M==not_stored_yet
                ->  true
                ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                              N),
                    delete_ht(N, k(A, C), D),
                    arg(4, D, O),
                    (   var(O)
                    ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                                  P),
                        P=[_|Q],
                        b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                                 Q),
                        (   Q=[R|_]
                        ->  setarg(4, R, _)
                        ;   true
                        )
                    ;   O=[_, _|Q],
                        setarg(2, O, Q),
                        (   Q=[R|_]
                        ->  setarg(4, R, O)
                        ;   true
                        )
                    ),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                              S),
                    delete_ht(S, k(A, B), D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                              T),
                    delete_ht(T, A, D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                              U),
                    delete_ht(U, C, D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                              V),
                    delete_ht(V, k(A, B, C), D)
                )
            ),
            spawns_all(A, B)
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      W),
            lookup_ht(W, H, X),
            member(Y, X),
            Y=suspension(_, active, _, _, _, _),
            may_trigger(C),
            \+ is_passive(H, I),
            !,
            (   var(D)
            ->  true
            ;   arg(2, D, Z),
                setarg(2, D, removed),
                (   Z==not_stored_yet
                ->  true
                ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                              A1),
                    delete_ht(A1, k(A, C), D),
                    arg(4, D, B1),
                    (   var(B1)
                    ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                                  C1),
                        C1=[_|D1],
                        b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                                 D1),
                        (   D1=[E1|_]
                        ->  setarg(4, E1, _)
                        ;   true
                        )
                    ;   B1=[_, _|D1],
                        setarg(2, B1, D1),
                        (   D1=[E1|_]
                        ->  setarg(4, E1, B1)
                        ;   true
                        )
                    ),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                              F1),
                    delete_ht(F1, k(A, B), D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                              G1),
                    delete_ht(G1, A, D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                              H1),
                    delete_ht(H1, C, D),
                    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                              I1),
                    delete_ht(I1, k(A, B, C), D)
                )
            ),
            spawns_all_triggers(A, B)
        )
    ;   !,
        D=suspension(J1, not_stored_yet, t, _, A, B, C),
        'chr gen_id'(J1),
        spawns___3__1__0__11(F, A, B, C, D)
    ).
spawns___3__1__0__11([], A, B, C, D) :-
    spawns___3__2(A, B, C, D).
spawns___3__1__0__11([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, _, H, I, _),
        G==E,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                  J),
        lookup_ht(J, H, K)
    ->  spawns___3__1__1__11(K,
                             H,
                             I,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F)
    ;   spawns___3__1__0__11(B, C, D, E, F)
    ).
spawns___3__1__1__11([], _, _, _, A, B, C, D, E) :-
    spawns___3__1__0__11(A, B, C, D, E).
spawns___3__1__1__11([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _, K, _),
        K==C,
        L=t(123, A, E, J),
        '$novel_production'(A, L),
        '$novel_production'(J, L),
        \+ may_trigger(I),
        \+ is_passive(C, D)
    ->  '$extend_history'(J, L),
        arg(2, J, M),
        setarg(2, J, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      N),
            insert_ht(N, k(G, I), J),
            nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                      O),
            P=[J|O],
            b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      R),
            insert_ht(R, k(G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      S),
            insert_ht(S, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      T),
            insert_ht(T, I, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      U),
            insert_ht(U, k(G, H, I), J)
        ;   true
        ),
        spawns_all_triggers(G, H),
        (   J=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            spawns___3__1__1__11(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J)
        ;   true
        )
    ;   spawns___3__1__1__11(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J)
    ).
spawns___3__1(A, B, C, D) :-
    D=suspension(E, not_stored_yet, t, _, A, B, C),
    'chr gen_id'(E),
    spawns___3__2(A, B, C, D).
spawns___3__2(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', E),
    lookup_ht(E, A, F),
    !,
    spawns___3__2__0__12(F, A, B, C, D).
spawns___3__2__0__12([], A, B, C, D) :-
    spawns___3__3(A, B, C, D).
spawns___3__2__0__12([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, _, H, I, _),
        H==C,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3', J),
        lookup_ht(J, G, K)
    ->  spawns___3__2__1__12(K,
                             G,
                             I,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F)
    ;   spawns___3__2__0__12(B, C, D, E, F)
    ).
spawns___3__2__1__12([], _, _, _, A, B, C, D, E) :-
    spawns___3__2__0__12(A, B, C, D, E).
spawns___3__2__1__12([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _, K, L, M),
        M==C,
        N=t(124, J, E, A),
        '$novel_production'(J, N),
        '$novel_production'(A, N),
        K\==G,
        C\==I,
        \+ is_passive(G, D)
    ->  '$extend_history'(J, N),
        arg(2, J, O),
        setarg(2, J, active),
        (   O==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      P),
            insert_ht(P, k(G, I), J),
            nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                      Q),
            R=[J|Q],
            b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      T),
            insert_ht(T, k(G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      U),
            insert_ht(U, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      V),
            insert_ht(V, I, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      W),
            insert_ht(W, k(G, H, I), J)
        ;   true
        ),
        spawns(K, L, I),
        (   J=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            spawns___3__2__1__12(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J)
        ;   true
        )
    ;   spawns___3__2__1__12(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J)
    ).
spawns___3__2(A, B, C, D) :-
    spawns___3__3(A, B, C, D).
spawns___3__3(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', E),
    lookup_ht(E, C, F),
    !,
    spawns___3__3__0__13(F, A, B, C, D).
spawns___3__3__0__13([], A, B, C, D) :-
    spawns___3__4(A, B, C, D).
spawns___3__3__0__13([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, _, H, I, _),
        G==E,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1', J),
        lookup_ht(J, H, K)
    ->  spawns___3__3__1__13(K,
                             H,
                             I,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F)
    ;   spawns___3__3__0__13(B, C, D, E, F)
    ).
spawns___3__3__1__13([], _, _, _, A, B, C, D, E) :-
    spawns___3__3__0__13(A, B, C, D, E).
spawns___3__3__1__13([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _, K, _, L),
        K==C,
        M=t(124, A, E, J),
        '$novel_production'(A, M),
        '$novel_production'(J, M),
        G\==C,
        I\==L,
        \+ is_passive(C, D)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      O),
            insert_ht(O, k(G, I), J),
            nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                      P),
            Q=[J|P],
            b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      S),
            insert_ht(S, k(G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      T),
            insert_ht(T, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      U),
            insert_ht(U, I, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      V),
            insert_ht(V, k(G, H, I), J)
        ;   true
        ),
        spawns(G, H, L),
        (   J=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            spawns___3__3__1__13(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J)
        ;   true
        )
    ;   spawns___3__3__1__13(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J)
    ).
spawns___3__3(A, B, C, D) :-
    spawns___3__4(A, B, C, D).
spawns___3__4(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', E),
    lookup_ht(E, A, F),
    !,
    spawns___3__4__0__14(F, A, B, C, D).
spawns___3__4__0__14([], A, B, C, D) :-
    spawns___3__5(A, B, C, D).
spawns___3__4__0__14([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, _, H, I, _),
        H==C,
        nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                  J)
    ->  spawns___3__4__1__14(J,
                             G,
                             I,
                             A,
                             B,
                             C,
                             D,
                             E,
                             F)
    ;   spawns___3__4__0__14(B, C, D, E, F)
    ).
spawns___3__4__1__14([], _, _, _, A, B, C, D, E) :-
    spawns___3__4__0__14(A, B, C, D, E).
spawns___3__4__1__14([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, _, K, L),
        M=t(125, J, E, A),
        '$novel_production'(J, M),
        '$novel_production'(A, M),
        \+ is_passive(G, D),
        may_trigger(C),
        \+ may_trigger(I)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      O),
            insert_ht(O, k(G, I), J),
            nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                      P),
            Q=[J|P],
            b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                      S),
            insert_ht(S, k(G, H), J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                      T),
            insert_ht(T, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                      U),
            insert_ht(U, I, J),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      V),
            insert_ht(V, k(G, H, I), J)
        ;   true
        ),
        spawns(K, L, I),
        (   J=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            spawns___3__4__1__14(B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J)
        ;   true
        )
    ;   spawns___3__4__1__14(B,
                             C,
                             D,
                             E,
                             F,
                             G,
                             H,
                             I,
                             J)
    ).
spawns___3__4(A, B, C, D) :-
    spawns___3__5(A, B, C, D).
spawns___3__5(A, B, C, D) :-
    arg(2, D, E),
    setarg(2, D, active),
    (   E==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                  F),
        insert_ht(F, k(A, C), D),
        nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                  G),
        H=[D|G],
        b_setval('$chr_store_global_ground_chr_translate____spawns___3', H),
        (   G=[I|_]
        ->  setarg(4, I, H)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                  J),
        insert_ht(J, k(A, B), D),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                  K),
        insert_ht(K, A, D),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                  L),
        insert_ht(L, C, D),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  M),
        insert_ht(M, k(A, B, C), D)
    ;   true
    ).
spawns_all(A, B) :-
    spawns_all___2__0(A, B, _).
spawns_all___2__0(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
spawns_all___2__0(A, body, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              B),
    lookup_ht(B, k(A, guard), C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
spawns_all___2__0(A, guard, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              C),
    lookup_ht(C, k(A, body), D),
    !,
    spawns_all___2__0__0__4(D, A, guard, B).
spawns_all___2__0__0__4([], A, B, C) :-
    spawns_all___2__1(A, B, C).
spawns_all___2__0__0__4([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G),
        F==C,
        G=body
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                    H),
          delete_ht(H, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
                  I),
        delete_ht(I, k(C, body), A),
        spawns_all___2__0__0__4(B, C, D, E)
    ;   spawns_all___2__0__0__4(B, C, D, E)
    ).
spawns_all___2__0(A, B, C) :-
    spawns_all___2__1(A, B, C).
spawns_all___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12', D),
    lookup_ht(D, k(A, B), E),
    !,
    spawns_all___2__1__0__5(E, A, B, C).
spawns_all___2__1__0__5([], A, B, C) :-
    spawns_all___2__2(A, B, C).
spawns_all___2__1__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H),
        F==C,
        G==D
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    I),
          delete_ht(I, k(C, H), A)
        ),
        ( arg(4, A, J),
          (   var(J)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        K),
              K=[_|L],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       L),
              (   L=[M|_]
              ->  setarg(4, M, _)
              ;   true
              )
          ;   J=[_, _|L],
              setarg(2, J, L),
              (   L=[M|_]
              ->  setarg(4, M, J)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    N),
          delete_ht(N, k(C, D), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    O),
          delete_ht(O, C, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    P),
          delete_ht(P, H, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  Q),
        delete_ht(Q, k(C, D, H), A),
        spawns_all___2__1__0__5(B, C, D, E)
    ;   spawns_all___2__1__0__5(B, C, D, E)
    ).
spawns_all___2__1(A, B, C) :-
    spawns_all___2__2(A, B, C).
spawns_all___2__2(A, guard, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12', C),
    lookup_ht(C, k(A, body), D),
    !,
    spawns_all___2__2__0__6(D, A, guard, B).
spawns_all___2__2__0__6([], A, B, C) :-
    spawns_all___2__3(A, B, C).
spawns_all___2__2__0__6([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H),
        F==C,
        G=body
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    I),
          delete_ht(I, k(C, H), A)
        ),
        ( arg(4, A, J),
          (   var(J)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        K),
              K=[_|L],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       L),
              (   L=[M|_]
              ->  setarg(4, M, _)
              ;   true
              )
          ;   J=[_, _|L],
              setarg(2, J, L),
              (   L=[M|_]
              ->  setarg(4, M, J)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    N),
          delete_ht(N, k(C, body), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    O),
          delete_ht(O, C, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    P),
          delete_ht(P, H, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  Q),
        delete_ht(Q, k(C, body, H), A),
        spawns_all___2__2__0__6(B, C, D, E)
    ;   spawns_all___2__2__0__6(B, C, D, E)
    ).
spawns_all___2__2(A, B, C) :-
    spawns_all___2__3(A, B, C).
spawns_all___2__3(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    spawns_all___2__3__0__7(E, A, B, C).
spawns_all___2__3__0__7([], A, B, C) :-
    spawns_all___2__4(A, B, C).
spawns_all___2__3__0__7([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G),
        F==C,
        G==D
    ->  setarg(2, A, removed),
        ( arg(4, A, H),
          (   var(H)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                        I),
              I=[_|J],
              b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                       J),
              (   J=[K|_]
              ->  setarg(4, K, _)
              ;   true
              )
          ;   H=[_, _|J],
              setarg(2, H, J),
              (   J=[K|_]
              ->  setarg(4, K, H)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                    L),
          delete_ht(L, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  M),
        delete_ht(M, k(C, D), A),
        spawns_all___2__3__0__7(B, C, D, E)
    ;   spawns_all___2__3__0__7(B, C, D, E)
    ).
spawns_all___2__3(A, B, C) :-
    spawns_all___2__4(A, B, C).
spawns_all___2__4(A, guard, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              C),
    lookup_ht(C, k(A, body), D),
    !,
    spawns_all___2__4__0__8(D, A, guard, B).
spawns_all___2__4__0__8([], A, B, C) :-
    spawns_all___2__5(A, B, C).
spawns_all___2__4__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G),
        F==C,
        G=body
    ->  setarg(2, A, removed),
        ( arg(4, A, H),
          (   var(H)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                        I),
              I=[_|J],
              b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                       J),
              (   J=[K|_]
              ->  setarg(4, K, _)
              ;   true
              )
          ;   H=[_, _|J],
              setarg(2, H, J),
              (   J=[K|_]
              ->  setarg(4, K, H)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                    L),
          delete_ht(L, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  M),
        delete_ht(M, k(C, body), A),
        spawns_all___2__4__0__8(B, C, D, E)
    ;   spawns_all___2__4__0__8(B, C, D, E)
    ).
spawns_all___2__4(A, B, C) :-
    spawns_all___2__5(A, B, C).
spawns_all___2__5(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, A, B),
    'chr gen_id'(F),
    spawns_all___2__5__0__9(E, A, B, C).
spawns_all___2__5__0__9([], A, B, C) :-
    spawns_all___2__6(A, B, C).
spawns_all___2__5__0__9([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, _),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3', I),
        lookup_ht(I, F, J)
    ->  spawns_all___2__5__1__9(J,
                                F,
                                H,
                                A,
                                B,
                                C,
                                D,
                                E)
    ;   spawns_all___2__5__0__9(B, C, D, E)
    ).
spawns_all___2__5__1__9([], _, _, _, A, B, C, D) :-
    spawns_all___2__5__0__9(A, B, C, D).
spawns_all___2__5__1__9([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K, L),
        L==C,
        \+ is_passive(G, D)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    M),
          delete_ht(M, k(J, C), A)
        ),
        ( arg(4, A, N),
          (   var(N)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        O),
              O=[_|P],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       P),
              (   P=[Q|_]
              ->  setarg(4, Q, _)
              ;   true
              )
          ;   N=[_, _|P],
              setarg(2, N, P),
              (   P=[Q|_]
              ->  setarg(4, Q, N)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    R),
          delete_ht(R, k(J, K), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    S),
          delete_ht(S, J, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    T),
          delete_ht(T, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  U),
        delete_ht(U, k(J, K, C), A),
        arg(2, I, V),
        setarg(2, I, active),
        (   V==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                      W),
            insert_ht(W, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
                      X),
            insert_ht(X, k(G, H), I)
        ;   true
        ),
        spawns_all(J, K),
        (   I=suspension(_, active, _, _)
        ->  setarg(2, I, inactive),
            spawns_all___2__5__1__9(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G,
                                    H,
                                    I)
        ;   true
        )
    ;   spawns_all___2__5__1__9(B,
                                C,
                                D,
                                E,
                                F,
                                G,
                                H,
                                I)
    ).
spawns_all___2__5(A, B, C) :-
    C=suspension(D, not_stored_yet, A, B),
    'chr gen_id'(D),
    spawns_all___2__6(A, B, C).
spawns_all___2__6(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    spawns_all___2__6__0__10(E, A, B, C).
spawns_all___2__6__0__10([], A, B, C) :-
    spawns_all___2__7(A, B, C).
spawns_all___2__6__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, _),
        G==C,
        \+ \+ spawns_all_triggers_implies_spawns_all,
        \+ is_passive(C, H),
        may_trigger(F)
    ->  arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                      J),
            insert_ht(J, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
                      K),
            insert_ht(K, k(C, D), E)
        ;   true
        ),
        spawns_all_triggers_implies_spawns_all,
        (   E=suspension(_, active, _, _)
        ->  setarg(2, E, inactive),
            spawns_all___2__6__0__10(B, C, D, E)
        ;   true
        )
    ;   spawns_all___2__6__0__10(B, C, D, E)
    ).
spawns_all___2__6(A, B, C) :-
    spawns_all___2__7(A, B, C).
spawns_all___2__7(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                  E),
        insert_ht(E, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
                  F),
        insert_ht(F, k(A, B), C)
    ;   true
    ).
spawns_all_triggers(A, B) :-
    spawns_all_triggers___2__0(A, B, _).
spawns_all_triggers___2__0(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _, _),
    !.
spawns_all_triggers___2__0(A, body, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              B),
    lookup_ht(B, k(A, guard), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
spawns_all_triggers___2__0(A, guard, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
              C),
    lookup_ht(C, k(A, body), D),
    !,
    spawns_all_triggers___2__0__0__4(D, A, guard, B).
spawns_all_triggers___2__0__0__4([], A, B, C) :-
    spawns_all_triggers___2__1(A, B, C).
spawns_all_triggers___2__0__0__4([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G),
        F==C,
        G=body
    ->  setarg(2, A, removed),
        ( arg(4, A, H),
          (   var(H)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                        I),
              I=[_|J],
              b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                       J),
              (   J=[K|_]
              ->  setarg(4, K, _)
              ;   true
              )
          ;   H=[_, _|J],
              setarg(2, H, J),
              (   J=[K|_]
              ->  setarg(4, K, H)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                    L),
          delete_ht(L, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  M),
        delete_ht(M, k(C, body), A),
        spawns_all_triggers___2__0__0__4(B, C, D, E)
    ;   spawns_all_triggers___2__0__0__4(B, C, D, E)
    ).
spawns_all_triggers___2__0(A, B, C) :-
    spawns_all_triggers___2__1(A, B, C).
spawns_all_triggers___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _),
    !,
    (   var(C)
    ->  true
    ;   arg(2, C, G),
        setarg(2, C, removed),
        (   G==not_stored_yet
        ->  true
        ;   arg(4, C, H),
            (   var(H)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                          I),
                I=[_|J],
                b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                         J),
                (   J=[K|_]
                ->  setarg(4, K, _)
                ;   true
                )
            ;   H=[_, _|J],
                setarg(2, H, J),
                (   J=[K|_]
                ->  setarg(4, K, H)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      L),
            delete_ht(L, A, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      M),
            delete_ht(M, k(A, B), C)
        )
    ).
spawns_all_triggers___2__1(A, body, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              C),
    lookup_ht(C, k(A, guard), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !,
    (   var(B)
    ->  true
    ;   arg(2, B, F),
        setarg(2, B, removed),
        (   F==not_stored_yet
        ->  true
        ;   arg(4, B, G),
            (   var(G)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                          H),
                H=[_|I],
                b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                         I),
                (   I=[J|_]
                ->  setarg(4, J, _)
                ;   true
                )
            ;   G=[_, _|I],
                setarg(2, G, I),
                (   I=[J|_]
                ->  setarg(4, J, G)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      K),
            delete_ht(K, A, B),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      L),
            delete_ht(L, k(A, body), B)
        )
    ).
spawns_all_triggers___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12', D),
    lookup_ht(D, k(A, B), E),
    !,
    spawns_all_triggers___2__1__0__7(E, A, B, C).
spawns_all_triggers___2__1__0__7([], A, B, C) :-
    spawns_all_triggers___2__2(A, B, C).
spawns_all_triggers___2__1__0__7([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H),
        F==C,
        G==D,
        may_trigger(H)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    I),
          delete_ht(I, k(C, H), A)
        ),
        ( arg(4, A, J),
          (   var(J)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        K),
              K=[_|L],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       L),
              (   L=[M|_]
              ->  setarg(4, M, _)
              ;   true
              )
          ;   J=[_, _|L],
              setarg(2, J, L),
              (   L=[M|_]
              ->  setarg(4, M, J)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    N),
          delete_ht(N, k(C, D), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    O),
          delete_ht(O, C, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    P),
          delete_ht(P, H, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  Q),
        delete_ht(Q, k(C, D, H), A),
        spawns_all_triggers___2__1__0__7(B, C, D, E)
    ;   spawns_all_triggers___2__1__0__7(B, C, D, E)
    ).
spawns_all_triggers___2__1(A, B, C) :-
    spawns_all_triggers___2__2(A, B, C).
spawns_all_triggers___2__2(A, guard, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12', C),
    lookup_ht(C, k(A, body), D),
    !,
    spawns_all_triggers___2__2__0__8(D, A, guard, B).
spawns_all_triggers___2__2__0__8([], A, B, C) :-
    spawns_all_triggers___2__3(A, B, C).
spawns_all_triggers___2__2__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H),
        F==C,
        G=body,
        may_trigger(H)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    I),
          delete_ht(I, k(C, H), A)
        ),
        ( arg(4, A, J),
          (   var(J)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        K),
              K=[_|L],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       L),
              (   L=[M|_]
              ->  setarg(4, M, _)
              ;   true
              )
          ;   J=[_, _|L],
              setarg(2, J, L),
              (   L=[M|_]
              ->  setarg(4, M, J)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    N),
          delete_ht(N, k(C, body), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    O),
          delete_ht(O, C, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    P),
          delete_ht(P, H, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  Q),
        delete_ht(Q, k(C, body, H), A),
        spawns_all_triggers___2__2__0__8(B, C, D, E)
    ;   spawns_all_triggers___2__2__0__8(B, C, D, E)
    ).
spawns_all_triggers___2__2(A, B, C) :-
    spawns_all_triggers___2__3(A, B, C).
spawns_all_triggers___2__3(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
              D),
    member(E, D),
    E=suspension(_, active, _),
    !,
    (   var(C)
    ->  true
    ;   arg(2, C, F),
        setarg(2, C, removed),
        (   F==not_stored_yet
        ->  true
        ;   arg(4, C, G),
            (   var(G)
            ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                          H),
                H=[_|I],
                b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                         I),
                (   I=[J|_]
                ->  setarg(4, J, _)
                ;   true
                )
            ;   G=[_, _|I],
                setarg(2, G, I),
                (   I=[J|_]
                ->  setarg(4, J, G)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      K),
            delete_ht(K, A, C),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      L),
            delete_ht(L, k(A, B), C)
        )
    ),
    spawns_all(A, B).
spawns_all_triggers___2__3(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, t, _, A, B),
    'chr gen_id'(F),
    spawns_all_triggers___2__3__0__10(E, A, B, C).
spawns_all_triggers___2__3__0__10([], A, B, C) :-
    spawns_all_triggers___2__4(A, B, C).
spawns_all_triggers___2__3__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, _),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3', I),
        lookup_ht(I, F, J)
    ->  spawns_all_triggers___2__3__1__10(J,
                                          F,
                                          H,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   spawns_all_triggers___2__3__0__10(B, C, D, E)
    ).
spawns_all_triggers___2__3__1__10([], _, _, _, A, B, C, D) :-
    spawns_all_triggers___2__3__0__10(A, B, C, D).
spawns_all_triggers___2__3__1__10([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K, L),
        L==C,
        may_trigger(C),
        \+ is_passive(G, D)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                    M),
          delete_ht(M, k(J, C), A)
        ),
        ( arg(4, A, N),
          (   var(N)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns___3',
                        O),
              O=[_|P],
              b_setval('$chr_store_global_ground_chr_translate____spawns___3',
                       P),
              (   P=[Q|_]
              ->  setarg(4, Q, _)
              ;   true
              )
          ;   N=[_, _|P],
              setarg(2, N, P),
              (   P=[Q|_]
              ->  setarg(4, Q, N)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-12',
                    R),
          delete_ht(R, k(J, K), A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-1',
                    S),
          delete_ht(S, J, A)
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3',
                    T),
          delete_ht(T, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                  U),
        delete_ht(U, k(J, K, C), A),
        arg(2, I, V),
        setarg(2, I, active),
        (   V==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                      W),
            X=[I|W],
            b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                     X),
            (   W=[Y|_]
            ->  setarg(4, Y, X)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      Z),
            insert_ht(Z, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      A1),
            insert_ht(A1, k(G, H), I)
        ;   true
        ),
        spawns_all_triggers(J, K),
        (   I=suspension(_, active, _, _, _, _)
        ->  setarg(2, I, inactive),
            spawns_all_triggers___2__3__1__10(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G,
                                              H,
                                              I)
        ;   true
        )
    ;   spawns_all_triggers___2__3__1__10(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H,
                                          I)
    ).
spawns_all_triggers___2__3(A, B, C) :-
    C=suspension(D, not_stored_yet, t, _, A, B),
    'chr gen_id'(D),
    spawns_all_triggers___2__4(A, B, C).
spawns_all_triggers___2__4(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', D),
    lookup_ht(D, A, E),
    !,
    spawns_all_triggers___2__4__0__11(E, A, B, C).
spawns_all_triggers___2__4__0__11([], A, B, C) :-
    spawns_all_triggers___2__5(A, B, C).
spawns_all_triggers___2__4__0__11([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G, H, _),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-3', I),
        lookup_ht(I, F, J)
    ->  spawns_all_triggers___2__4__1__11(J,
                                          F,
                                          H,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   spawns_all_triggers___2__4__0__11(B, C, D, E)
    ).
spawns_all_triggers___2__4__1__11([], _, _, _, A, B, C, D) :-
    spawns_all_triggers___2__4__0__11(A, B, C, D).
spawns_all_triggers___2__4__1__11([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K, L),
        L==C,
        M=t(123, I, E, A),
        '$novel_production'(I, M),
        '$novel_production'(A, M),
        \+ may_trigger(C),
        \+ is_passive(G, D)
    ->  '$extend_history'(I, M),
        arg(2, I, N),
        setarg(2, I, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                      O),
            P=[I|O],
            b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      R),
            insert_ht(R, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      S),
            insert_ht(S, k(G, H), I)
        ;   true
        ),
        spawns_all_triggers(J, K),
        (   I=suspension(_, active, _, _, _, _)
        ->  setarg(2, I, inactive),
            spawns_all_triggers___2__4__1__11(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G,
                                              H,
                                              I)
        ;   true
        )
    ;   spawns_all_triggers___2__4__1__11(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H,
                                          I)
    ).
spawns_all_triggers___2__4(A, B, C) :-
    spawns_all_triggers___2__5(A, B, C).
spawns_all_triggers___2__5(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns___3', D),
    !,
    spawns_all_triggers___2__5__0__12(D, A, B, C).
spawns_all_triggers___2__5__0__12([], A, B, C) :-
    spawns_all_triggers___2__6(A, B, C).
spawns_all_triggers___2__5__0__12([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, _, G),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  H),
        lookup_ht(H, F, I)
    ->  spawns_all_triggers___2__5__1__12(I,
                                          F,
                                          G,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   spawns_all_triggers___2__5__0__12(B, C, D, E)
    ).
spawns_all_triggers___2__5__1__12([], _, _, _, A, B, C, D) :-
    spawns_all_triggers___2__5__0__12(A, B, C, D).
spawns_all_triggers___2__5__1__12([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, _, K, L, _),
        K==C,
        M=t(125, E, A, I),
        '$novel_production'(E, M),
        '$novel_production'(I, M),
        \+ is_passive(C, L),
        may_trigger(J),
        \+ may_trigger(D)
    ->  '$extend_history'(I, M),
        arg(2, I, N),
        setarg(2, I, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                      O),
            P=[I|O],
            b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                     P),
            (   O=[Q|_]
            ->  setarg(4, Q, P)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                      R),
            insert_ht(R, G, I),
            nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                      S),
            insert_ht(S, k(G, H), I)
        ;   true
        ),
        spawns(G, H, D),
        (   I=suspension(_, active, _, _, _, _)
        ->  setarg(2, I, inactive),
            spawns_all_triggers___2__5__1__12(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G,
                                              H,
                                              I)
        ;   true
        )
    ;   spawns_all_triggers___2__5__1__12(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H,
                                          I)
    ).
spawns_all_triggers___2__5(A, B, C) :-
    spawns_all_triggers___2__6(A, B, C).
spawns_all_triggers___2__6(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                  E),
        F=[C|E],
        b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                 F),
        (   E=[G|_]
        ->  setarg(4, G, F)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                  H),
        insert_ht(H, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  I),
        insert_ht(I, k(A, B), C)
    ;   true
    ).
spawns_all_triggers_implies_spawns_all :-
    spawns_all_triggers_implies_spawns_all___0__0(_).
spawns_all_triggers_implies_spawns_all___0__0(_) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
              A),
    member(B, A),
    B=suspension(_, active, _),
    !,
    setarg(2, B, removed),
    arg(3, B, C),
    (   var(C)
    ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                  D),
        D=[_|E],
        b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                 E),
        (   E=[F|_]
        ->  setarg(3, F, _)
        ;   true
        )
    ;   C=[_, _|E],
        setarg(2, C, E),
        (   E=[F|_]
        ->  setarg(3, F, C)
        ;   true
        )
    ),
    fail.
spawns_all_triggers_implies_spawns_all___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
              B),
    !,
    A=suspension(C, not_stored_yet, _),
    'chr gen_id'(C),
    spawns_all_triggers_implies_spawns_all___0__0__0__3(B, A).
spawns_all_triggers_implies_spawns_all___0__0__0__3([], A) :-
    spawns_all_triggers_implies_spawns_all___0__1(A).
spawns_all_triggers_implies_spawns_all___0__0__0__3([A|B], C) :-
    (   A=suspension(_, active, _, _, D, E)
    ->  setarg(2, A, removed),
        ( arg(4, A, F),
          (   var(F)
          ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                        G),
              G=[_|H],
              b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers___2',
                       H),
              (   H=[I|_]
              ->  setarg(4, I, _)
              ;   true
              )
          ;   F=[_, _|H],
              setarg(2, F, H),
              (   H=[I|_]
              ->  setarg(4, I, F)
              ;   true
              )
          )
        ),
        ( nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                    J),
          delete_ht(J, D, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  K),
        delete_ht(K, k(D, E), A),
        arg(2, C, L),
        setarg(2, C, active),
        (   L==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                      M),
            N=[C|M],
            b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                     N),
            (   M=[O|_]
            ->  setarg(3, O, N)
            ;   true
            )
        ;   true
        ),
        spawns_all(D, E),
        (   C=suspension(_, active, _)
        ->  setarg(2, C, inactive),
            spawns_all_triggers_implies_spawns_all___0__0__0__3(B,
                                                                C)
        ;   true
        )
    ;   spawns_all_triggers_implies_spawns_all___0__0__0__3(B, C)
    ).
spawns_all_triggers_implies_spawns_all___0__0(A) :-
    A=suspension(B, not_stored_yet, _),
    'chr gen_id'(B),
    spawns_all_triggers_implies_spawns_all___0__1(A).
spawns_all_triggers_implies_spawns_all___0__1(A) :-
    arg(2, A, B),
    setarg(2, A, active),
    (   B==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                  C),
        D=[A|C],
        b_setval('$chr_store_global_ground_chr_translate____spawns_all_triggers_implies_spawns_all___0',
                 D),
        (   C=[E|_]
        ->  setarg(3, E, D)
        ;   true
        )
    ;   true
    ).
all_spawned(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
              C),
    (   lookup_ht(C, k(A, guard), D),
        member(E, D),
        E=suspension(_, active, _, _),
        !
    ;   lookup_ht(C, k(A, B), F),
        member(G, F),
        G=suspension(_, active, _, _),
        !
    ).
all_spawned(_, _) :-
    fail.
is_observed(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, _, _),
    !,
    do_is_observed(A, G, C).
is_observed(_, _, _) :-
    fail.
do_is_observed(A, B, C) :-
    var(C),
    !,
    do_is_observed(A, B).
do_is_observed(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, _, _),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', H),
    lookup_ht(H, G, I),
    member(J, I),
    J=suspension(_, active, _, _, K, _, _, L, _),
    J\==F,
    ground(C),
    (   (   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-12',
                      M),
            lookup_ht(M, k(B, C), N),
            member(O, N),
            O=suspension(_, active, _, _),
            \+ is_passive(G, L),
            !
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-123',
                      P),
            lookup_ht(P, k(B, C, K), Q),
            member(R, Q),
            R=suspension(_, active, _, _, _, _, _),
            \+ is_passive(G, L),
            !
        )
    ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-12',
                  S),
        lookup_ht(S, k(B, C), T),
        member(U, T),
        U=suspension(_, active, _, _, _, _),
        \+ is_passive(G, L),
        may_trigger(K),
        !
    ).
do_is_observed(_, _, _) :-
    chr_pp_flag(observation_analysis, off).
do_is_observed(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, _, _, F, _, _),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', G),
    lookup_ht(G, F, H),
    member(I, H),
    I=suspension(_, active, _, _, J, _, _, K, _),
    I\==E,
    (   (   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all___2-1',
                      L),
            lookup_ht(L, B, M),
            member(N, M),
            N=suspension(_, active, _, _),
            \+ is_passive(F, K),
            !
        ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns___3-13',
                      O),
            lookup_ht(O, k(B, J), P),
            member(Q, P),
            Q=suspension(_, active, _, _, _, _, _),
            \+ is_passive(F, K),
            !
        )
    ;   nb_getval('$chr_store_multi_hash_chr_translate____spawns_all_triggers___2-1',
                  R),
        lookup_ht(R, B, S),
        member(T, S),
        T=suspension(_, active, _, _, _, _),
        \+ is_passive(F, K),
        may_trigger(J),
        !
    ).
do_is_observed(_, _) :-
    chr_pp_flag(observation_analysis, off).
generate_indexed_variables_body(A/B, C, D, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
              F),
    lookup_ht(F, A/B, G),
    member(H, G),
    H=suspension(_, active, _, _, _, I),
    !,
    get_indexing_spec(A/B, J),
    (   chr_pp_flag(term_indexing, on)
    ->  spectermvars(J, C, A, B, D, E)
    ;   get_constraint_type_det(A/B, K),
        create_indexed_variables_body(C,
                                      I,
                                      K,
                                      E,
                                      1,
                                      A/B,
                                      L,
                                      M),
        (   L==empty
        ->  D=true,
            E=[]
        ;   M==0
        ->  (   C=[N]
            ->  true
            ;   N=..[term|C]
            ),
            D=term_variables(N, E)
        ;   L=D
        )
    ).
generate_indexed_variables_body(A, _, _, _) :-
    chr_error(internal,
              'generate_indexed_variables_body: missing mode info for ~w.\n',
              [A]).
empty_named_history_initialisations(A, B) :-
    C=suspension(D, active, _, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
              E),
    F=[C|E],
    b_setval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
             F),
    (   E=[G|_]
    ->  setarg(3, G, F)
    ;   true
    ).
generate_empty_named_history_initialisation(A) :-
    generate_empty_named_history_initialisation___1__0(A, _).
generate_empty_named_history_initialisation___1__0(A, B) :-
    (   'chr newvia_1'(A, C)
    ->  get_attr(C, chr_translate, D),
        D=v(_, E, _, _, _, _, _, _, _, _, _)
    ;   nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                  E)
    ),
    member(F, E),
    F=suspension(_, active, _, _, G),
    G==A,
    !,
    (   var(B)
    ->  true
    ;   B=suspension(_, H, _, _, I),
        setarg(2, B, removed),
        (   H==not_stored_yet
        ->  J=[]
        ;   term_variables(I, J),
            arg(4, B, K),
            (   var(K)
            ->  nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                          L),
                L=[_|M],
                b_setval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                         M),
                (   M=[N|_]
                ->  setarg(4, N, _)
                ;   true
                )
            ;   K=[_, _|M],
                setarg(2, K, M),
                (   M=[N|_]
                ->  setarg(4, N, K)
                ;   true
                )
            ),
            detach_generate_empty_named_history_initialisation___1(J,
                                                                   B)
        )
    ).
generate_empty_named_history_initialisation___1__0(A, B) :-
    nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
              C),
    !,
    (   var(B)
    ->  B=suspension(D, not_stored_yet, 0, _, A),
        'chr gen_id'(D)
    ;   true
    ),
    generate_empty_named_history_initialisation___1__0__0__3(C,
                                                             A,
                                                             B).
generate_empty_named_history_initialisation___1__0__0__3([], A, B) :-
    generate_empty_named_history_initialisation___1__1(A, B).
generate_empty_named_history_initialisation___1__0__0__3([A|B], C, D) :-
    (   A=suspension(_, active, _, E, F)
    ->  setarg(2, A, removed),
        arg(3, A, G),
        (   var(G)
        ->  nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
                      H),
            H=[_|I],
            b_setval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
                     I),
            (   I=[J|_]
            ->  setarg(3, J, _)
            ;   true
            )
        ;   G=[_, _|I],
            setarg(2, G, I),
            (   I=[J|_]
            ->  setarg(3, J, G)
            ;   true
            )
        ),
        arg(2, D, K),
        setarg(2, D, active),
        ( arg(3, D, L),
          M is L+1,
          setarg(3, D, M)
        ),
        (   K==not_stored_yet
        ->  D=suspension(_, _, _, _, N),
            term_variables(N, O),
            nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                      P),
            Q=[D|P],
            b_setval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                     Q),
            (   P=[R|_]
            ->  setarg(4, R, Q)
            ;   true
            ),
            attach_generate_empty_named_history_initialisation___1(O,
                                                                   D)
        ;   true
        ),
        empty_named_history_global_variable(C, S),
        E=[(:-nb_setval(S, 0))|T],
        empty_named_history_initialisations(T, F),
        (   D=suspension(_, active, M, _, _)
        ->  setarg(2, D, inactive),
            generate_empty_named_history_initialisation___1__0__0__3(B,
                                                                     C,
                                                                     D)
        ;   true
        )
    ;   generate_empty_named_history_initialisation___1__0__0__3(B,
                                                                 C,
                                                                 D)
    ).
generate_empty_named_history_initialisation___1__0(A, B) :-
    (   var(B)
    ->  B=suspension(C, not_stored_yet, 0, _, A),
        'chr gen_id'(C)
    ;   true
    ),
    generate_empty_named_history_initialisation___1__1(A, B).
generate_empty_named_history_initialisation___1__1(_, A) :-
    arg(2, A, B),
    setarg(2, A, active),
    arg(3, A, C),
    D is C+1,
    setarg(3, A, D),
    (   B==not_stored_yet
    ->  A=suspension(_, _, _, _, E),
        term_variables(E, F),
        nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                  G),
        H=[A|G],
        b_setval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                 H),
        (   G=[I|_]
        ->  setarg(4, I, H)
        ;   true
        ),
        attach_generate_empty_named_history_initialisation___1(F,
                                                               A)
    ;   true
    ).
find_empty_named_histories :-
    find_empty_named_histories___0__0(_).
find_empty_named_histories___0__0(A) :-
    nb_getval('$chr_store_constants_chr_translate____history___3___[3]___[]',
              B),
    !,
    A=suspension(C, not_stored_yet, t, _),
    'chr gen_id'(C),
    find_empty_named_histories___0__0__0__1(B, A).
find_empty_named_histories___0__0__0__1([], A) :-
    find_empty_named_histories___0__1(A).
find_empty_named_histories___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, _, D, E),
        E=[],
        F=t(142, C, A),
        '$novel_production'(C, F),
        '$novel_production'(A, F)
    ->  '$extend_history'(C, F),
        arg(2, C, G),
        setarg(2, C, active),
        (   G==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                      H),
            I=[C|H],
            b_setval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                     I),
            (   H=[J|_]
            ->  setarg(4, J, I)
            ;   true
            )
        ;   true
        ),
        generate_empty_named_history_initialisation(D),
        (   C=suspension(_, active, _, _)
        ->  setarg(2, C, inactive),
            find_empty_named_histories___0__0__0__1(B, C)
        ;   true
        )
    ;   find_empty_named_histories___0__0__0__1(B, C)
    ).
find_empty_named_histories___0__0(A) :-
    A=suspension(B, not_stored_yet, t, _),
    'chr gen_id'(B),
    find_empty_named_histories___0__1(A).
find_empty_named_histories___0__1(A) :-
    nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
              B),
    !,
    find_empty_named_histories___0__1__0__2(B, A).
find_empty_named_histories___0__1__0__2([], A) :-
    find_empty_named_histories___0__2(A).
find_empty_named_histories___0__1__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _, _)
    ->  A=suspension(_, _, _, _, D),
        setarg(2, A, removed),
        term_variables(D, E),
        ( arg(4, A, F),
          (   var(F)
          ->  nb_getval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                        G),
              G=[_|H],
              b_setval('$chr_store_global_list_chr_translate____generate_empty_named_history_initialisation___1',
                       H),
              (   H=[I|_]
              ->  setarg(4, I, _)
              ;   true
              )
          ;   F=[_, _|H],
              setarg(2, F, H),
              (   H=[I|_]
              ->  setarg(4, I, F)
              ;   true
              )
          )
        ),
        detach_generate_empty_named_history_initialisation___1(E,
                                                               A),
        find_empty_named_histories___0__1__0__2(B, C)
    ;   find_empty_named_histories___0__1__0__2(B, C)
    ).
find_empty_named_histories___0__1(A) :-
    find_empty_named_histories___0__2(A).
find_empty_named_histories___0__2(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
              B),
    member(C, B),
    C=suspension(_, active, _, D, E),
    !,
    setarg(2, C, removed),
    arg(3, C, F),
    (   var(F)
    ->  nb_getval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
                  G),
        G=[_|H],
        b_setval('$chr_store_global_ground_chr_translate____empty_named_history_initialisations___2',
                 H),
        (   H=[I|_]
        ->  setarg(3, I, _)
        ;   true
        )
    ;   F=[_, _|H],
        setarg(2, F, H),
        (   H=[I|_]
        ->  setarg(3, I, F)
        ;   true
        )
    ),
    (   var(A)
    ->  true
    ;   arg(2, A, J),
        setarg(2, A, removed),
        (   J==not_stored_yet
        ->  true
        ;   arg(4, A, K),
            (   var(K)
            ->  nb_getval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                          L),
                L=[_|M],
                b_setval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                         M),
                (   M=[N|_]
                ->  setarg(4, N, _)
                ;   true
                )
            ;   K=[_, _|M],
                setarg(2, K, M),
                (   M=[N|_]
                ->  setarg(4, N, K)
                ;   true
                )
            )
        )
    ),
    D=E.
find_empty_named_histories___0__2(A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(4, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____find_empty_named_histories___0',
                         E),
                (   E=[F|_]
                ->  setarg(4, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(4, F, C)
                ;   true
                )
            )
        )
    ),
    chr_error(internal, 'find_empty_named_histories was not removed', []).
module_initializer(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____module_initializer___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____module_initializer___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
module_initializers(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____module_initializer___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____module_initializer___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____module_initializer___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    A=(D, I),
    module_initializers(I).
module_initializers(true).
actual_atomic_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, G),
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              H),
    lookup_ht(H, A, I),
    member(J, I),
    J=suspension(_, active, _),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              K),
    delete_ht(K, A, F),
    selectchk(multi_hash([B]), G, L),
    B=[M],
    (   get_constraint_arg_type(A, M, N),
        enumerated_atomic_type(N, O)
    ->  P=O,
        Q=complete
    ;   P=C,
        Q=incomplete
    ),
    actual_store_types(A,
                       [atomic_constants(B, P, Q)|L]).
actual_atomic_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              H),
    delete_ht(H, k(A, B), F),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              I),
    delete_ht(I, A, F),
    append(C, G, J),
    sort(J, K),
    actual_atomic_multi_hash_keys(A, B, K).
actual_atomic_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              H),
    delete_ht(H, k(A, B), F),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              I),
    delete_ht(I, A, F),
    append(G, C, J),
    sort(J, K),
    actual_ground_multi_hash_keys(A, B, K).
actual_atomic_multi_hash_keys(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
actual_atomic_multi_hash_keys(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              F),
    insert_ht(F, k(A, B), D),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              G),
    insert_ht(G, A, D).
actual_ground_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, G),
    nb_getval('$chr_store_multi_hash_chr_translate____validate_store_type_assumption___1-1',
              H),
    lookup_ht(H, A, I),
    member(J, I),
    J=suspension(_, active, _),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_store_types___2-1',
              K),
    delete_ht(K, A, F),
    (   B=[L],
        get_constraint_arg_type(A, L, M),
        M=chr_enum(N)
    ->  O=complete
    ;   N=C,
        O=incomplete
    ),
    selectchk(multi_hash([B]), G, P),
    actual_store_types(A,
                       [ground_constants(B, N, O)|P]).
actual_ground_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              H),
    delete_ht(H, k(A, B), F),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
              I),
    delete_ht(I, A, F),
    append(C, G, J),
    sort(J, K),
    actual_ground_multi_hash_keys(A, B, K).
actual_ground_multi_hash_keys(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              H),
    delete_ht(H, k(A, B), F),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              I),
    delete_ht(I, A, F),
    append(C, G, J),
    sort(J, K),
    actual_ground_multi_hash_keys(A, B, K).
actual_ground_multi_hash_keys(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
actual_ground_multi_hash_keys(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              F),
    insert_ht(F, k(A, B), D),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
              G),
    insert_ht(G, A, D).
actual_non_ground_multi_hash_key(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
actual_non_ground_multi_hash_key(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
              C),
    lookup_ht(C, k(A, B), D),
    !,
    actual_non_ground_multi_hash_key___2__0__0__3(D, A, B).
actual_non_ground_multi_hash_key___2__0__0__3([], A, B) :-
    actual_non_ground_multi_hash_key___2__1(A, B).
actual_non_ground_multi_hash_key___2__0__0__3([A|B], C, D) :-
    (   A=suspension(_, active, E, F, _),
        E==C,
        F==D
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-12',
                    G),
          delete_ht(G, k(C, D), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_atomic_multi_hash_keys___3-1',
                  H),
        delete_ht(H, C, A),
        actual_non_ground_multi_hash_key___2__0__0__3(B, C, D)
    ;   actual_non_ground_multi_hash_key___2__0__0__3(B, C, D)
    ).
actual_non_ground_multi_hash_key(A, B) :-
    actual_non_ground_multi_hash_key___2__1(A, B).
actual_non_ground_multi_hash_key___2__1(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
              C),
    lookup_ht(C, k(A, B), D),
    !,
    actual_non_ground_multi_hash_key___2__1__0__4(D, A, B).
actual_non_ground_multi_hash_key___2__1__0__4([], A, B) :-
    actual_non_ground_multi_hash_key___2__2(A, B).
actual_non_ground_multi_hash_key___2__1__0__4([A|B], C, D) :-
    (   A=suspension(_, active, E, F, _),
        E==C,
        F==D
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-12',
                    G),
          delete_ht(G, k(C, D), A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____actual_ground_multi_hash_keys___3-1',
                  H),
        delete_ht(H, C, A),
        actual_non_ground_multi_hash_key___2__1__0__4(B, C, D)
    ;   actual_non_ground_multi_hash_key___2__1__0__4(B, C, D)
    ).
actual_non_ground_multi_hash_key___2__1(A, B) :-
    actual_non_ground_multi_hash_key___2__2(A, B).
actual_non_ground_multi_hash_key___2__2(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____actual_non_ground_multi_hash_key___2-12',
              E),
    insert_ht(E, k(A, B), C).
symbol_count(A, B) :-
    symbol_count___2__0(A, B, _).
symbol_count___2__0(A, B, C) :-
    (   'chr newvia_1'(A, D)
    ->  get_attr(D, chr_translate, E),
        E=v(_, _, _, F, _, _, _, _, _, _, _)
    ;   nb_getval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                  F)
    ),
    member(G, F),
    G=suspension(_, active, _, _, _, H, I),
    H==A,
    !,
    G=suspension(_, _, _, _, _, J, _),
    setarg(2, G, removed),
    term_variables(J, K),
    arg(5, G, L),
    (   var(L)
    ->  nb_getval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                  M),
        M=[_|N],
        b_setval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                 N),
        (   N=[O|_]
        ->  setarg(5, O, _)
        ;   true
        )
    ;   L=[_, _|N],
        setarg(2, L, N),
        (   N=[O|_]
        ->  setarg(5, O, L)
        ;   true
        )
    ),
    detach_fresh_symbol___2(K, G),
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, P, _),
        setarg(2, C, removed),
        term_variables(P, Q),
        arg(3, C, R),
        (   var(R)
        ->  nb_getval('$chr_store_global_list_chr_translate____symbol_count___2',
                      S),
            S=[_|T],
            b_setval('$chr_store_global_list_chr_translate____symbol_count___2',
                     T),
            (   T=[U|_]
            ->  setarg(3, U, _)
            ;   true
            )
        ;   R=[_, _|T],
            setarg(2, R, T),
            (   T=[U|_]
            ->  setarg(3, U, R)
            ;   true
            )
        ),
        detach_symbol_count___2(Q, C)
    ),
    atom_concat(A, B, I),
    V is B+1,
    symbol_count(A, V).
symbol_count___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, active, _, A, B),
        term_variables(A, E),
        'chr gen_id'(D),
        nb_getval('$chr_store_global_list_chr_translate____symbol_count___2',
                  F),
        G=[C|F],
        b_setval('$chr_store_global_list_chr_translate____symbol_count___2',
                 G),
        (   F=[H|_]
        ->  setarg(3, H, G)
        ;   true
        ),
        attach_symbol_count___2(E, C)
    ;   setarg(2, C, active)
    ).
fresh_symbol(A, B) :-
    fresh_symbol___2__0(A, B, _).
fresh_symbol___2__0(A, B, C) :-
    (   'chr newvia_1'(A, D)
    ->  get_attr(D, chr_translate, E),
        E=v(_, _, F, _, _, _, _, _, _, _, _)
    ;   nb_getval('$chr_store_global_list_chr_translate____symbol_count___2',
                  F)
    ),
    member(G, F),
    G=suspension(_, active, _, H, I),
    H==A,
    !,
    G=suspension(_, _, _, J, _),
    setarg(2, G, removed),
    term_variables(J, K),
    arg(3, G, L),
    (   var(L)
    ->  nb_getval('$chr_store_global_list_chr_translate____symbol_count___2',
                  M),
        M=[_|N],
        b_setval('$chr_store_global_list_chr_translate____symbol_count___2',
                 N),
        (   N=[O|_]
        ->  setarg(3, O, _)
        ;   true
        )
    ;   L=[_, _|N],
        setarg(2, L, N),
        (   N=[O|_]
        ->  setarg(3, O, L)
        ;   true
        )
    ),
    detach_symbol_count___2(K, G),
    (   var(C)
    ->  true
    ;   C=suspension(_, P, _, _, _, Q, _),
        setarg(2, C, removed),
        (   P==not_stored_yet
        ->  R=[]
        ;   term_variables(Q, R),
            arg(5, C, S),
            (   var(S)
            ->  nb_getval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                          T),
                T=[_|U],
                b_setval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                         U),
                (   U=[V|_]
                ->  setarg(5, V, _)
                ;   true
                )
            ;   S=[_, _|U],
                setarg(2, S, U),
                (   U=[V|_]
                ->  setarg(5, V, S)
                ;   true
                )
            ),
            detach_fresh_symbol___2(R, C)
        )
    ),
    atom_concat(A, I, B),
    W is I+1,
    symbol_count(A, W).
fresh_symbol___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(D)
    ;   true
    ),
    (   '$novel_production'(C, 157),
        !,
        '$extend_history'(C, 157),
        arg(2, C, E),
        setarg(2, C, active),
        arg(4, C, F),
        G is F+1,
        setarg(4, C, G),
        (   E==not_stored_yet
        ->  C=suspension(_, _, _, _, _, H, _),
            term_variables(H, I),
            nb_getval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                      J),
            K=[C|J],
            b_setval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                     K),
            (   J=[L|_]
            ->  setarg(5, L, K)
            ;   true
            ),
            attach_fresh_symbol___2(I, C)
        ;   true
        ),
        symbol_count(A, 0),
        (   C=suspension(_, active, _, G, _, _, _)
        ->  setarg(2, C, inactive),
            fresh_symbol___2__1(A, B, C)
        ;   true
        )
    ;   fresh_symbol___2__1(A, B, C)
    ).
fresh_symbol___2__1(_, _, A) :-
    arg(2, A, B),
    setarg(2, A, active),
    arg(4, A, C),
    D is C+1,
    setarg(4, A, D),
    (   B==not_stored_yet
    ->  A=suspension(_, _, _, _, _, E, _),
        term_variables(E, F),
        nb_getval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                  G),
        H=[A|G],
        b_setval('$chr_store_global_list_chr_translate____fresh_symbol___2',
                 H),
        (   G=[I|_]
        ->  setarg(5, I, H)
        ;   true
        ),
        attach_fresh_symbol___2(F, A)
    ;   true
    ).
prolog_global_variable(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
prolog_global_variable(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1',
              G),
    insert_ht(G, A, B).
prolog_global_variables(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____prolog_global_variable___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____prolog_global_variable___1-1',
              I),
    delete_ht(I, D, C),
    A=[D|J],
    prolog_global_variables(J).
prolog_global_variables([]).
background_info(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____background_info___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____background_info___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____background_info___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    append(A, D, I),
    background_info(I).
background_info(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____background_info___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____background_info___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
background_info(A, B) :-
    background_info___2__0(A, B, _).
background_info___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____get_bg_info___2', D),
    !,
    (   var(C)
    ->  C=suspension(E, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(E)
    ;   true
    ),
    background_info___2__0__0__1(D, A, B, C).
background_info___2__0__0__1([], A, B, C) :-
    background_info___2__1(A, B, C).
background_info___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, _, F, _),
        G=t(164, E, A),
        '$novel_production'(E, G),
        '$novel_production'(A, G),
        copy_term_nat(C, H),
        subsumes_term(H, F)
    ->  '$extend_history'(E, G),
        arg(2, E, I),
        setarg(2, E, active),
        ( arg(4, E, J),
          K is J+1,
          setarg(4, E, K)
        ),
        (   I==not_stored_yet
        ->  E=suspension(_, _, _, _, _, L, _),
            term_variables(L, M),
            nb_getval('$chr_store_global_list_chr_translate____background_info___2',
                      N),
            O=[E|N],
            b_setval('$chr_store_global_list_chr_translate____background_info___2',
                     O),
            (   N=[P|_]
            ->  setarg(5, P, O)
            ;   true
            ),
            attach_background_info___2(M, E)
        ;   true
        ),
        copy_term_nat(C-D, F-Q),
        get_bg_info_answer([Q]),
        (   E=suspension(_, active, _, K, _, _, _)
        ->  setarg(2, E, inactive),
            background_info___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   background_info___2__0__0__1(B, C, D, E)
    ).
background_info___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(D)
    ;   true
    ),
    background_info___2__1(A, B, C).
background_info___2__1(_, _, A) :-
    arg(2, A, B),
    setarg(2, A, active),
    arg(4, A, C),
    D is C+1,
    setarg(4, A, D),
    (   B==not_stored_yet
    ->  A=suspension(_, _, _, _, _, E, _),
        term_variables(E, F),
        nb_getval('$chr_store_global_list_chr_translate____background_info___2',
                  G),
        H=[A|G],
        b_setval('$chr_store_global_list_chr_translate____background_info___2',
                 H),
        (   G=[I|_]
        ->  setarg(5, I, H)
        ;   true
        ),
        attach_background_info___2(F, A)
    ;   true
    ).
get_bg_info(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____background_info___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_bg_info([]).
get_bg_info(A, B) :-
    get_bg_info___2__0(A, B, _).
get_bg_info___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____background_info___2',
              D),
    !,
    (   var(C)
    ->  C=suspension(E, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(E)
    ;   true
    ),
    get_bg_info___2__0__0__1(D, A, B, C).
get_bg_info___2__0__0__1([], A, B, C) :-
    get_bg_info___2__1(A, B, C).
get_bg_info___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, _, F, G),
        H=t(164, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H),
        copy_term_nat(F, I),
        subsumes_term(I, C)
    ->  '$extend_history'(E, H),
        arg(2, E, J),
        setarg(2, E, active),
        ( arg(4, E, K),
          L is K+1,
          setarg(4, E, L)
        ),
        (   J==not_stored_yet
        ->  E=suspension(_, _, _, _, _, M, _),
            term_variables(M, N),
            nb_getval('$chr_store_global_list_chr_translate____get_bg_info___2',
                      O),
            P=[E|O],
            b_setval('$chr_store_global_list_chr_translate____get_bg_info___2',
                     P),
            (   O=[Q|_]
            ->  setarg(5, Q, P)
            ;   true
            ),
            attach_get_bg_info___2(N, E)
        ;   true
        ),
        copy_term_nat(F-G, C-R),
        get_bg_info_answer([R]),
        (   E=suspension(_, active, _, L, _, _, _)
        ->  setarg(2, E, inactive),
            get_bg_info___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   get_bg_info___2__0__0__1(B, C, D, E)
    ).
get_bg_info___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(D)
    ;   true
    ),
    get_bg_info___2__1(A, B, C).
get_bg_info___2__1(_, A, B) :-
    nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
              C),
    member(D, C),
    D=suspension(_, active, _, E),
    !,
    setarg(2, D, removed),
    arg(3, D, F),
    (   var(F)
    ->  nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
                  G),
        G=[_|H],
        b_setval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
                 H),
        (   H=[I|_]
        ->  setarg(3, I, _)
        ;   true
        )
    ;   F=[_, _|H],
        setarg(2, F, H),
        (   H=[I|_]
        ->  setarg(3, I, F)
        ;   true
        )
    ),
    (   var(B)
    ->  true
    ;   B=suspension(_, J, _, _, _, K, _),
        setarg(2, B, removed),
        (   J==not_stored_yet
        ->  L=[]
        ;   term_variables(K, L),
            arg(5, B, M),
            (   var(M)
            ->  nb_getval('$chr_store_global_list_chr_translate____get_bg_info___2',
                          N),
                N=[_|O],
                b_setval('$chr_store_global_list_chr_translate____get_bg_info___2',
                         O),
                (   O=[P|_]
                ->  setarg(5, P, _)
                ;   true
                )
            ;   M=[_, _|O],
                setarg(2, M, O),
                (   O=[P|_]
                ->  setarg(5, P, M)
                ;   true
                )
            ),
            detach_get_bg_info___2(L, B)
        )
    ),
    A=E.
get_bg_info___2__1(_, A, B) :-
    (   var(B)
    ->  true
    ;   B=suspension(_, C, _, _, _, D, _),
        setarg(2, B, removed),
        (   C==not_stored_yet
        ->  E=[]
        ;   term_variables(D, E),
            arg(5, B, F),
            (   var(F)
            ->  nb_getval('$chr_store_global_list_chr_translate____get_bg_info___2',
                          G),
                G=[_|H],
                b_setval('$chr_store_global_list_chr_translate____get_bg_info___2',
                         H),
                (   H=[I|_]
                ->  setarg(5, I, _)
                ;   true
                )
            ;   F=[_, _|H],
                setarg(2, F, H),
                (   H=[I|_]
                ->  setarg(5, I, F)
                ;   true
                )
            ),
            detach_get_bg_info___2(E, B)
        )
    ),
    A=[].
get_bg_info_answer(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    append(A, D, I),
    get_bg_info_answer(I).
get_bg_info_answer(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____get_bg_info_answer___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
prev_guard_list(A, B, C, D, E, F, G, H) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', I),
    lookup_ht(I, B, J),
    member(K, J),
    K=suspension(_, active, _, _, _, L),
    L=pragma(rule(M, N, O, _), _, _, _, _),
    M\==[],
    make_head_matchings_explicit(B, P, Q),
    setof(R,
          chr_translate:head_subset(P, D, R),
          S),
    !,
    append(M, N, T),
    compute_derived_info(S,
                         Q,
                         P,
                         T,
                         O,
                         G,
                         D,
                         H,
                         U,
                         V),
    append(F, U, W),
    normalize_conj_list(W, X),
    append(V, H, Y),
    normalize_conj_list(Y, Z),
    next_prev_rule(C, A1, B1),
    prev_guard_list(A,
                    A1,
                    B1,
                    D,
                    E,
                    X,
                    G,
                    Z).
prev_guard_list(A, B, C, D, E, F, G, H) :-
    (   B>0
    ->  next_prev_rule(C, I, J),
        prev_guard_list(A,
                        I,
                        J,
                        D,
                        E,
                        F,
                        G,
                        H)
    ;   prev_guard_list(A, D, E, F, G, H)
    ).
prev_guard_list(A, B, C, D, E, F) :-
    prev_guard_list___6__0(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           _).
prev_guard_list___6__0(A, B, C, D, E, F, _) :-
    F\==[],
    !,
    head_types_modes_condition(F, B, G),
    conj2list(G, H),
    term_variables(B, I),
    append([chr_pp_headvariables(I)|H], D, J),
    normalize_conj_list(J, K),
    append(B, K, L),
    add_background_info([C|L], M),
    append(K, M, N),
    normalize_conj_list(N, O),
    prev_guard_list(A, B, C, O, E, []).
prev_guard_list___6__0(A, B, C, D, E, F, G) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', H),
    lookup_ht(H, A, I),
    (   member(J, I),
        J=suspension(_, active, _, _, _, K),
        K=pragma(rule(L, M, C, N), O, P, Q, A),
        C\==true,
        append(E, D, R),
        (   conj2list(C, S),
            append(R, S, T),
            guard_entailment:entails_guard(T, fail)
        ->  U=fail
        ;   simplify_guard(C, N, R, U, V)
        ),
        C\==U,
        !,
        setarg(2, J, removed),
        arg(4, J, W),
        (   var(W)
        ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                      X),
            X=[_|Y],
            b_setval('$chr_store_global_ground_chr_translate____rule___2',
                     Y),
            (   Y=[Z|_]
            ->  setarg(4, Z, _)
            ;   true
            )
        ;   W=[_, _|Y],
            setarg(2, W, Y),
            (   Y=[Z|_]
            ->  setarg(4, Z, W)
            ;   true
            )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', A1),
        delete_ht(A1, A, J),
        rule(A,
             pragma(rule(L, M, U, V),
                    O,
                    P,
                    Q,
                    A)),
        prev_guard_list(A, B, U, D, E, [])
    ;   !,
        G=suspension(B1, not_stored_yet, t, _, A, B, C, D, E, F),
        'chr gen_id'(B1),
        prev_guard_list___6__0__0__3(I,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G)
    ).
prev_guard_list___6__0__0__3([], A, B, C, D, E, F, G) :-
    prev_guard_list___6__1(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__0__0__3([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K),
        J==C,
        L=t(178, I, A),
        '$novel_production'(I, L),
        '$novel_production'(A, L),
        chr_pp_flag(check_impossible_rules, on),
        K=pragma(rule(_, _, E, _), _, _, _, C),
        conj2list(E, M),
        append(G, F, N),
        append(N, M, O),
        guard_entailment:entails_guard(O, fail)
    ->  '$extend_history'(I, L),
        arg(2, I, P),
        setarg(2, I, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                      Q),
            R=[I|Q],
            b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                      T),
            insert_ht(T, C, I)
        ;   true
        ),
        chr_warning(weird_program,
                    'Heads will never match or guard will always fail in ~@.\n\tThis rule will never fire!\n',
                    [format_rule(K)]),
        set_all_passive(C),
        (   I=suspension(_, active, _, _, _, _, _, _, _, _)
        ->  setarg(2, I, inactive),
            prev_guard_list___6__0__0__3(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I)
        ;   true
        )
    ;   prev_guard_list___6__0__0__3(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ).
prev_guard_list___6__0(A, B, C, D, E, F, G) :-
    G=suspension(H, not_stored_yet, t, _, A, B, C, D, E, F),
    'chr gen_id'(H),
    prev_guard_list___6__1(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__1(A, B, C, D, E, F, G) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', H),
    lookup_ht(H, A, I),
    !,
    prev_guard_list___6__1__0__4(I,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G).
prev_guard_list___6__1__0__4([], A, B, C, D, E, F, G) :-
    prev_guard_list___6__2(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__1__0__4([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K),
        J==C,
        K=pragma(rule(L, M, E, N), O, P, Q, C),
        simplify_heads(G, F, E, N, R, S),
        R\==[],
        extract_arguments(L, T),
        extract_arguments(M, U),
        extract_arguments(D, V),
        replace_some_heads(T,
                           U,
                           V,
                           R,
                           W,
                           X,
                           E,
                           N,
                           Y),
        substitute_arguments(L, W, Z),
        substitute_arguments(M, X, A1),
        append(S, Y, B1),
        list2conj(B1, C1),
        D1=pragma(rule(Z, A1, E, (C1, N)), O, P, Q, C),
        (   L\==Z
        ;   M\==A1
        )
    ->  setarg(2, A, removed),
        ( arg(4, A, E1),
          (   var(E1)
          ->  nb_getval('$chr_store_global_ground_chr_translate____rule___2',
                        F1),
              F1=[_|G1],
              b_setval('$chr_store_global_ground_chr_translate____rule___2',
                       G1),
              (   G1=[H1|_]
              ->  setarg(4, H1, _)
              ;   true
              )
          ;   E1=[_, _|G1],
              setarg(2, E1, G1),
              (   G1=[H1|_]
              ->  setarg(4, H1, E1)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', I1),
        delete_ht(I1, C, A),
        arg(2, I, J1),
        setarg(2, I, active),
        (   J1==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                      K1),
            L1=[I|K1],
            b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                     L1),
            (   K1=[M1|_]
            ->  setarg(4, M1, L1)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                      N1),
            insert_ht(N1, C, I)
        ;   true
        ),
        rule(C, D1),
        (   I=suspension(_, active, _, _, _, _, _, _, _, _)
        ->  setarg(2, I, inactive),
            prev_guard_list___6__1__0__4(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I)
        ;   true
        )
    ;   prev_guard_list___6__1__0__4(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ).
prev_guard_list___6__1(A, B, C, D, E, F, G) :-
    prev_guard_list___6__2(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__2(A, B, C, D, E, F, G) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', H),
    lookup_ht(H, A, I),
    !,
    prev_guard_list___6__2__0__5(I,
                                 A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G).
prev_guard_list___6__2__0__5([], A, B, C, D, E, F, G) :-
    prev_guard_list___6__3(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__2__0__5([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K),
        J==C,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  L),
        lookup_ht(L, C, M)
    ->  prev_guard_list___6__2__1__5(M,
                                     K,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ;   prev_guard_list___6__2__0__5(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ).
prev_guard_list___6__2__1__5([], _, _, A, B, C, D, E, F, G, H) :-
    prev_guard_list___6__2__0__5(A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H).
prev_guard_list___6__2__1__5([A|B], C, D, E, F, G, H, I, J, K, L) :-
    (   A=suspension(_, active, _, _, M, N, O, _, _),
        O==F,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  P),
        lookup_ht(P, k(M, F), Q)
    ->  prev_guard_list___6__2__2__5(Q,
                                     M,
                                     N,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L)
    ;   prev_guard_list___6__2__1__5(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L)
    ).
prev_guard_list___6__2__2__5([], _, _, _, A, B, C, D, E, F, G, H, I, J, K) :-
    prev_guard_list___6__2__1__5(A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K).
prev_guard_list___6__2__2__5([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O, P) :-
    (   A=suspension(_, active, _, _, Q, R, S, T, _),
        A\==E,
        Q==C,
        S==J,
        nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                  U)
    ->  prev_guard_list___6__2__3__5(U,
                                     R,
                                     T,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N,
                                     O,
                                     P)
    ;   prev_guard_list___6__2__2__5(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N,
                                     O,
                                     P)
    ).
prev_guard_list___6__2__3__5([], _, _, _, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) :-
    prev_guard_list___6__2__2__5(A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H,
                                 I,
                                 J,
                                 K,
                                 L,
                                 M,
                                 N,
                                 O).
prev_guard_list___6__2__3__5([A|B], C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T) :-
    (   A=suspension(_, active, _, U),
        C<H,
        chr_pp_flag(occurrence_subsumption, on),
        K=pragma(rule(V, _, _, _), _, _, _, _),
        V\==[],
        \+ tree_set_memberchk(G, U)
    ->  setarg(2, A, removed),
        arg(3, A, W),
        (   var(W)
        ->  nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                      X),
            X=[_|Y],
            b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                     Y),
            (   Y=[Z|_]
            ->  setarg(3, Z, _)
            ;   true
            )
        ;   W=[_, _|Y],
            setarg(2, W, Y),
            (   Y=[Z|_]
            ->  setarg(3, Z, W)
            ;   true
            )
        ),
        arg(2, T, A1),
        setarg(2, T, active),
        (   A1==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                      B1),
            C1=[T|B1],
            b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                     C1),
            (   B1=[D1|_]
            ->  setarg(4, D1, C1)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                      E1),
            insert_ht(E1, N, T)
        ;   true
        ),
        first_occ_in_rule(N, G, C, D),
        tree_set_add(U, G, F1),
        multiple_occ_constraints_checked(F1),
        (   T=suspension(_, active, _, _, _, _, _, _, _, _)
        ->  setarg(2, T, inactive),
            prev_guard_list___6__2__3__5(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I,
                                         J,
                                         K,
                                         L,
                                         M,
                                         N,
                                         O,
                                         P,
                                         Q,
                                         R,
                                         S,
                                         T)
        ;   true
        )
    ;   prev_guard_list___6__2__3__5(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L,
                                     M,
                                     N,
                                     O,
                                     P,
                                     Q,
                                     R,
                                     S,
                                     T)
    ).
prev_guard_list___6__2(A, B, C, D, E, F, G) :-
    prev_guard_list___6__3(A,
                           B,
                           C,
                           D,
                           E,
                           F,
                           G).
prev_guard_list___6__3(A, _, _, _, _, _, B) :-
    nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
              C),
    member(D, C),
    D=suspension(_, active, _, _),
    !,
    setarg(2, D, removed),
    arg(3, D, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    (   var(B)
    ->  true
    ;   arg(2, B, I),
        setarg(2, B, removed),
        (   I==not_stored_yet
        ->  true
        ;   arg(4, B, J),
            (   var(J)
            ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                          K),
                K=[_|L],
                b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                         L),
                (   L=[M|_]
                ->  setarg(4, M, _)
                ;   true
                )
            ;   J=[_, _|L],
                setarg(2, J, L),
                (   L=[M|_]
                ->  setarg(4, M, J)
                ;   true
                )
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                      N),
            delete_ht(N, A, B)
        )
    ).
prev_guard_list___6__3(A, _, _, _, _, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                  D),
        E=[B|D],
        b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                 E),
        (   D=[F|_]
        ->  setarg(4, F, E)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                  G),
        insert_ht(G, A, B)
    ;   true
    ).
simplify_guards(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, _, E),
    !,
    E=pragma(rule(F, G, H, _), ids(I, J), _, _, A),
    append(F, G, K),
    make_head_matchings_explicit_not_negated(K, L, M),
    tree_set_empty(N),
    multiple_occ_constraints_checked(N),
    apply_guard_wrt_term(K, H, O),
    append(I, J, P),
    findall(tuple(Q, R, A)-(-A),
            ( member(S, P),
              get_occurrence_from_id(Q, R, A, S)
            ),
            T),
    empty_q(U),
    insert_list_q(T, U, V),
    next_prev_rule(V, _, W),
    next_prev_rule(W, X, Y),
    prev_guard_list(A,
                    X,
                    Y,
                    L,
                    H,
                    [],
                    M,
                    [O]),
    Z is A+1,
    simplify_guards(Z).
simplify_guards(_).
set_all_passive(A) :-
    set_all_passive___1__0(A, _).
set_all_passive___1__0(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3', C),
    lookup_ht(C, A, D),
    !,
    B=suspension(E, not_stored_yet, t, A),
    'chr gen_id'(E),
    set_all_passive___1__0__0__1(D, A, B).
set_all_passive___1__0__0__1([], A, B) :-
    set_all_passive___1__1(A, B).
set_all_passive___1__0__0__1([A|B], C, D) :-
    (   A=suspension(_, active, _, _, _, _, E, F, _),
        E==C,
        G=t(180, D, A),
        '$novel_production'(D, G),
        '$novel_production'(A, G)
    ->  '$extend_history'(D, G),
        arg(2, D, H),
        setarg(2, D, active),
        (   H==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
                      I),
            insert_ht(I, C, D)
        ;   true
        ),
        passive(C, F),
        (   D=suspension(_, active, _, _)
        ->  setarg(2, D, inactive),
            set_all_passive___1__0__0__1(B, C, D)
        ;   true
        )
    ;   set_all_passive___1__0__0__1(B, C, D)
    ).
set_all_passive___1__0(A, B) :-
    B=suspension(C, not_stored_yet, t, A),
    'chr gen_id'(C),
    set_all_passive___1__1(A, B).
set_all_passive___1__1(A, B) :-
    (   var(B)
    ->  true
    ;   arg(2, B, C),
        setarg(2, B, removed),
        (   C==not_stored_yet
        ->  true
        ;   nb_getval('$chr_store_multi_hash_chr_translate____set_all_passive___1-1',
                      D),
            delete_ht(D, A, B)
        )
    ).
precompute_head_matchings :-
    precompute_head_matchings___0__0(_).
precompute_head_matchings___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____rule___2', B),
    !,
    A=suspension(C, not_stored_yet, t, _),
    'chr gen_id'(C),
    precompute_head_matchings___0__0__0__1(B, A).
precompute_head_matchings___0__0__0__1([], A) :-
    precompute_head_matchings___0__1(A).
precompute_head_matchings___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, _, D, E),
        F=t(174, A, C),
        '$novel_production'(A, F),
        '$novel_production'(C, F)
    ->  '$extend_history'(C, F),
        arg(2, C, G),
        setarg(2, C, active),
        (   G==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
                      H),
            I=[C|H],
            b_setval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
                     I),
            (   H=[J|_]
            ->  setarg(4, J, I)
            ;   true
            )
        ;   true
        ),
        E=pragma(rule(K, L, _, _), _, _, _, _),
        append(K, L, M),
        make_head_matchings_explicit_(M, N, O),
        copy_term_nat(N-O, P-Q),
        make_head_matchings_explicit_memo_table(D, P, Q),
        (   C=suspension(_, active, _, _)
        ->  setarg(2, C, inactive),
            precompute_head_matchings___0__0__0__1(B, C)
        ;   true
        )
    ;   precompute_head_matchings___0__0__0__1(B, C)
    ).
precompute_head_matchings___0__0(A) :-
    A=suspension(B, not_stored_yet, t, _),
    'chr gen_id'(B),
    precompute_head_matchings___0__1(A).
precompute_head_matchings___0__1(A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(4, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____precompute_head_matchings___0',
                         E),
                (   E=[F|_]
                ->  setarg(4, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(4, F, C)
                ;   true
                )
            )
        )
    ).
make_head_matchings_explicit_memo_table(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1',
              F),
    insert_ht(F, A, D).
make_head_matchings_explicit_memo_lookup(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____make_head_matchings_explicit_memo_table___3-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, G, H),
    !,
    B=G,
    C=H.
make_head_matchings_explicit_memo_lookup(_, _, _) :-
    fail.
first_occ_in_rule(A, B, C, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
              D),
    lookup_ht(D, k(B, A), E),
    member(F, E),
    F=suspension(_, active, _, _, _, G, _, H, _),
    G<C,
    !,
    first_occ_in_rule(A, B, G, H).
first_occ_in_rule(A, B, C, D) :-
    B=E/F,
    functor(G, E, F),
    next_occ_in_rule(A, B, C, D, [], G).
next_occ_in_rule(A, B, C, D, _, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-1', F),
    lookup_ht(F, A, G),
    member(H, G),
    H=suspension(_, active, _, _, I),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-134',
              J),
    lookup_ht(J, k(B, A, I), K),
    member(L, K),
    L=suspension(_, active, _, _, _, M, _, _, _),
    M is C+1,
    !,
    next_occ_in_rule(A, B, M, D, _, E).
next_occ_in_rule(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
              G),
    lookup_ht(G, A, H),
    member(I, H),
    I=suspension(_, active, _, _, _, J, K, L, M, _),
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', N),
    lookup_ht(N, A, O),
    member(P, O),
    P=suspension(_, active, _, _, _, Q),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
              R),
    lookup_ht(R, k(B, A), S),
    member(T, S),
    T=suspension(_, active, _, _, _, U, _, V, _),
    U is C+1,
    Q=pragma(rule(W, X, K, _), ids(_, _), _, _, A),
    !,
    append(W, X, Y),
    add_failing_occ(Q,
                    Y,
                    J,
                    D,
                    Z,
                    F,
                    M,
                    B,
                    A1),
    (   Z==[chr_pp_void_info]
    ->  next_occ_in_rule(A, B, U, V, E, F)
    ;   append(Z, E, B1),
        add_failing_occ(Q,
                        Y,
                        J,
                        V,
                        C1,
                        F,
                        M,
                        B,
                        D1),
        copy_term(L, E1),
        variable_replacement(L, E1, F1),
        copy_with_variable_replacement(L, G1, A1),
        copy_with_variable_replacement(L, H1, D1),
        copy_with_variable_replacement(H1, I1, F1),
        append(B1, G1, J1),
        append(J1, I1, K1),
        copy_with_variable_replacement(M, L1, A1),
        copy_with_variable_replacement(M, M1, D1),
        append(M1, K1, N1),
        append([chr_pp_active_constraint(F)|L1], N1, O1),
        list2conj(C1, P1),
        copy_term((B1, K1, O1, P1, F),
                  (_, _, Q1, R1, _)),
        (   P1\=chr_pp_void_info
        ->  (   guard_entailment:entails_guard(Q1, R1)
            ->  passive(A, V)
            ;   true
            )
        ;   true
        ),
        !,
        next_occ_in_rule(A, B, U, V, B1, F)
    ).
next_occ_in_rule(_, _, _, _, _, _).
multiple_occ_constraints_checked(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
              B),
    member(C, B),
    C=suspension(_, active, _, _, D, _, _, _, _, _),
    (   nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  E),
        lookup_ht(E, D, F),
        member(G, F),
        G=suspension(_, active, _, _, H, I, _, _, _),
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', J),
        lookup_ht(J, D, K),
        member(L, K),
        L=suspension(_, active, _, _, _, M),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
                  N),
        lookup_ht(N, k(H, D), O),
        member(P, O),
        P=suspension(_, active, _, _, _, Q, _, R, _),
        P\==G,
        Q<I,
        chr_pp_flag(occurrence_subsumption, on),
        M=pragma(rule(S, _, _, _), _, _, _, _),
        S\==[],
        \+ tree_set_memberchk(H, A),
        !,
        first_occ_in_rule(D, H, Q, R),
        tree_set_add(A, H, T),
        multiple_occ_constraints_checked(T)
    ;   !,
        setarg(2, C, removed),
        arg(4, C, U),
        (   var(U)
        ->  nb_getval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                      V),
            V=[_|W],
            b_setval('$chr_store_global_ground_chr_translate____prev_guard_list___6',
                     W),
            (   W=[X|_]
            ->  setarg(4, X, _)
            ;   true
            )
        ;   U=[_, _|W],
            setarg(2, U, W),
            (   W=[X|_]
            ->  setarg(4, X, U)
            ;   true
            )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____prev_guard_list___6-1',
                  Y),
        delete_ht(Y, D, C)
    ).
multiple_occ_constraints_checked(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____multiple_occ_constraints_checked___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
type_definition(A, B) :-
    type_definition___2__0(A, B, _).
type_definition___2__0(A, _, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, _),
    (   functor(A, F, G),
        functor(E, F, G),
        !,
        D=suspension(_, _, _, _, _, H, I),
        setarg(2, D, removed),
        term_variables(term(H, I), J),
        arg(5, D, K),
        (   var(K)
        ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                      L),
            L=[_|M],
            b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                     M),
            (   M=[N|_]
            ->  setarg(5, N, _)
            ;   true
            )
        ;   K=[_, _|M],
            setarg(2, K, M),
            (   M=[N|_]
            ->  setarg(5, N, K)
            ;   true
            )
        ),
        detach_type_definition___2(J, D),
        (   var(B)
        ->  true
        ;   B=suspension(_, O, _, _, _, P, Q),
            setarg(2, B, removed),
            (   O==not_stored_yet
            ->  R=[]
            ;   term_variables(term(P, Q), R),
                arg(5, B, S),
                (   var(S)
                ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                              T),
                    T=[_|U],
                    b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                             U),
                    (   U=[V|_]
                    ->  setarg(5, V, _)
                    ;   true
                    )
                ;   S=[_, _|U],
                    setarg(2, S, U),
                    (   U=[V|_]
                    ->  setarg(5, V, S)
                    ;   true
                    )
                ),
                detach_type_definition___2(R, B)
            )
        ),
        chr_error(type_error, 'Multiple definitions for type: ~w/~w.\n', [F, G])
    ;   functor(E, W, X),
        functor(A, W, X),
        !,
        D=suspension(_, _, _, _, _, Y, Z),
        setarg(2, D, removed),
        term_variables(term(Y, Z), A1),
        arg(5, D, B1),
        (   var(B1)
        ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                      C1),
            C1=[_|D1],
            b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                     D1),
            (   D1=[E1|_]
            ->  setarg(5, E1, _)
            ;   true
            )
        ;   B1=[_, _|D1],
            setarg(2, B1, D1),
            (   D1=[E1|_]
            ->  setarg(5, E1, B1)
            ;   true
            )
        ),
        detach_type_definition___2(A1, D),
        (   var(B)
        ->  true
        ;   B=suspension(_, F1, _, _, _, G1, H1),
            setarg(2, B, removed),
            (   F1==not_stored_yet
            ->  I1=[]
            ;   term_variables(term(G1, H1), I1),
                arg(5, B, J1),
                (   var(J1)
                ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                              K1),
                    K1=[_|L1],
                    b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                             L1),
                    (   L1=[M1|_]
                    ->  setarg(5, M1, _)
                    ;   true
                    )
                ;   J1=[_, _|L1],
                    setarg(2, J1, L1),
                    (   L1=[M1|_]
                    ->  setarg(5, M1, J1)
                    ;   true
                    )
                ),
                detach_type_definition___2(I1, B)
            )
        ),
        chr_error(type_error, 'Multiple definitions for type: ~w/~w.\n', [W, X])
    ).
type_definition___2__0(A, _, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, _),
    functor(A, F, G),
    functor(E, F, G),
    !,
    D=suspension(_, _, _, _, _, H, I),
    setarg(2, D, removed),
    term_variables(term(H, I), J),
    arg(5, D, K),
    (   var(K)
    ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                  L),
        L=[_|M],
        b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                 M),
        (   M=[N|_]
        ->  setarg(5, N, _)
        ;   true
        )
    ;   K=[_, _|M],
        setarg(2, K, M),
        (   M=[N|_]
        ->  setarg(5, N, K)
        ;   true
        )
    ),
    detach_type_alias___2(J, D),
    (   var(B)
    ->  true
    ;   B=suspension(_, O, _, _, _, P, Q),
        setarg(2, B, removed),
        (   O==not_stored_yet
        ->  R=[]
        ;   term_variables(term(P, Q), R),
            arg(5, B, S),
            (   var(S)
            ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                          T),
                T=[_|U],
                b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                         U),
                (   U=[V|_]
                ->  setarg(5, V, _)
                ;   true
                )
            ;   S=[_, _|U],
                setarg(2, S, U),
                (   U=[V|_]
                ->  setarg(5, V, S)
                ;   true
                )
            ),
            detach_type_definition___2(R, B)
        )
    ),
    chr_error(type_error,
              'Multiple definitions for type, once in a type definition and once as a type alias: ~w/~w.\n',
              [F, G]).
type_definition___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
              D),
    !,
    (   var(C)
    ->  C=suspension(E, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(E)
    ;   true
    ),
    type_definition___2__0__0__5(D, A, B, C).
type_definition___2__0__0__5([], A, B, C) :-
    type_definition___2__1(A, B, C).
type_definition___2__0__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        nonvar(C),
        functor(C, H, I),
        functor(F, H, I)
    ->  A=suspension(_, _, _, J, _),
        setarg(2, A, removed),
        term_variables(J, K),
        ( arg(3, A, L),
          (   var(L)
          ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                        M),
              M=[_|N],
              b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                       N),
              (   N=[O|_]
              ->  setarg(3, O, _)
              ;   true
              )
          ;   L=[_, _|N],
              setarg(2, L, N),
              (   N=[O|_]
              ->  setarg(3, O, L)
              ;   true
              )
          )
        ),
        detach_unalias_type___2(K, A),
        arg(2, E, P),
        setarg(2, E, active),
        ( arg(4, E, Q),
          R is Q+1,
          setarg(4, E, R)
        ),
        (   P==not_stored_yet
        ->  E=suspension(_, _, _, _, _, S, T),
            term_variables(term(S, T), U),
            nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                      V),
            W=[E|V],
            b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                     W),
            (   V=[X|_]
            ->  setarg(5, X, W)
            ;   true
            ),
            attach_type_definition___2(U, E)
        ;   true
        ),
        G=F,
        (   E=suspension(_, active, _, R, _, _, _)
        ->  setarg(2, E, inactive),
            type_definition___2__0__0__5(B, C, D, E)
        ;   true
        )
    ;   type_definition___2__0__0__5(B, C, D, E)
    ).
type_definition___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(D)
    ;   true
    ),
    type_definition___2__1(A, B, C).
type_definition___2__1(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
              D),
    !,
    type_definition___2__1__0__7(D, A, B, C).
type_definition___2__1__0__7([], A, B, C) :-
    type_definition___2__2(A, B, C).
type_definition___2__1__0__7([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _),
        F=t(235, E, A),
        '$novel_production'(E, F),
        '$novel_production'(A, F)
    ->  '$extend_history'(E, F),
        arg(2, E, G),
        setarg(2, E, active),
        ( arg(4, E, H),
          I is H+1,
          setarg(4, E, I)
        ),
        (   G==not_stored_yet
        ->  E=suspension(_, _, _, _, _, J, K),
            term_variables(term(J, K), L),
            nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                      M),
            N=[E|M],
            b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                     N),
            (   M=[O|_]
            ->  setarg(5, O, N)
            ;   true
            ),
            attach_type_definition___2(L, E)
        ;   true
        ),
        copy_term_nat(C-D, P-Q),
        maplist(dynamic_type_check_clause(P), Q, R),
        dynamic_type_check_clauses(R),
        (   E=suspension(_, active, _, I, _, _, _)
        ->  setarg(2, E, inactive),
            type_definition___2__1__0__7(B, C, D, E)
        ;   true
        )
    ;   type_definition___2__1__0__7(B, C, D, E)
    ).
type_definition___2__1(A, B, C) :-
    type_definition___2__2(A, B, C).
type_definition___2__2(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____atomic_type___1', D),
    !,
    type_definition___2__2__0__8(D, A, B, C).
type_definition___2__2__0__8([], A, B, C) :-
    type_definition___2__3(A, B, C).
type_definition___2__2__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F),
        functor(F, G, H),
        functor(C, G, H)
    ->  A=suspension(_, _, _, I),
        setarg(2, A, removed),
        term_variables(I, J),
        ( arg(3, A, K),
          (   var(K)
          ->  nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                        L),
              L=[_|M],
              b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                       M),
              (   M=[N|_]
              ->  setarg(3, N, _)
              ;   true
              )
          ;   K=[_, _|M],
              setarg(2, K, M),
              (   M=[N|_]
              ->  setarg(3, N, K)
              ;   true
              )
          )
        ),
        detach_atomic_type___1(J, A),
        arg(2, E, O),
        setarg(2, E, active),
        ( arg(4, E, P),
          Q is P+1,
          setarg(4, E, Q)
        ),
        (   O==not_stored_yet
        ->  E=suspension(_, _, _, _, _, R, S),
            term_variables(term(R, S), T),
            nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                      U),
            V=[E|U],
            b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                     V),
            (   U=[W|_]
            ->  setarg(5, W, V)
            ;   true
            ),
            attach_type_definition___2(T, E)
        ;   true
        ),
        maplist(atomic, D),
        (   E=suspension(_, active, _, Q, _, _, _)
        ->  setarg(2, E, inactive),
            type_definition___2__2__0__8(B, C, D, E)
        ;   true
        )
    ;   type_definition___2__2__0__8(B, C, D, E)
    ).
type_definition___2__2(A, B, C) :-
    type_definition___2__3(A, B, C).
type_definition___2__3(_, _, A) :-
    arg(2, A, B),
    setarg(2, A, active),
    arg(4, A, C),
    D is C+1,
    setarg(4, A, D),
    (   B==not_stored_yet
    ->  A=suspension(_, _, _, _, _, E, F),
        term_variables(term(E, F), G),
        nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                  H),
        I=[A|H],
        b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                 I),
        (   H=[J|_]
        ->  setarg(5, J, I)
        ;   true
        ),
        attach_type_definition___2(G, A)
    ;   true
    ).
type_alias(A, B) :-
    type_alias___2__0(A, B, _).
type_alias___2__0(A, B, C) :-
    var(A),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, D, _, _, _, E, F),
        setarg(2, C, removed),
        (   D==not_stored_yet
        ->  G=[]
        ;   term_variables(term(E, F), G),
            arg(5, C, H),
            (   var(H)
            ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                          I),
                I=[_|J],
                b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                         J),
                (   J=[K|_]
                ->  setarg(5, K, _)
                ;   true
                )
            ;   H=[_, _|J],
                setarg(2, H, J),
                (   J=[K|_]
                ->  setarg(5, K, H)
                ;   true
                )
            ),
            detach_type_alias___2(G, C)
        )
    ),
    chr_error(type_error, 'Variable alias definition: "~w".\n', [(:-chr_type A==B)]).
type_alias___2__0(A, B, C) :-
    var(B),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, D, _, _, _, E, F),
        setarg(2, C, removed),
        (   D==not_stored_yet
        ->  G=[]
        ;   term_variables(term(E, F), G),
            arg(5, C, H),
            (   var(H)
            ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                          I),
                I=[_|J],
                b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                         J),
                (   J=[K|_]
                ->  setarg(5, K, _)
                ;   true
                )
            ;   H=[_, _|J],
                setarg(2, H, J),
                (   J=[K|_]
                ->  setarg(5, K, H)
                ;   true
                )
            ),
            detach_type_alias___2(G, C)
        )
    ),
    chr_error(type_error, 'Variable alias definition: "~w".\n', [(:-chr_type A==B)]).
type_alias___2__0(A, B, C) :-
    subsumes_term(A, B),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, D, _, _, _, E, F),
        setarg(2, C, removed),
        (   D==not_stored_yet
        ->  G=[]
        ;   term_variables(term(E, F), G),
            arg(5, C, H),
            (   var(H)
            ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                          I),
                I=[_|J],
                b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                         J),
                (   J=[K|_]
                ->  setarg(5, K, _)
                ;   true
                )
            ;   H=[_, _|J],
                setarg(2, H, J),
                (   J=[K|_]
                ->  setarg(5, K, H)
                ;   true
                )
            ),
            detach_type_alias___2(G, C)
        )
    ),
    chr_error(type_error, 'Cyclic alias definition: "~w".\n', [A==B]).
type_alias___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', D),
    (   member(E, D),
        E=suspension(_, active, _, _, _, F, G),
        (   (   functor(A, H, I),
                functor(F, H, I),
                \+ A\=F,
                !,
                E=suspension(_, _, _, _, _, J, K),
                setarg(2, E, removed),
                term_variables(term(J, K), L),
                arg(5, E, M),
                (   var(M)
                ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                              N),
                    N=[_|O],
                    b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                             O),
                    (   O=[P|_]
                    ->  setarg(5, P, _)
                    ;   true
                    )
                ;   M=[_, _|O],
                    setarg(2, M, O),
                    (   O=[P|_]
                    ->  setarg(5, P, M)
                    ;   true
                    )
                ),
                detach_type_alias___2(L, E),
                (   var(C)
                ->  true
                ;   C=suspension(_, Q, _, _, _, R, S),
                    setarg(2, C, removed),
                    (   Q==not_stored_yet
                    ->  T=[]
                    ;   term_variables(term(R, S), T),
                        arg(5, C, U),
                        (   var(U)
                        ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                                      V),
                            V=[_|W],
                            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                                     W),
                            (   W=[X|_]
                            ->  setarg(5, X, _)
                            ;   true
                            )
                        ;   U=[_, _|W],
                            setarg(2, U, W),
                            (   W=[X|_]
                            ->  setarg(5, X, U)
                            ;   true
                            )
                        ),
                        detach_type_alias___2(T, C)
                    )
                ),
                copy_term_nat(A, Y),
                copy_term_nat(F, Z),
                Y=Z,
                chr_error(type_error, 'Ambiguous type aliases: you have defined \n\t`~w\'\n\t`~w\'\n\tresulting in two definitions for "~w".\n', [A==B, F==G, Y])
            ;   functor(F, A1, B1),
                functor(A, A1, B1),
                \+ F\=A,
                !,
                E=suspension(_, _, _, _, _, C1, D1),
                setarg(2, E, removed),
                term_variables(term(C1, D1), E1),
                arg(5, E, F1),
                (   var(F1)
                ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                              G1),
                    G1=[_|H1],
                    b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                             H1),
                    (   H1=[I1|_]
                    ->  setarg(5, I1, _)
                    ;   true
                    )
                ;   F1=[_, _|H1],
                    setarg(2, F1, H1),
                    (   H1=[I1|_]
                    ->  setarg(5, I1, F1)
                    ;   true
                    )
                ),
                detach_type_alias___2(E1, E),
                (   var(C)
                ->  true
                ;   C=suspension(_, J1, _, _, _, K1, L1),
                    setarg(2, C, removed),
                    (   J1==not_stored_yet
                    ->  M1=[]
                    ;   term_variables(term(K1, L1), M1),
                        arg(5, C, N1),
                        (   var(N1)
                        ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                                      O1),
                            O1=[_|P1],
                            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                                     P1),
                            (   P1=[Q1|_]
                            ->  setarg(5, Q1, _)
                            ;   true
                            )
                        ;   N1=[_, _|P1],
                            setarg(2, N1, P1),
                            (   P1=[Q1|_]
                            ->  setarg(5, Q1, N1)
                            ;   true
                            )
                        ),
                        detach_type_alias___2(M1, C)
                    )
                ),
                copy_term_nat(F, R1),
                copy_term_nat(A, S1),
                R1=S1,
                chr_error(type_error, 'Ambiguous type aliases: you have defined \n\t`~w\'\n\t`~w\'\n\tresulting in two definitions for "~w".\n', [F==G, A==B, R1])
            )
        ;   functor(F, T1, U1),
            functor(B, T1, U1),
            copy_term_nat((A, B, F, G),
                          (V1, W1, X1, Y1)),
            subsumes_term(X1, W1),
            !,
            (   var(C)
            ->  true
            ;   C=suspension(_, Z1, _, _, _, A2, B2),
                setarg(2, C, removed),
                (   Z1==not_stored_yet
                ->  C2=[]
                ;   term_variables(term(A2, B2), C2),
                    arg(5, C, D2),
                    (   var(D2)
                    ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                                  E2),
                        E2=[_|F2],
                        b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                                 F2),
                        (   F2=[G2|_]
                        ->  setarg(5, G2, _)
                        ;   true
                        )
                    ;   D2=[_, _|F2],
                        setarg(2, D2, F2),
                        (   F2=[G2|_]
                        ->  setarg(5, G2, D2)
                        ;   true
                        )
                    ),
                    detach_type_alias___2(C2, C)
                )
            ),
            type_alias(V1, Y1)
        )
    ;   !,
        (   var(C)
        ->  C=suspension(H2, not_stored_yet, t, 0, _, A, B),
            'chr gen_id'(H2)
        ;   true
        ),
        type_alias___2__0__0__7(D, A, B, C)
    ).
type_alias___2__0__0__7([], A, B, C) :-
    type_alias___2__1(A, B, C).
type_alias___2__0__0__7([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, _, F, G),
        functor(C, H, I),
        functor(G, H, I),
        copy_term_nat((F, G, C, D),
                      (J, K, L, M)),
        subsumes_term(L, K)
    ->  A=suspension(_, _, _, _, _, N, O),
        setarg(2, A, removed),
        term_variables(term(N, O), P),
        ( arg(5, A, Q),
          (   var(Q)
          ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                        R),
              R=[_|S],
              b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                       S),
              (   S=[T|_]
              ->  setarg(5, T, _)
              ;   true
              )
          ;   Q=[_, _|S],
              setarg(2, Q, S),
              (   S=[T|_]
              ->  setarg(5, T, Q)
              ;   true
              )
          )
        ),
        detach_type_alias___2(P, A),
        arg(2, E, U),
        setarg(2, E, active),
        ( arg(4, E, V),
          W is V+1,
          setarg(4, E, W)
        ),
        (   U==not_stored_yet
        ->  E=suspension(_, _, _, _, _, X, Y),
            term_variables(term(X, Y), Z),
            nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                      A1),
            B1=[E|A1],
            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                     B1),
            (   A1=[C1|_]
            ->  setarg(5, C1, B1)
            ;   true
            ),
            attach_type_alias___2(Z, E)
        ;   true
        ),
        type_alias(J, M),
        (   E=suspension(_, active, _, W, _, _, _)
        ->  setarg(2, E, inactive),
            type_alias___2__0__0__7(B, C, D, E)
        ;   true
        )
    ;   type_alias___2__0__0__7(B, C, D, E)
    ).
type_alias___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, not_stored_yet, t, 0, _, A, B),
        'chr gen_id'(D)
    ;   true
    ),
    type_alias___2__1(A, B, C).
type_alias___2__1(A, _, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, _),
    functor(E, F, G),
    functor(A, F, G),
    !,
    D=suspension(_, _, _, _, _, H, I),
    setarg(2, D, removed),
    term_variables(term(H, I), J),
    arg(5, D, K),
    (   var(K)
    ->  nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
                  L),
        L=[_|M],
        b_setval('$chr_store_global_list_chr_translate____type_definition___2',
                 M),
        (   M=[N|_]
        ->  setarg(5, N, _)
        ;   true
        )
    ;   K=[_, _|M],
        setarg(2, K, M),
        (   M=[N|_]
        ->  setarg(5, N, K)
        ;   true
        )
    ),
    detach_type_definition___2(J, D),
    (   var(B)
    ->  true
    ;   B=suspension(_, O, _, _, _, P, Q),
        setarg(2, B, removed),
        (   O==not_stored_yet
        ->  R=[]
        ;   term_variables(term(P, Q), R),
            arg(5, B, S),
            (   var(S)
            ->  nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                          T),
                T=[_|U],
                b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                         U),
                (   U=[V|_]
                ->  setarg(5, V, _)
                ;   true
                )
            ;   S=[_, _|U],
                setarg(2, S, U),
                (   U=[V|_]
                ->  setarg(5, V, S)
                ;   true
                )
            ),
            detach_type_alias___2(R, B)
        )
    ),
    chr_error(type_error,
              'Multiple definitions for type, once in a type definition and once as a type alias: ~w/~w.\n',
              [F, G]).
type_alias___2__1(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
              D),
    !,
    type_alias___2__1__0__10(D, A, B, C).
type_alias___2__1__0__10([], A, B, C) :-
    type_alias___2__2(A, B, C).
type_alias___2__1__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        functor(C, H, I),
        functor(F, H, I),
        copy_term_nat((C, D), (J, K)),
        F=J
    ->  A=suspension(_, _, _, L, _),
        setarg(2, A, removed),
        term_variables(L, M),
        ( arg(3, A, N),
          (   var(N)
          ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                        O),
              O=[_|P],
              b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                       P),
              (   P=[Q|_]
              ->  setarg(3, Q, _)
              ;   true
              )
          ;   N=[_, _|P],
              setarg(2, N, P),
              (   P=[Q|_]
              ->  setarg(3, Q, N)
              ;   true
              )
          )
        ),
        detach_unalias_type___2(M, A),
        arg(2, E, R),
        setarg(2, E, active),
        ( arg(4, E, S),
          T is S+1,
          setarg(4, E, T)
        ),
        (   R==not_stored_yet
        ->  E=suspension(_, _, _, _, _, U, V),
            term_variables(term(U, V), W),
            nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                      X),
            Y=[E|X],
            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                     Y),
            (   X=[Z|_]
            ->  setarg(5, Z, Y)
            ;   true
            ),
            attach_type_alias___2(W, E)
        ;   true
        ),
        unalias_type(K, G),
        (   E=suspension(_, active, _, T, _, _, _)
        ->  setarg(2, E, inactive),
            type_alias___2__1__0__10(B, C, D, E)
        ;   true
        )
    ;   type_alias___2__1__0__10(B, C, D, E)
    ).
type_alias___2__1(A, B, C) :-
    type_alias___2__2(A, B, C).
type_alias___2__2(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
              D),
    !,
    type_alias___2__2__0__12(D, A, B, C).
type_alias___2__2__0__12([], A, B, C) :-
    type_alias___2__3(A, B, C).
type_alias___2__2__0__12([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H, I),
        functor(C, J, K),
        functor(H, J, K)
    ->  A=suspension(_, _, _, _, _, L, _),
        setarg(2, A, removed),
        term_variables(L, M),
        ( arg(3, A, N),
          (   var(N)
          ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                        O),
              O=[_|P],
              b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                       P),
              (   P=[Q|_]
              ->  setarg(3, Q, _)
              ;   true
              )
          ;   N=[_, _|P],
              setarg(2, N, P),
              (   P=[Q|_]
              ->  setarg(3, Q, N)
              ;   true
              )
          )
        ),
        detach_ast_static_type_check_var___4(M, A),
        arg(2, E, R),
        setarg(2, E, active),
        ( arg(4, E, S),
          T is S+1,
          setarg(4, E, T)
        ),
        (   R==not_stored_yet
        ->  E=suspension(_, _, _, _, _, U, V),
            term_variables(term(U, V), W),
            nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                      X),
            Y=[E|X],
            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                     Y),
            (   X=[Z|_]
            ->  setarg(5, Z, Y)
            ;   true
            ),
            attach_type_alias___2(W, E)
        ;   true
        ),
        copy_term_nat(C-D, H-A1),
        ast_static_type_check_var(F, G, A1, I),
        (   E=suspension(_, active, _, T, _, _, _)
        ->  setarg(2, E, inactive),
            type_alias___2__2__0__12(B, C, D, E)
        ;   true
        )
    ;   type_alias___2__2__0__12(B, C, D, E)
    ).
type_alias___2__2(A, B, C) :-
    type_alias___2__3(A, B, C).
type_alias___2__3(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
              D),
    !,
    type_alias___2__3__0__13(D, A, B, C).
type_alias___2__3__0__13([], A, B, C) :-
    type_alias___2__4(A, B, C).
type_alias___2__3__0__13([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _),
        F=t(236, E, A),
        '$novel_production'(E, F),
        '$novel_production'(A, F)
    ->  '$extend_history'(E, F),
        arg(2, E, G),
        setarg(2, E, active),
        ( arg(4, E, H),
          I is H+1,
          setarg(4, E, I)
        ),
        (   G==not_stored_yet
        ->  E=suspension(_, _, _, _, _, J, K),
            term_variables(term(J, K), L),
            nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                      M),
            N=[E|M],
            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                     N),
            (   M=[O|_]
            ->  setarg(5, O, N)
            ;   true
            ),
            attach_type_alias___2(L, E)
        ;   true
        ),
        copy_term_nat(C-D, P-Q),
        dynamic_type_check_alias_clause(P, Q, R),
        dynamic_type_check_clauses([R]),
        (   E=suspension(_, active, _, I, _, _, _)
        ->  setarg(2, E, inactive),
            type_alias___2__3__0__13(B, C, D, E)
        ;   true
        )
    ;   type_alias___2__3__0__13(B, C, D, E)
    ).
type_alias___2__3(A, B, C) :-
    type_alias___2__4(A, B, C).
type_alias___2__4(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____atomic_type___1', D),
    !,
    type_alias___2__4__0__14(D, A, B, C).
type_alias___2__4__0__14([], A, B, C) :-
    type_alias___2__5(A, B, C).
type_alias___2__4__0__14([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F),
        functor(F, G, H),
        functor(C, G, H)
    ->  A=suspension(_, _, _, I),
        setarg(2, A, removed),
        term_variables(I, J),
        ( arg(3, A, K),
          (   var(K)
          ->  nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                        L),
              L=[_|M],
              b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                       M),
              (   M=[N|_]
              ->  setarg(3, N, _)
              ;   true
              )
          ;   K=[_, _|M],
              setarg(2, K, M),
              (   M=[N|_]
              ->  setarg(3, N, K)
              ;   true
              )
          )
        ),
        detach_atomic_type___1(J, A),
        arg(2, E, O),
        setarg(2, E, active),
        ( arg(4, E, P),
          Q is P+1,
          setarg(4, E, Q)
        ),
        (   O==not_stored_yet
        ->  E=suspension(_, _, _, _, _, R, S),
            term_variables(term(R, S), T),
            nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                      U),
            V=[E|U],
            b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                     V),
            (   U=[W|_]
            ->  setarg(5, W, V)
            ;   true
            ),
            attach_type_alias___2(T, E)
        ;   true
        ),
        atomic(D),
        copy_term_nat(C-D, F-X),
        atomic_type(X),
        (   E=suspension(_, active, _, Q, _, _, _)
        ->  setarg(2, E, inactive),
            type_alias___2__4__0__14(B, C, D, E)
        ;   true
        )
    ;   type_alias___2__4__0__14(B, C, D, E)
    ).
type_alias___2__4(A, B, C) :-
    type_alias___2__5(A, B, C).
type_alias___2__5(_, _, A) :-
    arg(2, A, B),
    setarg(2, A, active),
    arg(4, A, C),
    D is C+1,
    setarg(4, A, D),
    (   B==not_stored_yet
    ->  A=suspension(_, _, _, _, _, E, F),
        term_variables(term(E, F), G),
        nb_getval('$chr_store_global_list_chr_translate____type_alias___2',
                  H),
        I=[A|H],
        b_setval('$chr_store_global_list_chr_translate____type_alias___2',
                 I),
        (   H=[J|_]
        ->  setarg(5, J, I)
        ;   true
        ),
        attach_type_alias___2(G, A)
    ;   true
    ).
constraint_type(A, B) :-
    constraint_type___2__0(A, B, _).
constraint_type___2__0(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____static_type_check___2',
              D),
    !,
    C=suspension(E, not_stored_yet, _, A, B),
    'chr gen_id'(E),
    constraint_type___2__0__0__2(D, A, B, C).
constraint_type___2__0__0__2([], A, B, C) :-
    constraint_type___2__1(A, B, C).
constraint_type___2__0__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, _)
    ->  arg(2, E, F),
        setarg(2, E, active),
        (   F==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_type___2',
                      G),
            H=[E|G],
            b_setval('$chr_store_global_ground_chr_translate____constraint_type___2',
                     H),
            (   G=[I|_]
            ->  setarg(3, I, H)
            ;   true
            ),
            nb_getval('$chr_store_multi_hash_chr_translate____constraint_type___2-1',
                      J),
            insert_ht(J, C, E)
        ;   true
        ),
        forall(( member(K, D),
                 sub_term(K, L)
               ),
               (   get_type_definition(L, _)
               ->  true
               ;   chr_error(type_error,
                             'Undefined type "~w" used in type declaration of "~w".\n',
                             [L, C])
               )),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            constraint_type___2__0__0__2(B, C, D, E)
        ;   true
        )
    ;   constraint_type___2__0__0__2(B, C, D, E)
    ).
constraint_type___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, _, A, B),
    'chr gen_id'(D),
    constraint_type___2__1(A, B, C).
constraint_type___2__1(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____constraint_type___2',
                  D),
        E=[B|D],
        b_setval('$chr_store_global_ground_chr_translate____constraint_type___2',
                 E),
        (   D=[F|_]
        ->  setarg(3, F, E)
        ;   true
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____constraint_type___2-1',
                  G),
        insert_ht(G, A, B)
    ;   true
    ).
get_type_definition(A, _) :-
    \+ ground(A),
    !,
    chr_error(type_error, 'Non-ground type in constraint definition: "~w".\n', [A]).
get_type_definition(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    functor(E, G, H),
    functor(A, G, H),
    copy_term_nat((E, F), (I, J)),
    I=A,
    !,
    (   get_type_definition(J, B)
    ->  true
    ;   chr_error(type_error,
                  'Could not find type definition for "~w" (accessed via alias "~w").\n',
                  [J, I]),
        fail
    ).
get_type_definition(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    nonvar(E),
    functor(E, G, H),
    functor(A, G, H),
    copy_term_nat((E, F), (I, J)),
    I=A,
    !,
    B=J.
get_type_definition(A, B) :-
    atomic_builtin_type(A, _, _),
    !,
    B=[A].
get_type_definition(A, B) :-
    compound_builtin_type(A, _, _, _),
    !,
    B=[A].
get_type_definition(_, _) :-
    fail.
get_constraint_type(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____constraint_type___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F),
    !,
    B=F.
get_constraint_type(_, _) :-
    fail.
unalias_type(A, B) :-
    unalias_type___2__0(A, B, _).
unalias_type___2__0(A, B, C) :-
    var(A),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, D, _),
        setarg(2, C, removed),
        term_variables(D, E),
        arg(3, C, F),
        (   var(F)
        ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                      G),
            G=[_|H],
            b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                     H),
            (   H=[I|_]
            ->  setarg(3, I, _)
            ;   true
            )
        ;   F=[_, _|H],
            setarg(2, F, H),
            (   H=[I|_]
            ->  setarg(3, I, F)
            ;   true
            )
        ),
        detach_unalias_type___2(E, C)
    ),
    B=A.
unalias_type___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', D),
    member(E, D),
    E=suspension(_, active, _, _, _, F, G),
    functor(F, H, I),
    functor(A, H, I),
    copy_term_nat((F, G), (J, K)),
    A=J,
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, L, _),
        setarg(2, C, removed),
        term_variables(L, M),
        arg(3, C, N),
        (   var(N)
        ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                      O),
            O=[_|P],
            b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                     P),
            (   P=[Q|_]
            ->  setarg(3, Q, _)
            ;   true
            )
        ;   N=[_, _|P],
            setarg(2, N, P),
            (   P=[Q|_]
            ->  setarg(3, Q, N)
            ;   true
            )
        ),
        detach_unalias_type___2(M, C)
    ),
    unalias_type(K, B).
unalias_type___2__0(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              D),
    member(E, D),
    E=suspension(_, active, _, _, _, F, _),
    nonvar(F),
    functor(F, G, H),
    functor(A, G, H),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, I, _),
        setarg(2, C, removed),
        term_variables(I, J),
        arg(3, C, K),
        (   var(K)
        ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                      L),
            L=[_|M],
            b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                     M),
            (   M=[N|_]
            ->  setarg(3, N, _)
            ;   true
            )
        ;   K=[_, _|M],
            setarg(2, K, M),
            (   M=[N|_]
            ->  setarg(3, N, K)
            ;   true
            )
        ),
        detach_unalias_type___2(J, C)
    ),
    B=A.
unalias_type___2__0(A, B, C) :-
    atomic_builtin_type(A, _, _),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, D, _),
        setarg(2, C, removed),
        term_variables(D, E),
        arg(3, C, F),
        (   var(F)
        ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                      G),
            G=[_|H],
            b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                     H),
            (   H=[I|_]
            ->  setarg(3, I, _)
            ;   true
            )
        ;   F=[_, _|H],
            setarg(2, F, H),
            (   H=[I|_]
            ->  setarg(3, I, F)
            ;   true
            )
        ),
        detach_unalias_type___2(E, C)
    ),
    B=A.
unalias_type___2__0(A, B, C) :-
    compound_builtin_type(A, _, _, _),
    !,
    (   var(C)
    ->  true
    ;   C=suspension(_, _, _, D, _),
        setarg(2, C, removed),
        term_variables(D, E),
        arg(3, C, F),
        (   var(F)
        ->  nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                      G),
            G=[_|H],
            b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                     H),
            (   H=[I|_]
            ->  setarg(3, I, _)
            ;   true
            )
        ;   F=[_, _|H],
            setarg(2, F, H),
            (   H=[I|_]
            ->  setarg(3, I, F)
            ;   true
            )
        ),
        detach_unalias_type___2(E, C)
    ),
    B=A.
unalias_type___2__0(A, B, C) :-
    (   var(C)
    ->  C=suspension(D, active, _, A, B),
        term_variables(A, E),
        'chr gen_id'(D),
        nb_getval('$chr_store_global_list_chr_translate____unalias_type___2',
                  F),
        G=[C|F],
        b_setval('$chr_store_global_list_chr_translate____unalias_type___2',
                 G),
        (   F=[H|_]
        ->  setarg(3, H, G)
        ;   true
        ),
        attach_unalias_type___2(E, C)
    ;   setarg(2, C, active)
    ).
types_modes_condition([], [], A) :-
    !,
    A=true.
types_modes_condition([A|B], [C|D], E) :-
    nb_getval('$chr_store_global_ground_chr_translate____constraint_mode___2',
              F),
    member(G, F),
    G=suspension(_, active, _, _, H, I),
    H=J/K,
    functor(A, J, K),
    !,
    A=..[_|L],
    E=(M, N, O),
    modes_condition(I, L, M),
    get_constraint_type_det(J/K, P),
    C=..[_|Q],
    types_condition(P, L, Q, I, N),
    types_modes_condition(B, D, O).
types_modes_condition([A|_], _, _) :-
    !,
    functor(A, B, C),
    chr_error(internal, 'Mode information missing for ~w.\n', [B/C]).
types_modes_condition(A, B, C) :-
    D=suspension(E, active, _, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
              F),
    G=[D|F],
    b_setval('$chr_store_global_ground_chr_translate____types_modes_condition___3',
             G),
    (   F=[H|_]
    ->  setarg(3, H, G)
    ;   true
    ).
static_type_check(A, B) :-
    static_type_check___2__0(A, B, _).
static_type_check___2__0(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____constraint_type___2',
              D),
    !,
    C=suspension(E, not_stored_yet, _, A, B),
    'chr gen_id'(E),
    static_type_check___2__0__0__1(D, A, B, C).
static_type_check___2__0__0__1([], A, B, C) :-
    static_type_check___2__1(A, B, C).
static_type_check___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G)
    ->  arg(2, E, H),
        setarg(2, E, active),
        (   H==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____static_type_check___2',
                      I),
            J=[E|I],
            b_setval('$chr_store_global_ground_chr_translate____static_type_check___2',
                     J),
            (   I=[K|_]
            ->  setarg(3, K, J)
            ;   true
            )
        ;   true
        ),
        forall(( member(L, G),
                 sub_term(L, M)
               ),
               (   get_type_definition(M, _)
               ->  true
               ;   chr_error(type_error,
                             'Undefined type "~w" used in type declaration of "~w".\n',
                             [M, F])
               )),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            static_type_check___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   static_type_check___2__0__0__1(B, C, D, E)
    ).
static_type_check___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, _, A, B),
    'chr gen_id'(D),
    static_type_check___2__1(A, B, C).
static_type_check___2__1(A, B, C) :-
    (   var(C)
    ->  true
    ;   arg(2, C, D),
        setarg(2, C, removed),
        (   D==not_stored_yet
        ->  true
        ;   arg(3, C, E),
            (   var(E)
            ->  nb_getval('$chr_store_global_ground_chr_translate____static_type_check___2',
                          F),
                F=[_|G],
                b_setval('$chr_store_global_ground_chr_translate____static_type_check___2',
                         G),
                (   G=[H|_]
                ->  setarg(3, H, _)
                ;   true
                )
            ;   E=[_, _|G],
                setarg(2, E, G),
                (   G=[H|_]
                ->  setarg(3, H, E)
                ;   true
                )
            )
        )
    ),
    maplist(static_type_check_rule, A, B).
ast_static_type_check_term(_, _, A) :-
    A==any,
    !.
ast_static_type_check_term(A, B, C) :-
    nonvar(B),
    B=var(D, E),
    !,
    ast_static_type_check_var(D, var(D, E), C, A).
ast_static_type_check_term(A, B, C) :-
    ast_atomic_builtin_type(C, B, D),
    !,
    (   call(D)
    ->  true
    ;   throw(type_error(invalid_functor(A, B, C)))
    ).
ast_static_type_check_term(A, B, C) :-
    ast_compound_builtin_type(C, B, D),
    !,
    (   call(D)
    ->  true
    ;   throw(type_error(invalid_functor(A, B, C)))
    ).
ast_static_type_check_term(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', D),
    member(E, D),
    E=suspension(_, active, _, _, _, F, G),
    functor(C, H, I),
    functor(F, H, I),
    !,
    copy_term_nat(F-G, C-J),
    ast_static_type_check_term(A, B, J).
ast_static_type_check_term(A, B, C) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              D),
    member(E, D),
    E=suspension(_, active, _, _, _, F, G),
    functor(C, H, I),
    functor(F, H, I),
    !,
    copy_term_nat(F-G, C-J),
    ast_functor(B, K, L),
    (   member(M, J),
        functor(M, K, L)
    ->  ast_args(B, N),
        M=..[_|O],
        maplist(ast_static_type_check_term(A), N, O)
    ;   throw(type_error(invalid_functor(A, B, C)))
    ).
ast_static_type_check_term(A, B, C) :-
    chr_error(internal, 'Undefined type ~w while type checking term ~w in ~@.\n', [C, B, chr_translate:format_src(A)]).
ast_static_type_check_var(A, B, C, D) :-
    ast_static_type_check_var___4__0(A, B, C, D, _).
ast_static_type_check_var___4__0(A, B, C, D, E) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', F),
    member(G, F),
    G=suspension(_, active, _, _, _, H, I),
    functor(H, J, K),
    functor(C, J, K),
    !,
    (   var(E)
    ->  true
    ;   E=suspension(_, _, _, _, _, L, _),
        setarg(2, E, removed),
        term_variables(L, M),
        arg(3, E, N),
        (   var(N)
        ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                      O),
            O=[_|P],
            b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                     P),
            (   P=[Q|_]
            ->  setarg(3, Q, _)
            ;   true
            )
        ;   N=[_, _|P],
            setarg(2, N, P),
            (   P=[Q|_]
            ->  setarg(3, Q, N)
            ;   true
            )
        ),
        detach_ast_static_type_check_var___4(M, E)
    ),
    copy_term_nat(H-I, C-R),
    ast_static_type_check_var(A, B, R, D).
ast_static_type_check_var___4__0(A, B, C, D, E) :-
    atomic_builtin_type(C, _, _),
    !,
    (   var(E)
    ->  true
    ;   E=suspension(_, _, _, _, _, F, _),
        setarg(2, E, removed),
        term_variables(F, G),
        arg(3, E, H),
        (   var(H)
        ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                      I),
            I=[_|J],
            b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                     J),
            (   J=[K|_]
            ->  setarg(3, K, _)
            ;   true
            )
        ;   H=[_, _|J],
            setarg(2, H, J),
            (   J=[K|_]
            ->  setarg(3, K, H)
            ;   true
            )
        ),
        detach_ast_static_type_check_var___4(G, E)
    ),
    ast_static_atomic_builtin_type_check_var(A, B, C, D).
ast_static_type_check_var___4__0(_, _, A, _, B) :-
    compound_builtin_type(A, _, _, _),
    !,
    (   var(B)
    ->  true
    ;   B=suspension(_, _, _, _, _, C, _),
        setarg(2, B, removed),
        term_variables(C, D),
        arg(3, B, E),
        (   var(E)
        ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                      F),
            F=[_|G],
            b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                     G),
            (   G=[H|_]
            ->  setarg(3, H, _)
            ;   true
            )
        ;   E=[_, _|G],
            setarg(2, E, G),
            (   G=[H|_]
            ->  setarg(3, H, E)
            ;   true
            )
        ),
        detach_ast_static_type_check_var___4(D, B)
    ).
ast_static_type_check_var___4__0(A, B, C, D, E) :-
    nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
              F),
    member(G, F),
    G=suspension(_, active, _, H, I, J, K),
    H=A,
    (   C\==J,
        !,
        G=suspension(_, _, _, _, _, L, _),
        setarg(2, G, removed),
        term_variables(L, M),
        arg(3, G, N),
        (   var(N)
        ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                      O),
            O=[_|P],
            b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                     P),
            (   P=[Q|_]
            ->  setarg(3, Q, _)
            ;   true
            )
        ;   N=[_, _|P],
            setarg(2, N, P),
            (   P=[Q|_]
            ->  setarg(3, Q, N)
            ;   true
            )
        ),
        detach_ast_static_type_check_var___4(M, G),
        (   var(E)
        ->  true
        ;   E=suspension(_, _, _, _, _, R, _),
            setarg(2, E, removed),
            term_variables(R, S),
            arg(3, E, T),
            (   var(T)
            ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                          U),
                U=[_|V],
                b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                         V),
                (   V=[W|_]
                ->  setarg(3, W, _)
                ;   true
                )
            ;   T=[_, _|V],
                setarg(2, T, V),
                (   V=[W|_]
                ->  setarg(3, W, T)
                ;   true
                )
            ),
            detach_ast_static_type_check_var___4(S, E)
        ),
        throw(type_error(type_clash(B, D, K, C, J)))
    ;   J\==C,
        !,
        G=suspension(_, _, _, _, _, X, _),
        setarg(2, G, removed),
        term_variables(X, Y),
        arg(3, G, Z),
        (   var(Z)
        ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                      A1),
            A1=[_|B1],
            b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                     B1),
            (   B1=[C1|_]
            ->  setarg(3, C1, _)
            ;   true
            )
        ;   Z=[_, _|B1],
            setarg(2, Z, B1),
            (   B1=[C1|_]
            ->  setarg(3, C1, Z)
            ;   true
            )
        ),
        detach_ast_static_type_check_var___4(Y, G),
        (   var(E)
        ->  true
        ;   E=suspension(_, _, _, _, _, D1, _),
            setarg(2, E, removed),
            term_variables(D1, E1),
            arg(3, E, F1),
            (   var(F1)
            ->  nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                          G1),
                G1=[_|H1],
                b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                         H1),
                (   H1=[I1|_]
                ->  setarg(3, I1, _)
                ;   true
                )
            ;   F1=[_, _|H1],
                setarg(2, F1, H1),
                (   H1=[I1|_]
                ->  setarg(3, I1, F1)
                ;   true
                )
            ),
            detach_ast_static_type_check_var___4(E1, E)
        ),
        throw(type_error(type_clash(I, K, D, J, C)))
    ).
ast_static_type_check_var___4__0(A, B, C, D, E) :-
    (   var(E)
    ->  E=suspension(F, active, _, A, B, C, D),
        term_variables(C, G),
        'chr gen_id'(F),
        nb_getval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                  H),
        I=[E|H],
        b_setval('$chr_store_global_list_chr_translate____ast_static_type_check_var___4',
                 I),
        (   H=[J|_]
        ->  setarg(3, J, I)
        ;   true
        ),
        attach_ast_static_type_check_var___4(G, E)
    ;   setarg(2, E, active)
    ).
ast_static_atomic_builtin_type_check_var(_, _, any, _) :-
    !.
ast_static_atomic_builtin_type_check_var(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              E),
    lookup_ht(E, k(A, C), F),
    (   member(G, F),
        G=suspension(_, active, _, _, _, _),
        !
    ;   !,
        ast_static_atomic_builtin_type_check_var___4__0__0__3(F,
                                                              A,
                                                              B,
                                                              C,
                                                              D)
    ).
ast_static_atomic_builtin_type_check_var___4__0__0__3([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__1(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__0__0__3([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H==E
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, E), A),
        ast_static_atomic_builtin_type_check_var___4__0__0__3(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ;   ast_static_atomic_builtin_type_check_var___4__0__0__3(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ).
ast_static_atomic_builtin_type_check_var(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__1(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__1(A, _, number, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, float), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__1(A, B, float, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, number), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__1__0__5(E,
                                                          A,
                                                          B,
                                                          float,
                                                          C).
ast_static_atomic_builtin_type_check_var___4__1__0__5([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__8(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__1__0__5([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=number
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, number), A),
        ast_static_atomic_builtin_type_check_var___4__1__0__5(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ;   ast_static_atomic_builtin_type_check_var___4__1__0__5(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ).
ast_static_atomic_builtin_type_check_var___4__1(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__2(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__2(A, _, number, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, int), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__2(A, B, int, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, number), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__2__0__7(E,
                                                          A,
                                                          B,
                                                          int,
                                                          C).
ast_static_atomic_builtin_type_check_var___4__2__0__7([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__5(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__2__0__7([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=number
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, number), A),
        ast_static_atomic_builtin_type_check_var___4__2__0__7(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ;   ast_static_atomic_builtin_type_check_var___4__2__0__7(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ).
ast_static_atomic_builtin_type_check_var___4__2(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__3(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__3(A, _, number, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, natural), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__3(A, B, natural, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, number), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__3__0__9(E,
                                                          A,
                                                          B,
                                                          natural,
                                                          C).
ast_static_atomic_builtin_type_check_var___4__3__0__9([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__5(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__3__0__9([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=number
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, number), A),
        ast_static_atomic_builtin_type_check_var___4__3__0__9(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ;   ast_static_atomic_builtin_type_check_var___4__3__0__9(B,
                                                              C,
                                                              D,
                                                              E,
                                                              F)
    ).
ast_static_atomic_builtin_type_check_var___4__3(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__4(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__4(A, _, number, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, dense_int), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__4(A, B, dense_int, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, number), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__4__0__11(E,
                                                           A,
                                                           B,
                                                           dense_int,
                                                           C).
ast_static_atomic_builtin_type_check_var___4__4__0__11([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__6(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__4__0__11([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=number
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, number), A),
        ast_static_atomic_builtin_type_check_var___4__4__0__11(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ;   ast_static_atomic_builtin_type_check_var___4__4__0__11(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ).
ast_static_atomic_builtin_type_check_var___4__4(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__5(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__5(A, _, int, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, natural), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__5(A, B, natural, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, int), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__5__0__13(E,
                                                           A,
                                                           B,
                                                           natural,
                                                           C).
ast_static_atomic_builtin_type_check_var___4__5__0__13([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__7(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__5__0__13([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=int
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, int), A),
        ast_static_atomic_builtin_type_check_var___4__5__0__13(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ;   ast_static_atomic_builtin_type_check_var___4__5__0__13(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ).
ast_static_atomic_builtin_type_check_var___4__5(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__6(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__6(A, _, int, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, dense_int), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__6(A, B, dense_int, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, int), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__6__0__15(E,
                                                           A,
                                                           B,
                                                           dense_int,
                                                           C).
ast_static_atomic_builtin_type_check_var___4__6__0__15([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__7(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__6__0__15([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=int
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, int), A),
        ast_static_atomic_builtin_type_check_var___4__6__0__15(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ;   ast_static_atomic_builtin_type_check_var___4__6__0__15(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ).
ast_static_atomic_builtin_type_check_var___4__6(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__7(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__7(A, _, natural, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              B),
    lookup_ht(B, k(A, dense_int), C),
    member(D, C),
    D=suspension(_, active, _, _, _, _),
    !.
ast_static_atomic_builtin_type_check_var___4__7(A, B, dense_int, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              D),
    lookup_ht(D, k(A, natural), E),
    !,
    ast_static_atomic_builtin_type_check_var___4__7__0__17(E,
                                                           A,
                                                           B,
                                                           dense_int,
                                                           C).
ast_static_atomic_builtin_type_check_var___4__7__0__17([], A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__8(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__7__0__17([A|B], C, D, E, F) :-
    (   A=suspension(_, active, G, _, H, _),
        G==C,
        H=natural
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
                    I),
          delete_ht(I, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
                  J),
        delete_ht(J, k(C, natural), A),
        ast_static_atomic_builtin_type_check_var___4__7__0__17(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ;   ast_static_atomic_builtin_type_check_var___4__7__0__17(B,
                                                               C,
                                                               D,
                                                               E,
                                                               F)
    ).
ast_static_atomic_builtin_type_check_var___4__7(A, B, C, D) :-
    ast_static_atomic_builtin_type_check_var___4__8(A,
                                                    B,
                                                    C,
                                                    D).
ast_static_atomic_builtin_type_check_var___4__8(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              E),
    lookup_ht(E, A, F),
    member(G, F),
    G=suspension(_, active, _, _, H, I),
    !,
    setarg(2, G, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              J),
    delete_ht(J, A, G),
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              K),
    delete_ht(K, k(A, H), G),
    throw(type_error(type_clash(B, D, I, C, H))).
ast_static_atomic_builtin_type_check_var___4__8(A, B, C, D) :-
    E=suspension(F, active, A, B, C, D),
    'chr gen_id'(F),
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-1',
              G),
    insert_ht(G, A, E),
    nb_getval('$chr_store_multi_hash_chr_translate____ast_static_atomic_builtin_type_check_var___4-13',
              H),
    insert_ht(H, k(A, C), E).
dynamic_type_check :-
    dynamic_type_check___0__0(_).
dynamic_type_check___0__0(A) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              B),
    !,
    A=suspension(C, not_stored_yet, t, _),
    'chr gen_id'(C),
    dynamic_type_check___0__0__0__1(B, A).
dynamic_type_check___0__0__0__1([], A) :-
    dynamic_type_check___0__1(A).
dynamic_type_check___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, _, _, D, E),
        F=t(235, A, C),
        '$novel_production'(A, F),
        '$novel_production'(C, F)
    ->  '$extend_history'(C, F),
        arg(2, C, G),
        setarg(2, C, active),
        (   G==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                      H),
            I=[C|H],
            b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                     I),
            (   H=[J|_]
            ->  setarg(4, J, I)
            ;   true
            )
        ;   true
        ),
        copy_term_nat(D-E, K-L),
        maplist(dynamic_type_check_clause(K), L, M),
        dynamic_type_check_clauses(M),
        (   C=suspension(_, active, _, _)
        ->  setarg(2, C, inactive),
            dynamic_type_check___0__0__0__1(B, C)
        ;   true
        )
    ;   dynamic_type_check___0__0__0__1(B, C)
    ).
dynamic_type_check___0__0(A) :-
    A=suspension(B, not_stored_yet, t, _),
    'chr gen_id'(B),
    dynamic_type_check___0__1(A).
dynamic_type_check___0__1(A) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', B),
    !,
    dynamic_type_check___0__1__0__2(B, A).
dynamic_type_check___0__1__0__2([], A) :-
    dynamic_type_check___0__2(A).
dynamic_type_check___0__1__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _, _, D, E),
        F=t(236, A, C),
        '$novel_production'(A, F),
        '$novel_production'(C, F)
    ->  '$extend_history'(C, F),
        arg(2, C, G),
        setarg(2, C, active),
        (   G==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                      H),
            I=[C|H],
            b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                     I),
            (   H=[J|_]
            ->  setarg(4, J, I)
            ;   true
            )
        ;   true
        ),
        copy_term_nat(D-E, K-L),
        dynamic_type_check_alias_clause(K, L, M),
        dynamic_type_check_clauses([M]),
        (   C=suspension(_, active, _, _)
        ->  setarg(2, C, inactive),
            dynamic_type_check___0__1__0__2(B, C)
        ;   true
        )
    ;   dynamic_type_check___0__1__0__2(B, C)
    ).
dynamic_type_check___0__1(A) :-
    dynamic_type_check___0__2(A).
dynamic_type_check___0__2(A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(4, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check___0',
                         E),
                (   E=[F|_]
                ->  setarg(4, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(4, F, C)
                ;   true
                )
            )
        )
    ),
    findall(('$dynamic_type_check'(G, H):-I),
            (   atomic_builtin_type(G, H, I)
            ;   compound_builtin_type(G, H, I, _)
            ),
            J),
    dynamic_type_check_clauses(J).
dynamic_type_check_clauses(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    append(A, D, I),
    dynamic_type_check_clauses(I).
dynamic_type_check_clauses(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_dynamic_type_check_clauses(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____dynamic_type_check_clauses___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    A=D.
get_dynamic_type_check_clauses([]).
atomic_type(A) :-
    atomic_type___1__0(A, _).
atomic_type___1__0(A, B) :-
    atomic_builtin_type(A, _, _),
    !,
    (   var(B)
    ->  true
    ;   B=suspension(_, _, _, C),
        setarg(2, B, removed),
        term_variables(C, D),
        arg(3, B, E),
        (   var(E)
        ->  nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                      F),
            F=[_|G],
            b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                     G),
            (   G=[H|_]
            ->  setarg(3, H, _)
            ;   true
            )
        ;   E=[_, _|G],
            setarg(2, E, G),
            (   G=[H|_]
            ->  setarg(3, H, E)
            ;   true
            )
        ),
        detach_atomic_type___1(D, B)
    ),
    A\==any.
atomic_type___1__0(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    functor(A, G, H),
    functor(E, G, H),
    !,
    (   var(B)
    ->  true
    ;   B=suspension(_, _, _, I),
        setarg(2, B, removed),
        term_variables(I, J),
        arg(3, B, K),
        (   var(K)
        ->  nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                      L),
            L=[_|M],
            b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                     M),
            (   M=[N|_]
            ->  setarg(3, N, _)
            ;   true
            )
        ;   K=[_, _|M],
            setarg(2, K, M),
            (   M=[N|_]
            ->  setarg(3, N, K)
            ;   true
            )
        ),
        detach_atomic_type___1(J, B)
    ),
    maplist(atomic, F).
atomic_type___1__0(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    functor(A, G, H),
    functor(E, G, H),
    !,
    (   var(B)
    ->  true
    ;   B=suspension(_, _, _, I),
        setarg(2, B, removed),
        term_variables(I, J),
        arg(3, B, K),
        (   var(K)
        ->  nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                      L),
            L=[_|M],
            b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                     M),
            (   M=[N|_]
            ->  setarg(3, N, _)
            ;   true
            )
        ;   K=[_, _|M],
            setarg(2, K, M),
            (   M=[N|_]
            ->  setarg(3, N, K)
            ;   true
            )
        ),
        detach_atomic_type___1(J, B)
    ),
    atomic(F),
    copy_term_nat(E-F, A-O),
    atomic_type(O).
atomic_type___1__0(A, B) :-
    (   var(B)
    ->  B=suspension(C, active, _, A),
        term_variables(A, D),
        'chr gen_id'(C),
        nb_getval('$chr_store_global_list_chr_translate____atomic_type___1',
                  E),
        F=[B|E],
        b_setval('$chr_store_global_list_chr_translate____atomic_type___1',
                 F),
        (   E=[G|_]
        ->  setarg(3, G, F)
        ;   true
        ),
        attach_atomic_type___1(D, B)
    ;   setarg(2, B, active)
    ).
enumerated_atomic_type(A, _) :-
    atomic_builtin_type(A, _, _),
    !,
    fail.
enumerated_atomic_type(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_definition___2',
              C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    functor(A, G, H),
    functor(E, G, H),
    !,
    maplist(atomic, F),
    B=F.
enumerated_atomic_type(A, B) :-
    nb_getval('$chr_store_global_list_chr_translate____type_alias___2', C),
    member(D, C),
    D=suspension(_, active, _, _, _, E, F),
    functor(A, G, H),
    functor(E, G, H),
    !,
    atomic(F),
    copy_term_nat(E-F, A-I),
    enumerated_atomic_type(I, B).
enumerated_atomic_type(_, _) :-
    fail.
stored(A, B, yes) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _, _, F, G, _),
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', H),
    lookup_ht(H, k(F, G), I),
    member(J, I),
    J=suspension(_, active, _, _, _),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              K),
    lookup_ht(K, A, L),
    member(M, L),
    M=suspension(_, active, _, _, N, O),
    B<N,
    !,
    setarg(2, M, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              P),
    delete_ht(P, k(A, O), M),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              Q),
    delete_ht(Q, A, M),
    R is O-1,
    stored(A, B, maybe),
    stored_complete(A, N, R).
stored(A, B, yes) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F, G),
    B<F,
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              H),
    lookup_ht(H, k(A, B), I),
    member(J, I),
    J=suspension(_, active, _, _),
    !,
    setarg(2, E, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              K),
    delete_ht(K, k(A, G), E),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              L),
    delete_ht(L, A, E),
    M is G-1,
    stored(A, B, maybe),
    stored_complete(A, F, M).
stored(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', F),
    insert_ht(F, k(A, C), D),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', G),
    insert_ht(G, k(A, B, C), D).
stored_completing(A, B, C) :-
    ground(A),
    ground(B),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', D),
    (   (   lookup_ht(D, k(A, B, yes), E),
            member(F, E),
            F=suspension(_, active, _, _, _),
            !,
            G is B+1,
            H is C+1,
            stored_completing(A, G, H)
        ;   lookup_ht(D, k(A, B, maybe), I),
            member(J, I),
            J=suspension(_, active, _, _, _),
            !,
            K is B+1,
            stored_completing(A, K, C)
        )
    ;   lookup_ht(D, k(A, B, no), L),
        member(M, L),
        M=suspension(_, active, _, _, _),
        !,
        stored_complete(A, B, C)
    ).
stored_completing(A, B, C) :-
    stored_complete(A, B, C).
stored_complete(A, B, C) :-
    stored_complete___3__0(A, B, C, _).
stored_complete___3__0(A, B, C, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, _, G, H, I, _),
    G<B,
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', J),
    lookup_ht(J, k(H, I), K),
    member(L, K),
    L=suspension(_, active, _, _, _),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', M),
    lookup_ht(M, k(A, G, yes), N),
    member(O, N),
    O=suspension(_, active, _, _, _),
    !,
    setarg(2, O, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', P),
    delete_ht(P, k(A, yes), O),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', Q),
    delete_ht(Q, k(A, G, yes), O),
    R is C-1,
    stored(A, G, maybe),
    stored_complete(A, B, R).
stored_complete___3__0(A, B, C, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', D),
    lookup_ht(D, k(A, yes), E),
    member(F, E),
    F=suspension(_, active, _, G, _),
    G<B,
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              H),
    lookup_ht(H, k(A, G), I),
    member(J, I),
    J=suspension(_, active, _, _),
    !,
    setarg(2, F, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13', K),
    delete_ht(K, k(A, yes), F),
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', L),
    delete_ht(L, k(A, G, yes), F),
    M is C-1,
    stored(A, G, maybe),
    stored_complete(A, B, M).
stored_complete___3__0(A, B, 0, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, t, A, B, 0),
    'chr gen_id'(F),
    stored_complete___3__0__0__3(E, A, B, 0, C).
stored_complete___3__0__0__3([], A, B, C, D) :-
    stored_complete___3__1(A, B, C, D).
stored_complete___3__0__0__3([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, H),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1',
                  I),
        lookup_ht(I, C, J)
    ->  stored_complete___3__0__1__3(J,
                                     H,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F)
    ;   stored_complete___3__0__0__3(B, C, D, E, F)
    ).
stored_complete___3__0__1__3([], _, _, A, B, C, D, E) :-
    stored_complete___3__0__0__3(A, B, C, D, E).
stored_complete___3__0__1__3([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, _, K, _, _),
        J==F,
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-3',
                  L),
        lookup_ht(L, K, M)
    ->  stored_complete___3__0__2__3(M,
                                     K,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ;   stored_complete___3__0__1__3(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ).
stored_complete___3__0__2__3([], _, _, A, B, C, D, E, F, G, H) :-
    stored_complete___3__0__1__3(A,
                                 B,
                                 C,
                                 D,
                                 E,
                                 F,
                                 G,
                                 H).
stored_complete___3__0__2__3([A|B], C, D, E, F, G, H, I, J, K, L) :-
    (   A=suspension(_, active, _, _, _, _, M, N, _),
        A\==D,
        M==C,
        O=t(250, A, D, L, G),
        '$novel_production'(A, O),
        '$novel_production'(D, O),
        '$novel_production'(L, O),
        '$novel_production'(G, O),
        J=<F
    ->  '$extend_history'(L, O),
        arg(2, L, P),
        setarg(2, L, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                      Q),
            insert_ht(Q, k(I, K), L),
            nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                      R),
            insert_ht(R, I, L)
        ;   true
        ),
        passive(C, N),
        (   L=suspension(_, active, _, _, _, _)
        ->  setarg(2, L, inactive),
            stored_complete___3__0__2__3(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I,
                                         J,
                                         K,
                                         L)
        ;   true
        )
    ;   stored_complete___3__0__2__3(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I,
                                     J,
                                     K,
                                     L)
    ).
stored_complete___3__0(A, B, C, D) :-
    D=suspension(E, not_stored_yet, t, A, B, C),
    'chr gen_id'(E),
    stored_complete___3__1(A, B, C, D).
stored_complete___3__1(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-1', E),
    lookup_ht(E, A, F),
    !,
    stored_complete___3__1__0__4(F, A, B, C, D).
stored_complete___3__1__0__4([], A, B, C, D) :-
    stored_complete___3__2(A, B, C, D).
stored_complete___3__1__0__4([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, _, G, H, I, J, _),
        G==C,
        K=t(259, F, A),
        '$novel_production'(F, K),
        '$novel_production'(A, K),
        H>D
    ->  '$extend_history'(F, K),
        arg(2, F, L),
        setarg(2, F, active),
        (   L==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                      M),
            insert_ht(M, k(C, E), F),
            nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                      N),
            insert_ht(N, C, F)
        ;   true
        ),
        passive(I, J),
        (   F=suspension(_, active, _, _, _, _)
        ->  setarg(2, F, inactive),
            stored_complete___3__1__0__4(B,
                                         C,
                                         D,
                                         E,
                                         F)
        ;   true
        )
    ;   stored_complete___3__1__0__4(B, C, D, E, F)
    ).
stored_complete___3__1(A, B, C, D) :-
    stored_complete___3__2(A, B, C, D).
stored_complete___3__2(A, _, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                  E),
        insert_ht(E, k(A, B), C),
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                  F),
        insert_ht(F, A, C)
    ;   true
    ).
is_stored(A) :-
    ground(A),
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, E),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
              F),
    lookup_ht(F, k(A, 0), G),
    member(H, G),
    H=suspension(_, active, _, _, I, _),
    I=<E,
    !,
    fail.
is_stored(_).
is_finally_stored(A) :-
    ground(A),
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, E),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
              F),
    lookup_ht(F, A, G),
    member(H, G),
    H=suspension(_, active, _, _, I, _),
    I=<E,
    !,
    fail.
is_finally_stored(_).
check_all_passive(A, [B|C]) :-
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _),
    !,
    check_all_passive(A, C).
check_all_passive(A, []) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _, _, E),
    !,
    chr_warning(weird_program, 'All heads passive in ~@.\n\tThis rule never fires. Please check your program.\n', [format_rule(E)]).
check_all_passive(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-12',
              E),
    insert_ht(E, k(A, B), C),
    nb_getval('$chr_store_multi_hash_chr_translate____check_all_passive___2-1',
              F),
    insert_ht(F, A, C).
constraints_code1([], A, B) :-
    !,
    A=B.
constraints_code1([A|B], C, D) :-
    !,
    constraint_code(A, C, E),
    constraints_code1(B, E, D).
constraints_code1(A, B, C) :-
    D=suspension(E, active, _, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_global_ground_chr_translate____constraints_code1___3',
              F),
    G=[D|F],
    b_setval('$chr_store_global_ground_chr_translate____constraints_code1___3',
             G),
    (   F=[H|_]
    ->  setarg(3, H, G)
    ;   true
    ).
constraint_code(A, B, C) :-
    (   (   chr_pp_flag(debugable, on)
        ;   is_stored(A),
            (   has_active_occurrence(A)
            ;   chr_pp_flag(late_allocation, off)
            ),
            (   may_trigger(A)
            ;   get_allocation_occurrence(A, D),
                get_max_occurrence(A, E),
                E>=D
            )
        )
    ->  constraint_prelude(A, F),
        add_dummy_location(F, G),
        B=[G|H]
    ;   B=H
    ),
    I=[0],
    occurrences_code(A, 1, I, J, H, K),
    gen_cond_attach_clause(A, J, K, C).
has_active_occurrence(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
has_active_occurrence(A) :-
    has_active_occurrence(A, 1),
    memo_has_active_occurrence(A).
has_active_occurrence(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F),
    B>F,
    !,
    fail.
has_active_occurrence(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _, _, F, G, _),
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', H),
    lookup_ht(H, k(F, G), I),
    member(J, I),
    J=suspension(_, active, _, _, _),
    !,
    K is B+1,
    has_active_occurrence(A, K).
has_active_occurrence(_, _).
memo_has_active_occurrence(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____memo_has_active_occurrence___1-1',
              D),
    insert_ht(D, A, B).
use_auxiliary_predicate(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
use_auxiliary_predicate(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              D),
    insert_ht(D, A, B).
use_auxiliary_predicate(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
use_auxiliary_predicate(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1',
              E),
    insert_ht(E, A, C),
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12',
              F),
    insert_ht(F, k(A, B), C).
is_used_auxiliary_predicate(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
is_used_auxiliary_predicate(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
is_used_auxiliary_predicate(_) :-
    fail.
is_used_auxiliary_predicate(A, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
is_used_auxiliary_predicate(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_predicate___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
is_used_auxiliary_predicate(_, _) :-
    fail.
use_auxiliary_module(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
use_auxiliary_module(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              D),
    insert_ht(D, A, B).
is_used_auxiliary_module(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____use_auxiliary_module___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
is_used_auxiliary_module(_) :-
    fail.
occurrences_code(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              G),
    lookup_ht(G, A, H),
    member(I, H),
    I=suspension(_, active, _, _, J),
    B>J,
    !,
    D=C,
    E=F.
occurrences_code(A, B, C, D, E, F) :-
    occurrence_code(A, B, C, G, E, H),
    I is B+1,
    occurrences_code(A, I, G, D, H, F).
occurrence_code(A, B, C, D, E, F) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              G),
    lookup_ht(G, k(A, B), H),
    member(I, H),
    I=suspension(_, active, _, _, _, _, J, K, _),
    (   nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  L),
        lookup_ht(L, k(J, K), M),
        member(N, M),
        N=suspension(_, active, _, _, _),
        !,
        (   named_history(J, _, _)
        ->  does_use_history(A, B)
        ;   true
        ),
        D=C,
        E=F
    ;   nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', O),
        lookup_ht(O, J, P),
        member(Q, P),
        Q=suspension(_, active, _, _, _, R),
        !,
        R=pragma(rule(S, T, _, _), ids(U, V), _, _, _),
        (   select2(K, W, U, S, X, Y)
        ->  D=C,
            head1_code(W,
                       K,
                       Y,
                       X,
                       R,
                       A,
                       B,
                       C,
                       E,
                       F)
        ;   select2(K, Z, V, T, A1, B1)
        ->  head2_code(Z,
                       K,
                       B1,
                       A1,
                       R,
                       A,
                       B,
                       C,
                       E,
                       C1),
            (   should_skip_to_next_id(A, B)
            ->  inc_id(C, D),
                (   unconditional_occurrence(A, B)
                ->  C1=F
                ;   gen_alloc_inc_clause(A,
                                         B,
                                         C,
                                         C1,
                                         F)
                )
            ;   D=C,
                C1=F
            )
        )
    ).
occurrence_code(A, B, _, _, _, _) :-
    chr_error(internal, 'occurrence_code/6: missing information to compile ~w:~w\n', [A, B]).
functional_dependency(A, B, C, D) :-
    B>1,
    nb_getval('$chr_store_multi_hash_chr_translate____allocation_occurrence___2-1',
              E),
    lookup_ht(E, A, F),
    member(G, F),
    G=suspension(_, active, _, _, H),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-13',
              I),
    lookup_ht(I, k(A, B), J),
    member(K, J),
    K=suspension(_, active, _, _, _, L, _, _, _),
    H>L,
    !,
    functional_dependency(A, 1, C, D).
functional_dependency(A, B, C, D) :-
    E=suspension(F, active, A, B, C, D),
    'chr gen_id'(F),
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              G),
    insert_ht(G, A, E),
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-12',
              H),
    insert_ht(H, k(A, B), E).
get_functional_dependency(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____functional_dependency___4-1',
              E),
    lookup_ht(E, A, F),
    member(G, F),
    G=suspension(_, active, _, H, I, J),
    B>=H,
    !,
    C=I,
    D=J.
get_functional_dependency(_, _, _, _) :-
    fail.
initial_call_pattern(A) :-
    initial_call_pattern___1__0(A, _).
initial_call_pattern___1__0(A, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
initial_call_pattern___1__0(A, B) :-
    B=suspension(C, active, t, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              D),
    insert_ht(D, A, B),
    call_pattern(A),
    (   B=suspension(_, active, _, _)
    ->  setarg(2, B, inactive),
        initial_call_pattern___1__1(A, B)
    ;   true
    ).
initial_call_pattern___1__1(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    initial_call_pattern___1__1__0__4(D, A, B).
initial_call_pattern___1__1__0__4([], A, B) :-
    initial_call_pattern___1__2(A, B).
initial_call_pattern___1__1__0__4([A|B], C, D) :-
    (   A=suspension(_, active, _, E, F),
        E==C,
        nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
                  G)
    ->  initial_call_pattern___1__1__1__4(G,
                                          F,
                                          A,
                                          B,
                                          C,
                                          D)
    ;   initial_call_pattern___1__1__0__4(B, C, D)
    ).
initial_call_pattern___1__1__1__4([], _, _, A, B, C) :-
    initial_call_pattern___1__1__0__4(A, B, C).
initial_call_pattern___1__1__1__4([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H),
        I=t(303, G, D, A),
        '$novel_production'(G, I),
        '$novel_production'(D, I)
    ->  '$extend_history'(G, I),
        setarg(2, G, active),
        ai_observation_schedule_new_calls(H, C),
        (   G=suspension(_, active, _, _)
        ->  setarg(2, G, inactive),
            initial_call_pattern___1__1__1__4(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G)
        ;   true
        )
    ;   initial_call_pattern___1__1__1__4(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G)
    ).
initial_call_pattern___1__1(A, B) :-
    initial_call_pattern___1__2(A, B).
initial_call_pattern___1__2(_, A) :-
    setarg(2, A, active).
call_pattern(A) :-
    call_pattern___1__0(A, _).
call_pattern___1__0(A, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____call_pattern___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
call_pattern___1__0(A, B) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____call_pattern___1-1',
              D),
    insert_ht(D, A, B),
    call_pattern_worker(A),
    (   B=suspension(_, active, _)
    ->  setarg(2, B, inactive),
        call_pattern___1__1(A, B)
    ;   true
    ).
call_pattern___1__1(_, A) :-
    setarg(2, A, active).
call_pattern_worker(odom([], A)) :-
    !,
    final_answer_pattern(odom([], A), odom([], A)).
call_pattern_worker(odom([A|B], C)) :-
    !,
    D=odom(A, C),
    depends_on_goal(odom([A|B], C), D),
    call_pattern(D).
call_pattern_worker(odom((A;B), C)) :-
    !,
    D=odom((A;B), C),
    E=odom([], C),
    final_answer_pattern(D, E),
    F=odom(A, C),
    G=odom(B, C),
    call_pattern(F),
    call_pattern(G),
    depends_on_as(D, F, G).
call_pattern_worker(odom(builtin, A)) :-
    !,
    ord_empty(B),
    final_answer_pattern(odom(builtin, A), odom([], B)).
call_pattern_worker(odom(occ(A, B), C)) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    B>G,
    !,
    final_answer_pattern(odom(occ(A, B), C), odom([], C)).
call_pattern_worker(odom(A, B)) :-
    A=_/_,
    !,
    C=odom(occ(A, 1), B),
    call_pattern(C),
    depends_on(odom(A, B), C).
call_pattern_worker(odom(occ(A, B), C)) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, H, _),
    is_passive(G, H),
    !,
    I is B+1,
    J=odom(occ(A, I), C),
    call_pattern(J),
    final_answer_pattern(odom(occ(A, B), C), odom([], C)),
    depends_on(odom(occ(A, B), C), J).
call_pattern_worker(odom(occ(A, B), C)) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
              D),
    (   lookup_ht(D, k(A, B, simplification), E),
        member(F, E),
        F=suspension(_, active, _, _, _, _, G, H, _),
        \+ is_passive(G, H),
        !,
        ai_observation_memo_simplification_rest_heads(A, B, I),
        ai_observation_observe_set(C, I, J),
        ai_observation_memo_abstract_goal(G, K),
        call_pattern(odom(K, J)),
        L is B+1,
        M=odom(occ(A, L), C),
        call_pattern(M),
        depends_on_as(odom(occ(A, B), C),
                      odom(K, J),
                      M),
        final_answer_pattern(odom(occ(A, B), C),
                             odom([], C))
    ;   lookup_ht(D, k(A, B, propagation), N),
        member(O, N),
        O=suspension(_, active, _, _, _, _, P, Q, _),
        \+ is_passive(P, Q),
        !,
        ai_observation_memo_propagation_rest_heads(A, B, R),
        ai_observation_observe_set(C, R, S),
        ord_add_element(S, A, T),
        ai_observation_memo_abstract_goal(P, U),
        call_pattern(odom(U, T)),
        (   ord_memberchk(A, S)
        ->  V=no
        ;   V=yes
        ),
        W is B+1,
        X=odom(occ(A, W), C),
        call_pattern(X),
        depends_on_ap(odom(occ(A, B), C),
                      odom(U, T),
                      X,
                      V)
    ).
call_pattern_worker(A) :-
    chr_error(internal, 'AI observation analysis: unexpected abstract state ~w\n', [A]).
final_answer_pattern(A, B) :-
    final_answer_pattern___2__0(A, B, _).
final_answer_pattern___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              D),
    lookup_ht(D, A, E),
    (   member(F, E),
        F=suspension(_, active, _, _, G),
        ai_observation_leq(B, G),
        !
    ;   !,
        final_answer_pattern___2__0__0__2(E, A, B, C)
    ).
final_answer_pattern___2__0__0__2([], A, B, C) :-
    final_answer_pattern___2__1(A, B, C).
final_answer_pattern___2__0__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==C,
        ai_observation_leq(G, D)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  H),
        delete_ht(H, C, A),
        final_answer_pattern___2__0__0__2(B, C, D, E)
    ;   final_answer_pattern___2__0__0__2(B, C, D, E)
    ).
final_answer_pattern___2__0(A, B, C) :-
    final_answer_pattern___2__1(A, B, C).
final_answer_pattern___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____initial_call_pattern___1-1',
              D),
    lookup_ht(D, A, E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    final_answer_pattern___2__1__0__3(E, A, B, C).
final_answer_pattern___2__1__0__3([], A, B, C) :-
    final_answer_pattern___2__2(A, B, C).
final_answer_pattern___2__1__0__3([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F),
        F==C,
        nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
                  G)
    ->  final_answer_pattern___2__1__1__3(G,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   final_answer_pattern___2__1__0__3(B, C, D, E)
    ).
final_answer_pattern___2__1__1__3([], _, A, B, C, D) :-
    final_answer_pattern___2__1__0__3(A, B, C, D).
final_answer_pattern___2__1__1__3([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H),
        I=t(303, C, G, A),
        '$novel_production'(C, I),
        '$novel_production'(G, I)
    ->  '$extend_history'(G, I),
        arg(2, G, J),
        setarg(2, G, active),
        (   J==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      K),
            insert_ht(K, E, G)
        ;   true
        ),
        ai_observation_schedule_new_calls(H, F),
        (   G=suspension(_, active, _, _, _)
        ->  setarg(2, G, inactive),
            final_answer_pattern___2__1__1__3(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G)
        ;   true
        )
    ;   final_answer_pattern___2__1__1__3(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G)
    ).
final_answer_pattern___2__1(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    final_answer_pattern___2__2(A, B, C).
final_answer_pattern___2__2(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2', D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__2__0__4(E, A, B, C).
final_answer_pattern___2__2__0__4([], A, B, C) :-
    final_answer_pattern___2__3(A, B, C).
final_answer_pattern___2__2__0__4([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        G==C,
        H=t(305, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H)
    ->  '$extend_history'(E, H),
        arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      J),
            insert_ht(J, C, E)
        ;   true
        ),
        final_answer_pattern(F, D),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            final_answer_pattern___2__2__0__4(B, C, D, E)
        ;   true
        )
    ;   final_answer_pattern___2__2__0__4(B, C, D, E)
    ).
final_answer_pattern___2__2(A, B, C) :-
    final_answer_pattern___2__3(A, B, C).
final_answer_pattern___2__3(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__3__0__5(E, A, B, C).
final_answer_pattern___2__3__0__5([], A, B, C) :-
    final_answer_pattern___2__4(A, B, C).
final_answer_pattern___2__3__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
                  H),
        lookup_ht(H, F, I)
    ->  final_answer_pattern___2__3__1__5(I,
                                          F,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   final_answer_pattern___2__3__0__5(B, C, D, E)
    ).
final_answer_pattern___2__3__1__5([], _, _, A, B, C, D) :-
    final_answer_pattern___2__3__0__5(A, B, C, D).
final_answer_pattern___2__3__1__5([A|B], C, D, E, F, G, H) :-
    (   A=suspension(_, active, _, I, J),
        I==C
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
                    K),
          delete_ht(K, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2',
                  L),
        delete_ht(L, J, A),
        final_answer_pattern___2__3__1__5(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H)
    ;   final_answer_pattern___2__3__1__5(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H)
    ).
final_answer_pattern___2__3(A, B, C) :-
    final_answer_pattern___2__4(A, B, C).
final_answer_pattern___2__4(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__4__0__6(E, A, B, C).
final_answer_pattern___2__4__0__6([], A, B, C) :-
    final_answer_pattern___2__5(A, B, C).
final_answer_pattern___2__4__0__6([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        G==C,
        H=t(310, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H)
    ->  '$extend_history'(E, H),
        arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      J),
            insert_ht(J, C, E)
        ;   true
        ),
        F=odom([_|K], _),
        D=odom([], L),
        M=odom(K, L),
        call_pattern(M),
        depends_on(F, M),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            final_answer_pattern___2__4__0__6(B, C, D, E)
        ;   true
        )
    ;   final_answer_pattern___2__4__0__6(B, C, D, E)
    ).
final_answer_pattern___2__4(A, B, C) :-
    final_answer_pattern___2__5(A, B, C).
final_answer_pattern___2__5(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-2',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__5__0__7(E, A, B, C).
final_answer_pattern___2__5__0__7([], A, B, C) :-
    final_answer_pattern___2__6(A, B, C).
final_answer_pattern___2__5__0__7([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  I),
        lookup_ht(I, H, J)
    ->  final_answer_pattern___2__5__1__7(J,
                                          F,
                                          H,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   final_answer_pattern___2__5__0__7(B, C, D, E)
    ).
final_answer_pattern___2__5__1__7([], _, _, _, A, B, C, D) :-
    final_answer_pattern___2__5__0__7(A, B, C, D).
final_answer_pattern___2__5__1__7([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K),
        J==D,
        L=t(317, E, I, A),
        '$novel_production'(E, L),
        '$novel_production'(I, L),
        '$novel_production'(A, L)
    ->  '$extend_history'(I, L),
        arg(2, I, M),
        setarg(2, I, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      N),
            insert_ht(N, G, I)
        ;   true
        ),
        ai_observation_lub(H, K, O),
        final_answer_pattern(C, O),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            final_answer_pattern___2__5__1__7(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G,
                                              H,
                                              I)
        ;   true
        )
    ;   final_answer_pattern___2__5__1__7(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H,
                                          I)
    ).
final_answer_pattern___2__5(A, B, C) :-
    final_answer_pattern___2__6(A, B, C).
final_answer_pattern___2__6(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__6__0__8(E, A, B, C).
final_answer_pattern___2__6__0__8([], A, B, C) :-
    final_answer_pattern___2__7(A, B, C).
final_answer_pattern___2__6__0__8([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H),
        H==C,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  I),
        lookup_ht(I, G, J)
    ->  final_answer_pattern___2__6__1__8(J,
                                          F,
                                          G,
                                          A,
                                          B,
                                          C,
                                          D,
                                          E)
    ;   final_answer_pattern___2__6__0__8(B, C, D, E)
    ).
final_answer_pattern___2__6__1__8([], _, _, _, A, B, C, D) :-
    final_answer_pattern___2__6__0__8(A, B, C, D).
final_answer_pattern___2__6__1__8([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K),
        J==D,
        L=t(317, E, A, I),
        '$novel_production'(E, L),
        '$novel_production'(A, L),
        '$novel_production'(I, L)
    ->  '$extend_history'(I, L),
        arg(2, I, M),
        setarg(2, I, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      N),
            insert_ht(N, G, I)
        ;   true
        ),
        ai_observation_lub(K, H, O),
        final_answer_pattern(C, O),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            final_answer_pattern___2__6__1__8(B,
                                              C,
                                              D,
                                              E,
                                              F,
                                              G,
                                              H,
                                              I)
        ;   true
        )
    ;   final_answer_pattern___2__6__1__8(B,
                                          C,
                                          D,
                                          E,
                                          F,
                                          G,
                                          H,
                                          I)
    ).
final_answer_pattern___2__6(A, B, C) :-
    final_answer_pattern___2__7(A, B, C).
final_answer_pattern___2__7(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__7__0__9(E, A, B, C).
final_answer_pattern___2__7__0__9([], A, B, C) :-
    final_answer_pattern___2__8(A, B, C).
final_answer_pattern___2__7__0__9([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, _, G, _),
        G==C,
        H=t(325, A, E),
        '$novel_production'(A, H),
        '$novel_production'(E, H)
    ->  '$extend_history'(E, H),
        arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      J),
            insert_ht(J, C, E)
        ;   true
        ),
        final_answer_pattern(F, D),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            final_answer_pattern___2__7__0__9(B, C, D, E)
        ;   true
        )
    ;   final_answer_pattern___2__7__0__9(B, C, D, E)
    ).
final_answer_pattern___2__7(A, B, C) :-
    final_answer_pattern___2__8(A, B, C).
final_answer_pattern___2__8(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__8__0__10(E, A, B, C).
final_answer_pattern___2__8__0__10([], A, B, C) :-
    final_answer_pattern___2__9(A, B, C).
final_answer_pattern___2__8__0__10([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H, I),
        G==C,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  J),
        lookup_ht(J, H, K)
    ->  final_answer_pattern___2__8__1__10(K,
                                           F,
                                           H,
                                           I,
                                           A,
                                           B,
                                           C,
                                           D,
                                           E)
    ;   final_answer_pattern___2__8__0__10(B, C, D, E)
    ).
final_answer_pattern___2__8__1__10([], _, _, _, _, A, B, C, D) :-
    final_answer_pattern___2__8__0__10(A, B, C, D).
final_answer_pattern___2__8__1__10([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, K, L),
        K==D,
        M=t(326, F, J, A),
        '$novel_production'(F, M),
        '$novel_production'(J, M),
        '$novel_production'(A, M)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      O),
            insert_ht(O, H, J)
        ;   true
        ),
        C=odom(occ(P, Q), _),
        (   ai_observation_is_observed(I, P)
        ->  ai_observed_internal(P, Q)
        ;   ai_not_observed_internal(P, Q)
        ),
        (   E==yes
        ->  I=odom([], R),
            ord_del_element(R, P, S),
            T=odom([], S)
        ;   T=I
        ),
        ai_observation_lub(T, L, U),
        final_answer_pattern(C, U),
        (   J=suspension(_, active, _, _, _)
        ->  setarg(2, J, inactive),
            final_answer_pattern___2__8__1__10(B,
                                               C,
                                               D,
                                               E,
                                               F,
                                               G,
                                               H,
                                               I,
                                               J)
        ;   true
        )
    ;   final_answer_pattern___2__8__1__10(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I,
                                           J)
    ).
final_answer_pattern___2__8(A, B, C) :-
    final_answer_pattern___2__9(A, B, C).
final_answer_pattern___2__9(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
              D),
    lookup_ht(D, A, E),
    !,
    final_answer_pattern___2__9__0__11(E, A, B, C).
final_answer_pattern___2__9__0__11([], A, B, C) :-
    final_answer_pattern___2__10(A, B, C).
final_answer_pattern___2__9__0__11([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G, H, I),
        H==C,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  J),
        lookup_ht(J, G, K)
    ->  final_answer_pattern___2__9__1__11(K,
                                           F,
                                           G,
                                           I,
                                           A,
                                           B,
                                           C,
                                           D,
                                           E)
    ;   final_answer_pattern___2__9__0__11(B, C, D, E)
    ).
final_answer_pattern___2__9__1__11([], _, _, _, _, A, B, C, D) :-
    final_answer_pattern___2__9__0__11(A, B, C, D).
final_answer_pattern___2__9__1__11([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, K, L),
        K==D,
        M=t(326, F, A, J),
        '$novel_production'(F, M),
        '$novel_production'(A, M),
        '$novel_production'(J, M)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                      O),
            insert_ht(O, H, J)
        ;   true
        ),
        C=odom(occ(P, Q), _),
        (   ai_observation_is_observed(L, P)
        ->  ai_observed_internal(P, Q)
        ;   ai_not_observed_internal(P, Q)
        ),
        (   E==yes
        ->  L=odom([], R),
            ord_del_element(R, P, S),
            T=odom([], S)
        ;   T=L
        ),
        ai_observation_lub(T, I, U),
        final_answer_pattern(C, U),
        (   J=suspension(_, active, _, _, _)
        ->  setarg(2, J, inactive),
            final_answer_pattern___2__9__1__11(B,
                                               C,
                                               D,
                                               E,
                                               F,
                                               G,
                                               H,
                                               I,
                                               J)
        ;   true
        )
    ;   final_answer_pattern___2__9__1__11(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I,
                                           J)
    ).
final_answer_pattern___2__9(A, B, C) :-
    final_answer_pattern___2__10(A, B, C).
final_answer_pattern___2__10(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
abstract_constraints(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              B),
    (   member(C, B),
        C=suspension(_, active, _, _),
        !
    ;   !,
        abstract_constraints___1__0__0__2(B, A)
    ).
abstract_constraints___1__0__0__2([], A) :-
    abstract_constraints___1__1(A).
abstract_constraints___1__0__0__2([A|B], C) :-
    (   A=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        arg(3, A, D),
        (   var(D)
        ->  nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
                      E),
            E=[_|F],
            b_setval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
                     F),
            (   F=[G|_]
            ->  setarg(3, G, _)
            ;   true
            )
        ;   D=[_, _|F],
            setarg(2, D, F),
            (   F=[G|_]
            ->  setarg(3, G, D)
            ;   true
            )
        ),
        abstract_constraints___1__0__0__2(B, C)
    ;   abstract_constraints___1__0__0__2(B, C)
    ).
abstract_constraints(A) :-
    abstract_constraints___1__1(A).
abstract_constraints___1__1(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
depends_on(A, B) :-
    depends_on___2__0(A, B, _).
depends_on___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              D),
    lookup_ht(D, B, E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    depends_on___2__0__0__1(E, A, B, C).
depends_on___2__0__0__1([], A, B, C) :-
    depends_on___2__1(A, B, C).
depends_on___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==D,
        H=t(305, E, A),
        '$novel_production'(E, H),
        '$novel_production'(A, H)
    ->  '$extend_history'(E, H),
        arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
                      J),
            insert_ht(J, C, E),
            nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2',
                      K),
            insert_ht(K, D, E)
        ;   true
        ),
        final_answer_pattern(C, G),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            depends_on___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   depends_on___2__0__0__1(B, C, D, E)
    ).
depends_on___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    depends_on___2__1(A, B, C).
depends_on___2__1(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
                  E),
        insert_ht(E, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2',
                  F),
        insert_ht(F, B, C)
    ;   true
    ).
depends_on_ap(A, B, C, D) :-
    depends_on_ap___4__0(A, B, C, D, _).
depends_on_ap___4__0(A, B, C, D, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              F),
    lookup_ht(F, C, G),
    !,
    E=suspension(H, not_stored_yet, t, A, B, C, D),
    'chr gen_id'(H),
    depends_on_ap___4__0__0__1(G,
                               A,
                               B,
                               C,
                               D,
                               E).
depends_on_ap___4__0__0__1([], A, B, C, D, E) :-
    depends_on_ap___4__1(A, B, C, D, E).
depends_on_ap___4__0__0__1([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H, I),
        H==E,
        J=t(325, G, A),
        '$novel_production'(G, J),
        '$novel_production'(A, J)
    ->  '$extend_history'(G, J),
        arg(2, G, K),
        setarg(2, G, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
                      L),
            insert_ht(L, D, G),
            nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
                      M),
            insert_ht(M, E, G)
        ;   true
        ),
        final_answer_pattern(C, I),
        (   G=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, G, inactive),
            depends_on_ap___4__0__0__1(B,
                                       C,
                                       D,
                                       E,
                                       F,
                                       G)
        ;   true
        )
    ;   depends_on_ap___4__0__0__1(B,
                                   C,
                                   D,
                                   E,
                                   F,
                                   G)
    ).
depends_on_ap___4__0(A, B, C, D, E) :-
    E=suspension(F, not_stored_yet, t, A, B, C, D),
    'chr gen_id'(F),
    depends_on_ap___4__1(A, B, C, D, E).
depends_on_ap___4__1(A, B, C, D, E) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              F),
    lookup_ht(F, B, G),
    !,
    depends_on_ap___4__1__0__2(G,
                               A,
                               B,
                               C,
                               D,
                               E).
depends_on_ap___4__1__0__2([], A, B, C, D, E) :-
    depends_on_ap___4__2(A, B, C, D, E).
depends_on_ap___4__1__0__2([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H, I),
        H==D,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  J),
        lookup_ht(J, E, K)
    ->  depends_on_ap___4__1__1__2(K,
                                   I,
                                   A,
                                   B,
                                   C,
                                   D,
                                   E,
                                   F,
                                   G)
    ;   depends_on_ap___4__1__0__2(B,
                                   C,
                                   D,
                                   E,
                                   F,
                                   G)
    ).
depends_on_ap___4__1__1__2([], _, _, A, B, C, D, E, F) :-
    depends_on_ap___4__1__0__2(A,
                               B,
                               C,
                               D,
                               E,
                               F).
depends_on_ap___4__1__1__2([A|B], C, D, E, F, G, H, I, J) :-
    (   A=suspension(_, active, _, K, L),
        A\==D,
        K==H,
        M=t(326, J, D, A),
        '$novel_production'(J, M),
        '$novel_production'(D, M),
        '$novel_production'(A, M)
    ->  '$extend_history'(J, M),
        arg(2, J, N),
        setarg(2, J, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
                      O),
            insert_ht(O, G, J),
            nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
                      P),
            insert_ht(P, H, J)
        ;   true
        ),
        F=odom(occ(Q, R), _),
        (   ai_observation_is_observed(C, Q)
        ->  ai_observed_internal(Q, R)
        ;   ai_not_observed_internal(Q, R)
        ),
        (   I==yes
        ->  C=odom([], S),
            ord_del_element(S, Q, T),
            U=odom([], T)
        ;   U=C
        ),
        ai_observation_lub(U, L, V),
        final_answer_pattern(F, V),
        (   J=suspension(_, active, _, _, _, _, _)
        ->  setarg(2, J, inactive),
            depends_on_ap___4__1__1__2(B,
                                       C,
                                       D,
                                       E,
                                       F,
                                       G,
                                       H,
                                       I,
                                       J)
        ;   true
        )
    ;   depends_on_ap___4__1__1__2(B,
                                   C,
                                   D,
                                   E,
                                   F,
                                   G,
                                   H,
                                   I,
                                   J)
    ).
depends_on_ap___4__1(A, B, C, D, E) :-
    depends_on_ap___4__2(A, B, C, D, E).
depends_on_ap___4__2(_, A, B, _, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-2',
                  E),
        insert_ht(E, A, C),
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on_ap___4-3',
                  F),
        insert_ht(F, B, C)
    ;   true
    ).
depends_on_goal(A, B) :-
    depends_on_goal___2__0(A, B, _).
depends_on_goal___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1', D),
    lookup_ht(D, A, E),
    !,
    depends_on_goal___2__0__0__1(E, A, B, C).
depends_on_goal___2__0__0__1([], A, B, C) :-
    depends_on_goal___2__1(A, B, C).
depends_on_goal___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==C,
        ground(D),
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  H),
        lookup_ht(H, D, I),
        member(J, I),
        J=suspension(_, active, _, _, _)
    ->  setarg(2, A, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-1',
                    K),
          delete_ht(K, C, A)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on___2-2',
                  L),
        delete_ht(L, G, A),
        depends_on_goal___2__0__0__1(B, C, D, E)
    ;   depends_on_goal___2__0__0__1(B, C, D, E)
    ).
depends_on_goal___2__0(A, B, C) :-
    depends_on_goal___2__1(A, B, C).
depends_on_goal___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              D),
    lookup_ht(D, B, E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    depends_on_goal___2__1__0__2(E, A, B, C).
depends_on_goal___2__1__0__2([], A, B, C) :-
    depends_on_goal___2__2(A, B, C).
depends_on_goal___2__1__0__2([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F==D,
        H=t(310, E, A),
        '$novel_production'(E, H),
        '$novel_production'(A, H)
    ->  '$extend_history'(E, H),
        arg(2, E, I),
        setarg(2, E, active),
        (   I==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
                      J),
            insert_ht(J, D, E)
        ;   true
        ),
        C=odom([_|K], _),
        G=odom([], L),
        M=odom(K, L),
        call_pattern(M),
        depends_on(C, M),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            depends_on_goal___2__1__0__2(B, C, D, E)
        ;   true
        )
    ;   depends_on_goal___2__1__0__2(B, C, D, E)
    ).
depends_on_goal___2__1(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    depends_on_goal___2__2(A, B, C).
depends_on_goal___2__2(_, A, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_goal___2-2',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
ai_observed_internal(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    !,
    ai_observed_internal___2__0__0__1(D, A, B).
ai_observed_internal___2__0__0__1([], A, B) :-
    ai_observed_internal___2__1(A, B).
ai_observed_internal___2__0__0__1([A|B], C, D) :-
    (   A=suspension(_, active, _, E, F),
        E==C,
        F==D
    ->  setarg(2, A, removed),
        ( arg(3, A, G),
          (   var(G)
          ->  nb_getval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
                        H),
              H=[_|I],
              b_setval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
                       I),
              (   I=[J|_]
              ->  setarg(3, J, _)
              ;   true
              )
          ;   G=[_, _|I],
              setarg(2, G, I),
              (   I=[J|_]
              ->  setarg(3, J, G)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
                  K),
        delete_ht(K, k(C, D), A),
        ai_observed_internal___2__0__0__1(B, C, D)
    ;   ai_observed_internal___2__0__0__1(B, C, D)
    ).
ai_observed_internal(A, B) :-
    ai_observed_internal___2__1(A, B).
ai_observed_internal___2__1(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
ai_observed_internal___2__1(_, _) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
              A),
    member(B, A),
    B=suspension(_, active, _),
    !.
ai_observed_internal___2__1(A, B) :-
    C=suspension(D, active, _, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
              E),
    F=[C|E],
    b_setval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
             F),
    (   E=[G|_]
    ->  setarg(3, G, F)
    ;   true
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
              H),
    insert_ht(H, k(A, B), C).
ai_not_observed_internal(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
ai_not_observed_internal(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
ai_not_observed_internal(A, B) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
              C),
    member(D, C),
    D=suspension(_, active, _),
    !,
    ai_not_observed(A, B).
ai_not_observed_internal(A, B) :-
    C=suspension(D, active, _, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
              E),
    F=[C|E],
    b_setval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
             F),
    (   E=[G|_]
    ->  setarg(3, G, F)
    ;   true
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
              H),
    insert_ht(H, k(A, B), C).
ai_not_observed(A, B) :-
    ai_not_observed___2__0(A, B, _).
ai_not_observed___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123', D),
    lookup_ht(D, k(A, B, yes), E),
    !,
    C=suspension(F, not_stored_yet, A, B),
    'chr gen_id'(F),
    ai_not_observed___2__0__0__1(E, A, B, C).
ai_not_observed___2__0__0__1([], A, B, C) :-
    ai_not_observed___2__1(A, B, C).
ai_not_observed___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G, H),
        F==C,
        G==D,
        H=yes,
        ( ground(C),
          nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                    I),
          lookup_ht(I, C, J)
        ),
        member(K, J),
        K=suspension(_, active, _, _, L, M),
        D<L
    ->  ( setarg(2, A, removed),
          nb_getval('$chr_store_multi_hash_chr_translate____stored___3-13',
                    N),
          delete_ht(N, k(C, yes), A),
          nb_getval('$chr_store_multi_hash_chr_translate____stored___3-123',
                    O),
          delete_ht(O, k(C, D, yes), A)
        ),
        setarg(2, K, removed),
        ( nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-13',
                    P),
          delete_ht(P, k(C, M), K)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____stored_complete___3-1',
                  Q),
        delete_ht(Q, C, K),
        arg(2, E, R),
        setarg(2, E, active),
        (   R==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
                      S),
            insert_ht(S, k(C, D), E)
        ;   true
        ),
        T is M-1,
        stored(C, D, maybe),
        stored_complete(C, L, T),
        (   E=suspension(_, active, _, _)
        ->  setarg(2, E, inactive),
            ai_not_observed___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   ai_not_observed___2__0__0__1(B, C, D, E)
    ).
ai_not_observed___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, A, B),
    'chr gen_id'(D),
    ai_not_observed___2__1(A, B, C).
ai_not_observed___2__1(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
                  E),
        insert_ht(E, k(A, B), C)
    ;   true
    ).
ai_is_observed(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !,
    fail.
ai_is_observed(_, _).
depends_on_as(A, B, C) :-
    depends_on_as___3__0(A, B, C, _).
depends_on_as___3__0(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
              E),
    lookup_ht(E, B, F),
    !,
    D=suspension(G, not_stored_yet, t, A, B, C),
    'chr gen_id'(G),
    depends_on_as___3__0__0__1(F, A, B, C, D).
depends_on_as___3__0__0__1([], A, B, C, D) :-
    depends_on_as___3__1(A, B, C, D).
depends_on_as___3__0__0__1([A|B], C, D, E, F) :-
    (   A=suspension(_, active, _, G, H),
        G==D,
        nb_getval('$chr_store_multi_hash_chr_translate____final_answer_pattern___2-1',
                  I),
        lookup_ht(I, E, J)
    ->  depends_on_as___3__0__1__1(J,
                                   H,
                                   A,
                                   B,
                                   C,
                                   D,
                                   E,
                                   F)
    ;   depends_on_as___3__0__0__1(B, C, D, E, F)
    ).
depends_on_as___3__0__1__1([], _, _, A, B, C, D, E) :-
    depends_on_as___3__0__0__1(A, B, C, D, E).
depends_on_as___3__0__1__1([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K),
        A\==D,
        J==H,
        L=t(317, I, D, A),
        '$novel_production'(I, L),
        '$novel_production'(D, L),
        '$novel_production'(A, L)
    ->  '$extend_history'(I, L),
        arg(2, I, M),
        setarg(2, I, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
                      N),
            insert_ht(N, H, I),
            nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-2',
                      O),
            insert_ht(O, G, I)
        ;   true
        ),
        ai_observation_lub(C, K, P),
        final_answer_pattern(F, P),
        (   I=suspension(_, active, _, _, _, _)
        ->  setarg(2, I, inactive),
            depends_on_as___3__0__1__1(B,
                                       C,
                                       D,
                                       E,
                                       F,
                                       G,
                                       H,
                                       I)
        ;   true
        )
    ;   depends_on_as___3__0__1__1(B,
                                   C,
                                   D,
                                   E,
                                   F,
                                   G,
                                   H,
                                   I)
    ).
depends_on_as___3__0(A, B, C, D) :-
    D=suspension(E, not_stored_yet, t, A, B, C),
    'chr gen_id'(E),
    depends_on_as___3__1(A, B, C, D).
depends_on_as___3__1(_, A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-3',
                  E),
        insert_ht(E, B, C),
        nb_getval('$chr_store_multi_hash_chr_translate____depends_on_as___3-2',
                  F),
        insert_ht(F, A, C)
    ;   true
    ).
ai_observation_gather_results :-
    ai_observation_gather_results___0__0(_).
ai_observation_gather_results___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
              B),
    !,
    ai_observation_gather_results___0__0__0__1(B, A).
ai_observation_gather_results___0__0__0__1([], A) :-
    ai_observation_gather_results___0__1(A).
ai_observation_gather_results___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, D, E)
    ->  setarg(2, A, removed),
        ( arg(3, A, F),
          (   var(F)
          ->  nb_getval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
                        G),
              G=[_|H],
              b_setval('$chr_store_global_ground_chr_translate____ai_observed_internal___2',
                       H),
              (   H=[I|_]
              ->  setarg(3, I, _)
              ;   true
              )
          ;   F=[_, _|H],
              setarg(2, F, H),
              (   H=[I|_]
              ->  setarg(3, I, F)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ai_observed_internal___2-12',
                  J),
        delete_ht(J, k(D, E), A),
        ai_observation_gather_results___0__0__0__1(B, C)
    ;   ai_observation_gather_results___0__0__0__1(B, C)
    ).
ai_observation_gather_results___0__0(A) :-
    ai_observation_gather_results___0__1(A).
ai_observation_gather_results___0__1(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
              B),
    !,
    A=suspension(C, not_stored_yet, _),
    'chr gen_id'(C),
    ai_observation_gather_results___0__1__0__2(B, A).
ai_observation_gather_results___0__1__0__2([], A) :-
    ai_observation_gather_results___0__2(A).
ai_observation_gather_results___0__1__0__2([A|B], C) :-
    (   A=suspension(_, active, _, D, E)
    ->  setarg(2, A, removed),
        ( arg(3, A, F),
          (   var(F)
          ->  nb_getval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
                        G),
              G=[_|H],
              b_setval('$chr_store_global_ground_chr_translate____ai_not_observed_internal___2',
                       H),
              (   H=[I|_]
              ->  setarg(3, I, _)
              ;   true
              )
          ;   F=[_, _|H],
              setarg(2, F, H),
              (   H=[I|_]
              ->  setarg(3, I, F)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____ai_not_observed_internal___2-12',
                  J),
        delete_ht(J, k(D, E), A),
        arg(2, C, K),
        setarg(2, C, active),
        (   K==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
                      L),
            M=[C|L],
            b_setval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
                     M),
            (   L=[N|_]
            ->  setarg(3, N, M)
            ;   true
            )
        ;   true
        ),
        ai_not_observed(D, E),
        (   C=suspension(_, active, _)
        ->  setarg(2, C, inactive),
            ai_observation_gather_results___0__1__0__2(B, C)
        ;   true
        )
    ;   ai_observation_gather_results___0__1__0__2(B, C)
    ).
ai_observation_gather_results___0__1(A) :-
    A=suspension(B, not_stored_yet, _),
    'chr gen_id'(B),
    ai_observation_gather_results___0__2(A).
ai_observation_gather_results___0__2(A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(3, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____ai_observation_gather_results___0',
                         E),
                (   E=[F|_]
                ->  setarg(3, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(3, F, C)
                ;   true
                )
            )
        )
    ).
ai_observation_memo_simplification_rest_heads(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    C=G.
ai_observation_memo_simplification_rest_heads(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, H, _),
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', I),
    lookup_ht(I, G, J),
    member(K, J),
    K=suspension(_, active, _, _, _, L),
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              M),
    member(N, M),
    N=suspension(_, active, _, O),
    !,
    L=pragma(rule(P, Q, _, _), ids(R, _), _, _, _),
    once(select2(H, _, R, P, _, S)),
    ai_observation_abstract_constraints(S, O, T),
    ai_observation_abstract_constraints(Q, O, U),
    append(T, U, V),
    sort(V, C),
    ai_observation_memoed_simplification_rest_heads(A, B, C).
ai_observation_memo_simplification_rest_heads(_, _, _) :-
    fail.
ai_observation_memoed_simplification_rest_heads(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_simplification_rest_heads___3-12',
              F),
    insert_ht(F, k(A, B), D).
ai_observation_memo_propagation_rest_heads(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    C=G.
ai_observation_memo_propagation_rest_heads(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, H, _),
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', I),
    lookup_ht(I, G, J),
    member(K, J),
    K=suspension(_, active, _, _, _, L),
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              M),
    member(N, M),
    N=suspension(_, active, _, O),
    !,
    L=pragma(rule(P, Q, _, _), ids(_, R), _, _, _),
    once(select2(H, _, R, Q, _, S)),
    ai_observation_abstract_constraints(S, O, T),
    ai_observation_abstract_constraints(P, O, U),
    append(T, U, V),
    sort(V, C),
    ai_observation_memoed_propagation_rest_heads(A, B, C).
ai_observation_memo_propagation_rest_heads(_, _, _) :-
    fail.
ai_observation_memoed_propagation_rest_heads(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_propagation_rest_heads___3-12',
              F),
    insert_ht(F, k(A, B), D).
ai_observation_memoed_abstract_goal(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1',
              E),
    insert_ht(E, A, C).
ai_observation_memo_abstract_goal(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____ai_observation_memoed_abstract_goal___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, F),
    !,
    B=F.
ai_observation_memo_abstract_goal(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, _, F),
    nb_getval('$chr_store_global_ground_chr_translate____abstract_constraints___1',
              G),
    member(H, G),
    H=suspension(_, active, _, I),
    !,
    F=pragma(rule(J, K, L, M), _, _, _, _),
    ai_observation_abstract_goal_(J,
                                  K,
                                  L,
                                  M,
                                  I,
                                  N),
    B=N,
    ai_observation_memoed_abstract_goal(A, N).
ai_observation_memo_abstract_goal(A, B) :-
    C=suspension(D, active, _, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_global_ground_chr_translate____ai_observation_memo_abstract_goal___2',
              E),
    F=[C|E],
    b_setval('$chr_store_global_ground_chr_translate____ai_observation_memo_abstract_goal___2',
             F),
    (   E=[G|_]
    ->  setarg(3, G, F)
    ;   true
    ).
partial_wake_analysis :-
    partial_wake_analysis___0__0(_).
partial_wake_analysis___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____occurrence___5',
              B),
    !,
    A=suspension(C, not_stored_yet, t, _),
    'chr gen_id'(C),
    partial_wake_analysis___0__0__0__1(B, A).
partial_wake_analysis___0__0__0__1([], A) :-
    partial_wake_analysis___0__1(A).
partial_wake_analysis___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, _, D, _, E, F, G),
        nb_getval('$chr_store_multi_hash_chr_translate____constraint_mode___2-1',
                  H),
        lookup_ht(H, D, I)
    ->  partial_wake_analysis___0__0__1__1(I,
                                           D,
                                           E,
                                           F,
                                           G,
                                           A,
                                           B,
                                           C)
    ;   partial_wake_analysis___0__0__0__1(B, C)
    ).
partial_wake_analysis___0__0__1__1([], _, _, _, _, _, A, B) :-
    partial_wake_analysis___0__0__0__1(A, B).
partial_wake_analysis___0__0__1__1([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, _, J, K),
        J==C,
        nb_getval('$chr_store_multi_hash_chr_translate____rule___2-1', L),
        lookup_ht(L, D, M)
    ->  partial_wake_analysis___0__0__2__1(M,
                                           K,
                                           A,
                                           B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I)
    ;   partial_wake_analysis___0__0__1__1(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I)
    ).
partial_wake_analysis___0__0__2__1([], _, _, A, B, C, D, E, F, G, H) :-
    partial_wake_analysis___0__0__1__1(A,
                                       B,
                                       C,
                                       D,
                                       E,
                                       F,
                                       G,
                                       H).
partial_wake_analysis___0__0__2__1([A|B], C, D, E, F, G, H, I, J, K, L) :-
    (   A=suspension(_, active, _, _, M, N),
        M==G,
        O=t(330, L, J, A, D),
        '$novel_production'(L, O),
        '$novel_production'(J, O),
        '$novel_production'(A, O),
        '$novel_production'(D, O)
    ->  '$extend_history'(L, O),
        arg(2, L, P),
        setarg(2, L, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                      Q),
            R=[L|Q],
            b_setval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                     R),
            (   Q=[S|_]
            ->  setarg(4, S, R)
            ;   true
            )
        ;   true
        ),
        N=pragma(rule(T, U, V, _), _, _, _, _),
        (   is_passive(G, H)
        ->  true
        ;   I==simplification
        ->  select(W, T, _),
            W=..[_|X],
            term_variables(V, Y),
            partial_wake_args(X, C, Y, F)
        ;   select(W, U, _),
            W=..[_|X],
            term_variables(V, Y),
            partial_wake_args(X, C, Y, F)
        ),
        (   L=suspension(_, active, _, _)
        ->  setarg(2, L, inactive),
            partial_wake_analysis___0__0__2__1(B,
                                               C,
                                               D,
                                               E,
                                               F,
                                               G,
                                               H,
                                               I,
                                               J,
                                               K,
                                               L)
        ;   true
        )
    ;   partial_wake_analysis___0__0__2__1(B,
                                           C,
                                           D,
                                           E,
                                           F,
                                           G,
                                           H,
                                           I,
                                           J,
                                           K,
                                           L)
    ).
partial_wake_analysis___0__0(A) :-
    A=suspension(B, not_stored_yet, t, _),
    'chr gen_id'(B),
    partial_wake_analysis___0__1(A).
partial_wake_analysis___0__1(A) :-
    arg(2, A, B),
    setarg(2, A, active),
    (   B==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                  C),
        D=[A|C],
        b_setval('$chr_store_global_ground_chr_translate____partial_wake_analysis___0',
                 D),
        (   C=[E|_]
        ->  setarg(4, E, D)
        ;   true
        )
    ;   true
    ).
no_partial_wake(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !.
no_partial_wake(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              D),
    insert_ht(D, A, B).
wakes_partially(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____no_partial_wake___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !,
    fail.
wakes_partially(_).
phase_end(A) :-
    phase_end___1__0(A, _).
phase_end___1__0(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
              C),
    lookup_ht(C, A, D),
    !,
    B=suspension(E, not_stored_yet, A),
    'chr gen_id'(E),
    phase_end___1__0__0__1(D, A, B).
phase_end___1__0__0__1([], A, B) :-
    phase_end___1__1(A, B).
phase_end___1__0__0__1([A|B], C, D) :-
    (   A=suspension(_, active, E, F),
        E==C
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
                  G),
        delete_ht(G, C, A),
        arg(2, D, H),
        setarg(2, D, active),
        (   H==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____phase_end___1-1',
                      I),
            insert_ht(I, C, D)
        ;   true
        ),
        call(F),
        (   D=suspension(_, active, _)
        ->  setarg(2, D, inactive),
            phase_end___1__0__0__1(B, C, D)
        ;   true
        )
    ;   phase_end___1__0__0__1(B, C, D)
    ).
phase_end___1__0(A, B) :-
    B=suspension(C, not_stored_yet, A),
    'chr gen_id'(C),
    phase_end___1__1(A, B).
phase_end___1__1(A, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____phase_end___1-1',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
delay_phase_end(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____phase_end___1-1', C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _),
    !,
    call(B).
delay_phase_end(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____delay_phase_end___2-1',
              E),
    insert_ht(E, A, C).
does_use_history(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
does_use_history(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-1',
              E),
    insert_ht(E, A, C),
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-12',
              F),
    insert_ht(F, k(A, B), C).
uses_history(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _),
    !.
uses_history(_) :-
    fail.
novel_production_call(A, B, C, D) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_history___2-12',
              E),
    lookup_ht(E, k(A, B), F),
    member(G, F),
    G=suspension(_, active, _, _),
    !,
    D=C.
novel_production_call(_, _, _, true).
does_use_field(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
does_use_field(A, B) :-
    C=suspension(D, active, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              E),
    insert_ht(E, k(A, B), C).
uses_field(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____does_use_field___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
uses_field(_, _) :-
    fail.
uses_state(A, B) :-
    uses_state___2__0(A, B, _).
uses_state___2__0(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _),
    !.
uses_state___2__0(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
              D),
    !,
    C=suspension(E, not_stored_yet, A, B),
    'chr gen_id'(E),
    uses_state___2__0__0__3(D, A, B, C).
uses_state___2__0__0__3([], A, B, C) :-
    uses_state___2__1(A, B, C).
uses_state___2__0__0__3([A|B], C, D, E) :-
    (   A=suspension(_, active, _),
        nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
                  F),
        lookup_ht(F, k(C, D), G)
    ->  uses_state___2__0__1__3(G,
                                A,
                                B,
                                C,
                                D,
                                E)
    ;   uses_state___2__0__0__3(B, C, D, E)
    ).
uses_state___2__0__1__3([], _, A, B, C, D) :-
    uses_state___2__0__0__3(A, B, C, D).
uses_state___2__0__1__3([A|B], C, D, E, F, G) :-
    (   A=suspension(_, active, _, H, I, J, _, K),
        H==E,
        I==F
    ->  setarg(2, A, removed),
        ( arg(3, A, L),
          (   var(L)
          ->  nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
                        M),
              M=[_|N],
              b_setval('$chr_store_global_ground_chr_translate____if_used_state___5',
                       N),
              (   N=[O|_]
              ->  setarg(3, O, _)
              ;   true
              )
          ;   L=[_, _|N],
              setarg(2, L, N),
              (   N=[O|_]
              ->  setarg(3, O, L)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
                  P),
        delete_ht(P, k(E, F), A),
        arg(2, G, Q),
        setarg(2, G, active),
        (   Q==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
                      R),
            insert_ht(R, k(E, F), G)
        ;   true
        ),
        K=J,
        (   G=suspension(_, active, _, _)
        ->  setarg(2, G, inactive),
            uses_state___2__0__1__3(B,
                                    C,
                                    D,
                                    E,
                                    F,
                                    G)
        ;   true
        )
    ;   uses_state___2__0__1__3(B,
                                C,
                                D,
                                E,
                                F,
                                G)
    ).
uses_state___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, A, B),
    'chr gen_id'(D),
    uses_state___2__1(A, B, C).
uses_state___2__1(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
                  E),
        insert_ht(E, k(A, B), C)
    ;   true
    ).
if_used_state(A, B, C, D, E) :-
    nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
              F),
    member(G, F),
    G=suspension(_, active, _),
    (   nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
                  H),
        lookup_ht(H, k(A, B), I),
        member(J, I),
        J=suspension(_, active, _, _),
        !,
        E=C
    ;   !,
        E=D
    ).
if_used_state(A, B, C, D, E) :-
    F=suspension(G, active, _, A, B, C, D, E),
    'chr gen_id'(G),
    nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
              H),
    I=[F|H],
    b_setval('$chr_store_global_ground_chr_translate____if_used_state___5',
             I),
    (   H=[J|_]
    ->  setarg(3, J, I)
    ;   true
    ),
    nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
              K),
    insert_ht(K, k(A, B), F).
used_states_known :-
    used_states_known___0__0(_).
used_states_known___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
              B),
    !,
    A=suspension(C, not_stored_yet, _),
    'chr gen_id'(C),
    used_states_known___0__0__0__1(B, A).
used_states_known___0__0__0__1([], A) :-
    used_states_known___0__1(A).
used_states_known___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, D, E, F, _, G),
        ( ground(D),
          ground(E)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____uses_state___2-12',
                  H),
        lookup_ht(H, k(D, E), I),
        member(J, I),
        J=suspension(_, active, _, _)
    ->  setarg(2, A, removed),
        ( arg(3, A, K),
          (   var(K)
          ->  nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
                        L),
              L=[_|M],
              b_setval('$chr_store_global_ground_chr_translate____if_used_state___5',
                       M),
              (   M=[N|_]
              ->  setarg(3, N, _)
              ;   true
              )
          ;   K=[_, _|M],
              setarg(2, K, M),
              (   M=[N|_]
              ->  setarg(3, N, K)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
                  O),
        delete_ht(O, k(D, E), A),
        arg(2, C, P),
        setarg(2, C, active),
        (   P==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
                      Q),
            R=[C|Q],
            b_setval('$chr_store_global_ground_chr_translate____used_states_known___0',
                     R),
            (   Q=[S|_]
            ->  setarg(3, S, R)
            ;   true
            )
        ;   true
        ),
        G=F,
        (   C=suspension(_, active, _)
        ->  setarg(2, C, inactive),
            used_states_known___0__0__0__1(B, C)
        ;   true
        )
    ;   used_states_known___0__0__0__1(B, C)
    ).
used_states_known___0__0(A) :-
    A=suspension(B, not_stored_yet, _),
    'chr gen_id'(B),
    used_states_known___0__1(A).
used_states_known___0__1(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
              B),
    !,
    used_states_known___0__1__0__2(B, A).
used_states_known___0__1__0__2([], A) :-
    used_states_known___0__2(A).
used_states_known___0__1__0__2([A|B], C) :-
    (   A=suspension(_, active, _, D, E, _, F, G)
    ->  setarg(2, A, removed),
        ( arg(3, A, H),
          (   var(H)
          ->  nb_getval('$chr_store_global_ground_chr_translate____if_used_state___5',
                        I),
              I=[_|J],
              b_setval('$chr_store_global_ground_chr_translate____if_used_state___5',
                       J),
              (   J=[K|_]
              ->  setarg(3, K, _)
              ;   true
              )
          ;   H=[_, _|J],
              setarg(2, H, J),
              (   J=[K|_]
              ->  setarg(3, K, H)
              ;   true
              )
          )
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____if_used_state___5-12',
                  L),
        delete_ht(L, k(D, E), A),
        arg(2, C, M),
        setarg(2, C, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
                      N),
            O=[C|N],
            b_setval('$chr_store_global_ground_chr_translate____used_states_known___0',
                     O),
            (   N=[P|_]
            ->  setarg(3, P, O)
            ;   true
            )
        ;   true
        ),
        G=F,
        (   C=suspension(_, active, _)
        ->  setarg(2, C, inactive),
            used_states_known___0__1__0__2(B, C)
        ;   true
        )
    ;   used_states_known___0__1__0__2(B, C)
    ).
used_states_known___0__1(A) :-
    used_states_known___0__2(A).
used_states_known___0__2(A) :-
    arg(2, A, B),
    setarg(2, A, active),
    (   B==not_stored_yet
    ->  nb_getval('$chr_store_global_ground_chr_translate____used_states_known___0',
                  C),
        D=[A|C],
        b_setval('$chr_store_global_ground_chr_translate____used_states_known___0',
                 D),
        (   C=[E|_]
        ->  setarg(3, E, D)
        ;   true
        )
    ;   true
    ).
stored_assertion(A) :-
    B=suspension(C, active, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              D),
    insert_ht(D, A, B).
never_stored_default(A, B) :-
    never_stored_default___2__0(A, B, _).
never_stored_default___2__0(A, B, C) :-
    nb_getval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
              D),
    !,
    C=suspension(E, not_stored_yet, A, B),
    'chr gen_id'(E),
    never_stored_default___2__0__0__1(D, A, B, C).
never_stored_default___2__0__0__1([], A, B, C) :-
    never_stored_default___2__1(A, B, C).
never_stored_default___2__0__0__1([A|B], C, D, E) :-
    (   A=suspension(_, active, _, F, G),
        F=[H|I],
        H==C
    ->  setarg(2, A, removed),
        arg(3, A, J),
        (   var(J)
        ->  nb_getval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
                      K),
            K=[_|L],
            b_setval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
                     L),
            (   L=[M|_]
            ->  setarg(3, M, _)
            ;   true
            )
        ;   J=[_, _|L],
            setarg(2, J, L),
            (   L=[M|_]
            ->  setarg(3, M, J)
            ;   true
            )
        ),
        arg(2, E, N),
        setarg(2, E, active),
        (   N==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
                      O),
            insert_ht(O, C, E)
        ;   true
        ),
        C=P/Q,
        functor(R, P, Q),
        inc_rule_count(S),
        T=pragma(rule([R], [], true, D), ids([0], []), [], no, S),
        G=[T|U],
        never_stored_rules(I, U),
        (   E=suspension(_, active, _, _)
        ->  setarg(2, E, inactive),
            never_stored_default___2__0__0__1(B, C, D, E)
        ;   true
        )
    ;   never_stored_default___2__0__0__1(B, C, D, E)
    ).
never_stored_default___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, A, B),
    'chr gen_id'(D),
    never_stored_default___2__1(A, B, C).
never_stored_default___2__1(A, _, B) :-
    arg(2, B, C),
    setarg(2, B, active),
    (   C==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
                  D),
        insert_ht(D, A, B)
    ;   true
    ).
never_stored_rules([], A) :-
    !,
    A=[].
never_stored_rules([A|B], C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
              D),
    lookup_ht(D, A, E),
    member(F, E),
    F=suspension(_, active, _, G),
    !,
    A=H/I,
    functor(J, H, I),
    inc_rule_count(K),
    L=pragma(rule([J], [], true, G), ids([0], []), [], no, K),
    C=[L|M],
    never_stored_rules(B, M).
never_stored_rules([_|A], B) :-
    !,
    never_stored_rules(A, B).
never_stored_rules(A, B) :-
    C=suspension(D, active, _, A, B),
    'chr gen_id'(D),
    nb_getval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
              E),
    F=[C|E],
    b_setval('$chr_store_global_ground_chr_translate____never_stored_rules___2',
             F),
    (   E=[G|_]
    ->  setarg(3, G, F)
    ;   true
    ).
check_storedness_assertion(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _),
    !,
    setarg(2, D, removed),
    nb_getval('$chr_store_multi_hash_chr_translate____stored_assertion___1-1',
              E),
    delete_ht(E, A, D),
    (   is_stored(A)
    ->  true
    ;   chr_warning(assertion_failed,
                    'Constraint ~w is not stored. However, it was asserted to be stored.\n',
                    [A])
    ).
check_storedness_assertion(A) :-
    nb_getval('$chr_store_multi_hash_chr_translate____never_stored_default___2-1',
              B),
    lookup_ht(B, A, C),
    member(D, C),
    D=suspension(_, active, _, _),
    !,
    (   is_finally_stored(A)
    ->  chr_warning(assertion_failed,
                    'Constraint ~w is stored. However, it was asserted not to be stored.\n',
                    [A])
    ;   is_stored(A)
    ->  chr_warning(assertion_failed,
                    'Constraint ~w is temporarily stored. However, it was asserted not to be stored.\n',
                    [A])
    ;   true
    ).
check_storedness_assertion(A) :-
    (   is_finally_stored(A)
    ->  chr_warning(assertion_failed,
                    'Constraint ~w is stored. However, it was asserted not to be stored.\n',
                    [A])
    ;   is_stored(A)
    ->  chr_warning(assertion_failed,
                    'Constraint ~w is temporarily stored. However, it was asserted not to be stored.\n',
                    [A])
    ;   true
    ).
continuation_occurrence(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              F),
    insert_ht(F, k(A, B), D).
get_success_continuation_occurrence(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    C=G.
get_success_continuation_occurrence(A, B, _) :-
    chr_error(internal, 'Success continuation not found for ~w.\n', [A:B]).
skip_to_next_id(A, B) :-
    skip_to_next_id___2__0(A, B, _).
skip_to_next_id___2__0(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
skip_to_next_id___2__0(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    C=suspension(F, not_stored_yet, t, A, B),
    'chr gen_id'(F),
    skip_to_next_id___2__0__0__4(E, A, B, C).
skip_to_next_id___2__0__0__4([], A, B, C) :-
    skip_to_next_id___2__1(A, B, C).
skip_to_next_id___2__0__0__4([A|B], C, D, E) :-
    (   A=suspension(_, active, _, _, F, G, H, I, _),
        F==C,
        G==D,
        nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12',
                  J),
        lookup_ht(J, k(H, I), K)
    ->  skip_to_next_id___2__0__1__4(K,
                                     H,
                                     I,
                                     A,
                                     B,
                                     C,
                                     D,
                                     E)
    ;   skip_to_next_id___2__0__0__4(B, C, D, E)
    ).
skip_to_next_id___2__0__1__4([], _, _, _, A, B, C, D) :-
    skip_to_next_id___2__0__0__4(A, B, C, D).
skip_to_next_id___2__0__1__4([A|B], C, D, E, F, G, H, I) :-
    (   A=suspension(_, active, _, J, K),
        J==C,
        K==D,
        L=t(362, E, A, I),
        '$novel_production'(E, L),
        '$novel_production'(A, L),
        '$novel_production'(I, L),
        H>1
    ->  '$extend_history'(I, L),
        arg(2, I, M),
        setarg(2, I, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                      N),
            insert_ht(N, k(G, H), I)
        ;   true
        ),
        O is H-1,
        skip_to_next_id(G, O),
        (   I=suspension(_, active, _, _, _)
        ->  setarg(2, I, inactive),
            skip_to_next_id___2__0__1__4(B,
                                         C,
                                         D,
                                         E,
                                         F,
                                         G,
                                         H,
                                         I)
        ;   true
        )
    ;   skip_to_next_id___2__0__1__4(B,
                                     C,
                                     D,
                                     E,
                                     F,
                                     G,
                                     H,
                                     I)
    ).
skip_to_next_id___2__0(A, B, C) :-
    C=suspension(D, not_stored_yet, t, A, B),
    'chr gen_id'(D),
    skip_to_next_id___2__1(A, B, C).
skip_to_next_id___2__1(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    !,
    skip_to_next_id___2__1__0__5(E, A, B, C).
skip_to_next_id___2__1__0__5([], A, B, C) :-
    skip_to_next_id___2__2(A, B, C).
skip_to_next_id___2__1__0__5([A|B], C, D, E) :-
    (   A=suspension(_, active, F, G, H),
        F==C,
        G==D,
        ( ground(C),
          ground(D)
        ),
        nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
                  I),
        lookup_ht(I, k(C, D, propagation), J),
        member(K, J),
        K=suspension(_, active, _, _, _, _, _, _, _)
    ->  setarg(2, A, removed),
        nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
                  L),
        delete_ht(L, k(C, D), A),
        arg(2, E, M),
        setarg(2, E, active),
        (   M==not_stored_yet
        ->  nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                      N),
            insert_ht(N, k(C, D), E)
        ;   true
        ),
        occurrence_code_id(C, D, H),
        O is D+1,
        P is H+1,
        set_occurrence_code_id(C, O, P),
        (   E=suspension(_, active, _, _, _)
        ->  setarg(2, E, inactive),
            skip_to_next_id___2__1__0__5(B, C, D, E)
        ;   true
        )
    ;   skip_to_next_id___2__1__0__5(B, C, D, E)
    ).
skip_to_next_id___2__1(A, B, C) :-
    skip_to_next_id___2__2(A, B, C).
skip_to_next_id___2__2(A, B, C) :-
    arg(2, C, D),
    setarg(2, C, active),
    (   D==not_stored_yet
    ->  nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                  E),
        insert_ht(E, k(A, B), C)
    ;   true
    ).
should_skip_to_next_id(A, B) :-
    nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
              C),
    lookup_ht(C, k(A, B), D),
    member(E, D),
    E=suspension(_, active, _, _, _),
    !.
should_skip_to_next_id(_, _) :-
    fail.
bulk_propagation(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____max_occurrence___2-1',
              C),
    lookup_ht(C, A, D),
    member(E, D),
    E=suspension(_, active, _, _, F),
    B>=F,
    !,
    skip_to_next_id(A, B).
bulk_propagation(A, B, C) :-
    C=:=B+1,
    !,
    skip_to_next_id(A, B),
    get_max_occurrence(A, D),
    E is D+1,
    bulk_propagation(A, C, E).
bulk_propagation(A, B, _) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-15',
              C),
    lookup_ht(C, k(A, simplification), D),
    member(E, D),
    E=suspension(_, active, _, _, _, F, _, _, _),
    F=:=B+1,
    !,
    skip_to_next_id(A, B),
    get_max_occurrence(A, G),
    H is G+1,
    bulk_propagation(A, F, H).
bulk_propagation(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____continuation_occurrence___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    G>B+1,
    !,
    H is min(C, G),
    I is B+1,
    bulk_propagation(A, I, H).
bulk_propagation(A, B, _) :-
    skip_to_next_id(A, B),
    get_max_occurrence(A, C),
    D is C+1,
    E is B+1,
    bulk_propagation(A, E, D).
set_occurrence_code_id(A, B, C) :-
    get_max_occurrence(A, D),
    B>D,
    !,
    occurrence_code_id(A, B, C).
set_occurrence_code_id(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, _, _, G, H, _),
    nb_getval('$chr_store_multi_hash_chr_translate____passive___2-12', I),
    lookup_ht(I, k(G, H), J),
    member(K, J),
    K=suspension(_, active, _, _, _),
    !,
    occurrence_code_id(A, B, C),
    L is B+1,
    set_occurrence_code_id(A, L, C).
set_occurrence_code_id(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence___5-125',
              D),
    (   (   lookup_ht(D, k(A, B, simplification), E),
            member(F, E),
            F=suspension(_, active, _, _, _, _, _, _, _),
            !,
            occurrence_code_id(A, B, C),
            G is B+1,
            set_occurrence_code_id(A, G, C)
        ;   lookup_ht(D, k(A, B, propagation), H),
            member(I, H),
            I=suspension(_, active, _, _, _, _, _, _, _),
            nb_getval('$chr_store_multi_hash_chr_translate____skip_to_next_id___2-12',
                      J),
            lookup_ht(J, k(A, B), K),
            member(L, K),
            L=suspension(_, active, _, _, _),
            !,
            occurrence_code_id(A, B, C),
            M is B+1,
            N is C+1,
            set_occurrence_code_id(A, M, N)
        )
    ;   lookup_ht(D, k(A, B, propagation), O),
        member(P, O),
        P=suspension(_, active, _, _, _, _, _, _, _),
        !,
        occurrence_code_id(A, B, C),
        Q is B+1,
        set_occurrence_code_id(A, Q, C)
    ).
set_occurrence_code_id(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____set_occurrence_code_id___3-12',
              F),
    insert_ht(F, k(A, B), D).
occurrence_code_id(A, B, C) :-
    D=suspension(E, active, A, B, C),
    'chr gen_id'(E),
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12',
              F),
    insert_ht(F, k(A, B), D).
get_occurrence_code_id(A, B, C) :-
    nb_getval('$chr_store_multi_hash_chr_translate____occurrence_code_id___3-12',
              D),
    lookup_ht(D, k(A, B), E),
    member(F, E),
    F=suspension(_, active, _, _, G),
    !,
    C=G.
get_occurrence_code_id(A, B, _) :-
    (   B==0
    ->  true
    ;   format('no occurrence code for ~w!\n', [A:B])
    ).
chr_constants(A) :-
    B=suspension(C, active, _, A),
    'chr gen_id'(C),
    nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
              D),
    E=[B|D],
    b_setval('$chr_store_global_ground_chr_translate____chr_constants___1',
             E),
    (   D=[F|_]
    ->  setarg(3, F, E)
    ;   true
    ).
get_chr_constants(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    A=D.
get_chr_constants(A) :-
    chr_warning(internal, 'No constants found for key ~w.\n', [_]),
    A=[].
add_chr_constants_(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
              B),
    member(C, B),
    C=suspension(_, active, _, D),
    !,
    setarg(2, C, removed),
    arg(3, C, E),
    (   var(E)
    ->  nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
                  F),
        F=[_|G],
        b_setval('$chr_store_global_ground_chr_translate____chr_constants___1',
                 G),
        (   G=[H|_]
        ->  setarg(3, H, _)
        ;   true
        )
    ;   E=[_, _|G],
        setarg(2, E, G),
        (   G=[H|_]
        ->  setarg(3, H, E)
        ;   true
        )
    ),
    sort([A|D], I),
    chr_constants(I).
add_chr_constants_(A) :-
    chr_constants([A]).
print_chr_constants :-
    print_chr_constants___0__0(_).
print_chr_constants___0__0(A) :-
    nb_getval('$chr_store_global_ground_chr_translate____chr_constants___1',
              B),
    !,
    A=suspension(C, not_stored_yet, _),
    'chr gen_id'(C),
    print_chr_constants___0__0__0__1(B, A).
print_chr_constants___0__0__0__1([], A) :-
    print_chr_constants___0__1(A).
print_chr_constants___0__0__0__1([A|B], C) :-
    (   A=suspension(_, active, _, D)
    ->  format('\t* chr_constants : ~w.\n', [D]),
        print_chr_constants___0__0__0__1(B, C)
    ;   print_chr_constants___0__0__0__1(B, C)
    ).
print_chr_constants___0__0(A) :-
    A=suspension(B, not_stored_yet, _),
    'chr gen_id'(B),
    print_chr_constants___0__1(A).
print_chr_constants___0__1(A) :-
    (   var(A)
    ->  true
    ;   arg(2, A, B),
        setarg(2, A, removed),
        (   B==not_stored_yet
        ->  true
        ;   arg(3, A, C),
            (   var(C)
            ->  nb_getval('$chr_store_global_ground_chr_translate____print_chr_constants___0',
                          D),
                D=[_|E],
                b_setval('$chr_store_global_ground_chr_translate____print_chr_constants___0',
                         E),
                (   E=[F|_]
                ->  setarg(3, F, _)
                ;   true
                )
            ;   C=[_, _|E],
                setarg(2, C, E),
                (   E=[F|_]
                ->  setarg(3, F, C)
                ;   true
                )
            )
        )
    ).
end_of_file.
