haskell - () 作为空约束

标签 haskell constraint-kinds

如何表示空约束?

对于以下文件

{-# 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 * or Constraint, depending on context, never a -> Constraint. Similarly (,) has kind * -> * -> * or Constraint -> Constraint -> Constraint, depending on context.

Simon Peyton-Jones

只是 () 仅针对类型或约束进行重载。也就是说,您应该编写 () => a,而不是 (() a) => a。所以我认为:

class Empty x
instance Empty x

这里是正确的解决方案。 (也许类似的东西应该在 base 中。)

关于haskell - () 作为空约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50261658/

相关文章:

haskell - 用 char 或 int 拆分字符串/列表

haskell - 是否有一个库使用 ConstraintKinds 来概括所有基类型类以允许约束?

haskell - Haskell在记录字段中的保留关键字如何处理?

arrays - 使用 Data.Vector 进行动态编程

windows - Windows 上 Haskell 的彩色外壳?

haskell - 构建 Spock 教程示例失败

haskell - 什么时候(如果有的话)可以部分应用类型同义词?

haskell - 受限异构列表

haskell - 是否有 "trivial constraint"或 "object class"的标准实现?

haskell - 约束种类: Pass multiple constraints