启用语言扩展程序TypeOperators
后,可以定义自己的类型运算符。此外,可以使用infix*
设置其相对优先级。但是,例如(->)
的优先级是什么?
> :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'
答案 0 :(得分:15)
以下是compiler/basicTypes/BasicTypes.lhs
中GHC来源的相关位:
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 '->'
因此->
的固定性为infixr 0
。
您还可以从错误消息中推断出这一点。创建以下Haskell源文件:
{-# LANGUAGE TypeOperators #-}
data a // b
infixl 0 //
然后:
GHCi> :kind Int // Int -> Int
<interactive>:1:5:
Precedence parsing error
cannot mix ‘//’ [infixl 0] and ‘(->)’ [infixr 0] in the same infix expression