9
$\begingroup$

I'm taking the enjoyable (but very high-level) Wolfram U course in Discrete Mathematics. That course gives the logical operator precedence as ()>\[Not]>\[And]>\[Or]>\[Implies]>\[DoubleLeftRightArrow] but doesn't mention the NAND, NOR, or XOR operators in this context.

We are given the exercise to convert to CNF the expression p \[Xor] q \[Nor] r \[Nand] r \[Or] p \[And] q \[Or s \[Nand] r.

All we are expected to do is use BooleanConvert[] to reach the answer (! p || q || ! r || ! s) && (p || ! q || ! r || ! s)

What I want to do is to replicate the conversion manually. To do this I need to know the operator precedence, which I can't find in the WL documentation (not saying it isn't there) and which all my wider searches insist is language-specific. I think this is equivalent to asking whether, absent parentheses, BooleanConvert[] proceeds left to right or follows some other rule.

$\endgroup$
4

2 Answers 2

13
$\begingroup$

More of a pedagogical comment than a proper answer...

Valid WL code is just an expression with very simple syntax similar to Lisp M-expressions. This syntax makes precedence rules unnecessary. There is a variety of syntactic sugar allowed when inputting expressions, but every input gets translated to a fully formed WL expression before evaluation. So, you can always see the precedence by just looking at the full WL expression. If you just evaluate your logical expression, you can see how precedence was applied.

p \[Xor] q \[Nor] r \[Nand] r \[Or] p \[And] q \[Or] s \[Nand] r
(* ((p \[Xor] q) \[Nor] (r \[Nand] r)) || (p && q) || (s \[Nand] r) *)

FullForm might be even clearer:

p \[Xor] q \[Nor] r \[Nand] r \[Or] p \[And] q \[Or] s \[Nand] r // FullForm
(* Or[Nor[Xor[p, q], Nand[r, r]], And[p, q], Nand[s, r]] *)
$\endgroup$
3
  • 2
    $\begingroup$ And if the brackets of FullForm seem too much, you can try TreeForm $\endgroup$ Commented Feb 9 at 21:47
  • $\begingroup$ I upvoted both you and @Syed (after all, they did answer the question) ... but I like the pedagogy far more. $\endgroup$ Commented Feb 10 at 11:16
  • $\begingroup$ Thanks, @ lericr, for the response (which I had to ponder a bit) and the useful examples, whose results are completely consistent with the `WolframLanguageData[operator,"PrecedenceRanks"] approach proposed by @ Syed in another response below. $\endgroup$ Commented Feb 10 at 12:58
10
$\begingroup$
WolframLanguageData[#, "PrecedenceRanks"] & /@ {"Not", "And", "Or", 
  "Implies", "Nand", "Nor", "Xor", "Xnor"}

$\endgroup$
1
  • $\begingroup$ Thanks, @Syed, this was very useful. I learned from @lericr 's response that applying FullForm to the expression will reveal the precedence also, but it's also good to get the big picture and knowing about "PrecedenceRanks" gives access to it. $\endgroup$ Commented Feb 10 at 13:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.