简体   繁体   English

如何在 agda 中通过 W 类型进行编码?

[英]How to encode via W-types in agda?

I'm trying to encode lists via W-types in Agda, when trying to prove my encoding correct, I get the following unsolveable goal.我试图通过 Agda 中的 W 类型对列表进行编码,当试图证明我的编码正确时,我得到了以下无法解决的目标。

Goal: g (f (x a)) ≡ x a'
Have: g (f (x a')) ≡ x a'
————————————————————————————————————————————————————————————
a' : A
x  : A → W (⊤ ⊔ A) Blist
a  : A
A  : Type

I assume that when defining my forward function in the equivalance, f (sup (inr a) x) = a ∷ f (xa) , needs to somehow not just apply x to a, but I don't see how to do this.我假设在等价中定义我的前向 function 时, f (sup (inr a) x) = a ∷ f (xa)需要以某种方式不仅仅将 x 应用于 a,但我不知道如何做到这一点。 Otherwise, am I defning by B x wrong, or is there some other minor error in backwards function, g ?否则,我定义的B x是否错误,或者向后 function, g是否存在其他小错误? How does one approach debugging this?一种方法如何调试这个? I can provide all the code used, but I'm hoping the error can be spotted by eye for brevity.我可以提供所有使用的代码,但为了简洁起见,我希望可以通过眼睛发现错误。 Also note, λ≡ denotes function extensionality.另请注意,λ≡ 表示 function 可扩展性。

data W (A : Type) (B : A → Type) : Type where
  sup : (a : A) → ((b : B a) → W A B) → W A B


Blist : ∀ {A} → ⊤ ⊔ A → Type
Blist (inl x) = ⊥
Blist {A} (inr x) = A

List' : Type → Type
List' A = W (⊤ ⊔ A) Blist

data List (A : Type) : Type where
  [] : List A
  _∷_ : A → List A → List A

ListsEquiv : ∀ {A} → List' A ≃ List A
ListsEquiv {A} = equiv f g fg gf
  where
    f : List' A → List A
    f (sup (inl top) x) = []
    f (sup (inr a) x) = a ∷ f (x a)
    g : List A → List' A
    g [] = sup (inl tt) abort
    g (a ∷ as) = sup (inr a) λ a' → g as
    fg : (y : List A) → f (g y) ≡ y
    fg [] = refl
    fg (x ∷ y) = ap (λ - → x ∷ -) (fg y)
    gf : (x : List' A) → g (f x) ≡ x
    gf (sup (inl tt) x) = ap (λ - → sup (inl tt) -) (λ≡ (λ x₁ → abort x₁))
    gf (sup (inr a) x) = ap (λ - → sup (inr a) -) (λ≡ (λ a' → {!gf (x a')!}))

As pointed out by @HTNW, the way you encode arity indexing type is the same as for the natural numbers, thus it is only the Base Type over which serves as the index data.正如@HTNW 所指出的,您对arity 索引类型进行编码的方式与自然数相同,因此它只是用作索引数据的基本类型。 Thus, the modification below allows the proof to go through smoothly.因此,下面的修改允许对 go 的证明顺利通过。

It's easy to get confused as to how the data should be ecoded into the Well-order constructor, and therefore lends itself to seemingly simple mistakes.数据应该如何编码到 Well-order 构造函数中很容易混淆,因此容易出现看似简单的错误。

Blist : ∀ {A} → ⊤ ⊔ A → Type
Blist (inl x) = ⊥
Blist (inr x) = ⊤

List' : Type → Type
List' A = W (⊤ ⊔ A) Blist


ListsEquiv : ∀ {A} → List' A ≃ List A
ListsEquiv {A} = equiv f g fg gf
  where
    f : List' A → List A
    f (sup (inl top) x) = []
    f (sup (inr a) x) = a ∷ f (x tt)
    g : List A → List' A
    g [] = sup (inl tt) abort
    g (a ∷ as) = sup (inr a) λ a' → g as
    fg : (y : List A) → f (g y) ≡ y
    fg [] = refl
    fg (x ∷ y) = ap (λ - → x ∷ -) (fg y)
    gf : (x : List' A) → g (f x) ≡ x
    gf (sup (inl tt) x) = ap (λ - → sup (inl tt) -) (λ≡ (λ x₁ → abort x₁))
    gf (sup (inr a) x) = ap (λ - → sup (inr a) -) (λ≡ (λ a' → gf (x a')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM