哈斯克尔是否有善意的统一?

时间:2017-11-10 19:12:55

标签: haskell dependent-type higher-kinded-types data-kinds singleton-type

我正在研究单例类型可以在多大程度上模拟依赖类型,并且我遇到了一个问题。最小的代码我用以下方法复制错误:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeInType #-}

import Data.Kind(Type)

data SBool :: Bool -> Type where
  STrue :: SBool 'True
  SFalse :: SBool 'False

data SSBool :: SBool b -> Type where
  SSFalse :: SSBool 'SFalse
  SSTrue  :: SSBool 'STrue

错误消息是:

  

预期种类'SBool b',但''SFalse'有点'SBool'错误'

1 个答案:

答案 0 :(得分:6)

您需要明确依赖关系。以下编译与GHC 8.0.1。

import Data.Kind(Type)

data SBool :: Bool -> Type where
  STrue :: SBool 'True
  SFalse :: SBool 'False

data SSBool :: forall (b :: Bool) . SBool b -> Type where
  SSFalse :: SSBool 'SFalse
  SSTrue  :: SSBool 'STrue

说实话,我对此感到很惊讶。我根本不知道这种依赖性是完全允许的。

请注意,这与Coq没有什么不同,其中

Inductive SSBool (b: bool) : SBool b -> Type :=
  | SSFalse : SSBool SFalse
  | SSTrue  : SSBool STrue
.

无法进行编译,而

Inductive SSBool : forall (b: bool), SBool b -> Type :=
  | SSFalse : SSBool false SFalse
  | SSTrue  : SSBool true STrue
.

编译。