Agda 如何避免隐式与显式函数类型错误?

Agda 如何避免隐式与显式函数类型错误?,agda,plfa,Agda,Plfa,这是PLFA书的一部分 import Relation.Binary.PropositionalEquality as Eq open Eq using (_≡_; refl; sym; trans; cong) open import Data.Product using (_×_; ∃; ∃-syntax; Σ; Σ-syntax) renaming (_,_ to ⟨_,_⟩) infix 0 _≃_ record _≃_ (A B : Set) : Set where field

这是PLFA书的一部分

import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; sym; trans; cong)
open import Data.Product using (_×_; ∃; ∃-syntax; Σ; Σ-syntax) renaming (_,_ to ⟨_,_⟩)

infix 0 _≃_
record _≃_ (A B : Set) : Set where
  field
    to   : A → B
    from : B → A
    from∘to : ∀ (x : A) → from (to x) ≡ x
    to∘from : ∀ (y : B) → to (from y) ≡ y
open _≃_

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

infixr 5 _∷_

data All {A : Set} (P : A → Set) : List A → Set where
  []  : All P []
  _∷_ : ∀ {x : A} {xs : List A} → P x → All P xs → All P (x ∷ xs)

data Any {A : Set} (P : A → Set) : List A → Set where
  here  : ∀ {x : A} {xs : List A} → P x → Any P (x ∷ xs)
  there : ∀ {x : A} {xs : List A} → Any P xs → Any P (x ∷ xs)

infix 4 _∈_

_∈_ : ∀ {A : Set} (x : A) (xs : List A) → Set
x ∈ xs = Any (x ≡_) xs

All-∀ : ∀ {A : Set} {P : A → Set} {xs} → All P xs ≃ (∀ {x} → x ∈ xs → P x)
All-∀ {A} {P} =
  record { to = to'
         ; from = from'
         ; from∘to = from∘to'
         ; to∘from = to∘from'
         }
  where
    to' : ∀ {xs} → All P xs → (∀ {x} → x ∈ xs → P x)
    from' : ∀ {xs} → (∀ {x} → x ∈ xs → P x) → All P xs

    from∘to' : ∀ {xs : List A} → (x : All P xs) → from' (to' x) ≡ x
    to∘from' : ∀ {xs : List A} → (x∈xs→Px : ∀ {x} → x ∈ xs → P x) → to' (from' x∈xs→Px) ≡ x∈xs→Px
当我用
to(从x开始)填充孔时∈xs→Px)≡ x∈xs→Px
,我得到以下错误

_x_1668 (x∈xs→Px = x∈xs→Px) ∈ xs → P (_x_1668 (x∈xs→Px = x∈xs→Px))
!= {x : A} → x ∈ xs → P x because one is an implicit function type
and the other is an explicit function type
when checking that the expression to∘from has type
(y : {x : A} → x ∈ xs → P x) → to (from y) ≡ y
我不确定这意味着什么,但当涉及隐式参数时,Agda可能会有疑问。我没有尝试过的一件事是在
∀ {x}→ x∈ xs→ P x
,因为它是问题定义的一部分


这里的类型签名应该是什么?对于同构中的每个函数,还有比
where
块更简单的方法吗?我不喜欢繁重地复制字体签名。

即使@gallais在Agda页面上说了些什么,我还是花了将近3个小时才弄明白怎么做。下面是我推荐的类型签名。我在函数扩展性方面遇到了很多麻烦。相比之下,实际问题微不足道

我认为隐式参数的推理方式肯定需要一些维护

postulate
  extensionality : ∀ {A : Set} {B : A → Set} {f g : (x : A) → B x}
    → (∀ (x : A) → f x ≡ g x)
      -----------------------
    → f ≡ g

postulate
  extensionality_impl : ∀ {X : Set}{Y : X → Set}
                  → {f g : {x : X} → Y x}
                  → ((x : X) → f {x} ≡ g {x})
                  → (λ {x} → f {x}) ≡ g

All-∀ : ∀ {A : Set} {P : A → Set} {xs} → All P xs ≃ (∀ {x} → x ∈ xs → P x)
All-∀ {A} {P} =
  record { to = to
         ; from = from
         ; from∘to = from∘to
         ; to∘from = λ x'∈xs→Px → extensionality_impl λ x → extensionality λ x∈xs → to∘from x'∈xs→Px x∈xs
         }
  where
    to : ∀ {xs} → All P xs → (∀ {x} → x ∈ xs → P x)
    from : ∀ {xs} → (∀ {x} → x ∈ xs → P x) → All P xs
    from∘to : ∀ {xs : List A} → (x : All P xs) → from (to x) ≡ x
    to∘from : ∀ {xs : List A} (x∈xs→Px : ∀ {x} → x ∈ xs → P x) {x} (x∈xs : x ∈ xs) → to (from x∈xs→Px) x∈xs ≡ x∈xs→Px x∈xs

我已经探索了一些基于的替代方案,以使代码更可读、更简单

首先,我发现了一种使用库中的扩展性为隐式参数定义扩展性的稍微简单的方法:

open import Axiom.Extensionality.Propositional using (ExtensionalityImplicit)
open Level using (0ℓ)
postulate
  extensionality-implicit-0ℓ : ExtensionalityImplicit 0ℓ 0ℓ
这项练习似乎还需要一个隐式版本的
cong-app

cong-app-implicit : ∀ {A : Set} {B : A → Set} {f g : {x : A} → B x} →
  (λ {x} → f {x}) ≡ (λ {x} → g {x}) → {x : A} → f {x} ≡ g {x}
cong-app-implicit refl = refl

您可以扩展函数并编写例如
(\pr->to(从x∈xs→Px)pr)≡ x∈xs→Px
。不,我也有同样的错误。你也试过在另一边扩展吗?我无法测试我的建议,因为您的问题不是独立的。我编辑了示例,使其是独立的。请你再看一下好吗?Agda发行版上有一个a。