将函数定义为 codomain 的子集

Defining a function to a subset of the codomain

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

def fun_to_image {A B: Type*} (f: A → B): A → image f set.univ :=
        λ a, f a

然而,这被拒绝了,因为 (f a) 是类型 B 而不是 (image f set.univ)。

我什至尝试证明 f(a) ∈ (image f univ) 。它没有帮助:

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)

你可以看到令人毛骨悚然的 up-arrow 。让我解释一下这是什么意思:

正如您所记得的,image f set.univ 被定义为一个子集。由于您将其视为一种类型,因此它会自动强制转换为 so-called 子类型:如果 s : set X,则相应的 subtype s 具有 ⟨x, h⟩ 形式的项(键入这些如 \<\> in VScode),其中 x : Xh : x ∈ s.

这种“类型强制”由 表示。

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


请注意,在 main library there is also a definition of range (here) 中用于代替 image _ set.univ。 它已经带有 (L1167)

def range_factorization (f : ι → β) : ι → range f :=
λ i, ⟨f i, mem_range_self i⟩