polkovnikov.ph
polkovnikov.ph

Reputation: 6632

Haskell type operator precedence

When the language extension TypeOperators is enabled, it's possible to define own type operators. Also, it's possible to set their relative precedence with infix*. But what's the precedence of (->), for example?

> :i (->)
data (->) a b   -- Defined in `GHC.Prim'
instance Monad ((->) r) -- Defined in `GHC.Base'
instance Functor ((->) r) -- Defined in `GHC.Base'
instance Applicative ((->) a) -- Defined in `Control.Applicative'
instance Arrow (->) -- Defined in `Control.Arrow'
instance Monoid b => Monoid (a -> b) -- Defined in `Data.Monoid'
instance ArrowLoop (->) -- Defined in `Control.Arrow'
instance ArrowChoice (->) -- Defined in `Control.Arrow'
instance ArrowApply (->) -- Defined in `Control.Arrow'

Upvotes: 18

Views: 1643

Answers (1)

kosmikus
kosmikus

Reputation: 19637

Here are the relevant bits of the GHC sources in compiler/basicTypes/BasicTypes.lhs:

maxPrecedence, minPrecedence :: Int
maxPrecedence = 9
minPrecedence = 0
defaultFixity :: Fixity
defaultFixity = Fixity maxPrecedence InfixL
negateFixity, funTyFixity :: Fixity
-- Wired-in fixities
negateFixity = Fixity 6 InfixL  -- Fixity of unary negate
funTyFixity  = Fixity 0 InfixR  -- Fixity of '->'

So the fixity of -> is infixr 0.

You can also infer this from an error message. Create the following Haskell source file:

{-# LANGUAGE TypeOperators #-}
data a // b
infixl 0 //

Then:

GHCi> :kind Int // Int -> Int

<interactive>:1:5:
    Precedence parsing error
        cannot mix ‘//’ [infixl 0] and ‘(->)’ [infixr 0] in the same infix expression

Upvotes: 18

Related Questions