Message Boards Message Boards

0
|
414 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

What is the precedence of logical operators in Wolfram Language

Hello. 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.

POSTED BY: Thomas Barson
5 Replies

This is just a guess. I would expect that in the absence of explicit statements to the contrary, Nand will have the same precedence as And, and Nor, Xor the same as Or. This is by analogy with arithmetic placing Subtract with Plus and Quotient with Times.

POSTED BY: Daniel Lichtblau
Posted 2 days ago

There is a function, Precedence, which returns the precedence of functions. For some reason it is not documented.

POSTED BY: Hans Milton

Thanks for the response.

Your result is interesting because another search led me to the property "PrecedenceRanks", accessed by the WolframLanguageData[] function. So

WolframLanguageData["Xor", "PrecedenceRanks"]

returns 59, while "And" and "Nand" return 58, and "or" and "Nor" return 60.

So there are two differents raking mechansisms -- and they don't agree! :-)

POSTED BY: Thomas Barson

Thanks for the response! This is what I thought,also, although if you pull "PrecedenceRanks" (see my comment to the other response to this question), it shows Xor and Xnor below And and Nand and above Or and Nor.

POSTED BY: Thomas Barson

Thanks for the response. From some other searches, it turns out that their is a WL property called "PrecedenceRanks" which can be queried from WolframLanguageData[]. It also turns out that applying FullForm or TreeForm to the input expression reveals the precedence. All good things to know!

POSTED BY: Thomas Barson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract