Prolog solving prefix arithmetic expression with unknown variable
Asked Answered
F

1

0

I want to make an arithmetic solver in Prolog that can have +,-,*,^ operations on numbers >= 2. It should also be possible to have a variable x in there. The input should be a prefix expression in a list.

I have made a program that parses an arithmetic expression in prefix format into a syntax tree. So that:

?- parse([+,+,2,9,*,3,x],Tree).
Tree = plus(plus(num(2), num(9)), mul(num(3), var(x))) .

(1) At this stage, I want to extend this program to be able to solve it for a given x value. This should be done by adding another predicate evaluate(Tree, Value, Solution) which given a value for the unknown x, calculates the solution.

Example:

?- parse([*, 2, ^, x, 3],Tree), evaluate(Ast, 2, Solution).
Tree = mul(num(2), pow(var(x), num(3))) ,
Solution = 16.

I'm not sure how to solve this problem due to my lack of Prolog skills, but I need a way of setting the var(x) to num(2) like in this example (because x = 2). Maybe member in Prolog can be used to do this. Then I have to solve it using perhaps is/2

Edit: My attempt to solving it. Getting error: 'Undefined procedure: evaluate/3 However, there are definitions for: evaluate/5'

evaluate(plus(A,B),Value,Sol) --> evaluate(A,AV,Sol), evaluate(B,BV,Sol), Value is AV+BV.
evaluate(mul(A,B),Value,Sol) --> evaluate(A,AV,Sol), evaluate(B,BV,Sol), Value is AV*BV.
evaluate(pow(A,B),Value,Sol) --> evaluate(A,AV,Sol), evaluate(B,BV,Sol), Value is AV^BV.
evaluate(num(Num),Value,Sol) --> number(Num).
evaluate(var(x),Value,Sol) --> number(Value).

(2) I'd also want to be able to express it in postfix form. Having a predicate postfixform(Tree, Postfixlist)

Example:

?- parse([+, *, 2, x, ^, x, 5 ],Tree), postfix(Tree,Postfix).
Tree = plus(mul(num(2), var(x)), pow(var(x), num(5))) ,
Postfix = [2, x, *, x, 5, ^, +].

Any help with (1) and (2) would be highly appreciated!

Freehand answered 11/11, 2013 at 21:38 Comment(10)
The first part: is answered here: https://mcmap.net/q/1694602/-prolog-parsing.Variscite
@Variscite Yes, thanks! I used your approach to implement the first part, but I'm stuck in my approach for (1) and (2). Any ideas?Freehand
The last part is: #13132140Variscite
The part in the middle is not that clear. Do you permit several variables?Variscite
@Variscite Thanks again. No, only one variable x. So in evaluate(Tree, Value, Solution), Value will always correspond to the value of x.Freehand
So you need a different interface: You need to be able to indicate several variables at once.Variscite
I was considering this approach. ktiml.mff.cuni.cz/~bartak/prolog/arithmetics.html In my example it would be eval_v(plus(A,B),CV,Vars):-eval_v(A,AV,Vars),eval_v(B,BV,Vars),CV is AV+BV. eval_v(mul(A,B),CV,Vars):-eval_v(A,AV,Vars),eval_v(B,BV,Vars),CV is AV*BV. eval_v(pow(A,B),CV,Vars):-eval_v(A,AV,Vars),eval_v(B,BV,Vars),CV is AV^BV. eval_v(Num,Num,Vars):-number(Num). eval_v(Var,Value,Vars):-atom(Var),member(Var/Value,Vars) but there seems to be something wrong with it. Any idea?Freehand
You are putting too much into a single question.Variscite
Sorry for that. Have a look at my attempt at solving it. I edited the main question.Freehand
This site works if you ask a clear question. Not everything at once. See: stackoverflow.com/helpVariscite
D
1

You don't need to use a grammar for this, as you are doing. You should use normal rules.

This is the pattern you need to follow.

evaluate(plus(A,B),Value,Sol) :- 
   evaluate(A, Value, A2),
   evaluate(B, Value, B2),
   Sol is A2+B2.

And

evaluate(num(X),_Value,Sol) :- Sol = X.
evaluate(var(x),Value,Sol) :- Sol = Value.
Dress answered 13/11, 2013 at 1:56 Comment(2)
Thanks a lot! I tried normal rules as well, but I did a typo 'Value is ...' instead of 'Sol is ...' Now it's working fine!Freehand
Of course! Could you have a look at my other problem with my implementation? #19960222 Thanks!Freehand

© 2022 - 2024 — McMap. All rights reserved.