This commit is contained in:
Vitor Santos Costa
2017-05-19 09:56:37 +01:00
parent 3802a588f9
commit 18af47bdde
13 changed files with 484 additions and 431 deletions

View File

@@ -70,9 +70,9 @@ add_custom_target( YAP4PY ALL
COMMAND ${CMAKE_COMMAND} -E copy ${pl_library} ${PROLOG_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog
COMMAND ${CMAKE_COMMAND} -E copy ${pl_boot_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/pl
COMMAND ${CMAKE_COMMAND} -E copy ${pl_os_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py sdist bdist_wheel
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS STARTUP ${dlls} ${PYTHON_SOURCES} ${PROLOG_SOURCES} setup.py ${SWIG_MODULE_Py2YAP_REAL_NAME} )
DEPENDS STARTUP ${dlls} ${PYTHON_SOURCES} ${PROLOG_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/setup.py ${SWIG_MODULE_Py2YAP_REAL_NAME} )
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --no-index -f dist yap4py
@@ -95,7 +95,7 @@ DEPENDS STARTUP ${dlls} ${PYTHON_SOURCES} ${PROLOG_SOURCES} setup.py ${SWIG_MOD
# generate .i from doxygen .xml
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doxy2swig.py -n
${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
${CMAKE_BINARY_DIR}/doc/xm11l/ftdi_8c.xml
${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
DEPENDS ${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
)

View File

@@ -3,7 +3,7 @@ import yap
import os.path
import sys
# debugging support.
import pdb
# import pdb
from collections import namedtuple
yap_lib_path = os.path.dirname(__file__)
@@ -12,6 +12,7 @@ use_module = namedtuple( 'use_module', 'file')
bindvars = namedtuple( 'bindvars', 'list')
library = namedtuple( 'library', 'list')
v = namedtuple( '_', 'slot')
yap_query = namedtuple( 'yap_query', 'query owner')
def numbervars( engine, l ):
@@ -24,8 +25,6 @@ def numbervars( engine, l ):
o = o +[i]
return o
def query_prolog(engine, s):
def answer(q):
try:
return q.next()
@@ -36,44 +35,29 @@ def query_prolog(engine, s):
#
# construct a query from a one-line string
# q is opaque to Python
q = engine.query(s)
if g0:
q = g0
else:
q = engine.run_query(s)
# 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:
# if not isinstance(eq[0],str):
# print( "Error: Variable Name matches a Python Symbol")
# return
ask = True
# 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'):

View File

@@ -1,53 +1,76 @@
%% @file yapi.yap
%% @brief support yap shell
%%
:- module(yapi, [bindvars/2]).
:- module(yapi, [query/3]).
:- use_module( library(lists) ).
:- use_module( library(maplist) ).
:- use_module( library(rbtrees) ).
bindvars( [], [] ) :- !.
bindvars( L, NL ) :-
rb_new(T),
% trace,
foldl2( bind, L, NL, T, _ , 0, _),
term_variables(NL, Vs),
foldl( bind_new, Vs, 0, _).
%% @pred yap_query(0:Goal, + VarList, +OutStream, - Dictionary)
%% @pred yap_query(0:Goal, + VarList, - Dictionary)
%%
%% dictionary, Examples
%%
%%
yap_query( Goal, VarNames, Stream, Dictionary) :-
(
call(Goal)
*->
constraints(VarNames, Goal, Stream, Dictionary)
).
prolog:yap_query( Goal, VarNames, Dictionary) :-
yap_query( Goal, VarNames, user_output, Dictionary).
constraints(QVs, Goal, Stream, {Dict}) :-
!,
term_variables(Goal, IVs),
foldl(enumerate, IVs, 0, _Ns),
out(QVs, Stream, Dict).
constraints(_, _, {}) :-
format(' yes.~n', [] ).
bind_qv(V=V0, Vs, Vs) :- var(V0), !, V0='$VAR'(V).
bind_qv(V=V, Vs, Vs) :- !.
bind_qv(V=S, Vs, [V=S|Vs]).
enumerate('$VAR'(A), I, I1) :-
enum(I, Chars),
atom_codes(A,[0'_|Chars]),
I1 is I + 1.
enum(I, [C]) :-
I < 26,
!, C is "A" + I.
enum(I, [C|Cs]) :-
J is I//26,
K is I mod 26,
C is "A" +K,
enum(J, Cs).
out(Bs, S, _Dict) :-
output(Bs, S),
fail.
out(Bs, _S, Dict) :-
bvs(Bs, Dict).
v2py(v(I0) = _V, I0, I) :-
!,
I is I0+1.
v2py(v(I0) = v(I0), I0, I) :-
I is I0+1.
output([V=B], S) :-
format(S, 'a = ~q~n', [V, B]).
output([V=B|Ns], S) :-
format( S, 'a = ~q.~n', [V, B]),
output( Ns, S).
bind(t(_,t(X,Y)), Z, T0, T, N1, N2) :-
!,
bind(X=Y, Z, T0, T, N1, N2).
bind(tuple(_,tuple(X,Y)), Z, T0, T, N1, N2) :-
!,
bind(X=Y, Z, T0, T, N1, N2).
bind(X=Y, X=X, T0, T, N, N) :-
var(Y),
!,
rb_update(T0, Y, X, T).
bind(X = G, X = G, T, T, N0, N0) :-
ground(G),
!.
bind(X = C, X = NC, T, NT, N0, NF) :-
C =.. [N|L],
foldl2(newb, L, NL, T, NT, N0, NF),
NC =.. [N|NL].
newb(Y, X, T, T, N, N) :-
var(Y),
rb_lookup(Y, X, T),
!.
newb(Y, X, T, TN, N, NF) :-
var(Y),
!,
rb_insert(Y, T, X, TN),
NF is N+1,
atomic_concat('_',N,X).
newb(Y, Y, T, T, N, N) :-
ground(Y),
!.
newb(Y, X, T, NT, N0, NF) :-
Y =.. [N|L],
foldl2(newb, L, NL, T, NT, N0, NF),
X =.. [N|NL].
bvs([V=B],{V:B}) :-
!.
bvs([V=B|Ns], (V:B,N)) :-
output( Ns, N).
:- start_low_level_trace.

View File

@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
"""YAP Stuff for Main IPython class."""
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
from __future__ import absolute_import, print_function
@@ -92,7 +92,7 @@ use_module = namedtuple('use_module', 'file')
bindvars = namedtuple('bindvars', 'list')
library = namedtuple('library', 'list')
v = namedtuple('_', 'slot')
load_fieos = namedtuple('load_files', 'file ofile args')
load_files = namedtuple('load_files', 'file ofile args')
class YAPInteraction:
@@ -129,6 +129,8 @@ class YAPInteraction:
def numbervars(self, l):
return self.yapeng.fun(bindvars(l))
def run_cell(self, s, store_history=True, silent=False,
shell_futures=True):
"""Run a complete IPython cell.
@@ -148,8 +150,9 @@ class YAPInteraction:
and logging. silent=True forces store_history=False.
shell_futures : bool
If True, the code will share future statements with the interactive
shell. It will both be affected by previous __future__ imports, and
any __future__ imports in the code will affect the shell. If False,
shell. It will both be affected by previous
__future__ imports, and any __future__ imports in the code
will affect the shell. If False,
__future__ imports are not shared in either direction.
Returns
@@ -158,9 +161,7 @@ class YAPInteraction:
"""
result = ExecutionResult()
if store_history:
result.execution_count = self.shell.execution_count
result.execution_count = self.shell.execution_count
def error_before_exec(value):
result.error_before_exec = value
@@ -168,32 +169,28 @@ class YAPInteraction:
return result
# inspect for ?? in the text
st = s.strip('\n\j\r\t ')
if (st):
(p0, pm, pf) = st.rpartition('??')
if pm == '??':
if pf.isdigit(p):
maxits = int(pf)*2
s = p0
elif pf.isspace(p):
maxits = 1
s = p0
else:
s = st
maxits = 2
else:
# business as usual
s = st
maxits = 2
elif st == '':
# next. please
maxis = 2
self.qclose()
# print(st)
#
maxits = 2
s = s.strip('\n\j\r\t ')
if not self.q or s:
(self.q,out) = self.top_level(s, out)
else:
out = q.next_answer()
if self.q:
st = s.strip('\n\j\r\t ')
if st and st == '*':
maxits = 1
elif st and st.isdigit():
maxits = int(st)*2
elif st and st != ';':
self.closeq()
if not self.q:
try:
if s:
self.q = self.yapeng.query(s)
self.q = self.yapeng.query(ya[q.__hash__])
self.vs = self.q.namedVarsVector()
else:
return
except SyntaxError:
@@ -204,37 +201,11 @@ class YAPInteraction:
# if not silent:
# self.shell..logger.log(cell, s)
has_raised = False
self.run = True
try:
while self.run and maxits != 0:
# f = io.StringIO()
# with redirect_stdout(f):
self.run = self.q.next()
# print('{0}'.format(f.getvalue()))
# Execute the user code
if self.run:
myvs = self.numbervars(self.q.namedVars())
if myvs:
for eq in myvs:
name = eq[0]
binding = eq[1]
if name != binding:
print(name + " = " + str(binding))
else:
print("yes")
if self.q.deterministic():
self.closeq()
self.run = False
self.q = None
else:
maxits -= 2
else:
print("No (more) answers")
self.closeq()
self.run = False
while (maxits != 0):
self.do_loop(maxits, gs)
except Exception:
result.error_in_exec = sys.exc_info()[1]
# self.showtraceback()
has_raised = True
self.closeq()
@@ -244,8 +215,8 @@ class YAPInteraction:
# ExecutionResult
# self.displayhook.exec_result = None
self.events.trigger('post_execute')
if not silent:
self.events.trigger('post_self.run_cell')
# self.events.trigger('post_execute')
# if not silent:
# self.events.trigger('post_self.run_cell')
return result

View File

@@ -57,6 +57,7 @@ class YAPKernel(KernelBase):
self.shell_handlers[msg_type] = getattr(self.comm_manager, msg_type)
self.engine = YAPInteraction(self)
self.shell._last_traceback = None
self.shell.run_cell = self.engine.run_cell
help_links = List([