Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 514
polkovnikov.ph Avatar asked Nov 24 '14 00:11

polkovnikov.ph


1 Answers

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
like image 93
kosmikus Avatar answered Sep 20 '22 11:09

kosmikus