python
This commit is contained in:
@@ -112,7 +112,10 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
|
||||
// char *p = malloc(strlen(s)+1);
|
||||
//strcpy(p, s);
|
||||
PyObject *pobj = PyUnicode_FromString(s);
|
||||
Py_IncRef(pobj);
|
||||
return CHECKNULL(t, pobj);
|
||||
}
|
||||
} break;
|
||||
|
@@ -1,48 +1,98 @@
|
||||
|
||||
import yap
|
||||
import os.path
|
||||
import sys
|
||||
# debugging support.
|
||||
import pdb
|
||||
# import pdb
|
||||
from collections import namedtuple
|
||||
|
||||
from yap import *
|
||||
|
||||
class Engine( ):
|
||||
def __init__(self, args=None):
|
||||
|
||||
def run(self, g, m=None):
|
||||
if m:
|
||||
self.mgoal(g, m)
|
||||
else:
|
||||
self.goal(g)
|
||||
|
||||
def f(self, g):
|
||||
self.E.fun(g)
|
||||
|
||||
|
||||
class EngineArgs( YAPEngineArgs ):
|
||||
""" Interface to Engine Options class"""
|
||||
|
||||
|
||||
class Predicate( YAPPredicate ):
|
||||
""" Interface to Generic Predicate"""
|
||||
|
||||
|
||||
class PrologPredicate( YAPPrologPredicate ):
|
||||
""" Interface to Prolog Predicate"""
|
||||
|
||||
|
||||
|
||||
global engine, handler
|
||||
|
||||
yap_lib_path = os.path.dirname(__file__)
|
||||
|
||||
use_module = namedtuple( 'use_module', 'file')
|
||||
bindvars = namedtuple( 'bindvars', 'list')
|
||||
library = namedtuple( 'library', 'list')
|
||||
v = namedtuple( '_', 'slot')
|
||||
use_module = namedtuple('use_module', 'file')
|
||||
bindvars = namedtuple('bindvars', 'list')
|
||||
library = namedtuple('library', 'list')
|
||||
v = namedtuple( 'v', 'slot')
|
||||
yap_query = namedtuple( 'yap_query', 'query owner')
|
||||
jupyter_query = namedtuple( 'jupyter_query', 'vars dict')
|
||||
python_query = namedtuple( 'python_query', 'vars dict')
|
||||
yapi_query = namedtuple( 'yapi_query', 'vars dict')
|
||||
show_answer = namedtuple( 'show_answer', 'vars dict')
|
||||
set_prolog_flag = namedtuple('set_prolog_flag', 'flag new_value')
|
||||
|
||||
def v():
|
||||
return yap.YAPVarTerm()
|
||||
|
||||
def numbervars( engine, l ):
|
||||
rc = engine.fun(bindvars(l))
|
||||
def numbervars( q ):
|
||||
Dict = {}
|
||||
if True:
|
||||
engine.goal(show_answer( q.namedVars(), Dict))
|
||||
return Dict
|
||||
rc = q.namedVarsVector()
|
||||
q.r = q.goal().numbervars()
|
||||
print( rc )
|
||||
o = []
|
||||
for i in rc:
|
||||
if i[0] == "=":
|
||||
o = o + [i[1]]
|
||||
else:
|
||||
o = o +[i]
|
||||
if len(i) == 2:
|
||||
do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
o += do
|
||||
print(do)
|
||||
else:
|
||||
do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
o += do
|
||||
print(do)
|
||||
return o
|
||||
|
||||
def answer(q):
|
||||
try:
|
||||
v = q.next()
|
||||
if v:
|
||||
print( bindings )
|
||||
return v
|
||||
except Exception as e:
|
||||
print(e.args[1])
|
||||
return False
|
||||
|
||||
def query_prolog(engine, s):
|
||||
|
||||
def answer(q):
|
||||
try:
|
||||
return q.next()
|
||||
except Exception as e:
|
||||
print(e.args[1])
|
||||
return False
|
||||
|
||||
import pdb; pdb.set_trace()
|
||||
#
|
||||
# construct a query from a one-line string
|
||||
# q is opaque to Python
|
||||
q = engine.query(s)
|
||||
bindings = {}
|
||||
q = engine.query(python_query(s, bindings))
|
||||
# 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
|
||||
# pdb.set_trace()
|
||||
vs = q.namedVars()
|
||||
#pdb.set_trace()
|
||||
# #pdb.set_trace()
|
||||
# atom match either symbols, or if no symbol exists, sttrings, In this case
|
||||
# variable names should match strings
|
||||
#for eq in vs:
|
||||
@@ -52,28 +102,11 @@ def query_prolog(engine, s):
|
||||
ask = True
|
||||
# launch the query
|
||||
while answer(q):
|
||||
# this new vs should contain bindings to vars
|
||||
vs= q.namedVars()
|
||||
if vs != []:
|
||||
gs = numbervars( engine, vs)
|
||||
i=0
|
||||
# iterate
|
||||
for eq in gs:
|
||||
name = eq[0]
|
||||
binding = eq[1]
|
||||
# this is tricky, we're going to bind the variables in the term so thay we can
|
||||
# output X=Y. The Python way is to use dictionares.
|
||||
#Instead, we use the T function to tranform the Python term back to Prolog
|
||||
if name != binding:
|
||||
print(name + " = " + str(binding))
|
||||
#ok, that was Prolog code
|
||||
else:
|
||||
print("yes")
|
||||
# deterministic = one solution
|
||||
if q.deterministic():
|
||||
# done
|
||||
q.close()
|
||||
return
|
||||
return True, True
|
||||
if ask:
|
||||
s = input("more(;), all(*), no(\\n), python(#) ?").lstrip()
|
||||
if s.startswith(';') or s.startswith('y'):
|
||||
@@ -92,15 +125,7 @@ def query_prolog(engine, s):
|
||||
q.close()
|
||||
return
|
||||
|
||||
|
||||
def live():
|
||||
yap_lib_path = os.path.dirname(__file__)
|
||||
args = yap.YAPEngineArgs()
|
||||
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||
args.setYapLibDir(yap_lib_path)
|
||||
#args.setYapPrologBootFile(os.path.join(yap_lib_path."startup.yss"))
|
||||
engine = yap.YAPEngine(args)
|
||||
engine.goal( use_module(library('yapi') ) )
|
||||
def live(**kwargs):
|
||||
loop = True
|
||||
while loop:
|
||||
try:
|
||||
@@ -121,6 +146,18 @@ def live():
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
engine.close()
|
||||
|
||||
def boot_yap(**kwargs):
|
||||
args = EngineArgs()
|
||||
yap_lib_path = os.path.dirname(__file__)
|
||||
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||
args.setYapLibDir(yap_lib_path)
|
||||
args.setSavedState(os.path.join(yap_lib_path,"startup.yss"))
|
||||
engine = YAPEngine(args)
|
||||
engine.goal( set_prolog_flag('verbose', 'silent' ) )
|
||||
engine.goal( use_module(library('yapi') ) )
|
||||
retun engine
|
||||
|
||||
#
|
||||
# initialize engine
|
||||
# engine = yap.YAPEngine();
|
||||
@@ -129,4 +166,6 @@ def live():
|
||||
#
|
||||
|
||||
if __name__ == "__main__":
|
||||
live()
|
||||
engine = boot_yap()
|
||||
handler = numbervars
|
||||
live()
|
||||
|
@@ -1,4 +1,4 @@
|
||||
%% @file yapi.yap
|
||||
drye%% @file yapi.yap
|
||||
%% @brief support yap shell
|
||||
%%
|
||||
:- module(yapi, [python_query/2,
|
||||
@@ -75,7 +75,7 @@ bind_qv(V=V0, V1 = V01, Vs, Vs, Vs1-RB, Vs1-RB) :-
|
||||
!,
|
||||
'$VAR'(V) = V0,
|
||||
V1 = V01.
|
||||
% atom_string(V1, V01).
|
||||
% atom_string(V1, V01).
|
||||
bind_qv(V='$VAR'(Vi), V1=S1, Vs, [V='$VAR'(Vi)|Vs], D0-RB, D-RB) :- !,
|
||||
add2dict(D0, V1:S1, D).
|
||||
bind_qv(V=S, V1=S1, Vs, [V=S|Vs], D0-RB0, D-RB0) :-
|
||||
@@ -113,6 +113,3 @@ output([V=B], S) :-
|
||||
output([V=B|_Ns], S) :-
|
||||
format( S, '~a = ~q.~n', [V, B]),
|
||||
fail.
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user