Next: , Previous: , Up: prolog   [Index]


16.2 Interpreter

guile version >= 2.0 support an interpreter that can switch between different shells. To start the prolog interpreter use the standard way of switching the language through

scheme@(guile-user)> ,L prolog

Happy hacking with Prolog!  To switch back, type `,L scheme'.
prolog@(guile-user)>

This will have the namespce guile-user, so we need to import the prolog code bt

prolog@(guile-user)> .[use-modules (logic guile-log iso-prolog)]
prolog@(guile-user)>

This is pretty hackable, you will have all guile and all prolog in the same environment. If you want to be more prolog centric environment, you can in case the is-prolog is loaded do

prolog@(guile-user)> default_module().
prolog@(prolog-user)>

This will make sure that we hack in a module where all scheme is preficed by scm- this together with the naming convention in prolog to use '_' in stead of '-' we risk less mixups.

As we saw, if the first character is '.[', then in everything until the next ] will be treated as a scheme expression. This is handy to evaluate scheme without switching the language. What’s nice is that all shell commands that guile has (prefixed with ',') is preserved so that one can basically do all of

prolog@(guile-user)> ,h all
Help Commands [abbrev]:

 ,help [all | GROUP | [-c] COMMAND]
                              [,h] - Show help.
 ,show [TOPIC]                     - Gives information about Guile.
 ,apropos REGEXP              [,a] - Find bindings/modules/packages.
 ,describe OBJ                [,d] - Show description/documentation.

Module Commands [abbrev]:

 ,module [MODULE]             [,m] - Change modules / Show current module.
 ,import [MODULE ...]       [,use] - Import modules / List those imported.
 ,load FILE                   [,l] - Load a file in the current module.
 ,reload [MODULE]            [,re] - Reload the given module, or the current module if none was given.
 ,binding                     [,b] - List current bindings.
 ,in MODULE COMMAND-OR-EXPRESSION  - Evaluate an expression or command in the context of module.

Language Commands [abbrev]:

 ,language LANGUAGE           [,L] - Change languages.

Compile Commands [abbrev]:

 ,compile EXP                 [,c] - Generate compiled code.
 ,compile-file FILE          [,cc] - Compile a file.
 ,expand EXP                [,exp] - Expand any macros in a form.
 ,optimize EXP              [,opt] - Run the optimizer on a piece of code and print the result.
 ,disassemble EXP             [,x] - Disassemble a compiled procedure.
 ,disassemble-file FILE      [,xx] - Disassemble a file.

Profile Commands [abbrev]:

 ,time EXP                    [,t] - Time execution.
 ,profile EXP                [,pr] - Profile execution.
 ,trace EXP                  [,tr] - Trace execution.

Debug Commands [abbrev]:

 ,backtrace [COUNT] [#:width W] [#:full? F]
                             [,bt] - Print a backtrace.
 ,up [COUNT]                       - Select a calling stack frame.
 ,down [COUNT]                     - Select a called stack frame.
 ,frame [IDX]                [,fr] - Show a frame.
 ,procedure                [,proc] - Print the procedure for the selected frame.
 ,locals                           - Show local variables.
 ,error-message           [,error] - Show error message.
 ,break PROCEDURE        [,br ,bp] - Break on calls to PROCEDURE.
 ,break-at-source FILE LINE
                   [,break-at ,bs] - Break when control reaches the given source location.
 ,step                        [,s] - Step until control reaches a different source location.
 ,step-instruction           [,si] - Step until control reaches a different instruction.
 ,next                        [,n] - Step until control reaches a different source location in the current frame.
 ,next-instruction           [,ni] - Step until control reaches a different instruction in the current frame.
 ,finish                           - Run until the current frame finishes.
 ,tracepoint PROCEDURE       [,tp] - Add a tracepoint to PROCEDURE.
 ,traps                            - Show the set of currently attached traps.
 ,delete IDX                [,del] - Delete a trap.
 ,disable IDX                      - Disable a trap.
 ,enable IDX                       - Enable a trap.
 ,registers                [,regs] - Print registers.

Inspect Commands [abbrev]:

 ,inspect EXP                 [,i] - Inspect the result(s) of evaluating EXP.
 ,pretty-print EXP           [,pp] - Pretty-print the result(s) of evaluating EXP.

System Commands [abbrev]:

 ,gc                               - Garbage collection.
 ,statistics               [,stat] - Display statistics.
 ,option [KEY VALUE]          [,o] - List/show/set options.
 ,quit        [,q ,continue ,cont] - Quit this session.

prolog@(guile-user)> 

All such commands has to be the first commands in the input. But there are a set of prolog commands added (they are prefixed with ., let’s see them

prolog@(guile-user)> .h

(.n           )             try to find n solutions
(.all    | .* )             try to find all solutions
(.once   | .1 )             try to find one solution
(.mute   | .m )             no value output is written.
(.unmute | .um)             output values is written.
---------------------------------------------------------------------
(.save   | .s ) <ref>       associate current state with name ref
(.load   | .l ) <ref>       restore associate state with name ref
(.cont   | .c )             continue the execution from last stall point
(.lold   | .lo)             restore the last state at a stall
(.clear       )             clear the prolog stack and state
(.rec         )             enable rational tree handling
(.unrec       )             disable rational tree handling
---------------------------------------------------------------------
(.ref         ) <ref>       get value of reference user variable ref
(.set         ) <ref> <val> set user variable ref to value val
---------------------------------------------------------------------
(.setp        ) <key>       associate current state to key
(.refp        ) <key>       instate state referenced by key
(.savep       )             save all referenced states to disk
(.loadp       )             load new referenced states from disk

So now we can time the execution of one solution with no output by doing

prolog@(guile-user)> ,time .1 .m f(X,Y).

;; 0.000900s real time, 0.000836s run time.  0.000000s spent in GC.

prolog@(guile-user)> 

There is an api that can be used with convinience goals to be used in code to talk with the interactive shell, to use them do

scheme (use-modules (logic guile-log guile-prolog interpreter))

16.2.1 state

It is possible to store a state interactively and later retrieve the state through a mechansim that brings back control to the interpreter. You do that with prolog stall/0. This will escape the program and return control to the shell.

in the shell you can store the state, retrieve a state and continue the execution from the actual state. Then by using global variables or user variables (preserved when storing the state) it is possible to create very nice interactive conversations for e.g. proof solvers and such. Also by using the dynamic feature framework one can contoll very fine grained how the state should be stored and restored.

16.2.2 user variables.

It is possible to use a special global variable in code and in the interactive prolog shell. They differ from global variables in that they are stored and restored relative the state. To use normal global variables just use the scheme globals and the scm[...] idiom to get the value. Ther is an accompanion

prolog user_ref(name,Var) To ref a user variable called name int Var.

prolog user_set(name,val) To set a user variable name to val


Next: , Previous: , Up: prolog   [Index]