(*************************************************************************)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Copyright (C) 2013 - 2015                                             *)
(* Author C. Cohen                                                       *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* DRAFT - PLEASE USE WITH CAUTION                                       *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* License CeCILL-B                                                      *)
(*************************************************************************)

From mathcomp
Require Import ssreflect ssrbool ssrnat eqtype ssrfun seq.
From mathcomp
Require Import choice path finset finfun fintype bigop.
From mathcomp
Require Import bigenough.

(*****************************************************************************)
(* This file provides representations for finite sets over a choiceType K,   *)
(* finite maps with keys in a choiceType K and the values in an arbitrary    *)
(* type V, and total functions from K to V with finite support.              *)
(* The domain (resp. support) of a finite map (resp. fintely supported       *)
(* function) is a finite set, and so is the codomain (resp. image) when V    *)
(* is a choice type.                                                         *)
(*                                                                           *)
(*                {fset K} == finite sets of elements of K                   *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*           {fmap K -> V} == finitely supported maps from K to V.           *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* {fsfun K -> T for dflt} == finitely supported functions                   *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*                     with default value dflt : K -> V outside the support  *)
(*                                                                           *)
(********* finite sets *******************************************************)
(*                                                                           *)
(* In the remainder, A and B are of type {fset K}.                           *)
(*                                                                           *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* - {fset K} is provided with a canonical structure of predType, in order   *)
(*   to enable the notation "a \in A"                                        *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* - There is a coercion from {fset K} to Type in order to interpret any     *)
(*   finset A as the subType of elements a : K such that a \in A.            *)
(*   Because of this coercion, writing a : A makes sense.                    *)
(*                                                                           *)
(* The following notations are in the %fset scope                            *)
(*            fset0 == the empty finite set                                  *)
(*         [fset k] == the singleton finite set {k}                          *)
(*          A `&` B == the intersection of A and B                           *)
(*          A `|` B == the union of A and B                                  *)
(*           a |` B == the union of singleton a and B                        *)
(*          A `\` B == the complement of B in A                              *)
(*           A `\ b == A without b                                           *)
(*          A `*` B == the cartesian product of A and B                      *)
(* [disjoint A & B] := A `&` B == 0                                          *)
(*         A `<=` B == A is a subset of B                                    *)
(*          A `<` B == A is a proper subset of B                             *)
(*            #|`A| == cardinal of A                                         *)
(*    fincl AsubB a == turns a : A  into an element of B                     *)
(*                     using a proof AsubB of A \fsubset B                   *)
(*         fsub B A == turns A : {fset K} into a {set B}                     *)
(*           f @` A == the image set of the collective predicate A by f.     *)
(*      f @2`(A, B) == the image set of A x B by the binary function f.      *)
(*                                                                           *)
(* In order to support the following notations, we introduce three canonical *)
(* structure that reflect the finiteness of a predicate, in the following    *)
(* notations, p (resp q) are such finite predicates, which are ultimately    *)
(* represented by elements A (resp B) from {fset K}.                         *)
(*                                                                           *)
(*    [fset x in p | P] == the set of all x of type K, such that             *)
(*                         x \in p and P x where P is a predicate on K       *)
(*  [fset x in p | P & Q] := [set x in p | P && Q].                          *)
(*                                                                           *)
(* [fset E | x in p] == the set of all the values of the expression E, for x *)
(*                     drawn from the collective finite predicate p.         *)
(* [fset E | x in p & P] == the set of values of E for x drawn from p, such  *)
(*                     that P is true.                                       *)
(* [fset E | x in p, y in q] == the set of values of E for x drawn from p and*)
(*                     and y drawn from q; q may depend on x.                *)
(* [fset E | x in p, y in q & P] == the set of values of E for x drawn from  *)
(*                    p and y drawn from q; such that P is true.             *)
(* [fsetval x in p] == the set of (val x) for x in the finite predicate p    *)
(* [fsetval x in p | P ] == the set of (val x) for x in p, such that P       *)
(* [fsetval x in p | P & Q] := [fsetval x in p | P && Q]                     *)
(*                                                                           *)
(* For each notation above, there is an additional one with ':' instead of   *)
(* 'in' which is used to range over the finite type A instead of the finite  *)
(* set A, and the optional predicate is over A instead of K                  *)
(* For example:                                                              *)
(*    [fset x : A | P] := [fset x in {: A} | P]                              *)
(*                     == the set of all x of type A, such that P x          *)
(* [fset E | x : A] == the set of all the values of the expression E, for x  *)
(*                     drawn from the finite type A                          *)
(*                                                                           *)
(* For each [fset ...] or [fsetval ...] notation, there is a keyed variant   *)
(* written [fset[key] ...] or [fsetval[key] ...] for locking                 *)
(******* finite maps *********************************************************)
(*                                                                           *)
(* Operations on finmap:                                                     *)
(* The following notations are in the %fmap scope                            *)
(*                                                                           *)
(*           f.[? k] == returns Some v if k maps to v, otherwise None        *)
(*             f.[p] == returns v if p has type k \in f, and k maps to v     *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*        f.[k <- v] == f extended with the mapping k -> v                   *)
(*            domf f == finite set (of type {fset K}) of keys of f           *)
(*          codomf f == finite set (of type {fset V}) of values of f         *)
(*           k \in f == k is a key of f                                      *)
(*                   := k \in domf f                                         *)
(*            [fmap] == the empty finite map                                 *)
(* [fmap x : S => E] == the finmap defined by E on the support S             *)
(*           f.[& A] == f restricted to A (intersected with domf f)          *)
(*           f.[\ A] := f.[& domf `\` A]                                     *)
(*                   == f where all the keys in A have been removed          *)
(*           f.[~ k] := f.[\ [fset k]]                                       *)
(*             f + g == concatenation of f and g,                            *)
(*                      the keys of g override the keys of f                 *)
(*                                                                           *)
(******* finitely supported functions ****************************************)
(*                                                                           *)
(* Operation on function with finite support, i.e. finmap with default value *)
(* for elements outside of the support. Contrarly to finmap, these are total *)
(* function, so we provide a coercion to Funclass                            *)
(*                                                                           *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* {fsfun K -> T for dflt} == finitely supported functions with default      *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*                            value dflt : K -> V outside the support        *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* {fsfun K -> T of x => dflt} :=  {fsfun K -> T for fun x => dflt}          *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*    {fsfun K -> T with dflt} :=  {fsfun K -> T for fun=> dflt}             *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(*              {fsfun K -> K} :=  {fsfun K -> T for fun x => x}             *)
(*                                                                           *)
(*             [fsfun for dflt] == the default fsfun                         *)
(*         [fsfun of x => dflt] == the default fsfun                         *)
(* [fsfun x : A => F | default] == the fsfun which takes value F on A        *)
(*                                    x has type A : {fset T}                *)
(* [fsfun x in A => F | default] == the fsfun which takes value F on A       *)
(*                                     x has type T, where A : {fset T}      *)
(* we also provide untyped variants and variants where default is ommitted   *)
(* e.g.  [fsfun x : A => F] [fsfun x => F | default] [fsfun]...              *)
(* and many variants to give the possibility to insert a key : unit          *)
(* to prevent conversion from diverging, e.g.                                *)
(* [fsfun[key] x : A => F | default] and [fsfun[key] x in A => F | default]  *)
(*                ...                                                        *)
(*     (f \o g)%fsfun == composition of fsfun                                *)
(*     fsinjectiveb f == boolean predicate of injectivity of f               *)
(*****************************************************************************)

Set Implicit Arguments.
Unset Strict Implicit.
Import Prenex Implicits.

Reserved Notation "{fset K }" (at level 0, format "{fset  K }").
Reserved Notation "A `&` B"  (at level 48, left associativity).
Reserved Notation "A `*` B"  (at level 46, left associativity).
Reserved Notation "A `+` B"  (at level 54, left associativity).
Reserved Notation "A +` B"  (at level 54, left associativity).
Reserved Notation "A `|` B" (at level 52, left associativity).
Reserved Notation "a |` A" (at level 52, left associativity).
Reserved Notation "A `\` B" (at level 50, left associativity).
Reserved Notation "A `\ b" (at level 50, left associativity).

Reserved Notation "{fmap T }" (at level 0, format "{fmap  T }").
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Reserved Notation "x .[ k <- v ]"
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (at level 2, k at level 200, v at level 200, format "x .[ k  <-  v ]").
Reserved Notation "x .[~ k ]" (at level 2, k at level 200, format "x .[~  k ]").
Reserved Notation "x .[& k ]" (at level 2, k at level 200, format "x .[&  k ]").
Reserved Notation "x .[\ k ]" (at level 2, k at level 200, format "x .[\  k ]").
Reserved Notation "x .[? k ]" (at level 2, k at level 200, format "x .[?  k ]").
Reserved Infix "`~`" (at level 52).
Reserved Notation "[ 'fset' k ]" (at level 0, k at level 99, format "[ 'fset'  k ]").

Local Notation predOfType T := (sort_of_simpl_pred (@pred_of_argType T)).

Section extra.

Lemma mem_remF (T : eqType) (s : seq T) x : uniq s -> x \in rem x s = false.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
Proof. by move=> us; rewrite mem_rem_uniq // inE eqxx. Qed.

Definition ffun0 (T : finType) (X : Type) : #|T| = 0 -> {ffun T -> X}.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
Proof. by move=> T0; split; rewrite T0; exists nil. Defined.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition oextract (T : Type) (o : option T) : o -> T :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  if o is Some t return o -> T then fun=> t else False_rect T \o notF.

Lemma oextractE (T : Type) (x : T) (xP : Some x) : oextract xP = x.
Proof. by []. Qed.

Lemma Some_oextract T (x : option T) (x_ex : x) : Some (oextract x_ex) = x.
Proof. by case: x x_ex. Qed.

Definition ojoin T (x : option (option T)) :=
  if x is Some y then y else None.

Lemma Some_ojoin T (x : option (option T)) : x -> Some (ojoin x) = x.
Proof. by case : x. Qed.

Lemma ojoinT T (x : option (option T)) : ojoin x -> x.
Proof. by case: x. Qed.

Section AllSigs.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Variables (S : Type) (T : S -> Type)  (R : Type) (f : forall x, T x -> R).
Implicit Types (s : seq S) (t : forall x, seq (T x)).

Definition allsigs s t := foldr (fun x => cat (map (@f x) (t x))) [::] s.

Lemma size_allsigs s t : size (allsigs s t) = sumn [seq size (t x) | x <- s].
Proof. by elim: s => //= x s IHs; rewrite size_cat size_map IHs. Qed.

Lemma allsigs_cat s1 s2 t :
  allsigs (s1 ++ s2) t = allsigs s1 t ++ allsigs s2 t.
Proof. by elim: s1 => //= x s1 ->; rewrite catA. Qed.

End AllSigs.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma allsigs_comp S (T : S -> Type) R R' (f : forall x, T x -> R) (g : R -> R') s t :
  allsigs (fun x y => g (f x y)) s t = map g (allsigs f s t).
Proof. by elim: s => //= x s ->; rewrite map_cat map_comp. Qed.

Prenex Implicits allsigs.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Notation "[ 'seq' E | i <- s & j <- t ]" := (allsigs (fun i j => E) s (fun i => t))
  (at level 0, E at level 99, i ident, j ident,
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   format "[ '[hv' 'seq'  E '/ '  |  i  <-  s  & '/   '  j  <-  t ] ']'")
   : seq_scope.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Notation "[ 'seq' E | i : T <- s  & j : U <- t ]" :=
  (allsigs (fun (i : T) (j : U) => E) s (fun i : T => t))
  (at level 0, E at level 99, i ident, j ident, only parsing) : seq_scope.

Section EqAllSigs.

Variables (S : eqType) (T : S -> eqType).
Implicit Types (R : eqType) (s : seq S) (t : forall x, seq (T x)).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma allsigsP R (f : forall x, T x -> R) s t z : reflect
  (exists p : sigT T, [/\ tag p \in s, tagged p \in t (tag p) & z = f (tag p) (tagged p)])
  (z \in allsigs f s t).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
elim: s => [|x s IHs /=]; first by right=> [[p []]].
(* Goal: @eq bool (@fsubset K A (@fsetI K B C)) (andb (@fsubset K A B) (@fsubset K A C)) *)
rewrite mem_cat; have [fxt_z | not_fxt_z] := altP mapP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by left; have [y t_y ->] := fxt_z; exists (Tagged T y); rewrite mem_head.
(* Goal: is_true (negb (@eq_op (fset_eqType K) (@fsetD K C B) (@fsetD K C A))) *)
apply: (iffP IHs) => [] [[x' y] /= [s_x' t_y def_z]]; exists (Tagged T y) => /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite !inE predU1r.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [def_x' | //] := predU1P s_x'.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by do [case: _ / def_x'; rewrite def_z map_f] in s_x' not_fxt_z *.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma mem_allsigs R (f : forall x, T x -> R) s1 t1 s2 t2 :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   s1 =i s2 -> (forall x, x \in s1 -> t1 x =i t2 x) ->
   allsigs f s1 t1 =i allsigs f s2 t2.
Proof.
move=> eq_s eq_t z; apply/allsigsP/allsigsP=> [] [p fpz]; exists p => [];
by move: fpz (fpz) => [???]; rewrite eq_s eq_t //= 1?eq_s.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma allsigs_catr R (f : forall x, T x -> R) s t1 t2 :
  allsigs f s (fun x => t1 x ++ t2 x) =i allsigs f s t1 ++ allsigs f s t2.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> z; rewrite mem_cat.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/allsigsP/orP=> [[p [sP1]]|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite mem_cat; case/orP; [left | right]; apply/allsigsP; exists p.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case=> /allsigsP[p [sp1 sp2 ->]]; exists p; rewrite mem_cat sp2 ?orbT.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma allsigs_uniq R (f : forall x, T x -> R) s t :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
    uniq s -> (forall x, x \in s -> uniq (t x)) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
    {in [seq Tagged T y | x <- s & y <- t x] &,
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
     injective (fun p : sigT T => f (tag p) (tagged p))} ->
  uniq (allsigs f s t).
Proof.
(* Goal: forall (_ : forall (x : Choice.sort T) (px : is_true (@in_mem (Choice.sort T) x (@pred_of_finmempred (Choice.eqType T) p))), P (@FSetSub K (@Imfset.imfset key T K f p (Phantom (mem_pred (Equality.sort (Choice.eqType T))) (@pred_of_finmempred (Choice.eqType T) p))) (f x) (@in_imfset T f p x px))) (k : @fset_sub_type K (@Imfset.imfset key T K f p (Phantom (mem_pred (Equality.sort (Choice.eqType T))) (@pred_of_finmempred (Choice.eqType T) p)))), P k *)
move=> Us Ut inj_f; have: all (mem s) s by apply/allP.
(* Goal: forall _ : is_true (@fsubset T (@fsetD T U X) U), P (@fsetD T U (@fsetD T U X)) *)
elim: {-2}s Us => //= x s1 IHs /andP[s1'x Us1] /andP[sx1 ss1].
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite cat_uniq {}IHs // andbT map_inj_in_uniq ?Ut // => [|y1 y2 *].
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@restrictf K (Choice.sort V) f A)) (@Imfset.imfset imfset_key (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) V (fun k : Choice.sort (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@subfinset_finpred (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@fin_finpred (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (pred_finpredType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A)))) (Phantom (mem_pred (@fset_sub_type K (@domf K (Choice.sort V) f))) (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => andb (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) k (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (@in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A))))))) *)
  apply/hasPn=> _ /allsigsP[z [s1z tz ->]]; apply/mapP=> [[y ty Dy]].
  suffices [Dz1 _]: Tagged T (tagged z) = Tagged T y.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
     by rewrite -Dz1 s1z in s1'x.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  apply: inj_f => //; apply/allsigsP; last by exists (Tagged T y).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have:= allP ss1 _ s1z; exists z.
suffices /eqP: Tagged T y1 = Tagged T y2 by rewrite eq_Tagged => /eqP.
apply: inj_f => //; apply/allsigsP;
by [exists (Tagged T y1) | exists (Tagged T y2)].
Qed.

End EqAllSigs.

Lemma big_allsigs (R : Type) (idx : R) (op : Monoid.law idx)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (I1 : Type)  (I2 : I1 -> Type)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (r1 : seq I1) (r2 : forall i1, seq (I2 i1)) (F : sigT I2 -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
\big[op/idx]_(i <- [seq Tagged I2 i2 | i1 <- r1 & i2 <- r2 i1]) F i =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
\big[op/idx]_(i1 <- r1) \big[op/idx]_(i2 <- r2 i1) F (Tagged I2 i2).
Proof.
(* Goal: @eq R (@BigOp.bigop R (@sigT I1 (fun x : I1 => I2 x)) idx (@allsigs I1 (fun i1 : I1 => I2 i1) (@sigT I1 (fun x : I1 => I2 x)) (fun (i1 : I1) (i2 : (fun i2 : I1 => I2 i2) i1) => @Tagged I1 i1 I2 i2) r1 (fun i1 : I1 => r2 i1)) (fun i : @sigT I1 (fun x : I1 => I2 x) => @BigBody R (@sigT I1 (fun x : I1 => I2 x)) i (@Monoid.operator R idx op) true (F i))) (@BigOp.bigop R I1 idx r1 (fun i1 : I1 => @BigBody R I1 i1 (@Monoid.operator R idx op) true (@BigOp.bigop R (I2 i1) idx (r2 i1) (fun i2 : I2 i1 => @BigBody R (I2 i1) i2 (@Monoid.operator R idx op) true (F (@Tagged I1 i1 I2 i2)))))) *)
elim: r1 => [|i1 r1 IHr1]; rewrite !(big_nil, big_cons)//= big_cat {}IHr1.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (r2 i1) => [|i2 r21]; rewrite /= !(big_nil, big_cons)//= big_map.
Qed.

Section NatHomomorphism.
Variable T : Type.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma homo_ltn_in (D : pred nat) (f : nat -> T) (r : T -> T -> Prop) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall y x z, r x y -> r y z -> r x z) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D &, forall i j k, i < k < j -> k \in D} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D, forall i, i.+1 \in D -> r (f i) (f i.+1)} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D &, {homo f : i j / i < j >-> r i j}}.
Proof.
move=> r_trans Dcx r_incr i j iD jD lt_ij; move: (lt_ij) (jD) => /subnKC<-.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
elim: (_ - _) => [|k ihk]; first by rewrite addn0 => Dsi; apply: r_incr.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> DSiSk [: DSik]; apply: (r_trans _ _ _ (ihk _)); rewrite ?addnS.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by abstract: DSik; apply: (Dcx _ _ iD DSiSk); rewrite ltn_addr ?addnS /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: r_incr; rewrite -?addnS.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma homo_ltn (f : nat -> T) (r : T -> T -> Prop) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall y x z, r x y -> r y z -> r x z) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall i, r (f i) (f i.+1)) -> {homo f : i j / i < j >-> r i j}.
Proof. by move=> /(@homo_ltn_in predT f) fr fS i j; apply: fr. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma homo_leq_in (D : pred nat) (f : nat -> T) (r : T -> T -> Prop) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall x, r x x) -> (forall y x z, r x y -> r y z -> r x z) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D &, forall i j k, i < k < j -> k \in D} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D, forall i, i.+1 \in D -> r (f i) (f i.+1)} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  {in D &, {homo f : i j / i <= j >-> r i j}}.
Proof.
(* Goal: forall _ : is_true (@fsubset K (@fsetU K A B) C), @eq bool (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) (@fdisjoint K A B) *)
move=> r_refl r_trans Dcx /(homo_ltn_in r_trans Dcx) lt_r i j iD jD.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite leq_eqVlt => /predU1P[->//|/lt_r]; apply.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma homo_leq (f : nat -> T) (r : T -> T -> Prop) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   (forall x, r x x) -> (forall y x z, r x y -> r y z -> r x z) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall i, r (f i) (f i.+1)) -> {homo f : i j / i <= j >-> r i j}.
Proof. by move=> rrefl /(@homo_leq_in predT f r) fr fS i j; apply: fr. Qed.

End NatHomomorphism.
End extra.

Module Type SortKeysSig.
Section SortKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).

Axiom f : seq K -> seq K.
Axiom perm : forall s, perm_eq (f s) (undup s).
Axiom uniq : forall s, uniq (f s).
Axiom E : forall (s : seq K), f s =i s.
Axiom eq : forall (s s' : seq K), s =i s' <-> f s = f s'.
End SortKeys.
End SortKeysSig.

Module SortKeys : SortKeysSig.
Section SortKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).
Definition f (s : seq K) := choose (perm_eq (undup s)) (undup s).
Fact perm s : perm_eq (f s) (undup s).
Proof. by rewrite perm_eq_sym chooseP. Qed.
Fact uniq s : uniq (f s).
Proof. by rewrite (perm_eq_uniq (perm _)) undup_uniq. Qed.
Fact E (s : seq K) : f s =i s.
Proof. by move=> x; rewrite (perm_eq_mem (perm _)) mem_undup. Qed.
Lemma eq (s s' : seq K) : s =i s' <-> f s = f s'.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
split=> [eq_ss'|eq_ss' k]; last by rewrite -E eq_ss' E.
(* Goal: @eq bool (@fsubset K A (@fsetI K B C)) (andb (@fsubset K A B) (@fsubset K A C)) *)
rewrite /f; have peq_ss' : perm_eq (undup s) (undup s').
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply: uniq_perm_eq; rewrite ?undup_uniq // => x; rewrite !mem_undup.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite (@choose_id _ _ _ (undup s')) //=; apply: eq_choose => x /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: sym_left_transitive; [exact: perm_eq_sym|exact: perm_eq_trans|].
Qed.
End SortKeys.
End SortKeys.

Hint Resolve SortKeys.perm.
Hint Resolve SortKeys.uniq.
Hint Resolve SortKeys.E.

Notation sort_keys      := SortKeys.f.
Notation sort_keys_perm := SortKeys.perm.
Notation sort_keys_uniq := SortKeys.uniq.
Notation sort_keysE     := SortKeys.E.
Notation eq_sort_keys   := SortKeys.eq.

Section ChoiceKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).

Lemma mem_sort_keys ks k : k \in ks -> k \in sort_keys ks.
Proof. by rewrite sort_keysE. Qed.

Lemma mem_sort_keys_intro ks k : k \in sort_keys ks -> k \in ks.
Proof. by rewrite sort_keysE. Qed.

Lemma sort_keys_nil : sort_keys [::] = [::] :> seq K.
Proof.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have := sort_keysE ([::] : seq K).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: sort_keys => //= a l /(_ a); rewrite mem_head.
Qed.

Lemma sort_keys_id ks : sort_keys (sort_keys ks) = sort_keys ks.
Proof. by have /eq_sort_keys := sort_keysE ks. Qed.

Definition canonical_keys ks := sort_keys ks == ks.

Lemma canonical_uniq ks : canonical_keys ks -> uniq ks.
Proof. by move=> /eqP <-; exact: sort_keys_uniq. Qed.

Lemma canonical_sort_keys ks : canonical_keys (sort_keys ks).
Proof. by rewrite /canonical_keys sort_keys_id. Qed.

Lemma canonical_eq_keys ks ks' :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  canonical_keys ks -> canonical_keys ks' ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  ks =i ks' -> ks = ks'.
Proof.
(* Goal: forall (_ : is_true (canonical_keys ks)) (_ : is_true (canonical_keys ks')) (_ : @eq_mem (Equality.sort (Choice.eqType K)) (@mem (Equality.sort (Choice.eqType K)) (seq_predType (Choice.eqType K)) ks) (@mem (Equality.sort (Choice.eqType K)) (seq_predType (Choice.eqType K)) ks')), @eq (list (Choice.sort K)) ks ks' *)
move=> /eqP; case: _ /; move=> /eqP; case: _ / => eq_ks_ks'.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/eq_sort_keys => x; rewrite -sort_keysE eq_ks_ks' sort_keysE.
Qed.

Lemma size_sort_keys ks : size (sort_keys ks) = size (undup ks).
Proof. exact: perm_eq_size. Qed.

End ChoiceKeys.
Arguments eq_sort_keys {K s s'}.

Section Def.
Variables (K : choiceType).

Structure finSet : Type := mkFinSet {
  enum_fset :> seq K;
  _ : canonical_keys enum_fset
}.

Definition finset_of (_ : phant K) := finSet.

End Def.

Identity Coercion type_of_finset : finset_of >-> finSet.
Notation "{fset T }" := (@finset_of _ (Phant T)) : type_scope.

Definition pred_of_finset (K : choiceType)
  (f : finSet K) : pred K := fun k => k \in (enum_fset f).
Canonical finSetPredType (K : choiceType) :=
  Eval hnf in mkPredType (@pred_of_finset K).

Section FinSetCanonicals.

Variable (K : choiceType).

Canonical fsetType := Eval hnf in [subType for (@enum_fset K)].
Definition fset_eqMixin := Eval hnf in [eqMixin of {fset K} by <:].
Canonical fset_eqType := Eval hnf in EqType {fset K} fset_eqMixin.
Definition fset_choiceMixin := Eval hnf in [choiceMixin of {fset K} by <:].
Canonical fset_choiceType := Eval hnf in ChoiceType {fset K} fset_choiceMixin.

End FinSetCanonicals.

Section FinTypeSet.

Variables (K : choiceType) (A : finSet K).

Lemma keys_canonical : canonical_keys (enum_fset A).
Proof. by case: A. Qed.

Lemma fset_uniq : uniq (enum_fset A).
Proof. by rewrite canonical_uniq // keys_canonical. Qed.

Record fset_sub_type : predArgType :=
  FSetSub {fsval : K; fsvalP : in_mem fsval (@mem K _ A)}.

Canonical fset_sub_subType := Eval hnf in [subType for fsval].
Definition fset_sub_eqMixin := Eval hnf in [eqMixin of fset_sub_type by <:].
Canonical fset_sub_eqType := Eval hnf in EqType fset_sub_type fset_sub_eqMixin.
Definition fset_sub_choiceMixin := Eval hnf in [choiceMixin of fset_sub_type by <:].
Canonical fset_sub_choiceType := Eval hnf in ChoiceType fset_sub_type fset_sub_choiceMixin.

Definition fset_sub_enum : seq fset_sub_type :=
  undup (pmap insub (enum_fset A)).

Lemma mem_fset_sub_enum x : x \in fset_sub_enum.
Proof. by rewrite mem_undup mem_pmap -valK map_f // fsvalP. Qed.

Lemma val_fset_sub_enum : map val fset_sub_enum = enum_fset A.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite /fset_sub_enum undup_id ?pmap_sub_uniq ?fset_uniq//.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite (pmap_filter (@insubK _ _ _)); apply/all_filterP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/allP => x; rewrite isSome_insub.
Qed.

Definition fset_sub_pickle x := index x fset_sub_enum.
Definition fset_sub_unpickle n := nth None (map some fset_sub_enum) n.
Lemma fset_sub_pickleK : pcancel fset_sub_pickle fset_sub_unpickle.
Proof.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite /fset_sub_unpickle => x.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (nth_map x) ?nth_index ?index_mem ?mem_fset_sub_enum.
Qed.

Definition fset_sub_countMixin := CountMixin fset_sub_pickleK.
Canonical fset_sub_countType := Eval hnf in CountType fset_sub_type fset_sub_countMixin.

Definition fset_sub_finMixin :=
  Eval hnf in UniqFinMixin (undup_uniq _) mem_fset_sub_enum.
Canonical fset_sub_finType := Eval hnf in FinType fset_sub_type fset_sub_finMixin.
Canonical fset_sub_subfinType := [subFinType of fset_sub_type].

Lemma enum_fsetE : enum_fset A = [seq val i | i <- enum fset_sub_type].
Proof. by rewrite enumT unlock val_fset_sub_enum. Qed.

Lemma cardfE : size (enum_fset A) = #|fset_sub_type|.
Proof. by rewrite cardE enum_fsetE size_map. Qed.

End FinTypeSet.

Identity Coercion finSet_sub_type : finset_of >-> finSet.
Coercion fset_sub_type : finSet >-> predArgType.
Hint Resolve fsvalP fset_uniq mem_fset_sub_enum.

Delimit Scope fset_scope with fset.
Local Open Scope fset_scope.

Notation "[` kf ]" := (FSetSub kf) (format "[`  kf ]") : fset_scope.

Lemma fsetsubE (T : choiceType) (A : {fset T}) (x : A) (xA : val x \in A) :
 [` xA] = x.
Proof. by apply/val_inj => /=. Qed.

Notation "#|` A |" := (size (enum_fset A))
  (at level 0, A at level 99, format "#|`  A |") : nat_scope.
Definition fset_predT {T : choiceType} {A : {fset T}} : simpl_pred A := @predT A.
Coercion set_of_fset (K : choiceType) (A : {fset K}) : {set A} := [set x in {: A}].

Arguments pred_of_finset : simpl never.

Section SeqFset.

Variable finset_key : unit.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition seq_fset : forall K : choiceType, seq K -> {fset K} :=
   locked_with finset_key (fun K s => mkFinSet (@canonical_sort_keys K s)).

Variable (K : choiceType) (s : seq K).

Lemma seq_fsetE : seq_fset s =i s.
Proof. by move=> a; rewrite [seq_fset]unlock sort_keysE. Qed.

Lemma size_seq_fset : size (seq_fset s) = size (undup s).
Proof. by rewrite [seq_fset]unlock /= size_sort_keys. Qed.

Lemma seq_fset_uniq  : uniq (seq_fset s).
Proof. by rewrite [seq_fset]unlock /= sort_keys_uniq. Qed.

Lemma seq_fset_perm : perm_eq (seq_fset s) (undup s).
Proof. by rewrite [seq_fset]unlock //= sort_keys_perm. Qed.

End SeqFset.


Hint Resolve keys_canonical.
Hint Resolve sort_keys_uniq.

Canonical  finSetSubType K := [subType for (@enum_fset K)].
Definition finSetEqMixin (K : choiceType) := [eqMixin of {fset K} by <:].
Canonical  finSetEqType  (K : choiceType) := EqType {fset K} (finSetEqMixin K).
Definition finSetChoiceMixin (K : choiceType) := [choiceMixin of {fset K} by <:].
Canonical  finSetChoiceType  (K : choiceType) := ChoiceType {fset K} (finSetChoiceMixin K).

Section FinPredStruct.

Structure finpredType (T : eqType) := FinPredType {
  finpred_sort :> Type;
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  tofinpred : finpred_sort -> pred T;
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  _ : {mem : finpred_sort -> mem_pred T | isMem tofinpred mem};
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  _ : {finpred_seq : finpred_sort -> seq T |
       ((forall p, uniq (finpred_seq p))
       * forall p x, x \in finpred_seq p = tofinpred p x)%type}
}.

Canonical finpredType_predType (T : eqType) (fpT : finpredType T) :=
  @PredType T (finpred_sort fpT) (@tofinpred T fpT)
            (let: FinPredType _ _ mem _ := fpT in mem).

Definition enum_finpred  (T : eqType) (fpT : finpredType T) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
    fpT -> seq T :=
  let: FinPredType _ _ _ (exist s _) := fpT in s.

Lemma enum_finpred_uniq (T : eqType) (fpT : finpredType T) (p : fpT) :
   uniq (enum_finpred p).
Proof. by case: fpT p => ??? [s sE] p; rewrite sE. Qed.

Lemma enum_finpredE (T : eqType) (fpT : finpredType T) (p : fpT) :
   enum_finpred p =i p.
Proof. by case: fpT p => ??? [s sE] p x; rewrite sE -topredE. Qed.

Lemma mkFinPredType_of_subproof (T : eqType) (pT : predType T)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   (fpred_seq : pT -> seq T) (pred_fsetE : forall p, fpred_seq p =i p) :
  forall p x, x \in fpred_seq p = topred p x.
Proof. by move=> p x; rewrite topredE pred_fsetE. Qed.

Definition mkFinPredType_of (T : eqType) (U : Type) :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  fun (pT : predType T) & pred_sort pT -> U =>
  fun a mP (pT' := @PredType T U a mP) & phant_id pT' pT =>
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  fun (fpred_seq : pT' -> seq T)
      (fpred_seq_uniq : forall p, uniq (fpred_seq p))
      (fpred_seqE : forall p, fpred_seq p =i p) =>
  @FinPredType T U a mP (exist _ fpred_seq
   (fpred_seq_uniq, (mkFinPredType_of_subproof fpred_seqE))).

Definition clone_finpredType (T : eqType) (U : Type) :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  fun (pT : finpredType T) & finpred_sort pT -> U =>
  fun a mP pP (pT' := @FinPredType T U a mP pP) & phant_id pT' pT => pT'.

Structure is_finite (T : eqType) (P : pred T) := IsFinite {
  seq_of_is_finite :> seq T;
  _ : uniq seq_of_is_finite;
  _ : forall x, x \in seq_of_is_finite = P x;
}.

Lemma is_finite_uniq (T : eqType) (P : pred T) (p : is_finite P) : uniq p.
Proof. by case: p. Qed.

Lemma is_finiteE (T : eqType) (P : pred T) (p : is_finite P) x :
  x \in (seq_of_is_finite p) = P x.
Proof. by case: p. Qed.

Structure finpred (T : eqType) (pT : predType T) := FinPred {
  pred_of_finpred :> pT;
  _ : is_finite [pred x in pred_of_finpred]
}.

Definition enum_fin (T : eqType) (pT : predType T) (p : finpred pT) : seq T :=
  let: FinPred _ fp := p in fp.

Lemma enum_fin_uniq (T : eqType) (pT : predType T) (p : finpred pT) :
  uniq (enum_fin p).
Proof. by case: p => ?[]. Qed.

Lemma enum_finE  (T : eqType) (pT : predType T) (p : finpred pT) :
  enum_fin p =i (pred_of_finpred p).
Proof. by case: p => ?[]. Qed.

Canonical fin_finpred (T : eqType) (pT : finpredType T) (p : pT) :=
  @FinPred _ _ p (@IsFinite _ _ (enum_finpred p)
                            (enum_finpred_uniq p) (enum_finpredE p)).

Definition finpred_of (T : eqType) (pT : predType T) (p : pT)
 (fp : finpred pT) & phantom pT fp : finpred pT := fp.

Structure finmempred (T : eqType) := FinMemPred {
  pred_of_finmempred :> mem_pred T;
  _ : is_finite (fun x => in_mem x pred_of_finmempred)
}.

Definition enum_finmem (T : eqType) (p : finmempred T) : seq T :=
  let: FinMemPred _ fp := p in fp.

Lemma enum_finmem_uniq (T : eqType) (p : finmempred T) :
  uniq (enum_finmem p).
Proof. by case: p => ?[]. Qed.

Lemma enum_finmemE  (T : eqType) (p : finmempred T) :
  enum_finmem p =i p.
Proof. by case: p => ?[]. Qed.

Definition finmempred_of (T : eqType) (P : pred T)
 (mP : finmempred T) & phantom (mem_pred T) mP : finmempred T := mP.

Canonical mem_fin (T : eqType) (pT : predType T) (p : finpred pT) :=
  @FinMemPred  _ (mem p)
   (@IsFinite _ _ (enum_fin p) (enum_fin_uniq p) (enum_finE p)).

End FinPredStruct.

Notation "[ 'finpredType' 'of' T ]" := (@clone_finpredType _ T _ id _ _ _ id)
  (at level 0, format "[ 'finpredType'  'of'  T ]") : form_scope.

Notation mkFinPredType T s s_uniq sE :=
  (@mkFinPredType_of _ T _ id _ _ id s s_uniq sE).

Section CanonicalFinPred.

Canonical fset_finpredType (T: choiceType) :=
  mkFinPredType (finSet T) (@enum_fset T) (@fset_uniq T)  (fun _ _ => erefl).

Canonical pred_finpredType (T : finType) :=
  mkFinPredType (pred T) (fun P => enum_mem (mem P)) (@enum_uniq T) (@mem_enum T).

Canonical simpl_pred_finpredType (T : finType) :=
  mkFinPredType (simpl_pred T) (fun P => enum_mem (mem P)) (@enum_uniq T) (@mem_enum T).

Canonical fset_finpred (T: choiceType) (A : finSet T) :=
  @FinPred _ _ (enum_fset A)
     (@IsFinite _ _ (enum_fset A) (fset_uniq _) (fun=> erefl)).

Program Canonical subfinset_finpred (T : choiceType)
  (A : finmempred T) (Q : pred T) :=
  @FinPred _ _ [pred x in A | Q x]
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
          (@IsFinite _ _ [seq x <- enum_finmem A | Q x] _ _).
Next Obligation. by rewrite filter_uniq// enum_finmem_uniq. Qed.
Next Obligation. by rewrite !inE !mem_filter andbC enum_finmemE. Qed.

Canonical seq_finpredType (T : eqType) :=
  mkFinPredType (seq T) undup (@undup_uniq T) (@mem_undup T).

End CanonicalFinPred.

Local Notation imfset_def key :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (fun (T K : choiceType) (f : T -> K) (p : finmempred T)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
       of phantom (mem_pred T) p => seq_fset key [seq f x | x <- enum_finmem p]).
Local Notation imfset2_def key :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (fun (K T1 : choiceType) (T2 : T1 -> choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
       (f : forall x : T1, T2 x -> K)
       (p1 : finmempred T1) (p2 : forall x : T1, finmempred (T2 x))
   of phantom (mem_pred T1) p1 & phantom (forall x, mem_pred (T2 x)) p2 =>
  seq_fset key (allsigs f (enum_finmem p1) (fun x => enum_finmem (p2 x)))).

Module Type ImfsetSig.
Parameter imfset : forall (key : unit) (T K : choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
       (f : T -> K) (p : finmempred T),
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  phantom (mem_pred T) p ->  {fset K}.
Parameter imfset2 : forall (key : unit) (K T1 : choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
       (T2 : T1 -> choiceType)(f : forall x : T1, T2 x -> K)
       (p1 : finmempred T1) (p2 : forall x : T1, finmempred (T2 x)),
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  phantom (mem_pred T1) p1 -> phantom (forall x, mem_pred (T2 x)) p2 -> {fset K}.
Axiom imfsetE : forall key, imfset key = imfset_def key.
Axiom imfset2E : forall key, imfset2 key = imfset2_def key.
End ImfsetSig.

Module Imfset : ImfsetSig.
Definition imfset key := imfset_def key.
Definition imfset2 key := imfset2_def key.
Lemma imfsetE key : imfset key = imfset_def key. Proof. by []. Qed.
Lemma imfset2E key : imfset2 key = imfset2_def key. Proof. by []. Qed.
End Imfset.

Notation imfset key f p :=
   (Imfset.imfset key f (Phantom _ (pred_of_finmempred p))).
Notation imfset2 key f p q :=
   (Imfset.imfset2 key f (Phantom _ (pred_of_finmempred p))
                   (Phantom _ (fun x => (pred_of_finmempred (q x))))).
Canonical imfset_unlock k := Unlockable (Imfset.imfsetE k).
Canonical imfset2_unlock k := Unlockable (Imfset.imfset2E k).

Notation "A `=` B" := (A = B :> {fset _})
  (at level 70, no associativity, only parsing) : fset_scope.
Notation "A `<>` B" := (A <> B :> {fset _})
  (at level 70, no associativity, only parsing) : fset_scope.
Notation "A `==` B" := (A == B :> {fset _})
  (at level 70, no associativity, only parsing) : fset_scope.
Notation "A `!=` B" := (A != B :> {fset _})
  (at level 70, no associativity, only parsing) : fset_scope.
Notation "A `=P` B" := (A =P B :> {fset _})
  (at level 70, no associativity, only parsing) : fset_scope.

Notation "f @`[ key ] A" := (Imfset.imfset key f (Phantom _ (mem A)))
  (at level 24, key at level 0) : fset_scope.
Notation "f @2`[ key ] ( A , B )" :=
   (Imfset.imfset2 key f (Phantom _ (mem A)) (Phantom _ (fun x => (mem (B x)))))
  (at level 24, format "f  @2`[ key ]  ( A ,  B )") : fset_scope.

Fact imfset_key : unit. Proof. exact: tt. Qed.

Notation "f @` A" := (f @`[imfset_key] A) (at level 24) : fset_scope.
Notation "f @2` ( A , B )" := (f @2`[imfset_key] (A, B))
  (at level 24, format "f  @2`  ( A ,  B )") : fset_scope.

(* unary *)
Notation "[ 'fset' E | x : T 'in' A ]" :=
  ((fun x : T => E) @` A)
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A ]" := [fset E | x : _ in A]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A ]" := [fset E | x : _ in {: A} ]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset'  x  :  T  'in'  A ]" := [fset (x : T) | x in A]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset'  x  :  T  'in'  A  |  P ]" :=
  [fset (x : T) | x in [pred x in A | P]]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A | P ]" := [fset x : _ in A | P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A ]" := [fset x : _ in A ]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' x : T | P ]" := [fset x in {: T} | P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' x : T | P & Q ]" := [fset x : T | P && Q]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset'  x  :  T  'in' A  |  P  &  Q ]" := [fset x : T in A | P && Q]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A | P & Q ]" := [fset x in A | P && Q]
  (at level 0, x at level 99,  only parsing) : fset_scope.

(* binary *)
Notation "[ 'fset' E | x : T 'in' A , y : T' 'in' B ]" :=
  ((fun (x : T) (y : T') => E) @2` (A, fun x => B))
  (at level 0, E, x at level 99, A at level 200,
   y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y 'in' B ]" :=
  [fset E | x : _ in A, y : _ in B]
  (at level 0, E, x at level 99, A at level 200, y at level 99,
   only parsing) : fset_scope.

(* keyed parse only *)
Notation "[ 'fset[' key ] E | x : T 'in' A ]" :=
  ((fun x : T => E) @`[key] A)
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A ]" := [fset[key] E | x : _ in A]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A ]" := [fset[key] E | x : _ in {: A} ]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ]  x  :  T  'in'  A ]" := [fset[key] (x : T) | x in A]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ]  x  :  T  'in'  A  |  P ]" :=
  [fset[key] (x : T) | x in [pred x in A | P]]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A | P ]" := [fset[key] x : _ in A | P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A ]" := [fset[key] x : _ in A ]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T | P ]" := [fset[key] x in {: T} | P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T | P & Q ]" := [fset[key] x : T | P && Q]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ]  x  :  T  'in' A  |  P  &  Q ]" :=
  [fset[key] x : T in A | P && Q]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A | P & Q ]" := [fset[key] x in A | P && Q]
  (at level 0, x at level 99, only parsing) : fset_scope.

Notation "[ 'fset[' key ] E | x : T 'in' A , y : T' 'in' B ]" :=
  ((fun (x : T) (y : T') => E) @2` (A, fun x => B))
  (at level 0, E, x at level 99, A at level 200,
   y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B ]" :=
  [fset[key] E | x : _ in A, y : _ in B]
  (at level 0, E, x at level 99, A at level 200, y at level 99,
   only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B ]" :=
  [fset[key] E | x : _ in {: A}, y : _ in {: B}]
  (at level 0, E, x at level 99, A at level 200, y at level 99,
   only parsing) : fset_scope.

(* printing only *)
Notation "[ 'f' 'set' E | x 'in' A ]" := [fset[_] E | x in A]
  (at level 0, E, x at level 99,
   format "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A ] ']'") : fset_scope.
Notation "[ 'f' 'set' E | x : A ]" := [fset[_] E | x : A]
  (at level 0, E, x at level 99,
   format "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A ] ']'") : fset_scope.
Notation "[ 'f' 'set' x 'in' A | P ]" := [fset[_] x in A | P]
  (at level 0, x at level 99,
   format "[ 'f' 'set'  x  'in'  A  |  P ]") : fset_scope.
Notation "[ 'f' 'set' x 'in' A ]" := [fset[_] x in A]
  (at level 0, x at level 99,
   format "[ 'f' 'set'  x  'in'  A ]") : fset_scope.
Notation "[ 'f' 'set' x : T | P ]" := [fset[_] x : T | P]
  (at level 0, x at level 99,
   format "[ 'f' 'set'  x  :  T  |  P ]") : fset_scope.
Notation "[ 'f' 'set' x : T | P & Q ]" := [fset[_] x : T | P & Q]
  (at level 0, x at level 99,
   format "[ 'f' 'set'  x  :  T  |  P  &  Q ]") : fset_scope.
Notation "[ 'f' 'set' x 'in' A | P & Q ]" := [fset[_] x in A | P & Q]
  (at level 0, x at level 99,
   format "[ 'f' 'set'  x  'in'  A  |  P  &  Q ]") : fset_scope.
(* binary printing only*)
Notation "[ 'f' 'set' E | x 'in' A , y 'in' B ]" :=
  [fset[_] E | x in A, y in B]
  (at level 0, E, x at level 99, A at level 200, y at level 99,
   format "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A , '/' y  'in'  B ] ']'"
  ) : fset_scope.


Section Ops.

Context {K K': choiceType}.
Implicit Types (a b c : K) (A B C D : {fset K}) (E : {fset K'}) (s : seq K).

Definition fset0 : {fset K} :=
  @mkFinSet K [::] (introT eqP (@sort_keys_nil K)).
(* very transparent, to ensure x \in fset0 is convertible to false *)

Fact fset1_key : unit. Proof. exact: tt. Qed.
Definition fset1 a : {fset K} := [fset[fset1_key] x in [:: a]].

Fact fsetU_key : unit. Proof. exact: tt. Qed.
Definition fsetU A B := [fset[fsetU_key] x in enum_fset A ++ enum_fset B].

Fact fsetI_key : unit. Proof. exact: tt. Qed.
Definition fsetI A B := [fset[fsetI_key] x in A | x \in B].

Fact fsetD_key : unit. Proof. exact: tt. Qed.
Definition fsetD A B := [fset[fsetD_key] x in A | x \notin B].

Fact fsetM_key : unit. Proof. exact: tt. Qed.
Definition fsetM A E := [fset[fsetM_key] (x, y) | x : K in A, y : K' in E].

Definition fsubset A B := fsetI A B == A.

Definition fproper A B := fsubset A B && ~~ fsubset B A.

Definition fdisjoint A B := (fsetI A B == fset0).

End Ops.

Notation "[ 'fset' a ]" := (fset1 a)
  (at level 0, a at level 99, format "[ 'fset'  a ]") : fset_scope.
Notation "[ 'fset' a : T ]" := [fset (a : T)]
  (at level 0, a at level 99, format "[ 'fset'  a   :  T ]") : fset_scope.
Notation "A `|` B" := (fsetU A B) : fset_scope.
Notation "a |` A" := ([fset a] `|` A) : fset_scope.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* This is left-associative due to historical limitations of the .. Notation. *)
Notation "[ 'fset' a1 ; a2 ; .. ; an ]" := (fsetU .. (a1 |` [fset a2]) .. [fset an])
  (at level 0, a1 at level 99,
   format "[ 'fset'  a1 ;  a2 ;  .. ;  an ]") : fset_scope.
Notation "A `&` B" := (fsetI A B) : fset_scope.
Notation "A `*` B" := (fsetM A B) : fset_scope.
Notation "A `\` B" := (fsetD A B) : fset_scope.
Notation "A `\ a" := (A `\` [fset a]) : fset_scope.

Notation "A `<=` B" := (fsubset A B)
  (at level 70, no associativity) : fset_scope.

Notation "A `<` B" := (fproper A B)
  (at level 70, no associativity) : fset_scope.

Notation "[ 'disjoint' A & B ]" := (fdisjoint A B) : fset_scope.

(* Comprehensions *)
Notation "[ 'fset' E | x 'in' A & P ]" := [fset E | x in [pred x in A | P]]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A & P ]" := [fset E | x in {: A} & P]
  (at level 0, E, x at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y 'in' B ]" := [fset E | x in {: A}, y in B]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y : B ]" := [fset E | x in A, y in {: B}]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y : B ]" := [fset E | x in {: A}, y in {: B}]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y 'in' B & P ]" :=
  [fset E | x in A, y in [pred y in B | P]]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y 'in' B & P ]" :=
  [fset E | x in {: A}, y in B & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y : B & P ]" :=
  [fset E | x in A, y in {: B} & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y : B & P ]" :=
  [fset E | x in {: A}, y in {: B} & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.

Notation "[ 'fsetval' x 'in' A ]" := [fset val x | x in A]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval' x 'in' A | P ]" := [fset val x | x in A & P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval' x 'in' A | P & Q ]" := [fsetval x in A | (P && Q)]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval' x : A ]" := [fset val x | x in {: A}]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval' x : A | P ]" := [fset val x | x in {: A} & P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval' x : A | P & Q ]" := [fsetval x in {: A} | (P && Q)]
  (at level 0, x at level 99, only parsing) : fset_scope.

(* keyed parse only *)
Notation "[ 'fset[' key ] E | x 'in' A & P ]" :=
  [fset[key] E | x in [pred x in A | P]]
  (at level 0, E, x at level 99, only parsing).
Notation "[ 'fset[' key ] E | x : A & P ]" :=
  [fset[key] E | x in {: A} & P]
  (at level 0, E, x at level 99, only parsing).
Notation "[ 'fset[' key ] E | x : A , y 'in' B ]" :=
  [fset[key] E | x in {: A}, y in B]
  (at level 0, E, x, y at level 99, only parsing).
Notation "[ 'fset[' key ] E | x 'in' A , y : B ]" :=
  [fset[key] E | x in A, y in {: B}]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B ]" :=
  [fset[key] E | x in {: A}, y in {: B}]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B & P ]" :=
  [fset[key] E | x in A, y in [pred y in B | P]]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y 'in' B & P ]" :=
  [fset[key] E | x in {: A}, y in B & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y : B & P ]" :=
  [fset[key] E | x in A, y in {: B} & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B & P ]" :=
  [fset[key] E | x in {: A}, y in {: B} & P]
  (at level 0, E, x, y at level 99, only parsing) : fset_scope.

Notation "[ 'fsetval[' key ] x 'in' A ]" := [fset[key] val x | x in A]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x 'in' A | P ]" := [fset[key] val x | x in A & P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x 'in' A | P & Q ]" :=
  [fsetval[key] x in A | (P && Q)]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A ]" := [fset[key] val x | x in {: A}]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A | P ]" :=
  [fset[key] val x | x in {: A} & P]
  (at level 0, x at level 99, only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A | P & Q ]" :=
  [fsetval[key] x in {: A} | (P && Q)]
  (at level 0, x at level 99, only parsing) : fset_scope.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Print-only variants to work around the Coq pretty-printer K-term kink. *)
Notation "[ 'f' 'set' E | x 'in' A & P ]" := [fset[_] E | x in A & P]
  (at level 0, E, x at level 99,
   format "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A '/ '  &  P ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x : A & P ]" := [fset[_] E | x : A & P]
  (at level 0, E, x at level 99,
   format "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A '/ '  &  P ] ']'") : fset_scope.
Notation "[ 'f' 'set' E | x : A , y 'in' B ]" := [fset[_] E | x : A, y in B]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A , '/   '  y  'in'  B ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x 'in' A , y : B ]" := [fset[_] E | x in A, y : B]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A , '/   '  y  :  B ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x : A , y : B ]" :=
  [fset[_] E | x : A, y : B]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A , '/   '  y  :  B ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x 'in' A , y 'in' B & P ]" :=
  [fset[_] E | x in A, y in B & P]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A , '/   '  y  'in'  B '/ '  &  P ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x : A , y 'in' B & P ]" :=
  [fset[_] E | x : A, y in B & P]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A , '/   '  y  'in'  B  &  P ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x 'in' A , y : B & P ]" :=
  [fset[_] E | x in A, y : B & P]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  'in'  A , '/   '  y  :  B  &  P ] ']'"
  ) : fset_scope.
Notation "[ 'f' 'set' E | x : A , y : B & P ]" :=
  [fset[_] E | x : A, y : B & P]
  (at level 0, E, x, y at level 99, format
   "[ '[hv' 'f' 'set'  E '/ '  |  x  :  A , '/   '  y  :  B  &  P ] ']'"
  ) : fset_scope.

Notation "[ 'f' 'setval' x 'in' A ]" := [fset[_] val x | x in A]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  'in'  A ]") : fset_scope.
Notation "[ 'f' 'setval' x 'in' A | P ]" := [fset[_] val x | x in A & P]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  'in'  A  |  P ]") : fset_scope.
Notation "[ 'f' 'setval' x 'in' A | P & Q ]" := [fsetval[_] x in A | (P && Q)]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  'in'  A  |  P  &  Q ]") : fset_scope.
Notation "[ 'f' 'setval' x : A ]" := [fsetval[_] x : A]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  :  A ]") : fset_scope.
Notation "[ 'f' 'setval' x : A | P ]" := [fsetval[_] x : A | P]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  :  A  |  P ]") : fset_scope.
Notation "[ 'f' 'setval' x : A | P & Q ]" := [fsetval[_] x : A | (P && Q)]
  (at level 0, x at level 99,
   format "[ 'f' 'setval'  x  :  A  |  P  &  Q ]") : fset_scope.

Section imfset.

Variables (key : unit) (K : choiceType).
Implicit Types (A B : {fset K}).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma imfsetP (T : choiceType) (f : T -> K) (p : finmempred T) (k : K) :
  reflect (exists2 x : T, in_mem x p & k = f x) (k \in imfset key f p).
Proof.
rewrite unlock seq_fsetE /=; apply: (iffP mapP) => [] [x xp eqkf];
by exists x => //=; move: xp; rewrite enum_finmemE.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma in_imfset (T : choiceType) (f : T -> K) (p : finmempred T) (x : T) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   in_mem x p -> f x \in imfset key f p.
Proof. by move=> px; apply/imfsetP; exists x. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma imfset_rec (T : choiceType) (f : T -> K) (p : finmempred T)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (P : imfset key f p -> Prop) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall (x : T) (px : in_mem x p), P [` in_imfset f px ]) -> forall k, P k.
Proof.
(* Goal: forall (_ : forall (x : Choice.sort T) (px : is_true (@in_mem (Choice.sort T) x (@pred_of_finmempred (Choice.eqType T) p))), P (@FSetSub K (@Imfset.imfset key T K f p (Phantom (mem_pred (Equality.sort (Choice.eqType T))) (@pred_of_finmempred (Choice.eqType T) p))) (f x) (@in_imfset T f p x px))) (k : @fset_sub_type K (@Imfset.imfset key T K f p (Phantom (mem_pred (Equality.sort (Choice.eqType T))) (@pred_of_finmempred (Choice.eqType T) p)))), P k *)
move=> PP v; have /imfsetP [k pk vv_eq] := valP v.
(* Goal: P v *)
pose vP := in_imfset f pk; suff -> : P v = P [` vP] by apply: PP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by congr P; apply/val_inj => /=; rewrite vv_eq.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma mem_imfset (T : choiceType) (f : T -> K) (p : finmempred T) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  injective f -> forall (x : T), (f x \in imfset key f p) = (in_mem x p).
Proof. by move=> f_inj x; rewrite unlock seq_fsetE mem_map// enum_finmemE. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma imfset2P (T1 : choiceType) (T2 : T1 -> choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (f : forall x, T2 x -> K) (p1 : finmempred T1)
      (p2 : forall x, finmempred (T2 x)) k :
  reflect (exists2 x : T1, in_mem x p1
         & exists2 y : T2 x, in_mem y (p2 x) & k = f x y)
          (k \in imfset2 key f p1 p2).
Proof.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite unlock !seq_fsetE; apply: (iffP (allsigsP _ _ _ _)).
(* Goal: forall (_ : @eq (@finset_of K (Phant (Choice.sort K))) (@domf K V f) (@domf K V g)) (_ : forall (k : Choice.sort K) (kMf : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f))) (kMg : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) g))), @eq V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V f)) V (@ffun_of_fmap K V f) (@FSetSub K (@domf K V f) k kMf)) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V g)) V (@ffun_of_fmap K V g) (@FSetSub K (@domf K V g) k kMg))), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
  move=> [[/=x y]]; rewrite !enum_finmemE => -[xp yp ->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by exists x => //; exists y.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> [x xp [y yp ->]]; exists (Tagged T2 y); rewrite ?enum_finmemE.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma in_imfset2  (T1 : choiceType) (T2 : T1 -> choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (f : forall x, T2 x -> K) (p1 : finmempred T1)
      (p2 : forall x, finmempred (T2 x)) (x : T1) (y : T2 x) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   in_mem x p1 -> in_mem y (p2 x) -> f x y \in imfset2 key f p1 p2.
Proof. by move=> xD1 yD2; apply/imfset2P; exists x => //; exists y. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma mem_imfset2  (T1 : choiceType) (T2 : T1 -> choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (f : forall x, T2 x -> K) (p1 : finmempred T1)
      (p2 : forall x, finmempred (T2 x)) (x : T1) (y : T2 x) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   injective (fun x : sigT T2 => f (tag x) (tagged x)) ->
   f x y \in imfset2 key f p1 p2 = (in_mem x p1) && (in_mem y (p2 x)).
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> f_inj; rewrite unlock seq_fsetE.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply/allsigsP/idP => [[t]|]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> /andP[xp1 xp2]; exists (Tagged T2 y); rewrite ?enum_finmemE.
rewrite !enum_finmemE => -[pt1 pt2]; pose xy := Tagged T2 y.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite -[x]/(tag xy) -[y]/(tagged xy) => /f_inj ->; apply/andP.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma enum_imfset (T : choiceType) (f : T -> K) (p : finmempred T) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   {in p &, injective f} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   perm_eq (imfset key f p) [seq f x | x <- enum_finmem p].
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> f_inj; rewrite unlock -[X in perm_eq _ X]undup_id ?seq_fset_perm//.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite map_inj_in_uniq ?enum_finmem_uniq // => ??.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite ?enum_finmemE; apply: f_inj.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma enum_imfset2  (T1 : choiceType) (T2 : T1 -> choiceType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (f : forall x, T2 x -> K) (p1 : finmempred T1)
      (p2 : forall x, finmempred (T2 x)) :
   {in  [pred t | p1 (tag t) & p2 _ (tagged t)] &,
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
        injective (fun t : sigT T2 => f (tag t) (tagged t))} ->
   perm_eq (imfset2 key f p1 p2)
           (allsigs f (enum_finmem p1) (fun x => enum_finmem (p2 x))).
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> f_inj; rewrite unlock.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply: uniq_perm_eq => [||i]; rewrite ?seq_fset_uniq ?seq_fsetE //.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite allsigs_uniq ?enum_finmem_uniq//.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> x; rewrite enum_finmem_uniq.
(* Goal: forall (_ : @eq (@finset_of K (Phant (Choice.sort K))) (@domf K V f) (@domf K V g)) (_ : forall (k : Choice.sort K) (kMf : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f))) (kMg : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) g))), @eq V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V f)) V (@ffun_of_fmap K V f) (@FSetSub K (@domf K V f) k kMf)) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V g)) V (@ffun_of_fmap K V g) (@FSetSub K (@domf K V g) k kMg))), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> t0 t0' /allsigsP[t]; rewrite !enum_finmemE => -[tp1 tp2 ->].
(* Goal: forall (_ : @eq (@finset_of K (Phant (Choice.sort K))) (@domf K V f) (@domf K V g)) (_ : forall (k : Choice.sort K) (kMf : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f))) (kMg : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) g))), @eq V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V f)) V (@ffun_of_fmap K V f) (@FSetSub K (@domf K V f) k kMf)) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V g)) V (@ffun_of_fmap K V g) (@FSetSub K (@domf K V g) k kMg))), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> /allsigsP[t']; rewrite !enum_finmemE => -[t'p1 t'p2 ->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: f_inj; rewrite inE; apply/andP.
Qed.

End imfset.

Section in_imfset.

Variable (key : unit) (K : choiceType).
Implicit Types (A B : {fset K}) (a b : K).

Lemma in_fset (p : finmempred K) (k : K) : (k \in imfset key id p) = in_mem k p.
Proof. by rewrite mem_imfset; apply: inj_id. Qed.

Lemma val_in_fset A (p : finmempred _) (k : A) :
   (val k \in imfset key val p) = (in_mem k p).
Proof. by rewrite mem_imfset ?in_finmempred //; exact: val_inj. Qed.

Lemma in_fset_val A (p : finmempred [eqType of A]) (k : K) :
  (k \in imfset key val p) = if insub k is Some a then in_mem a p else false.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [a _ <- /=|kNA] := insubP; first by rewrite val_in_fset.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/imfsetP => [] [a _ k_def]; move: kNA; rewrite k_def [_ \in _]valP.
Qed.

Lemma in_fset_valT A (p : finmempred _) (k : K) (kA : k \in A) :
  (k \in imfset key val p) = in_mem [` kA] p.
Proof. by rewrite in_fset_val insubT /=. Qed.

Lemma in_fset_valP A (p : finmempred _) (k : K) :
  reflect {kA : k \in A & in_mem [` kA] p} (k \in imfset key val p).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP (imfsetP _ _ _ _)) => [|[kA xkA]]; last by exists [`kA].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /sig2_eqW[/= x Xx ->]; exists (valP x); rewrite fsetsubE.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma in_fset_valF A (p : finmempred [eqType of A]) (k : K) : k \notin A ->
  (k \in imfset key val p) = false.
Proof. by apply: contraNF => /imfsetP[/= a Xa->]. Qed.

Lemma in_fset_nil a : a \in [fset[key] x in [::]] = false.
Proof. by rewrite !mem_imfset. Qed.

Lemma in_fset_cons x (xs : seq K) a :
  (a \in [fset[key] x in x :: xs]) = ((a == x) || (a \in [fset[key] x in xs])).
Proof. by rewrite !mem_imfset. Qed.

Lemma in_fset_cat (xs ys : seq K) a :
  (a \in [fset[key] x in xs ++ ys]) =
  ((a \in [fset[key] x in xs]) || (a \in [fset[key] x in ys])).
Proof. by rewrite !mem_imfset//= mem_cat. Qed.

Definition in_fset_ (key : unit) :=
  (in_fset_cons, in_fset_nil, in_fset_cat, in_fset).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma card_in_imfset (T T' : choiceType) (f : T -> T') (p : finmempred T) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   {in p &, injective f} -> #|` (imfset key f p)| = (size (enum_finmem p)).
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> f_inj; rewrite unlock /= size_seq_fset undup_id ?size_map//.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite map_inj_in_uniq ?enum_finmem_uniq// => ??.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !enum_finmemE; apply: f_inj.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma card_imfset (T T' : choiceType) (f : T -> T') (p : finmempred _) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  injective f -> #|` (imfset key f p)| = size (enum_finmem p).
Proof. by move=> f_inj; rewrite card_in_imfset //= => x y ? ?; apply: f_inj. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma leq_imfset_card (T T' : choiceType) (f : T -> T') (p : finmempred _) :
   (#|` imfset key f p| <= size (enum_finmem p))%N.
Proof. by rewrite unlock size_seq_fset (leq_trans (size_undup _)) ?size_map. Qed.

End in_imfset.

Section Theory.

Variables (key : unit) (K K': choiceType).
Implicit Types (a b x : K) (A B C D : {fset K}) (E : {fset K'})
         (pA pB pC : pred K) (s : seq K).

Lemma fsetP {A B} : A =i B <-> A = B.
Proof. by split=> [eqAB|-> //]; apply/val_inj/canonical_eq_keys => //= a. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
CoInductive in_fset_spec (A : {fset K}) (x : K) : K -> bool -> Prop :=
 | InFset (u : A) & x = val u : in_fset_spec A x (val u) true
 | OutFset of x \notin A : in_fset_spec A x x false.

Lemma in_fsetP A x : in_fset_spec A x x (x \in A).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [xA|xNA] := boolP (x \in A); last by constructor.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have {2}-> : x = val [` xA] by []; constructor.
Qed.

Lemma fset_eqP {A B} : reflect (A =i B) (A == B).
Proof. exact: (equivP eqP (iff_sym fsetP)). Qed.

Lemma in_fset0 x : x \in fset0 = false. Proof. by []. Qed.

Lemma in_fset1 a' a : a \in [fset a'] = (a == a').
Proof. by rewrite !in_fset_ orbF. Qed.

Lemma in_fsetU A B a : (a \in A `|` B) = (a \in A) || (a \in B).
Proof. by rewrite !in_fset_. Qed.

Lemma in_fset1U a' A a : (a \in a' |` A) = (a == a') || (a \in A).
Proof. by rewrite in_fsetU in_fset1. Qed.

Lemma in_fsetI A B a : (a \in A `&` B) = (a \in A) && (a \in B).
Proof. by rewrite in_fset. Qed.

Lemma in_fsetD A B a : (a \in A `\` B) = (a \notin B) && (a \in A).
Proof. by rewrite in_fset andbC. Qed.

Lemma in_fsetD1 A b a : (a \in A `\ b) = (a != b) && (a \in A).
Proof. by rewrite in_fsetD in_fset1. Qed.

Lemma in_fsetM A E (u : K * K') : (u \in A `*` E) = (u.1 \in A) && (u.2 \in E).
Proof. by case: u => /= x y; rewrite mem_imfset2//= => -[??] [??] [-> ->]. Qed.

Definition in_fsetE :=
  (@in_fset_ imfset_key,
   val_in_fset, in_fset0, in_fset1,
   in_fsetU, in_fsetI, in_fsetD, in_fsetM,
   in_fset1U, in_fsetD1).

Let inE := (inE, in_fsetE).

Lemma fsetIC (A B : {fset K}) : A `&` B = B `&` A.
Proof. by apply/fsetP => a; rewrite !inE andbC. Qed.

Lemma fsetUC (A B : {fset K}) : A `|` B = B `|` A.
Proof. by apply/fsetP => a; rewrite !inE orbC. Qed.

Lemma fset0I A : fset0 `&` A = fset0.
Proof. by apply/fsetP => x; rewrite !inE andFb. Qed.

Lemma fsetI0 A : A `&` fset0 = fset0.
Proof. by rewrite fsetIC fset0I. Qed.

Lemma fsetIA A B C : A `&` (B `&` C) = A `&` B `&` C.
Proof. by apply/fsetP=> x; rewrite !inE andbA. Qed.

Lemma fsetICA A B C : A `&` (B `&` C) = B `&` (A `&` C).
Proof. by rewrite !fsetIA (fsetIC A). Qed.

Lemma fsetIAC A B C : A `&` B `&` C = A `&` C `&` B.
Proof. by rewrite -!fsetIA (fsetIC B). Qed.

Lemma fsetIACA A B C D : (A `&` B) `&` (C `&` D) = (A `&` C) `&` (B `&` D).
Proof. by rewrite -!fsetIA (fsetICA B). Qed.

Lemma fsetIid A : A `&` A = A.
Proof. by apply/fsetP=> x; rewrite inE andbb. Qed.

Lemma fsetIIl A B C : A `&` B `&` C = (A `&` C) `&` (B `&` C).
Proof. by rewrite fsetIA !(fsetIAC _ C) -(fsetIA _ C) fsetIid. Qed.

Lemma fsetIIr A B C : A `&` (B `&` C) = (A `&` B) `&` (A `&` C).
Proof. by rewrite !(fsetIC A) fsetIIl. Qed.

Lemma fsetUA A B C : A `|` (B `|` C) = A `|` B `|` C.
Proof. by apply/fsetP => x; rewrite !inE orbA. Qed.

Lemma fsetUCA A B C : A `|` (B `|` C) = B `|` (A `|` C).
Proof. by rewrite !fsetUA (fsetUC B). Qed.

Lemma fsetUAC A B C : A `|` B `|` C = A `|` C `|` B.
Proof. by rewrite -!fsetUA (fsetUC B). Qed.

Lemma fsetUACA A B C D : (A `|` B) `|` (C `|` D) = (A `|` C) `|` (B `|` D).
Proof. by rewrite -!fsetUA (fsetUCA B). Qed.

Lemma fsetUid A : A `|` A = A.
Proof. by apply/fsetP=> x; rewrite inE orbb. Qed.

Lemma fsetUUl A B C : A `|` B `|` C = (A `|` C) `|` (B `|` C).
Proof. by rewrite fsetUA !(fsetUAC _ C) -(fsetUA _ C) fsetUid. Qed.

Lemma fsetUUr A B C : A `|` (B `|` C) = (A `|` B) `|` (A `|` C).
Proof. by rewrite !(fsetUC A) fsetUUl. Qed.

(* distribute /cancel *)

Lemma fsetIUr A B C : A `&` (B `|` C) = (A `&` B) `|` (A `&` C).
Proof. by apply/fsetP=> x; rewrite !inE andb_orr. Qed.

Lemma fsetIUl A B C : (A `|` B) `&` C = (A `&` C) `|` (B `&` C).
Proof. by apply/fsetP=> x; rewrite !inE andb_orl. Qed.

Lemma fsetUIr A B C : A `|` (B `&` C) = (A `|` B) `&` (A `|` C).
Proof. by apply/fsetP=> x; rewrite !inE orb_andr. Qed.

Lemma fsetUIl A B C : (A `&` B) `|` C = (A `|` C) `&` (B `|` C).
Proof. by apply/fsetP=> x; rewrite !inE orb_andl. Qed.

Lemma fsetUKC A B : (A `|` B) `&` A = A.
Proof. by apply/fsetP=> x; rewrite !inE orbK. Qed.

Lemma fsetUK A B : (B `|` A) `&` A = A.
Proof. by rewrite fsetUC fsetUKC. Qed.

Lemma fsetKUC A B : A `&` (B `|` A) = A.
Proof. by rewrite fsetIC fsetUK. Qed.

Lemma fsetKU A B : A `&` (A `|` B) = A.
Proof. by rewrite fsetIC fsetUKC. Qed.

Lemma fsetIKC A B : (A `&` B) `|` A = A.
Proof. by apply/fsetP=> x; rewrite !inE andbK. Qed.

Lemma fsetIK A B : (B `&` A) `|` A = A.
Proof. by rewrite fsetIC fsetIKC. Qed.

Lemma fsetKIC A B : A `|` (B `&` A) = A.
Proof. by rewrite fsetUC fsetIK. Qed.

Lemma fsetKI A B : A `|` (A `&` B) = A.
Proof. by rewrite fsetIC fsetKIC. Qed.

Lemma fsetUKid A B : B `|` A `|` A = B `|` A.
Proof. by rewrite -fsetUA fsetUid. Qed.

Lemma fsetUKidC A B : A `|` B `|` A = A `|` B.
Proof. by rewrite fsetUAC fsetUid. Qed.

Lemma fsetKUid A B : A `|` (A `|` B) = A `|` B.
Proof. by rewrite fsetUA fsetUid. Qed.

Lemma fsetKUidC A B : A `|` (B `|` A) = B `|` A.
Proof. by rewrite fsetUCA fsetUid. Qed.

Lemma fsetIKid A B : B `&` A `&` A = B `&` A.
Proof. by rewrite -fsetIA fsetIid. Qed.

Lemma fsetIKidC A B : A `&` B `&` A = A `&` B.
Proof. by rewrite fsetIAC fsetIid. Qed.

Lemma fsetKIid A B : A `&` (A `&` B) = A `&` B.
Proof. by rewrite fsetIA fsetIid. Qed.

Lemma fsetKIidC A B : A `&` (B `&` A) = B `&` A.
Proof. by rewrite fsetICA fsetIid. Qed.

(* subset *)

Lemma fsubsetP {A B} : reflect {subset A <= B} (A `<=` B).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP fset_eqP) => AsubB a; first by rewrite -AsubB inE => /andP[].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite inE; have [/AsubB|] := boolP (a \in A).
Qed.

Lemma fset_sub_val A (p : finmempred [eqType of A]) :
  (imfset key val p) `<=` A.
Proof. by apply/fsubsetP => k /in_fset_valP []. Qed.

Lemma fset_sub A (P : pred K) : [fset x in A | P x] `<=` A.
Proof. by apply/fsubsetP => k; rewrite in_fset inE /= => /andP []. Qed.

Lemma fsetD_eq0 (A B : {fset K}) : (A `\` B == fset0) = (A `<=` B).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/fset_eqP/fsubsetP => sAB a.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> aA; have := sAB a; rewrite !inE aA andbT => /negPn.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !inE andbC; apply/negP => /andP [/sAB ->].
Qed.

Lemma fsubset_refl A : A `<=` A. Proof. exact/fsubsetP. Qed.
Hint Resolve fsubset_refl.

Definition fincl A B (AsubB : A `<=` B) (a : A) : B :=
  [` (fsubsetP AsubB) _ (valP a)].

Definition fsub B A : {set B} := [set x : B | val x \in A].

Lemma fsubE A B (AsubB : A `<=` B) :
  fsub B A = [set fincl AsubB x | x : A].
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@fmap0 K (Choice.sort V))) (@fset0 V) *)
apply/setP => x; rewrite in_set; apply/idP/imsetP => [|[[a aA] aA' ->]] //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> xA; exists [` xA]=> //; apply: val_inj.
Qed.

Lemma fincl_fsub A B (AsubB : A `<=` B) (a : A) :
  fincl AsubB a \in fsub B A.
Proof. by rewrite inE /= (valP a). Qed.

Lemma in_fsub B A (b : B) : (b \in fsub B A) = (val b \in A).
Proof. by rewrite inE. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma subset_fsubE C A B : A `<=` C -> B `<=` C ->
   (fsub C A \subset fsub C B) = (A `<=` B).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> sAC sBC; apply/subsetP/fsubsetP => sAB a; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite !inE => /sAB.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> aA; have := sAB _ (fincl_fsub sAC [` aA]); rewrite inE.
Qed.

Lemma fsubset_trans : transitive (@fsubset K).
Proof. by move=>??? s t ; apply/fsubsetP => a /(fsubsetP s) /(fsubsetP t). Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma subset_fsub A B C : A `<=` B -> B `<=` C ->
  fsub C A \subset fsub C B.
Proof. by move=> sAB sBC; rewrite subset_fsubE // (fsubset_trans sAB). Qed.

Lemma fsetIidPl {A B} : reflect (A `&` B = A) (A `<=` B).
Proof. exact: eqP. Qed.

Lemma fsetIidPr {A B} : reflect (A `&` B = B) (B `<=` A).
Proof. by rewrite fsetIC; apply: fsetIidPl. Qed.

Lemma fsubsetIidl A B : (A `<=` A `&` B) = (A `<=` B).
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsubsetP/fsubsetP=> sAB a aA; have := sAB _ aA; rewrite !inE ?aA.
Qed.

Lemma fsubsetIidr A B : (B `<=` A `&` B) = (B `<=` A).
Proof. by rewrite fsetIC fsubsetIidl. Qed.

Lemma fsetUidPr A B : reflect (A `|` B = B) (A `<=` B).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP fsubsetP) => sAB; last by move=> a aA; rewrite -sAB inE aA.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsetP => b; rewrite inE; have [/sAB|//] := boolP (_ \in _).
Qed.

Lemma fsetUidPl A B : reflect (A `|` B = A) (B `<=` A).
Proof. by rewrite fsetUC; apply/fsetUidPr. Qed.

Lemma fsubsetUl A B : A `<=` A `|` B.
Proof. by apply/fsubsetP => a; rewrite inE => ->. Qed.
Hint Resolve fsubsetUl.

Lemma fsubsetUr A B : B `<=` A `|` B.
Proof. by rewrite fsetUC. Qed.
Hint Resolve fsubsetUr.

Lemma fsubsetU1 x A : A `<=` x |` A.
Proof. by rewrite fsubsetUr. Qed.
Hint Resolve fsubsetU1.

Lemma fsubsetU A B C : (A `<=` B) || (A `<=` C) -> A `<=` B `|` C.
Proof. by move=> /orP [] /fsubset_trans ->. Qed.

Lemma fincl_inj A B (AsubB : A `<=` B) : injective (fincl AsubB).
Proof. by move=> a b [eq_ab]; apply: val_inj. Qed.
Hint Resolve fincl_inj.

Lemma fsub_inj B : {in [pred A | A `<=` B] &, injective (fsub B)}.
Proof.
(* Goal: @prop_in2 (@finset_of K (Phant (Choice.sort K))) (@mem (@finset_of K (Phant (Choice.sort K))) (simplPredType (@finset_of K (Phant (Choice.sort K)))) (@SimplPred (@finset_of K (Phant (Choice.sort K))) (fun A : @finset_of K (Phant (Choice.sort K)) => @fsubset K A B))) (fun x1 x2 : @finset_of K (Phant (Choice.sort K)) => forall _ : @eq (@set_of (@fset_sub_finType K B) (Phant (@fset_sub_type K B))) (fsub B x1) (fsub B x2), @eq (@finset_of K (Phant (Choice.sort K))) x1 x2) (inPhantom (@injective (@set_of (@fset_sub_finType K B) (Phant (@fset_sub_type K B))) (@finset_of K (Phant (Choice.sort K))) (fsub B))) *)
move=> A A'; rewrite -!topredE /= => sAB sA'B /setP eqAA'; apply/fsetP => a.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/idP/idP => mem_a.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have := eqAA' (fincl sAB [` mem_a]); rewrite !inE // => <-.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have := eqAA' (fincl sA'B [` mem_a]); rewrite !inE // => ->.
Qed.
Hint Resolve fsub_inj.

Lemma eqEfsubset A B : (A == B) = (A `<=` B) && (B `<=` A).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/eqP/andP => [-> //|[/fsubsetP AB /fsubsetP BA]].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsetP=> x; apply/idP/idP=> [/AB|/BA].
Qed.

Lemma subEfproper A B : A `<=` B = (A == B) || (A `<` B).
Proof. by rewrite eqEfsubset -andb_orr orbN andbT. Qed.

Lemma fproper_sub A B : A `<` B -> A `<=` B.
Proof. by rewrite subEfproper orbC => ->. Qed.

Lemma eqVfproper A B : A `<=` B -> A = B \/ A `<` B.
Proof. by rewrite subEfproper => /predU1P. Qed.

Lemma fproperEneq A B : A `<` B = (A != B) && (A `<=` B).
Proof. by rewrite andbC eqEfsubset negb_and andb_orr andbN. Qed.

Lemma fproper_neq A B : A `<` B -> A != B.
Proof. by rewrite fproperEneq; case/andP. Qed.

Lemma fproper_irrefl A : ~~ (A `<` A).
Proof. by rewrite fproperEneq eqxx. Qed.

Lemma eqEfproper A B : (A == B) = (A `<=` B) && ~~ (A `<` B).
Proof. by rewrite negb_and negbK andb_orr andbN eqEfsubset. Qed.

Lemma card_fsub B A : A `<=` B -> #|fsub B A| = #|` A|.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> sAB; rewrite cardfE fsubE card_imset //; apply: fincl_inj.
Qed.

Lemma eqEfcard A B : (A == B) = (A `<=` B) &&
  (#|` B| <= #|` A|)%N.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite -(inj_in_eq (@fsub_inj (A `|` B))) -?topredE //=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite eqEcard !(@subset_fsubE (A `|` B)) ?(@card_fsub (A `|` B)).
Qed.

Lemma fproperEcard A B :
  (A `<` B) = (A `<=` B) && (#|` A| < #|` B|)%N.
Proof. by rewrite fproperEneq ltnNge andbC eqEfcard; case: (A `<=` B). Qed.

Lemma fsubset_leqif_cards A B : A `<=` B -> (#|` A| <= #|` B| ?= iff (A == B))%N.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite -!(@card_fsub (A `|` B)) // -(@subset_fsubE (A `|` B)) //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /subset_leqif_cards; rewrite (inj_in_eq (@fsub_inj _)) -?topredE /=.
Qed.

Lemma fsub0set A : fset0 `<=` A.
Proof. by apply/fsubsetP=> x; rewrite inE. Qed.
Hint Resolve fsub0set.

Lemma fsubset0 A : (A `<=` fset0) = (A == fset0).
Proof. by rewrite eqEfsubset fsub0set andbT. Qed.

Lemma fproper0 A : (fset0 `<` A) = (A != fset0).
Proof. by rewrite /fproper fsub0set fsubset0. Qed.

Lemma fproperE A B : (A `<` B) = (A `<=` B) && ~~ (B `<=` A).
Proof. by []. Qed.

Lemma fsubEproper A B : (A `<=` B) = (A == B) || (A `<` B).
Proof. by rewrite fproperEneq; case: eqP => //= ->; apply: fsubset_refl. Qed.

Lemma fsubset_leq_card A B : A `<=` B -> (#|` A| <= #|` B|)%N.
Proof. by move=> /fsubset_leqif_cards ->. Qed.

Lemma fproper_ltn_card A B : A `<` B -> (#|` A| < #|` B|)%N.
Proof. by rewrite fproperEcard => /andP []. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fsubset_cardP A B : #|` A| = #|` B| ->
  reflect (A =i B) (A `<=` B).
Proof.
(* Goal: forall (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f)))) (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f')))) (_ : @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@setf K V f k v) (@setf K V f' k v)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f f' *)
move=> eq_cardAB; apply: (iffP idP) => [/eqVfproper [->//|]|/fsetP -> //].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fproperEcard eq_cardAB ltnn andbF.
Qed.

Lemma fproper_sub_trans B A C : A `<` B -> B `<=` C -> A `<` C.
Proof.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite !fproperEcard => /andP [sAB lt_AB] sBC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (fsubset_trans sAB) //= (leq_trans lt_AB) // fsubset_leq_card.
Qed.

Lemma fsub_proper_trans B A C :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  A `<=` B -> B `<` C -> A `<` C.
Proof.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite !fproperEcard => sAB /andP [sBC lt_BC].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (fsubset_trans sAB) //= (leq_ltn_trans _ lt_BC) // fsubset_leq_card.
Qed.

Lemma fsubset_neq0 A B : A `<=` B -> A != fset0 -> B != fset0.
Proof. by rewrite -!fproper0 => sAB /fproper_sub_trans->. Qed.

(* fsub is a morphism *)

Lemma fsub0 A : fsub A fset0 = set0 :> {set A}.
Proof. by apply/setP => x; rewrite !inE. Qed.

Lemma fsubT A : fsub A A = [set : A].
Proof. by apply/setP => x; rewrite !inE (valP x). Qed.

Lemma fsub1 A a (aA : a \in A) : fsub A [fset a] = [set [` aA]] :> {set A}.
Proof. by apply/setP=> x; rewrite !inE; congr eq_op. Qed.

Lemma fsubU C A B : fsub C (A `|` B) = fsub C A :|: fsub C B.
Proof. by apply/setP => x; rewrite !inE. Qed.

Lemma fsubI C A B : fsub C (A `&` B) = fsub C A :&: fsub C B.
Proof. by apply/setP => x; rewrite !inE. Qed.

Lemma fsubD C A B : fsub C (A `\` B) = fsub C A :\: fsub C B.
Proof. by apply/setP => x; rewrite !inE andbC. Qed.

Lemma fsubD1 C A b (bC : b \in C) : fsub C (A `\ b) = fsub C A :\ [` bC].
Proof. by rewrite fsubD fsub1. Qed.

Lemma fsub_eq0 A B : A `<=` B -> (fsub B A == set0) = (A == fset0).
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> sAB; rewrite -fsub0 (inj_in_eq (@fsub_inj _)) -?topredE /=.
Qed.

Lemma fset_0Vmem A : (A = fset0) + {x : K | x \in A}.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [|[x mem_x]] := set_0Vmem (fsub A A); last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by right; exists (val x); rewrite inE // in mem_x.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /eqP; rewrite fsub_eq0 // => /eqP; left.
Qed.

Lemma fset1P x a : reflect (x = a) (x \in [fset a]).
Proof. by rewrite inE; exact: eqP. Qed.

Lemma fset11 x : x \in [fset x].
Proof. by rewrite inE. Qed.

Lemma fset1_inj : injective (@fset1 K).
Proof. by move=> a b eqsab; apply/fset1P; rewrite -eqsab fset11. Qed.

Lemma fset1UP x a B : reflect (x = a \/ x \in B) (x \in a |` B).
Proof. by rewrite !inE; exact: predU1P. Qed.

Lemma fset_cons a s : [fset[key] x in a :: s] = a |` [fset[key] x in s].
Proof. by apply/fsetP=> x; rewrite in_fset_cons !inE. Qed.

Lemma fset_nil : [fset[key] x in [::] : seq K] = fset0.
Proof. by apply/fsetP=> x; rewrite in_fset_nil. Qed.

Lemma fset_cat s s' :
   [fset[key] x in s ++ s'] = [fset[key] x in s] `|` [fset[key] x in s'].
Proof. by apply/fsetP=> x; rewrite !inE !in_fset_cat. Qed.

Lemma fset1U1 x B : x \in x |` B. Proof. by rewrite !inE eqxx. Qed.

Lemma fset1Ur x a B : x \in B -> x \in a |` B.
Proof. by move=> Bx; rewrite !inE predU1r. Qed.

(* We need separate lemmas for the explicit enumerations since they *)
(* associate on the left.                                           *)
Lemma fsetU1l x A b : x \in A -> x \in A `|` [fset b].
Proof. by move=> Ax; rewrite !inE Ax. Qed.

Lemma fsetU1r A b : b \in A `|` [fset b].
Proof. by rewrite !inE eqxx orbT. Qed.

Lemma fsetD1P x A b : reflect (x != b /\ x \in A) (x \in A `\ b).
Proof. by rewrite !inE; exact: andP. Qed.

Lemma fsetD11 b A : (b \in A `\ b) = false. Proof. by rewrite !inE eqxx. Qed.

Lemma fsetD1K a A : a \in A -> a |` (A `\ a) = A.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> Aa; apply/fsetP=> x; rewrite !inE; case: eqP => // ->.
Qed.

Lemma fsetU1K a B : a \notin B -> (a |` B) `\ a = B.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move/negPf=> nBa; apply/fsetP=> x; rewrite !inE; case: eqP => // ->.
Qed.

Lemma fset2P x a b : reflect (x = a \/ x = b) (x \in [fset a; b]).
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !inE; apply: (iffP orP) => [] [] /eqP ->; [left|right|left|right].
Qed.

Lemma in_fset2 x a b : (x \in [fset a; b]) = (x == a) || (x == b).
Proof. by rewrite !inE. Qed.

Lemma fset21 a b : a \in [fset a; b]. Proof. by rewrite fset1U1. Qed.

Lemma fset22 a b : b \in [fset a; b]. Proof. by rewrite in_fset2 eqxx orbT. Qed.

Lemma fsetUP x A B : reflect (x \in A \/ x \in B) (x \in A `|` B).
Proof. by rewrite !inE; exact: orP. Qed.

Lemma fsetULVR x A B : x \in A `|` B -> (x \in A) + (x \in B).
Proof. by rewrite inE; case: (x \in A); [left|right]. Qed.

Lemma fsetUS A B C : A `<=` B -> C `|` A `<=` C `|` B.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> sAB; apply/fsubsetP=> x; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (x \in C) => //; exact: (fsubsetP sAB).
Qed.

Lemma fsetSU A B C : A `<=` B -> A `|` C `<=` B `|` C.
Proof. by move=> sAB; rewrite -!(fsetUC C) fsetUS. Qed.

Lemma fsetUSS A B C D : A `<=` C -> B `<=` D -> A `|` B `<=` C `|` D.
Proof. by move=> /(fsetSU B) /fsubset_trans sAC /(fsetUS C)/sAC. Qed.

Lemma fset0U A : fset0 `|` A = A.
Proof. by apply/fsetP => x; rewrite !inE orFb. Qed.

Lemma fsetU0 A : A `|` fset0 = A.
Proof. by rewrite fsetUC fset0U. Qed.

(* intersection *)

Lemma fsetIP x A B : reflect (x \in A /\ x \in B) (x \in A `&` B).
Proof. by rewrite inE; apply: andP. Qed.

Lemma fsetIS A B C : A `<=` B -> C `&` A `<=` C `&` B.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> sAB; apply/fsubsetP=> x; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (x \in C) => //; exact: (fsubsetP sAB).
Qed.

Lemma fsetSI A B C : A `<=` B -> A `&` C `<=` B `&` C.
Proof. by move=> sAB; rewrite -!(fsetIC C) fsetIS. Qed.

Lemma fsetISS A B C D : A `<=` C -> B `<=` D -> A `&` B `<=` C `&` D.
Proof. by move=> /(fsetSI B) /fsubset_trans sAC /(fsetIS C) /sAC. Qed.

(* difference *)

Lemma fsetDP A B x : reflect (x \in A /\ x \notin B) (x \in A `\` B).
Proof. by rewrite inE andbC; apply: andP. Qed.

Lemma fsetSD C A B : A `<=` B -> A `\` C `<=` B `\` C.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> sAB; apply/fsubsetP=> x; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (x \in C) => //; exact: (fsubsetP sAB).
Qed.

Lemma fsetDS C A B : A `<=` B -> C `\` B `<=` C `\` A.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> sAB; apply/fsubsetP=> x; rewrite !inE ![_ && (_ \in _)]andbC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (x \in C) => //; apply: contra; exact: (fsubsetP sAB).
Qed.

Lemma fsetDSS A B C D : A `<=` C -> D `<=` B -> A `\` B `<=` C `\` D.
Proof. by move=> /(fsetSD B) /fsubset_trans sAC /(fsetDS C) /sAC. Qed.

Lemma fsetD0 A : A `\` fset0 = A.
Proof. by apply/fsetP=> x; rewrite !inE. Qed.

Lemma fset0D A : fset0 `\` A = fset0.
Proof. by apply/fsetP=> x; rewrite !inE andbF. Qed.

Lemma fsetDv A : A `\` A = fset0.
Proof. by apply/fsetP=> x; rewrite !inE andNb. Qed.

Lemma fsetID B A : A `&` B `|` A `\` B = A.
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDUl A B C : (A `|` B) `\` C = (A `\` C) `|` (B `\` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDUr A B C : A `\` (B `|` C) = (A `\` B) `&` (A `\` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDIl A B C : (A `&` B) `\` C = (A `\` C) `&` (B `\` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetIDA A B C : A `&` (B `\` C) = (A `&` B) `\` C.
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetIDAC A B C : (A `\` B) `&` C = (A `&` C) `\` B.
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDIr A B C : A `\` (B `&` C) = (A `\` B) `|` (A `\` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDDl A B C : (A `\` B) `\` C = A `\` (B `|` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDDr A B C : A `\` (B `\` C) = (A `\` B) `|` (A `&` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetDK A B : B `<=` A -> A `\` (A `\` B) = B.
Proof. by rewrite fsetDDr => /fsetIidPr->; rewrite fsetDv fset0U. Qed.

Lemma fsetUDl (A B C : {fset K}) : A `|` (B `\` C) = (A `|` B) `\` (C `\` A).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

Lemma fsetUDr (A B C : {fset K}) : (A `\` B) `|` C = (A `|` C) `\` (B `\` C).
Proof. by apply/fsetP=> x; rewrite !inE; do ?case: (_ \in _). Qed.

(* other inclusions *)

Lemma fsubsetIl A B : A `&` B `<=` A.
Proof. by apply/fsubsetP=> x; rewrite inE => /andP []. Qed.

Lemma fsubsetIr A B : A `&` B `<=` B.
Proof. by apply/fsubsetP=> x; rewrite inE => /andP []. Qed.

Lemma fsubsetDl A B : A `\` B `<=` A.
Proof. by apply/fsubsetP=> x; rewrite inE => /andP []. Qed.

Lemma fsubD1set A x : A `\ x `<=` A.
Proof. by rewrite fsubsetDl. Qed.

Lemma fsubsetD2l C A B : A `<=` C -> B `<=` C -> (C `\` B `<=` C `\` A) = (A `<=` B).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> sAC sBC; apply/idP/idP; last exact: fsetDS.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /(@fsetDS C); rewrite !fsetDK //; apply; apply: fsubsetDl.
Qed.

Hint Resolve fsubsetIl fsubsetIr fsubsetDl fsubD1set.

(* cardinal lemmas for fsets *)

Lemma card_finset (T : finType) (P : pred T) : #|` [fset x in P] | = #|P|.
Proof. by rewrite card_imfset //= -cardE. Qed.

Lemma card_fset (T : choiceType) (A : {fset T}) : #|` [fset x in A] | = #|` A|.
Proof. by rewrite card_imfset. Qed.

Lemma card_fseq (T : choiceType) (s : seq T) : #|` [fset x in s] | = size (undup s).
Proof. by rewrite card_imfset. Qed.

Lemma cardfs0 : #|` @fset0 K| = 0.
Proof. by rewrite -(@card_fsub fset0) // fsub0 cards0. Qed.

Lemma cardfT0 : #|{: @fset0 K}| = 0.
Proof. by rewrite -cardfE cardfs0. Qed.

Lemma cardfs_eq0 A : (#|` A| == 0) = (A == fset0).
Proof. by rewrite -(@card_fsub A) // cards_eq0 fsub_eq0. Qed.

Lemma cardfs0_eq A : #|` A| = 0 -> A = fset0.
Proof. by move=> /eqP; rewrite cardfs_eq0 => /eqP. Qed.

Lemma fset0Pn A : reflect (exists x, x \in A) (A != fset0).
Proof.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite -cardfs_eq0 cardfE; apply: (equivP existsP).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by split=> [] [a aP]; [exists (val a); apply: valP|exists [` aP]].
Qed.

Lemma cardfs_gt0 A : (0 < #|` A|)%N = (A != fset0).
Proof. by rewrite lt0n cardfs_eq0. Qed.

Lemma cardfs1 x : #|` [fset x]| = 1.
Proof. by rewrite card_imfset. Qed.

Lemma cardfsUI A B : #|` A `|` B| + #|` A `&` B| = #|` A| + #|` B|.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite -!(@card_fsub (A `|` B)) ?(fsubset_trans (fsubsetIl _ _)) //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fsubU fsubI cardsUI.
Qed.

Lemma cardfsU A B : #|` A `|` B| = (#|` A| + #|` B| - #|` A `&` B|)%N.
Proof. by rewrite -cardfsUI addnK. Qed.

Lemma cardfsI A B : #|` A `&` B| = (#|` A| + #|` B| - #|` A `|` B|)%N.
Proof. by rewrite  -cardfsUI addKn. Qed.

Lemma cardfsID B A : #|` A `&` B| + #|` A `\` B| = #|` A|.
Proof. by rewrite -!(@card_fsub A) // fsubI fsubD cardsID. Qed.

Lemma cardfsD A B : #|` A `\` B| = (#|` A| - #|` A `&` B|)%N.
Proof. by rewrite -(cardfsID B A) addKn. Qed.

Lemma mem_fset1U a A : a \in A -> a |` A = A.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> aA; apply/fsetP => x; rewrite !inE orbC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [//|/=] := boolP (_ \in A); apply: contraNF => /eqP ->.
Qed.

Lemma mem_fsetD1 a A : a \notin A -> A `\ a = A.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> aA; apply/fsetP => x; rewrite !inE andbC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [/= xA|//] := boolP (_ \in A); apply: contraNneq aA => <-.
Qed.

Lemma fsetI1 a A : A `&` [fset a] = if a \in A then [fset a] else fset0.
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsetP => x; rewrite (fun_if (fun X => _ \in X)) !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [[->|?] []] := (altP (x =P a), boolP (a \in A)); rewrite ?andbF.
Qed.

Lemma cardfsU1 a A : #|` a |` A| = (a \notin A) + #|` A|.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [aA|aNA] := boolP (a \in A); first by rewrite mem_fset1U.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite cardfsU -addnBA ?fsubset_leq_card // fsetIC -cardfsD.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite mem_fsetD1 // cardfs1.
Qed.

Lemma cardfs2 a b : #|` [fset a; b]| = (a != b).+1.
Proof. by rewrite !cardfsU1 cardfs1 inE addn1. Qed.

Lemma cardfsD1 a A : #|` A| = (a \in A) + #|` A `\ a|.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite -(cardfsID [fset a]) fsetI1 (fun_if (fun A => #|` A|)).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite cardfs0 cardfs1; case: (_ \in _).
Qed.

(* other inclusions *)

Lemma fsub1set A x : ([fset x] `<=` A) = (x \in A).
Proof.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite -(@subset_fsubE (x |` A)) // fsub1 ?fset1U1 // => xxA.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite sub1set inE.
Qed.

Lemma cardfs1P A : reflect (exists x, A = [fset x]) (#|` A| == 1).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP idP) => [|[x ->]]; last by rewrite cardfs1.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
rewrite eq_sym eqn_leq cardfs_gt0=> /andP[/fset0Pn[x Ax] leA1].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by exists x; apply/eqP; rewrite eq_sym eqEfcard fsub1set cardfs1 leA1 Ax.
Qed.

Lemma fsubset1 A x : (A `<=` [fset x]) = (A == [fset x]) || (A == fset0).
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite eqEfcard cardfs1 -cardfs_eq0 orbC andbC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: posnP => // A0; rewrite (cardfs0_eq A0) fsub0set.
Qed.

Arguments fsetIidPl {A B}.

Lemma cardfsDS A B : B `<=` A -> #|` A `\` B| = (#|` A| - #|` B|)%N.
Proof. by rewrite cardfsD => /fsetIidPr->. Qed.

Lemma fsubIset A B C : (B `<=` A) || (C `<=` A) -> (B `&` C `<=` A).
Proof. by case/orP; apply: fsubset_trans; rewrite (fsubsetIl, fsubsetIr). Qed.

Lemma fsubsetI A B C : (A `<=` B `&` C) = (A `<=` B) && (A `<=` C).
Proof.
(* Goal: @eq bool (@fsubset K A (@fsetI K B C)) (andb (@fsubset K A B) (@fsubset K A C)) *)
rewrite !(sameP fsetIidPl eqP) fsetIA; have [-> //| ] := altP (A `&` B =P A).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: contraNF => /eqP <-; rewrite -fsetIA -fsetIIl fsetIAC.
Qed.

Lemma fsubsetIP A B C : reflect (A `<=` B /\ A `<=` C) (A `<=` B `&` C).
Proof. by rewrite fsubsetI; exact: andP. Qed.

Lemma fsubUset A B C : (B `|` C `<=` A) = (B `<=` A) && (C `<=` A).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply/idP/idP => [subA|/andP [AB CA]]; last by rewrite -[A]fsetUid fsetUSS.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !(fsubset_trans _ subA).
Qed.

Lemma fsubUsetP A B C : reflect (A `<=` C /\ B `<=` C) (A `|` B `<=` C).
Proof. by rewrite fsubUset; exact: andP. Qed.

Lemma fsubDset A B C : (A `\` B `<=` C) = (A `<=` B `|` C).
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsubsetP/fsubsetP=> sABC x; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by case Bx: (x \in B) => // Ax; rewrite sABC ?in_fsetD ?Bx.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case Bx: (x \in B) => //; move/sABC; rewrite inE Bx.
Qed.

Lemma fsetU_eq0 A B : (A `|` B == fset0) = (A == fset0) && (B == fset0).
Proof. by rewrite -!fsubset0 fsubUset. Qed.

Lemma fsubsetD1 A B x : (A `<=` B `\ x) = (A `<=` B) && (x \notin A).
Proof.
(* Goal: @eq bool (@fsubset K A (@fsetD K B (@fset1 K x))) (andb (@fsubset K A B) (negb (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) A)))) *)
do !rewrite -(@subset_fsubE (x |` A `|` B)) ?fsubDset ?fsetUA // 1?fsetUAC //.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite fsubD1 => [|mem_x]; first by rewrite -fsetUA fset1U1.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite subsetD1 // inE.
Qed.

Lemma fsubsetD1P A B x : reflect (A `<=` B /\ x \notin A) (A `<=` B `\ x).
Proof. by rewrite fsubsetD1; exact: andP. Qed.

Lemma fsubsetPn A B : reflect (exists2 x, x \in A & x \notin B) (~~ (A `<=` B)).
Proof.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
 rewrite -fsetD_eq0; apply: (iffP (fset0Pn _)) => [[x]|[x xA xNB]].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite inE => /andP[]; exists x.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by exists x; rewrite inE xA xNB.
Qed.

Lemma fproperD1 A x : x \in A -> A `\ x `<` A.
Proof.
(* Goal: forall _ : is_true (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) A)), is_true (@fproper K (@fsetD K A (@fset1 K x)) A) *)
move=> Ax; rewrite fproperE fsubsetDl; apply/fsubsetPn; exists x=> //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite in_fsetD1 Ax eqxx.
Qed.

Lemma fproperIr A B : ~~ (B `<=` A) -> A `&` B `<` B.
Proof. by move=> nsAB; rewrite fproperE fsubsetIr fsubsetI negb_and nsAB. Qed.

Lemma fproperIl A B : ~~ (A `<=` B) -> A `&` B `<` A.
Proof. by move=> nsBA; rewrite fproperE fsubsetIl fsubsetI negb_and nsBA orbT. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fproperUr A B : ~~ (A `<=` B) ->  B `<` A `|` B.
Proof. by rewrite fproperE fsubsetUr fsubUset fsubset_refl /= andbT. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fproperUl A B : ~~ (B `<=` A) ->  A `<` A `|` B.
Proof. by move=> not_sBA; rewrite fsetUC fproperUr. Qed.

Lemma fproper1set A x : ([fset x] `<` A) -> (x \in A).
Proof. by move/fproper_sub; rewrite fsub1set. Qed.

Lemma fproperIset A B C : (B `<` A) || (C `<` A) -> (B `&` C `<` A).
Proof. by case/orP; apply: fsub_proper_trans; rewrite (fsubsetIl, fsubsetIr). Qed.

Lemma fproperI A B C : (A `<` B `&` C) -> (A `<` B) && (A `<` C).
Proof.
(* Goal: forall (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f)))) (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f')))) (_ : @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@setf K V f k v) (@setf K V f' k v)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f f' *)
move=> pAI; apply/andP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by split; apply: (fproper_sub_trans pAI); rewrite (fsubsetIl, fsubsetIr).
Qed.

Lemma fproperU A B C : (B `|` C `<` A) -> (B `<` A) && (C `<` A).
Proof.
(* Goal: forall (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f)))) (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f')))) (_ : @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@setf K V f k v) (@setf K V f' k v)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f f' *)
move=> pUA; apply/andP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by split; apply: fsub_proper_trans pUA; rewrite (fsubsetUr, fsubsetUl).
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fsetDpS C A B : B `<=` C ->  A `<` B -> C `\` B `<` C `\` A.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> subBC subAB; rewrite fproperEneq fsetDS 1?fproper_sub// andbT.
(* Goal: is_true (negb (@eq_op (fset_eqType K) (@fsetD K C B) (@fsetD K C A))) *)
apply/negP => /eqP /(congr1 (fsetD C)); rewrite !fsetDK // => [eqAB//|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
 by rewrite eqAB (negPf (fproper_irrefl _)) in subAB.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: fsubset_trans subBC; apply: fproper_sub.
Qed.

Lemma fproperD2l C A B : A `<=` C -> B `<=` C -> (C `\` B `<` C `\` A) = (A `<` B).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> sAC sBC; apply/idP/idP; last exact: fsetDpS.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /(@fsetDpS C); rewrite !fsetDK //; apply; apply: fsubsetDl.
Qed.

Lemma fsetI_eq0 A B : (A `&` B == fset0) = [disjoint A & B].
Proof. by []. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fdisjoint_sub {A B} : [disjoint A & B]%fset ->
  forall C : {fset K}, [disjoint fsub C A & fsub C B]%bool.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K A B)) (C : @finset_of K (Phant (Choice.sort K))), is_true (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) *)
move=> disjointAB C; apply/pred0P => a /=; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have /eqP /fsetP /(_ (val a)) := disjointAB; rewrite !inE.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma disjoint_fsub C A B : A `|` B `<=` C ->
  [disjoint fsub C A & fsub C B]%bool = [disjoint A & B].
Proof.
(* Goal: forall _ : is_true (@fsubset K (@fsetU K A B) C), @eq bool (@disjoint (@fset_sub_finType K C) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C A))) (@mem (Finite.sort (@fset_sub_finType K C)) (predPredType (Finite.sort (@fset_sub_finType K C))) (@SetDef.pred_of_set (@fset_sub_finType K C) (fsub C B)))) (@fdisjoint K A B) *)
move=> ABsubC.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@restrictf K (Choice.sort V) f A)) (@Imfset.imfset imfset_key (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) V (fun k : Choice.sort (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@subfinset_finpred (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@fin_finpred (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (pred_finpredType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A)))) (Phantom (mem_pred (@fset_sub_type K (@domf K (Choice.sort V) f))) (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => andb (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) k (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (@in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A))))))) *)
apply/idP/idP=> [/pred0P DAB|/fdisjoint_sub->//]; apply/eqP/fsetP=> a.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite !inE; have [aC|] := boolP (a \in A `|` B); last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite !inE => /norP [/negPf-> /negPf->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have /= := DAB [` fsubsetP ABsubC _ aC]; rewrite !inE.
Qed.

Lemma fdisjointP {A B} :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  reflect (forall a, a \in A -> a \notin B) [disjoint A & B]%fset.
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply: (iffP eqP) => [AIB_eq0 a aA|neq_ab].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have /fsetP /(_ a) := AIB_eq0; rewrite !inE aA /= => ->.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsetP => a; rewrite !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (boolP (a \in A)) => // /neq_ab /negPf ->.
Qed.

Lemma fsetDidPl A B : reflect (A `\` B = A) [disjoint A & B]%fset.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP fdisjointP)=> [NB|<- a]; last by rewrite inE => /andP[].
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsetP => a; rewrite !inE andbC.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (boolP (a \in A)) => //= /NB ->.
Qed.

Lemma disjoint_fsetI0 A B : [disjoint A & B] -> A `&` B = fset0.
Proof. by rewrite -fsetI_eq0; move/eqP. Qed.

Lemma fsubsetD A B C :
  (A `<=` (B `\` C)) = (A `<=` B) && [disjoint A & C]%fset.
Proof.
(* Goal: @eq bool (@fsubset K A (@fsetD K B C)) (andb (@fsubset K A B) (@fdisjoint K A C)) *)
pose D := A `|` B `|` C.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have AD : A `<=` D by rewrite /D -fsetUA fsubsetUl.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have BD : B `<=` D by rewrite /D fsetUAC fsubsetUr.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite -(@subset_fsubE D) //; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite fsubDset (fsubset_trans BD) // fsubsetUr.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite fsubD subsetD !subset_fsubE // disjoint_fsub //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite /D fsetUAC fsubsetUl.
Qed.

Lemma fsubsetDP A B C :
   reflect (A `<=` B /\ [disjoint A & C]%fset) (A `<=` (B `\` C)).
Proof. by rewrite fsubsetD; apply: andP. Qed.

Lemma fdisjoint_sym A B : [disjoint A & B] = [disjoint B & A].
Proof. by rewrite -!fsetI_eq0 fsetIC. Qed.

Lemma fdisjointP_sym {A B} :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  reflect (forall a, a \in A -> a \notin B) [disjoint B & A]%fset.
Proof. by rewrite fdisjoint_sym; apply: fdisjointP. Qed.

Lemma fdisjoint_trans A B C :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   A `<=` B -> [disjoint B & C] -> [disjoint A & C].
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> AsubB; rewrite -!(@disjoint_fsub (B `|` C)) ?fsetSU //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: disjoint_trans; rewrite subset_fsub.
Qed.

Lemma fdisjoint0X A : [disjoint fset0 & A].
Proof. by rewrite -fsetI_eq0 fset0I. Qed.

Lemma fdisjointX0 A : [disjoint A & fset0].
Proof. by rewrite -fsetI_eq0 fsetI0. Qed.

Lemma fdisjoint1X x A : [disjoint [fset x] & A] = (x \notin A).
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite -(@disjoint_fsub (x |` A)) //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (@eq_disjoint1 _ [` fset1U1 _ _]) ?inE =>// ?; rewrite !inE.
Qed.

Lemma fdisjointX1 x A : [disjoint A & [fset x]] = (x \notin A).
Proof. by rewrite fdisjoint_sym fdisjoint1X. Qed.

Lemma fdisjointUX A B C :
   [disjoint A `|` B & C] = [disjoint A & C]%fset && [disjoint B & C]%fset.
Proof. by rewrite -!fsetI_eq0 fsetIUl fsetU_eq0. Qed.

Lemma fdisjointXU A B C :
   [disjoint A & B `|` C] = [disjoint A & B]%fset && [disjoint A & C]%fset.
Proof. by rewrite -!fsetI_eq0 fsetIUr fsetU_eq0. Qed.

Lemma fdisjointU1X x A B :
   [disjoint x |` A & B]%fset = (x \notin B) && [disjoint A & B]%fset.
Proof. by rewrite fdisjointUX fdisjoint1X. Qed.

Lemma fsubK A B : A `<=` B -> [fsetval k in fsub B A] = A.
Proof.
(* Goal: forall _ : is_true (@fsubset K A B), @eq (@finset_of K (Phant (Choice.sort K))) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@subFin_sort K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B))) K (fun k : Choice.sort (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@subFin_sort K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B))) => @val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@subFin_sort K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B)) k) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@subFin_sort K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B)))) (predPredType (Finite.sort (@fset_sub_finType K B))) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@subFin_sort K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B)))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) B)) (@fset_sub_subfinType K B))) (@SetDef.pred_of_set (@fset_sub_finType K B) (fsub B A)))) (Phantom (mem_pred (Finite.sort (@fset_sub_finType K B))) (@mem (Finite.sort (@fset_sub_finType K B)) (predPredType (Finite.sort (@fset_sub_finType K B))) (@SetDef.pred_of_set (@fset_sub_finType K B) (fsub B A))))) A *)
move=> AsubB; apply/fsetP => k /=; symmetry.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [kB|kNB] := (boolP (k \in B)).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
   by rewrite in_fset_valT /= inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite in_fset_valF //; apply: contraNF kNB; apply/fsubsetP.
Qed.

Lemma FSetK A (X : {set A}) : fsub A [fsetval k in X] = X.
Proof. by apply/setP => x; rewrite !inE. Qed.

End Theory.
Hint Resolve fsubset_refl.
Hint Resolve fsubset_trans.
Hint Resolve fproper_irrefl.
Hint Resolve fsub0set.

Module Import FSetInE.
Definition inE := (inE, in_fsetE).
End FSetInE.

Section Card.

(* Lemma card_finset (T : finType) (P : pred T) : #|` [fset x in P] | = #|P|. *)
(* Proof. *)
(* rewrite cardfE cardE; apply/eqP. *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* rewrite -uniq_size_uniq ?fset_uniq ?enum_uniq // => x. *)
(* by rewrite !inE mem_enum. *)
(* Qed. *)

End Card.

Section Enum.

Lemma enum_fset0 (T : choiceType) :
  enum [finType of fset0] = [::] :> seq (@fset0 T).
Proof. by rewrite enumT unlock. Qed.

Lemma enum_fset1 (T : choiceType) (x : T) :
  enum [finType of [fset x]] = [:: [`fset11 x]].
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@restrictf K (Choice.sort V) f A)) (@Imfset.imfset imfset_key (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) V (fun k : Choice.sort (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@subfinset_finpred (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@fin_finpred (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (pred_finpredType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A)))) (Phantom (mem_pred (@fset_sub_type K (@domf K (Choice.sort V) f))) (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => andb (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) k (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (@in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A))))))) *)
apply/perm_eq_small=> //; apply/uniq_perm_eq => //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply/enum_uniq.
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default x) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) x)) (default x) *)
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default (@fsval K S u)) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) (@fsval K S u))) (h u) *)
case=> [y hy]; rewrite mem_seq1 mem_enum /in_mem /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite eqE /=; rewrite in_fset1 in hy.
Qed.

End Enum.

Section ImfsetTh.
Variables (key : unit) (K V : choiceType).
Implicit Types (f : K -> V) (g : V -> K) (A V : {fset K}).

Lemma imfset_id (A : {fset K}) : id @` A = A.
Proof. by apply/fsetP=> a; rewrite in_fset. Qed.

Lemma imfset_comp f g (p : finmempred _) :
  imfset key (g \o f) p = g @` (imfset key f p).
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@restrictf K (Choice.sort V) f A)) (@Imfset.imfset imfset_key (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) V (fun k : Choice.sort (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@subfinset_finpred (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@fin_finpred (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (pred_finpredType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A)))) (Phantom (mem_pred (@fset_sub_type K (@domf K (Choice.sort V) f))) (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => andb (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) k (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (@in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A))))))) *)
apply/fsetP=> a; apply/imfsetP/imfsetP=> [[/= x xA ->]|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by exists (f x); rewrite // in_imfset.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> [/= x /imfsetP [/= y yA ->] ->]; exists y.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma subset_imfset f (p q : finmempred _) : {subset p <= q} ->
  imfset key f p `<=` imfset key f q.
Proof.
(* Goal: forall (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f)))) (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f')))) (_ : @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@setf K V f k v) (@setf K V f' k v)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f f' *)
move=> subPQ; apply/fsubsetP=> x /imfsetP [y /= yA ->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite in_imfset //= [in_mem _ _]subPQ.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_imfset (f f' : K -> V) (p q : finmempred _):
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f =1 f' -> (forall x, in_mem x p = in_mem x q) ->
  imfset key f p = imfset key f' q.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> eq_f eqP; apply/fsetP => x; apply/imfsetP/imfsetP => /= [] [k Pk ->];
by exists k => //=; rewrite ?eq_f ?eqP in Pk *.
Qed.

End ImfsetTh.

Section PowerSetTheory.
Variable (K : choiceType).

Fact fpowerset_key : unit. Proof. exact: tt. Qed.
Definition fpowerset (A : {fset K}) : {fset {fset K}} :=
  [fset[fpowerset_key] [fsetval y in Y : {set A}] | Y in powerset [set: A]].

Lemma fpowersetE A B : (B \in fpowerset A) = (B `<=` A).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/imfsetP/fsubsetP => /= [[Z _ -> y /in_fset_valP [] //]|/fsubsetP subYX].
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
exists (fsub _ B); last by rewrite fsubK.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite powersetE /= -fsubT subset_fsub ?fsubset_refl.
Qed.

Lemma fpowersetCE (X A B : {fset K}) :
 (A \in fpowerset (X `\` B)) = (A `<=` X) && [disjoint A & B]%fset.
Proof. by rewrite fpowersetE fsubsetD. Qed.

Lemma fpowersetS A B : (fpowerset A `<=` fpowerset B) = (A `<=` B).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/fsubsetP/fsubsetP => [sub_pA_pB a|subAB X].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have := sub_pA_pB [fset a]; rewrite !fpowersetE !fsub1set.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !fpowersetE => /fsubsetP XA; apply/fsubsetP => x /XA /subAB.
Qed.

Lemma fpowerset0 : fpowerset fset0 = [fset fset0].
Proof. by apply/fsetP=> X; rewrite inE fpowersetE fsubset0. Qed.

Lemma fpowerset1 (x : K) : fpowerset [fset x] = [fset fset0; [fset x]].
Proof. by apply/fsetP => X; rewrite !inE fpowersetE fsubset1 orbC. Qed.

Lemma fpowersetI A B : fpowerset (A `&` B) = fpowerset A `&` fpowerset B.
Proof. by apply/fsetP=> X; rewrite inE !fpowersetE fsubsetI. Qed.

Lemma card_fpowerset (A : {fset K}) : #|` fpowerset A| = 2 ^ #|` A|.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite !card_imfset; first by rewrite -cardE card_powerset cardsE cardfE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> X Y /fsetP eqXY; apply/setP => x; have := eqXY (val x); rewrite !inE.
Qed.

End PowerSetTheory.

Section BigFSet.
Variable (R : Type) (idx : R) (op : Monoid.law idx).
Variable (I : choiceType).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_seq_fsetE (X : {fset I}) (P : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- X | P i) F i = \big[op/idx]_(x : X | P (val x)) F (val x).
Proof. by rewrite enum_fsetE big_map enumT. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big1_fset (X : {fset I}) (P : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall i, i \in X -> P i -> F i = idx) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- X | P i) F i = idx.
Proof. by move=> Fid; rewrite big_seq_fsetE big1//= => -[]. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fset0 (P : pred fset0) (F : @fset0 I -> R) :
  \big[op/idx]_(i : fset0 | P i) F i = idx.
Proof. by rewrite /index_enum -enumT /= enum_fset0 big_nil. Qed.

Lemma big_seq_fset0 (F : I -> R): \big[op/idx]_(i <- fset0) F i = idx.
Proof. by rewrite big_seq_fsetE big_fset0. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fset1 (a : I) (F : [fset a] -> R) :
  \big[op/idx]_(i : [fset a]) F i = F [` fset11 a].
Proof. by rewrite /index_enum -enumT enum_fset1 big_seq1. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_seq_fset1 (a : I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- [fset a]) F i = F a.
Proof. by rewrite big_seq_fsetE big_fset1. Qed.

End BigFSet.

Notation eq_big_imfset := (eq_big_perm _ (enum_imfset _ _)).

Section BigComFSet.
Variable (R : Type) (idx : R) (op : Monoid.com_law idx).
Variable (I J : choiceType).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fset (X : finmempred _) (P : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- [fset i in X | P i]) F i = \big[op/idx]_(i <- enum_finmem X | P i) F i.
Proof. by rewrite !eq_big_imfset//= !big_map !big_filter_cond big_andbC. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fset_condE (X : {fset I}) (P : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- X | P i) F i = \big[op/idx]_(i <- [fset i in X | P i]) F i.
Proof. by rewrite big_fset. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_fbig_cond (A B : {fset I}) (P Q : pred I) (F G : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  [fset x in A | P x] =i [fset x in B | Q x] ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall x, x \in A -> P x -> F x = G x) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- A | P i) F i = \big[op/idx]_(i <- B | Q i) G i.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> /fsetP eqABPQ FG; rewrite big_fset_condE [in RHS]big_fset_condE -eqABPQ.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite big_seq_cond [in RHS]big_seq_cond; apply: eq_bigr => i.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite in_fset !inE => /andP[/andP[??] _]; apply: FG.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_fbig (A B : {fset I}) (F G : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  A =i B -> (forall x, x \in A -> F x = G x) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- A) F i = \big[op/idx]_(i <- B) G i.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> AB FG; apply: eq_fbig_cond => x; rewrite ?inE/= -?AB// => /FG.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_fbigl_cond (A B : {fset I}) (P Q : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  [fset x in A | P x] =i [fset x in B | Q x] ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- A | P i) F i = \big[op/idx]_(i <- B | Q i) F i.
Proof. by move=> AB; apply: eq_fbig_cond. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_fbigl (A B : {fset I}) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  A =i B -> \big[op/idx]_(i <- A) F i = \big[op/idx]_(i <- B) F i.
Proof. by move=> AB; apply: eq_fbig. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_fbigr (A : {fset I}) (P : pred I) (F G : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall x, x \in A -> P x -> F x = G x) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- A | P i) F i = \big[op/idx]_(i <- A | P i) G i.
Proof. by apply: eq_fbig_cond. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fsetID  (B : pred I) (A : {fset I}) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- A) F i =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   op (\big[op/idx]_(i <- [fset x in A | B x]) F i)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (\big[op/idx]_(i <- [fset x in A | ~~ B x]) F i).
Proof. by rewrite !big_fset; apply: bigID. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fsetIDcond (B : pred I) (A : {fset I}) (P : pred I) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- A | P i) F i =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   op (\big[op/idx]_(i <- [fset x in A | B x] | P i) F i)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (\big[op/idx]_(i <- [fset x in A | ~~ B x] | P i) F i).
Proof. by rewrite big_mkcond (big_fsetID B) // -!big_mkcond. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fsetD1 (a : I) (A : {fset I}) (F : I -> R) : a \in A ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(i <- A) F i = op (F a) (\big[op/idx]_(i <- A `\ a) F i).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> aA; rewrite (big_fsetID (mem [fset a])); congr (op _ _); last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply: eq_fbigl=> i; rewrite !inE/= andbC.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite (_ : [fset _ | _ in _ & _] = [fset a]) ?big_seq_fset1//=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsetP=> i; rewrite !inE /= andbC; case: eqP => //->.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_setU1 (a : I) (A : {fset I}) (F : I -> R) : a \notin A ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- (a |` A)) F i = op (F a) (\big[op/idx]_(i <- A) F i).
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> aNA; rewrite eq_big_imfset//= big_map undup_id ?big_cat ?big_seq_fset1//.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@setf K (Choice.sort V) f k v)) (@fsetU V (@fset1 V v) (codomf (@restrictf K (Choice.sort V) f (@fsetD K (@domf K (Choice.sort V) f) (@fset1 K k))))) *)
rewrite cat_uniq ?fset_uniq andbT//=; apply/hasPn=> /= x xA; rewrite !inE/=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: contraNneq aNA => <-.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_fset_incl (A : {fset I}) B F : A `<=` B ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall x, x \in B -> x \notin A -> F x = idx) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  \big[op/idx]_(x <- A) F x = \big[op/idx]_(x <- B) F x.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> subAB Fid; rewrite [in RHS](big_fsetID (mem A)) /=.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite [X in op _ X]big1_fset ?Monoid.mulm1; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> i; rewrite !inE /= => /andP[iB iNA _]; apply: Fid.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: eq_fbigl => i; rewrite !inE /= -(@in_fsetI _ B A) (fsetIidPr _).
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma big_imfset key (h : I -> J) (A : finmempred _)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   (G : J -> R) : {in A &, injective h} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(j <- imfset key h A) G j =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- enum_finmem A) G (h i).
Proof. by move=> h_inj; rewrite eq_big_imfset// big_map. Qed.

End BigComFSet.
Arguments big_fsetD1 {R idx op I} a [A F].


Notation eq_big_imfset2 := (eq_big_perm _ (enum_imfset2 _ _)).

Section BigComImfset2.
Variables (R : Type) (idx : R) (op : Monoid.com_law idx)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
          (I : choiceType) (J : I -> choiceType) (K : choiceType).

Lemma big_imfset2 key (A : finmempred I) (B : forall i, finmempred (J i))
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (h : forall i : I, J i -> K) (F : K -> R) :
   {in  [pred t : sigT J | A (tag t) & B _ (tagged t)] &,
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
        injective (fun t => h (tag t) (tagged t))} ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(k <- imfset2 key h A B) F k =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- enum_finmem A)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
    \big[op/idx]_(j <- enum_finmem (B i)) F (h i j).
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> h_inj; rewrite eq_big_imfset2 //.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite (allsigs_comp (fun _ j => Tagged _ j) (fun t => h (tag t) (tagged t))).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite big_map big_allsigs /=.
Qed.
End BigComImfset2.

Section BigFsetDep.
Variables (R : Type) (idx : R) (op : Monoid.com_law idx)
          (I : choiceType) (J : choiceType) (K : choiceType).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma pair_big_dep_cond (A : {fset I}) (B : I -> {fset J})
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (P : pred I) (Q : I -> pred J) (F : I -> J -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- A | P i) \big[op/idx]_(j <- B i | Q i j) F i j =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(p <- [fset ((i, j) : I * J) | i in [fset i in A | P i],
                             j in [fset j in B i | Q i j]]) F p.1 p.2.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite big_imfset2 //=; last by move=> [??] [??] _ _ /= [-> ->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite big_fset /=; apply: eq_bigr => i _; rewrite big_fset.
Qed.
End BigFsetDep.

Section BigComImfset.
Variables (R : Type) (idx : R) (op : Monoid.com_law idx)
          (I : choiceType) (J : choiceType) (K : choiceType).


(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma partition_big_imfset (h : I -> J) (A : {fset I}) (F : I -> R) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(i <- A) F i =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \big[op/idx]_(j <- [fset h x | x in A]) \big[op/idx]_(i <- A | h i == j) F i.
Proof.
transitivity (\big[op/idx]_(i <- [fset (h i, i) | i in A]) F i.2).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
   by rewrite eq_big_imfset ?big_map// => i j ? ? [].
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
transitivity (\big[op/idx]_(i <- [fset ij | ij in
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  [seq (i1, i2) | i1 <- [fset h x | x in A], i2 <- A]]) if h i.2 == i.1 then F i.2 else idx).
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  rewrite -big_mkcond; apply: eq_fbigl_cond;
    move=> x; rewrite !inE/= andbT.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
  apply/imfsetP/idP => [[i /= iA -> /=]|].
    rewrite eqxx andbT; apply/allpairsP; exists (h i, i) => /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
    by rewrite in_imfset.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> /andP[/allpairsP[[/= j i] [/imfsetP[/=a aA ->] iA ->/= /eqP<-]]]; exists i.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite eq_big_imfset //= big_map undup_id.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite big_allpairs; apply: eq_bigr => i /= _; rewrite -big_mkcond.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite allpairs_uniq => //= -[j0 i0] [j1 i1] /=.
Qed.

End BigComImfset.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Notation "\bigcup_ ( i <- r | P ) F" :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (\big[@fsetU _/fset0]_(i <- r | P%fset) F%fset) : fset_scope.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Notation "\bigcup_ ( i <- r ) F" :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (\big[@fsetU _/fset0]_(i <- r) F%fset) : fset_scope.

Notation "\bigcup_ ( i | P ) F" :=
  (\big[@fsetU _/fset0]_(i | P) F%fset) : fset_scope.

Notation "\bigcup_ ( i 'in' A | P ) F" :=
  (\big[@fsetU _/fset0]_(i in A | P) F%fset) : fset_scope.

Notation "\bigcup_ ( i 'in' A ) F" :=
  (\big[@fsetU _/fset0]_(i in A) F%fset) : fset_scope.

Section FSetMonoids.

Import Monoid.
Variable (T : choiceType).

Canonical fsetU_monoid := Law (@fsetUA T) (@fset0U T) (@fsetU0 T).
Canonical fsetU_comoid := ComLaw (@fsetUC T).

End FSetMonoids.
Section BigFOpsSeq.

Variables (T : choiceType) (I : eqType) (r : seq I).
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Implicit Types (P : pred I) (F :  I -> {fset T}).

Lemma bigfcup_undup P F :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   \bigcup_(i <- undup r | P i) F i = \bigcup_(i <- r | P i) F i.
Proof. by rewrite big_undup => //= A; rewrite fsetUid. Qed.

Lemma bigfcup_sup j P F : j \in r -> P j -> F j `<=` \bigcup_(i <- r | P i) F i.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> jr Pj; rewrite -bigfcup_undup big_mkcond.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (bigD1_seq j) ?mem_undup ?undup_uniq ?Pj //= fsubsetUl.
Qed.

Lemma bigfcupP x F P :
  reflect (exists2 i : I, (i \in r) && P i & x \in F i)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
          (x \in \bigcup_(i <- r | P i) F i).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP idP) => [|[i /andP[ri Pi]]]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply: fsubsetP x; rewrite bigfcup_sup.
rewrite big_seq_cond; elim/big_rec: _ => [|i _ /andP[ri Pi] _ /fsetUP[|//]].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite in_fset0.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by exists i; rewrite ?ri.
Qed.

Lemma bigfcupsP (U : {fset T}) P F :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  reflect (forall i : I, i \in r -> P i -> F i `<=` U)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
          (\bigcup_(i <- r | P i) F i `<=` U).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply: (iffP idP) => [sFU i ri Pi| sFU].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply: fsubset_trans sFU; apply: bigfcup_sup.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsubsetP=> x /bigfcupP[i /andP[ri Pi]]; apply/fsubsetP/sFU.
Qed.

End BigFOpsSeq.

(* ** Induction Principles *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma finSet_rect (T : choiceType) (P : {fset T} -> Type) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  P fset0 -> (forall X, (forall Y, Y `<` X -> P Y) -> P X) -> forall X, P X.
Proof.
(* Goal: forall (_ : P (@fset0 T)) (_ : forall (X : @finset_of T (Phant (Choice.sort T))) (_ : forall (Y : @finset_of T (Phant (Choice.sort T))) (_ : is_true (@fproper T Y X)), P Y), P X) (X : @finset_of T (Phant (Choice.sort T))), P X *)
move=> P0 Psub X; move: (leqnn #|` X|); move: (X in Y in _ <= Y) => Y.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
elim: #|` _| X => [|n IHn] {Y} X; first by rewrite leqn0 cardfs_eq0 => /eqP->.
(* Goal: forall _ : is_true (leq (@size (Choice.sort T) (@enum_fset T X)) (S n)), P X *)
move=> Xleq; apply: Psub => Y XsubY; apply: IHn.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite -ltnS (leq_trans _ Xleq) // fproper_ltn_card.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fset_bounded_coind (T : choiceType) (P : {fset T} -> Type) (U : {fset T}):
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall X, (forall Y, Y `<=` U -> X `<` Y -> P Y) -> P X) ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   forall X, X `<=` U -> P X.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> Psuper X XsubU; rewrite -[X](fsetDK XsubU)//.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
have {XsubU}: (U `\` X) `<=` U by rewrite fsubsetDl.
(* Goal: forall _ : is_true (@fsubset T (@fsetD T U X) U), P (@fsetD T U (@fsetD T U X)) *)
elim: (_ `\` X) => {X} [|X IHX] XsubU.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  rewrite fsetD0; apply: Psuper => Y /fsub_proper_trans UY/UY.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite fproperEneq eqxx.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply: Psuper => Y /fsetDK<-; rewrite fproperD2l ?fsubsetDl //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /IHX; apply; rewrite fsubsetDl.
Qed.

(** ** Fixpoints *)

Lemma iter_fix T (f : T -> T) x n : f x = x -> iter n f x = x.
Proof. by move=> fixf; elim: n => //= n ->. Qed.

Section SetFixpoint.
(** Least Fixpoints *)
Section Least.
Variables (T : finType) (F : {set T} -> {set T}).
Hypothesis (F_mono : {homo F : X Y / X \subset Y}).

Let n := #|T|.
Notation iterF := (fun i => iter i F set0).

Lemma set_iterF_sub i : iterF i \subset iterF i.+1.
Proof. by elim: i => [| i IHi]; rewrite /= ?sub0set ?F_mono. Qed.

Lemma set_iterF_mono : {homo iterF : i j / i <= j >-> i \subset j}.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: homo_leq => //[???|]; [apply: subset_trans|apply: set_iterF_sub].
Qed.

Definition set_fix := iterF n.

Lemma set_fixK : F set_fix = set_fix.
Proof.
suff /'exists_eqP[x /= e]: [exists k : 'I_n.+1, iterF k == iterF k.+1].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite /set_fix -(subnK (leq_ord x)) iter_add iter_fix.
(* Goal: is_true (negb (@eq_op (fset_eqType K) (@fsetD K C B) (@fsetD K C A))) *)
apply: contraT; rewrite negb_exists => /forallP /(_ (Ordinal _)) /= neq_iter.
suff iter_big k : k <= n.+1 -> k <= #|iter k F set0|.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have := iter_big _ (leqnn _); rewrite ltnNge max_card.
elim: k => [|k IHk] k_lt //=; apply: (leq_ltn_trans (IHk (ltnW k_lt))).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite proper_card// properEneq// set_iterF_sub neq_iter.
Qed.
Hint Resolve set_fixK.

Lemma set_fixKn k : iter k F set_fix = set_fix.
Proof. by rewrite iter_fix. Qed.

Lemma iter_sub_fix k : iterF k \subset set_fix.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [/set_iterF_mono//|/ltnW/subnK<-] := leqP k n;
by rewrite iter_add set_fixKn.
Qed.

Lemma fix_order_proof x : x \in set_fix -> exists n, x \in iterF n.
Proof. by move=> x_fix; exists n. Qed.

Definition fix_order (x : T) :=
 if (x \in set_fix) =P true isn't ReflectT x_fix then 0
 else (ex_minn (fix_order_proof x_fix)).

Lemma fix_order_le_max (x : T) : fix_order x <= n.
Proof.
(* Goal: @eq bool (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) (@fsetD K (@domf K (Choice.sort V) f) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (negb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) *)
rewrite /fix_order; case: eqP => //= x_in.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: ex_minnP => //= ??; apply.
Qed.

Lemma in_iter_fix_orderE (x : T) :
  (x \in iterF (fix_order x)) = (x \in set_fix).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite /fix_order; case: eqP; last by move=>/negP/negPf->; rewrite inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> x_in; case: ex_minnP => m ->; rewrite x_in.
Qed.

Lemma fix_order_gt0 (x : T) : (fix_order x > 0) = (x \in set_fix).
Proof.
(* Goal: @eq bool (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) (@fsetD K (@domf K (Choice.sort V) f) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (negb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) *)
rewrite /fix_order; case: eqP => [x_in|/negP/negPf->//].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite x_in; case: ex_minnP => -[|m]; rewrite ?inE//= => _; apply.
Qed.

Lemma fix_order_eq0 (x : T) : (fix_order x == 0) = (x \notin set_fix).
Proof. by rewrite -fix_order_gt0 -ltnNge ltnS leqn0. Qed.

Lemma in_iter_fixE (x : T) k : (x \in iterF k) = (0 < fix_order x <= k).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite /fix_order; case: eqP => //= [x_in|/negP xNin]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply: contraNF xNin; apply/subsetP/iter_sub_fix.
(* Goal: @eq bool (@in_mem (Finite.sort T) x (@mem (Finite.sort T) (predPredType (Finite.sort T)) (@SetDef.pred_of_set T (@iter (@set_of T (Phant (Finite.sort T))) k F (@set0 T))))) (andb (leq (S O) (@ex_minn (fun n : nat => @in_mem (Finite.sort T) x (@mem (Finite.sort T) (predPredType (Finite.sort T)) (@SetDef.pred_of_set T (@iter (@set_of T (Phant (Finite.sort T))) n F (@set0 T))))) (@fix_order_proof x x_in))) (leq (@ex_minn (fun n : nat => @in_mem (Finite.sort T) x (@mem (Finite.sort T) (predPredType (Finite.sort T)) (@SetDef.pred_of_set T (@iter (@set_of T (Phant (Finite.sort T))) n F (@set0 T))))) (@fix_order_proof x x_in)) k)) *)
case: ex_minnP => -[|m]; rewrite ?inE// => xm mP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/idP/idP=> [/mP//|lt_mk]; apply: subsetP xm; apply: set_iterF_mono.
Qed.

Lemma in_iter (x : T) k : x \in set_fix -> fix_order x <= k -> x \in iterF k.
Proof. by move=> x_in xk; rewrite in_iter_fixE fix_order_gt0 x_in xk. Qed.

Lemma notin_iter (x : T) k : k < fix_order x -> x \notin iterF k.
Proof. by move=> k_le; rewrite in_iter_fixE negb_and orbC -ltnNge k_le. Qed.

Lemma fix_order_small x k : x \in iterF k -> fix_order x <= k.
Proof. by rewrite in_iter_fixE => /andP[]. Qed.

Lemma fix_order_big x k : x \in set_fix -> x \notin iterF k -> fix_order x > k.
Proof. by move=> x_in; rewrite in_iter_fixE fix_order_gt0 x_in /= -ltnNge. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma le_fix_order (x y : T) : y \in iterF (fix_order x) ->
  fix_order y <= fix_order x.
Proof. exact: fix_order_small. Qed.

End Least.

Section Greatest.
Variables (T : finType) (F : {set T} -> {set T}).
Hypothesis (F_mono : {homo F : X Y / X \subset Y}).

Notation n := #|T|.
Definition funsetC X := ~: (F (~: X)).
Notation G := funsetC.
Lemma funsetC_mono : {homo G : X Y / X \subset Y}.
Proof. by move=> *; rewrite subCset setCK F_mono// subCset setCK. Qed.
Hint Resolve funsetC_mono.

Definition set_cofix := ~: set_fix G.

Lemma set_cofixK : F set_cofix = set_cofix.
Proof. by rewrite /set_cofix -[in RHS]set_fixK ?setCK. Qed.

End Greatest.

End SetFixpoint.

Section Fixpoints.
Variables (T : choiceType) (U : {fset T}).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition sub_fun (F : {fset T} -> {fset T}) (X : {set U}) : {set U} :=
  fsub U (F [fsetval x in X]).

Lemma fset_fsub X : X `<=` U -> [fsetval x in fsub U X] = X.
Proof.
(* Goal: forall _ : is_true (leq (@size (Choice.sort T) (@enum_fset T X)) (S n)), P X *)
move=> XU; apply/fsetP => x; apply/in_fset_valP/idP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> [xU/=]; rewrite in_fsub.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> xX; exists (fsubsetP XU x xX); rewrite /= in_fsub.
Qed.

Variable (F : {fset T} -> {fset T}).
Hypothesis (F_mono : {homo F : X Y / X `<=` Y}) (F_bound : {homo F : X / X `<=` U}).

Notation Fsub := (sub_fun F).
Notation iterF := (fun i => iter i F fset0).

Lemma Fsub_mono : {homo Fsub : X Y / X \subset Y}.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
move=> X Y subXY; apply: subset_fsub; last by apply/F_bound/fset_sub_val.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/F_mono/subset_imfset/subsetP.
Qed.
Hint Resolve Fsub_mono.

Definition fset_fix := [fsetval x in set_fix Fsub].

Lemma fset_iterFE i : iterF i = [fsetval x in iter i Fsub set0].
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
elim: i => [|i /= -> /=]; last by rewrite fset_fsub // F_bound//= fset_sub_val.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsetP=> x; rewrite in_fset_val /=; case: insub=> //= ?; rewrite !inE.
Qed.

Lemma fset_iterF_sub i : iterF i `<=` U.
Proof. by rewrite /= fset_iterFE fset_sub_val. Qed.

Lemma fset_fixK : F fset_fix = fset_fix.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite /fset_fix -[in RHS]set_fixK// fset_fsub// F_bound//= fset_sub_val.
Qed.
Hint Resolve fset_fixK.

Lemma fset_fixKn k : iter k F fset_fix = fset_fix.
Proof. by rewrite iter_fix. Qed.

Lemma iter_sub_ffix k : iterF k `<=` fset_fix.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite /fset_fix !fset_iterFE; apply/subset_imfset/subsetP/iter_sub_fix.
Qed.

Definition ffix_order (x : T) :=
 if x \in U =P true is ReflectT xU then fix_order Fsub [` xU] else 0.

Lemma ffix_order_le_max (x : T) : ffix_order x <= #|` U|.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite /ffix_order; case: eqP => //= x_in; rewrite cardfE fix_order_le_max.
Qed.

Lemma in_iter_ffix_orderE (x : T) :
  (x \in iterF (ffix_order x)) = (x \in fset_fix).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite /ffix_order; case: eqP => [xU|/negP xNU]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite !inE /fset_fix in_fset_valF.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fset_iterFE !in_fset_valT /= in_iter_fix_orderE.
Qed.

Lemma ffix_order_gt0 (x : T) : (ffix_order x > 0) = (x \in fset_fix).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite /ffix_order; case: eqP => [xU|/negP xNU]; last by rewrite in_fset_valF.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fix_order_gt0 in_fset_valT.
Qed.

Lemma ffix_order_eq0 (x : T) : (ffix_order x == 0) = (x \notin fset_fix).
Proof. by rewrite -ffix_order_gt0 -ltnNge ltnS leqn0. Qed.

Lemma in_iter_ffixE (x : T) k : (x \in iterF k) = (0 < ffix_order x <= k).
Proof.
rewrite /ffix_order fset_iterFE; case: eqP => [xU|/negP xNU];
by [rewrite in_fset_valF|rewrite in_fset_valT /= in_iter_fixE].
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma in_iter_ffix (x : T) k : x \in fset_fix -> ffix_order x <= k ->
  x \in iterF k.
Proof. by move=> x_in xk; rewrite in_iter_ffixE ffix_order_gt0 x_in xk. Qed.

Lemma notin_iter_ffix (x : T) k : k < ffix_order x -> x \notin iterF k.
Proof. by move=> k_le; rewrite in_iter_ffixE negb_and orbC -ltnNge k_le. Qed.

Lemma ffix_order_small x k : x \in iterF k -> ffix_order x <= k.
Proof. by rewrite in_iter_ffixE => /andP[]. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma ffix_order_big x k : x \in fset_fix -> x \notin iterF k ->
   ffix_order x > k.
Proof. by move=> x_in; rewrite in_iter_ffixE ffix_order_gt0 x_in -ltnNge. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma le_ffix_order (x y : T) : y \in iterF (ffix_order x) ->
  ffix_order y <= ffix_order x.
Proof. exact: ffix_order_small. Qed.

End Fixpoints.


(* apply/apply/fsetP=> x /=. *)
(* apply//in_fset_valP. *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* rewrite -IHi /=. *)
(* rewrite in_imfset. *)


Section DefMap.
Variables (K : choiceType) (V : Type).

Record finMap : Type := FinMap {
  domf : {fset K};
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  ffun_of_fmap :> {ffun domf -> V}
}.

Definition finmap_of (_ : phant (K -> V)) := finMap.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Let T_ (domf : {fset K}) :=  {ffun domf -> V}.
Local Notation finMap' := {domf : _ & T_ domf}.

End DefMap.

Notation "{fmap T }" := (@finmap_of _ _ (Phant T)) : type_scope.

Definition pred_of_finmap (K : choiceType) (V : Type)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (f : {fmap K -> V}) : pred K := mem (domf f).
Canonical finMapPredType (K : choiceType) (V : Type) :=
  Eval hnf in mkPredType (@pred_of_finmap K V).

Delimit Scope fmap_scope with fmap.
Local Open Scope fmap_scope.
Notation "f .[ kf ]" := (f [` kf]) : fmap_scope.
Arguments ffun_of_fmap : simpl never.

Notation "[ 'fmap' x : aT => F ]" := (FinMap [ffun x : aT => F])
  (at level 0, x ident, only parsing) : fun_scope.

Notation "[ 'fmap' : aT => F ]" := (FinMap [ffun : aT => F])
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fmap' x => F ]" := [fmap x : _ => F]
  (at level 0, x ident, format "[ 'fmap'  x  =>  F ]") : fun_scope.

Notation "[ 'fmap' => F ]" := [fmap: _ => F]
  (at level 0, format "[ 'fmap' =>  F ]") : fun_scope.

Canonical finmap_of_finfun (K : choiceType) V (A : {fset K}) (f : {ffun A -> V}) := FinMap f.
Arguments finmap_of_finfun /.
Arguments ffun_of_fmap : simpl nomatch.

Section OpsMap.

Variables (K : choiceType).

Definition fmap0 V : {fmap K -> V} := FinMap (ffun0 _ (cardfT0 K)).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition fnd V (A : {fset K}) (f : {ffun A -> V}) (k : K) :=
  omap f (insub k).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Inductive fnd_spec V (A : {fset K}) (f : {ffun A -> V}) k :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  bool -> option A -> option V -> Type :=
| FndIn  (kf : k \in A) : fnd_spec f k true (some [` kf]) (some (f.[kf]))
| FndOut (kNf : k \notin A) : fnd_spec f k false None None.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition setf V (f : {fmap K -> V}) (k0 : K) (v0 : V) : {fmap K -> V} :=
  [fmap k : k0 |` domf f => if val k == k0 then v0
                            else odflt v0 (fnd f (val k))].

End OpsMap.

Prenex Implicits fnd setf.
Arguments fmap0 {K V}.
Arguments setf : simpl never.
Arguments fnd : simpl never.

Notation "[ 'fmap' 'of' T ]" := (fmap0 : {fmap T}) (only parsing) : fmap_scope.
Notation "[fmap]" := fmap0 : fmap_scope.
Notation "x .[ k <- v ]" := (setf x k v) : fmap_scope.
Notation "f .[? k ]" := (fnd f k) : fmap_scope.

Section FinMapCanonicals.
Variable K : choiceType.

Let finMap_on (V : Type) (d : {fset K}) := {ffun d -> V}.
Local Notation finMap_ V := {d : _ & finMap_on V d}.

Definition finMap_encode V (f : {fmap K -> V}) := Tagged (finMap_on V) (ffun_of_fmap f).
Definition finMap_decode V (f : finMap_ V) := FinMap (tagged f).
Lemma finMap_codeK V : cancel (@finMap_encode V) (@finMap_decode V).
Proof. by case. Qed.

Section FinMapEqType.
Variable V : eqType.

Definition finMap_eqMixin := CanEqMixin (@finMap_codeK V).
Canonical finMap_eqType := EqType {fmap K -> V} finMap_eqMixin.

End FinMapEqType.

Section FinMapChoiceType.
Variable V : choiceType.

Definition finMap_choiceMixin := CanChoiceMixin (@finMap_codeK V).
Canonical finMap_choiceType := ChoiceType {fmap K -> V} finMap_choiceMixin.

End FinMapChoiceType.

End FinMapCanonicals.

Section FinMapTheory.
Variables (K : choiceType).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fndP V (f : {fmap K -> V}) k :
  fnd_spec f k (k \in domf f) (insub k) (f.[? k]).
Proof.
(* Goal: @eq bool (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) (@fsetD K (@domf K (Choice.sort V) f) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (negb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) *)
rewrite /fnd; case: insubP=> [[k' k'f] _ {k} <- /=|kNf].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite k'f; constructor.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (negPf kNf); constructor.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fndSome V (f : {fmap K -> V}) (k : K) :
  f.[? k] = (k \in f) :> bool.
Proof. by case: fndP. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma not_fnd V (f : {fmap K -> V}) (k : K) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  k \notin f -> f.[? k] = None.
Proof. by case: fndP. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma getfE V (f : {fmap K -> V}) (k : domf f)
      (kf : val k \in domf f) : f.[kf] = f k :> V.
Proof. by congr (_ _); apply: val_inj. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma eq_getf V (f : {fmap K -> V}) k (kf kf' : k \in domf f) :
  f.[kf] = f.[kf'] :> V.
Proof. by rewrite (@getfE _ _ [` kf']). Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma Some_fnd V (f : {fmap K -> V}) (k : domf f) :
  Some (f k) = f.[? val k].
Proof. by case: fndP (valP k) => // ? _; rewrite getfE. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma in_fnd V (f : {fmap K -> V}) (k : K)
      (kf : k \in domf f) : f.[? k] = Some f.[kf].
Proof. by rewrite Some_fnd. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fnd_if V (cond : bool) (f g : {fmap K -> V}) (k : K) :
  ((if cond then f else g) : finMap _ _).[? k] =
  if cond then f.[? k] else g.[? k].
Proof. by case: cond. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma getfP V (f g : {fmap K -> V}) : domf f = domf g ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (forall k (kMf : k \in f) (kMg : k \in g), f.[kMf] = g.[kMg]) -> f = g.
Proof.
(* Goal: forall (_ : @eq (@finset_of K (Phant (Choice.sort K))) (@domf K V f) (@domf K V g)) (_ : forall (k : Choice.sort K) (kMf : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f))) (kMg : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) g))), @eq V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V f)) V (@ffun_of_fmap K V f) (@FSetSub K (@domf K V f) k kMf)) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K V g)) V (@ffun_of_fmap K V g) (@FSetSub K (@domf K V g) k kMg))), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move: f g => [kf f] [kg g] /= eq_kfg; case: _ / eq_kfg in g * => {kg}.
(* Goal: forall _ : forall (k : Choice.sort K) (kMf : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) (@FinMap K V kf f)))) (kMg : is_true (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) (@FinMap K V kf g)))), @eq V (@FunFinfun.fun_of_fin (@fset_sub_finType K kf) V f (@FSetSub K kf k kMf)) (@FunFinfun.fun_of_fin (@fset_sub_finType K kf) V g (@FSetSub K kf k kMg)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@FinMap K V kf f) (@FinMap K V kf g) *)
move=> eq_fg; congr FinMap; apply/ffunP => /= x.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by do [rewrite -!getfE; do ?exact: valP] => *.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fmapP V (f g : {fmap K -> V}) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (forall k, f.[? k] = g.[? k]) <-> f = g.
Proof.
(* Goal: iff (@eqfun (Equality.sort V) (Choice.sort K) (@fun_of_fsfun K V default f) (@fun_of_fsfun K V default f')) (@eq (@fsfun K V default) f f') *)
split=> [fnd_fg|-> //]; apply: getfP => [|k kMf kMg].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply/fsetP => x; rewrite -!fndSome fnd_fg.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: Some_inj; rewrite !Some_fnd.
Qed.

Lemma fnd_fmap0 V k : ([fmap] : {fmap K -> V}).[? k] = None.
Proof. by rewrite not_fnd // in_fset0. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma mem_setf V (f : {fmap K -> V}) (k0 : K) (v0 : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[k0 <- v0] =i predU1 k0 (mem (domf f)).
Proof. by move=> k; rewrite !inE. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma dom_setf V (f : {fmap K -> V}) (k0 : K) (v0 : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  domf (f.[k0 <- v0]) = k0 |` domf f.
Proof. by apply/fsetP=> k; rewrite mem_setf. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fnd_set_in V (f : {fmap K -> V}) k0 v0 (x : domf f.[k0 <- v0]) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  val x != k0 -> val x \in f.
Proof. by have := valP x; rewrite mem_setf inE; case: eqP. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma setfK V (f : {fmap K -> V}) k0 v0 (x : domf f.[k0 <- v0]):
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   f.[k0 <- v0] x = if eqVneq (val x) k0 is right xNk0
                    then f.[fnd_set_in xNk0] else v0.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: eqVneq => [|xNk0]; rewrite ?ffunE /=; first by move->; rewrite eqxx.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (negPf xNk0) in_fnd ?fnd_set_in //= => xf; apply: eq_getf.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fnd_set V (f : {fmap K -> V}) k0 v0 k :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   f.[k0 <- v0].[? k] = if k == k0 then Some v0 else f.[? k].
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: fndP => [ksf|]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite mem_setf inE negb_or => /andP [/negPf ->]; case: fndP.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite setfK; case: eqVneq => //= [->|kNk0]; first by rewrite eqxx.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite Some_fnd (negPf kNk0).
Qed.

Lemma fmap_nil V (f : {fmap K -> V}) : domf f = fset0 -> f = [fmap].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> kf0; apply: getfP => //= k ? kMg; have := kMg; rewrite !inE.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma getf_set V (f : {fmap K -> V}) (k : K) (v : V) (kf' : k \in _) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   f.[k <- v].[kf'] = v.
Proof. by apply: Some_inj; rewrite Some_fnd fnd_set eqxx. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma setf_get V (f : {fmap K -> V}) (k : domf f) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[val k <- f k] = f.
Proof. by apply/fmapP=> k'; rewrite fnd_set Some_fnd; case: eqP => [->|]. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma setfNK V (f : {fmap K -> V}) (k k' : K) (v : V)
      (k'f : k' \in _) (k'f' : k' \in _):
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   f.[k <- v].[k'f'] = if k' == k then v else f.[k'f].
Proof. by apply: Some_inj; rewrite Some_fnd !fnd_set in_fnd; case: ifP. Qed.

Lemma domf0 V : domf [fmap of K -> V] = fset0. Proof. by apply/fsetP => x. Qed.

End FinMapTheory.

Section ReduceOp.

Variable (K : choiceType) (V : Type).
Implicit Types (f : {fmap K -> option V}).

Lemma reducef_subproof f (x : [fsetval x : domf f | f x]) :
  f (fincl (fset_sub_val _ _) x).
Proof.
(* Goal: is_true (@isSome V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (option V) f)) (option V) (@ffun_of_fmap K (option V) f) (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f))) K (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f))) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (simplPredType (@fset_sub_type K (@domf K (option V) f))) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f))) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (predPredType (@fset_sub_type K (@domf K (option V) f) : predArgType)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subfinType K (@domf K (option V) f)))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (option V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (option V) f) : predArgType))))) (fun x : @fset_sub_type K (@domf K (option V) f) => @isSome V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (option V) f)) (option V) (@ffun_of_fmap K (option V) f) x)))) (Phantom (mem_pred (Equality.sort (@Equality.clone (@fset_sub_type K (@domf K (option V) f)) (@fset_sub_eqType K (@domf K (option V) f)) (@fset_sub_eqMixin K (@domf K (option V) f)) (fun x : Equality.sort (@fset_sub_eqType K (@domf K (option V) f)) => x) (fun x : phantom Equality.type (@Equality.Pack (@fset_sub_type K (@domf K (option V) f)) (@fset_sub_eqMixin K (@domf K (option V) f))) => x)))) (@pred_of_finmempred (@Equality.clone (@fset_sub_type K (@domf K (option V) f)) (@fset_sub_eqType K (@domf K (option V) f)) (@fset_sub_eqMixin K (@domf K (option V) f)) (fun x : Equality.sort (@fset_sub_eqType K (@domf K (option V) f)) => x) (fun x : phantom Equality.type (@Equality.Pack (@fset_sub_type K (@domf K (option V) f)) (@fset_sub_eqMixin K (@domf K (option V) f))) => x)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (simplPredType (@fset_sub_type K (@domf K (option V) f))) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f))) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (predPredType (@fset_sub_type K (@domf K (option V) f) : predArgType)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subfinType K (@domf K (option V) f)))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (option V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (option V) f) : predArgType))))) (fun x : @fset_sub_type K (@domf K (option V) f) => @isSome V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (option V) f)) (option V) (@ffun_of_fmap K (option V) f) x))))))) (@domf K (option V) f) (@fset_sub_val imfset_key K (@domf K (option V) f) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (simplPredType (@fset_sub_type K (@domf K (option V) f))) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f))) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (predPredType (@fset_sub_type K (@domf K (option V) f) : predArgType)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subType K (@domf K (option V) f)))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (option V) f))) (@fset_sub_subfinType K (@domf K (option V) f)))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (option V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (option V) f) : predArgType))))) (fun x : @fset_sub_type K (@domf K (option V) f) => @isSome V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (option V) f)) (option V) (@ffun_of_fmap K (option V) f) x))))) x))) *)
set y := (y in f y); suff : val y \in [fsetval x : domf f | f x].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite val_in_fset.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by suff -> : val y = val x by exact: valP.
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition reducef f : {fmap K -> V} :=
  [fmap x => oextract (@reducef_subproof f x)].

Lemma domf_reduce f : domf (reducef f) = [fsetval x : domf f | f x].
Proof. by []. Qed.

Lemma mem_reducef f k : k \in reducef f = ojoin f.[? k].
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite inE; case: fndP => [kf|] /=; first by rewrite in_fset_valT.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: contraNF; apply: (fsubsetP (fset_sub_val _ _)).
Qed.

Lemma fnd_reducef f k : (reducef f).[? k] = ojoin f.[? k].
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: fndP => /= [kf|]; last by rewrite mem_reducef; case: ojoin.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@setf K (Choice.sort V) f k v)) (@fsetU V (@fset1 V v) (codomf (@restrictf K (Choice.sort V) f (@fsetD K (@domf K (Choice.sort V) f) (@fset1 K k))))) *)
rewrite ffunE /= Some_oextract; apply: Some_inj; rewrite Some_fnd.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite Some_ojoin // ojoinT // -mem_reducef.
Qed.

Lemma get_reducef f k (krf : k \in reducef f) (kf : k \in f):
  Some (reducef f).[krf] = f.[kf].
Proof. by rewrite Some_fnd fnd_reducef in_fnd. Qed.

End ReduceOp.

Arguments reducef : simpl never.

Section RestrictionOps.
Variable (K : choiceType) (V : Type).
Implicit Types (f g : {fmap K -> V}).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition filterf f (P : pred K) : {fmap K -> V} :=
   [fmap x : [fset x in domf f | P x] => f (fincl (fset_sub _ _) x)].

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition restrictf f (A : {fset K}) : {fmap K -> V} :=
  filterf f (mem A).

Notation "x .[& A ]" := (restrictf x A) : fmap_scope.
Notation "x .[\ A ]" := (x.[& domf x `\` A]) : fmap_scope.
Notation "x .[~ k ]" := (x.[\ [fset k]]) : fmap_scope.

Lemma domf_filterf f (P : pred K) :
 domf (filterf f P) = [fset k in domf f | P k].
Proof. by []. Qed.

Lemma mem_filterf f (P : pred K) (k : K) :
  (k \in domf (filterf f P)) = (k \in f) && (P k) :> bool.
Proof. by rewrite !inE. Qed.

Lemma mem_restrictf f (A : {fset K}) (k : K) :
   k \in f.[& A] = (k \in A) && (k \in f) :> bool.
Proof. by rewrite mem_filterf andbC. Qed.

Lemma mem_remf f (A : {fset K}) (k : K) :
   k \in f.[\ A] = (k \notin A) && (k \in f) :> bool.
Proof. by rewrite mem_restrictf inE -andbA andbb. Qed.

Lemma mem_remf1 f (k' k : K) :
   k \in f.[~ k'] = (k != k') && (k \in f) :> bool.
Proof. by rewrite mem_remf inE. Qed.

Lemma domf_restrict f A : domf f.[& A] = A `&` domf f.
Proof. by apply/fsetP=> k'; rewrite mem_restrictf !inE. Qed.

Lemma domf_rem f A : domf f.[\ A] = domf f `\` A.
Proof. by rewrite domf_restrict fsetIDAC fsetIid. Qed.

Lemma mem_remfF f (k : K) : k \in f.[~ k] = false.
Proof. by rewrite mem_remf1 eqxx. Qed.

Lemma fnd_filterf f P k : (filterf f P).[? k] = if P k then f.[? k] else None.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: fndP => [kff|]; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite in_fset => /nandP [/not_fnd->|/negPf-> //]; rewrite if_same.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have := kff; rewrite in_fset => /andP [kf ->]; rewrite ffunE Some_fnd.
Qed.

Lemma get_filterf f P k (kff : k \in filterf f P) (kf : k \in f) :
  (filterf f P).[kff] = f.[kf].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply: Some_inj; rewrite !Some_fnd /= fnd_filterf.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move: kff; rewrite !(in_fset, inE) => /andP [? ->].
Qed.

Lemma fnd_restrict f A (k : K) :
   f.[& A].[? k] = if k \in A then f.[? k] else None.
Proof. by rewrite fnd_filterf. Qed.

Lemma fnd_rem f A (k : K) : f.[\ A].[? k] = if k \in A then None else f.[? k].
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite fnd_restrict inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: fndP => ?; rewrite ?(andbT, andbF) //=; case: (_ \in _).
Qed.

Lemma restrictf_comp f A B : f.[& A].[& B] = f.[& A `&` B].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fmapP=> k; rewrite !fnd_restrict !inE; do !case: (_ \in _).
Qed.

Lemma remf_comp f A B : f.[\ A].[\ B] = f.[\ A `|` B].
Proof. by apply/fmapP=> k; rewrite !fnd_rem inE; do !case: (_ \in _). Qed.

Lemma restrictfT f : f.[& domf f] = f.
Proof. by apply/fmapP=> k; rewrite fnd_restrict; case: fndP. Qed.

Lemma restrictf0 f : f.[& fset0] = [fmap].
Proof. by apply/fmapP => k; rewrite fnd_restrict !(inE, not_fnd). Qed.

Lemma remf0 f : f.[\ fset0] = f. Proof. by rewrite fsetD0 restrictfT. Qed.

Lemma fnd_rem1 f (k k' : K) :
  f.[~ k].[? k'] = if k' != k then f.[? k'] else None.
Proof. by rewrite fnd_rem inE; case: eqP. Qed.

Lemma getf_restrict f A (k : K) (kf : k \in f) (kfA : k \in f.[& A]) :
      f.[& A].[kfA] = f.[kf].
Proof. by rewrite get_filterf. Qed.

Lemma setf_restrict f A (k : K) (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[& A].[k <- v] = f.[k <- v].[& k |` A].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fmapP=> k'; rewrite !(fnd_set, fnd_restrict, inE); case: eqP.
Qed.

Lemma setf_rem f A (k : K) (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[\ A].[k <- v] = f.[k <- v].[\ (A `\ k)].
Proof. by rewrite setf_restrict fsetUDl. Qed.

Lemma setf_rem1 f (k : K) (v : V) : f.[~ k].[k <- v] = f.[k <- v].
Proof. by rewrite setf_rem fsetDv remf0. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma setfC f k1 k2 v1 v2 : f.[k1 <- v1].[k2 <- v2] =
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   if k2 == k1 then f.[k2 <- v2] else f.[k2 <- v2].[k1 <- v1].
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply/fmapP => k. rewrite fnd_if !fnd_set.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [[->|kNk2] [// <-|k2Nk1]] // := (altP (k =P k2), altP (k2 =P k1)).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite (negPf kNk2).
Qed.

Lemma restrictf_mkdom f A : f.[& A] = f.[& domf f `&` A].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP=> k; rewrite !fnd_restrict inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: fndP => ?; rewrite ?(andbT, andbF) //=; case: (_ \in _).
Qed.

Lemma restrictf_id f A : [disjoint domf f & A] -> f.[& A] = [fmap].
Proof. by move=> dAf; rewrite restrictf_mkdom (eqP dAf) restrictf0. Qed.

Lemma remf_id f A : [disjoint domf f & A] -> f.[\ A] = f.
Proof. by move=> /fsetDidPl ->; rewrite restrictfT. Qed.

Lemma remf1_id f k : k \notin f -> f.[~ k] = f.
Proof. by move=> kNf; rewrite remf_id //= fdisjointX1. Qed.

Lemma restrictf_set f A (k : K) (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[k <- v].[& A] = if k \in A then f.[& A].[k <- v] else f.[& A].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP => k' /=; rewrite !(fnd_if, fnd_set, fnd_restrict).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: eqP => [->|]; do !case: ifP.
Qed.

Lemma remf_set f A (k : K) (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[k <- v].[\ A] = if k \in A then f.[\ A] else f.[\ A].[k <- v].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP => k' /=; rewrite !(fnd_if, fnd_rem, fnd_set, inE).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: eqP => [->|]; do !case: (_ \in _).
Qed.

Lemma remf1_set f (k k' : K) (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[k' <- v].[~ k] = if k == k' then f.[~ k] else f.[~ k].[k' <- v].
Proof. by rewrite remf_set inE eq_sym. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma setf_inj f f' k v : k \notin f -> k \notin f' ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
                          f.[k <- v] = f'.[k <- v]-> f = f'.
Proof.
(* Goal: forall (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f)))) (_ : is_true (negb (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finMapPredType K V) f')))) (_ : @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@setf K V f k v) (@setf K V f' k v)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f f' *)
move=> kf kf' eq_fkv; apply/fmapP => k'.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have := congr1 (fun g => g.[? k']) eq_fkv.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !fnd_set; case: eqP => // ->; rewrite !not_fnd.
Qed.

End RestrictionOps.

Arguments filterf : simpl never.
Arguments restrictf : simpl never.
Notation "x .[& A ]" := (restrictf x A) : fmap_scope.
Notation "x .[\ A ]" := (x.[& domf x `\` A]) : fmap_scope.
Notation "x .[~ k ]" := (x.[\ [fset k]]) : fmap_scope.

Section Cat.
Variables (K : choiceType) (V : Type).
Implicit Types (f g : {fmap K -> V}).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition catf (f g : {fmap K -> V}) :=
  [fmap k : (domf f `\` domf g) `|` domf g=>
          match fsetULVR (valP k) with
            | inl kfDg => f.[fsubsetP (fsubsetDl _ _) _ kfDg]
            | inr kg => g.[kg]
          end].

Local Notation "f + g" := (catf f g) : fset_scope.

Lemma domf_cat f g : domf (f + g) = domf f `|` domf g.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fsetP=> x; rewrite !inE; case: (boolP (_ \in domf _)); rewrite ?orbT.
Qed.

Lemma mem_catf f g k : k \in domf (f + g) = (k \in f) || (k \in g).
Proof. by rewrite domf_cat inE. Qed.

Lemma fnd_cat f g k :
  (f + g).[? k] = if k \in domf g then g.[? k] else f.[? k].
Proof.
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default x) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) x)) (default x) *)
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default (@fsval K S u)) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) (@fsval K S u))) (h u) *)
case: fndP => //= [kfg|]; rewrite /catf /=.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  rewrite ffunE /=; case: fsetULVR => [kf|kg]; last by rewrite Some_fnd kg.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite -in_fnd; move: kf; rewrite inE => /andP[/negPf ->].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite mem_catf => /norP [kNf kNg]; rewrite !not_fnd // if_same.
Qed.

Lemma catfE f g : f + g = f.[\ domf g] + g.
Proof. by apply/fmapP=> k; rewrite !(fnd_cat, fnd_rem); case: ifP. Qed.

Lemma getf_catl f g k (kfg : k \in domf (f + g))
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      (kf : k \in domf f) : k \notin domf g -> (f + g).[kfg] = f.[kf].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> kNg; apply: Some_inj; rewrite Some_fnd fnd_cat (negPf kNg) in_fnd.
Qed.

Lemma getf_catr f g k (kfg : k \in domf (f + g))
      (kg : k \in domf g) : (f + g).[kfg] = g.[kg].
Proof. by apply: Some_inj; rewrite Some_fnd fnd_cat kg in_fnd. Qed.

Lemma catf0 f : f + [fmap] = f.
Proof. by apply/fmapP => k; rewrite fnd_cat in_fset0. Qed.

Lemma cat0f f : [fmap] + f = f.
Proof.
(* Goal: @eq (finMap K V) (catf (@fmap0 K V) f) f *)
apply/fmapP => k; rewrite fnd_cat; case: ifPn => //= kf.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !not_fnd ?inE.
Qed.

Lemma catf_setl f g k (v : V) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f.[k <- v] + g = if k \in g then f + g else (f + g).[k <- v].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP=> k'; rewrite !(fnd_if, fnd_cat, fnd_set).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [->|Nkk'] := altP eqP; do !case: (_ \in _).
Qed.

Lemma catf_setr f g k (v : V) : f + g.[k <- v] = (f + g).[k <- v].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP=> k'; rewrite !(fnd_cat, fnd_set, mem_setf, inE).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [->|Nkk'] := altP eqP; do !case: (_ \in _).
Qed.

Lemma restrictf_cat f g A : (f + g).[& A] = f.[& A] + g.[& A].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP => k'; rewrite !(fnd_cat, fnd_restrict) mem_restrictf.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: (_ \in _).
Qed.

Lemma restrictf_cat_domr f g : (f + g).[& domf g] = g.
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite catfE restrictf_cat restrictf_comp.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fsetIDAC fsetDIl fsetDv fsetI0 restrictf0 restrictfT cat0f.
Qed.

Lemma remf_cat f g A : (f + g).[\ A] = f.[\ A] + g.[\ A].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fmapP => k'; rewrite !(fnd_cat, fnd_rem) mem_remf; case: (_ \in _).
Qed.

Lemma catf_restrictl A f g : f.[& A] + g = (f + g).[& A `|` domf g].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP=> k; rewrite !(fnd_cat, fnd_restrict) !inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by do !case: (_ \in _).
Qed.

Lemma catf_reml A f g : f.[\ A] + g = (f + g).[\ A `\` domf g].
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fmapP=> k; rewrite !(fnd_cat, fnd_rem) inE; case: (_ \in _).
Qed.

Lemma catf_rem1l k f g :
  f.[~ k] + g = if k \in g then f + g else (f + g).[~ k].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP => k'; rewrite !(fnd_if, fnd_cat, fnd_rem1).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [->|?] := altP eqP; do !case: (_ \in _).
Qed.

Lemma setf_catr f g k (v : V) : (f + g).[k <- v] = f + g.[k <- v].
Proof. by rewrite catf_setr. Qed.

Lemma setf_catl f g k (v : V) : (f + g).[k <- v] = f.[k <- v] + g.[~ k].
Proof. by rewrite catf_setl mem_remf1 eqxx /= !setf_catr setf_rem1. Qed.

Lemma catfA f g h : f + (g + h) = f + g + h.
Proof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/fmapP => k; rewrite !fnd_cat !mem_catf; do !case: (_ \in _).
Qed.

Lemma catfC f g : f + g = g + f.[\ domf g].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fmapP=> k; rewrite !fnd_cat fnd_rem domf_rem inE.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [|kNg] //= := boolP (_ \in domf g); rewrite (not_fnd kNg); case: fndP.
Qed.

Lemma disjoint_catfC f g : [disjoint domf f & domf g] -> f + g = g + f.
Proof. by move=> dfg; rewrite catfC remf_id. Qed.

Lemma catfAC f g h : f + g + h = f + h + g.[\ domf h].
Proof. by rewrite -!catfA [X in _ + X]catfC. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma disjoint_catfAC f g h : [disjoint domf g & domf h]%fmap ->
     f + g + h = f + h + g.
Proof. by move=> dgh; rewrite catfAC remf_id. Qed.

Lemma catfCA f g h : f + (g + h) = g + (f.[\ domf g] + h).
Proof. by rewrite !catfA [X in X + _]catfC. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma disjoint_catfCA f g h : [disjoint domf f & domf g]%fmap ->
     f + (g + h) = g + (f + h).
Proof. by move=> dfg; rewrite catfCA remf_id. Qed.

Lemma catfIs f g h : f + h = g + h -> f.[\ domf h] = g.[\ domf h].
Proof.
(* Goal: forall _ : @eq (finMap K V) (catf f g) (catf f h), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@restrictf K V g (@domf K V h)) (@restrictf K V h (@domf K V g)) *)
move=> /fmapP eq_fg_fh; apply/fmapP => k; have := eq_fg_fh k.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite !fnd_cat !fnd_rem; case: ifP.
Qed.

Lemma disjoint_catfIs h f g :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  [disjoint domf f & domf h] -> [disjoint domf g & domf h] ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  f + h = g + h -> f = g.
Proof. by move=> dfg dgh /catfIs; rewrite !remf_id. Qed.

Lemma restrict_catfsI f g h : f + g = f + h -> g.[& domf h] = h.[& domf g].
Proof.
(* Goal: forall _ : @eq (finMap K V) (catf f g) (catf f h), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) (@restrictf K V g (@domf K V h)) (@restrictf K V h (@domf K V g)) *)
move=> /fmapP eq_fg_fh; apply/fmapP => k; have := eq_fg_fh k.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite !fnd_cat !fnd_restrict.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by do ![case: (boolP (_ \in domf _)) => ? //=] => _; rewrite not_fnd.
Qed.

Lemma disjoint_catfsI h f g :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  [disjoint domf f & domf h] -> [disjoint domf g & domf h] ->
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  h + f = h + g -> f = g.
Proof.
(* Goal: forall (_ : is_true (@fdisjoint K (@domf K V f) (@domf K V h))) (_ : is_true (@fdisjoint K (@domf K V g) (@domf K V h))) (_ : @eq (finMap K V) (catf h f) (catf h g)), @eq (@finmap_of K V (Phant (forall _ : Choice.sort K, V))) f g *)
move=> dfg dgh; rewrite -disjoint_catfC // -[RHS]disjoint_catfC //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: disjoint_catfIs.
Qed.

End Cat.

Module Import FmapE.
Definition fmapE := (fndSome, getfE, setfK, fnd_set, getf_set,
  setfNK, fnd_reducef, get_reducef, fnd_filterf, get_filterf,
  fnd_restrict, getf_restrict, fnd_rem, fnd_rem1,
  restrictfT, restrictf0, restrictf_id, remf_id, remf1_id,
  fnd_cat).
End FmapE.

Arguments catf : simpl never.
Notation "f + g" := (catf f g) : fset_scope.

Section FinMapKeyType.
Variables (K V : choiceType).
Implicit Types (f g : {fmap K -> V}).

Definition codomf f : {fset V} := [fset f k | k : domf f].

Lemma mem_codomf f v : (v \in codomf f) = [exists x : domf f, f x == v].
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply: sameP existsP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: (iffP (imfsetP _ _ _ _)) => /= [[x _ ->]|[x /eqP <-]]; exists x.
Qed.

Lemma codomfP f v : reflect (exists x, f.[? x] = Some v) (v \in codomf f).
Proof.
(* Goal: Bool.reflect (@ex (Choice.sort K) (fun x : Choice.sort K => @eq (option (Choice.sort V)) (@fnd K (Choice.sort V) (@domf K (Choice.sort V) f) (@ffun_of_fmap K (Choice.sort V) f) x) (@Some (Choice.sort V) v))) (@in_mem (Choice.sort V) v (@mem (Choice.sort V) (finSetPredType V) (codomf f))) *)
apply: (iffP (imfsetP _ _ _ _)) => /= [[x _ ->]|[k]].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by exists (val x); rewrite Some_fnd.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: fndP => //= kf [<-]; exists [` kf].
Qed.

Lemma codomfPn f v : reflect (forall x, f.[? x] != Some v) (v \notin codomf f).
Proof.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite mem_codomf negb_exists; apply: (iffP forallP) => f_eq_v x /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by case: fndP => //= kf; rewrite f_eq_v.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: contraNneq (f_eq_v (val x)) => <-; rewrite Some_fnd.
Qed.

Lemma codomf0 : codomf [fmap] = fset0.
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@fmap0 K (Choice.sort V))) (@fset0 V) *)
apply/fsetP=> k; rewrite inE; apply/negP => /codomfP [k'].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite not_fnd //= inE.
Qed.

Lemma in_codomf f (k : domf f) : f k \in codomf f.
Proof. by rewrite in_imfset. Qed.

Lemma fndSomeP f (k : K) (v : V):
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (f.[? k] = Some v) <-> {kf : k \in f & f.[kf] = v}.
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
split => [fk|[kf fk]]; last by rewrite in_fnd fk.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have kf : k \in f by rewrite -fndSome fk.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by exists kf; apply: Some_inj; rewrite Some_fnd.
Qed.

Lemma codomf_restrict f (A : {fset K})  :
  codomf f.[& A] = [fset f k | k : domf f & val k \in A].
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@restrictf K (Choice.sort V) f A)) (@Imfset.imfset imfset_key (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) V (fun k : Choice.sort (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@subfinset_finpred (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@mem_fin (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@fin_finpred (Choice.eqType (Finite.choiceType (@fset_sub_finType K (@domf K (Choice.sort V) f)))) (pred_finpredType (@fset_sub_finType K (@domf K (Choice.sort V) f))) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A)))) (Phantom (mem_pred (@fset_sub_type K (@domf K (Choice.sort V) f))) (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => andb (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) k (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (predPredType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType)) (@sort_of_simpl_pred (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType) (pred_of_argType (@fset_sub_type K (@domf K (Choice.sort V) f) : predArgType))))) (@in_mem (Choice.sort K) (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@fset_sub_subType K (@domf K (Choice.sort V) f)) k) (@mem (Choice.sort K) (finSetPredType K) A))))))) *)
apply/fsetP=> v; apply/imfsetP/imfsetP => /= [] [k kP ->].
(* Goal: @ex2 (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun x : @fset_sub_type K (@domf K (Choice.sort V) f) => is_true (@in_mem (@fset_sub_type K (@domf K (Choice.sort V) f)) x (@mem (@fset_sub_type K (@domf K (Choice.sort V) f)) (simplPredType (@fset_sub_type K (@domf K (Choice.sort V) f))) (@SimplPred (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun x0 : @fset_sub_type K (@domf K (Choice.sort V) f) => @in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) x0) (@mem (Choice.sort K) (finSetPredType K) A)))))) (fun x : @fset_sub_type K (@domf K (Choice.sort V) f) => @eq (Choice.sort V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (Choice.sort V) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (Choice.sort V) (fun x0 : @fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A))))))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) (@fincl K (@Imfset.imfset imfset_key K K (fun x1 : Choice.sort K => x1) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x1 : Choice.sort K => andb (@in_mem (Choice.sort K) x1 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x1 (@mem (Choice.sort K) (finSetPredType K) A))))))) (@domf K (Choice.sort V) f) (@fset_sub K (@domf K (Choice.sort V) f) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A)))) x0))) k) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) x)) *)
(* Goal: @ex2 (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x : Choice.sort K => x) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x : Choice.sort K => andb (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) A)))))))) (fun x : @fset_sub_type K (@Imfset.imfset imfset_key K K (fun x : Choice.sort K => x) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x : Choice.sort K => andb (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) A))))))) => is_true (@in_mem (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) x (@mem (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (predPredType (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A))))))))) (@sort_of_simpl_pred (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (pred_of_argType (@fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A))))))))))))) (fun x : @fset_sub_type K (@Imfset.imfset imfset_key K K (fun x : Choice.sort K => x) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x : Choice.sort K => andb (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) A))))))) => @eq (Choice.sort V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (Choice.sort V) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A)))))))) (Choice.sort V) (fun x0 : @fset_sub_type K (@Imfset.imfset imfset_key K K (fun x0 : Choice.sort K => x0) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x0 : Choice.sort K => andb (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) A))))))) => @FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) (@fincl K (@Imfset.imfset imfset_key K K (fun x1 : Choice.sort K => x1) (@mem_fin (Choice.eqType K) (simplPredType (Choice.sort K)) (@subfinset_finpred K (@mem_fin (Choice.eqType K) (seq_predType (Choice.eqType K)) (@fset_finpred K (@domf K (Choice.sort V) f))) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A))))) (Phantom (mem_pred (Choice.sort K)) (@mem (Choice.sort K) (simplPredType (Choice.sort K)) (@SimplPred (Choice.sort K) (fun x1 : Choice.sort K => andb (@in_mem (Choice.sort K) x1 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Choice.sort V) f))) (@in_mem (Choice.sort K) x1 (@mem (Choice.sort K) (finSetPredType K) A))))))) (@domf K (Choice.sort V) f) (@fset_sub K (@domf K (Choice.sort V) f) (@pred_of_simpl (Choice.sort K) (@pred_of_mem_pred (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) A)))) x0))) x)) *)
  have := valP k; rewrite !inE => /andP [kf kA]; exists [` kf] => //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite ffunE /= -getfE.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have kA : val k \in [fset x | x in domf f & x \in A] by rewrite !inE (valP k).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by exists [` kA]; rewrite // ?ffunE !getfE.
Qed.

Lemma codomf_restrict_exists f (A : {fset K})  :
  codomf f.[& A] = [fset v in codomf f
                   | [exists k : domf f, (val k \in A) && (f k == v)]].
Proof.
rewrite codomf_restrict; apply/fsetP => v; rewrite !(in_fset, inE) /=; apply/imfsetP/idP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> [k kA ->]; rewrite in_codomf /=; apply/existsP; exists k; apply/andP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /andP[fkdom /existsP [k /andP[kA /eqP<-]]]; exists k.
Qed.

Lemma codomf_rem f (A : {fset K})  :
  codomf f.[\ A] = [fset f k | k : domf f & val k \notin A].
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite codomf_restrict.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: eq_imfset => //= x /=; rewrite -!topredE /= !inE (valP x) andbT.
Qed.

Lemma codomf_rem_exists f (A : {fset K})  :
  codomf f.[\ A] = [fset v in codomf f
                   | [exists k : domf f, (val k \notin A) && (f k == v)]].
Proof.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite codomf_restrict_exists; apply: eq_imfset => x //=.
(* Goal: @eq bool (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) (@fsetD K (@domf K (Choice.sort V) f) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (simplPredType (Choice.sort V)) (@SimplPred (Choice.sort V) (fun x : Choice.sort V => andb (@in_mem (Choice.sort V) x (@mem (Choice.sort V) (predPredType (Choice.sort V)) (fun x0 : Choice.sort V => @pred_of_eq_seq (Choice.eqType V) (@enum_fset V (codomf f)) x0))) (negb (@FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Choice.sort V) f)) (fun k : @fset_sub_type K (@domf K (Choice.sort V) f) => @FiniteQuant.ex (@fset_sub_finType K (@domf K (Choice.sort V) f)) (FiniteQuant.Quantified (andb (negb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) k) (@mem (Choice.sort K) (finSetPredType K) A))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) k) x))) k))))))) *)
  rewrite !inE; case: (_ \in _) => //=.
apply/existsP/existsP => [] /= [k]; rewrite ?inE;
by do 2?[move=>/andP []]; exists k; rewrite ?inE; do 2?[apply/andP; split].
Qed.

Lemma in_codomf_rem1 f (k : K) (kf : k \in domf f)  :
  codomf f.[~ k] =
  if [exists k' : domf f, (val k' != k) && (f k' == f.[kf])] then codomf f
  else codomf f `\ f.[kf].
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsetP => v; rewrite codomf_rem_exists (fun_if (fun x => v \in x)) !(in_fset, inE).
(* Goal: @eq (option (Equality.sort V)) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) k) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) k) *)
have [vf|vNf] := boolP (_ \in codomf f); rewrite /= ?(andbF,andbT) ?if_same //.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite -/(_ || _); apply/existsP/idP => /= [[k' /andP[xk /eqP <-]]|].
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  rewrite orbC -implybE; apply/implyP => eq_fk.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite inE /= in xk; apply/existsP; exists k'; rewrite // xk eq_fk.
(* Goal: @eq (option (Equality.sort V)) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) k) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) k) *)
have /imfsetP /= [[l lf] _ ->] := vf; rewrite orbC.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [->|neq_f _] := altP (f.[lf] =P _).
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> /existsP [m /andP[Nmk /eqP <-]]; exists m; rewrite eqxx inE Nmk.
(* Goal: @ex (@fset_sub_type K (@domf K (Choice.sort V) f)) (fun x : @fset_sub_type K (@domf K (Choice.sort V) f) => is_true (andb (negb (@in_mem (Choice.sort K) (@fsval K (@domf K (Choice.sort V) f) x) (@mem (Choice.sort K) (finSetPredType K) (@fset1 K k)))) (@eq_op (Choice.eqType V) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) x) (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Choice.sort V) f)) (Choice.sort V) (@ffun_of_fmap K (Choice.sort V) f) (@FSetSub K (@domf K (Choice.sort V) f) l lf))))) *)
exists [` lf]; rewrite eqxx andbT inE /=.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply: contra neq_f => /eqP eq_lk; rewrite eq_lk in lf *.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply/eqP; congr f.[_]; apply: bool_irrelevance.
Qed.

Lemma codomf_set f (k : K) (v : V) (kf : k \in domf f) :
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  codomf f.[k <- v] = v |` codomf f.[~ k].
Proof.
(* Goal: @eq (@finset_of V (Phant (Choice.sort V))) (codomf (@setf K (Choice.sort V) f k v)) (@fsetU V (@fset1 V v) (codomf (@restrictf K (Choice.sort V) f (@fsetD K (@domf K (Choice.sort V) f) (@fset1 K k))))) *)
rewrite -setf_rem1; apply/fsetP=> v'; rewrite !inE.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [->|neq_v'v] /= := altP eqP.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by apply/codomfP; exists k; rewrite fnd_set eqxx.
apply/codomfP/codomfP => [] [k' fk'_eq]; exists k';
move: fk'_eq; rewrite fnd_set.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by have [_ [eq_vv']|//] := altP eqP; rewrite eq_vv' eqxx in neq_v'v *.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have [->|//] := altP eqP; rewrite fnd_rem inE eqxx.
Qed.

End FinMapKeyType.

Module Import FinmapInE.
Definition inE := (inE, mem_codomf, mem_catf, mem_remfF,
                   mem_filterf, mem_reducef, mem_restrictf,
                   mem_remf, mem_remf1, mem_setf).
End FinmapInE.

Section FsfunDef.

Variables (K : choiceType) (V : eqType) (default : K -> V).

Record fsfun := Fsfun {
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  fmap_of_fsfun : {fmap K -> V};
  _ : [forall k : domf fmap_of_fsfun,
       fmap_of_fsfun k != default (val k)]
}.

Canonical fsfun_subType := Eval hnf in [subType for fmap_of_fsfun].
Definition fsfun_eqMixin := [eqMixin of fsfun by <:].
Canonical  fsfun_eqType := EqType fsfun fsfun_eqMixin.

Fact fsfun_subproof (f : fsfun) :
  forall (k : K) (kf : k \in fmap_of_fsfun f),
  (fmap_of_fsfun f).[kf]%fmap != default k.
Proof.
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default (@fsval K S u)) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) (@fsval K S u))) (h u) *)
case:f => f f_stable k kf /=.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
exact: (forallP f_stable [` kf]).
Qed.

Definition fun_of_fsfun (f : fsfun) (k : K) :=
  odflt (default k) (fmap_of_fsfun f).[? k]%fmap.
End FsfunDef.

Coercion fun_of_fsfun : fsfun >-> Funclass.

Module Type FinSuppSig.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Axiom fs : forall (K : choiceType) (V : eqType) (default : K -> V),
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  fsfun default -> {fset K}.
Axiom E : fs = (fun K V d f => domf (@fmap_of_fsfun K V d f)).
End FinSuppSig.
Module FinSupp : FinSuppSig.
Definition fs := (fun K V d f => domf (@fmap_of_fsfun K V d f)).
Definition E := erefl fs.
End FinSupp.
Notation finsupp := FinSupp.fs.
Canonical unlockable_finsupp := Unlockable FinSupp.E.

Section FSfunBasics.

Variables (K : choiceType) (V : eqType) (default : K -> V).
Implicit Types (f : fsfun default) (k : K) (v : V).

Lemma mem_finsupp f k : (k \in finsupp f) = (f k != default k).
Proof.
(* Goal: @eq bool (@in_mem (Choice.sort K) k (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K V default f))) (negb (@eq_op V (@fun_of_fsfun K V default f k) (default k))) *)
rewrite /fun_of_fsfun [finsupp]unlock; case: fndP; rewrite ?eqxx //=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> kf; rewrite fsfun_subproof.
Qed.

Lemma memNfinsupp f k : (k \notin finsupp f) = (f k == default k).
Proof. by rewrite mem_finsupp negbK. Qed.

Lemma fsfun_dflt f k : k \notin finsupp f -> f k = default k.
Proof. by rewrite mem_finsupp negbK => /eqP. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
CoInductive fsfun_spec f k : V -> bool -> Type :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  | FsfunOut : k \notin finsupp f -> fsfun_spec f k (default k) false
  | FsfunIn  (kf : k \in finsupp f) : fsfun_spec f k (f k) true.

Lemma finsuppP f k : fsfun_spec f k (f k) (k \in finsupp f).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [kf|kNf] := boolP (_ \in _); first by constructor.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite fsfun_dflt // ; constructor.
Qed.

Lemma fsfunP f f' : f =1 f' <-> f = f'.
Proof.
(* Goal: iff (@eqfun (Equality.sort V) (Choice.sort K) (@fun_of_fsfun K V default f) (@fun_of_fsfun K V default f')) (@eq (@fsfun K V default) f f') *)
split=> [eq_fg|->//]; apply/val_inj/fmapP => k.
(* Goal: @eq (option (Equality.sort V)) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f)) k) (@fnd K (Equality.sort V) (@domf K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) (@ffun_of_fmap K (Equality.sort V) (@val (@finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V))) (fun x : @finmap_of K (Equality.sort V) (Phant (forall _ : Choice.sort K, Equality.sort V)) => @FiniteQuant.quant0b (@fset_sub_finType K (@domf K (Equality.sort V) x)) (fun k : @fset_sub_type K (@domf K (Equality.sort V) x) => @FiniteQuant.all (@fset_sub_finType K (@domf K (Equality.sort V) x)) (FiniteQuant.Quantified (negb (@eq_op V (@FunFinfun.fun_of_fin (@fset_sub_finType K (@domf K (Equality.sort V) x)) (Equality.sort V) (@ffun_of_fmap K (Equality.sort V) x) k) (default (@val (Choice.sort K) (fun x0 : Choice.sort K => @in_mem (Choice.sort K) x0 (@mem (Choice.sort K) (finSetPredType K) (@domf K (Equality.sort V) x))) (@fset_sub_subType K (@domf K (Equality.sort V) x)) k))))) k)) (@fsfun_subType K V default) f')) k) *)
have := eq_fg k; rewrite /(f _) /(f' _) /=.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: fndP; case: fndP => //= kf kf'; first by move->.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move/eqP/negPn; rewrite fsfun_subproof.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move/eqP/negPn; rewrite eq_sym fsfun_subproof.
Qed.

Lemma fsfun_injective_inP  f (T : {fset K}) :
  reflect {in T &, injective f} (injectiveb [ffun x : T => f (val x)]).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
apply: (iffP (@injectiveP _ _ _)) => f_inj a b; last first.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite !ffunE => *; apply: val_inj; apply: f_inj => //; apply: valP.
(* Goal: forall (_ : is_true (@in_mem (Choice.sort K) a (@mem (Choice.sort K) (finSetPredType K) T))) (_ : is_true (@in_mem (Choice.sort K) b (@mem (Choice.sort K) (finSetPredType K) T))) (_ : @eq (Equality.sort V) (@fun_of_fsfun K V default f a) (@fun_of_fsfun K V default f b)), @eq (Choice.sort K) a b *)
move=> aT bT eq_ga_gb; have := f_inj.[aT].[bT]; rewrite !ffunE /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /(_ eq_ga_gb) /(congr1 val).
Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition fsfun_of_can_ffun (T : {fset K}) (g : {ffun T -> V})
          (can_g : forall k : T,  g k != default (val k)) :=
  @Fsfun K V default (FinMap g) (appP forallP idP can_g).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fsfun_of_can_ffunE (T : {fset K}) (g : {ffun T -> V})
          (can_g : forall k : T ,  g k != default (val k)) k (kg : k \in T) :
  (fsfun_of_can_ffun can_g) k = g.[kg].
Proof. by rewrite/fun_of_fsfun in_fnd. Qed.

End FSfunBasics.

Notation "{ 'fsfun' ty 'for' dflt }" := (fsfun (dflt : ty))
  (at level 0, format "{ 'fsfun' ty  'for'  dflt }") : type_scope.
Notation "{ 'fsfun' ty 'of' x => dflt }" := {fsfun ty for fun x => dflt}
  (at level 0, x at level 99,
   format "{ 'fsfun'  ty  'of'  x  =>  dflt }") : type_scope.
Notation "{ 'fsfun' ty 'with' dflt }" := {fsfun ty of _ => dflt}
  (at level 0, format "{ 'fsfun'  ty  'with'  dflt }") : type_scope.
Notation "{ 'fsfun' ty }" := {fsfun ty of x => x}
  (at level 0, format "{ 'fsfun'  ty }") : type_scope.

Notation "{ 'fsfun' 'for' dflt }" := {fsfun _ for dflt}
  (at level 0, only parsing) : type_scope.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Notation "{ 'fsfun' 'of' x : T => dflt }" := {fsfun T -> _ of x => dflt}
  (at level 0, x at level 99, only parsing) : type_scope.
Notation "{ 'fsfun' 'of' x => dflt }" := {fsfun of x : _ => dflt}
  (at level 0, x at level 99, only parsing) : type_scope.
Notation "{ 'fsfun' 'with' dflt }" := {fsfun of _ => dflt}
  (at level 0, only parsing) : type_scope.

Module Type FsfunSig.
Section FsfunSig.
Variables (K : choiceType) (V : eqType) (default : K -> V).

Parameter of_ffun : forall (S : {fset K}), (S -> V) -> unit -> fsfun default.
Variables (S : {fset K}) (h : S -> V).
Axiom of_ffunE :forall key x, of_ffun h key x = odflt (default x) (omap h (insub x)).

End FsfunSig.
End FsfunSig.

Module Fsfun : FsfunSig.
Section FsfunOfFinfun.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Variables (K : choiceType) (V : eqType) (default : K -> V)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
          (S : {fset K}) (h : S -> V).

Let fmap :=
  [fmap a : [fsetval a in {: S} | h a != default (val a)]
   => h (fincl (fset_sub_val _ _) a)].

Fact fmapP a : fmap a != default (val a).
Proof.
(* Goal: @eq bool (@fsubset K A (@fsetI K B C)) (andb (@fsubset K A B) (@fsubset K A C)) *)
rewrite ffunE; have /in_fset_valP [a_in_S] := valP a.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by have -> : [` a_in_S] = fincl (fset_sub_val _ _) a by exact/val_inj.
Qed.

Definition of_ffun (k : unit) := fsfun_of_can_ffun fmapP.

Lemma of_ffunE key x : of_ffun key x = odflt (default x) (omap h (insub x)).
Proof.
(* Goal: @eq (Equality.sort V) (@fun_of_fsfun K V default (of_ffun key) x) (@Option.default (Equality.sort V) (default x) (@Option.map (@fset_sub_type K S) (Equality.sort V) h (@insub (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) x))) *)
rewrite /fun_of_fsfun /=.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
case: insubP => /= [u _ <-|xNS]; last first.
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default x) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) x)) (default x) *)
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default (@fsval K S u)) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) (@fsval K S u))) (h u) *)
  case: fndP => //= kf; rewrite !ffunE /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by set y := (X in h X); rewrite (valP y) in xNS.
(* Goal: @eq (Equality.sort V) (@Option.default (Equality.sort V) (default (@fsval K S u)) (@fnd K (Equality.sort V) (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) (@FunFinfun.finfun (@fset_sub_finType K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a))))))))) (Equality.sort V) (fun a : @fset_sub_type K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (fun a : @fset_sub_type K S => @fsval K S a) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun a : @fset_sub_type K S => negb (@eq_op V (h a) (default (@fsval K S a)))))))) => h (@fincl K (@Imfset.imfset imfset_key (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) K (@fsval K S) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0)))))) (Phantom (mem_pred (@fset_sub_type K S)) (@mem (@fset_sub_type K S) (simplPredType (@fset_sub_type K S)) (@SimplPred (@fset_sub_type K S) (fun x : @fset_sub_type K S => negb (@eq_op V (h x) (default (@fsval K S x)))))))) S (@fset_sub_val imfset_key K S (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (simplPredType (@fset_sub_type K S)) (@subfinset_finpred (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S)) (@mem_fin (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (predPredType (@fset_sub_type K S)) (@fin_finpred (Choice.eqType (@sub_choiceType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S))) (pred_finpredType (@subFinType_finType K (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subfinType K S))) (@sort_of_simpl_pred (@fset_sub_type K S) (pred_of_argType (@fset_sub_type K S))))) (fun a0 : @fset_sub_type K S => negb (@eq_op V (h a0) (default (@fsval K S a0))))))) a))) (@fsval K S u))) (h u) *)
case: fndP => /= [kf|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite ffunE; congr (h _); apply/val_inj => //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite inE /= -topredE /= negbK => /eqP ->.
Qed.

End FsfunOfFinfun.
End Fsfun.
Canonical fsfun_of_funE K V default S h key x :=
   Unlockable (@Fsfun.of_ffunE K V default S h key x).

Fact fsfun_key : unit. Proof. exact: tt. Qed.

Definition fsfun_of_ffun key (K : choiceType) (V : eqType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  (S : {fset K}) (h : S -> V) (default : K -> V) :=
  (Fsfun.of_ffun default h key).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition fsfun_choiceMixin (K V : choiceType) (d : K -> V) :=
  [choiceMixin of fsfun d by <:].
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Canonical  fsfun_choiceType (K V : choiceType) (d : K -> V) :=
  ChoiceType (fsfun d) (fsfun_choiceMixin d).

Delimit Scope fsfun_scope with fsfun.

Notation "[ 'fsfun[' key ] x : aT => F | default ]" :=
  (fsfun_of_ffun key (fun x : aT => F) (fun x => default))
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x : aT => F | default ]" :=
  [fsfun[fsfun_key] x : aT => F | default]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] x : aT => F ]" := [fsfun[key] x : aT => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x : aT => F ]" := [fsfun x : aT => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] x => F | default ]" :=
  [fsfun[key] x : _ => F | default ]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x => F | default ]" := [fsfun x : _ => F | default]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] x => F ]" := [fsfun[key] x => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x => F ]" := [fsfun x => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ]=> F | default ]" :=
  [fsfun[key] _ => F | default ]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun=>' F | default ]" := [fsfun _ => F | default]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ]=> F ]" := [fsfun[key]=> F | _]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun=>' F ]" := [fsfun=> F | _]
  (at level 0, only parsing) : fun_scope.

Definition fsfun_of_fun key (K : choiceType) (V : eqType)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
   (S : {fset K}) (h : K -> V) default :=
  [fsfun[key] x : S => h (val x) | default x].

Notation "[ 'fsfun[' key ] x 'in' S => F | default ]" :=
  (fsfun_of_fun key S (fun x => F) (fun x => default))
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x 'in' S => F | default ]" :=
  [fsfun[fsfun_key] x in S => F | default]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] x 'in' S => F ]" :=
  [fsfun[key] x in S => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' x 'in' S => F ]" :=
  [fsfun[fsfun_key] x in S => F | _]
  (at level 0, x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] 'in' S => F | default ]" :=
  [fsfun[key] _ in S => F | default]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun' 'in' S => F | default ]" :=
  [fsfun[fsfun_key] in S => F | default]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun[' key ] 'in' S => F ]" :=
  [fsfun[key] in S => F | _]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun' 'in' S => F ]" :=
  [fsfun[fsfun_key] in S => F | _]
  (at level 0, only parsing) : fun_scope.

(* only printing *)
Notation "[ 'fs' 'fun' x : aT => F ]" := [fsfun[_] x : aT => F]
  (at level 0, x at level 99,
  format "[ 'fs' 'fun'  x  :  aT  =>  F ]") : fun_scope.

Notation "[ 'fs' 'fun' x 'in' aT => F ]" := [fsfun[_] x in aT => F]
  (at level 0, x at level 99,
  format "[ 'fs' 'fun'  x  'in'  aT  =>  F ]") : fun_scope.

Fact fsfun0_key : unit. Proof. exact: tt. Qed.
Notation "[ 'fsfun' 'for' default ]" := (fsfun_of_ffun fsfun0_key [fmap] default)
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun' 'of' x => default ]" := [fsfun for fun x => default]
  (at level 0,  x at level 99, only parsing) : fun_scope.

Notation "[ 'fsfun' 'with' default ]" := [fsfun of _ => default]
  (at level 0, only parsing) : fun_scope.

Notation "[ 'fsfun' ]" := [fsfun for _]
 (at level 0, format "[ 'fsfun' ]") : fun_scope.

Section FsfunTheory.
Variables (key : unit) (K : choiceType) (V : eqType) (default : K -> V).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fsfun_ffun (S : {fset K}) (h : S -> V) (x : K) :
  [fsfun[key] a : S => h a | default a] x =
  odflt (default x) (omap h (insub x)).
Proof. by rewrite unlock. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma fsfun_fun (S : {fset K}) (h : K -> V) (x : K) :
  [fsfun[key] a in S => h a | default a] x =
   if x \in S then h x else (default x).
Proof. by rewrite fsfun_ffun; case: insubP => //= [u -> ->|/negPf ->]. Qed.

Lemma fsfun0E : [fsfun for default] =1 default.
Proof. by move=> x; rewrite unlock insubF ?inE. Qed.

Definition fsfunE := (fsfun_fun, fsfun0E).

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Lemma finsupp_sub  (S : {fset K}) (h : S -> V) :
  finsupp [fsfun[key] a : S => h a | default a] `<=` S.
Proof.
(* Goal: is_true (@fsubset K (@FinSupp.fs K V (fun a : Choice.sort K => default a) (@fsfun_of_ffun key K V S (fun a : @fset_sub_type K S => h a) (fun a : Choice.sort K => default a))) S) *)
apply/fsubsetP => a; rewrite mem_finsupp unlock /=.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by case: insubP => //=; rewrite eqxx.
Qed.

Lemma finsupp0 : finsupp [fsfun for default] = fset0.
Proof. by apply/fsetP => x; rewrite !inE mem_finsupp fsfunE eqxx. Qed.

Lemma fsfun0_inj : injective default -> injective [fsfun for default].
Proof. by move=> def_inj x y; rewrite !fsfunE => /def_inj. Qed.

Lemma in_finsupp0 x : x \in finsupp [fsfun for default] = false.
Proof. by rewrite finsupp0 inE. Qed.

End FsfunTheory.

Module Import FsfunInE2.
Definition inE := (inE, in_finsupp0).
End FsfunInE2.

Section FsfunIdTheory.

Variables (K : choiceType).
Implicit Types (f g : {fsfun K -> K}).

Fact fsfun_comp_key : unit. Proof. exact: tt. Qed.
Definition fsfun_comp g f : {fsfun _} :=
  [fsfun[fsfun_comp_key] k in finsupp f `|` finsupp g => g (f k)].

Notation "g \o f" := (fsfun_comp g f) : fsfun_scope.

Lemma fscompE g f : (g \o f)%fsfun =1 g \o f.
Proof.
(* Goal: @eqfun (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) (fsfun_comp g f)) (@funcomp (Equality.sort (Choice.eqType K)) (Choice.sort K) (Choice.sort K) tt (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) g) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)) *)
move=> k; rewrite fsfun_ffun; case: insubP => //= [u _ <- //|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite inE => /norP [kNf kNg]; rewrite !fsfun_dflt.
Qed.

Lemma fscomp0f : left_id [fsfun] fsfun_comp.
Proof. by move=> f; apply/fsfunP=> k; rewrite fscompE /= fsfun0E. Qed.

Lemma fscompA : associative fsfun_comp.
Proof. by move=> f g h; apply/fsfunP => k; rewrite !fscompE /= !fscompE. Qed.

Lemma fscomp_inj g f : injective f -> injective g -> injective (g \o f)%fsfun.
Proof. by move=> f_inj g_inj k k'; rewrite !fscompE => /= /g_inj /f_inj. Qed.

(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
Definition fsinjectiveb : pred {fsfun K -> K} :=
  [pred f | (injectiveb [ffun a : finsupp f => f (val a)])
            && [forall a : finsupp f, f (val a) \in finsupp f]].

Let equivalent (Ps : seq Prop) :=
  if Ps is P0 :: Ps then
  let fix aux (P : Prop) (Qs : seq Prop) :=
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
      if Qs is Q :: Qs then (P -> Q) /\ (aux Q  Qs) else P -> P0
  in aux P0 Ps else True.

Lemma fsinjective_subproof f :
  equivalent [:: injective f
              ; let S := finsupp f in
                {in S &, injective f} /\ forall a : S, f (val a) \in S
              ; exists2 S : {fset K}, (finsupp f `<=` S)
                & {in S &, injective f} /\ forall a : S, f (val a) \in S].
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
split => /= [f_inj|]; last split=> [[f_inj f_st]|[S fS [f_inj f_st]] a b].
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
- split=> [a b ? ?|a]; first exact: f_inj.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  rewrite mem_finsupp (inj_eq f_inj) -mem_finsupp; apply/valP.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
- by exists (finsupp f)=> //; apply: fsubset_refl.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have Nfinsupp := contra (fsubsetP fS _).
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
wlog /andP [aS bNS] : a b / (a \in S) && (b \notin S) => [hwlog|]; last first.
(* Goal: @big_rel_class_of (@finset_of K (Phant (Choice.sort K))) (@fsubset K) *)
  rewrite (fsfun_dflt (Nfinsupp _ bNS)) => fb_eq_a.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  by move: bNS; rewrite -fb_eq_a (f_st.[aS]).
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
have [[aS|aNS] [bS|bNS]] := (boolP (a \in S), boolP (b \in S)); first 3 last.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
- by rewrite !fsfun_dflt // ?Nfinsupp.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
- exact: f_inj.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
- by apply: hwlog; rewrite aS.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> fab; symmetry; apply: hwlog; rewrite // bS.
Qed.

Lemma fsinjectiveP f : reflect (injective f) (fsinjectiveb f).
Proof.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [H1 [H2 H3]]:= fsinjective_subproof f.
(* Goal: is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) a)) (@mem (Choice.sort K) (finSetPredType K) (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f))) *)
(* Goal: @ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x1) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Choice.sort K) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f (@fsval K S a)) (@mem (Choice.sort K) (finSetPredType K) S)))) *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
rewrite /fsinjectiveb; apply: (iffP idP)=> [|].
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by move=> /andP [/fsfun_injective_inP ? /forallP ?]; apply/H3/H2.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by move=> /H1 [/fsfun_injective_inP ? /forallP ?]; apply/andP.
Qed.

Lemma fsinjectivebP f :
  reflect (exists2 S : {fset K}, (finsupp f `<=` S)
           & {in S &, injective f} /\ forall a : S, f (val a) \in S)
        (fsinjectiveb f).
Proof.
(* Goal: Bool.reflect (@ex2 (@finset_of K (Phant (Choice.sort K))) (fun S : @finset_of K (Phant (Choice.sort K)) => is_true (@fsubset K (@FinSupp.fs K (Choice.eqType K) (fun x : Choice.sort K => x) f) S)) (fun S : @finset_of K (Phant (Choice.sort K)) => and (@prop_in2 (Choice.sort K) (@mem (Choice.sort K) (finSetPredType K) S) (fun x1 x2 : Choice.sort K => forall _ : @eq (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x1) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f x2), @eq (Choice.sort K) x1 x2) (inPhantom (@injective (Equality.sort (Choice.eqType K)) (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f)))) (forall a : @fset_sub_type K S, is_true (@in_mem (Equality.sort (Choice.eqType K)) (@fun_of_fsfun K (Choice.eqType K) ((fun x : Choice.sort K => x) : forall _ : Choice.sort K, Choice.sort K) f (@val (Choice.sort K) (fun x : Choice.sort K => @in_mem (Choice.sort K) x (@mem (Choice.sort K) (finSetPredType K) S)) (@fset_sub_subType K S) a)) (@mem (Choice.sort K) (finSetPredType K) S))))) (fsinjectiveb f) *)
have [H1 [H2 H3]]:= fsinjective_subproof f.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by apply: (iffP (fsinjectiveP _)) => //; by move=> /H1 /H2.
Qed.

End FsfunIdTheory.

Definition inE := inE.

Export BigEnough.

Module BigEnoughFSet.
Definition big_rel_fsubset_class K : big_rel_class_of (@fsubset K).
Proof.
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
exists fsubset (fun G => \bigcup_(g <- G) g) => [|g s|g1 g2 j] //.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
  by rewrite big_cons fsubsetUl.
(* Goal: forall _ : is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) j (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))), is_true (@fsubset K g1 (@BigOp.bigop (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) (@fset0 K) (@cons (@finset_of K (Phant (Choice.sort K))) g2 j) (fun g : @finset_of K (Phant (Choice.sort K)) => @BigBody (@finset_of K (Phant (Choice.sort K))) (@finset_of K (Phant (Choice.sort K))) g (@fsetU K) true g))) *)
by rewrite big_cons => h; rewrite fsubsetU // h orbT.
Qed.

Canonical big_enough_fset K := BigRelOf (big_rel_fsubset_class K).

Ltac fset_big_enough_trans :=
  match goal with
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
(* Goal: forall _ : @eq (Choice.sort K) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f a) (@fun_of_fsfun K (Choice.eqType K) (fun x : Choice.sort K => x) f b), @eq (Choice.sort K) a b *)
  | [leq : is_true (?A `<=` ?B) |- is_true (?X `<=` ?B)] =>
       apply: fsubset_trans leq; big_enough; olddone
  end.

Ltac done := do [fset_big_enough_trans|BigEnough.done].

Ltac pose_big_fset K i :=
  evar (i : {fset K}); suff : closed i; first do
    [move=> _; instantiate (1 := bigger_than (@fsubset K) _) in (Value of i)].
End BigEnoughFSet.
