For Programmers: Free Programming Magazines  


Home > Archive > Prolog > November 2007 > Evaluate Equation at runtime









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Evaluate Equation at runtime
philipp.strack@gmail.com

2007-10-31, 7:12 pm

Hi everybody,

how can I evaluate an expression given by the user at runtime. I mean
for example given the rule

gleich(X,Y) :-
X is Y.

the user could ask

gleich(1,2-1).

and get true as a result

Thank you very much in advance

Philipp

Chip Eastham

2007-10-31, 7:12 pm

On Oct 31, 1:29 pm, philipp.str...@gmail.com wrote:
> Hi everybody,
>
> how can I evaluate an expression given by the user at runtime. I mean
> for example given the rule
>
> gleich(X,Y) :-
> X is Y.
>
> the user could ask
>
> gleich(1,2-1).
>
> and get true as a result
>
> Thank you very much in advance
>
> Philipp


Perhaps the =.. operator is what you need. It converts
a list headed by a functor followed by terms into the
term corresponding to the functor (operand) applied to
the terms (operands). So something like:

gleich(X,L) :- Y =.. L, X is Y.

might succeed with:

gleich(1,[-,2,1].

In any case parsing the user's input into the list
form may prove to be the difficult task. Again the
operator =.. may help, because it can succeed by
decoding an expression into a list as well.

regards, chip

Markus Triska

2007-10-31, 10:08 pm

philipp.strack@gmail.com writes:

> how can I evaluate an expression given by the user at runtime. I
> mean for example given the rule gleich(X,Y) :- X is Y. the user


Suppose:

allowed_goal(_ =:= _).
allowed_goal(_ < _).
allowed_goal((G1,G2)) :- allowed_goal(G1), allowed_goal(G2).

then: ?- read(G), ground(G), allowed_goal(G), G.
--> user types: 1 =:= 0+1, 0 < 1.
% ==> success

Also check out the call/N family of predicates.

You can of course always evaluate things yourself:

true(A =:= B) :- expr_val(A, VA), expr_val(B, VA).
true(A < B) :- expr_val(A, VA), expr_val(B, VB), VA < VB.
true((G1,G2)) :- true(G1), true(G2).

expr_val(n(N), N).
expr_val(A+B, V) :- expr_val(A, VA), expr_val(B, VB), V is VA+VB.
expr_val(A*B, V) :- expr_val(A, VA), expr_val(B, VB), V is VA*VB.

Example:

?- read(P), ground(P), true(P).
--> user types: n(3) < n(5), n(1) =:= n(1)+n(0).
% ==> success

This gives you more control over the process - for example, with
simple modifications, you can create a trace of the evaluation.

--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/
Jan Wielemaker

2007-11-01, 4:22 am

On 2007-10-31, Chip Eastham <hardmath@gmail.com> wrote:[color=darkred]
> On Oct 31, 1:29 pm, philipp.str...@gmail.com wrote:

Maybe you might want to try first:

1 ?- [user].
|: gleich(X,Y) :-
|: X is Y.
|: % user://1 compiled 0.00 sec, 584 bytes

Yes
2 ?- gleich(1,2-1).

Yes
3 ?-

So, this just works in SWI-Prolog, YAP and SICStus and if I'm not
mistaken it works in any ISO compatible Prolog system. In the old days
Quintus refused this, but later versions included it. Most of these
systems compile arithmetic expressions, but if they encounter a term at
runtime they have two options: throw an error or compute it. Computing
is a lot friendlier.

If it doesn't work you have to go along the interpretation approach
Markus outlines or get yourself a compatible Prolog system.

--- Jan
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com