非空列表comonad

时间:2017-10-03 22:00:37

标签: list idris category-theory comonad

我一直在思考comonads并且有一种直觉,即非空列表("完整列表")是一个comonad。我在伊德里斯构建了一个合理的实现,并努力证明comonad laws,但未能证明其中一个法则的递归分支。我如何证明这一点(?i_do_not_know_how_to_prove_this_if_its_provable漏洞) - 或者我错误地认为我的实施是一个有效的comonad(我已经查看了Haskell NonEmpty comonad实现,它似乎与矿)?

module FullList

%default total

data FullList : Type -> Type where
  Single : a -> FullList a
  Cons : a -> FullList a -> FullList a

extract : FullList a -> a
extract (Single x) = x
extract (Cons x _) = x

duplicate : FullList a -> FullList (FullList a)
duplicate = Single 

extend : (FullList a -> b) -> FullList a -> FullList b
extend f (Single x) = Single (f (Single x))
extend f (Cons x y) = Cons (f (Cons x y)) (extend f y)

extend_and_extract_are_inverse : (l : FullList a) -> extend FullList.extract l = l
extend_and_extract_are_inverse (Single x) = Refl
extend_and_extract_are_inverse (Cons x y) = rewrite extend_and_extract_are_inverse y in Refl

comonad_law_1 : (l : FullList a) -> extract (FullList.extend f l) = f l
comonad_law_1 (Single x) = Refl
comonad_law_1 (Cons x y) = Refl

nesting_extend : (l : FullList a) -> extend f (extend g l) = extend (\x => f (extend g x)) l
nesting_extend (Single x) = Refl
nesting_extend (Cons x y) = ?i_do_not_know_how_to_prove_this_if_its_provable

1 个答案:

答案 0 :(得分:3)

请注意,您的目标是以下形式:

firebase login
Already logged in as desiredAccount@gmail.com

你基本上需要证明尾部相等:

Cons (f (Cons (g (Cons x y)) (extend g y))) (extend f (extend g y)) =
Cons (f (Cons (g (Cons x y)) (extend g y))) (extend (\x1 => f (extend g x1)) y)

但这正是诱导假设(extend f (extend g y) = extend (\x1 => f (extend g x1)) y )所说的!因此,证据非常简单:

nesting_extend y

我使用了同余引理nesting_extend : (l : FullList a) -> extend f (extend g l) = extend (f . extend g) l nesting_extend (Single x) = Refl nesting_extend (Cons x y) = cong $ nesting_extend y

cong

表示任何函数cong : (a = b) -> f a = f b 将相等的术语映射为相等的术语。

此处,Idris推断ff,其中Cons (f (Cons (g (Cons x y)) (extend g y))) f引用Cons的参数nesting_extend