This commit is contained in:
Vitor Santos Costa 2019-02-06 00:08:15 +00:00
parent e5945b8e8b
commit 4092003cd6
4 changed files with 108 additions and 76 deletions

View File

@ -28,6 +28,8 @@
#include "YapHeap.h" #include "YapHeap.h"
#define debug_pop_text_stack(l) [ if (to_visit != to_visit0) printf("%d\n",__LINE__); pop_text_stack(l) }
#include "attvar.h" #include "attvar.h"
#include "yapio.h" #include "yapio.h"
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
@ -155,8 +157,9 @@ typedef struct non_single_struct_t {
++pt0; \ ++pt0; \
ptd0 = pt0; \ ptd0 = pt0; \
d0 = *ptd0; \ d0 = *ptd0; \
list_loop: \ list_loop: \
deref_head(d0, var_in_term_unk); \ fprintf(stderr,"%ld at %s\n", to_visit-to_visit0, __FUNCTION__);\
deref_head(d0, var_in_term_unk); \
var_in_term_nvar : { \ var_in_term_nvar : { \
if (IsPairTerm(d0)) { \ if (IsPairTerm(d0)) { \
if (to_visit + 32 >= to_visit_max) { \ if (to_visit + 32 >= to_visit_max) { \
@ -256,19 +259,19 @@ typedef struct non_single_struct_t {
CELL *ptd0 = to_visit->ptd0; \ CELL *ptd0 = to_visit->ptd0; \
*ptd0 = to_visit->d0; \ *ptd0 = to_visit->d0; \
} \ } \
pop_text_stack(lvl); \ pop_text_stack(lvl); fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);\
return true; \ return true; \
} }
#define CYC_APPL \ #define CYC_APPL \
if (IsAtomTerm((CELL)f)) { \ if (IsAtomTerm((CELL)f)) { \
while (to_visit > to_visit0) { \ while (to_visit > to_visit0) { \
to_visit--; \ to_visit--; \
CELL *ptd0 = to_visit->ptd0; \ CELL *ptd0 = to_visit->ptd0; \
*ptd0 = to_visit->d0; \ *ptd0 = to_visit->d0; \
} \ } \
pop_text_stack(lvl); \ pop_text_stack(lvl); fprintf(stderr,"<%d at %s\n", to_visit-to_visit0, __FUNCTION__);\
return true; \ return true; \
} }
/** /**
@ -697,6 +700,7 @@ static Term attvars_in_complex_term(register CELL *pt0, register CELL *pt0_end,
clean_tr(TR0 PASS_REGS); clean_tr(TR0 PASS_REGS);
pop_text_stack(lvl); pop_text_stack(lvl);
fprintf(stderr,"<%ld at %s\n", to_visit-to_visit0, __FUNCTION__);
if (HR != InitialH) { if (HR != InitialH) {
/* close the list */ /* close the list */
Term t2 = Deref(inp); Term t2 = Deref(inp);
@ -1000,7 +1004,7 @@ static Term bind_vars_in_complex_term(CELL *pt0, CELL *pt0_end,
if (to_visit > to_visit0) { if (to_visit > to_visit0) {
to_visit--; to_visit--;
pt0 = to_visit->ptd0; pt0 = to_visit->ptd0;
*pt0 = to_visit0->d0; *pt0 = to_visit->d0;
goto list_loop; goto list_loop;
} }
@ -1311,7 +1315,7 @@ static Int largest_numbervar(USES_REGS1) {
return Yap_unify(MaxNumberedVar(Deref(ARG1), 2 PASS_REGS), ARG2); return Yap_unify(MaxNumberedVar(Deref(ARG1), 2 PASS_REGS), ARG2);
} }
static Term BREAK_LOOP(int ddep) { static Term BREAK_LOOP(Int ddep) {
Term t0 = MkIntegerTerm(ddep); Term t0 = MkIntegerTerm(ddep);
return Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("@^"), 1), 1, &t0); return Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("@^"), 1), 1, &t0);
} }
@ -1326,28 +1330,26 @@ static Term UNFOLD_LOOP(Term t, Term *b) {
return o; return o;
} }
static int loops_in_complex_term(CELL *pt0, CELL *pt0_end, static Term *loops_in_complex_term(CELL *pt0, CELL *pt0_end,
Term *listp, Term *listp USES_REGS) {
Term tail USES_REGS) {
int lvl = push_text_stack(); int lvl = push_text_stack();
struct non_single_struct_t *to_visit = Malloc( struct non_single_struct_t *to_visit = Malloc(
1024 * sizeof(struct non_single_struct_t)), 1024 * sizeof(struct non_single_struct_t)),
*to_visit0 = to_visit, *to_visit0 = to_visit,
*to_visit_max = to_visit + 1024; *to_visit_max = to_visit + 1024;
CELL *ptd0;
CELL *ptf = HR; CELL *ptf0 = HR,
CELL *ptd0; *ptf = HR;
if (listp) restart:
*listp = tail;
restart:
while (pt0 < pt0_end) { while (pt0 < pt0_end) {
CELL d0; CELL d0;
++pt0; ++pt0;
ptd0 = pt0; ptd0 = pt0;
d0 = *ptd0; d0 = *ptd0;
list_loop: list_loop:
deref_head(d0, vars_in_term_unk); fprintf(stderr,"%d at %s\n", to_visit-to_visit0, __FUNCTION__);
deref_head(d0, vars_in_term_unk);
vars_in_term_nvar: vars_in_term_nvar:
if (IsPairTerm(d0)) { if (IsPairTerm(d0)) {
if (to_visit + 32 >= to_visit_max) { if (to_visit + 32 >= to_visit_max) {
@ -1361,8 +1363,7 @@ restart:
struct non_single_struct_t *v0 = struct non_single_struct_t *v0 =
(struct non_single_struct_t *)AtomOfTerm(d0); (struct non_single_struct_t *)AtomOfTerm(d0);
if (listp) { if (listp) {
*ptf = UNFOLD_LOOP(AbsPair(v0->ptf-1), listp); *ptf++ = UNFOLD_LOOP(AbsPair(v0->ptf-1), listp);
ptf++;
} else { } else {
*ptf++ = BREAK_LOOP(to_visit - v0); *ptf++ = BREAK_LOOP(to_visit - v0);
} }
@ -1395,8 +1396,7 @@ restart:
if (IsAtomTerm((CELL)f)) { if (IsAtomTerm((CELL)f)) {
struct non_single_struct_t *v0 = (struct non_single_struct_t *)AtomOfTerm(*ptd0); struct non_single_struct_t *v0 = (struct non_single_struct_t *)AtomOfTerm(*ptd0);
if (listp) { if (listp) {
*ptf = UNFOLD_LOOP(AbsAppl(v0->ptf-1), listp); *ptf++ = UNFOLD_LOOP(AbsAppl(v0->ptf-1), listp);
ptf++;
} else { } else {
*ptf++ = BREAK_LOOP(to_visit - v0); *ptf++ = BREAK_LOOP(to_visit - v0);
} }
@ -1425,7 +1425,7 @@ restart:
goto restart; goto restart;
derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar); derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar);
*ptf++ = (CELL)ptd0; *ptf++ = *ptd0;
goto restart; goto restart;
} }
/* Do we still have compound terms to visit */ /* Do we still have compound terms to visit */
@ -1439,28 +1439,24 @@ restart:
*ptd0 = to_visit->d0; *ptd0 = to_visit->d0;
goto restart; goto restart;
} }
fprintf(stderr,"exit %d at %s\n", to_visit-to_visit0, __FUNCTION__);
pop_text_stack(lvl); pop_text_stack(lvl);
return 0; return ptf0;
def_aux_overflow(); def_aux_overflow();
} }
Term Yap_BreakCycles(Term inp, UInt arity, Term *listp, Term tail USES_REGS) { Term Yap_BreakCycles(Term inp, UInt arity, Term *listp USES_REGS) {
Term t = Deref(inp); Term t = Deref(inp);
if (IsVarTerm(t) || IsPrimitiveTerm(t)) { if (IsVarTerm(t) || IsPrimitiveTerm(t)) {
return t; return t;
} else { } else if (IsPairTerm(t)) {
Int res; return AbsPair(loops_in_complex_term((&t) - 1, &t, listp PASS_REGS));
CELL *op = HR;
res = loops_in_complex_term((&t) - 1, &t, listp, tail PASS_REGS);
if (res < 0)
return -1;
if (IsPairTerm(t))
return AbsPair(op);
else
return AbsAppl(op);
} }
return AbsAppl(loops_in_complex_term((&t) - 1, &t, listp PASS_REGS));
} }
/** @pred rational_term_to_tree(? _TI_,- _TF_, ?SubTerms, ?MoreSubterms) /** @pred rational_term_to_tree(? _TI_,- _TF_, ?SubTerms, ?MoreSubterms)
@ -1476,11 +1472,11 @@ Term Yap_BreakCycles(Term inp, UInt arity, Term *listp, Term tail USES_REGS) {
*/ */
static Int p_break_rational(USES_REGS1) { static Int p_break_rational(USES_REGS1) {
Term t = (ARG1); Term t = (ARG1);
Term l = Deref(ARG4), k; Term l = Deref(ARG4);
if (IsVarTerm(l)) if (IsVarTerm(l))
Yap_unify(l, MkVarTerm()); Yap_unify(l, MkVarTerm());
return Yap_unify(Yap_BreakCycles(t, 4, &k, l PASS_REGS), ARG2) && return Yap_unify(Yap_BreakCycles(t, 4, &l PASS_REGS), ARG2) &&
Yap_unify(k, ARG3); Yap_unify(l, ARG3) ;
} }
void Yap_InitTermCPreds(void) { void Yap_InitTermCPreds(void) {

View File

@ -70,11 +70,11 @@ typedef struct rewind_term {
typedef struct write_globs { typedef struct write_globs {
StreamDesc *stream; StreamDesc *stream;
int Quote_illegal, Ignore_ops, Handle_vars, Use_portray, Portray_delays; bool Quote_illegal, Ignore_ops, Handle_vars, Use_portray, Portray_delays;
int Keep_terms; bool Keep_terms;
int Write_Loops; bool Write_Loops;
int Write_strings; bool Write_strings;
int last_atom_minus; UInt last_atom_minus;
UInt MaxDepth, MaxArgs; UInt MaxDepth, MaxArgs;
wtype lw; wtype lw;
} wglbs; } wglbs;
@ -1089,8 +1089,21 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags,
wglb.stream = mywrite; wglb.stream = mywrite;
wglb.Ignore_ops = flags & Ignore_ops_f; wglb.Ignore_ops = flags & Ignore_ops_f;
wglb.Write_strings = flags & BackQuote_String_f; wglb.Write_strings = flags & BackQuote_String_f;
wglb.Use_portray = false;
wglb.Handle_vars = true;
wglb.Use_portray = false;
wglb.Portray_delays = false;
wglb.Keep_terms = false;
wglb.Write_Loops = false;
wglb.Write_strings = false;
wglb.Quote_illegal = false;
wglb.Ignore_ops = false;
wglb.MaxDepth = false;
wglb.MaxArgs = false;
wglb.lw = separator;
if ((flags & Handle_cyclics_f) && Yap_IsCyclicTerm(t) ){ if ((flags & Handle_cyclics_f) && Yap_IsCyclicTerm(t) ){
t = Yap_BreakCycles(t, 3, NULL, TermNil PASS_REGS); t = Yap_BreakCycles(t, 3, NULL PASS_REGS);
} }
/* protect slots for portray */ /* protect slots for portray */
writeTerm(t, priority, 1, false, &wglb, &rwt); writeTerm(t, priority, 1, false, &wglb, &rwt);

View File

@ -446,7 +446,7 @@ extern bool Yap_Exists(const char *f);
/* terms.c */ /* terms.c */
extern bool Yap_IsCyclicTerm(Term inp USES_REGS); extern bool Yap_IsCyclicTerm(Term inp USES_REGS);
extern Term Yap_BreakCycles(Term inp, UInt arity, Term *listp, Term tail USES_REGS); extern Term Yap_BreakCycles(Term inp, UInt arity, Term *listp USES_REGS);
extern void Yap_InitTermCPreds(void); extern void Yap_InitTermCPreds(void);
/* threads.c */ /* threads.c */

View File

@ -2,21 +2,24 @@
:- linitialization(main). :- linitialization(main).
:- op(700, xfx, :=: ).
main :- main :-
main( cyclic_term(X), X). main( cyclic_term(X), X).
main :- main :-
writeln('-----------------------'), writeln('--- cyclic_term/1 --------------------'),
fail. fail.
main :- main :-
main( ground(X), X). main( ground(X), X).
main :- main :-
writeln('-----------------------'), writeln('--- ground/1 ------------------'),
fail. fail.
main :- main :-
main2( (terms:variables_in_term(X, O), writeln(X=O) ), X, L, O). main2( (terms:variables_in_term(X, O), writeln(X=O) ), X, L, O).
main :- main :-
writeln('-----------------------'), writeln('--------variables_in_term/2, writeln/1 ---------------'),
fail. fail.
main :- main :-
main2( (terms:new_variables_in_term(L,X, O), writeln(X+L=O) ), X, L, O). main2( (terms:new_variables_in_term(L,X, O), writeln(X+L=O) ), X, L, O).
main :- main :-
@ -30,7 +33,22 @@ main :-
main :- main :-
main( writeln(X), X). main( writeln(X), X).
main :- main :-
writeln('-----------------------'), writeln('------rational_term_to_tree(X,A,B,[]),\
writeln((A->B) -----------------'),
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. fail.
main :- main :-
main((rational_term_to_tree(X,A,B,[]), numbervars(A+B,1,_), main((rational_term_to_tree(X,A,B,[]), numbervars(A+B,1,_),
@ -55,35 +73,36 @@ m( G ) :-
writeln(no), writeln(no),
end. end.
d(X) :- X = [_A]. d(X) :- X :=: [_A].
d(X) :- X = [a,_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]. d(X) :- X :=: [_,X].
d(X) :- X = [_,x]. d(X) :- X :=: [_,x].
d(X) :- X = [_,x(X)]. d(X) :- X :=: [_,x(X)].
d(X) :- X= f(X). d(X) :- X:=: f(X).
d(X) :- X= f(X,X). d(X) :- X:=: f(X,X).
d(X) :- X= f(_,X). d(X) :- X:=: f(_,X).
d(X) :- X= f(A,A,X). d(X) :- X:=: f(A,A,X).
d(X) :- X= f(A,A,g(A)). d(X) :- X:=: f(A,A,g(A)).
d(X) :- X= f(A,g(X,[A|A]),X). d(X) :- X:=: f(A,g(X,[A|A]),X).
d(X) :- X= f(X,[X,X]). d(X) :- X:=: f(X,[X,X]).
d(X) :- X= f(X,[X,g(X)]). d(X) :- X:=: f(X,[X,g(X)]).
d(X) :- X= f(_,X/[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,[X,g(X)]).
d(X) :- X= f(_,A/[A]), A= f(X,[A,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]. d(X) :- X:=: f(_,A/[A]), A:=: f(B,[X,g(A)]), B:=:[C|B], C:=:[X].
end :- writeln('....'), fail. 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 :=: [B,E].
e(X,Y) :- X = t(A,_B,C,_D), Y = [ A,C]. e(X,Y) :- X :=: t(_A,_B,_C,_D), Y :=: [_,_E].
e(X,Y) :- X = t(A,[X,_D]), Y = [A,_C,_E]. e(X,Y) :- X :=: t(A,_B,C,_D), Y :=: [ A,C].
e(X,Y) :- X = t(A,[X,C]), Y = [A,C,_E]. e(X,Y) :- X :=: t(A,[X,_D]), Y :=: [A,_C,_E].
e(X,Y) :- X = t(A,X,_B,[X,C,_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(no, no). a(no, no).
@ -102,3 +121,7 @@ a(yes, yes).
a(yes, yes). a(yes, yes).
a(yes, no). a(yes, no).
a(yes, no). a(yes, no).
X :-: Y :- writeln(X), fail.
X :=: X.