debugging

This commit is contained in:
Vitor Santos Costa 2018-05-28 09:31:59 +01:00
parent b59af58616
commit 2415a2e58c
9 changed files with 178 additions and 45 deletions

View File

@ -1741,7 +1741,7 @@ X_API bool YAP_EnterGoal(YAP_PredEntryPtr ape, CELL *ptr, YAP_dogoalinfo *dgi) {
//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);
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();
@ -1792,8 +1792,8 @@ 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);
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) {
@ -1827,9 +1827,6 @@ X_API bool YAP_LeaveGoal(bool successful, YAP_dogoalinfo *dgi) {
DEPTH = ENV[E_DEPTH];
#endif
/* make sure we prune C-choicepoints */
if (POP_CHOICE_POINT(B->cp_b)) {
POP_EXECUTE();
}
ENV = (CELL *)(ENV[E_E]);
/* ASP should be set to the top of the local stack when we
did the call */
@ -1843,8 +1840,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);
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;
}

View File

@ -517,6 +517,8 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
#if YAP_PYTHON
// PyThreadState *_save;
std::cerr << "mgoal " << YAPTerm(t).text() << "\n";
// _save = PyEval_SaveThread();
#endif
CACHE_REGS
@ -715,6 +717,8 @@ RECOVER_MACHINE_REGS();
bool YAPQuery::next() {
CACHE_REGS
bool result = false;
std::cerr << "next " << goal.text() << "\n";
sigjmp_buf buf, *oldp = LOCAL_RestartEnv;
e = nullptr;
BACKUP_MACHINE_REGS();

View File

@ -11,6 +11,7 @@ set (LIBRARY_PL
charsio.yap
clauses.yap
coinduction.yap
completer.yap
dbqueues.yap
dbusage.yap
dgraphs.yap

123
library/completer.yap Normal file
View File

@ -0,0 +1,123 @@
/**
x @file complete.yap
*
* @brief Prolog completer.
*/
:- module( completer,
[
completions/2
]).
:- use_module(library(lists)).
:- use_module(library(maplist)).
:- use_module(library(python)).
%% completions( +Text, +PythonCell )
%
% Tries to complete the current text. The list with the set of completions
% is stored in the
% `matches` field of the python object.
%
completions(S, Self) :-
open_mem_read_stream(S, St),
scan_to_list(St, Tokens),
close(St),
reverse(Tokens, RTokens),
strip_final_tokens(RTokens, MyTokens),
setof( Completion, complete(MyTokens, Completion), Cs),
Self.matches := Cs.
strip_final_tokens(['EOT'|Ts], Ts) :- !.
strip_final_tokens( Ts, Ts ).
complete([E,l,C,l,A|More],
isconsult(A),
%B = l,
library(C,Lib),
%D=l,
E=atom(Prefix),
\+ arg( Rest ),
check_library( Prefix, Lib, C).
complete([E,l,C,l,-,'['|More],
isconsult(A),
%B = l,
library(C,Lib),
%D=l,
E=atom(Prefix),
\+ arg( Rest ),
check_library( Prefix, Lib, C).
complete([C,l,A|More],
isconsult(A),
%B = l,
C=atom(Prefix),
\+ arg( Rest ),
file_or_library( Prefix, C).
complete([C,l,-,'['|More],
isconsult(A),
%B = l,
C=atom(Prefix),
\+ arg( Rest ),
file_or_library( Prefix, C).
complete( [atom(F)|Rest], C) :-
\+ arg( Rest ),
predicate( F, Pred, Arity ),
cont( Arity, F, Pred, C).
isconsult( atom(use_module) ).
isconsult( atom(ensure_loaded) ).
isconsult( atom(compile) ).
isconsult( atom(consult) ).
isconsult( atom(reconsult) ).
isconsult( atom(load_files) ).
isconsult( '[' ).
arg([']'|_]).
arg([l|_]).
file_or_library(F,C) :-
libsym(C0),
atom_cooncat(F,C,C0).
file_or_library(F,C) :-
check_file(F,C).
check_file(F0,C) :-
atom_concat('\'',F,F0),
!,
absolute_file_name( F, FF, [access(none)] ),
atom_concat( FF, '*' , Pat),
absolute_file_name( Pat, C0, [glob(true)] ),
atom_concat(Pat,C00,C0),
atom_conct(C00,'\'',C).
check_file(F0,C) :-
atom_concat( F0, '*' , Pat),
absolute_file_name( Pat, C0, [glob(true)] ),
atom_concat(Pat,C,C0).
check_library( Lib, F, C) :-
atom_concat( F, '*' , Pat),
LibF =.. [Lib(Pat)],
absolute_file_name( LibF, Lib, [glob(true)] ),
file_directory_name( Lib, Name),
( atom_concat(C, '.yap', Name) -> true ;
atom_concat(C, '.ypp', Name) -> true ;
atom_concat(C, '.prolog', Name) -> true
).
predicate(N,P,A) :-
system_predicate(P0/A),
atom_concat(N,P,P0).
predicate(N,P,A) :-
current_predicate(P0/A),
atom_concat(N,P,P0).
cont(0, F, P, P0) :-
atom_concat( F, P, P0 ).
cont( _, F, P, PB ):-
atom_concat( [F, P, '( )'], PB ).

View File

@ -30,9 +30,9 @@
python/2,
acquire_GIL/0,
release_GIL/0,
python_threaded/0,
prolog_list_to_python_list/3,
op(100,fy,$),
python_threaded/0,
prolog_list_to_python_list/3,
op(100,fy,$),
op(950,fy,:=),
op(950,yfx,:=),
% op(950,fx,<-),

View File

@ -10,17 +10,19 @@
yap_query/4,
python_query/2,
python_query/3,
python_import/1,
yapi_query/2
]).
:- yap_flag(verbose, silent).
:- yap_flag(verbose, silent).
:- use_module(library(python)).
:- use_module( library(lists) ).
:- use_module( library(maplist) ).
:- use_module( library(rbtrees) ).
:- use_module( library(terms) ).
:- reexport( library(python) ).
:- python_import(yap4py.yapi).
%:- python_import(gc).

View File

@ -280,7 +280,8 @@ set (RENAMED_RESOURCES
# yap_kernel/resources/codemirror/mode/prolog/prolog.js
)
set (PL_SOURCES yap_ipython/prolog/jupyter.yap)
set (PL_SOURCES yap_ipython/prolog/jupyter.yap
)
set(FILES ${PYTHON_SOURCES} ${PL_SOURCES} ${EXTRAS} ${RESOURCES})

View File

@ -1,30 +1,24 @@
/**
* @file jupyter.yap
*
* @brief allow interaction between Jupyter and YAP.
*
* @long The code in here:
* - establishes communication between Prolog and Python Streams
* - inputs Prolog code and queries
* - supports completion of Prolog programs.
* -
*/
* @file jupyter.yap4py
*
* @brief JUpyter support.
*/
% :- module( jupyter,
% [jupyter_query/3,
% errors/2,
% ready/2,
% completion/2,
% ]
%% ).
:- [library(hacks)].
:- reexport(library(yapi)).
:- reexport(completer).
:- use_module(library(hacks)).
:- use_module(library(lists)).
:- use_module(library(maplist)).
:- use_module(library(python)).
:- use_module(library(yapi)).
:- python_import(sys).
@ -40,7 +34,8 @@ jupyter_cell( _Caller, _, Line ) :-
jupyter_cell( _Caller, _, [] ) :- !.
jupyter_cell( Caller, _, Line ) :-
Self := Caller.query,
python_query( Self, Line, box_input ).
start_low_level_trace,
python_query( Self, Line ).
jupyter_consult(Text) :-
blank( Text ),
@ -62,21 +57,26 @@ blankc('\t').
streams(false) :-
nb_setval(jupyter_cell, false),
flush_output,
forall(
stream_property( S, mode(_) ),
close(S)
).
nb_setval(jupyter_cell, false),
fail,
flush_output,
retract(cell_stream(S)),
close(S),
fail.
streams(false).
streams(true) :-
nb_setval(jupyter_cell, true),
% open('/python/input', read, _Input, [alias(user_input),bom(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]),
% set_prolog_flag(user_input,_Input),
set_prolog_flag(user_output,_Output),
set_prolog_flag(user_error,_Error).
nb_setval(jupyter_cell, true),
fail,
% open('/python/input', read, _Input, [alias(user_input),bom(false)]),
open('/python/sys.stdout', append, Output, [alias(user_output)]),
assert( cell_stream( Output) ),
open('/python/sys.stderr', append, Error, [alias(user_error)]),
assert( cell_stream( Error) ),
% set_prolog_flag(user_input,_Input),
set_prolog_flag(user_output, Output),
set_prolog_flag(user_error, Error).
streams(true).
ready(_Self, Line ) :-
blank( Line ),

View File

@ -496,8 +496,10 @@ class YAPCompleter(Completer):
self.matches = []
prolog_res = self.shell.yapeng.mgoal(completions(text, self), "user",True)
if self.matches:
print( text, magic_res )
return text, self.matches
magic_res = self.magic_matches(text)
print( text, magic_res )
return text, magic_res
@ -535,6 +537,7 @@ class YAPRun:
# construct a self.queryuery from a one-line string
# self.query is opaque to Python
try:
print(query,s, file=sys.stderr)
program,query,stop,howmany = self.prolog_cell(s)
found = False
if s != self.os:
@ -545,10 +548,12 @@ class YAPRun:
self.query = self.yapeng.query( pg)
self.query.port = "call"
self.query.answer = {}
print("new", file=sys.stderr)
else:
self.query.port = "retry"
self.os = s
howmany += self.iterations
print('old', file=sys.stderr)
while self.query.next():
answer = self.query.answer
found = True
@ -696,7 +701,7 @@ class YAPRun:
if linec:
self.shell.run_line_magic(magic, line)
else:
print("txt0: ",txt0,"\n")
print("txt0: ",txt0,"\n", file=sys.stderr)
if len(txt0) == 1:
cell = ""
else:
@ -730,7 +735,7 @@ class YAPRun:
self.yapeng.mgoal(streams(False),"user", True)
except Exception as e:
has_raised = True
self.yapeng.mgoal(streams("off"),"user")
self.yapeng.mgoal(streams(False),"user")
if state:
self.shell.last_execution_succeeded = True
self.result.result = (True, dicts)