我正在尝试做一些高级类型级编程;该示例是我原始程序的简化版本。
我有(Haskell)类型的表示。在这个例子中,我只介绍了函数类型、基本类型和类型变量。
该表示Type t
由一个类型变量参数化,t
以允许在类型级别上进行区分。为此,我主要使用 GADT。不同的类型和类型变量通过使用类型级文字来区分,因此KnownSymbol
约束和使用Proxy
s。
{-# LANGUAGE GADTs, TypeOperators, DataKinds, KindSignatures, TypeFamilies, PolyKinds #-}
import GHC.TypeLits
import Data.Proxy
import Data.Type.Equality
data Type :: TypeKind -> * where
TypeFun :: Type a -> Type b -> Type (a :-> b)
Type :: KnownSymbol t => Proxy t -> Type (Ty t)
TypeVar :: KnownSymbol t => Proxy t -> Type (TyVar t)
我还通过使用 DataKinds 和 KindSignatures 扩展并定义数据类型t
来限制类型:TypeKind
TypeKind
data TypeKind =
Ty Symbol
| TyVar Symbol
| (:->) TypeKind TypeKind
现在我想实现类型变量的替换,即在一个 type 中用 type 替换每个t
等于x
type variable 的变量。替换必须在表示以及类型级别上实现。对于后者,我们需要 TypeFamilies:y
t'
type family Subst (t :: TypeKind) (y :: Symbol) (t' :: TypeKind) :: TypeKind where
Subst (Ty t) y t' = Ty t
Subst (a :-> b) y t' = Subst a y t' :-> Subst b y t'
Subst (TyVar x) y t' = IfThenElse (x == y) t' (TyVar x)
类型变量是有趣的部分,因为我们检查符号的相等性x
和y
类型级别。为此,我们还需要一个(多类)类型族,它允许我们在两个结果之间进行选择:
type family IfThenElse (b :: Bool) (x :: k) (y :: k) :: k where
IfThenElse True x y = x
IfThenElse False x y = y
不幸的是,这还没有编译,这可能是我的问题的第一个指标:
Nested type family application
in the type family application: IfThenElse (x == y) t' ('TyVar x)
(Use UndecidableInstances to permit this)
In the equations for closed type family ‘Subst’
In the type family declaration for ‘Subst’
但是,启用 UndecidableInstances 扩展是可行的,因此我们继续定义一个subst
在值级别上工作的函数:
subst :: (KnownSymbol y) => Type t -> Proxy (y :: Symbol) -> Type t' -> Type (Subst t y t')
subst (TypeFun a b) y t = TypeFun (subst a y t) (subst b y t)
subst t@(Type _) _ _ = t
subst t@(TypeVar x) y t'
| Just Refl <- sameSymbol x y = t'
| otherwise = t
这段代码运行良好,除了最后一行产生以下编译错误:
Could not deduce (IfThenElse
(GHC.TypeLits.EqSymbol t1 y) t' ('TyVar t1)
~ 'TyVar t1)
from the context (t ~ 'TyVar t1, KnownSymbol t1)
bound by a pattern with constructor
TypeVar :: forall (t :: Symbol).
KnownSymbol t =>
Proxy t -> Type ('TyVar t),
in an equation for ‘subst’
at Type.hs:29:10-18
Expected type: Type (Subst t y t')
Actual type: Type t
Relevant bindings include
t' :: Type t' (bound at Type.hs:29:23)
y :: Proxy y (bound at Type.hs:29:21)
x :: Proxy t1 (bound at Type.hs:29:18)
subst :: Type t -> Proxy y -> Type t' -> Type (Subst t y t')
(bound at Type.hs:27:1)
In the expression: t
In an equation for ‘subst’:
subst t@(TypeVar x) y t'
| Just Refl <- sameSymbol x y = t'
| otherwise = t
我想问题在于我无法证明两个符号x
和y
的类型的不等式,并且需要某种类型不等式的见证。这可能吗?还是有其他更好的方法来实现我的目标?我不知道“惯用的”Haskell 类型不等式问题和GADT 可用于证明 GHC 中的类型不等式在多大程度上?已经回答了我的问题。任何帮助,将不胜感激。