From c4a0d5766b8d1b166e9ca965a0346cf19a64315d Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 25 May 2018 12:30:31 +0100 Subject: [PATCH] debug --- C/c_interface.c | 14 +++++++++++-- CXX/yapi.cpp | 11 ++++++---- CXX/yapq.hh | 10 ++++++--- packages/python/swig/yap4py/yapi.py | 32 +++++++++++++++-------------- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index b808c66da..ac7599127 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1738,7 +1738,10 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) { // slot=%d", pe, pe->CodeOfPred->opc, FAILCODE, Deref(ARG1), Deref(ARG2), // LOCAL_CurSlot); dgi->b = dgi->b0 = LCL0 - (CELL *)B; + fprintf(stderr,"PrepGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", + HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); out = Yap_exec_absmi(true, false); + fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", out,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); if (out) { dgi->EndSlot = LOCAL_CurSlot; Yap_StartSlots(); @@ -1758,7 +1761,7 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { BACKUP_MACHINE_REGS(); myB = (choiceptr)(LCL0 - dgi->b); myB0 = (choiceptr)(LCL0 - dgi->b0); - CP = myB->cp_cp; + CP = myB->cp_cp; /* sanity check */ if (B >= myB0) { return false; @@ -1767,6 +1770,8 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { // get rid of garbage choice-points B = myB; } + fprintf(stderr,"RetryGoal: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", + HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); P = FAILCODE; /* make sure we didn't leave live slots when we backtrack */ ASP = (CELL *)B; @@ -1787,9 +1792,11 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { CACHE_REGS choiceptr myB; + fprintf(stderr,"LeaveGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", + successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); BACKUP_MACHINE_REGS(); myB = (choiceptr)(LCL0 - dgi->b0); - if (B >= myB) { + if (B < myB) { /* someone cut us */ return false; } @@ -1810,6 +1817,7 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { #ifdef DEPTH_LIMIT DEPTH = B->cp_depth; #endif /* DEPTH_LIMIT */ + YENV = ENV = B->cp_env; } else { Yap_TrimTrail(); @@ -1835,6 +1843,8 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) { P = dgi->p; LOCAL_CurSlot = dgi->CurSlot; RECOVER_MACHINE_REGS(); + fprintf(stderr,"LeftGoal success=%d: H=%d ENV=%p B=%d TR=%d P=%p CP=%p Slots=%d\n", + successful,HR-H0,LCL0-ENV,LCL0-(CELL*)B,(CELL*)TR-LCL0, P, CP, LOCAL_CurSlot); return TRUE; } diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index de74ba419..06a285308 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -30,7 +30,7 @@ X_API void YAP_UserBackCPredicate(const char *, YAP_UserCPred, YAP_UserCPred, X_API bool do_init_python(void); #endif - + } static void YAPCatchError() @@ -510,7 +510,7 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) { return result; } -bool YAPEngine::mgoal(Term t, Term tmod) { +bool YAPEngine::mgoal(Term t, Term tmod, bool release) { #if YAP_PYTHON // PyThreadState *_save; @@ -548,7 +548,7 @@ bool YAPEngine::mgoal(Term t, Term tmod) { __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "exec "); result = (bool)YAP_EnterGoal(ap, nullptr, &q); - YAP_LeaveGoal(result, &q); + YAP_LeaveGoal(result && !release, &q); // PyEval_RestoreThread(_save); RECOVER_MACHINE_REGS(); return result; @@ -559,7 +559,10 @@ bool YAPEngine::mgoal(Term t, Term tmod) { return false; } } - +/** + * called when a query must be terminated and its state fully recovered, + * @type {[type]} + */ void YAPEngine::release() { BACKUP_MACHINE_REGS(); diff --git a/CXX/yapq.hh b/CXX/yapq.hh index 2279cfadb..62f944454 100644 --- a/CXX/yapq.hh +++ b/CXX/yapq.hh @@ -339,11 +339,15 @@ public: bool call(YAPPredicate ap, YAPTerm ts[]); /// current directory for the engine bool goal(YAPTerm Yt, YAPModule module) { return mgoal(Yt.term(),module.term()); }; - /// current directory for the engine - bool mgoal(Term t, Term tmod); + /// ru1n a goal in a module. + /// + /// By default, memory will only be fully + /// recovered on backtracking. The release option ensures + /// backtracking is called at the very end. + bool mgoal(Term t, Term tmod, bool release= false); /// current directory for the engine - bool goal(Term t) { return mgoal(t, CurrentModule); } + bool goal(Term t, bool release=false) { return mgoal(t, CurrentModule, release); } /// reset Prolog state void reSet(); /// assune that there are no stack pointers, just release memory diff --git a/packages/python/swig/yap4py/yapi.py b/packages/python/swig/yap4py/yapi.py index f94897b88..702490f3b 100644 --- a/packages/python/swig/yap4py/yapi.py +++ b/packages/python/swig/yap4py/yapi.py @@ -30,15 +30,15 @@ class Engine( YAPEngine ): args.setYapPLDIR(yap_lib_path) args.setSavedState(join(yap_lib_path, "startup.yss")) YAPEngine.__init__(self, args) - self.goal(set_prolog_flag('verbose', 'silent')) - self.goal(compile(library('yapi'))) - self.goal(set_prolog_flag('verbose', 'normal')) + self.goal(set_prolog_flag('verbose', 'silent'), recover=True) + self.goal(compile(library('yapi')), recover=True) + self.goal(set_prolog_flag('verbose', 'normal'), release=True) - def run(self, g, m=None): + def run(self, g, m=None, release=False): if m: - self.mgoal(g, m) + self.mgoal(g, m, release=release) else: - self.goal(g) + self.goal(release=release) class EngineArgs( YAPEngineArgs ): @@ -105,7 +105,7 @@ class v(YAPVarTerm,v0): class YAPShell: - + def numbervars( self ): Dict = {} self.engine.goal(show_answer( self, Dict)) @@ -124,6 +124,7 @@ class YAPShell: def query_prolog(self, query): + g = None #import pdb; pdb.set_trace() # # construct a query from a one-line string @@ -134,7 +135,7 @@ class YAPShell: # # vs is the list of variables # you can print it out, the left-side is the variable name, # the right side wraps a handle to a variable - import pdb; pdb.set_trace() + #import pdb; pdb.set_trace() # #pdb.set_trace() # atom match either symbols, or if no symbol exists, sttrings, In this case # variable names should match strings @@ -144,16 +145,17 @@ class YAPShell: # return try: engine = self.engine - engine.ReSet() - bindings = [] + bindings = [] loop = False + if g: + g.release() g = python_query(self, query) - q = Query( engine, g ) - for bind in q: + self.q = Query( engine, g ) + for bind in self.q: bindings += [bind] if loop: continue - if not q.port == "exit": + if not self.q.port == "exit": break s = input("more(;), all(*), no(\\n), python(#) ?").lstrip() if s.startswith(';') or s.startswith('y'): @@ -168,14 +170,14 @@ class YAPShell: continue else: break - if q: + if self.q: self.os = query if bindings: return True,bindings print("No (more) answers") return False, None except Exception as e: - if not q: + if not self.q: return False, None print("Exception") return False, None