
%%                     ********* preROTTER ***********
:- module clause_trans_r.
:- public translate/2, translate/6, clause2kl1/2, seq2list/7.
:- with_macro pimos.

translate(InL, OutL):-
        true|
                InL = "clause",
                OutL = "kl1".

translate(In, Out, VP, MesH, MesT, ST):-
        true|
                ST = normal,
                VP = [],
                MesH = MesT,
                clause2kl1(In, Out).

%%clause2kl1------------------------------------------------------------



clause2kl1(In, Out):-
        true|
                Out = [wrap#(:-module mgtp),
                       wrap#(:-public get_orgn_facts/1,get_CL/1,get_FCL/1,
                                      ch/3,  c/4,  d/4,
                                      chf1/3,cf1/4,df1/4,
                                      chf2/4,cf2/4,df2/4,ef2/4),
                       wrap#(:-with_macro pimos)
                       |CSM],
                CSM = [wrap#(get_orgn_facts(V_TBGL):-true|V_TBGL=TBGL),
                       wrap#(get_CL(V_CL):-true|V_CL=I_J),
                       wrap#(get_FCL(V_FCL):-true|V_FCL=fcl(I_J1,I_J2))
                       |CH],
                V_CL = variable("CL"),
                V_FCL = variable("FCL"),
                I_J = integer(J),
                I_J1 = integer(J1),
                I_J2 = integer(J2),
                V_TBGL = variable("TBGL"),
                TBGL = wrap#(tbgl(I_Max, Vc_TBGLV)),
                I_Max = integer(Max),
                Vc_TBGLV = vector(TBGLV),
                new_nil_vector(TBGLV0, Max),
                distribute(TBGLV0, Init, TBGLV),
                clause2kl10(In, J, Max, Init,
                             C   , D  , CHF1,
                             CH  , C  , D   ),
                clause2kl1f1(In, J1,
                             CF1 , DF1, CHF2,
                             CHF1, CF1, DF1 ),
                clause2kl1f2(In, J2,
                             CF2 , DF2, EF2, [],
                             CHF2, CF2, DF2, EF2).


%%new_nil_vector------------------------------------------------------------

new_nil_vector(TBGLV0, Max):-
        true|
                VecSize := Max + 1,
                new_vector(Vec0, VecSize),
                fill_by_nils(Max, Vec0, TBGLV0).


%%fill_by_nils------------------------------------------------------------

fill_by_nils(0, Vec0, Vec):-
        true|
                set_vector_element(Vec0, 0, Old, wrap#([]), Vec).

otherwise.

fill_by_nils(Pos, Vec0, Vec):-
        true|
                set_vector_element(Vec0, Pos, Old, wrap#([]), Vec1),
                Pos1 := Pos - 1,
                fill_by_nils(Pos1, Vec1, Vec).


%%distribute------------------------------------------------------------

distribute(TBGLV0, wrap#([]), TBGLV):-
        true|
                TBGLV = TBGLV0.

distribute(TBGLV0, wrap#([disj(I_N,RList)|InitR]), TBGLV):-
        true|
                I_N = integer(N),
                Pos = N,
                set_vector_element(TBGLV1, Pos, Old, New, TBGLV),
                New = wrap#([RList|Old]),
                distribute(TBGLV0, InitR, TBGLV1).




%%%%%%%%%%%%%%%%%%%%%%%%%%%% M A I N %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%clause2kl10------------------------------------------------------------

clause2kl10([], J, Max, Init,
            CH0, C0, D0,
            CH , C , D ):-
        true|
                J = 0,
                Max = 0,
                Init = wrap#([]),
                CH  = [wrap#(ch(0,V_bar,V_bar):-true|true),
                       wrap#(otherwise),
                       wrap#(ch(V_bar,V_bar,V_A):-true|V_A=fail)
                       |CH0],
                C   = [wrap#(c(0,V_bar,V_bar,V_bar):-true|true),
                       wrap#(otherwise),
                       wrap#(c(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                       |C0],
                D   = [wrap#(d(0,V_bar,V_bar,V_bar):-true|true),
                       wrap#(otherwise),
                       wrap#(d(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                       |D0],
                V_bar = variable("_"),
                V_A = variable("A_A").


clause2kl10([S|R], J, Max, Init,
            CH0, C0, D0,
            CH , C , D ):-
        true|
                seq2list(S, N, CList, Guard, Macro, RLists, M),
                new_max(Max1, M, Max),
                clause2kl1_a_sntnc(N,
                                   CList, Guard, Macro, RLists,
                                   J , Init ,
                                   CH , C , D ,
                                   J1, Init1,
                                   CH1, C1, D1),
                clause2kl10(R, J1, Max1, Init1,
                            CH0, C0, D0,
                            CH1, C1, D1).


%%clause2kl1_a_sntnc--------------------------------------------------

clause2kl1_a_sntnc(N,
                   wrap#([]), Guard, Macro, RLists,
                   J , Init ,
                   CH , C , D ,
                   J1, Init1,
                   CH1, C1, D1):-
        true|
                wappend(RLists,Init1,Init),
                J := J1,
                CH = CH1,
                C = C1,
                D = D1.

otherwise.

clause2kl1_a_sntnc(N,
                   CList, Guard, Macro, wrap#([disj(0,[])]),
                   J , Init ,
                   CH , C , D ,
                   J1, Init1,
                   CH1, C1, D1):-
        true|
                Init1 = Init,
                J := J1,
                CH = CH1,
                C = C1,
                D = D1.

otherwise.

clause2kl1_a_sntnc(N,
                   CList, Guard, Macro, RLists,
                   J , Init ,
                   CH , C , D ,
                   J1, Init1,
                   CH1, C1, D1):-
        true|
                Init = Init1,
                clause2kl1_a_sntnc0(N, N,
                                    CList, Guard, Macro ,RLists,
                                    J ,
                                    CH , C , D ,
                                    J1,
                                    CH1, C1, D1).

%%clause2kl1_a_sntnc0--------------------------------------------------

clause2kl1_a_sntnc0(N, 0,
                    CList, Guard, Macro, RLists,
                    J ,
                    CH , C , D ,
                    J1,
                    CH1, C1, D1):-
        true|
                J := J1,
                CH = CH1,
                C = C1,
                D = D1.

clause2kl1_a_sntnc0(N, NN,
                    CList, Guard, Macro, RLists,
                    J ,
                    CH , C , D ,
                    J2,
                    CH2, C2, D2):-
        0 < NN|
                J := J1 + 1,
                NN1 := NN - 1,
                clause2kl1_clauses(N, NN, N,
                                   J,
                                   CList, wrap#([PCh]), Guard, Macro, RLists,
                                   PCh,
                                   CH , C , D ,
                                   CH1, C1, D1),
                clause2kl1_a_sntnc0(N, NN1,
                                    CList, Guard, Macro, RLists,
                                    J1,
                                    CH1, C1, D1,
                                    J2,
                                    CH2, C2, D2).

%%clause2kl1_clauses------------------------------------------------

clause2kl1_clauses(N, NN, 0,
                   J,
                   CListY, CListA, Guard, Macro ,RLists,
                   PCh,
                   CH , C , D ,
                   CH1, C1, D1):-
        true|
                CH = CH1,
                C = C1,
                D = D1.

otherwise.

%%
%%  3 5 5 5 5 5 5 2              (1) Add c/4 to the list C (end).
%%  4 3 5 5 5 5 5 2 ^            (2) Add c/4 and d/4 to the list C and D (end).
%%  4 4 3 5 5 5 5 2 |            (3) Add ch/3 to the list CH.
%%  4 4 4 3 5 5 5 2 | NN         (4) Add c/4 to the list C.
%%  4 4 4 4 3 5 5 2 |            (5) Add c/4 and d/4 to the list C and D.
%%  4 4 4 4 4 3 5 2 |
%%  4 4 4 4 4 4 3 2 |
%%  4 4 4 4 4 4 1 3
%%    <----------
%%       NNN
%%  

%% (1) when NN =:= 1, NNN =:= 2

clause2kl1_clauses(N, NN, NNN,
                   J,
                   wrap#([P|CListYR]), CListA, Guard, Macro ,RLists,
                   PCh,
                   CH1, C , D1,
                   CH2, C2, D2):-
        NN =:= 1, NNN =:= 2|
                C = [wrap#(c(I_J,P,CListA,V_A):-
                           Guard|
                           Body)
                     |C1],
                Macro = macro(MList,Tail),
                Tail = wrap#(V_A=succ(RLists)),
                Body = MList,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, wrap#([P|CListA]), Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).

%% (2) when NN =\= 1, NNN =:= 1

clause2kl1_clauses(N, NN, NNN,
                   J,
                   wrap#([P|CListYR]), CListA, Guard, Macro ,RLists,
                   PCh,
                   CH1, C , D ,
                   CH2, C2, D2):-
        NN =\= 1, NNN =:= 1|
                C = [wrap#(c(I_J,P,CListA,V_A):-
                           Guard|
                           Body)
                     |C1],
                D = [wrap#(d(I_J,P,CListA,V_A):-
                           Guard|
                           Body)
                     |D1],
                Macro = macro(MList,Tail),
                Tail = wrap#(V_A=succ(RLists)),
                Body = MList,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, wrap#([P|CListA]), Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).

%% (3) when NN =:= NNN

clause2kl1_clauses(N, NN, NNN,
                   J,
                   wrap#([P|CListYR]), CListA, Guard, Macro, RLists,
                   PCh,
                   CH , C1, D1,
                   CH2, C2, D2):-
        NN =:= NNN, N =:= 1|
                CH = [wrap#(ch(I_J,P,V_A):-
                            Guard|
                            Body)
                      |CH1],
                Macro = macro(MList,Tail),
                Tail = wrap#(V_A=succ(RLists)),
                Body = MList,
                I_J = integer(J),
                V_A = variable("A_A"),
                PCh = P,
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, CListA, Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).

clause2kl1_clauses(N, NN, NNN,
                  J,
                  wrap#([P|CListYR]), CListA, Guard, Macro, RLists,
                  PCh,
                  CH , C1, D1,
                  CH2, C2, D2):-
        NN =:= NNN, 1 < N|
                CH = [wrap#(ch(I_J,P,V_A):-true|V_A=cont)
                      |CH1],
                I_J = integer(J),
                V_A = variable("A_A"),
                PCh = P,
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, CListA, Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).

%% (4) when 2 < NNN, NN < NNN

clause2kl1_clauses(N, NN, NNN,
                   J,
                   wrap#([P|CListYR]), CListA, Guard, Macro, RLists,
                   PCh,
                   CH1, C , D1,
                   CH2, C2, D2):-
        2 < NNN, NN < NNN|
                C = [wrap#(c(I_J,P,CListA,V_A):-true|V_A=cont)
                     |C1],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, wrap#([P|CListA]), Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).

%% (5) when 1 < NNN, NNN < NN

clause2kl1_clauses(N, NN, NNN,
                   J,
                   wrap#([P|CListYR]), CListA, Guard, Macro, RLists,
                   PCh,
                   CH1, C , D ,
                   CH2, C2, D2):-
        1 < NNN, NNN < NN|
                C = [wrap#(c(I_J,P,CListA,V_A):-true|V_A=cont)
                     |C1],
                D = [wrap#(d(I_J,P,CListA,V_A):-true|V_A=cont)
                     |D1],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1_clauses(N, NN, NNN1,
                                   J,
                                   CListYR, wrap#([P|CListA]), Guard,
                                   Macro, RLists,
                                   PCh,
                                   CH1, C1, D1,
                                   CH2, C2, D2).


%%%%%%%%%%%%%%%%%%%%%%%% FALSE CHECK PART I %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%clause2kl1f1------------------------------------------------------------

clause2kl1f1([], J,
             CHF10, CF10, DF10,
             CHF1 , CF1 , DF1 ):-
        true|
                J = 0,
                CHF1  = [wrap#(chf1(0,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(chf1(V_bar,V_bar,V_A):-true|V_A=fail)
                         |CHF10],
                CF1   = [wrap#(cf1(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(cf1(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |CF10],
                DF1   = [wrap#(df1(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(df1(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |DF10],
                V_bar = variable("_"),
                V_A = variable("A_A").

clause2kl1f1([wrap#(false :- Cond)|R], J,
             CHF10, CF10, DF10,
             CHF1 , CF1 , DF1 ):-
        true|
                S = wrap#(false :- Cond),
                seq2list(S, N, CList, Guard, Macro, RLists, M),
                clause2kl1f1_a_sntnc(N,
                                     CList, Guard,
                                     J ,
                                     CHF1 , CF1 , DF1 ,
                                     J1,
                                     CHF11, CF11, DF11),
                clause2kl1f1(R, J1,
                             CHF10, CF10, DF10,
                             CHF11, CF11, DF11).

otherwise.

clause2kl1f1([S|R], J,
             CHF10, CF10, DF10,
             CHF1 , CF1 , DF1 ):-
        true|
                clause2kl1f1(R, J,
                             CHF10, CF10, DF10,
                             CHF1 , CF1 , DF1 ).


%%clause2kl1f1_a_sntnc------------------------------------------------

clause2kl1f1_a_sntnc(N,
                     CList, Guard,
                     J ,
                     CHF1 , CF1 , DF1 ,
                     J1,
                     CHF11, CF11, DF11):-
        true|
                clause2kl1f1_a_sntnc0(N, N,
                                      CList, Guard,
                                      J ,
                                      CHF1 , CF1 , DF1 ,
                                      J1,
                                      CHF11, CF11, DF11).

%%clause2kl1f1_a_sntnc0------------------------------------------------------

clause2kl1f1_a_sntnc0(N, 0,
                      CList, Guard,
                      J,
                      CHF1 , CF1 , DF1 ,
                      J1,
                      CHF11, CF11, DF11):-
        true|
                J := J1,
                CHF1 = CHF11,
                CF1 = CF11,
                DF1 = DF11.

clause2kl1f1_a_sntnc0(N, NN,
                      CList, Guard,
                      J,
                      CHF1 , CF1 , DF1 ,
                      J2,
                      CHF12, CF12, DF12):-
        0 < NN|
                J := J1 + 1,
                NN1 := NN - 1,
                clause2kl1f1_clauses(N, NN, N,
                                     J,
                                     CList, wrap#([PCh]), Guard,
                                     PCh,
                                     CHF1 , CF1 , DF1 ,
                                     CHF11, CF11, DF11),
                clause2kl1f1_a_sntnc0(N, NN1,
                                      CList, Guard,
                                      J1,
                                      CHF11, CF11, DF11,
                                      J2,
                                      CHF12, CF12, DF12).

%%clause2kl1f1_clauses------------------------------------------------------

clause2kl1f1_clauses(N, NN, 0,
                     J,
                     CListY, CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF11, CF11, DF11):-
        true|
                CHF1 = CHF11,
                CF1 = CF11,
                DF1 = DF11.

otherwise.

%%
%%  3 2 2 2 2 2 2 2              (1) Add cf1/4 to the list CF1.
%%  1 3 2 2 2 2 2 2 ^            (2) Add cf1/4 and df1/4 
%%  1 1 3 2 2 2 2 2 |                    to the lists CF1 and DF1.
%%  1 1 1 3 2 2 2 2 | NN         (3) Add chf1/3 to the list CHF1.
%%  1 1 1 1 3 2 2 2 |
%%  1 1 1 1 1 3 2 2 |
%%  1 1 1 1 1 1 3 2 |
%%  1 1 1 1 1 1 1 3
%%    <----------
%%       NNN
%%  

%% (1) when NN < NNN

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NN =:= 1, NNN =:= 2|
                CHF1 = CHF11,
                CF1 = [wrap#(cf1(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF11],
                DF1 = DF11,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

otherwise.

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NN < NNN|
                CHF1 = CHF11,
                CF1 = [wrap#(cf1(I_J,P,CListA,V_A):-true|V_A=cont)
                       |CF11],
                DF1 = DF11,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

%% (2) when NNN < NN

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NNN < NN, NNN =:= 1|
                CHF1 = CHF11,
                CF1 = [wrap#(cf1(I_J,P,CListA,V_A):-Guard|V_A=succ)
                        |CF11],
                DF1 = [wrap#(df1(I_J,P,CListA,V_A):-Guard|V_A=succ)
                        |DF11],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

otherwise.

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NNN < NN|
                CHF1 = CHF11,
                CF1 = [wrap#(cf1(I_J,P,CListA,V_A):-true|V_A=cont)
                        |CF11],
                DF1 = [wrap#(df1(I_J,P,CListA,V_A):-true|V_A=cont)
                        |DF11],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

%% (3) when NN =:= NNN

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NN =:= NNN, N =:= 1|
                PCh = P,
                CHF1 = [wrap#(chf1(I_J,P,V_A):-Guard|V_A=succ)
                        |CHF11],
                CF1 = CF11,
                DF1 = DF11,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#(CListA), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

otherwise.

clause2kl1f1_clauses(N, NN, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh,
                     CHF1 , CF1 , DF1 ,
                     CHF12, CF12, DF12):-
        NN =:= NNN|
                PCh = P,
                CHF1 = [wrap#(chf1(I_J,P,V_A):-true|V_A=cont)
                        |CHF11],
                CF1 = CF11,
                DF1 = DF11,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f1_clauses(N, NN, NNN1,
                                     J,
                                     CListYR, wrap#(CListA), Guard,
                                     PCh,
                                     CHF11, CF11, DF11,
                                     CHF12, CF12, DF12).

%%%%%%%%%%%%%%%%%%%%%%%% FALSE CHECK PART II %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%clause2kl1f2------------------------------------------------------------

clause2kl1f2([], J,
             CHF20, CF20, DF20, EF20,
             CHF2 , CF2 , DF2 , EF2 ):-
        true|
                J = 0,
                CHF2 = [wrap#(chf2(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(chf2(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |CHF20],
                CF2  = [wrap#(cf2(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(cf2(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |CF20],
                DF2  = [wrap#(df2(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(df2(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |DF20],
                EF2  = [wrap#(ef2(0,V_bar,V_bar,V_bar):-true|true),
                         wrap#(otherwise),
                         wrap#(ef2(V_bar,V_bar,V_bar,V_A):-true|V_A=fail)
                         |EF20],
                V_bar = variable("_"),
                V_A = variable("A_A").

clause2kl1f2([wrap#(false :- Cond)|R], J,
             CHF20, CF20, DF20, EF20,
             CHF2 , CF2 , DF2 , EF2 ):-
        true|
                S = wrap#(false :- Cond),
                seq2list(S, N, CList, Guard, Macro, RLists, M),
                clause2kl1f2_a_sntnc(N,
                                     CList, Guard,
                                     J,
                                     CHF2 , CF2 , DF2 , EF2,
                                     J1,
                                     CHF21, CF21, DF21, EF21),
                clause2kl1f2(R, J1,
                             CHF20, CF20, DF20, EF20,
                             CHF21, CF21, DF21, EF21).

otherwise.

clause2kl1f2([S|R], J,
             CHF20, CF20, DF20, EF20,
             CHF2 , CF2 , DF2 , EF2 ):-
        true|
                clause2kl1f2(R, J,
                             CHF20, CF20, DF20, EF20,
                             CHF2 , CF2 , DF2 , EF2 ).


%%clause2kl1f2_a_sntnc------------------------------------------------------

clause2kl1f2_a_sntnc(N,
                     CList, Guard,
                     J,
                     CHF2 , CF2 , DF2 , EF2,
                     J1,
                     CHF21, CF21, DF21, EF21):-
        N < 2|
                J := J1,
                CHF2 = CHF21,
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21.

otherwise.

clause2kl1f2_a_sntnc(N,
                     CList, Guard,
                     J,
                     CHF2 , CF2 , DF2 , EF2,
                     J1,
                     CHF21, CF21, DF21, EF21):-
        true|
                clause2kl1f2_a_sntnc0(N, N,
                                      CList, Guard,
                                      J,
                                      CHF2 , CF2 , DF2 , EF2 ,
                                      J1,
                                      CHF21, CF21, DF21, EF21).

%%clause2kl1f2_a_sntnc0------------------------------------------------------

clause2kl1f2_a_sntnc0(N, 0,
                      CList, Guard,
                      J,
                      CHF2 , CF2 , DF2 , EF2,
                      J1,
                      CHF21, CF21, DF21, EF21):-
        true|
                J := J1,
                CHF2 = CHF21,
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21.

otherwise.

clause2kl1f2_a_sntnc0(N, NN,
                      CList, Guard,
                      J,
                      CHF2 , CF2 , DF2 , EF2,
                      J2,
                      CHF22, CF22, DF22, EF22):-
        true|
                NN1 := NN - 1,
                clause2kl1f2_a_sntnc00(N, NN, N,
                                       CList, Guard,
                                       J,
                                       CHF2 , CF2 , DF2 , EF2,
                                       J1,
                                       CHF21, CF21, DF21, EF21),
                clause2kl1f2_a_sntnc0(N, NN1,
                                      CList, Guard,
                                      J1,
                                      CHF21, CF21, DF21, EF21,
                                      J2,
                                      CHF22, CF22, DF22, EF22).


%%clause2kl1f2_a_sntnc00------------------------------------------------------

clause2kl1f2_a_sntnc00(N, NN, 0,
                       CList, Guard,
                       J,
                       CHF2 , CF2 , DF2 , EF2,
                       J1,
                       CHF21, CF21, DF21, EF21):-
        true|
                J := J1,
                CHF2 = CHF21,
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21.

clause2kl1f2_a_sntnc00(N, NN, Nn,
                       CList, Guard,
                       J,
                       CHF2 , CF2 , DF2 , EF2,
                       J1,
                       CHF22, CF22, DF22, EF22):-
        NN =:= Nn|
                Nn1 := Nn - 1,
                clause2kl1f2_a_sntnc00(N, NN, Nn1,
                                       CList, Guard,
                                       J,
                                       CHF2 , CF2 , DF2 , EF2,
                                       J1,
                                       CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_a_sntnc00(N, NN, Nn,
                       CList, Guard,
                       J,
                       CHF2 , CF2 , DF2 , EF2,
                       J2,
                       CHF22, CF22, DF22, EF22):-
        true|
                J := J1 + 1,
                Nn1 := Nn - 1,
                clause2kl1f2_clauses(N, NN, Nn, N,
                                     J,
                                     CList, wrap#([QCh,PCh]), Guard,
                                     PCh, QCh,
                                     CHF2 , CF2 , DF2 , EF2,
                                     CHF21, CF21, DF21, EF21),
                clause2kl1f2_a_sntnc00(N, NN, Nn1,
                                       CList, Guard,
                                       J1,
                                       CHF21, CF21, DF21, EF21,
                                       J2,
                                       CHF22, CF22, DF22, EF22).

%%clause2kl1f2_clauses------------------------------------------------------

clause2kl1f2_clauses(N, NN, Nn, 0,
                     J,
                     CListY, CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF21, CF21, DF21, EF21):-
        true|
                CHF2 = CHF21,
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21.

otherwise.

%%          Nn
%%          |
%%          v                    (0) Add nothing. (And unfy QCh with P.)
%%  5 2 2 2 0 4 4 4              (1) Add cf2/4 to the list CF2.
%%  1 5 2 2 0 4 4 4 ^            (2) Add cf2/4 and df2/4 
%%  1 1 5 2 0 4 4 4 |                    to the lists CF2 and DF2.
%%  1 1 1 5 0 4 4 4 | NN         (3) Add cf2/4 and ef2/4
%%  - - - - - - - - |                    to the lists CF2 and EF2.
%%  1 1 1 1 0 5 4 4 |            (4) Add cf2/4, df2/4 and ef2/4
%%  1 1 1 1 0 3 5 4 |                    to the lists CF2, DF2 and EF2.
%%  1 1 1 1 0 3 3 5              (5) Add chf2/4 to the list CHF2. 
%%    <----------                                   (And unfy PCh with P.)
%%       NNN
%%  

%% (0) when Nn =:= NNN

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        Nn =:= NNN |
                QCh = P,
                CHF2 = CHF21,
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21,
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, CListA, Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

%% (1) when NN < NNN, Nn < NNN

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NN < NNN, Nn < NNN, NN =:= 1, Nn =:= 2, NNN =:= 3|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF21],
                DF2 = DF21,
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NN < NNN, Nn < NNN, NN =:= 2, Nn =:= 1, NNN =:= 3|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF21],
                DF2 = DF21,
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NN < NNN, Nn < NNN|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |CF21],
                DF2 = DF21,
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

%% (2) when NNN < NN, Nn < NNN

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN < NN, Nn < NNN, 2 < NN, Nn =:= 1, NNN =:= 2|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF21],
                DF2 = [wrap#(df2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |DF21],
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN < NN, Nn < NNN|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |CF21],
                DF2 = [wrap#(df2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |DF21],
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

%% (3) when NN < NNN, NNN < Nn

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NN < NNN, NNN < Nn, NN =:= 1, 2 < Nn, NNN =:= 2|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF21],
                DF2 = DF21,
                EF2 = [wrap#(ef2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |EF21],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NN < NNN, NNN < Nn|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |CF21],
                DF2 = DF21,
                EF2 = [wrap#(ef2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |EF21],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

%% (4) when NNN < NN, NNN < Nn

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN < NN, NNN < Nn, NNN =:= 1|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |CF21],
                DF2 = [wrap#(df2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |DF21],
                EF2 = [wrap#(ef2(I_J,P,CListA,V_A):-Guard|V_A=succ)
                       |EF21],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN < NN, NNN < Nn|
                CHF2 = CHF21,
                CF2 = [wrap#(cf2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |CF21],
                DF2 = [wrap#(df2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |DF21],
                EF2 = [wrap#(ef2(I_J,P,CListA,V_A):-true|V_A=cont)
                       |EF21],
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, wrap#([P|CListA]), Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

%% (5) when NNN =:= NN

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN =:= NN, N =:= 2|
                PCh = P,
                CHF2 = [wrap#(chf2(I_J,P,QCh,V_A):-Guard|V_A=succ)
                       |CHF21],
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, CListA, Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).

otherwise.

clause2kl1f2_clauses(N, NN, Nn, NNN,
                     J,
                     wrap#([P|CListYR]), CListA, Guard,
                     PCh, QCh,
                     CHF2 , CF2 , DF2 , EF2,
                     CHF22, CF22, DF22, EF22):-
        NNN =:= NN, 2 < N|
                PCh = P,
                CHF2 = [wrap#(chf2(I_J,P,QCh,V_A):-true|V_A=cont)
                       |CHF21],
                CF2 = CF21,
                DF2 = DF21,
                EF2 = EF21,
                I_J = integer(J),
                V_A = variable("A_A"),
                NNN1 := NNN - 1,
                clause2kl1f2_clauses(N, NN, Nn, NNN1,
                                     J,
                                     CListYR, CListA, Guard,
                                     PCh, QCh,
                                     CHF21, CF21, DF21, EF21,
                                     CHF22, CF22, DF22, EF22).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%% C O M M O N %%%%%%%%%%%%%%%%%%%%%%%%%%%

%%new_max------------------------------------------------------------

new_max(Max1, M, Max):-
        Max1 < M|
                Max := M.

otherwise.

new_max(Max1, M, Max):-
        true|
                Max := Max1.



%%seq2list------------------------------------------------------------

seq2list(wrap#(Resu :- Cond), N, CList, Guard, Macro, RLists, M):-
        true|
                s2l_C(wrap#(true), Cond, N, CList, Guard ),
                s2l_R(Tail, Resu, M, MList, RLists),
                Macro = macro(MList,Tail).


%%s2l_C------------------------------------------------------------

s2l_C(Tail, wrap#(true,Cs), N, CList, Guard):-
        true|
                N := N1,
                CList = CList1,
                Guard = Guard1,
                s2l_C(Tail, Cs, N1, CList1, Guard1).

otherwise.

s2l_C(wrap#(true), wrap#({G},Cs), N, CList, Guard):-
        true|
                N := N1,
                CList = CList1,
                Guard = Guard1,
                s2l_C(G, Cs, N1, CList1, Guard1).

otherwise.

s2l_C(Tail, wrap#({G},Cs), N, CList, Guard):-
        true|
                N := N1,
                CList = CList1,
                Guard = wrap#(Tail,Guard1),
                s2l_C(G, Cs, N1, CList1, Guard1).

otherwise.

s2l_C(Tail, wrap#(C,Cs), N, CList, Guard):-
        true|
                N := N1 + 1,
                CList = wrap#([C|CList1]),
                Guard = Guard1,
                s2l_C(Tail, Cs, N1, CList1, Guard1).

otherwise.

s2l_C(Tail, wrap#(true), N, CList, Guard):-
        true|
                N := 0,
                CList = wrap#([]),
                Guard = Tail.

otherwise.

s2l_C(wrap#(true), wrap#({G}), N, CList, Guard):-
        true|
                N := 0,
                CList = wrap#([]),
                Guard = G.

otherwise.

s2l_C(Tail, wrap#({G}), N, CList, Guard):-
        true|
                N := 0,
                CList = wrap#([]),
                Guard = wrap#(Tail,G).

otherwise.

s2l_C(Tail, C, N1, CList, Guard):-
        true|
                N1 := 1,
                CList = wrap#([C]),
                Guard = Tail.

%%s2l_R------------------------------------------------------------

s2l_R(Tail, wrap#({Mc},Ds), M, MList, RLists):-
        true|
                M = M1,
                MList = wrap#(Mc,MList1),
                RLists = RLists1,
                s2l_R(Tail, Ds, M1, MList1, RLists1).

otherwise.

s2l_R(Tail, wrap#(D,Ds), M, MList, RLists):-
        true|
                new_max(M1, MM, M),
                MList = MList1,
                s2l_R0(D, MM, DL),
                RLists = wrap#([disj(I_MM,DL)|RLists1]),
                I_MM = integer(MM),
                s2l_R(Tail, Ds, M1, MList1, RLists1).

otherwise.

s2l_R(Tail, wrap#({Mc}), M, MList, RLists):-
        true|
                M = 0,
                MList = wrap#(Mc,Tail),
                RLists = wrap#([]).

otherwise.

s2l_R(Tail, D, M, MList, RLists):-
        true|
                M = MM,
                MList = Tail,
                s2l_R0(D, MM, DL),
                RLists = wrap#([disj(I_MM,DL)]),
                I_MM = integer(MM).


%%s2l_R0------------------------------------------------------------

s2l_R0(wrap#(false;Rs), MM, DL):-
        true|
                MM := MM1,
                DL = DL1,
                s2l_R0(Rs, MM1, DL1).

otherwise.

s2l_R0(wrap#(R;Rs), MM, DL):-
        true|
                MM := MM1 + 1,
                DL = wrap#([R|DL1]),
                s2l_R0(Rs, MM1, DL1).

otherwise.

s2l_R0(wrap#(false), MM, DL):-
        true|
                MM := 0,
                DL = wrap#([]).

otherwise.

s2l_R0(R, MM, DL):-
        true|
                MM := 1,
                DL = wrap#([R]).


%%wappend--------------------------------------------------------

wappend(wrap#([]), WList, NewWList):-
        true|
                NewWList = WList.


wappend(wrap#([H|T]), WList, NewWList):-
        true|
                NewWList = wrap#([H|WList1]),
                wappend(T, WList, WList1).

