我正在尝试做一些高级的类型级编程;该示例是我原始程序的简化版本。
我有(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
现在我想实现类型变量的替换,即在类型t
内替换等于类型变量x
的每个变量y
,类型为{{1 }}。替换必须在表示上以及在类型级别上实现。对于后者,我们需要TypeFamilies:
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
的相等性。为此,我们还需要一个(poly-kinded)类型族,它允许我们在两个结果之间进行选择:
y
不幸的是,这还没有编译,这可能是我问题的第一个指标:
type family IfThenElse (b :: Bool) (x :: k) (y :: k) :: k where
IfThenElse True x y = x
IfThenElse False x y = y
启用UndecidableInstances扩展可以正常工作,因此我们继续定义一个适用于价值级别的函数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’
:
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
的类型的不等式,并且需要某种类型不等式见证。这可能吗?或者是否有另一种更好的方法来实现我的目标?
我不知道问题'idiomatic' Haskell type inequality和Can GADTs be used to prove type inequalities in GHC?在多大程度上已经回答了我的问题。任何帮助将不胜感激。
答案 0 :(得分:2)
正如志在评论中所说,你需要的是Either ((x==y) :~: True) ((x==y) :~: False)
。不幸的是,类型文字当前部分被破坏了,这是我们只能用unsafeCoerce
做的事情之一(尽管在道德上可以接受)。
sameSymbol' ::
(KnownSymbol s, KnownSymbol s') =>
Proxy s -> Proxy s'
-> Either ((s == s') :~: True) ((s == s') :~: False)
sameSymbol' s s' = case sameSymbol s s' of
Just Refl -> Left Refl
Nothing -> Right (unsafeCoerce Refl)
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' = case sameSymbol' x y of
Left Refl -> t'
Right Refl -> t
另一方面,如果你不介意一些模板Haskell,singletons
库可以推导出你的定义(以及更多):
{-# language GADTs, TypeOperators, DataKinds, KindSignatures, TypeFamilies, PolyKinds #-}
{-# language UndecidableInstances, ScopedTypeVariables, TemplateHaskell, FlexibleContexts #-}
import GHC.TypeLits
import Data.Singletons.TH
import Data.Singletons.Prelude
singletons([d|
data Type sym
= Ty sym
| TyVar sym
| Type sym :-> Type sym
subst :: Eq sym => Type sym -> sym -> Type sym -> Type sym
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' = if x == y then t' else TyVar x
|])
这为我们提供了Type
和subst
的类型,种类和价值级别定义。例子:
-- some examples
-- type level
type T1 = Ty "a" :-> TyVar "b"
type T2 = Subst T1 "b" (Ty "c") -- this equals (Ty "a" :-> Ty "c")
-- value level
-- automatically create value-level representation of T1
t1 = sing :: Sing T1
-- or write it out by hand
t1' = STy (sing :: Sing "a") :%-> STyVar (sing :: Sing "b")
-- use value-level subst on t1:
t2 = sSubst t1 (sing :: Sing "b") (STy (sing :: Sing "c"))
-- or generate result from type-level representation
t2' = sing :: Sing (Subst T1 "b" (Ty "c"))
-- Convert singleton to non-singleton (and print it)
t3 :: Type String
t3 = fromSing t2 -- Ty "a" :-> Ty "c"