Solve the extremely simple equation in the prologue: A = B + C?

I have a very simple equation that I would like to solve in the prologue:

A = B + C

I would like to write a predicate expressing this relationship that can handle any of the arguments that are not instantiated. There is no need to generalize to more complex relationships or equations.

myEquation(A, B, C) :-
...something...

      

So that I can call the following semantics:

myEquation(A,1,2).
>    A = 3.
myEquation(3,B,2).
>    B = 1.
myEquation(3,1,C).
>    C = 2.

      

Any ideas? Working with arithmetic operators gives many "Arguments are not instantiated enough" errors. It looks like solving arbitrary systems of equations is outside the scope of most prologue implementations, but I hope this extremely simple equation is acceptable.

+3


source to share


2 answers


Not particularly interesting, but here it is. If you're not a complete beginner, you could do this too:

myEquation(A, B, C):- 
    var(A),number(B),number(C) -> A is B+C;
    var(B),number(A),number(C) -> B is A-C;
    var(C),number(A),number(B) -> C is A-B;
    A =:= B + C.

      



update: Same with programming Constraint logic:

:- use_module(library(clpq)).

myEquation(A, B, C):-
    {A = B + C}.

      

+4


source


If your domain is an integer use!

:- use_module(library(clpfd)).

:- assert(clpfd:full_answer).       % for SICStus Prolog

myEquation(A,B,C) :-
   A #= B+C.

      

Some examples of queries with , version 4.3.2:



? - myEquation (A, B, C).
B + C # = A, A in inf..sup, B in inf..sup, C in inf..sup? ;
no

? - myEquation (A, 2, C).
2 + C # = A, A in inf..sup, C in inf..sup? ;
no    

? - myEquation (X, X, X).
X + X # = X, X in inf..sup? ;
no

Let's run the same queries with , version 7.3.3:

? - myEquation (A, B, C).
B + C # = A.

? - myEquation (A, 2, C).
2 + C # = A.

? - myEquation (X, X, X).
X = 0.                               % succeeds deterministically
+1


source







All Articles