From 69fcc9494cd02cb3dd70888b6a60ad75e7851ce1 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 26 Mar 2019 09:40:54 +0000 Subject: [PATCH] jupyter --- C/adtdefs.c | 2 +- C/stack.c | 32 +------ os/fmem.c | 1 + os/format.c | 1 + packages/python/swig/prolog/yapi.yap | 85 +++++++++++++++++-- packages/python/swig/yap4py/systuples.py | 5 +- .../yap_kernel/yap_ipython/prolog/jupyter.yap | 12 +-- .../yap_kernel/yap_ipython/prolog/verify.yap | 3 +- .../python/yap_kernel/yap_ipython/yapi.py | 34 ++++---- pl/consult.yap | 5 -- pl/errors.yap | 2 +- pl/messages.yap | 6 +- 12 files changed, 115 insertions(+), 73 deletions(-) diff --git a/C/adtdefs.c b/C/adtdefs.c index 064f02421..de7ca09c7 100755 --- a/C/adtdefs.c +++ b/C/adtdefs.c @@ -1296,7 +1296,7 @@ Atom Yap_LookupAtomWithLength(const char *atom, at = NameOfFunctor(pe->FunctorOfPred); } } - if (mods == PROLOG_MODULE || mods == USER_MODULE) + if (pe->ModuleOfPred == PROLOG_MODULE || pe->ModuleOfPred == USER_MODULE) snprintf(LOCAL_FileNameBuf, YAP_FILENAME_MAX, "%s/" UInt_FORMAT, RepAtom(at)->StrOfAE, arity); else diff --git a/C/stack.c b/C/stack.c index 3418c0ef1..62e411d85 100644 --- a/C/stack.c +++ b/C/stack.c @@ -262,7 +262,7 @@ bool Yap_search_for_static_predicate_in_use(PredEntry *p, choiceptr b_ptr = B; CELL *env_ptr = ENV; - if (check_everything && P) { + if (check_everything && P && ENV) { PredEntry *pe = EnvPreg(P); if (p == pe) return true; @@ -284,7 +284,7 @@ bool Yap_search_for_static_predicate_in_use(PredEntry *p, PredEntry *pe; if (!cp) - return true; + return false; pe = EnvPreg(cp); if (p == pe) return true; @@ -296,38 +296,12 @@ bool Yap_search_for_static_predicate_in_use(PredEntry *p, } } /* now mark the choicepoint */ - if (b_ptr) { pe = PredForChoicePt(b_ptr->cp_ap, NULL); } else return false; if (pe == p) { - if (check_everything) - return true; - PELOCK(38, p); - if (p->PredFlags & IndexedPredFlag) { - yamop *code_p = b_ptr->cp_ap; - yamop *code_beg = p->cs.p_code.TrueCodeOfPred; - - /* FIX ME */ - - if (p->PredFlags & LogUpdatePredFlag) { - LogUpdIndex *cl = ClauseCodeToLogUpdIndex(code_beg); - if (find_owner_log_index(cl, code_p)) - b_ptr->cp_ap = cur_log_upd_clause(pe, b_ptr->cp_ap->y_u.Otapl.d); - } else if (p->PredFlags & MegaClausePredFlag) { - StaticIndex *cl = ClauseCodeToStaticIndex(code_beg); - if (find_owner_static_index(cl, code_p)) - b_ptr->cp_ap = cur_clause(pe, b_ptr->cp_ap->y_u.Otapl.d); - } else { - /* static clause */ - StaticIndex *cl = ClauseCodeToStaticIndex(code_beg); - if (find_owner_static_index(cl, code_p)) { - b_ptr->cp_ap = cur_clause(pe, b_ptr->cp_ap->y_u.Otapl.d); - } - } - } - UNLOCKPE(63, pe); + return true; } env_ptr = b_ptr->cp_env; b_ptr = b_ptr->cp_b; diff --git a/os/fmem.c b/os/fmem.c index b50763a5d..56707cbef 100755 --- a/os/fmem.c +++ b/os/fmem.c @@ -331,6 +331,7 @@ bool Yap_CloseMemoryStream(int sno) { if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) free(GLOBAL_Stream[sno].nbuf); } + GLOBAL_Stream[sno].status = Free_Stream_f; return true; } diff --git a/os/format.c b/os/format.c index 7e03ee227..0428b13ce 100644 --- a/os/format.c +++ b/os/format.c @@ -990,6 +990,7 @@ static Int doformat(volatile Term otail, volatile Term oargs, Term ta[2]; ta[0] = otail; ta[1] = oargs; + format_clean_up(sno, sno0, finfo); Yap_ThrowError(LOCAL_Error_TYPE, Yap_MkApplTerm(Yap_MkFunctor(AtomFormat, 2), 2, ta), "arguments to format"); diff --git a/packages/python/swig/prolog/yapi.yap b/packages/python/swig/prolog/yapi.yap index eeeb8066a..bc287d938 100644 --- a/packages/python/swig/prolog/yapi.yap +++ b/packages/python/swig/prolog/yapi.yap @@ -37,9 +37,9 @@ %% dictionary, Examples %% %% -yapi_query( VarNames, Self ) :- +yapi_query( VarNames, Caller ) :- show_answer(VarNames, Dict), - Self.bindings := Dict. + Caller.bindings := Dict. @@ -74,12 +74,9 @@ argi(N,I,I1) :- python_query( Caller, String ) :- 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), + query_to_answer( user:Goal, VarNames, Status, Bindings), Caller.q.port := Status, output(Caller, Bindings). @@ -87,12 +84,84 @@ python_query( Caller, String, Bindings ) :- %% write_query_answer( Bindings ), %% fail. output( Caller, Bindings) :- - maplist(into_dict(Caller),Bindings). + copy_term( Bindings, Bs), + simplify(Bs, 1, Bss), + numbervars(Bss, 0, _), + maplist(into_dict(Caller),Bss). + +simplify([],_,[]). +simplify([X=V|Xs], [X=V|NXs]) :- + var(V), + !, + X=V, + simplify(Xs,NXs). +simplify([X=V|Xs], I, NXs) :- + var(V), + !, + X=V, + simplify(Xs,I,NXs). +simplify([X=V|Xs], I, [X=V|NXs]) :- + !, + simplify(Xs,I,NXs). +simplify([G|Xs],I, [D=G|NXs]) :- + I1 is I+1, + atomic_concat(['__delay_',I,'__'],D), + simplify(Xs,I1,NXs). + bv(V,I,I1) :- atomic_concat(['__',I],V), I1 is I+1. into_dict(D,V0=T) :- - D.q.answer[V0] := T. + listify(T,L), + D.q.answer[V0] := L. + +listify('$VAR'(Bnd), V) :- + !, + listify_var(Bnd, V). +listify([A|As], V) :- + !, + maplist(listify,[A|As], V). +listify(A:As, A:Vs) :- + (atom(A);string(A)), + !, + maplist(listify,As, Vs). +listify(WellKnown, V) :- + WellKnown=..[N|As], + length(As,Sz), + well_known(N,Sz), + !, + maplist(listify,As, Vs), + V =.. [N|Vs]. + +listify('$VAR'(Bnd), V) :- + !, + listify_var(Bnd, V). +listify(T, t(S,V)) :- + T =.. [S,A|As], + !, + maplist(listify, [A|As], Vs), + V =.. [t|Vs]. +listify(S, S). + +listify_var(I, S) :- + I >= 0, + I =< 26, + !, + V is 0'A+I, + string_codes(S, [V]). +listify_var(I, S) :- + I < 0, + I >= -26, + !, + V is 0'A+I, + string_codes(S, [0'_+V]). +listify_var(S, S). + +well_known(+,2). +well_known(-,2). +well_known(*,2). +well_known(/,2). +well_known((','),2). diff --git a/packages/python/swig/yap4py/systuples.py b/packages/python/swig/yap4py/systuples.py index 4db75a74b..6676d2c11 100644 --- a/packages/python/swig/yap4py/systuples.py +++ b/packages/python/swig/yap4py/systuples.py @@ -4,7 +4,7 @@ asserta = namedtuple('asserta', 'clause') assertz = namedtuple('assertz', 'clause') bindvars = namedtuple('bindvars', 'list') compile = namedtuple('compile', 'file') -compdletionsile = namedtuple('completions', 'text self') +completions = namedtuple('completions', 'text self') dbms = namedtuple('dbms', 'filedbms') errors = namedtuple('errors', 'fileng engee') foreign = namedtuple('foreign', 'filedbms') @@ -15,10 +15,13 @@ ostreams = namedtuple('ostreams', ' text') prolog_library=namedtuple('prolog_library', 'listfiles') python_query = namedtuple('python_query', 'engine query') set_prolog_flag = namedtuple('set_prolog_flag', 'flag new_value') +current_prolog_flag = namedtuple('current_prolog_flag', 'flag value') show_answer = namedtuple('show_answer', 'vars dict') streams = namedtuple('streams', 'text') v = namedtuple('_', 'slot') v0 = namedtuple('v', 'slot') +yap_flag = namedtuple('yap_flag', 'flag value new_value') +show_answer = namedtuple('show_answer', 'vars dict') yap_query = namedtuple('yap_query', 'query owner') yapi_query = namedtuple('yapi_query', 'vars dict') diff --git a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap index 589ed0aa8..04e68ff6d 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 * @@ -57,7 +57,6 @@ next_streams( _Caller, answer, _Bindings ) :- % Caller.answer := Bindings, !. next_streams(_, redo, _ ) :- - streams(true), !. next_streams( _, _, _ ) :- streams(false). @@ -74,7 +73,7 @@ jupyter_cell( _Caller, _, Line , _) :- jupyter_cell(Caller, _, Line, Bindings ) :- Query = Caller, catch( - user:user_python_query(Query,Line, Bindings), + python_query(Query,Line, Bindings), error(A,B), system_error(A,B) ). @@ -103,11 +102,12 @@ jupyter_consult(Cell) :- ( Options = [], open_mem_read_stream( Cell, Stream), - load_files(Stream,[stream(Stream)| Options]) + load_files(user:Stream,[stream(Stream)| Options]) ), error(A,B), - (close(Stream), system_error(A,B)) + (close(Stream), system_error(A,B)) ), + close(Stream), fail. jupyter_consult(_Cell). @@ -124,7 +124,7 @@ blank(Text) :- streams(false) :- - close(user_input), + close(user_input), close(user_output), close(user_error). streams( true) :- diff --git a/packages/python/yap_kernel/yap_ipython/prolog/verify.yap b/packages/python/yap_kernel/yap_ipython/prolog/verify.yap index 164d236bf..315004fab 100644 --- a/packages/python/yap_kernel/yap_ipython/prolog/verify.yap +++ b/packages/python/yap_kernel/yap_ipython/prolog/verify.yap @@ -72,7 +72,8 @@ jupyter(En), close_esh( _Engine , Stream ) :- retractall(jupyter(_)), assertz(jupyter([])), - close(Stream). + close(Stream), + python_clear_errors. diff --git a/packages/python/yap_kernel/yap_ipython/yapi.py b/packages/python/yap_kernel/yap_ipython/yapi.py index fbf2f82e1..715b66045 100644 --- a/packages/python/yap_kernel/yap_ipython/yapi.py +++ b/packages/python/yap_kernel/yap_ipython/yapi.py @@ -560,7 +560,7 @@ class YAPRun(InteractiveShell): self.iterations = 0 pg = jupyter_query(self,program,squery) self.q = Query(self.engine, pg) - while self.q.next(): + for v in self.q: self.iterations += 1 o = '[ ' o += str(self.iterations ) @@ -704,39 +704,39 @@ class YAPRun(InteractiveShell): self.shell.displayhook.exec_result = result if self.syntaxErrors(cell): result.result = [] - return + return result has_raised = False try: + if not cell.strip('\n \t'): + return result builtin_mod.input = input self.shell.input = input self.engine.mgoal(streams(True),"user", True) - if cell.strip('\n \t'): - #create a Trace object, telling it what to ignore, and whether to - # do tracing or line-counting or both. - # tracer = trace.Trace( - # ignoredirs=[sys.prefix, sys.exec_prefix], - # trace=1, - # count=0) - # + #create a Trace object, telling it what to ignore, and whether to + # do tracing or line-counting or both. + # tracer = trace.Trace( + # ignoredirs=[sys.prefix, sys.exec_prefix], + # trace=1, + # count=0) + # - # def f(self, cell, state): - # state = self.jupyter_query( cell ) + # def f(self, cell, state): + # state = self.jupyter_query( cell ) # run the new command using the given tracer # # tracer.runfunc(f,self,cell,state) - answers = self.prolog( cell, result ) - # state = tracer.runfunc(hist - # er_query( self, cell ) ) - self.shell.last_execution_succeeded = True + answers = self.prolog( cell, result ) + # state = tracer.runfunc(hist + # er_query( self, cell ) ) except Exception as e: has_raised = True try: (etype, value, tb) = e traceback.print_exception(etype, value, tb) + self.engine.mgoal(streams(False),"user", True) except: print(e) - pass self.shell.last_execution_succeeded = not has_raised diff --git a/pl/consult.yap b/pl/consult.yap index 26ace8454..c799a4e2c 100644 --- a/pl/consult.yap +++ b/pl/consult.yap @@ -348,8 +348,6 @@ load_files(Files0,Opts) :- ; true ), - % make sure we can run consult - '$init_consult', '$lf'(Files, M, Call, TOpts). '$check_files'(Files, Call) :- @@ -736,7 +734,6 @@ db_files(Fs) :- b_setval('$lf_status', TOpts), '__NB_getval__'('$if_level', OldIfLevel, OldIfLevel=0), nb_setval('$if_level',0), - writeln(ln(OldIfLevel)), % take care with [a:f], a is the ContextModule '$current_module'(SourceModule, ContextModule), '$lf_opt'(consult, TOpts, Reconsult0), @@ -786,7 +783,6 @@ db_files(Fs) :- ; true ), - writeln(out(OldIfLevel)), nb_setval('$if_level',OldIfLevel), set_stream( OldStream, alias(loop_stream) ), set_prolog_flag(generate_debug_info, GenerateDebug), @@ -1553,7 +1549,6 @@ If an error occurs, the error is printed and processing proceeds as if '$if'(_Goal,_) :- '__NB_getval__'('$if_level',Level0,Level=0), Level is Level0 + 1, -writeln(Level), nb_setval('$if_level',Level), ( '__NB_getval__'('$endif', OldEndif, fail) -> true ; OldEndif=top), ( '__NB_getval__'('$if_skip_mode', Mode, fail) -> true ; Mode = run ), diff --git a/pl/errors.yap b/pl/errors.yap index abb4fb9ea..f4000aef9 100644 --- a/pl/errors.yap +++ b/pl/errors.yap @@ -94,7 +94,7 @@ error_handler(Error, Level) :- '$LoopError'(Error, Level). '$LoopError'(_, _) :- - %stop_low_level_trace, + stop_low_level_trace, flush_output(user_output), flush_output(user_error), fail. diff --git a/pl/messages.yap b/pl/messages.yap index 3cae1b7d9..08ffcc55c 100644 --- a/pl/messages.yap +++ b/pl/messages.yap @@ -206,6 +206,7 @@ compose_message(error(E, Exc), Level) --> { '$show_consult_level'(LC) }, + print_exception(Exc), location(error(E, Exc), Level, LC), main_message(error(E,Exc) , Level, LC ), c_goal( error(E, Exc), Level ), @@ -282,10 +283,8 @@ location( error(_,Info), Level, LC ) --> query_exception(prologPredArity, Desc, Ar) }, !, - display_consulting( File, Level, Info, LC ), {simplify_pred(M:Na/Ar,FF)}, [ '~a:~d:0 ~a while executing ~q:'-[File, FilePos,Level,FF] ]. - location( error(_,Info), Level, LC ) --> { '$error_descriptor'(Info, Desc) }, { @@ -294,7 +293,6 @@ location( error(_,Info), Level, LC ) --> query_exception(errorFunction, Desc, F) }, !, - display_consulting( File, Level, Info, LC ), {simplify_pred(F,FF)}, [ '~a:~d:0 ~a while executing ~a().'-[File, FilePos,Level,FF] ]. location( _Ball, _Level, _LC ) --> []. @@ -351,7 +349,7 @@ main_error_message(evaluation_error(What, Who)) --> [ '~*|** ~w caused ~a during evaluation of arithmetic expressions **' - [ 10,Who,What], nl ]. main_error_message(existence_error(Type , Who)) --> [nl], - [ '~*|** ~q ~q could not be found **' - [ 10,Type, Who], nl ]. + [ '~*|** ~q ~q does not exist **' - [ 10,Type, Who], nl ]. main_error_message(permission_error(Op, Type, Id)) --> [ '~*|** value ~q is not allowed in ~a ~q **' - [ 10, Op, Type,Id], nl ]. main_error_message(instantiation_error) -->