Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the precedence of ~, and why?

Tags:

haskell

The following snippet compiles:

{-# LANGUAGE TypeFamilies #-}
type family Foo a b

f :: (Foo a b ~ Int) => a -> b -> b
f = error ""

but infix type operators seem to behave differently:

{-# LANGUAGE TypeFamilies #-}
type family a \\ b

f :: (a \\ b ~ Int) => a -> b -> b
f = error ""

GHC complains that the second argument to \\ should have kind *, but b ~ Int has kind Constraint. Of course this can be fixed with parens, but I'm wondering if there's another way.

I've tried setting the precedence of my operator with the fixity declaration infixl 9 \\, but that doesn't fix the problem, indicating that the precedence of ~ is at least 9 (if I'm interpreting that correctly). I tried using the trick from this answer to make GHCi tell me the precedence of ~, but it didn't work.

Technically speaking, ~ probably isn't a type operator, it's more of a lexical construct similar to ,, but the questions still stand:

  1. Why do infix and prefix operators exhibit different behavior?
  2. Why does ~ bind so tightly?
  3. Is there something I can do to make my own operators bind even tighter?

(Note: This question asks about the precedence of type functions, but it doesn't say anything about ~.)

like image 571
crockeea Avatar asked Jan 31 '15 23:01

crockeea


People also ask

What is rule of precedence?

What Does Precedence Mean? Precedence, in C#, is the rule that specifies the order in which certain operations need to be performed in an expression. For a given expression containing more than two operators, it determines which operations should be calculated first.

What is the correct precedence order?

It stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. PEMDAS is often expanded to the mnemonic "Please Excuse My Dear Aunt Sally" in schools.

What is the precedence of arithmetic operators?

When there is more than one arithmetic operator in an expression; multiplication, division, and modulo are calculated first, followed by subtraction and addition. If all arithmetic operators in an expression have the same level of precedence, the order of execution is left to right.

What is the priority among (* (+ -) and (=) C operators?

1) What is the Priority among (*, /, %), (+, -) and (=) C Operators.? Explanation: Assignment operator in C has the least priority.


1 Answers

~ isn't an operator, it's a keyword, like module or case, so I think you could only alter the precedence using parentheses.

See more here: https://wiki.haskell.org/Keywords#.7E

like image 191
lightandlight Avatar answered Nov 15 '22 23:11

lightandlight