这是什么 Haskell 语法(类型级运算符?)
What is this Haskell Syntax (type level operators?)
'[]
或 ':
在 Haskell 代码中表示什么?一些例子-
data OrderPacket replies where
NoOrders :: OrderPacket '[]
data Elem :: [a] -> a -> * where
EZ :: Elem (x ': xs) x
来自 Promoted list and tuple lists 上的 Haskell 用户指南部分:
With -XDataKinds, Haskell's list and tuple types are natively promoted
to kinds, and enjoy the same convenient syntax at the type level,
albeit prefixed with a quote:
data HList :: [*] -> * where
HNil :: HList '[]
HCons :: a -> HList t -> HList (a ': t)
data Tuple :: (*,*) -> * where
Tuple :: a -> b -> Tuple '(a,b)
foo0 :: HList '[]
foo0 = HNil
foo1 :: HList '[Int]
foo1 = HCons (3::Int) HNil
foo2 :: HList [Int, Bool]
foo2 = ...
(Note: the declaration for HCons also requires -XTypeOperators because
of infix type operator (:').) For type-level lists of two or more
elements, such as the signature of foo2 above, the quote may be
omitted because the meaning is unambiguous. But for lists of one or
zero elements (as in foo0 and foo1), the quote is required, because
the types [] and [Int] have existing meanings in Haskell.
所以基本上它是相同的语法,以单引号为前缀,但在种类级别上运行。一些播放使用 ghci
和上面的代码:
λ> :t HNil
HNil :: HList '[]
λ> :t HCons
HCons :: a -> HList t -> HList (a : t)
λ> let x = 3 `HCons` HNil
λ> :t x
x :: Num a => HList '[a]
λ> let x = Tuple 3 "spj"
λ> :t x
x :: Num a => Tuple '(a, [Char])
'[]
或 ':
在 Haskell 代码中表示什么?一些例子-
data OrderPacket replies where
NoOrders :: OrderPacket '[]
data Elem :: [a] -> a -> * where
EZ :: Elem (x ': xs) x
来自 Promoted list and tuple lists 上的 Haskell 用户指南部分:
With -XDataKinds, Haskell's list and tuple types are natively promoted to kinds, and enjoy the same convenient syntax at the type level, albeit prefixed with a quote:
data HList :: [*] -> * where HNil :: HList '[] HCons :: a -> HList t -> HList (a ': t) data Tuple :: (*,*) -> * where Tuple :: a -> b -> Tuple '(a,b) foo0 :: HList '[] foo0 = HNil foo1 :: HList '[Int] foo1 = HCons (3::Int) HNil foo2 :: HList [Int, Bool] foo2 = ...
(Note: the declaration for HCons also requires -XTypeOperators because of infix type operator (:').) For type-level lists of two or more elements, such as the signature of foo2 above, the quote may be omitted because the meaning is unambiguous. But for lists of one or zero elements (as in foo0 and foo1), the quote is required, because the types [] and [Int] have existing meanings in Haskell.
所以基本上它是相同的语法,以单引号为前缀,但在种类级别上运行。一些播放使用 ghci
和上面的代码:
λ> :t HNil
HNil :: HList '[]
λ> :t HCons
HCons :: a -> HList t -> HList (a : t)
λ> let x = 3 `HCons` HNil
λ> :t x
x :: Num a => HList '[a]
λ> let x = Tuple 3 "spj"
λ> :t x
x :: Num a => Tuple '(a, [Char])