Prev Next Up Home Keys Figs Search New

Evaluation of Strings in Prolog

Appeared in Volume 6/1, February 1993

Keywords: strings.

walser@isa.de
Joachim Walser
4th June 1992

Here is a brief summary of simple predicates from different Prolog systems that do the kind of string evaluation I was looking for:

Prolog by BIM
?- sread('A is 2 + 3', _goal), call(_goal).

Sepia Prolog
[sepia]: term_string(Term, "A is 2 + 3"), call(Term).

LPA MacProlog
?- pname(Goal, 'X is 2 + 3'), call(Goal).

Quintus Prolog

:- use_module(library(charsio), [chars_to_term/2]).
?- chars_to_term("A is 2 + 3", Goal), call(Goal).
ALS Prolog
eval(String) :-
   bufread(String, [Structure, Variable_name|_]) ,
   arg(1, Structure, Answer),      % Structure =.. [is,Answer,_expression]
   Structure,                      % call(Structure)
   write(Variable_name = Answer),  % write(V_name),write(' = '),write(Ans)
   nl .
It seems that the only portable method is to write a string to a file, read it back and call/1 it.

Prev Next Up Home Keys Figs Search New