Arbitrary Expressions -- library(activeread)

Languages such as Lisp allow you to read an expression and to evaluate it, returning a data structure. Prolog provides "evaluation" only for arithmetic expressions, and then only in certain argument positions. library(activeread) provides a new experimental facility for reading an arbitrary "expression" and "evaluating" it.

     | ?- active_read(InputTerm).
     

reads a term from the current input stream. If this term has the form

     X | Goal.
     

then Goal is called and InputTerm is unified with X. Otherwise, InputTerm is unified with the term that was read. Note that Goal may backtrack, in which case active_read/1 will also backtrack.

EXAMPLES:

     | ?- active_read(X).
     |: T | append([1,2],[3,4], T).
     X = [1,2,3,4]
     yes
     
     | ?- active_read(X).
     |: Front+Back | append(Front, Back, [1,2,3,4]).
     X = []+[1,2,3,4] ;
     X = [1]+[2,3,4] ;
     X = [1,2]+[3,4] ;
     X = [1,2,3]+[4] ;
     X = [1,2,3,4]+[] ;
     no
     
     | ?- active_read(X).
     |: abort.
     X = abort
     yes
     

Please note: library(activeread) is not a module-file, but it is sufficiently small that there should be no problem with including a separate copy in each module where it is required.