Haskell类型运算符优先级 [英] Haskell type operator precedence
问题描述
当启用语言扩展 TypeOperators
时,可以定义自己的类型操作符。另外,可以使用 infix *
来设置它们的相对优先级。但是,例如( - >)
的优先级是多少?
> ( - >)
数据( - >)ab - 在'GHC.Prim'中定义
实例Monad(( - >)r) - 定义于`GHC.Base'
实例Functor(( - >)r) - 在`GHC.Base'中定义
实例应用(( - >)a) - 在Control.Applicative中定义
实例箭头( - >) - 在Control.Arrow中定义
实例Monoid b => Monoid(a - > b) - 在Data.Monoid中定义
实例ArrowLoop( - >) - 在Control.Arrow中定义
实例ArrowChoice( - >) - 在`Control.Arrow'中定义
实例ArrowApply( - >) - 在Control.Arrow中定义
以下是 compiler / basicTypes / BasicTypes.lhs
中GHC源代码的相关位:
maxPrecedence,minPrecedence :: Int
maxPrecedence = 9
minPrecedence = 0
defaultFixity :: Fixity
defaultFixity =固定maxPrecedence InfixL
negateFixity,funTyFixity ::固定
- 有线固定
negateFixity =固定6 InfixL - 固定一元否定
funTyFixity =固定0 InfixR - 固定' - >'
所以 - >
是 infixr 0
。
您也可以从错误信息。创建下面的Haskell源文件:
$ $ p $ { - #LANGUAGE TypeOperators# - }
data a // b
infixl 0 //
然后:
GHCI> :kind Int // Int - > Int
< interactive>:1:5:
优先解析错误
不能混合'//'[infixl 0]和'( - >)'[infixr 0 ]在相同的中缀表达式
中
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'
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
这篇关于Haskell类型运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!