Functional programming 将函数定义为codomain的子集

Functional programming 将函数定义为codomain的子集,functional-programming,lean,Functional Programming,Lean,我试图定义函数的图像限制 f:A→ B为f:A→ f[A],其中f'=f(A)。但是,我不知道如何在精益中定义它。 在我看来,最直观的定义方式是: def fun_to_image {A B: Type*} (f: A → B): A → image f set.univ := λ a, f a 但是,这会被拒绝,因为(fa)不是类型B(image f set.univ) 我甚至试着证明f(a)∈ (图f大学)。这没有帮助: def fun_to_image (f : A →

我试图定义函数的图像限制
f:A→ B为f:A→ f[A],其中f'=f(A)。但是,我不知道如何在精益中定义它。
在我看来,最直观的定义方式是:

def fun_to_image {A B: Type*} (f: A → B): A → image f set.univ :=
        λ a, f a
但是,这会被拒绝,因为(fa)不是类型B(image f set.univ)

我甚至试着证明f(a)∈ (图f大学)。这没有帮助:

def fun_to_image (f : A → B) : A → image f univ := 
    λ a , 
    have h : f a ∈ image f univ := 
        exists.intro a 
            (and.intro trivial (eq.refl (f a))),
    f a
错误消息是:

type mismatch, term
  λ (a : A), f a
has type
  A → B
but is expected to have type
  A → ↥(f '' univ)
set.univ和image在data.set中定义如下

def univ : set α :=
λ a, true

def image (f : α → β) (s : set α) : set β :=
{b | ∃ a, a ∈ s ∧ f a = b}
你知道怎么做吗?

你就快到了

错误消息中有一个小“警告标志”

but is expected to have type
  A → ↥(f '' univ)
您可以看到令人毛骨悚然的向上箭头
。让我解释一下它的含义:

正如您所记得的,
image f set.univ
被定义为一个子集。由于您将其视为一个类型,它会自动强制为一个所谓的子类型:如果
s:set X
,则相应的
子类型
具有
⟨x、 h⟩
(在VScode中键入
\
),其中
x:x
h:x∈ s

该“强制类型”由

因此,要完成定义,您必须编写
⟨f a,h⟩,而不是
f a


请注意,在中还有一个
范围
()的定义,用于代替
图像uuu.set.univ
。 它已经附带了()


谢谢你,这真的很有帮助,它回答了很多问题!
def range_factorization (f : ι → β) : ι → range f :=
λ i, ⟨f i, mem_range_self i⟩