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