如何在coq中生成反函数

如何在coq中生成反函数,coq,Coq,我有以下代码。我没有写完整的代码,但这应该是可行的 Definition in_domain {X Y : Set} (f : X -> option Y) x := match (f x) with | Some y => True | None => False end. Definition injective {X Y : Set} (f : X -> option Y) := forall x y z, f x = Some z -> f y = Som

我有以下代码。我没有写完整的代码,但这应该是可行的

Definition in_domain {X Y : Set} (f : X -> option Y) x := match (f x) with | Some y => True | None => False end.

Definition injective {X Y : Set} (f : X -> option Y) := forall x y z, f x = Some z -> f y = Some z -> x = y.
Definition surjective {X Y : Set} (f : X -> option Y) := forall y, exists x, f x = Some y.
Definition bijective {X Y : Set} (f : X -> option Y) := injective f /\ surjective f.

Definition compose {X Y Z : Set} (f : X -> option Y) (g : Y -> option Z) (H : forall x, in_domain f x -> in_domain g (f x)) := fun x => match (f x) with | Some y => g y | None => None end.
现在我试图编写
定义逆{xy:Set}(f:X->option Y)(H:bijective f:Y->option X
)。我无法使函数
g
成为
fx=someygy=somex


如果生成这样的函数是可能的,你能演示一下怎么做吗?

你需要公理来做到这一点,因为Coq默认情况下不允许你从存在证明中提取证人。在这种情况下,您只需要函数扩展性和唯一选择原则,这是选择公理的一个较弱的变体。以下是您的问题的简化变体的一种可能性:

Require Import Coq.Logic.Description.
Require Import Coq.Logic.FunctionalExtensionality.

Definition injective {X Y : Set} (f : X -> Y) := forall x y, f x = f y -> x = y.
Definition surjective {X Y : Set} (f : X -> Y) := forall y, exists x, f x = y.
Definition bijective {X Y : Set} (f : X -> Y) := injective f /\ surjective f.

Lemma inverse {X Y : Set} (f : X -> Y) :
  bijective f -> {g : Y -> X | (forall x, g (f x) = x) /\
                               (forall y, f (g y) = y) }.
Proof.
intros [inj sur].
apply constructive_definite_description.
assert (H : forall y, exists! x, f x = y).
{ intros y.
  destruct (sur y) as [x xP].
  exists x; split; trivial.
  intros x' x'P.
  now apply inj; rewrite xP, x'P. }
exists (fun y => proj1_sig (constructive_definite_description _ (H y))).
split.
- split.
  + intros x.
    destruct (constructive_definite_description _ _).
    simpl.
    now apply inj.
  + intros y.
    now destruct (constructive_definite_description _ _).
- intros g' [H1 H2].
  apply functional_extensionality.
  intros y.
  destruct (constructive_definite_description _ _) as [x e].
  simpl.
  now rewrite <- e, H1.
Qed.
需要导入Coq.Logic.Description。
需要导入Coq.Logic.FunctionalExtensionity。
定义内射{xy:Set}(f:X->Y):=forall xy,fx=fy->X=Y。
定义满射{xy:Set}(f:X->Y):=对于所有Y,存在X,fx=Y。
定义双射{xy:Set}(f:X->Y):=内射f/\surjective f。
引理逆{xy:Set}(f:X->Y):
双射f->{g:Y->X |(对于所有X,g(f X)=X)/\
(对于所有y,f(gy)=y)}。
证明。
简介[南印第安纳州]。
应用建设性的明确描述。
断言(H:forall y,exists!x,fx=y)。
{简介y。
自毁(sur y)为[x xP]。
存在x;分裂;平凡。
简介x'x'P。
现在应用inj;重写xP,x'P.}
存在(乐趣y=>proj1\u信号(构造性描述(y))。
分裂
-分开。
+简介x。
自毁(构造性描述)。
简单。
现在应用inj。
+介绍y。
现在进行销毁(构造性描述)。
-简介g'[H1H2]。
应用函数扩展性。
介绍y。
析构函数(构造性描述)为[Xe]。
简单。
现在重写