3

I'm writing a resolution prover in prolog, which takes inputs of the form

[[(a or b) equiv neg(c and d)]]

etc., and converts them into CNF. All logical operators work fine, but when the program attempts to expand an equiv operation, brackets go missing. For example, performing one step of the algorithm on

[[(a and b) equiv c]]

gives the result

[[neg(a and b) or c], [neg c or a and b]]

In the second clause, the brackets around a and b have been deleted, while they remain in the first clause. Why is this happening?

For reference, this is my code and the command giving that output is singlestep([[(a and b) equiv c]], X). The specific code for how equiv should be dealt with is at line 93, and the function singlestep is at line 108.

  • That probably is because of the precedence you defined in your operators, since then these brackets are redundant. – Willem Van Onsem May 08 '20 at 17:45
  • All operators have the same precedence, except `neg` which is higher. So surely brackets are required when operators are of the same precedence? – Nathan Stubbings May 08 '20 at 18:05
  • Well, the right one is then left-to-right so `(((neg c) or a) and b)`. – Willem Van Onsem May 08 '20 at 18:07
  • @WillemVanOnsem: I think your comment is slightly technically incorrect. When I query `F=(neg c or a and b), F =.. G`, I obtain as a result `G = [or, neg c, a and b]`. Consequently, the expression with parenthesis is `(neg c) or (a and b)`. Consequently, the code by Nathan seems to be correct. In general, if you use Prolog operators, the Prolog system will care about the parenthesis. – tphilipp May 08 '20 at 19:28
  • @tphilipp: arrghh yes, it goes in the opposite direction, sorry, somehow mixed it up. Thanks. – Willem Van Onsem May 08 '20 at 19:30
  • I like to point out some related questions: [Prolog infix operator definition](https://stackoverflow.com/questions/9340792/prolog-infix-operator-definition/9340873#9340873) and [Controlling parenthesis while working with operators](https://stackoverflow.com/questions/15175400/controlling-parenthesis-while-working-with-operators) – tphilipp May 08 '20 at 19:35

1 Answers1

0

This is happening because the operator precedence is defined in such a way that

neg c or (a and b)

and

neg c or a and b

are equivalent. You do not include your operator declaration, but they are likely to be both xfy with equal priority.

To test the equivalence, you could try simple pattern-matching:

?- neg c or a and b = X or Y.
X = neg c,
Y = a and b?

?- neg c or a and b = X and Y.
no.

Confirming how operators are interpretated.

boisvert
  • 3,679
  • 2
  • 27
  • 53