这不能编译,因为ghc告诉我Add不是单射的。如何告诉编译器Add实际上是可交换的(也许告诉它Add是单射的)?从无人教论文中可以看出,必须以某种方式提供代理。
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
data Nat = Z | S Nat
type family Add a b where
Add Z n = n
Add n Z = n
Add (S n) k = S (Add n k)
data VecList n a where
Nil :: VecList Z a
Cons :: a -> VecList n a -> VecList (S n) a
safeRev :: forall a n . VecList n a -> VecList n a
safeRev xs = safeRevAux Nil xs
where
safeRevAux :: VecList p a -> VecList q a -> VecList (Add p q) a
safeRevAux acc Nil = acc
safeRevAux acc (Cons y ys) = safeRevAux (Cons y acc) ys
人们可以做到这一点,但感觉就像我的口味一样,太多了。
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Proxy
import Data.Type.Equality
data Nat = Z | S Nat
type family n1 + n2 where
Z + n2 = n2
(S n1) + n2 = S (n1 + n2)
-- singleton for Nat
data SNat :: Nat -> * where
SZero :: SNat Z
SSucc :: SNat n -> SNat (S n)
-- inductive proof of right-identity of +
plus_id_r :: SNat n -> ((n + Z) :~: n)
plus_id_r SZero = Refl
plus_id_r (SSucc n) = gcastWith (plus_id_r n) Refl
-- inductive proof of simplification on the rhs of +
plus_succ_r :: SNat n1 -> Proxy n2 -> ((n1 + (S n2)) :~: (S (n1 + n2)))
plus_succ_r SZero _ = Refl
plus_succ_r (SSucc n1) proxy_n2 = gcastWith (plus_succ_r n1 proxy_n2) Refl
data VecList n a where
V0 :: VecList Z a
Cons :: a -> VecList n a -> VecList (S n) a
reverseList :: VecList n a -> VecList n a
reverseList V0 = V0
reverseList list = go SZero V0 list
where
go :: SNat n1 -> VecList n1 a-> VecList n2 a -> VecList (n1 + n2) a
go snat acc V0 = gcastWith (plus_id_r snat) acc
go snat acc (Cons h (t :: VecList n3 a)) =
gcastWith (plus_succ_r snat (Proxy :: Proxy n3))
(go (SSucc snat) (Cons h acc) t)
safeHead :: VecList (S n) a -> a
safeHead (Cons x _) = x
test = safeHead $ reverseList (Cons 'a' (Cons 'b' V0))
请参阅https://www.haskell.org/pipermail/haskell-cafe/2014-September/115919.html了解最初的想法。
编辑:
@ user3237465这是非常有趣的,更多的是我的想法 (虽然经过反思,我的问题可能并不是很好 配制)。
似乎我有“公理”
type family n1 :+ n2 where
Z :+ n2 = n2
(S n1) :+ n2 = S (n1 + n2)
因此可以产生类似
的证据plus_id_r :: SNat n -> ((n + Z) :~: n)
plus_id_r SZero = Refl
plus_id_r (SSucc n) = gcastWith (plus_id_r n) Refl
我觉得这很简洁。我通常会推理这样的事情
对于你的解决方案,你给出了“公理”
type family n :+ m where
Z :+ m = m
S n :+ m = n :+ S m
使用这些,(n + Z):〜:n的证明将不起作用。
我可以从新的第二个“公理”中产生证明 第二个“公理”(所以我的第二个“公理”现在是一个引理?)。
succ_plus_id :: SNat n1 -> SNat n2 -> (((S n1) :+ n2) :~: (S (n1 :+ n2)))
succ_plus_id SZero _ = Refl
succ_plus_id (SSucc n) m = gcastWith (succ_plus_id n (SSucc m)) Refl
所以现在我应该能够获得原始证据但我是 不确定目前的情况。
到目前为止,我的推理是否正确?
PS:ghc同意我为什么证明存在正确身份的证据不起作用Could not deduce ((n1 :+ 'S 'Z) ~ 'S n1)
...
or from ((n1 :+ 'Z) ~ n1)
答案 0 :(得分:4)
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ExplicitForAll #-}
import Data.Type.Equality
data Nat = Z | S Nat
type family (n :: Nat) :+ (m :: Nat) :: Nat where
Z :+ m = m
S n :+ m = n :+ S m
-- Singleton for Nat
data SNat :: Nat -> * where
SZero :: SNat Z
SSucc :: SNat n -> SNat (S n)
succ_plus_id :: SNat n1 -> SNat n2 -> (((S n1) :+ n2) :~: (S (n1 :+ n2)))
succ_plus_id SZero _ = Refl
succ_plus_id (SSucc n) m = gcastWith (succ_plus_id n (SSucc m)) Refl
plus_id_r :: SNat n -> ((n :+ Z) :~: n)
plus_id_r SZero = Refl
plus_id_r (SSucc x) = gcastWith (plus_id_r x) (succ_plus_id x SZero)
data Vec a n where
Nil :: Vec a Z
(:::) :: a -> Vec a n -> Vec a (S n)
size :: Vec a n -> SNat n
size Nil = SZero
size (_ ::: xs) = SSucc $ size xs
elim0 :: SNat n -> (Vec a (n :+ Z) -> Vec a n)
elim0 n x = gcastWith (plus_id_r n) x
accrev :: Vec a n -> Vec a n
accrev x = elim0 (size x) $ go Nil x where
go :: Vec a m -> Vec a n -> Vec a (n :+ m)
go acc Nil = acc
go acc (x ::: xs) = go (x ::: acc) xs
safeHead :: Vec a (S n) -> a
safeHead (x ::: _) = x
答案 1 :(得分:2)
您可以简化reverse
的定义:
{-# LANGUAGE GADTs, KindSignatures, DataKinds #-}
{-# LANGUAGE TypeFamilies, UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
data Nat = Z | S Nat
data Vec a n where
Nil :: Vec a Z
(:::) :: a -> Vec a n -> Vec a (S n)
type family n :+ m where
Z :+ m = m
S n :+ m = n :+ S m
elim0 :: Vec a (n :+ Z) -> Vec a n
elim0 = undefined
accrev :: Vec a n -> Vec a n
accrev = elim0 . go Nil where
go :: Vec a m -> Vec a n -> Vec a (n :+ m)
go acc Nil = acc
go acc (x ::: xs) = go (x ::: acc) xs
(:+)
运算符是根据(:::)
运算符定义的。 (:::)
案例中的统一进行如下:
x ::: xs
会导致n
成为S n
。因此,结果的类型变为Vec a (S n :+ m)
,或者在β减少后Vec a (n :+ S m)
。而
x ::: acc :: Vec a (S m)
xs :: Vec a n
go (x ::: acc) xs :: Vec a (n :+ S m)
所以我们有一场比赛。但是现在您需要定义elim0 :: Vec a (n :+ Z) -> Vec a n
,这需要您提问的证明。
Agda中的整个代码:http://lpaste.net/117679
reverse
定义的方式:
foldl : ∀ {a b} {A : Set a} (B : ℕ → Set b) {m} →
(∀ {n} → B n → A → B (suc n)) →
B zero →
Vec A m → B m
foldl b _⊕_ n [] = n
foldl b _⊕_ n (x ∷ xs) = foldl (λ n → b (suc n)) _⊕_ (n ⊕ x) xs
reverse : ∀ {a n} {A : Set a} → Vec A n → Vec A n
reverse {A = A} = foldl (Vec A) (λ rev x → x ∷ rev) []
那是因为foldl
带有关于_⊕_
行为的其他类型信息,因此您在每一步都满足了类型检查器,并且不需要证明。