简体   繁体   English

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

[英]Does Haskell have kind unification?

I'm researching to what extent singleton types can simulate dependent types and I've arrived at a problem. 我正在研究单例类型可以在多大程度上模拟依赖类型,并且我遇到了一个问题。 The minimal code I replicate the error with: 最小的代码我复制错误:

{-# 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

The error message is: 错误消息是:

Expected kind 'SBool b', but ''SFalse' has kind 'SBool 'False' 期待的'SBool b',但是''SFalse'有点'SBool'False'

You need to make the dependency explicit. 您需要使依赖项显式化。 The following compiles with GHC 8.0.1. 以下编译与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

To be honest, I'm suprised by this. 说实话,我对此感到很惊讶。 I was not aware that this sort of kind-dependency was allowed at all. 我不知道这种依赖是完全允许的。

Note that this is not that different from Coq, where 请注意,这与Coq没有什么不同,其中

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

fails to compile, while 无法编译,而

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

compiles. 编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM