Haskell 键入族以检查有效性而不是可满足性
考虑以下代码Haskell 键入族以检查有效性而不是可满足性,haskell,type-families,type-variables,Haskell,Type Families,Type Variables,考虑以下代码 {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} module Test () where import Data.Proxy import Data.Void import GHC.TypeLits da
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Test () where
import Data.Proxy
import Data.Void
import GHC.TypeLits
data N = S N | Z
type family Index e l where
Index e (Either e x) = 'Z
Index e (Either e' y) = S (Index e y)
Index e Void = TypeError ('Text " not found")
将此加载到GHCi上我运行以下命令
λ> :set -XRankNTypes
λ> :kind! forall a . Index a (Either a Void)
forall a . Index a (Either a Void) :: N
= 'Z
λ> :kind! forall a . Index a (Either a (Either Int Void))
forall a . Index a (Either a (Either Int Void)) :: N
= 'Z
λ> :kind! forall a . Index a (Either Int (Either a Void))
forall a . Index a (Either Int (Either a Void)) :: N
= Index a (Either Int (Either a Void))
λ> :kind! Index Char (Either Int (Either Char Void))
Index Char (Either Int (Either Char Void)) :: N
= 'S 'Z
现在很明显,GHC不计算索引e(Int或y)=
S(索引ey)
因为e
可能是Int
所以它就停止了。有没有
通用量化e
的方法,这样如果e~Int
不起作用
对于所有e
,然后继续?理想情况下,我希望:
λ> :kind! forall a . Index a (Either Int (Either Char Void))
forall a . Index a (Either Int (Either Char Void)) :: N
= 'S ('S (TypeError ...))
您能否提供更多关于为什么需要此功能的上下文,以便我们了解什么样的工作环境是可以接受的?另一种类型的族怎么样:
类型族Elem e l::Bool
?然后对所有a进行。(Elem a l~False)=>…
实际上会对相同的类型进行量化,但不清楚它是否适用于您的目的。如果您使用skolem类型(例如data a
)而不是类型变量,这可能会起作用,具体取决于您的需要。也就是说,只需用“新鲜”的具体类型实例化类型变量。也许你想要像?@chi@li yao xia这样看起来很有希望的东西,但不清楚我如何能够编写一个类型函数,它将用于所有类型。索引Int(或a(或Int(或Char Void))
评估到sz
@李耀霞这个问题主要是为了引发围绕这个主题的讨论。这是我多次遇到的问题,我想知道人们对它有什么看法。它不局限于特定的用例。