I have this scenario wherein I get a linear equation in the Prolog query like below:
?- myquery( 3X + 5Y = 10, Result).
So my query has an equation 3X + 5Y = 10, which in general assumes the form AX + BY = C, where A=3, B=5 and C=10.
Now, in my prolog program, I am trying to define a predicate that can take in the expression mentioned in the query above. That is, I somehow want to get A, B and C values and also the operator involved (in the above case the plus operator) stored and then used on the logic that I define withing the program. I am wondering how this can be done.
To be more generic, the question is how do I identify the constants and the operator involved in an equation that is passed on through the goal/query?
The following transcript may prove illuminating:
The last query reads: for
Termas given, 1stargofTermisVal1, the functor ofVal1isF1with arityA1(meaning, it hasA1args - subparts - itself), and 2ndargof the term inVal1is stored underVal12name. To clarify, any symbolic data in Prolog is in the form offff(aa,bb,cc,...)wherefffis some name, called functor, and the "arguments" in that expression can be accessed through theargcall.That means that the original expression
(3*_X + 5*_Y = 10)is actually stored in Prolog as'='( '+'( '*'(3,_X), '*'(5,_Y)), 10). When you get to the atomic parts (functors with arity 0), you can check them further:EDIT: to answer your other question (from the comments):
If you insist on not writing out the multiplication sign
*explicitly, you will have to represent your terms as strings, and to analyze that string. That would be a much more involved task.EDIT: another thing to try is
=..predicate, called "Univ":