如何解决隐式vs显式函数类型错误?

时间:2019-05-29 07:49:33

标签: agda

这是来自PLFA书的the last chapter

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 (from 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)替换∀ {x} → x ∈ xs → P x,因为它是问题定义的一部分。

这里的类型签名应该是什么?对于同构中的每个函数,是否有比where块更简单的方法?我不喜欢大量复制类型签名。

2 个答案:

答案 0 :(得分:1)

我已经探索了一些基于 Marko Grdinic's answer 的替代方案,以使代码更具可读性和更简单。

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

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

答案 1 :(得分:0)

即使@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