如何表示空约束?
对于以下文件
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE KindSignatures #-}
import Data.Kind(Type, Constraint)
type Empty = (() :: Type -> Constraint)
main :: IO ()
main = return ()
ghc 8.2.2 答案
constraint.hs:6:15: error:
• Expected kind ‘* -> Constraint’, but ‘()’ has kind ‘*’
• In the type ‘(() :: Type -> Constraint)’
In the type declaration for ‘Empty’
|
6 | type Empty = (() :: Type -> Constraint)
|
我想念什么?
我知道以下解决方案
{-# LANGUAGE FlexibleInstances #-}
class Empty x
instance Empty x
但我想知道为什么 () 不起作用
最佳答案
()
has kind*
orConstraint
, depending on context, nevera -> Constraint
. Similarly(,)
has kind* -> * -> *
orConstraint -> Constraint -> Constraint
, depending on context.
只是 ()
仅针对类型或约束进行重载。也就是说,您应该编写 () => a
,而不是 (() a) => a
。所以我认为:
class Empty x
instance Empty x
这里是正确的解决方案。 (也许类似的东西应该在 base
中。)
关于haskell - () 作为空约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50261658/