我可以在GHC 7.6中为类型检查器提供有关感应自然的证据吗?

时间:2012-09-15 22:03:37

标签: haskell ghc data-kinds

GHC 7.6.1附带了类型级编程的新功能,包括datatype promotion。以那里的类型级自然和向量为例,我希望能够在依赖算术基本定律的向量上编写函数。

不幸的是,即使我想要的法律通常通过案例分析和归纳很容易在归纳自然中得到证明,但我怀疑我是否可以说服这种类型检查。举个简单的例子,对下面的朴素反向函数进行类型检查需要n + Su Ze ~ Su n

的证明

我有什么方法可以提供这个证据,或者我现在真的处于完全依赖类型的领域?

{-# LANGUAGE DataKinds, KindSignatures, GADTs, TypeFamilies, TypeOperators #-}

data Nat = Ze | Su Nat

data Vec :: * -> Nat -> * where
  Nil  :: Vec a Ze
  Cons :: a -> Vec a n -> Vec a (Su n)

type family (m :: Nat) + (n :: Nat) :: Nat

type instance Ze + n = n
type instance (Su m + n) = Su (m + n)

append :: Vec a m -> Vec a n -> Vec a (m + n)
append Nil ys = ys
append (Cons x xs) ys = Cons x (append xs ys)

rev :: Vec a n -> Vec a n
rev Nil = Nil
rev (Cons x xs) = rev xs `append` Cons x Nil

1 个答案:

答案 0 :(得分:19)

(注意:我只对这些代码进行了类型检查(而不是实际运行)。)

方法1

实际上,可以通过将证据存储在GADT中来操作证据。您需要打开ScopedTypeVariables才能使用此方法。

data Proof n where
    NilProof  :: Proof Ze
    ConsProof :: (n + Su Ze) ~ Su n => Proof n -> Proof (Su n)

class PlusOneIsSucc n where proof :: Proof n
instance PlusOneIsSucc Ze where proof = NilProof
instance PlusOneIsSucc n => PlusOneIsSucc (Su n) where
    proof = case proof :: Proof n of
        NilProof    -> ConsProof proof
        ConsProof _ -> ConsProof proof

rev :: PlusOneIsSucc n => Vec a n -> Vec a n
rev = go proof where
    go :: Proof n -> Vec a n -> Vec a n
    go NilProof Nil = Nil
    go (ConsProof p) (Cons x xs) = go p xs `append` Cons x Nil

实际上,上面的Proof类型可能是有趣的动机,我最初只是

data Proof n where Proof :: (n + Su Ze) ~ Su n => Proof n

但是,这不起作用:GHC正确地抱怨说,仅仅因为我们知道(Su n)+1 = Su (Su n)并不意味着我们知道n+1 = Su n,这是我们需要知道的,以便递归调用rev案例中的Cons。因此,我必须扩展Proof的含义,以包括自然的所有等式的证明,包括n - 在从归纳到强诱导的过程中,与强化过程基本相似。

方法2

经过一番反思,我意识到这个课程有点多余;这使得这种方法特别好,因为它不需要任何额外的扩展(甚至ScopedTypeVariables),并且不会对Vec的类型引入任何额外的约束。

data Proof n where
    NilProof  :: Proof Ze
    ConsProof :: (n + Su Ze) ~ Su n => Proof n -> Proof (Su n)

proofFor :: Vec a n -> Proof n
proofFor Nil = NilProof
proofFor (Cons x xs) = let rec = proofFor xs in case rec of
    NilProof    -> ConsProof rec
    ConsProof _ -> ConsProof rec

rev :: Vec a n -> Vec a n
rev xs = go (proofFor xs) xs where
    go :: Proof n -> Vec a n -> Vec a n
    go NilProof Nil = Nil
    go (ConsProof p) (Cons x xs) = go p xs `append` Cons x Nil

方法3

或者,如果您将rev的实现切换为将最后一个元素转换为列表的反向初始段,那么代码看起来会更简单一些。 (这种方法也不需要额外的扩展。)

class Rev n where
    initLast :: Vec a (Su n) -> (a, Vec a n)
    rev :: Vec a n -> Vec a n

instance Rev Ze where
    initLast (Cons x xs) = (x, xs)
    rev x = x

instance Rev n => Rev (Su n) where
    initLast (Cons x xs) = case initLast xs of
        (x', xs') -> (x', Cons x xs')
    rev as = case initLast as of
        (a, as') -> Cons a (rev as')

方法4

就像方法3一样,但再次观察到类型类不是必需的。

initLast :: Vec a (Su n) -> (a, Vec a n)
initLast (Cons x xs) = case xs of
    Nil     -> (x, Nil)
    Cons {} -> case initLast xs of
        (x', xs') -> (x', Cons x xs')

rev :: Vec a n -> Vec a n
rev Nil = Nil
rev xs@(Cons {}) = case initLast xs of
    (x, xs') -> Cons x (rev xs')