From 4678b2baee02664f6d9d9a12b21a788af71262bd Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 8 Feb 2019 09:33:07 +0000 Subject: [PATCH] dbg --- C/terms.c | 267 ++++++++++++++++------------------------- C/write.c | 4 +- os/format.c | 10 +- pl/undefined.yap | 4 +- regression/cyclics.yap | 197 ++++++++++++------------------ 5 files changed, 194 insertions(+), 288 deletions(-) diff --git a/C/terms.c b/C/terms.c index 07be93eb1..c7ee968e4 100644 --- a/C/terms.c +++ b/C/terms.c @@ -105,7 +105,7 @@ typedef struct non_single_struct_t { *to_visit0 = to_visit, \ *to_visit_max = to_visit + 1024; \ \ - do{ \ + while (to_visit >= to_visit0) { \ CELL d0; \ CELL *ptd0; \ restart:\ @@ -114,7 +114,7 @@ while (pt0 < pt0_end) { \ ptd0 = pt0; \ d0 = *ptd0; \ list_loop: \ - fprintf(stderr, "%ld at %s\n", to_visit - to_visit0, __FUNCTION__); \ + /*fprintf(stderr, "%ld at %s\n", to_visit - to_visit0, __FUNCTION__);*/ \ deref_head(d0, var_in_term_unk); \ var_in_term_nvar : { \ if (IsPairTerm(d0)) { \ @@ -172,14 +172,13 @@ while (pt0 < pt0_end) { \ }\ }\ /* Do we still have compound terms to visit */ \ - if (to_visit > to_visit0) {\ - to_visit--;\ -\ - pt0 = to_visit->pt0;\ + to_visit--; \ + if (to_visit >= to_visit0) {\ + pt0 = to_visit->pt0; \ pt0_end = to_visit->pt0_end;\ - *to_visit->ptd0 = to_visit->d0;\ - } \ - } while (to_visit>to_visit0); \ + *to_visit->ptd0 = to_visit->d0; \ + }\ + }\ pop_text_stack(lvl); #define def_aux_overflow() \ @@ -221,13 +220,13 @@ while (pt0 < pt0_end) { \ #define CYC_LIST \ if (d0 == TermFreeTerm) { \ - fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);\ + /*fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \ while (to_visit > to_visit0) { \ to_visit--; \ to_visit->ptd0[0] = \ to_visit->d0; \ } \ - pop_text_stack(lvl); fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);\ + pop_text_stack(lvl); /*fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \ return true; \ } @@ -238,7 +237,7 @@ while (to_visit > to_visit0) { \ to_visit->ptd0[0] = \ to_visit->d0; \ } \ - fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);\ + /*fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \ return true; \ } @@ -645,7 +644,7 @@ static Term attvars_in_complex_term(register CELL *pt0, register CELL *pt0_end, } } - fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__); + /*fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__)*/; return (output); def_aux_overflow(); @@ -1189,8 +1188,9 @@ static Int largest_numbervar(USES_REGS1) { } static Term BREAK_LOOP(Int ddep) { - Term t0 = MkIntegerTerm(ddep); - return Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("@^"), 1), 1, &t0); + char buf[64]; + snprintf(buf, 63, "@^[" Int_FORMAT "]", ddep); + return MkAtomTerm(Yap_LookupAtom(buf)); } static Term UNFOLD_LOOP(Term t, Term *b) { @@ -1203,161 +1203,106 @@ static Term UNFOLD_LOOP(Term t, Term *b) { return o; } -static Term *loops_in_complex_term(CELL *pt0, CELL *pt0_end, - Term *listp USES_REGS) { - int lvl = push_text_stack(); - CELL *ptf0 = HR; - tr_fr_ptr TR0 = TR; - struct non_single_struct_t *to_visit = Malloc( - 1024 * sizeof(struct non_single_struct_t)), - *to_visit0 = to_visit, - *to_visit_max = to_visit + 1024; - do{ - CELL *ptd0, - *ptf = HR; - CELL d0; - restart: - while (pt0 < pt0_end) { - ++pt0; - ptd0 = pt0; - d0 = *ptd0; - list_loop: - { - fprintf(stderr,"%ld at %s\n", to_visit-to_visit0, __FUNCTION__); - deref_head(d0, vars_in_term_unk); - vars_in_term_nvar: - if (IsPairTerm(d0)) { - if (to_visit + 32 >= to_visit_max) { - goto aux_overflow; - } - ptd0 = RepPair(d0); - d0 = ptd0[0]; - if (listp) { - CELL *pt = VarOfTerm(d0); - if (pt &&pt >= ptf && - pt < HR) { - // LIST0; - *ptf++ = UNFOLD_LOOP(AbsPair(pt), listp); - continue; - } else { - *ptf++ = AbsPair(HR); - MaBind( ptd0, AbsPair(ptf - 1)); - } - } else { - struct non_single_struct_t *v0 = - (struct non_single_struct_t *)AtomOfTerm(d0); - if (IsAtomTerm(d0) && v0 >= to_visit0 && - (CELL *)AtomOfTerm(d0) < (CELL *)to_visit) { - // LIST0; - *ptf++ = BREAK_LOOP(to_visit - v0); - continue; - } else { - *ptf++ = AbsPair(HR); - to_visit->ptd0 = ptd0; - to_visit->d0 = d0 = *ptd0; - *ptd0 = MkAtomTerm((AtomEntry *)to_visit); - } - } - to_visit->pt0 = pt0; - to_visit->pt0_end = pt0_end; - to_visit->ptf = ptf; - to_visit++; - pt0 = ptd0; - pt0_end = pt0 + 1; - ptd0 = pt0; - ptf = HR; - HR+=2; - goto list_loop; - } else if (IsApplTerm(d0)) { - register Functor f; +typedef struct block_connector { + Int id; //> index in the array; + Term source; //> source; + CELL *copy; //> copy; + CELL header; //> backup of first word of the source data; + CELL reference; //> term used to refer the copy. +} cl_connector; - /* store the terms to visit */ - ptd0 = RepAppl(d0); - f = (Functor)(*ptd0); - if (IsExtensionFunctor(f) || f == FunctorDollarVar) { - *ptf++ = d0; - continue; - } - if (listp) { - CELL *pt = (CELL *)f; - if (IsVarTerm(d0) && pt >= ptf0 && - pt < HR) { - // LIST0; - *ptf++ = UNFOLD_LOOP(AbsAppl(pt), listp); - continue; - } else { - *ptf++ = AbsAppl(HR); - MaBind( pt, AbsAppl(ptf - 1)); - } - } else { - struct non_single_struct_t *v0 = - (struct non_single_struct_t *)AtomOfTerm(d0); - if (IsAtomTerm(d0) && v0 >= to_visit0 && - v0 < to_visit) { - // LIST0; - *ptf++ = BREAK_LOOP(to_visit - v0); - continue; - } else { - *ptf++ = AbsAppl(HR); - to_visit->ptd0 = ptd0; - to_visit->d0 = d0; - *ptd0 = MkAtomTerm((AtomEntry *)to_visit); - } - } - // STRUCT0; - if (to_visit + 32 >= to_visit_max) { - goto aux_overflow; - } - to_visit->pt0 = pt0; - to_visit->pt0_end = pt0_end; - to_visit->ptf = ptf; - to_visit++; - pt0 = ptd0; - pt0_end = ptd0 + (ArityOfFunctor(f)); - HR[0] = (CELL)f; - ptf = HR+1; - HR = ptf +ArityOfFunctor(f); - } else { - *ptf++ = d0; - } - continue; - } - - derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar); - *ptf++ = *ptd0; - continue; - } - /* Do we still have compound terms to visit */ - while (to_visit > to_visit0) { - to_visit--; - - pt0 = to_visit->pt0; - ptf = to_visit->ptf; - pt0_end = to_visit->pt0_end; - to_visit->ptd0[0] = to_visit->d0; - } - } while (to_visit > to_visit0) ; - fprintf(stderr,"exit %ld at %s\n", to_visit-to_visit0, __FUNCTION__); - if (listp) { - clean_tr(TR0); - } - pop_text_stack(lvl); - return ptf0; +Int cp_link(Term t,Int i, Int j, cl_connector *q, Int max, CELL *tailp) +{ + Term ref, h, *s, *ostart; + bool pair = false; + ssize_t n; - def_aux_overflow(); + if (IsVarTerm(t) || IsPrimitiveTerm(t)) { + q[i].copy[j] = t; + return max; + } + ostart = HR; + if (IsPairTerm(t)) { + h = HeadOfTerm(t); + s = RepPair(t); + n = 2; + pair = true; + ref = AbsPair(ostart); + } else { + h = (CELL)FunctorOfTerm(t); + s = RepAppl(t); + n = ArityOfFunctor(FunctorOfTerm(t)); + ref = AbsAppl(ostart); + *ostart++ = s[0]; + } + if (HR > s && H0 < s) { + // first time, create a new term + q[max].id = max; + q[max].source = t; + q[max].copy = ostart; + q[max].header = s[0]; + q[max].reference = ref; + s[0] = max*sizeof(CELL); + HR += n; + max++; + } else { + Int id = h/sizeof(CELL); + if (q[id].reference == ref) { + q[id].reference = UNFOLD_LOOP(t, tailp); + } + q[i].copy[j] = q[id].reference; + } + return max; } -Term Yap_BreakCycles(Term inp, UInt arity, Term *listp USES_REGS) { - Term t = Deref(inp); +Term Yap_BreakCycles(Term inp, UInt arity, Term *listp USES_REGS) { + + int lvl = push_text_stack(); + + Term t = Deref(inp); + ssize_t qsize = 2048, qlen=0; + cl_connector *q = Malloc(qsize * sizeof(cl_connector)), *q0 = q; + Term *s; + if (IsVarTerm(t) || IsPrimitiveTerm(t)) { return t; - } else if (IsPairTerm(t)) { - return AbsPair(loops_in_complex_term((&t) - 1, &t, listp PASS_REGS)); + } else { + Int i=0; + qlen = cp_link(t, 0, 0, q, qlen, listp); + while (i < qlen) { + arity_t n, j; + if (IsPairTerm( q[i].source )) { + s = RepPair( q[i].source ); + n = 2; + qlen = cp_link(q[i].header, i, 0, q, qlen, listp); + qlen = cp_link(s[1], i, 1, q, qlen, listp); + } else { + s = RepAppl( q[i].source )+1; + n = ArityOfFunctor((Functor)q[i].header); + for (j = 0; jB) -----------------'), - fail. -main :- - main((rational_term_to_tree(X,A,B,[]), - writeln((A->B))), X). -main :- - writeln('------ numbervars(A+B,1,_),\ - writeln((A->B) -----------------'), - fail. -main :- - main(( numbervars(A+B,1,_), - writeln((A->B))), X). -main :- - writeln('------rational_term_to_tree(X,A,B,[]), numbervars(A+B,1,_),\ - writeln((A->B) -----------------'), - fail. -main :- - main((rational_term_to_tree(X,A,B,[]), numbervars(A+B,1,_), - writeln((A->B))), X). -main. - -main(G, X) :- - d(X), - m(G). - - -main2(G, X, L, O) :- - e(X,L), - m(G). - -m( G ) :- - G, - !, - writeln(yes), - end. -m( G ) :- - writeln(no), - end. - -d(X) :- X :=: [_A]. -d(X) :- X :=: [a,_A]. -d(X) :- X :=: [X]. -d(X) :- X :=: [_|X]. -d(X) :- X :=: [_,X]. -d(X) :- X :=: [_,x]. -d(X) :- X :=: [_,x(X)]. -d(X) :- X:=: f(X). -d(X) :- X:=: f(X,X). -d(X) :- X:=: f(_,X). -d(X) :- X:=: f(A,A,X). -d(X) :- X:=: f(A,A,g(A)). -d(X) :- X:=: f(A,g(X,[A|A]),X). -d(X) :- X:=: f(X,[X,X]). -d(X) :- X:=: f(X,[X,g(X)]). -d(X) :- X:=: f(_,X/[X]). -d(X) :- X:=: f(_,A/[A]), A:=: f(X,[X,g(X)]). -d(X) :- X:=: f(_,A/[A]), A:=: f(X,[A,g(X)]). -d(X) :- X:=: f(_,A/[A]), A:=: f(B,[X,g(A)]), B:=:[C|B], C:=:[X]. - -end :- writeln('....'), fail. - -e(X,Y) :- X :=: t(_A,B,_C,D), Y :=: [B,E]. -e(X,Y) :- X :=: t(_A,_B,_C,_D), Y :=: [_,_E]. -e(X,Y) :- X :=: t(A,_B,C,_D), Y :=: [ A,C]. -e(X,Y) :- X :=: t(A,[X,_D]), Y :=: [A,_C,_E]. -e(X,Y) :- X :=: t(A,[X,C]), Y :=: [A,C,_E]. -e(X,Y) :- X :=: t(A,X,_B,[X,C,_D]), Y :=: [A,C,_E]. - -a(no, no). -a(no, no). -a(yes, yes). -a(yes, no). -a(yes, no). -a( no, no). -a(yes, no). -a(yes, yes). -a(yes, yes). -a(yes, no). -a(yes, no). -a( no, no). -a(yes, no). -a(yes, yes). -a(yes, yes). -a(yes, no). -a(yes, no). - -X :-: Y :- writeln(X), fail. X :=: X. +main :- + exec. + +test( cyclic_term(X), [X]). +test( ground(X), [X]). +test( (variables_in_term(X, O), writeln(X=O) ), [X, [], O]). +test( (new_variables_in_term(L,X, O), writeln(X+L=O) ), [X, L, O]). +test( (variables_within_term(L,X, O), writeln(X+L=O) ), [X, L, O]). +test( writeln(X), [X]). +test((rational_term_to_tree(X,A,B,[]), + writeln((A->B))), [X, A, B]). +test(( numbervars(A+B,1,_)), [A, B]). +test((rational_term_to_tree(X,A,B,[]), numbervars(A+B,1,_), + writeln((A->B))), [X,A,B]). + +:- dynamic i/1. +i(0). + +id(I) :- + retract(i(I)), + I1 is I+1, + assert(i(I1)). + +exec :- + test( G, [X|Ps] ), + functors(G, Fs), + format('**** ~w:~n',[Fs]), + d(X, GX), + id(I), + m(I, GX, G, [X|Ps]), + fail. +exec. + +functors((X,Y),(GX -> GY)) :- + !, + functors(X, GX), + functors(Y, GY). +functors(X, GX) :- + functor(X, GX, _). + +m( I, GX, G, Ps ) :- + %trace, + GX, + G, + !, + format( '~d. ~w: ~a.~n', [I, G,yes]). +m( I, GX, G, _Ps ) :- + GX, + format( '~d. ~w: ~a.~n',[I,G,no]). + +d(X, X = [_A] ). +d(X, ( X = [a,_A]) ). +d(X, ( X = [X]) ). +d(X, ( X = [_|X]) ). +d(X, ( X = [_,X]) ). +d(X, ( X = [_,x]) ). +d(X, ( X = [_,x(X)]) ). +d(X, ( X= f(X)) ). +d(X, ( X= f(X,X)) ). +d(X, ( X= f(_,X)) ). +d(X, ( X= f(A,A,X)) ). +d(X, ( X= f(A,A,g(A))) ). +d(X, ( X= f(A,g(X,[A|A]),X)) ). +d(X, ( X= f(X,[X,X])) ). +d(X, ( X= f(X,[X,g(X)])) ). +d(X, ( X= f(_,X/[X])) ). +d(X, ( X= f(_,A/[A]), A= f(X,[X,g(X)])) ). +d(X, ( X= f(_,A/[A]), A= f(X,[A,g(X)])) ). +d(X, ( X= f(_,A/[A]), A= f(B,[X,g(A)]), B=[C|B], C=[X]) ). + +e(X,Y, ( X = t(_A,B,_C,D), Y = [B,E]) ). +e(X,Y, ( X = t(_A,_B,_C,_D), Y = [_,_E]) ). +e(X,Y, ( X = t(A,_B,C,_D), Y = [ A,C]) ). +e(X,Y, ( X = t(A,[X,_D]), Y = [A,_C,_E]) ). +e(X,Y, ( X = t(A,[X,C]), Y = [A,C,_E]) ). +e(X,Y, ( X = t(A,X,_B,[X,C,_D]), Y = [A,C,_E]) ).