Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm curious if Logic Programs can do algebra

I read a brief article about Prolog and Logic Programming. I'm curious if Logic Programs can do algebra. Like would you be able to ask what the Variable of X is in the equation 5+X = 7 and get an answer of -2?

like image 495
James Scourtos Avatar asked Dec 03 '12 19:12

James Scourtos


4 Answers

All serious Prolog systems provide constraint logic programming over finite domains, called CLP(FD) for short, with which you can solve many such equations easily. For example, with SICStus Prolog, SWI and Yap:

?- use_module(library(clpfd)).
true.

?- 5+X #= 7.
X = 2.

Apparently, the answer is 2 instead of -2. Also check out constraint logic programming over other domains, like the rationals with library(clpq).

like image 54
mat Avatar answered Oct 29 '22 19:10

mat


Yes, Prolog can do algebra.

If you Google for Prolog and CAS (Computer algebra system) you will get lots of results.

e.g. Using Prolog as a CAS

If you understand that Prolog = Syntactic unification + backward-chaining + REPL,

then realize that it is the unification that is the heart of solving the problems/equations, you may run into equational reasoning which is used for solving problems that contain equals (=). This same logic is also used with automated theorem provers and proof assistants.

If you look here you will find prolog.ml which implements the unification and backward-chaining in this automated theorem prover.

You should also check out term rewriting and search Google for term rewriting to learn more about the science of solving equations using terms.

like image 32
Guy Coder Avatar answered Oct 29 '22 20:10

Guy Coder


There are a few implementations of computer algebra systems in Prolog, including an equation simplifier and the PRESS equation solver. There are also several constraint solvers for linear and nonlinear equations, including CLP(R,Q) and CLP (BNR), and several other solvers that have been implemented using constraint handling rules.

Instead of implementing a computer algebra system in Prolog, it is also possible to implement a Prolog interpreter in a computer algebra system. For example, there is an article that describes an implementation of a rule-based programming system in Mathematica. There is also an implementation of logic programming system in Mathematica from the Wolfram Library Archive.

It is also apparently possible to implement logic programs in Sympy using its unification algorithm.

like image 3
Anderson Green Avatar answered Oct 29 '22 20:10

Anderson Green


How about this? Note that this will only work for X+Y=Z.

equation(X,Y,Z):- var(X),X is Z-Y.
equation(X,Y,Z):- var(Y),Y is Z-X.
equation(X,Y,Z):- var(Z),Z is X+Y.

You can ask:

equation(5,X,7).
X = 2 .
?- equation(2,5,X).
X = 7.
?- equation(X,5,7).
X = 2
like image 2
ssbarbee Avatar answered Oct 29 '22 18:10

ssbarbee