diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index ec043c20e..4c5cfc381 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -1,15 +1,6 @@ - - - - - - - - - #include "Yap.h" #include "py4yap.h" diff --git a/packages/python/swig/prolog/yapi.yap b/packages/python/swig/prolog/yapi.yap index 167b12773..6d9bb6ef8 100644 --- a/packages/python/swig/prolog/yapi.yap +++ b/packages/python/swig/prolog/yapi.yap @@ -42,6 +42,8 @@ yapi_query( VarNames, Self ) :- Self.bindings := Dict. + + %:- initialization set_preds. set_preds :- @@ -70,51 +72,31 @@ argi(N,I,I1) :- I1 is I+1. python_query( Caller, String ) :- - python_query( Caller, String, _Bindings). + python_query( Caller, String, _Bindings). + +user:user_python_query( Caller, String, Bindings ) :- + python_query( Caller, String, _Bindings). python_query( Caller, String, Bindings ) :- atomic_to_term( String, Goal, VarNames ), query_to_answer( Goal, VarNames, Status, Bindings), - Caller.port := Status, - output(Caller, Bindings). + Caller.q.port := Status, + output(Caller, Bindings). -output( Caller, Bindings ) :- - Caller.answer := {}, - /* % start_low_level_trace, - foldl(ground_dict(answer), Bindings, [], Ts), - term_variables( Ts, Hidden), - foldl(bv, Hidden , 0, _), - */ maplist(into_dict(answer),Bindings), - := print(answer)}, - Caller.answer := answer. - output( _, Bindings ) :- write_query_answer( Bindings ), fail. -output(_Caller, _Bindings). - +output( Caller, Bindings) :- + maplist(into_dict(Caller),Bindings). + bv(V,I,I1) :- atomic_concat(['__',I],V), I1 is I+1. into_dict(D,V0=T) :- - atom(T), - !, - D[V0] := T. -into_dict(D,V0=T) :- - integer(T), -writeln((D[V0]:=T)), -!, - D[V0] := T, - := print(D). -into_dict(D,V0=T) :- - string(T), - !, - D[V0] := T. -into_dict(D,V0=T) :- - python_represents(T1,T), - D[V0] := T1. - + D.q.answer[V0] := T. + + /** * */ diff --git a/packages/python/swig/yap4py/yapi.py b/packages/python/swig/yap4py/yapi.py index 11fcdb724..460d3a30f 100644 --- a/packages/python/swig/yap4py/yapi.py +++ b/packages/python/swig/yap4py/yapi.py @@ -1,7 +1,7 @@ import readline import copy from yap4py.yap import * -from yap4py.systuples import * +from yap4py.systuples import python_query, show_answer, library, prolog_library, v0, compile, namedtuple from os.path import join, dirname import sys @@ -52,7 +52,7 @@ class JupyterEngine( Engine ): pass class EngineArgs( YAPEngineArgs ): - """ Interface to Engine Options class""" + """ Interface to EngneOptions class""" def __init__(self, args=None,**kwargs): super().__init__() @@ -69,6 +69,7 @@ class Query (YAPQuery): super().__init__(g) self.engine = engine self.port = "call" + self.answer = {} def __iter__(self): return self @@ -80,7 +81,7 @@ class Query (YAPQuery): if self.port == "fail" or self.port == "exit": raise StopIteration() if self.next(): - return copy.deepcopy(self.answer) + return True raise StopIteration() def name( name, arity): @@ -125,7 +126,7 @@ class YAPShell: def query_prolog(self, query): g = None - #import pdb; pdb.set_trace() + import pdb; pdb.set_trace() # # construct a query from a one-line string # q is opaque to Python @@ -147,9 +148,11 @@ class YAPShell: engine = self.engine bindings = [] loop = False - q = Query( engine, python_query( engine, query) ) + self.q = Query( engine, python_query( self, query) ) + q = self.q for answer in q: - bindings += [answer] + bindings += [q.answer] + print(q.answer) if q.done(): return bindings if loop: @@ -170,7 +173,7 @@ class YAPShell: if self.q: self.q.close() self.q = None - print("No (more) answers") + print("No (more) answers, found", bindings) return bindings except Exception as e: if not self.q: @@ -182,34 +185,41 @@ class YAPShell: raise def live(self, engine, **kwargs): - loop = True - self.q = None - while loop: - try: - s = input("?- ") - if not s: + try: + loop = True + self.q = None + while loop: + try: + s = input("?- ") + if not s: + continue + else: + self.query_prolog(s) + except SyntaxError as err: + print("Syntax Error error: {0}".format(err)) continue - else: - self.query_prolog(s) - except SyntaxError as err: - print("Syntax Error error: {0}".format(err)) - continue - except EOFError: - return - except RuntimeError as err: - print("YAP Execution Error: {0}".format(err)) - except ValueError: - print("Could not convert data to an integer.") - except: - print("Unexpected error:", sys.exc_info()[0]) - raise - engine.close() + except EOFError: + return + except RuntimeError as err: + print("YAP Execution Error: {0}".format(err)) + except ValueError: + print("Could not convert data to an integer.") + except: + print("Unexpected error:", sys.exc_info()[0]) + raise + engine.close() + except Exception as e: + print("Exception",e) + e.errorNo = 0 + raise + # # initialize engine # engine = yap.YAPEngine(); # engine = yap.YAPEngine(yap.YAPParams()); # def __init__(self, engine, **kwargs): + #import pdb; pdb.set_trace() self.engine = engine self.live(engine) diff --git a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap index 806caf705..7f5686fee 100644 --- a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap +++ b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap @@ -1,4 +1,4 @@ - +u /** * @file jupyter.yap * @@ -30,7 +30,7 @@ :- use_module(library(python)). - :- use_module(library(yapi)). +:- use_module(library(yapi)). :- use_module(library(complete)). :- use_module(library(verify)). @@ -73,7 +73,7 @@ jupyter_cell( _Caller, _, Line , _) :- jupyter_cell(Caller, _, Line, Bindings ) :- Query = Caller, catch( - python_query(Query,Line, Bindings), + user:user_python_query(Query,Line, Bindings), error(A,B), system_error(A,B) ). diff --git a/packages/python/yap_kernel/yap_ipython/yapi.py b/packages/python/yap_kernel/yap_ipython/yapi.py index 5ebf94fa5..fbf2f82e1 100644 --- a/packages/python/yap_kernel/yap_ipython/yapi.py +++ b/packages/python/yap_kernel/yap_ipython/yapi.py @@ -1,6 +1,5 @@ import sys - from typing import List from traitlets import Bool @@ -509,7 +508,7 @@ class YAPRun(InteractiveShell): global engine engine = self.engine self.errors = [] - self.query = None + self.q = None self.os = None self.it = None self.port = "None" @@ -542,51 +541,53 @@ class YAPRun(InteractiveShell): return self.errors def prolog(self, s, result): + # - # construct a self.queryuery from a one-line string - # self.query is opaque to Python + # construct a self.query from a one-line string + # self.q is opaque to Python try: - program,squery,_ ,howmany = self.prolog_cell(s) + program,squery,_ ,howmany = self.prolog_cell(s) # sys.settrace(tracefunc) - if self.query and self.os == (program,squery): + if self.q and self.os == (program,squery): howmany += self.iterations else: - if self.query: - self.query.close() - self.query = None - self.answers = [] - result.result = [] + if self.q: + self.q.close() + self.q = None + self.answers = [] + result.result = [] self.os = (program,squery) self.iterations = 0 - pg = jupyter_query(self.engine,program,squery) - self.query = Query(self.engine, pg) - self.answers = [] - for answer in self.query: - print( answer ) - self.answers += [answer] - print( self.answers) + pg = jupyter_query(self,program,squery) + self.q = Query(self.engine, pg) + while self.q.next(): self.iterations += 1 - - self.os = None - self.query.close() - self.query = None + o = '[ ' + o += str(self.iterations ) + o += ' ' + o += json.dumps(self.q.answer) + o += ' ]\n\n' + sys.stderr.write( o ) + self.answers += [self.q.answer] + if self.q.port == "exit": + break + if self.iterations == howmany: + break + if self.q.port != "answer" and self.iterations == howmany: + self.q.close() + self.q = None if self.answers: - sys.stderr.write('\n'+'[ ' +str(len(self.answers))+' answer(s): ]\n[ ') - print( self.answers ) - result.result = json.dumps(self.answers) - sys.stderr.write(result.result+' ]\n\n') + return self.answers else: - result.result = [] - return result.result + return None except Exception as e: - sys.stderr.write('Exception '+str(e)+'in query '+ str(self.query)+ - '\n '+str( self.bindings)+ '\n') - has_raised = True - result.result = [] - return result.result + sys.stderr.write('Exception '+str(e)+' in query '+ str(self.q)+ + '\n Answers'+ json.dumps( self.answers)+ '\n') + has_raised = True + return result.result def _yrun_cell(self, raw_cell, result, store_history=True, silent=False, @@ -728,10 +729,8 @@ class YAPRun(InteractiveShell): # state = tracer.runfunc(hist # er_query( self, cell ) ) self.shell.last_execution_succeeded = True - result.result = answers except Exception as e: has_raised = True - result.result = [] try: (etype, value, tb) = e traceback.print_exception(etype, value, tb) diff --git a/pl/debug.yap b/pl/debug.yap index 5889ebe90..1550a3bb8 100644 --- a/pl/debug.yap +++ b/pl/debug.yap @@ -477,14 +477,7 @@ be lost. '$trace_goal'(G, M, GoalNumber, H) :- '$undefined'(G, M), !, - '$get_predicate_definition'(M:G, NM:Goal), - ( ( M == NM ; NM == prolog), G == Goal - -> - yap_flag( unknown, Action ), - '$undefp'([M|G], Action ) - ; - '$trace_goal'(Goal, NM, GoalNumber, H) - ). + '$undefp'([M|G], _ ). % meta system '$trace_goal'(G, M, GoalNumber, H) :- '$is_metapredicate'(G, prolog), diff --git a/pl/top.yap b/pl/top.yap index 1f05b24d9..74ee6b29f 100644 --- a/pl/top.yap +++ b/pl/top.yap @@ -741,7 +741,6 @@ write_query_answer( Bindings ) :- '$current_module'(OldModule), repeat, '$system_catch'(dbload_from_stream(Stream, OldModule, db), '$db_load', Error, - user:'$LoopError'(Error, top)), prolog_flag(agc_margin,_,Old), !. '$loop'(Stream,Status) :-