1

I'm working on defining the logical operators in Prolog as an exercise. The first few were pretty straight forward:

and(A, B) :-
    A, B.

or(true).

or(A, _) :-
    or(A).

or(_, B) :-
    or(B).

neg(false).

But now I want to define nand and nor, and it would be nice to define them in terms of my other predicates. For example, maybe something like below:

nand(A, B) :-
    neg(and(A, B)).

But the inner and expression doesn't evaluate. I'm guessing that Prolog interprets the and expression as an atom. Is there any way to force it to evaluate the nested predicate?

false
  • 10,264
  • 13
  • 101
  • 209
  • This isn't as you think it is. I am not sure your "straight forward" definitions are correct, depends what you expect from those. Could you explain better what you expect to see for different inputs? Have you tested to see if your definitions are correct? – TA_intern Jul 05 '22 at 05:29
  • Related: https://stackoverflow.com/a/19632741/ – brebs Jul 05 '22 at 07:25

1 Answers1

0
eval(true).
eval(or(A,_)):- eval(A),!.
eval(or(_,A)):- eval(A),!.
eval(and(A,B)):- eval(A),eval(B).
eval(neg(A)):- \+eval(A).

:- eval(true).
:- \+eval(and(true,false)).
:- eval(and(true,true)).
:- eval(neg(neg(true))).
:- halt.
  • I'm guessing `eval` forces Prolog to evaluate the expression and give us `true` or `false`? I'm probably not going to accept this answer without some explanation. – Andrew Lubrino Jul 05 '22 at 02:19