出于教育目的,我一直在尝试通过使用各种语言扩展和单例类型来重构Haskell中的"使用Idris进行类型驱动开发"(即RemoveElem.idr)一书中的示例.它的要点是一个从非空向量中移除元素的函数,给出了元素实际上在向量中的证明.以下代码是自包含的(GHC 8.4或更高版本).问题出现在最后:
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeInType #-}
import Data.Kind
import Data.Type.Equality
import Data.Void
-- | Inductively defined natural numbers.
data Nat = Z | S Nat deriving (Eq, Show)
-- | Singleton types for natural numbers.
data SNat :: Nat -> Type where
SZ :: SNat 'Z
SS :: SNat n -> SNat ('S n)
deriving instance Show (SNat n)
-- | "Demote" a singleton-typed natural number to an ordinary 'Nat'.
fromSNat :: SNat n -> Nat
fromSNat SZ = Z
fromSNat (SS n) = S (fromSNat n)
-- | A decidable proposition.
data Dec a = Yes a | No (a -> Void)
-- | Propositional equality of natural numbers.
eqSNat :: SNat a -> SNat b -> Dec (a :~: b)
eqSNat SZ SZ = Yes Refl
eqSNat SZ (SS _) = No (\case {})
eqSNat (SS _) SZ = No (\case {})
eqSNat (SS a) (SS b) = case eqSNat a b of
No f -> No (\case Refl -> f Refl)
Yes Refl -> Yes Refl
-- | A length-indexed list (aka vector).
data Vect :: Nat -> Type -> Type where
Nil :: Vect 'Z a
(:::) :: a -> Vect n a -> Vect ('S n) a
infixr 5 :::
deriving instance Show a => Show (Vect n a)
-- | @Elem a v@ is the proposition that an element of type @a@
-- is contained in a vector of type @v@. To be useful, @a@ and @v@
-- need to refer to singleton types.
data Elem :: forall a n. a -> Vect n a -> Type where
Here :: Elem x (x '::: xs)
There :: Elem x xs -> Elem x (y '::: xs)
deriving instance Show a => Show (Elem a v)
------------------------------------------------------------------------
-- From here on, to simplify things, only vectors of natural
-- numbers are considered.
-- | Singleton types for vectors of 'Nat's.
data SNatVect :: forall n. Nat -> Vect n Nat -> Type where
SNatNil :: SNatVect 'Z 'Nil
SNatCons :: SNat a -> SNatVect n v -> SNatVect ('S n) (a '::: v)
deriving instance Show (SNatVect n v)
-- | "Demote" a singleton-typed vector of 'SNat's to an
-- ordinary vector of 'Nat's.
fromSNatVect :: SNatVect n v -> Vect n Nat
fromSNatVect SNatNil = Nil
fromSNatVect (SNatCons a v) = fromSNat a ::: fromSNatVect v
-- | Decide whether a value is in a vector.
isElem :: SNat a -> SNatVect n v -> Dec (Elem a v)
isElem _ SNatNil = No (\case {})
isElem a (SNatCons b as) = case eqSNat a b of
Yes Refl -> Yes Here
No notHere -> case isElem a as of
Yes there -> Yes (There there)
No notThere -> No $ \case
Here -> notHere Refl
There there -> notThere there
type family RemoveElem (a :: Nat) (v :: Vect ('S n) Nat) :: Vect n Nat where
RemoveElem a (a '::: as) = as
RemoveElem a (b '::: as) = b '::: RemoveElem a as
-- | Remove a (singleton-typed) element from a (non-empty, singleton-typed)
-- vector, given a proof that the element is in the vector.
removeElem :: forall (a :: Nat) (v :: Vect ('S n) Nat)
. SNat a
-> Elem a v
-> SNatVect ('S n) v
-> SNatVect n (RemoveElem a v)
removeElem x prf (SNatCons y ys) = case prf of
Here -> ys
There later -> case ys of
SNatNil -> case later of {}
SNatCons{} -> SNatCons y (removeElem x later ys)
-- ^ Could not deduce:
-- RemoveElem a (y '::: (a2 '::: v2))
-- ~ (y '::: RemoveElem a (a2 '::: v2))
显然,类型系统需要说服值的类型x
并且y
在代码的该分支中不可能相等,因此可以明确地使用类型族的第二个等式来根据需要减少返回类型.我不知道该怎么做.天真地,我希望构造函数There
和模式匹配There later
来携带/揭示GHC类型不等式的证明.
以下是一个明显冗余和部分的解决方案,它只是演示了GHC对递归调用进行类型检查所需的类型不等式:
SNatCons{} -> case (x, y) of (SZ, SS _) -> SNatCons y (removeElem x later ys) (SS _, SZ) -> SNatCons y (removeElem x later ys)
现在,例如,这有效:
?> let vec = SNatCons SZ (SNatCons (SS SZ) (SNatCons SZ SNatNil)) ?> :t vec vec :: SNatVect ('S ('S ('S 'Z))) ('Z '::: ('S 'Z '::: ('Z '::: 'Nil))) ?> let Yes prf = isElem (SS SZ) vec ?> :t prf prf :: Elem ('S 'Z) ('Z '::: ('S 'Z '::: ('Z '::: 'Nil))) ?> let vec' = removeElem (SS SZ) prf vec ?> :t vec' vec' :: SNatVect ('S ('S 'Z)) ('Z '::: ('Z '::: 'Nil)) ?> fromSNatVect vec' Z ::: (Z ::: Nil)解析度
正如在@ chi的评论中暗示并在HTNW的答案中详细阐述的那样,我试图通过removeElem
使用上述类型签名和类型系列来解决错误的问题,如果我能够这样做,那么所得到的程序将是错误的类型.
以下是我根据HTNW的答案所做的更正(您可能需要在继续阅读之前阅读).
第一个错误,或不必要的并发症,是重复SNatVect
s类型的向量的长度.我觉得有必要写fromSNatVect
,但肯定不是:
data SNatVect (v :: Vect n Nat) :: Type where SNatNil :: SNatVect 'Nil SNatCons :: SNat a -> SNatVect v -> SNatVect (a '::: v) deriving instance Show (SNatVect v) fromSNatVect :: forall (v :: Vect n Nat). SNatVect v -> Vect n Nat -- implementation unchanged
现在有两种写作方法removeElem
.第一个接受a Elem
,an SNatVect
并返回Vect
:
removeElem :: forall (a :: Nat) (n :: Nat) (v :: Vect ('S n) Nat) . Elem a v -> SNatVect v -> Vect n Nat removeElem prf (SNatCons y ys) = case prf of Here -> fromSNatVect ys There later -> case ys of SNatNil -> case later of {} SNatCons{} -> fromSNat y ::: removeElem later ys
第二接受一个SElem
,一个SNatVect
并返回SNatVect
,使用RemoveElem
型家族,反映了值级函数:
data SElem (e :: Elem a (v :: Vect n k)) where SHere :: forall x xs. SElem ('Here :: Elem x (x '::: xs)) SThere :: forall x y xs (e :: Elem x xs). SElem e -> SElem ('There e :: Elem x (y '::: xs)) type family RemoveElem (xs :: Vect ('S n) a) (e :: Elem x xs) :: Vect n a where RemoveElem (x '::: xs) 'Here = xs RemoveElem (x '::: xs) ('There later) = x '::: RemoveElem xs later sRemoveElem :: forall (xs :: Vect ('S n) Nat) (e :: Elem x xs) . SElem e -> SNatVect xs -> SNatVect (RemoveElem xs e) sRemoveElem prf (SNatCons y ys) = case prf of SHere -> ys SThere later -> case ys of SNatNil -> case later of {} SNatCons{} -> SNatCons y (sRemoveElem later ys)
有趣的是,两个版本都不会将元素作为单独的参数传递,因为该信息包含在Elem
/ SElem
值中.该value
参数也可以从该函数的Idris版本中删除,但是removeElem_auto变体可能有点令人困惑,因为它只会将向量作为显式参数,如果隐式prf
参数是,则删除向量的第一个元素未明确使用不同的证据.
考虑[1, 2, 1]
.RemoveElem 1 [1, 2, 1]
是[2, 1]
.现在,调用removeElem 1 (There $ There $ Here) ([1, 2, 1] :: SNatVect 3 [1, 2, 1]) :: SNatVect 2 [2, 1]
,应该编译.这是错的.该Elem
参数表示要删除的第三个元素,这将使[1, 2]
,但类型签名说,它必须是一个[2, 1]
.
首先,SNatVect
有点破碎.它有两个Nat
参数:
data SNatVect :: forall n. Nat -> Vect n a -> Type where ...
第一个是n
,第二个是未命名的Nat
.通过结构SNatVect
,它们总是相等的.它允许一个SNatVect
双重作为相等证明,但它可能不是那样的意图.你可能意味着
data SNatVect (n :: Nat) :: Vect n Nat -> Type where ...
只使用普通->
语法就无法在源Haskell中编写此签名.但是,当GHC打印此类型时,您有时会得到
SNatVect :: forall (n :: Nat) -> Vect n Nat -> Type
但这是多余的.您可以将其Nat
作为隐式forall
参数,并从Vect
s类型推断:
data SNatVect (xs :: Vect n Nat) where SNatNil :: SNatVect 'Nil SNatCons :: SNat x -> SNatVect xs -> SNatVect (x '::: xs)
这给了
SNatVect :: forall (n :: Nat). Vect n Nat -> Type
第二,尝试写作
removeElem :: forall (n :: Nat) (x :: Nat) (xs :: Vect (S n) Nat). Elem x xs -> SNatVect xs -> Vect n Nat
注意SNat
参数是如何消失的,以及返回类型是如何简单的Vect
.这个SNat
论点使得这个类型"太大了",所以当函数没有意义时,你就会陷入困境.在SNatVect
返回类型意味着你跳过一些步骤.粗略地说,每个函数都有三种形式:基本形式f :: a -> b -> c
; 类型级别,type family F (x :: a) (y :: b) :: c
; 和从属的,f :: forall (x :: a) (y :: b). Sing x -> Sing y -> Sing (F x y)
.每个都以"相同"的方式实现,但尝试实现一个而不实现其前任是一种让人感到困惑的可靠方法.
现在,你可以提升一点:
data SElem (e :: Elem x (xs :: Vect n k)) where SHere :: forall x xs. SElem ('Here :: Elem x (x '::: xs)) SThere :: forall x y xs (e :: Elem x xs). SElem e -> SElem ('There e :: Elem x (y '::: xs)) type family RemoveElem (xs :: Vect (S n) a) (e :: Elem x xs) :: Vect n a
记下removeElem
和的类型之间的关系RemoveElem
.对参数的重新排序是因为类型e
依赖于xs
,因此需要相应地进行排序.或者:将xs
参数从forall
"d-and-implicitly-given" 提升为"明确给定",然后Sing xs
参数被添加,因为它不包含任何信息,因为它是一个单例.
最后,你可以编写这个函数:
sRemoveElem :: forall (xs :: Vect (S n) Nat) (e :: Elem x xs). SElem e -> SNatVect xs -> SNatVect (RemoveElem xs e)