:- module( _, [simplify/2], [assertions , regtypes , basicmodes , nativeprops] ).

simplify([[ref(loc(1))],[[num(int(A)),num(int(0))]]],[ref(loc(2)),[[num(int(A)),num(int(0))],[num(int(B)),num(int(C))]]]) :-
        A=\=0,
        B is A//A,
        A=\=0,
        C is 0//A .
simplify([[ref(loc(1))],[[num(int(0)),num(int(0))]]],[refE(loc(2)),[[num(int(0)),num(int(0))],[]]]).
simplify([[ref(loc(1))],[[num(int(A)),num(int(B))]]],[C,D]) :-
        B\=0,
        B=\=0,
        E is A rem B,
        execute_notrace_1(A,B,E,D,_1,_2,_3,_4,C,_5) .

execute_notrace_1(A,B,0,[[num(int(A)),num(int(B))],[num(int(C)),num(int(D))]],_1,_2,_3,_4,ref(loc(2)),[]) :-
        B=\=0,
        C is A//B,
        B=\=0,
        D is B//B .
execute_notrace_1(A,0,0,[[num(int(A)),num(int(0))],[]],_1,_2,_3,_4,refE(loc(2)),[]).
execute_notrace_1(A,B,C,D,E,F,G,H,I,J) :-
        C\=0,
        C=\=0,
        K is B rem C,
        execute_notrace_2(A,B,C,K,D,E,F,G,H,I,J) .

execute_notrace_2(A,B,C,0,[[num(int(A)),num(int(B))],[num(int(D)),num(int(E))]],_1,_2,_3,_4,ref(loc(2)),[]) :-
        C=\=0,
        D is A//C,
        C=\=0,
        E is B//C .
execute_notrace_2(A,B,0,0,[[num(int(A)),num(int(B))],[]],_1,_2,_3,_4,refE(loc(2)),[]).
execute_notrace_2(A,B,C,D,E,F,G,H,I,J,K) :-
        D\=0,
        D=\=0,
        L is C rem D,
        execute_notrace_2(A,B,D,L,E,F,G,H,I,J,K) .


