user2129623
user2129623

Reputation:

prolog variable not exist

Here is my simple program to test if else and function call in swi prolog. What is wrong with below code which says rule does not exist

call_rule(Roll):-
(
    member(Roll,[123]),
    writeln('inside call rule'),
    nb_getval(rule, 'this is the rule')
).
print_roll(Roll) :-
    (   Roll < 2 ->
        writeln('not a roll')
    ;
        (  Roll > 1243 ->
           writeln('not a roll'),writeln('this is 2nd alternative'),writeln('this is third alternative')
        ;
           ( Roll =:= 12 ->
                writeln('boxcars')
            ;
                ( call_rule(Roll) ->
                    nb_getval(rule, RULE),
            writeln('snake eyes')
                ;
                    nb_getval(rule,SUBJECT),
            writeln(SUBJECT)    
                )
            )
        )
    ).

result:

3 ?- print_roll(123).  
inside call rule
ERROR: nb_getval/2: variable `rule' does not exist

Upvotes: 0

Views: 1708

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

Before you can get a value from the non-backtrackable store, you need to nb_setval the key:

Example:

?- nb_getval(a,X).
ERROR: nb_getval/2: variable `a' does not exist
?- nb_setval(a,foo).
true.

?- nb_getval(a,X).
X = foo.

Upvotes: 1

Related Questions