()作为空约束

lib*_*ako 3 haskell constraint-kinds

怎么能代表空约束呢?

对于以下文件

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE KindSignatures #-}

import Data.Kind(Type, Constraint)

type Empty = (() :: Type -> Constraint)

main :: IO ()
main = return ()
Run Code Online (Sandbox Code Playgroud)

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)
  |  
Run Code Online (Sandbox Code Playgroud)

我想念什么?

我知道以下解决方案

{-# LANGUAGE FlexibleInstances #-}

class Empty x
instance Empty x
Run Code Online (Sandbox Code Playgroud)

但我想知道为什么()不起作用

Jon*_*rdy 5

()善良*Constraint依赖于背景,永远不会a -> Constraint.同样(,)具有种类* -> * -> *Constraint -> Constraint -> Constraint取决于上下文.

- 西蒙佩顿琼斯

它只是因为()它是一个类型还是一个约束而被重载.也就是说,你写的() => a不是(() a) => a.所以我想这个:

class Empty x
instance Empty x
Run Code Online (Sandbox Code Playgroud)

这是正确的解决方案.(也许这样的东西应该在base.)