Prolog Jugs Solution

3.7k views Asked by At

I have the current solution for the jugs problem in Prolog but I am having a problem in how to run the program. Can someone help me please. Thanks in advance.

Error Undefined procedure: state/1,

However, there are definitions for: state/2

 database
        rstate(integer,integer)
    predicates
        state(integer,integer)

clauses

    state(2,_).
    state(0,0):-
        not(rstate(0,0)),
        assert(rstate(0,0)),    
        state(0,0).
    state(X,Y):-        
        X < 4,
        not(rstate(4,Y)),
        assert(rstate(4,Y)),
        write("\n Rule 1 => (4,",Y,")"),
        state(4,Y).
    state(X,Y):-
        Y < 3,
        not(rstate(X,3)),
        assert(rstate(X,3)),
        write("\n Rule 2 => (",X,",3)"),
        state(X,3).
    state(X,Y):-      
        X>0,
        not(rstate(0,Y)),
        assert(rstate(0,Y)),
        write("\n Rule 5 => (0,",Y,")"),
        state(0,Y).
    state(X,Y):-       
        Y>0,
        not(rstate(X,0)),
        assert(rstate(X,0)),
        write("\n Rule 6 => (",X,",0)"),
        state(X,0).
    state(X,Y):-
        X+Y >= 4,
        Y > 0,
        Z=Y-(4-X),
        not(rstate(4,Z)),
        assert(rstate(4,Z)),
        write("\n Rule 7 => (4,",Z,")"),
        state(4,Z).
    state(X,Y):-
        X+Y >= 3,
        X>0,
        Z=X-(3-Y),
        not(rstate(Z,3)),
        assert(rstate(Z,3)),
        write("\n Rule 8 => (",Z,",3)"),
        state(Z,3).
    state(X,Y):-
        X+Y <= 4,
        Y > 0,
        Z=X+Y,
        not(rstate(Z,0)),
        assert(rstate(Z,0)),
        write("\n Rule 9 => (",Z,",0)"),
        state(Z,0).
    state(X,Y):-
        X+Y <= 3,
        X>0,
        Z=X+Y,
        not(rstate(0,Z)),
        assert(rstate(0,Z)),
        write("\n Rule 10 => (0,",Z,")"),
        state(0,Z).
    state(X,Y):-
        X=0,
        Y=2,
        not(rstate(2,0)),
        assert(rstate(2,0)),
        write("\n Rule 11 => (2,0)"),
        state(2,0).
    state(X,Y):-
        X=2,
        assert(rstate(0,Y)),
        write("\n Rule 12 => (0,",Y,")"),
        state(0,Y).

goal
    makewindow(1,2,3,"Water Jug Problem",0,0,25,80),
    write("Initially state(0,0)"),
    state(0,0),
    save("output.txt").

OUTPUT

Initially state(0,0)
 Rule 1 => (4,0)
 Rule 2 => (4,3)
 Rule 5 => (0,3)
 Rule 9 => (3,0)
 Rule 2 => (3,3)
 Rule 7 => (4,2)
 Rule 5 => (0,2)
 Rule 9 => (2,0)
Press the SPACE bar

    output.txt

rstate(0,0)
rstate(4,0)
rstate(4,3)
rstate(0,3)
rstate(3,0)
rstate(3,3)
rstate(4,2)
rstate(0,2)
rstate(2,0)
1

There are 1 answers

4
Shevliaskovic On

Hint:

Error Undefined procedure: state/1

means that you tried to execute a state query with only one argument, like state(12).

All the state rules you have defined have 2 arguments, like state(2,_)., state(0,0).and state(X,Y).

You have not defined anything like state(X)., so when you tried to execute that query, you got the undefined procedure error.

You will have to execute a state query with two arguments (that's what state/2 means) -> you need two numbers in the parentheses; execute something like state(0,2).