{"action":"created","issue":{"url":"https://api.github.com/repos/sympy/sympy/issues/8863","repository_url":"https://api.github.com/repos/sympy/sympy","labels_url":"https://api.github.com/repos/sympy/sympy/issues/8863/labels{/name}","comments_url":"https://api.github.com/repos/sympy/sympy/issues/8863/comments","events_url":"https://api.github.com/repos/sympy/sympy/issues/8863/events","html_url":"https://github.com/sympy/sympy/issues/8863","id":55009754,"number":8863,"title":"Logical operators precedence in the sympy.logic module","user":{"login":"laituan245","id":3805149,"avatar_url":"https://avatars.githubusercontent.com/u/3805149?v=3","gravatar_id":"","url":"https://api.github.com/users/laituan245","html_url":"https://github.com/laituan245","followers_url":"https://api.github.com/users/laituan245/followers","following_url":"https://api.github.com/users/laituan245/following{/other_user}","gists_url":"https://api.github.com/users/laituan245/gists{/gist_id}","starred_url":"https://api.github.com/users/laituan245/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laituan245/subscriptions","organizations_url":"https://api.github.com/users/laituan245/orgs","repos_url":"https://api.github.com/users/laituan245/repos","events_url":"https://api.github.com/users/laituan245/events{/privacy}","received_events_url":"https://api.github.com/users/laituan245/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-21T11:59:44Z","updated_at":"2016-03-03T23:57:32Z","closed_at":null,"body":"In sympy, probably the implication operators (>> and <<) have higher precedence than the AND , OR , and NOT operators. For example:\r\n\r\n``` Python\r\n>>> from sympy import *\r\n>>> false & true >> true\r\nFalse\r\n```\r\n\r\nIn this case, if the AND operator (&) had higher precedence than the implication operator (>>), the result would be True.\r\n\r\nBut the Wikipedia says that AND, OR, and NOT operators actually have higher precedence than the implication operators (http://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence).\r\n\r\nI guess the reason for this problem is because the code in boolalg.py overloads the operators | (bitwise OR) , ! (bitwise NOT), & (bitwise AND), >> (right shift), and << (left shift) of Python. And hence, it must conform to the precedence rules of Python.  And in Python, shift operators have higher precedence than other bitwise operators. That explains why in sympy, the implication operators (>> and <<) have higher precedence than the AND , OR , and NOT operators. \r\n\r\nI don't know if there is any easy fix for this. Because Python doesn't allow you to change the precedence rules (http://stackoverflow.com/questions/11811051/change-operator-precedence-in-python)"},"comment":{"url":"https://api.github.com/repos/sympy/sympy/issues/comments/192026989","html_url":"https://github.com/sympy/sympy/issues/8863#issuecomment-192026989","issue_url":"https://api.github.com/repos/sympy/sympy/issues/8863","id":192026989,"user":{"login":"asmeurer","id":71486,"avatar_url":"https://avatars.githubusercontent.com/u/71486?v=3","gravatar_id":"","url":"https://api.github.com/users/asmeurer","html_url":"https://github.com/asmeurer","followers_url":"https://api.github.com/users/asmeurer/followers","following_url":"https://api.github.com/users/asmeurer/following{/other_user}","gists_url":"https://api.github.com/users/asmeurer/gists{/gist_id}","starred_url":"https://api.github.com/users/asmeurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asmeurer/subscriptions","organizations_url":"https://api.github.com/users/asmeurer/orgs","repos_url":"https://api.github.com/users/asmeurer/repos","events_url":"https://api.github.com/users/asmeurer/events{/privacy}","received_events_url":"https://api.github.com/users/asmeurer/received_events","type":"User","site_admin":false},"created_at":"2016-03-03T23:57:32Z","updated_at":"2016-03-03T23:57:32Z","body":"There's no way to change operator precedence in Python, unfortunately. This is a pitfall to overriding >> unfortunately, and the only way around it is to either avoid >> (use `Implies`), or to remember to always parenthesize things. \r\n\r\nSymPy already assumes that AND and IMPLIES have no implied relative precedence in its pretty printer:\r\n\r\n```\r\nIn [1]: And(x, Implies(y, z))\r\nOut[1]: x ∧ (y → z)\r\n\r\nIn [2]: Implies(And(x, y), z)\r\nOut[2]: (x ∧ y) → z\r\n```\r\n\r\nI suppose if you work with these symbols enough it might be convenient to assume precedence so that you don't need so many parentheses (and AND higher than IMPLIES probably makes the most sense). Personally, when I see `x ∧ y → z` I'm not quite sure what it should mean.\r\n\r\nIt's also worth pointing out that the inequality operators (`<`, `<=`, `>`, `>=`) do have lower precedence than `&` and `|` (https://docs.python.org/3/reference/expressions.html#operator-precedence). I'm not sure if it's worth using, them, though, given their usual meaning for normal expressions. \r\n\r\nAt any rate, we should document this. "}}