prologinstantiation-error

Can't check equality with unknowns in Prolog


When trying with a simple program to find numbers that multiply to make 100, I can check for set values, like prod_hundred(2, 50), but if I want to find a value with prod_hundred(4, X), I get :

Arguments are not sufficiently instantiated
In:
   [1] 4*_1680=:=100

I know this is because I can't use '=:=' to evaluate with unknowns, but with == it just compares "2*50" to "100", instead of evaluating 2*50

Code:

prod_hundred(X, Y) :- X*Y =:= 100.

?- prod_hundred(4, X).

Arguments are not sufficiently instantiated
In:
   [1] 2*_1680=:=100

Solution

  • "Solve for X" is harder than evaluating 2*50 to get the result; it needs math knowledge of how to rearrange the equation to 100/4 = X. Classic Prolog doesn't have that built in, you would have to code it yourself.

    But that kind of thing is in newer constraint solver libraries such as clpfd in SWI Prolog which gives you #= and can solve by finding integer answers to number problems:

    :- use_module(library(clpfd)).
    
    prod_hundred(X, Y) :- 
        X*Y #= 100.
    

    Then:

    ?- prod_hundred(4, X).
    X = 25