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

search([[ref(loc(1)),num(int(_1))],[array(A,_2)]],[num(int(0))]) :-
        0>=A .
search([[ref(loc(1)),num(int(A))],[array(B,[num(int(D))|C])]],[E]) :-
        0<B,
        0<B,
        D\=A,
        execute_notrace_1([num(int(D))|C],B,A,0,1,_1,_2,_3,_4,_5,E,_6) .
search([[ref(loc(1)),num(int(A))],[array(B,[num(int(A))|C])]],[D]) :-
        0<B,
        0<B,
        execute_notrace_1([num(int(A))|C],B,A,1,0,_1,_2,_3,_4,_5,D,_6) .
search([[ref(loc(1)),num(int(_1))],[array(A,_2)]],[refE(loc(2))]) :-
        0<A,
        0>=A .
search([[null,num(int(_1))],[]],[refE(loc(1))]).

execute_notrace_1(A,B,_1,_2,C,[array(B,A)],_3,_4,_5,_6,num(int(C)),[]) :-
        C>=B .
execute_notrace_1(A,B,_1,C,D,[array(B,A)],_2,_3,_4,_5,num(int(D)),[]) :-
        D<B,
        C\=0 .
execute_notrace_1(A,B,C,0,D,E,F,G,H,I,J,K) :-
        D<B,
        0=<D,
        D<B,
        L is D+1,
        mynth(L,A,num(int(M))),
        M\=C,
        N is D+1,
        execute_notrace_1(A,B,C,0,N,E,F,G,H,I,J,K) .
execute_notrace_1(A,B,C,0,D,E,F,G,H,I,J,K) :-
        D<B,
        0=<D,
        D<B,
        L is D+1,
        mynth(L,A,num(int(C))),
        execute_notrace_1(A,B,C,1,D,E,F,G,H,I,J,K) .
execute_notrace_1(A,B,_1,0,C,[array(B,A),[]],_2,_3,_4,_5,refE(loc(2)),[]) :-
        C<B,
        C<0 .
execute_notrace_1(A,B,_1,0,C,[array(B,A),[]],_2,_3,_4,_5,refE(loc(2)),[]) :-
        C<B,
        C>=B .


