docs & python:

This commit is contained in:
Vitor Santos Costa 2018-05-30 21:54:12 +01:00
parent 9afb5b07b2
commit bbd0122fc3
6 changed files with 78 additions and 47 deletions

View File

@ -1,5 +1,4 @@
#define _EXPORT_KERNEL 1 #define _EXPORT_KERNEL 1
#include "yapi.hh" #include "yapi.hh"
@ -746,8 +745,10 @@ if (!result) {
} }
PredEntry *YAPQuery::rewriteUndefQuery() { PredEntry *YAPQuery::rewriteUndefQuery() {
ARG1 = goal; Term ts[2];
goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorMetaCall, 4, &ARG1)); ts[0] = CurrentModule;
ts[1] = goal;
ARG1 = goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorModule, 2, ts));
return ap = PredCall; return ap = PredCall;
} }

View File

@ -25,19 +25,33 @@
context_variables/1 context_variables/1
]). ]).
/** /**
* @defgroup yap_hacks YAP hacking * @addtogroup Hacks
* @ingroup library * @{
* * @brief Manipulate the Prolog stacks, including setting and resetting
* Manipulate the Prolog stacks, including setting and resetting
* choice-points. * choice-points.
* *
**/
/**
* @pred stack_dump
*
* Write the current ancestor stack to the outout. Ancestors may have:
* - terminated
* - still have sub-goals to execute, if so, they left an _environment_
* - still have clauses they may nacktrack to; if so, they left a _choice point_
*
*/ */
stack_dump :- stack_dump :-
stack_dump(-1). stack_dump(-1).
/**
* @pred stack_dump(+N)
*
* Report the last _N_ entries in the stack (see stack_dump/0)
*/
stack_dump(Max) :- stack_dump(Max) :-
current_choicepoints(CPs), current_choicepoints(CPs),
current_continuations([Env|Envs]), current_continuations([Env|Envs]),
@ -53,6 +67,14 @@ run_formats([Com-Args|StackInfo], Stream) :-
format(Stream, Com, Args), format(Stream, Com, Args),
run_formats(StackInfo, user_error). run_formats(StackInfo, user_error).
/**
* @pred virtual_alarm(+Interval, 0:Goal, -Left)
*
* Activate an alarm to execute _Goal_ in _Interval_ seconds. If the alarm was active,
* bind _Left_ to the previous value.
*
* If _Interval_ is 0, disable the current alarm.
*/
virtual_alarm(Interval, Goal, Left) :- virtual_alarm(Interval, Goal, Left) :-
Interval == 0, !, Interval == 0, !,
virtual_alarm(0, 0, Left0, _), virtual_alarm(0, 0, Left0, _),
@ -68,3 +90,6 @@ virtual_alarm(Interval.USecs, Goal, Left.LUSecs) :-
fully_strip_module(T,M,S) :- fully_strip_module(T,M,S) :-
'$hacks':fully_strip_module(T,M,S). '$hacks':fully_strip_module(T,M,S).
%% @}

View File

@ -69,16 +69,14 @@ argi(N,I,I1) :-
I1 is I+1. I1 is I+1.
python_query( Caller, String ) :- python_query( Caller, String ) :-
writeln(String),
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.q.port := Status, atom_to_string( Status, SStatus ),
% := print( gc.get_referrers(Caller.port)), Caller.q.port := SStatus,
write_query_answer( Bindings ), write_query_answer( Bindings ),
nl(user_error), nl(user_error),
Caller.q.answer := {}, Caller.q.answer := {},
maplist(in_dict(Caller.q.answer), Bindings). maplist(in_dict(Caller.q.answer), Bindings).
% := print( "b", gc.get_referrers(Caller.answer)).
in_dict(Dict, var([V0,V|Vs])) :- !, in_dict(Dict, var([V0,V|Vs])) :- !,
Dict[V] := V0, Dict[V] := V0,

View File

@ -70,17 +70,14 @@ class Query:
def __next__(self): def __next__(self):
if not self.q: if not self.q:
raise StopIteration()
if self.port == "exit":
self.close()
return
if self.q.next():
rc = self.answer
return rc
else:
if self.q:
self.close()
raise RuntimeError() raise RuntimeError()
if self.port == "exit":
return
else:
if self.q.next():
return self.port,self.answer
else:
self.close()
def close( self ): def close( self ):
if self.q: if self.q:
@ -140,7 +137,7 @@ class YAPShell:
# # vs is the list of variables # # vs is the list of variables
# you can print it out, the left-side is the variable name, # you can print it out, the left-side is the variable name,
# the right side wraps a handle to a variable # the right side wraps a handle to a variable
#import pdb; pdb.set_trace() import pdb; pdb.set_trace()
# #pdb.set_trace() # #pdb.set_trace()
# atom match either symbols, or if no symbol exists, sttrings, In this case # atom match either symbols, or if no symbol exists, sttrings, In this case
# variable names should match strings # variable names should match strings
@ -156,14 +153,12 @@ class YAPShell:
g.release() g.release()
g = python_query(self, query) g = python_query(self, query)
self.q = Query( engine, g ) self.q = Query( engine, g )
print(self.q.port) for port,bind in self.q:
for bind in self.q:
print(bind,self.q.port)
bindings += [bind] bindings += [bind]
if port == "exit":
break
if loop: if loop:
continue continue
if self.q.port == "exit":
break
s = input("more(;), all(*), no(\\n), python(#)? ").lstrip() s = input("more(;), all(*), no(\\n), python(#)? ").lstrip()
if s.startswith(';') or s.startswith('y'): if s.startswith(';') or s.startswith('y'):
continue continue

View File

@ -21,14 +21,9 @@
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan> * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
* @date Thu Oct 19 12:02:56 2017 * @date Thu Oct 19 12:02:56 2017
* *
* @brief Low-level access * @brief Access to the YAP engine internal data
*
* @defgroup Hacks Low-level access
* @ingroup builtins
*
*/ */
:- module('$hacks', :- module('$hacks',
[display_stack_info/4, [display_stack_info/4,
display_stack_info/6, display_stack_info/6,
@ -36,6 +31,15 @@
fully_strip_module/3, fully_strip_module/3,
code_location/3]). code_location/3]).
/**
*
* @defgroup Hacks Low-level access
* @ingroup builtins
* @{
*
**/
/** hacks:context_variables(-NamedVariables) /** hacks:context_variables(-NamedVariables)
Access variable names. Access variable names.
@ -259,3 +263,5 @@ beautify_hidden_goal('$current_predicate'(Na,M,S,_),prolog) -->
[current_predicate(Na,M:S)]. [current_predicate(Na,M:S)].
beautify_hidden_goal('$list_clauses'(Stream,M,Pred),prolog) --> beautify_hidden_goal('$list_clauses'(Stream,M,Pred),prolog) -->
[listing(Stream,M:Pred)]. [listing(Stream,M:Pred)].
%% @}

View File

@ -22,16 +22,22 @@
* *
* @brief list predicates in a module * @brief list predicates in a module
* *
* @defgroup Listing list predicates in a module
* @ingroup builtins
*
*/ */
:- system_module( '$_listing', [listing/0, :- system_module( '$_listing', [listing/0,
listing/1, listing/1,
portray_clause/1, portray_clause/1,
portray_clause/2], []). portray_clause/2], []).
/**
* @defgroup Listing List predicates in a module
* @{
* @ingroup builtins
*
*/
:- use_system_module( '$_errors', ['$do_error'/2]). :- use_system_module( '$_errors', ['$do_error'/2]).
:- use_system_module( '$_preds', ['$clause'/4, :- use_system_module( '$_preds', ['$clause'/4,
@ -44,7 +50,7 @@
/** @pred listing /** @pred listing
vxuLists in the current output stream all the clauses for which source code Lists in the current output stream all the clauses for which source code
is available (these include all clauses for dynamic predicates and is available (these include all clauses for dynamic predicates and
clauses for static predicates compiled when source mode was `on`). clauses for static predicates compiled when source mode was `on`).