Library NumAnalysis.Subset.Subsets.FF.Finite_family

This file is part of the Rocq Numerical Analysis library
Copyright (C) Boldo, Clément, Martin, Mayero, Mouhcine
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the COPYING file for more details.

Brief description

Support for finite families.

Description

Finite families are functions from some ordinal type [0..n) to any type E. For instance, they can represent vectors. But the present module only provides results related to the finite nature of ordinals, no algebraic structure is assumed on the support type E.

Additional notation

  • 'E^n is for [0..n) → E, for any type E.

Support for operations on finite families

Naming rules:
  • functions having finite families as input and/or output have a name with suffix "F", eg notF, eqPF, castF and mapF;
  • predicates stating a universally quantified property have a name with suffix "AF", eg PAF, neqAF, iffAF;
  • predicates stating a existentially quantified property have a name with suffix "EF", eg PEF, eqEF;
  • functions involving a predicate on an ordinal type have a name with suffix "PF", eg eqPF, extendPF, filterPF.
Let E be any type. Let PE be a subset of E. Let x, x0, x1 and x2 be in E. Let A and B be families of n items of E, a.k.a. n-families (of E). Let A1 and A2 respectively be an n1-family and an n2-family.

Constructors

  • constF n x is the n-family with all items equal to x.
  • singleF x0 is the 1-family with only item x0.
  • coupleF x0 x1 is the 2-family with only items x0 and x1, in that order.
  • tripleF x0 x1 x2 is the 3-family with only items x0, x1 and x2, in that order.

Predicates

Let P be a predicate on [0..n), ie a subset of [0..n).
  • PAF P states that P i holds for all i.
  • PEF P states that there exists some i such that P i holds.
  • notF P is the complementary predicate of P, ie associating ¬ P i to each i.
Let R be a binary relation on E.
  • brAF R A B states that R (A i) (B i) holds for all i.
  • brEF R A B states that there exists some i such that R (A i) (B i) holds.
  • eqAF and eqEF are the specializations of brAF and brEF for equality.
  • neqAF and neqEF are the specializations of brAF and brEF for the negation of equality.
  • same_funF is the specialization of brAF for extensional equality of functions.
  • eqF A x associates equality A i = x to each i.
  • neqF A x associates inequality A i ≠ x to each i.
  • inF x A states that x is some item of A.
  • inclF A PE states that all items of A belong to PE.
  • invalF A1 A2 states that all items of A1 appear in A2. Warning: in the presence of doubles in A1, we may have invalF A1 A2 with n2 < n1.
  • eqPF P A B states that A and B are equal when P holds.
  • neqPF P A B states that there exists some i such that A i and B i are distinct and P i holds.
  • eqxF i0 A B states that A and B are equal except for item i0.
  • neqxF i0 A B states that there exists some i distinct from i0 such that A i and B i are distinct.
  • eqx2F i0 i1 A B states that A and B are equal except for items i0 and i1.
  • neqx2F i0 i1 A B states that there exists some i distinct from i0 and i1 such that A i and B i are distinct.
Let P1 and P2 be predicates on [0..n1) and [0..n2). Let f be a function from [0..n1) to [0..n2).
  • extendPF f P1 P2 states that either ordinal i2 : [0..n2) is in the range of f (ie, there exists some i1 such that f i1 = i2) and equality P1 i1 = P2 i2 holds, or ¬ P2 i2 holds.

Operators

Let f be a function from [0..n1) to [0..n2).
  • funF f A2 is the n1-family of values of A2 on f [0..n1), in the same order.
  • unfunF f x A1 is the n2-family with values of A1 on f [0..n1), in the same order, and x elsewhere.
Let P be a predicate on [0..n).
  • maskPF P x A is the n-family with values of A when P holds and x otherwise, in the same order.
Let H be a proof of n1 = n2.
  • castF H A1 is the n2-family made of the items of A1, in the same order.
Let B be an n.+1-family.
  • widenF_S B is the n-family made of the items of B, in the same order, except the last one.
  • liftF_S B is the n-family made of the items of B, in the same order, except the first one.
Let H be a proof of n1 ≤ n2.
  • widenF H A2 is the n1-family made of the n1 first items of A2, in the same order.
Let A12 be an (n1+n2)-family.
  • firstF A12 is the n1-family made of the n1 first items of A12, in the same order.
  • lastF A12 is the n2-family made of the n2 last items of A12, in the same order.
  • concatF A1 A2 is the (n1+n2)-family with items of A1, then items of A2, in the same order.
  • insertF i0 x0 A is the n.+1-family made of the n items of A, in the same order, and x0 inserted as the i0-th item.
Let B be an n.+1-family.
  • skipF i0 B is the n-family made of the items of B, in the same order, except the i0-th.
Let H be a proof of i1 ≠ i0.
  • insert2F H x0 x1 A is the n.+2-family made of the n items of A, in the same order, x0 inserted as the i0-th item, and x1 inserted as the i1-th item.
Let C be an n.+2-family.
  • skip2F H C is the n-family made of the items of C, in the same order, except the i0-th and the i1-th.
  • replaceF i0 x0 A is the n-family made of the items of A, in the same order, except that the i0-th item is replaced by x0.
  • replace2F i0 i1 x0 x1 A is the n-family made of the items of A, in the same order, except that the i0-th item is replaced by x0, and the i1-th is replaced by x1.
Let p : [0..n) -> [0..n).
  • permutF p A is the n-family made of items A (p 0), A (p 1),..., and A (p n-1).
  • revF A is the reversed n-family made of items A (n-1), A (n-2),..., A 1, A 0.
Let B be an n.+1-family.
  • moveF i0 i1 B is the n.+1-family made of items of B, in the same order, except that i0-th item is moved to i1-th slot.
  • transpF i0 i1 A is the n-family made of items of A, in the same order, except that i0-th and i1-th items are exchanged.
Let P be a predicate on [0..n).
Let F be any type. Let f : E → F and fi : [0..n) → E → F be functions.
  • mapF f A is the n-family made of the images by f of items of A, in the same order.
  • mapiF fi A is the n-family made of items fi 0 (A 0), fi 1 (A 1),..., and fi (n-1) (A (n-1)).
Let G be any type. Let f : E → F → G be a function. Let A B be n-families.
  • map2F f A B is the n-family made of the images by f of items of A and B, in the same order.
Let f be an n-family of functions F → G. Let g : E → F. Let f : F → G. Let g be an n-family of functions E → F. Let f be an n-family of functions F → G. Let g be an n-family of functions E → F.

Functional types

Let T be any type, and d m be natural numbers.
  • FTd T d := 'T^d → T is the type of functions from 'T^d to T;
  • FTdm d m := 'T^d → 'T^m is the type of functions from 'T^d to 'T^m;
  • FRd and FRdm are specializations for the type of real numbers R.

About the API

Definitions and statements are mainly plain-Coq compatible (ie use of Prop instead of bool).

Used logic axioms

Usage

This module may be used through the import of NumAnalysis.Subset.Subsets.Subsets, NumAnalysis.Subset.Subsets.Subsets_wDep, NumAnalysis.Algebra.Algebra_wDep, NumAnalysis.Lebesgue.Lebesgue_p_wDep, NumAnalysis.Lebesgue.Bochner.Bochner_wDep, NumAnalysis.LM.LM_wDep, or NumAnalysis.FEM.FEM_wDep, where it is exported.

From NumAnalysis Require Import stdlib_wR.

From NumAnalysis Require Import ssr_wMC.
From mathcomp Require Import seq path.

From NumAnalysis Require Import Numbers_wDep.
From NumAnalysis Require Import Subset Subset_dec Subset_charac.
From NumAnalysis Require Import Function Function_sub Sub_type.
From NumAnalysis Require Import Binary_relation ord_compl.

Open Scope nat_scope.

Notation "''' E ^ n" := ([0..n) → E)
  (at level 8, E at level 2, n at level 2, format "''' E ^ n").

Section FF_Def0.

Context {n : nat}.
Variable P Q : 'Prop^n.

The prefix "P" stands for Prop, "A" for ∀ and "E" for ∃.
Definition PAF : Prop := ∀ i, P i.
Definition PEF : Prop := ∃ i, P i.

End FF_Def0.

Section FF_Def1a.

Context {E : Type}.
Variable R : E → E → Prop.

Context {n : nat}.
Variable A B : 'E^n.

The prefix "br" stands for "binary relation".
Definition brAF : Prop := PAF (fun i ⇒ R (A i) (B i)).
Definition brEF : Prop := PEF (fun i ⇒ R (A i) (B i)).

End FF_Def1a.

Section FF_Def1b.

Context {E F : Type}.
Variable R : E → E → Prop.

Context {n : nat}.
Variable A B : 'E^n.

Definition eqAF : Prop := brAF eq A B.
Definition eqEF : Prop := brEF eq A B.

Definition neqAF : Prop := brAF neq A B.
Definition neqEF : Prop := brEF neq A B.

Variable f g : '(E → F)^n.

Definition same_funF : Prop := brAF same_fun f g.

End FF_Def1b.

Section FF_Facts0a.

Context {E : Type}.
Context {n : nat}.

Variable A B : 'E^n.

Lemma extF : eqAF A B → A = B.
Proof. intros; apply fun_ext; easy. Qed.

Lemma extF_rev : A = B → eqAF A B.
Proof. intros H; rewrite H; easy. Qed.

Lemma extF_equiv : A = B ↔ eqAF A B.
Proof. split; [apply extF_rev | apply extF]. Qed.

Lemma extF_compat : ∀ i j, i = j → A i = A j.
Proof. move=>>; apply f_equal. Qed.

Lemma nextF : neqEF A B → A ≠ B.
Proof. intros H1 H2; rewrite H2 in H1; destruct H1; easy. Qed.

Lemma nextF_rev : A ≠ B → neqEF A B.
Proof.
intros H1; apply not_all_ex_not_equiv; intros H2; apply H1, extF; easy.
Qed.

Lemma nextF_equiv : A ≠ B ↔ neqEF A B.
Proof. split; [apply nextF_rev | apply nextF]. Qed.

Lemma nextF_reg : ∀ i j, A i ≠ A j → i ≠ j.
Proof. move=>>; apply contra_not, extF_compat. Qed.

End FF_Facts0a.

Tactic Notation "extF" := apply extF; intro.
Tactic Notation "extF" ident(i) := apply extF; intros i.
Tactic Notation "extF" ident(i) ident(j) := extF i; extF j.

Section FF_ops_Def1.

Context {E F G : Type}.

Definition constF n (x : E) : 'E^n := fun⇒ x.

Definition singleF (x0 : E) : 'E^1 := constF 1 x0.

Definition coupleF (x0 x1 : E) : 'E^2 :=
  fun i ⇒ match ord2_dec i with
    | left _ ⇒ x0
    | right _ ⇒ x1
    end.

Definition tripleF (x0 x1 x2 : E) : 'E^3 :=
  fun i ⇒ match ord3_dec i with
    | inleft H ⇒ match H with
      | left _ ⇒ x0
      | right _ ⇒ x1
      end
    | inright _ ⇒ x2
    end.

Definition eqF {n} (A : 'E^n) x i : Prop := A i = x.
Definition neqF {n} (A : 'E^n) x i : Prop := A i ≠ x.

Definition inF {n} x (A : 'E^n) : Prop := ∃ i, x = A i.

Definition inclF {n} (A : 'E^n) (PE : E → Prop) : Prop := ∀ i, PE (A i).

Definition invalF {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2) : Prop :=
  inclF A1 (inF^~ A2).

Definition iffAF {n} (P Q : 'Prop^n) := ∀ i, P i ↔ Q i.

Definition eqPF {n} (P : 'Prop^n) (A B : 'E^n) : Prop :=
  ∀ i, P i → A i = B i.
Definition neqPF {n} (P : 'Prop^n) (A B : 'E^n) : Prop :=
  ∃ i, P i ∧ A i ≠ B i.

Definition eqxF {n} i0 (A B : 'E^n) : Prop := eqPF (fun i ⇒ i ≠ i0) A B.
Definition neqxF {n} i0 (A B : 'E^n) : Prop := neqPF (fun i ⇒ i ≠ i0) A B.

Definition eqx2F {n} i0 i1 (A B : 'E^n) : Prop :=
  eqPF (fun i ⇒ i ≠ i0 ∧ i ≠ i1) A B.
Definition neqx2F {n} i0 i1 (A B : 'E^n) : Prop :=
  neqPF (fun i ⇒ i ≠ i0 ∧ i ≠ i1) A B.

Definition extendPF {n1 n2} (f : [0..n1) → [0..n2))
    (P1 : 'Prop^n1) (P2 : 'Prop^n2) : Prop :=
  ∀ i2, (∃ i1, f i1 = i2 ∧ P1 i1 = P2 i2) ∨ (¬ Rg f i2 ∧ ¬ P2 i2).

Definition funF {n1 n2} (f : [0..n1) → [0..n2)) : 'E^n2 → 'E^n1 :=
  fun A2 i1 ⇒ A2 (f i1).

Definition unfunF {n1 n2} (f : [0..n1) → [0..n2)) (x0 : E) : 'E^n1 → 'E^n2 :=
  fun A1 i2 ⇒ match im_dec f i2 with
    | inleft H1 ⇒ A1 (proj1_sig H1)
    | inright _ ⇒ x0
    end.

Definition maskPF {n} (P : 'Prop^n) (x0 : E) : 'E^n → 'E^n :=
  fun A i ⇒ match classic_dec (P i) with
    | left _ ⇒ A i
    | right _ ⇒ x0
    end.

Definition castF {n1 n2} (H : n1 = n2) : 'E^n1 → 'E^n2 :=
  fun A1 i2 ⇒ A1 (cast_ord (eq_sym H) i2).

Definition castF_fun
    {T : Type} {n1 n2} (H : n1 = n2) : ('E^n1 → T) → 'E^n2 → T :=
  fun f1 A2 ⇒ f1 (castF (eq_sym H) A2).

Definition cast2F_fun {T1 T2 : Type} {n1 n2} (H : n1 = n2) :
    (('E^n1 → T1) → T2) → ('E^n2 → T1) → T2 :=
  fun F1 f2 ⇒ F1 (castF_fun (eq_sym H) f2).

Definition castF_p1S {n} (A : 'E^(n + 1)) : 'E^n.+1 := castF (addn1 n) A.
Definition castF_Sp1 {n} (A : 'E^n.+1) : 'E^(n + 1) := castF (addn1_sym n) A.
Definition castF_1pS {n} (A : 'E^(1 + n)) : 'E^n.+1 := castF (add1n n) A.
Definition castF_S1p {n} (A : 'E^n.+1) : 'E^(1 + n) := castF (add1n_sym n) A.

Definition castF_ipn {n} (i0 : [0..n]) (A : 'E^(i0 + (n - i0))) : 'E^n :=
  castF (eq_sym (ord_split i0)) A.
Definition castF_nip {n} (A : 'E^n) (i0 : [0..n]) : 'E^(i0 + (n - i0)) :=
  castF (ord_split i0) A.

Definition castF_ipS {n} (i0 : [0..n]) (A : 'E^(i0 + (n - i0).+1)) : 'E^n.+1 :=
  castF (eq_sym (ord_splitS i0)) A.
Definition castF_Sip {n} (A : 'E^n.+1) (i0 : [0..n]) : 'E^(i0 + (n - i0).+1) :=
  castF (ord_splitS i0) A.

Definition castF_SpS {n} (i0 : [0..n]) (A : 'E^(i0.+1 + (n - i0))) : 'E^n.+1 :=
  castF (eq_sym (ordS_splitS i0)) A.
Definition castF_SSp {n} (A : 'E^n.+1) (i0 : [0..n]) : 'E^(i0.+1 + (n - i0)) :=
  castF (ordS_splitS i0) A.

Definition widenF_S {n} : 'E^n.+1 → 'E^n := fun A i ⇒ A (widen_S i).
Definition liftF_S {n} : 'E^n.+1 → 'E^n := fun A i ⇒ A (lift_S i).

Definition widenF {n1 n2} (H : n1 ≤ n2) : 'E^n2 → 'E^n1 :=
  fun A2 i1 ⇒ A2 (widen_ord H i1).

Definition firstF {n1 n2} : 'E^(n1 + n2) → 'E^n1 :=
  fun A i1 ⇒ A (first_ord n2 i1).

Definition lastF {n1 n2} : 'E^(n1 + n2) → 'E^n2 :=
  fun A i2 ⇒ A (last_ord n1 i2).

Definition concatF {n1 n2} : 'E^n1 → 'E^n2 → 'E^(n1 + n2) :=
  fun A1 A2 i ⇒ match lt_dec i n1 with
    | left H ⇒ A1 (concat_l_ord H)
    | right H ⇒ A2 (concat_r_ord H)
    end.

Definition insertF {n} (i0 : [0..n]) (x0 : E) : 'E^n → 'E^n.+1 :=
  fun A i ⇒ match ord_eq_dec i i0 with
    | left _ ⇒ x0
    | right H ⇒ A (insert_ord H)
    end.

Definition insert2F
    {n} {i0 i1 : [0..n.+1]} (H : i1 ≠ i0) (x0 x1 : E) : 'E^n → 'E^n.+2 :=
  fun A i ⇒ match ord_eq_dec i i0 with
    | left _ ⇒ x0
    | right H0 ⇒ match ord_eq_dec i i1 with
      | left _ ⇒ x1
      | right H1 ⇒ A (insert2_ord H H0 H1)
      end
    end.

Definition skipF {n} (i0 : [0..n]) : 'E^n.+1 → 'E^n :=
  fun A j ⇒ A (skip_ord i0 j).

Definition skip2F {n} {i0 i1 : [0..n.+1]} (H : i1 ≠ i0) : 'E^n.+2 → 'E^n :=
  fun A j ⇒ A (skip2_ord H j).

Definition replaceF {n} (i0 : [0..n)) (x0 : E) : 'E^n → 'E^n :=
  fun A i ⇒ match ord_eq_dec i i0 with
    | left _ ⇒ x0
    | right _ ⇒ A i
    end.

Definition replace2F {n} (i0 i1 : [0..n)) (x0 x1 : E) : 'E^n → 'E^n :=
  fun A ⇒ replaceF i1 x1 (replaceF i0 x0 A).

Definition permutF {n} (p : [0..n) → [0..n)) : 'E^n → 'E^n :=
  fun A i ⇒ A (p i).
Definition revF {n} : 'E^n → 'E^n := permutF (@rev_ord n).
Definition moveF {n} (i0 i1 : [0..n]) : 'E^n.+1 → 'E^n.+1 :=
  permutF (move_ord i0 i1).
Definition transpF {n} (i0 i1 : [0..n)) : 'E^n → 'E^n :=
  permutF (transp_ord i0 i1).

Definition filterPF {n} (P : 'Prop^n) : 'E^n → 'E^(lenPF P) :=
  fun A ⇒ funF filterP_ord A.

Definition splitPF {n} (P : 'Prop^n) :
    'E^n → 'E^(lenPF P + lenPF (fun i ⇒ ¬ P i)) :=
  fun A ⇒ concatF (filterPF P A) (filterPF (fun i ⇒ ¬ P i) A).

Definition mapiF {n} (f : [0..n) → E → F) : 'E^n → 'F^n :=
  fun A i ⇒ f i (A i).
Definition mapF {n} (f : E → F) : 'E^n → 'F^n := mapiF (fun⇒ f).

Definition map2F {n} (f : E → F → G) : 'E^n → 'F^n → 'G^n :=
  fun A B i ⇒ f (A i) (B i).

End FF_ops_Def1.

Section FF_ops_Def2.

Context {E F G : Type}.

Definition pullbackF {n} : (E → F) → '(F → G)^n → '(E → G)^n :=
  fun g f ⇒ mapF (comp^~ g) f.

Definition compF_r {n} : (F → G) → '(E → F)^n → '(E → G)^n :=
  fun f g ⇒ mapF (comp f) g.

Definition compF {n} : '(F → G)^n → '(E → F)^n → '(E → G)^n :=
  fun f g ⇒ map2F comp f g.

End FF_ops_Def2.

Notation eqxF0 := (eqxF ord0).
Notation eqxFmax := (eqxF ord_max).
Notation neqxF0 := (neqxF ord0).
Notation neqxFmax := (neqxF ord_max).

Notation insertF0 := (insertF ord0).
Notation insertFmax := (insertF ord_max).
Notation skipF0 := (skipF ord0).
Notation skipFmax := (skipF ord_max).

Notation replaceF0 := (replaceF ord0).
Notation replaceFmax := (replaceF ord_max).

Notation filter_eqF_gen x0 A B := (filterPF (eqF A x0) B).
Notation filter_eqF x0 A := (filter_eqF_gen x0 A A).
Notation filter_neqF_gen x0 A B := (filterPF (neqF A x0) B).
Notation filter_neqF x0 A := (filter_neqF_gen x0 A A).
Notation split_eqF_gen x0 A B := (splitPF (eqF A x0) B).
Notation split_eqF x0 A := (split_eqF_gen x0 A A).

Section FF_ops_Facts0.

Correctness lemmas.

Context {E : Type}.

Lemma constF_correct : ∀ n (x : E) i, constF n x i = x.
Proof. easy. Qed.

Lemma singleF_0 : ∀ (x0 : E) i, singleF x0 i = x0.
Proof. easy. Qed.

Lemma singleF_correct : ∀ (A : 'E^1) i, A = singleF (A i).
Proof. intros; extF; rewrite 2!ord_one; easy. Qed.

Lemma constF_1 : ∀ (x : E), constF 1 x = singleF x.
Proof. easy. Qed.

Lemma coupleF_0 : ∀ (x0 x1 : E), coupleF x0 x1 ord0 = x0.
Proof. move=>>; unfold coupleF; destruct (ord2_dec _); easy. Qed.

Lemma coupleF_1 : ∀ (x0 x1 : E), coupleF x0 x1 ord_max = x1.
Proof. move=>>; unfold coupleF; destruct (ord2_dec _); easy. Qed.

Lemma coupleF_l :
  ∀ (x0 x1 : E) (i : {0,1}), i = ord0 → coupleF x0 x1 i = x0.
Proof. move=>> ->; apply coupleF_0. Qed.

Lemma coupleF_r :
  ∀ (x0 x1 : E) (i : {0,1}), i = ord_max → coupleF x0 x1 i = x1.
Proof. move=>> ->; apply coupleF_1. Qed.

Lemma coupleF_correct : ∀ (A : 'E^2), A = coupleF (A ord0) (A ord_max).
Proof.
intros A; unfold coupleF; extF i; destruct (ord2_dec i) as [-> | ->]; easy.
Qed.

Lemma constF_2 : ∀ (x : E), constF 2 x = coupleF x x.
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite coupleF_0; easy.
rewrite coupleF_1; easy.
Qed.

Lemma tripleF_0 : ∀ (x0 x1 x2 : E), tripleF x0 x1 x2 ord0 = x0.
Proof. move=>>; unfold tripleF; destruct (ord3_dec _) as [[|]|]; easy. Qed.

Lemma tripleF_1 : ∀ (x0 x1 x2 : E), tripleF x0 x1 x2 ord1 = x1.
Proof. move=>>; unfold tripleF; destruct (ord3_dec _) as [[|]|]; easy. Qed.

Lemma tripleF_2 : ∀ (x0 x1 x2 : E), tripleF x0 x1 x2 ord_max = x2.
Proof. move=>>; unfold tripleF; destruct (ord3_dec _) as [[|]|]; easy. Qed.

Lemma tripleF_l :
  ∀ (x0 x1 x2 : E) (i : {0,1,2}), i = ord0 → tripleF x0 x1 x2 i = x0.
Proof. move=>> ->; apply tripleF_0. Qed.

Lemma tripleF_m :
  ∀ (x0 x1 x2 : E) (i : {0,1,2}), i = ord1 → tripleF x0 x1 x2 i = x1.
Proof. move=>> ->; apply tripleF_1. Qed.

Lemma tripleF_r :
  ∀ (x0 x1 x2 : E) (i : {0,1,2}), i = ord_max → tripleF x0 x1 x2 i = x2.
Proof. move=>> ->; apply tripleF_2. Qed.

Lemma tripleF_correct :
  ∀ (A : 'E^3), A = tripleF (A ord0) (A ord1) (A ord_max).
Proof.
intros A; unfold tripleF; extF i;
    destruct (ord3_dec i) as [[-> | ->] | ->]; easy.
Qed.

Lemma constF_3 : ∀ (x : E), constF 3 x = tripleF x x x.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite tripleF_0; easy.
rewrite tripleF_1; easy.
rewrite tripleF_2; easy.
Qed.

Lemma castF_eq_sym :
  ∀ {n1 n2} (H : n1 = n2) {A2 : 'E^n2},
    castF (eq_sym H) A2 = fun i1 ⇒ A2 (cast_ord H i1).
Proof. intros; unfold castF; rewrite eq_sym_involutive; easy. Qed.

Lemma concatF_correct_l :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2)
      (i : [0..n1 + n2)) (Hi : (i < n1)%coq_nat),
    concatF A1 A2 i = A1 (concat_l_ord Hi).
Proof.
intros n1 n2 A1 A2 i Hi; unfold concatF; destruct (lt_dec _ _); try easy.
f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_correct_r :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2)
      (i : [0..n1 + n2)) (Hi : ¬ (i < n1)%coq_nat),
    concatF A1 A2 i = A2 (concat_r_ord Hi).
Proof.
intros n1 n2 A1 A2 i Hi; unfold concatF; destruct (lt_dec _ _); try easy.
f_equal; apply ord_inj; easy.
Qed.

Lemma unfunF_correct_l :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} x0 {A1 : 'E^n1} i1 i2,
    injective f → f i1 = i2 → unfunF f x0 A1 i2 = A1 i1.
Proof.
move=>> Hf <-; unfold unfunF; destruct (im_dec _ _) as [[j1 Hj1] | Hi1].
simpl; apply Hf in Hj1; rewrite Hj1; easy.
contradict Hi1; apply nonempty_is_not_empty; eexists; easy.
Qed.

Lemma unfunF_correct_r :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {x0} (A1 : 'E^n1) i2,
    (∀ i1, f i1 ≠ i2) → unfunF f x0 A1 i2 = x0.
Proof.
move=>> Hi2; unfold unfunF; destruct (im_dec _ _) as [[i1 Hi1] | Hi1] ⇒ //.
contradict Hi2; apply not_all_not_ex_equiv; ∃ i1; easy.
Qed.

Lemma unfunF_correct :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} x0 (A1 : 'E^n1) i2,
    injective f →
    (∃ i1, f i1 = i2 ∧ unfunF f x0 A1 i2 = A1 i1) ∨
    (∀ i1, f i1 ≠ i2) ∧ unfunF f x0 A1 i2 = x0.
Proof.
intros n1 n2 f x0 A1 i2 Hf; destruct (im_dec f i2) as [[i1 Hi1] | Hi2].
left; ∃ i1; split; try apply unfunF_correct_l; easy.
right; split; [| apply unfunF_correct_r];
    intros i1 Hi1; apply (Hi2 i1); easy.
Qed.

Lemma maskPF_correct_l :
  ∀ {n} {P : 'Prop^n} x0 {A : 'E^n} i, P i → maskPF P x0 A i = A i.
Proof. intros; unfold maskPF; destruct (classic_dec _); easy. Qed.

Lemma maskPF_correct_r :
  ∀ {n} {P : 'Prop^n} {x0} (A : 'E^n) i, ¬ P i → maskPF P x0 A i = x0.
Proof. intros; unfold maskPF; destruct (classic_dec _); easy. Qed.

Lemma funF_unfunF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (x0 : E),
    injective f → cancel (unfunF f x0) (funF f).
Proof. move=>> Hf A1; extF; apply unfunF_correct_l; easy. Qed.

Lemma unfunF_funF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} x0 {A2 : 'E^n2},
    injective f → unfunF f x0 (funF f A2) = maskPF (Rg f) x0 A2.
Proof.
intros n1 n2 f x0 A2 Hf; extF i2; destruct (im_dec f i2) as [[i1 <-] | Hi2].
rewrite (unfunF_correct_l _ i1)// maskPF_correct_l//.
rewrite unfunF_correct_r// maskPF_correct_r//.
rewrite image_ex; apply all_not_not_ex; intros i1 Hi1; apply (Hi2 i1); easy.
Qed.

Lemma insertF_correct_l :
  ∀ {n i0 i} x0 (A : 'E^n), i = i0 → insertF i0 x0 A i = x0.
Proof. intros; unfold insertF; destruct (ord_eq_dec _ _); easy. Qed.

Lemma insertF_correct_r :
  ∀ {n i0 i} (H : i ≠ i0) x0 (A : 'E^n),
    insertF i0 x0 A i = A (insert_ord H).
Proof.
intros; unfold insertF; destruct (ord_eq_dec _ _); try easy.
f_equal; apply insert_ord_compat_P.
Qed.

Lemma insertF_correct_rl :
  ∀ {n} {i0 i : [0..n]} (H : (i < i0)%coq_nat) x0 (A : 'E^n),
    insertF i0 x0 A i = A (narrow_S (ord_nmax_lt H)).
Proof.
intros n i0 i H x0 A; unfold insertF; destruct (ord_eq_dec _ _) as [Hi | Hi].
contradict H; rewrite Hi; apply Nat.nlt_ge; easy.
f_equal; rewrite -insert_ord_correct_l; apply insert_ord_compat_P.
Qed.

Lemma insertF_correct_rr :
  ∀ {n} {i0 i : [0..n]} (H : (i0 < i)%coq_nat) x0 (A : 'E^n),
    insertF i0 x0 A i = A (lower_S (ord_n0_gt H)).
Proof.
intros n i0 i H x0 A; unfold insertF; destruct (ord_eq_dec _ _) as [Hi | Hi].
contradict H; rewrite Hi; apply Nat.nlt_ge; easy.
f_equal; rewrite -insert_ord_correct_r; apply insert_ord_compat_P.
Qed.

Lemma insertF_correct :
  ∀ {n} i0 x0 (A : 'E^n) i, insertF i0 x0 A (skip_ord i0 i) = A i.
Proof.
intros; rewrite (insertF_correct_r (skip_ord_correct_m _ _))
    insert_skip_ord; easy.
Qed.

Lemma insert2F_correct :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 (A : 'E^n),
    insert2F H x0 x1 A = insertF i0 x0 (insertF (insert_ord H) x1 A).
Proof.
move=>>; extF; unfold insert2F, insertF.
destruct (ord_eq_dec _ _) as [Hi0 | Hi0]; try easy.
destruct (ord_eq_dec _ _) as [Hi1 | Hi1],
    (ord_eq_dec _ _) as [Hi1' | Hi1']; try easy.
contradict Hi1'; subst; apply insert_ord_compat_P.
contradict Hi1; apply (insert_ord_inj _ _ Hi1').
f_equal; unfold insert2_ord; apply insert_ord_compat_P.
Qed.

Lemma insert2F_equiv_def :
  ∀ {n i0 i1} (H10 : i1 ≠ i0) (H01 : i0 ≠ i1) x0 x1 (A : 'E^n),
    insert2F H10 x0 x1 A = insertF i1 x1 (insertF (insert_ord H01) x0 A).
Proof.
intros n i0 i1 H10 H01 x0 x1 A; extF; unfold insert2F, insertF.
destruct (ord_eq_dec _ _) as [Hi0 | Hi0],
    (ord_eq_dec _ _) as [Hi1 | Hi1]; try easy.
contradict H10; rewrite -Hi0 -Hi1; easy.
1,2: destruct (ord_eq_dec _ _) as [Hi1' | Hi1']; try easy.
contradict Hi1'; subst; apply insert_ord_compat_P.
contradict Hi0; apply (insert_ord_inj _ _ Hi1').
f_equal; apply insert2_ord_eq_sym.
Qed.

Lemma insert2F_equiv_def_alt :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 (A : 'E^n),
    insert2F H x0 x1 A =
      insertF i1 x1 (insertF (insert_ord (not_eq_sym H)) x0 A).
Proof. intros; apply insert2F_equiv_def. Qed.

Lemma skipF_correct_l :
  ∀ {n} (i0 : [0..n]) {j : [0..n)} {A : 'E^n.+1},
    (j < i0)%coq_nat → skipF i0 A j = widenF_S A j.
Proof. intros; unfold skipF; rewrite skip_ord_correct_l; easy. Qed.

Lemma skipF_correct_m :
  ∀ {n} i0 j {A : 'E^n.+1}, injective A → skipF i0 A j ≠ A i0.
Proof.
intros n i0 j A HA; unfold skipF.
apply (contra_not (HA (skip_ord i0 j) i0)), skip_ord_correct_m.
Qed.

Lemma skipF_correct_r :
  ∀ {n} (i0 : [0..n]) {j : [0..n)} {A : 'E^n.+1},
    ¬ (j < i0)%coq_nat → skipF i0 A j = liftF_S A j.
Proof. intros; unfold skipF; rewrite skip_ord_correct_r; easy. Qed.

Lemma skipF_correct :
  ∀ {n i0 i} (H : i ≠ i0) {A : 'E^n.+1},
    skipF i0 A (insert_ord H) = A i.
Proof. intros; unfold skipF; rewrite skip_insert_ord; easy. Qed.

Lemma skipF_correct_alt :
  ∀ {n i0 i j} {A : 'E^n.+1},
    i ≠ i0 → skip_ord i0 j = i → skipF i0 A j = A i.
Proof. move=>> H /(skip_insert_ord_eq H) ->; apply skipF_correct. Qed.

Lemma skip2F_correct :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A : 'E^n.+2),
    skip2F H A = skipF (insert_ord H) (skipF i0 A).
Proof. easy. Qed.

Lemma skip2F_sym :
  ∀ {n i0 i1} {H10 : i1 ≠ i0} (H01 : i0 ≠ i1) (A : 'E^n.+2),
    skip2F H10 A = skip2F H01 A.
Proof. intros; unfold skip2F; rewrite skip2_ord_sym; easy. Qed.

Lemma skip2F_sym_alt :
  ∀ {n i0 i1} {H : i1 ≠ i0} (A : 'E^n.+2),
    skip2F H A = skip2F (not_eq_sym H) A.
Proof. intros; apply skip2F_sym. Qed.

Lemma skip2F_equiv_def :
  ∀ {n i0 i1} (H10 : i1 ≠ i0) (H01 : i0 ≠ i1) {A : 'E^n.+2},
    skip2F H10 A = skipF (insert_ord H01) (skipF i1 A).
Proof. intros; rewrite -(skip2F_correct); apply skip2F_sym. Qed.

Lemma skip2F_equiv_def_alt :
  ∀ {n i0 i1} (H : i1 ≠ i0) {A : 'E^n.+2},
    skip2F H A = skipF (insert_ord (not_eq_sym H)) (skipF i1 A).
Proof. intros; apply skip2F_equiv_def. Qed.

Lemma replaceF_correct_l :
  ∀ {n i0 i} x0 (A : 'E^n), i = i0 → replaceF i0 x0 A i = x0.
Proof. intros; unfold replaceF; destruct (ord_eq_dec _ _); easy. Qed.

Lemma replaceF_correct_r :
  ∀ {n i0 i} x0 (A : 'E^n), i ≠ i0 → replaceF i0 x0 A i = A i.
Proof. intros; unfold replaceF; destruct (ord_eq_dec _ _); easy. Qed.

Lemma replace2F_correct_l0 :
  ∀ {n i0 i1 i} x0 x1 (A : 'E^n),
    i1 ≠ i0 → i = i0 → replace2F i0 i1 x0 x1 A i = x0.
Proof.
intros; subst; unfold replace2F.
rewrite replaceF_correct_r; try now apply not_eq_sym.
apply replaceF_correct_l; easy.
Qed.

Lemma replace2F_correct_l1 :
  ∀ {n} i0 {i1 i} x0 x1 (A : 'E^n),
    i = i1 → replace2F i0 i1 x0 x1 A i = x1.
Proof. intros; unfold replace2F; apply replaceF_correct_l; easy. Qed.

Lemma replace2F_correct_r :
  ∀ {n i0 i1 i} x0 x1 (A : 'E^n),
    i ≠ i0 → i ≠ i1 → replace2F i0 i1 x0 x1 A i = A i.
Proof. intros; unfold replace2F; rewrite → 2!replaceF_correct_r; easy. Qed.

Lemma replace2F_correct_eq :
  ∀ {n i0 i1} x0 x1 (A : 'E^n),
    i1 = i0 → replace2F i0 i1 x0 x1 A = replaceF i1 x1 A.
Proof.
intros n i0 i1 x0 x1 A H; extF i; unfold replace2F.
destruct (ord_eq_dec i i1) as [-> | Hi].
rewrite → 2!replaceF_correct_l; easy.
rewrite <- H, 3!replaceF_correct_r; easy.
Qed.

Lemma replace2F_equiv_def :
  ∀ {n i0 i1} x0 x1 (A : 'E^n),
    i1 ≠ i0 → replace2F i0 i1 x0 x1 A = replaceF i0 x0 (replaceF i1 x1 A).
Proof.
intros n i0 i1 x0 x1 A H; extF; unfold replace2F, replaceF.
destruct (ord_eq_dec _ _) as [Hi0 | Hi0]; try easy.
destruct (ord_eq_dec _ _) as [Hi1 | Hi1]; try easy.
rewrite -Hi0 -Hi1 in H; easy.
Qed.

Lemma permutF_correct : ∀ {n} p (A : 'E^n) i, permutF p A i = A (p i).
Proof. easy. Qed.

Lemma revF_correct : ∀ {n} (A : 'E^n) i, revF A i = A (rev_ord i).
Proof. easy. Qed.

Lemma moveF_correct_l :
  ∀ {n i0 i1 i} (A : 'E^n.+1), i = i1 → moveF i0 i1 A i = A i0.
Proof. intros; unfold moveF, permutF; rewrite move_ord_correct_l; easy. Qed.

Lemma moveF_correct_r :
  ∀ {n i0 i1 i} (H : i ≠ i1) (A : 'E^n.+1),
    moveF i0 i1 A i = A (skip_ord i0 (insert_ord H)).
Proof. intros; unfold moveF, permutF; rewrite move_ord_correct_r; easy. Qed.

Lemma transpF_correct_l0 :
  ∀ {n i0 i1 i} (A : 'E^n), i = i0 → transpF i0 i1 A i = A i1.
Proof.
intros; unfold transpF, permutF; rewrite transp_ord_correct_l0; easy.
Qed.

Lemma transpF_correct_l1 :
  ∀ {n i0 i1 i} (A : 'E^n), i = i1 → transpF i0 i1 A i = A i0.
Proof.
intros; unfold transpF, permutF; rewrite transp_ord_correct_l1; easy.
Qed.

Lemma transpF_correct_r :
  ∀ {n i0 i1 i} (A : 'E^n),
    i ≠ i0 → i ≠ i1 → transpF i0 i1 A i = A i.
Proof.
intros; unfold transpF, permutF; rewrite transp_ord_correct_r; easy.
Qed.

Context {F G : Type}.

Lemma mapF_correct :
  ∀ {n} (f : E → F) (A : 'E^n), mapF f A = fun i ⇒ f (A i).
Proof. easy. Qed.

Lemma map2F_correct :
  ∀ {n} (f : E → F → G) (A : 'E^n) B,
    map2F f A B = fun i ⇒ f (A i) (B i).
Proof. easy. Qed.

Lemma pullbackF_correct :
  ∀ {n} (g : E → F) (f : '(F → G)^n), pullbackF g f = fun i ⇒ f i \o g.
Proof. easy. Qed.

Lemma compF_r_correct :
  ∀ {n} (f : F → G) (g : '(E → F)^n), compF_r f g = fun i ⇒ f \o g i.
Proof. easy. Qed.

Lemma compF_correct :
  ∀ {n} (f : '(F → G)^n) (g : '(E → F)^n),
    compF f g = fun i ⇒ f i \o g i.
Proof. easy. Qed.

Lemma filter_eqF_correct : ∀ {n} x0 (A : 'E^n) i, filter_eqF x0 A i = x0.
Proof. intros n x0 A i; apply (filterP_ord_correct (fun i ⇒ A i = x0)). Qed.

Lemma filter_neqF_correct :
  ∀ {n} x0 (A : 'E^n) i, filter_neqF x0 A i ≠ x0.
Proof. intros n x0 A i; apply (filterP_ord_correct (fun i ⇒ A i ≠ x0)). Qed.

End FF_ops_Facts0.

Section FF_0_Facts.

Lemma hat0F_nonempty : ∀ (E : Type) {n}, n = 0 → inhabited 'E^n.
Proof. intros; subst; apply fun_from_empty_nonempty, I_0_is_empty. Qed.

Lemma hatSF_nonempty_equiv :
  ∀ (E : Type) n, inhabited 'E^n.+1 ↔ inhabited E.
Proof.
intros; split.
rewrite contra_equiv; apply fun_to_empty_is_empty, I_S_nonempty.
apply fun_to_nonempty_compat.
Qed.

Context {E : Type}.

Lemma hat0F_unit : ∀ {n} (A : 'E^n), n = 0 → unit_type A.
Proof. intros; subst; apply fun_from_empty_unit, I_0_is_empty. Qed.

Lemma hat0F_is_unit : ∀ {n}, n = 0 → is_unit_type 'E^n.
Proof. intros; subst; apply fun_from_empty_is_unit, I_0_is_empty. Qed.

Lemma choiceF :
  ∀ {n} (R : [0..n) → E → Prop),
    (∀ i, ∃ x, R i x) → ∃ (A : 'E^n), ∀ i, R i (A i).
Proof.
intros n; induction n as [| n Hn]; intros R HR.
+ ∃ (fun_from_I_0 E); intros [i Hi]; easy.
+ destruct (HR ord0) as [x0 Hx0].
  pose (R1 := fun (i1 : [0..n)) x ⇒ R (lift_S i1) x).
  assert (HR1 : ∀ (i : [0..n]) (Hi : i ≠ ord0) x,
      R i x = R1 (lower_S Hi) x).
    intros i Hi x; unfold R1; rewrite lift_lower_S; easy.
  destruct (Hn R1) as [A1 HA1].
    intros i1; destruct (HR (lift_S i1)) as [x1 Hx1]; ∃ x1; easy.
  assert (HA1' : ∀ i j : [0..n), i = j → R1 i (A1 j))
      by now intros; subst.
  ∃ (concatF (singleF x0) A1); intros i.
  destruct (lt_dec i 1) as [Hi | Hi].
  × assert (Hi' : i = ord0) by now apply ord0_lt_equiv.
    rewrite Hi' concatF_correct_l singleF_0; easy.
  × assert (Hi' : i ≠ ord0) by now apply ord_n0_nlt_equiv.
    rewrite concatF_correct_r HR1; apply HA1'.
    apply ord_inj; easy.
Qed.

End FF_0_Facts.

Section FF_constr_Facts.

Context {E : Type}.

Properties of constructors constF/singleF/coupleF.

Lemma unit_typeF :
  ∀ {n} (A : 'E^n) (O : E), is_unit_type E → A = constF n O.
Proof. intros n A O [O' HE]; extF i; rewrite (HE (A i)) (HE O); easy. Qed.

Lemma constF_eq : ∀ {n} (x y : E), x = y → constF n x = constF n y.
Proof. move=>>; apply f_equal. Qed.

Lemma singleF_eq : ∀ (x0 y0 : E), x0 = y0 → singleF x0 = singleF y0.
Proof. intros; f_equal; easy. Qed.

Lemma coupleF_eq :
  ∀ (x0 x1 y0 y1 : E),
    x0 = y0 → x1 = y1 → coupleF x0 x1 = coupleF y0 y1.
Proof. intros; f_equal; easy. Qed.

Lemma tripleF_eq :
  ∀ (x0 x1 x2 y0 y1 y2 : E),
    x0 = y0 → x1 = y1 → x2 = y2 → tripleF x0 x1 x2 = tripleF y0 y1 y2.
Proof. intros; f_equal; easy. Qed.

Lemma constF_inj : ∀ n, injective (@constF E n.+1).
Proof.
move⇒ n x y /extF_rev H.
rewrite -(constF_correct n.+1 x ord0); rewrite H; apply constF_correct.
Qed.

Lemma constF1_surj : surjective (@constF E 1).
Proof.
intros y; ∃ (y ord0); extF; rewrite ord_one constF_correct; easy.
Qed.

Lemma singleF_inj : ∀ (x0 y0 : E), singleF x0 = singleF y0 → x0 = y0.
Proof. intros; apply (constF_inj 0); easy. Qed.

Lemma coupleF_inj_l :
  ∀ (x0 x1 y0 y1 : E), coupleF x0 x1 = coupleF y0 y1 → x0 = y0.
Proof.
intros x0 x1 y0 y1 H; rewrite -(coupleF_0 x0 x1) -(coupleF_0 y0 y1) H; easy.
Qed.

Lemma coupleF_inj_r :
  ∀ (x0 x1 y0 y1 : E), coupleF x0 x1 = coupleF y0 y1 → x1 = y1.
Proof.
intros x0 x1 y0 y1 H; rewrite -(coupleF_1 x0 x1) -(coupleF_1 y0 y1) H; easy.
Qed.

Lemma coupleF_inj :
  ∀ (x0 x1 y0 y1 : E),
    coupleF x0 x1 = coupleF y0 y1 → x0 = y0 ∧ x1 = y1.
Proof.
move=>> H; split; [eapply coupleF_inj_l | eapply coupleF_inj_r]; apply H.
Qed.

Lemma tripleF_inj_l :
  ∀ (x0 x1 x2 y0 y1 y2 : E),
    tripleF x0 x1 x2 = tripleF y0 y1 y2 → x0 = y0.
Proof.
intros x0 x1 x2 y0 y1 y2 H;
    rewrite -(tripleF_0 x0 x1 x2) -(tripleF_0 y0 y1 y2) H; easy.
Qed.

Lemma tripleF_inj_m :
  ∀ (x0 x1 x2 y0 y1 y2 : E),
    tripleF x0 x1 x2 = tripleF y0 y1 y2 → x1 = y1.
Proof.
intros x0 x1 x2 y0 y1 y2 H;
    rewrite -(tripleF_1 x0 x1 x2) -(tripleF_1 y0 y1 y2) H; easy.
Qed.

Lemma tripleF_inj_r :
  ∀ (x0 x1 x2 y0 y1 y2 : E),
    tripleF x0 x1 x2 = tripleF y0 y1 y2 → x2 = y2.
Proof.
intros x0 x1 x2 y0 y1 y2 H;
    rewrite -(tripleF_2 x0 x1 x2) -(tripleF_2 y0 y1 y2) H; easy.
Qed.

Lemma tripleF_inj :
  ∀ (x0 x1 x2 y0 y1 y2 : E),
    tripleF x0 x1 x2 = tripleF y0 y1 y2 → x0 = y0 ∧ x1 = y1 ∧ x2 = y2.
Proof.
move=>> H; repeat split;
    [eapply tripleF_inj_l | eapply tripleF_inj_m | eapply tripleF_inj_r];
    apply H.
Qed.

Lemma coupleF_diag : ∀ (x : E), coupleF x x = constF 2 x.
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->];
    [rewrite coupleF_0 | rewrite coupleF_1]; easy.
Qed.

Lemma tripleF_diag : ∀ (x : E), tripleF x x x = constF 3 x.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->];
    [rewrite tripleF_0 | rewrite tripleF_1 | rewrite tripleF_2]; easy.
Qed.

End FF_constr_Facts.

Section FF_pred_Facts.

Context {E : Type}.

Properties of predicate inF.

Lemma inF_refl : ∀ {n} (A : 'E^n) i , inF (A i) A.
Proof. intros n A i; ∃ i; easy. Qed.

Lemma inF_monot :
  ∀ {n1 n2} x (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A1 A2 → inF x A1 → inF x A2.
Proof.
move=>> HA [i1 Hi1]; destruct (HA i1) as [i2 Hi2];
    rewrite Hi1 Hi2; ∃ i2; easy.
Qed.

Lemma inF_not :
  ∀ {n} x (A : 'E^n), ¬ inF x A ↔ ∀ i, x ≠ A i.
Proof. intros; apply not_ex_all_not_equiv. Qed.

Lemma inF_constF : ∀ {n} (x : E), inF x (constF n.+1 x).
Proof. intros; ∃ ord0; easy. Qed.

Lemma inF_singleF : ∀ (x0 : E), inF x0 (singleF x0).
Proof. intros; apply inF_constF. Qed.

Lemma inF_coupleF_0 : ∀ (x0 x1 : E), inF x0 (coupleF x0 x1).
Proof. intros; ∃ ord0; rewrite coupleF_0; easy. Qed.

Lemma inF_coupleF_1 : ∀ (x0 x1 : E), inF x1 (coupleF x0 x1).
Proof. intros; ∃ ord_max; rewrite coupleF_1; easy. Qed.

Lemma inF_tripleF_0 : ∀ (x0 x1 x2 : E), inF x0 (tripleF x0 x1 x2).
Proof. intros; ∃ ord0; rewrite tripleF_0; easy. Qed.

Lemma inF_tripleF_1 : ∀ (x0 x1 x2 : E), inF x1 (tripleF x0 x1 x2).
Proof. intros; ∃ ord1; rewrite tripleF_1; easy. Qed.

Lemma inF_tripleF_2 : ∀ (x0 x1 x2 : E), inF x2 (tripleF x0 x1 x2).
Proof. intros; ∃ ord_max; rewrite tripleF_2; easy. Qed.

Lemma inFF_1 :
  ∀ {E : Type } {m n} {x : E} {AA : '('E^m)^n} i,
    m = 1%nat → inF x (AA^~ i) → inF (constF m x) AA.
Proof. move=>> Hm [j Hj]; subst; ∃ j; extF; rewrite !ord_one//. Qed.

Properties of predicate inclF.

Lemma inclF_fullset : ∀ {n} (A : 'E^n), inclF A fullset.
Proof. easy. Qed.

Lemma inclF_nil : ∀ (PE : E → Prop) (A : 'E^0), inclF A PE.
Proof. intros PE A [i Hi]; easy. Qed.

Lemma inclF_constF :
  ∀ (PE : E → Prop) n x, PE x → inclF (constF n x) PE.
Proof. intros PE n x Hx i; auto. Qed.

Lemma inclF_singleton_equiv :
  ∀ {n} x (A : 'E^n), inclF A (singleton x) ↔ A = constF n x.
Proof.
intros; split; intros HA; [extF; rewrite HA | subst; apply inclF_constF]; easy.
Qed.

Lemma inclF_singleF :
  ∀ (PE : E → Prop) x0, PE x0 → inclF (singleF x0) PE.
Proof. intros; apply inclF_constF; easy. Qed.

Lemma inclF_coupleF :
  ∀ (PE : E → Prop) x0 x1, PE x0 → PE x1 → inclF (coupleF x0 x1) PE.
Proof. intros; intro; unfold coupleF; destruct (ord2_dec _); easy. Qed.

Lemma inclF_tripleF :
  ∀ (PE : E → Prop) x0 x1 x2,
    PE x0 → PE x1 → PE x2 → inclF (tripleF x0 x1 x2) PE.
Proof.
intros; intro; unfold tripleF; destruct (ord3_dec _) as [[K | K] | K]; easy.
Qed.

Lemma inclF_trans :
  ∀ {n} (A : 'E^n) x PE, inF x A → inclF A PE → PE x.
Proof. move=>> [i Hi]; rewrite Hi; easy. Qed.

Lemma inclF_monot_l :
  ∀ {n1 n2} PE (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A2 A1 → inclF A1 PE → inclF A2 PE.
Proof. move=>> H H1 i2; destruct (H i2) as [i1 Hi1]; rewrite Hi1; easy. Qed.

Lemma inclF_monot_r :
  ∀ {n} PE1 PE2 (A : 'E^n), incl PE1 PE2 → inclF A PE1 → inclF A PE2.
Proof. move=>> H H1 i; auto. Qed.

Lemma inclF_image :
  ∀ {F : Type} {f : E → F} {PE : E → Prop} {n} {B : 'F^n},
    (∃ A, inclF A PE ∧ B = mapF f A) → inclF B (image f PE).
Proof. move=>> [A [HA1 /extF_rev HA2]] i; rewrite HA2; easy. Qed.

Lemma inclF_image_rev :
  ∀ {F : Type} {f : E → F} {PE : E → Prop} {n} {B : 'F^n},
    inclF B (image f PE) → ∃ A, inclF A PE ∧ B = mapF f A.
Proof.
move=>>; rewrite image_ex; intros HB; destruct (choiceF _ HB) as [A HA].
∃ A; split; [intro | apply eq_sym; extF]; apply HA.
Qed.

Lemma inclF_image_equiv :
  ∀ {F : Type} {f : E → F} {PE : E → Prop} {n} {B : 'F^n},
    inclF B (image f PE) ↔ ∃ A, inclF A PE ∧ B = mapF f A.
Proof. intros; split; [apply inclF_image_rev | apply inclF_image]. Qed.

Properties of predicate invalF.

Lemma invalF_refl : ∀ {n} (A : 'E^n), invalF A A.
Proof. move=>>; eexists; easy. Qed.

Lemma invalF_singleF_refl : ∀ {n} (A : 'E^n) i, invalF (singleF (A i)) A.
Proof. move=>>; rewrite singleF_0; eexists; easy. Qed.

Lemma invalF_trans :
  ∀ {n1 n2 n3} (A2 : 'E^n2) (A1 : 'E^n1) (A3 : 'E^n3),
    invalF A1 A2 → invalF A2 A3 → invalF A1 A3.
Proof.
move=>> H12 H23 i1; destruct (H12 i1) as [i2 Hi2], (H23 i2) as [i3 Hi3].
∃ i3; rewrite Hi2; easy.
Qed.

Lemma invalF_fun :
  ∀ {n1 n2} {A1 : 'E^n1} {A2 : 'E^n2},
    invalF A1 A2 → ∃ f, ∀ i1, A1 i1 = A2 (f i1).
Proof. move=>> HA; apply (choiceF _ HA). Qed.

Lemma invalF_fun_inj :
  ∀ {n1 n2} {A1 : 'E^n1} {A2 : 'E^n2} (f : [0..n1) → [0..n2)),
    injective A1 → invalF A1 A2 →
    (∀ i1, A1 i1 = A2 (f i1)) → injective f.
Proof.
move=>> HA1 HA Hf i1 j1 H1; apply HA1; do 2 rewrite Hf; f_equal; easy.
Qed.

Lemma invalF_le :
  ∀ {n1 n2} {A1 : 'E^n1} {A2 : 'E^n2},
    injective A1 → invalF A1 A2 → n1 ≤ n2.
Proof.
move=>> HA1 HA; destruct (invalF_fun HA) as [f Hf].
apply (inj_leq f), (invalF_fun_inj _ HA1 HA Hf).
Qed.

Lemma injF_monot :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2),
    n1 = n2 → invalF A1 A2 → injective A1 → injective A2.
Proof.
intros n1' n A1 A2 Hn HA HA1; subst; destruct (invalF_fun HA) as [f Hf].
assert (Hf' : bijective f) by apply injF_bij, (invalF_fun_inj f HA1 HA Hf).
destruct Hf' as [g Hg1 Hg2].
assert (Hg' : injective g) by apply bij_inj, (Bijective Hg2 Hg1).
assert (Hg : ∀ i2, A2 i2 = A1 (g i2)) by (now intros; rewrite Hf Hg2).
move=>> H2; rewrite 2!Hg in H2; apply HA1 in H2.
apply Hg', H2.
Qed.

Lemma invalF_sym :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2),
    n1 = n2 → injective A1 → invalF A1 A2 → invalF A2 A1.
Proof.
move=>> Hn HA1 HA; subst; destruct (invalF_fun HA) as [f Hf].
assert (Hf' : bijective f) by apply injF_bij, (invalF_fun_inj f HA1 HA Hf).
destruct Hf' as [g _ Hg]; intros i; ∃ (g i); rewrite Hf Hg; easy.
Qed.

Lemma injF_equiv :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2),
    n1 = n2 → invalF A1 A2 → invalF A2 A1 → injective A1 ↔ injective A2.
Proof. intros; split; apply injF_monot; easy. Qed.

Lemma invalF_coupleF_sym :
  ∀ (x0 x1 : E), invalF (coupleF x0 x1) (coupleF x1 x0).
Proof.
intros; intro; unfold inF, coupleF at 1; destruct (ord2_dec _).
∃ ord_max; rewrite coupleF_1; easy.
∃ ord0; rewrite coupleF_0; easy.
Qed.

Lemma invalF_P :
  ∀ {P : E → Prop} {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A1 A2 → (∀ i2, P (A2 i2)) → ∀ i1, P (A1 i1).
Proof. move=>> /invalF_fun [f Hf] H2 i1; rewrite Hf; easy. Qed.

Properties of predicates iffAF.

Lemma iffAF_refl : ∀ {n} (P : 'Prop^n), iffAF P P.
Proof. easy. Qed.

Lemma iffAF_sym : ∀ {n} {P Q : 'Prop^n}, iffAF P Q → iffAF Q P.
Proof. easy. Qed.

Lemma iffAF_trans :
  ∀ {n} (Q P R : 'Prop^n), iffAF P Q → iffAF Q R → iffAF P R.
Proof. move=>> HPQ HQR i; rewrite HPQ; auto. Qed.

Lemma iffAF_eq_sym :
  ∀ {n1 n2} (H : n1 = n2) {P1 : 'Prop^n1} {P2 : 'Prop^n2},
    iffAF P1 (castF (eq_sym H) P2) → ∀ i1, P1 i1 ↔ P2 (cast_ord H i1).
Proof. move=>>; rewrite castF_eq_sym; easy. Qed.

Properties of predicates eqPF/neqPF.

Lemma eqPF_refl : ∀ {n} (P : 'Prop^n) (A : 'E^n), eqPF P A A.
Proof. easy. Qed.

Lemma eqPF_sym :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n), eqPF P A B → eqPF P B A.
Proof. move=>> H i Hi; symmetry; auto. Qed.

Lemma eqPF_trans :
  ∀ {n} (P : 'Prop^n) (B A C : 'E^n),
    eqPF P A B → eqPF P B C → eqPF P A C.
Proof. move=>> H1 H2 i Hi; rewrite H1; auto. Qed.

Lemma eqPF_compat : ∀ {n} (P : 'Prop^n) (A B : 'E^n), A = B → eqPF P A B.
Proof. move=>> H; rewrite H; easy. Qed.

Lemma eqPF_reg :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n),
    (∀ i, ¬ P i → A i = B i) → eqPF P A B → A = B.
Proof.
intros n P A B H0 H1; extF i;
    destruct (classic (P i)) as [Hi |]; try rewrite Hi; auto.
Qed.

Lemma eqPF_not_equiv :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n), eqPF P A B ↔ ¬ neqPF P A B.
Proof.
intros; split.
intros H; apply all_not_not_ex; intros; rewrite -imp_and_equiv; apply H.
move⇒ /not_ex_all_not H i; apply imp_and_equiv, H.
Qed.

Lemma neqPF_not_equiv :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n), neqPF P A B ↔ ¬ eqPF P A B.
Proof. intros; rewrite iff_not_r_equiv eqPF_not_equiv; easy. Qed.

Lemma neqPF_compat :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n),
    A ≠ B → (∃ i, ¬ P i ∧ A i ≠ B i) ∨ neqPF P A B.
Proof.
intros n P A B; rewrite contra_not_l_equiv;
    move⇒ /not_or_equiv [/not_ex_all_not_equiv H1 /eqPF_not_equiv H2].
apply (eqPF_reg P); [intros i; specialize (H1 i); tauto | easy].
Qed.

Lemma neqPF_reg : ∀ {n} (P : 'Prop^n) (A B : 'E^n), neqPF P A B → A ≠ B.
Proof. move=>>; rewrite neqPF_not_equiv -contra_equiv; apply eqPF_compat. Qed.

Properties of predicates eqxF/neqxF/eqx2F/neqx2F.

Lemma eqxF_refl : ∀ {n} i0 (A : 'E^n), eqxF i0 A A.
Proof. easy. Qed.

Lemma eqxF_sym : ∀ {n} i0 (A B : 'E^n), eqxF i0 A B → eqxF i0 B A.
Proof. move=>>; apply eqPF_sym. Qed.

Lemma eqxF_trans :
  ∀ {n} i0 (B A C : 'E^n), eqxF i0 A B → eqxF i0 B C → eqxF i0 A C.
Proof. move=>>; apply eqPF_trans. Qed.

Lemma eqxF_compat : ∀ {n} i0 {A B : 'E^n}, A = B → eqxF i0 A B.
Proof. move=>>; apply eqPF_compat. Qed.

Lemma eqxF_reg :
  ∀ {n} i0 {A} (B : 'E^n), A i0 = B i0 → eqxF i0 A B → A = B.
Proof. move=>> H; apply eqPF_reg; move=>> /NNPP ->; easy. Qed.

Lemma eqxF_not_equiv :
  ∀ {n} i0 {A B : 'E^n}, eqxF i0 A B ↔ ¬ neqxF i0 A B.
Proof. move=>>; apply eqPF_not_equiv. Qed.

Lemma neqxF_not_equiv :
  ∀ {n} i0 {A B : 'E^n}, neqxF i0 A B ↔ ¬ eqxF i0 A B.
Proof. move=>>; apply neqPF_not_equiv. Qed.

Lemma neqxF_compat :
  ∀ {n} i0 {A B : 'E^n}, A ≠ B → A i0 ≠ B i0 ∨ neqxF i0 A B.
Proof.
move⇒ n i0 A B /(neqPF_compat (fun i ⇒ i ≠ i0)) [[i [/NNPP → Hi2]] | H];
    [left | right]; easy.
Qed.

Lemma neqxF_reg : ∀ {n} i0 {A B : 'E^n}, neqxF i0 A B → A ≠ B.
Proof. move=>>; apply neqPF_reg. Qed.

Lemma eqx2F_refl : ∀ {n} i0 i1 (A : 'E^n), eqx2F i0 i1 A A.
Proof. easy. Qed.

Lemma eqx2F_sym :
  ∀ {n} i0 i1 (A B : 'E^n), eqx2F i0 i1 A B → eqx2F i0 i1 B A.
Proof. move=>>; apply eqPF_sym. Qed.

Lemma eqx2F_trans :
  ∀ {n} i0 i1 (B A C : 'E^n),
    eqx2F i0 i1 A B → eqx2F i0 i1 B C → eqx2F i0 i1 A C.
Proof. move=>>; apply eqPF_trans. Qed.

Lemma eqx2F_compat : ∀ {n} i0 i1 {A B : 'E^n}, A = B → eqx2F i0 i1 A B.
Proof. move=>>; apply eqPF_compat. Qed.

Lemma eqx2F_reg :
  ∀ {n} i0 i1 {A} (B : 'E^n),
    A i0 = B i0 → A i1 = B i1 → eqx2F i0 i1 A B → A = B.
Proof.
move=>> H0 H1; apply eqPF_reg; intro; move⇒ /not_or_equiv /NNPP [-> | ->]//.
Qed.

Lemma eqx2F_not_equiv :
  ∀ {n} i0 i1 {A B : 'E^n}, eqx2F i0 i1 A B ↔ ¬ neqx2F i0 i1 A B.
Proof. move=>>; apply eqPF_not_equiv. Qed.

Lemma neqx2F_not_equiv :
  ∀ {n} i0 i1 {A B : 'E^n}, neqx2F i0 i1 A B ↔ ¬ eqx2F i0 i1 A B.
Proof. move=>>; apply neqPF_not_equiv. Qed.

Lemma eqx2F_sym_i :
  ∀ {n} i0 i1 {A B : 'E^n}, eqx2F i0 i1 A B → eqx2F i1 i0 A B.
Proof. move=>> H i Hi; apply H; easy. Qed.

Lemma neqx2F_sym_i :
  ∀ {n} i0 i1 {A B : 'E^n}, neqx2F i0 i1 A B → neqx2F i1 i0 A B.
Proof. move=>> [i Hi]; ∃ i; easy. Qed.

End FF_pred_Facts.

Section FF_sub_Facts.

Context {E : Type}.
Context {PE : E → Prop}.

Lemma in_subF : ∀ {n} (A_sub : '(sub PE)^n), inclF (mapF val A_sub) PE.
Proof. move=>>; apply in_sub. Qed.

Lemma mk_subF_eq :
  ∀ {n} (A_sub : '(sub PE)^n), A_sub = fun i ⇒ mk_sub (in_subF A_sub i).
Proof. intros; extF; apply val_inj; easy. Qed.

Lemma valF_eq :
  ∀ {n} (A : 'E^n) (HA : inclF A PE),
    A = mapF val (fun i ⇒ mk_sub_ PE _ (HA i)).
Proof. easy. Qed.

End FF_sub_Facts.

Section CastF_Facts.

Context {E : Type}.

Properties of operator castF/castF_fun/cast2F_fun.

Lemma castF_refl : ∀ {n} {H : n = n} (A : 'E^n), castF H A = A.
Proof. intros; unfold castF; extF; f_equal; apply ord_inj; easy. Qed.

Lemma castF_id : ∀ {n} {H : n = n}, castF H = @id 'E^n.
Proof. intros; fun_ext; apply castF_refl. Qed.

Lemma castF_fun_id :
  ∀ {T : Type} {n} {H : n = n}, castF_fun H = @id ('E^n → T).
Proof. intros; unfold castF_fun; rewrite castF_id; easy. Qed.

Lemma cast2F_fun_id :
  ∀ {T1 T2 : Type} {n} {H : n = n},
    cast2F_fun H = @id (('E^n → T1) → T2).
Proof. intros; unfold cast2F_fun; rewrite castF_fun_id; easy. Qed.

Lemma eqAF_nil :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1) (A2 : 'E^n2),
    n1 = 0 ∨ n2 = 0 → A2 = castF H A1.
Proof. move=>> [H | H]; subst; rewrite castF_id; apply hat0F_unit; easy. Qed.

Lemma eqAF_1 :
  ∀ {n1 n2} (H : n1 = n2) {A1 : 'E^n1} {A2 : 'E^n2} i2,
    n1 = 1 ∨ n2 = 1 → A2 i2 = castF H A1 i2 → A2 = castF H A1.
Proof.
move=>> [H1 | H1] H; subst; extF; move: H; rewrite castF_id !ord_one; easy.
Qed.

Lemma castF_trans :
  ∀ {n1 n2 n3}
      (H12 : n1 = n2) (H23 : n2 = n3) (H13 : n1 = n3) (A1 : 'E^n1),
    castF H23 (castF H12 A1) = castF H13 A1.
Proof. intros; unfold castF; extF; f_equal; apply ord_inj; easy. Qed.

Lemma castF_comp :
  ∀ {n1 n2 n3} (H12 : n1 = n2) (H23 : n2 = n3) (A1 : 'E^n1),
    castF H23 (castF H12 A1) = castF (eq_trans H12 H23) A1.
Proof. intros; apply (castF_trans _ _ (eq_trans _ _)). Qed.

Lemma castF_can :
  ∀ {n1 n2} (H12 : n1 = n2) (H21 : n2 = n1),
    cancel (@castF E _ _ H12) (castF H21).
Proof. move=>>; rewrite castF_trans castF_id; easy. Qed.

Lemma castF_eq_l :
  ∀ {n1 n2} (H H' : n1 = n2) (A1 : 'E^n1), castF H A1 = castF H' A1.
Proof. intros; f_equal; easy. Qed.

Lemma castF_eq_r :
  ∀ {n1 n2} (H H' : n1 = n2) (A1 A'1 : 'E^n1),
    A1 = A'1 → castF H A1 = castF H' A'1.
Proof. intros; f_equal; easy. Qed.

Lemma castF_eq_r_alt :
  ∀ {n1 n2 m} (H1 : n1 = m) (H2 : n2 = m) (A1 : 'E^n1) (A2 : 'E^n2),
    (∀ i1 i2, nat_of_ord i1 = nat_of_ord i2 → A1 i1 = A2 i2) →
    castF H1 A1 = castF H2 A2.
Proof. move=>> H; extF; apply H; easy. Qed.

Lemma castF_eq :
  ∀ {n1 n2} (H : n1 = n2) {A1 : 'E^n1} i1 i2,
    nat_of_ord i1 = nat_of_ord i2 → castF H A1 i2 = A1 i1.
Proof. intros; unfold castF; f_equal; apply ord_inj; easy. Qed.

Lemma castF_inj :
  ∀ {n1 n2} (H : n1 = n2) (A1 B1 : 'E^n1),
    castF H A1 = castF H B1 → A1 = B1.
Proof.
intros n1 n2 H A1 B1; rewrite -{2}(castF_can H (eq_sym H) A1)
    -{2}(castF_can H (eq_sym H) B1).
apply castF_eq_r.
Qed.

Lemma castF_can_l :
  ∀ {n1 n2} (H : n1 = n2), cancel (castF (eq_sym H)) (@castF E _ _ H).
Proof. intros; apply castF_can. Qed.

Lemma castF_can_r :
  ∀ {n1 n2} (H : n1 = n2), cancel (@castF E _ _ H) (castF (eq_sym H)).
Proof. intros; apply castF_can. Qed.

Lemma castF_cast_ord :
  ∀ {n1 n2} (H H' : n1 = n2) (A1 : 'E^n1) i1,
    castF H A1 (cast_ord H' i1) = A1 i1.
Proof. intros; unfold castF; rewrite cast_ord_comp cast_ord_id; easy. Qed.

Lemma castF_surjF :
  ∀ {n1 n2} (H : n1 = n2) (A2 : 'E^n2),
    ∃ (A1 : 'E^n1), A2 = castF H A1.
Proof.
intros n1 n2 H A2; ∃ (fun i1 ⇒ A2 (cast_ord H i1)).
extF; unfold castF; f_equal; rewrite cast_ordKV; easy.
Qed.

Lemma sortedF_castF :
  ∀ {leE : E → E → Prop} {n1 n2} (H : n1 = n2) {A : 'E^n1},
    sortedF leE A → sortedF leE (castF H A).
Proof. move=>> HA i2 j2 H2; apply HA; easy. Qed.

Lemma sortedF_castF_rev :
  ∀ {leE : E → E → Prop} {n1 n2} (H : n1 = n2) {A : 'E^n1},
    sortedF leE (castF H A) → sortedF leE A.
Proof.
intros leE n1 n2 H A HA i1 j1 H1;
    rewrite -(cast_ordK H i1) -(cast_ordK H j1); apply HA; easy.
Qed.

Lemma sortedF_castF_equiv :
  ∀ {leE : E → E → Prop} {n1 n2} (H : n1 = n2) {A : 'E^n1},
    sortedF leE A ↔ sortedF leE (castF H A).
Proof. intros; split; [apply sortedF_castF | apply sortedF_castF_rev]. Qed.

Lemma invalF_castF_l_equiv :
  ∀ {m1 m2 n} (Hm : m1 = m2) (Am : 'E^m1) (An : 'E^n),
    invalF (castF Hm Am) An ↔ invalF Am An.
Proof.
intros m1 m2 n Hm Am An; split; intros HA i.
destruct (HA (cast_ord Hm i)) as [j Hj];
    rewrite castF_cast_ord in Hj; ∃ j; easy.
destruct (HA (cast_ord (eq_sym Hm) i)) as [j Hj]; ∃ j; easy.
Qed.

Lemma invalF_castF_r_equiv :
  ∀ {m n1 n2} (Hn : n1 = n2) (Am : 'E^m) (An : 'E^n1),
    invalF Am (castF Hn An) ↔ invalF Am An.
Proof.
intros m n1 n2 Hn Am An; split; intros HA i; destruct (HA i) as [j Hj].
∃ (cast_ord (eq_sym Hn) j); easy.
∃ (cast_ord Hn j); rewrite castF_cast_ord; easy.
Qed.

Lemma invalF_castF_equiv :
  ∀ {m1 m2 n1 n2} (Hm : m1 = m2) (Hn : n1 = n2) (Am : 'E^m1) (An : 'E^n1),
    invalF (castF Hm Am) (castF Hn An) ↔ invalF Am An.
Proof. intros; rewrite invalF_castF_l_equiv invalF_castF_r_equiv; easy. Qed.

Lemma invalF_castF_l :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1), invalF (castF H A1) A1.
Proof. intros n1 n2 H A1 i2; ∃ (cast_ord (eq_sym H) i2); easy. Qed.

Lemma invalF_castF_r :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1), invalF A1 (castF H A1).
Proof.
intros n1 n2 H A1 i1; ∃ (cast_ord H i1).
rewrite castF_cast_ord; easy.
Qed.

Lemma castF_sym_equiv_alt :
  ∀ {n1 n2} i1 i2 (H : n1 = n2) (A1 : 'E^n1) (A2 : 'E^n2),
    i1 = cast_ord (eq_sym H) i2 →
    castF H A1 i2 = A2 i2 ↔ castF (eq_sym H) A2 i1 = A1 i1.
Proof. move=>> H; rewrite H castF_cast_ord; easy. Qed.

Lemma castF_sym_equiv :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1) (A2 : 'E^n2),
    castF H A1 = A2 ↔ castF (eq_sym H) A2 = A1.
Proof.
intros n1 n2 H A1 A2; rewrite -{2}(castF_can H (eq_sym H) A1); split.
intros; subst; easy.
move⇒ /castF_inj; easy.
Qed.

Lemma castF_constF :
  ∀ {n1 n2} (H : n1 = n2) (x : E), castF H (constF n1 x) = constF n2 x.
Proof. intros; extF; unfold castF; rewrite 2!constF_correct; easy. Qed.

Lemma castF_1 :
  ∀ {n} (H : n = 1) (A : 'E^1) i, castF (eq_sym H) A i = A ord0.
Proof.
intros; subst; rewrite ord_one; unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma castF_p1S_Sp1 :
  ∀ {n} (A : 'E^n.+1), castF_p1S (castF_Sp1 A) = A.
Proof. intros; unfold castF_p1S, castF_Sp1; apply castF_can. Qed.

Lemma castF_Sp1_p1S :
  ∀ {n} (A : 'E^(n + 1)), castF_Sp1 (castF_p1S A) = A.
Proof. intros; unfold castF_p1S, castF_Sp1; apply castF_can. Qed.

Lemma castF_1pS_S1p : ∀ {n} (A : 'E^n.+1), castF_1pS (castF_S1p A) = A.
Proof. intros; unfold castF_1pS, castF_S1p; apply castF_can. Qed.

Lemma castF_S1p_1pS : ∀ {n} (A : 'E^(1 + n)), castF_S1p (castF_1pS A) = A.
Proof. intros; unfold castF_1pS, castF_S1p; apply castF_can. Qed.

End CastF_Facts.

Section Fun_ord.

More basic properties of functions on ordinals.

Lemma injF_extend_bij_EX :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f),
    { p2 | bijective p2 ∧ ∀ i1, f i1 = widenF (injF_leq Hf) p2 i1 }.
Proof.
intros n1 n2 f Hf; induction n1 as [| n1 Hn1].
∃ id; repeat split; [apply bij_id | now intros [i Hi]].
pose (f' := fun i1 ⇒ f (widen_S i1)).
assert (Hf' : injective f') by now move⇒ i1 j1 /Hf /widen_ord_inj.
destruct (Hn1 _ Hf') as [p2' [Hp2'a Hp2'b]]; clear Hn1.
pose (nn1 := widen_ord (injF_leq Hf) ord_max).
pose (p2 := fun i2 ⇒ transp_ord (p2' nn1) (f ord_max) (p2' i2)).
∃ p2; repeat split.
pose (q2 := fun j2 ⇒
              transp_ord nn1 (f_inv Hp2'a (f ord_max)) (f_inv Hp2'a j2)).
move: (bij_inj Hp2'a) ⇒ Hp2'c.
assert (Hq2a : cancel p2 q2).
  intros i2; unfold q2, p2.
  destruct (ord_eq2_dec (p2' i2) (p2' nn1) (f ord_max)) as [[H | H] | [Ha Hb]].
  rewrite (transp_ord_correct_l0 H) transp_ord_correct_l1//; apply Hp2'c; easy.
  rewrite (transp_ord_correct_l1 H) transp_ord_correct_l0.
  apply eq_sym, f_inv_eq_equiv; easy. apply f_inv_can_l.
  rewrite (transp_ord_correct_r Ha Hb) transp_ord_correct_r;
      rewrite f_inv_can_l; try easy.
  contradict Ha; subst; easy.
  contradict Hb; subst; rewrite f_inv_can_r; easy.
assert (Hq2b : cancel q2 p2).
  intros j2; unfold q2, p2.
  destruct (ord_eq2_dec j2 (p2' nn1) (f ord_max)) as [[H | H] | [Ha Hb]].
  rewrite H f_inv_can_l transp_ord_correct_l1// transp_ord_correct_l0//
      f_inv_can_r; easy.
  rewrite H transp_ord_correct_l0// transp_ord_correct_l1//.
  apply not_eq_sym, (f_inv_neq_equiv Hp2'a) in Ha;
      rewrite (transp_ord_correct_r (not_eq_sym Ha) _).
  apply f_inv_neq_equiv, not_eq_sym in Ha;
      rewrite f_inv_can_r transp_ord_correct_r//.
  contradict Hb; apply (f_inv_inj Hp2'a); easy.
apply (Bijective Hq2a Hq2b).
unfold p2, widenF in *; move: Hp2'a ⇒ /bij_equiv [Hp2'c _].
intros i1; destruct (ord_eq_dec i1 ord_max) as [Hi1 | Hi1].
rewrite Hi1 transp_ord_correct_l0// f_inv_can_r//.
rewrite -{1}(widen_narrow_S Hi1); fold (f' (narrow_S Hi1)); rewrite Hp2'b.
rewrite transp_ord_correct_r; try now f_equal; apply ord_inj.
contradict Hi1; apply Hp2'c, widen_ord_inj in Hi1; easy.
replace (widen_ord _ _) with (widen_ord (injF_leq Hf') (narrow_S Hi1));
    try now apply ord_inj.
rewrite -Hp2'b; unfold f'; rewrite widen_narrow_S.
contradict Hi1; apply Hf; easy.
Qed.

Lemma injF_restr_bij_EX :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f),
    { p1 : [0..n1) → [0..n1) | bijective p1 ∧ incrF (f \o p1) }.
Proof.
intros n1 n2 f Hf; destruct n1 as [| n1].
∃ (fun_from_I_0 ∅); split;
    [apply fun_from_I_0_bij | intros [i Hi]; easy].
destruct n2 as [| n2];
    [move: (injF_le Hf) ⇒ Hn1; contradict Hn1; apply Nat.nle_gt, S_pos |].
pose (lf := (map f (ord_enum n1.+1))).
assert (Hlf : ∀ i : [0..n1], nth ord0 lf i = f i)
    by now intros i; rewrite (nth_map ord0);
      [rewrite nth_ord_enum | rewrite size_ord_enum].
destruct (sort_perm_EX ord_leq_antisym ord_leq_trans ord_leq_total ord0 lf)
    as [p1 Hp1a Hp1b].
assert (Hn1 : size lf = n1.+1) by now rewrite size_map size_ord_enum.
rewrite Hn1 in p1, Hp1a, Hp1b.
pose (lf' := map (fun i1 ⇒ nth ord0 lf (p1 i1)) (ord_enum n1.+1)).
assert (Hlf'a : size lf' = n1.+1) by now rewrite size_map size_ord_enum.
assert (Hlf'b : lf' = map (f \o p1) (ord_enum n1.+1)).
  apply (@eq_from_nth _ ord0); [rewrite !size_map; easy |].
  intros i Hi; rewrite Hlf'a in Hi; rewrite !(nth_map ord0).
  rewrite nth_ord_enum; easy.
  1,2,3: rewrite size_ord_enum; easy.
∃ p1; split; [apply injF_bij; easy | apply incrF_equiv].
intros i1 Hj1; rewrite !comp_correct; pose (j1 := Ordinal Hj1); fold j1.
assert (H : i1 < j1) by easy.
apply /ltP; destruct (proj1 ltn_equiv H) as [H1 H2].
apply leq_neq_ltn;
    [| apply ord_neq_compat, (inj_contra Hf), (inj_contra Hp1a), ord_neq; easy].
assert (Hi1a : i1 < n1.+1) by now destruct i1.
assert (Hj1a : j1 < n1.+1) by now destruct j1.
assert (Hj1b : j1 < size lf') by now rewrite Hlf'a.
move: (sort_sorted ord_leq_total lf); rewrite Hp1b; simpl; fold lf'.
move⇒ /(sortedP ord0) ⇒ H3; specialize (H3 (nat_of_ord i1) Hj1b); move: H3.
replace i1.+1 with (nat_of_ord j1) by easy.
rewrite Hlf'b !(nth_map ord0); [| rewrite size_ord_enum; easy..].
rewrite !comp_correct !nth_ord_enum; easy.
Qed.

End Fun_ord.

Section FunF_Facts.

Context {E : Type}.

Properties of operators funF/unfunF/maskPF.

Lemma funF_equiv :
  ∀ {n1 n2} (f : [0..n1) → [0..n2)) (A1 : 'E^n1) (A2 : 'E^n2),
    funF f A2 = A1 ↔ (∀ i1 i2, i2 = f i1 → A2 i2 = A1 i1).
Proof.
intros; split; intros HA. intros; subst; easy.
extF i1; apply (HA i1 (f i1)); easy.
Qed.

Lemma funF_neqF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {A2 : 'E^n2} {x0},
    funF f (neqF A2 x0) = neqF (funF f A2) x0.
Proof. easy. Qed.

Lemma funF_comp :
  ∀ {n1 n2 n3} {f12 : [0..n1) → [0..n2)}
      {f23 : [0..n2) → [0..n3)} (A3 : 'E^n3),
    funF (f23 \o f12) A3 = funF f12 (funF f23 A3).
Proof. easy. Qed.

Lemma unfunF_nil :
  ∀ {n} (f : ∅ → [0..n)) x0 (A : 'E^0), unfunF f x0 A = (fun⇒ x0).
Proof.
intros n f x0 A; extF i; unfold unfunF.
destruct (im_dec f i) as [[[j Hj1] Hj2] | Hi]; easy.
Qed.

Lemma unfunF_ub :
  ∀ {n1 n2} (f : [0..n1) → [0..n2)) x0 (A1 : 'E^n1),
    injective f → invalF A1 (unfunF f x0 A1).
Proof. move=>> Hf i1; eexists; rewrite (unfunF_correct_l _ i1); easy. Qed.

Lemma unfunF_castF :
  ∀ {n1 p1 n2} (H1 : n1 = p1) (f : [0..n1) → [0..n2)) x0 (A1 : 'E^n1),
    injective f → unfunF (castF H1 f) x0 (castF H1 A1) = unfunF f x0 A1.
Proof.
intros n1 p1 n2 H1 f x0 A1 Hf; extF i2; unfold unfunF.
destruct (im_dec (castF H1 f) i2) as [[k1 Hk1] | Hi2],
    (im_dec f i2) as [[j1 Hj1] | Hi2']; simpl; [| exfalso.. | easy].
unfold castF; rewrite (Hf (cast_ord (eq_sym H1) k1) j1)// Hj1//.
contradict Hi2'. 2: contradict Hi2.
1,2: subst; rewrite castF_id; apply not_all_not_ex_equiv; eexists; easy.
Qed.

Lemma unfunF_eq :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f),
    ∃ p, injective p ∧ ∀ x0 (A1 : 'E^n1),
      unfunF f x0 A1 =
        permutF p
          (castF (injF_plus_minus_r Hf) (concatF A1 (constF (n2 - n1) x0))).
Proof.
intros n1 n2 f Hf; destruct (injF_extend_bij_EX Hf) as [p [Hp1 Hp2]].
∃ (f_inv Hp1); split; [apply bij_inj, f_inv_bij |].
intros x0 A1; extF i2.
destruct (im_dec f i2) as [[i1 Hi1] | Hi2]; unfold permutF, castF.
rewrite (unfunF_correct_l _ i1)// -Hi1 Hp2 f_inv_can_l.
assert (Hi1' : (cast_ord (eq_sym (injF_plus_minus_r Hf))
    (widen_ord (m:=n2) (injF_leq Hf) i1) < n1)%coq_nat)
    by now simpl; apply /ltP.
rewrite concatF_correct_l; f_equal; apply ord_inj; easy.
rewrite unfunF_correct_r; [| intros i1 Hi1; apply (Hi2 i1); easy].
assert (¬ (cast_ord (eq_sym (injF_plus_minus_r Hf))
    (f_inv Hp1 i2) < n1)%coq_nat).
  simpl; contradict Hi2; move: Hi2 ⇒ /ltP Hi2.
  apply not_all_not_ex_equiv; ∃ (Ordinal Hi2).
  rewrite Hp2; unfold widenF, widen_ord; simpl.
  rewrite -{3}(f_inv_can_r Hp1 i2).
  f_equal; apply ord_inj; easy.
rewrite concatF_correct_r; easy.
Qed.

Lemma unfunF_neqF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {x0} {A1 : 'E^n1},
    injective f → unfunF f False (neqF A1 x0) = neqF (unfunF f x0 A1) x0.
Proof.
intros n1 n2 f x0 A1 Hf; extF i2; unfold neqF;
    destruct (unfunF_correct x0 A1 i2 Hf) as [[i1 [<- ->]] | [Hi2 ->]].
rewrite (unfunF_correct_l _ i1); easy.
rewrite unfunF_correct_r; [apply prop_ext |]; easy.
Qed.

End FunF_Facts.

Section ExtendPF_Facts.

Context {E : Type}.

Properties of predicate extendPF.

Lemma extendPF_refl : ∀ {n} (P : 'Prop^n), extendPF id P P.
Proof. intros n P i; left; ∃ i; easy. Qed.

Note that incl P2 (Rg f) <-> forall i2, ~ Rg f i2 -> ~ P2 i2.
Lemma extendPF_funF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P2 : 'Prop^n2},
    injective f → incl P2 (Rg f) → extendPF f (funF f P2) P2.
Proof.
move⇒ n1 n2 f P2 Hf /compl_monot HP2 i2;
    destruct (im_dec f i2) as [[i1 <-] | Hi2].
left; ∃ i1; easy.
right; split; [| apply HP2; unfold compl]; rewrite Rg_compl; easy.
Qed.

Lemma extendPF_funF_rev :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2},
    injective f → extendPF f P1 P2 → P1 = funF f P2 ∧ incl P2 (Rg f).
Proof.
intros n1 n2 f P1 P2 Hf HP; split.
extF i1; destruct (HP (f i1)) as [[i1' [Hi1'a Hi1'b]] | [Hi1 _]].
apply Hf in Hi1'a; subst; easy.
contradict Hi1; easy.
apply incl_compl_equiv; unfold compl.
intros i2 Hi2. destruct (HP i2) as [[i1 [Hi1 _]] | Hi2'];
    [contradict Hi2; subst |]; easy.
Qed.

Lemma extendPF_funF_equiv :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2},
    injective f → extendPF f P1 P2 ↔ P1 = funF f P2 ∧ incl P2 (Rg f).
Proof.
intros; split; [apply extendPF_funF_rev |
    intros [HP1 HP2]; subst; apply extendPF_funF]; easy.
Qed.

Lemma extendPF_funF_neqF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {A2 : 'E^n2} {x0},
    injective f → incl (neqF A2 x0) (Rg f) →
    extendPF f (neqF (funF f A2) x0) (neqF A2 x0).
Proof. move=>> Hf HA2; rewrite -funF_neqF; apply (extendPF_funF Hf HA2). Qed.

Lemma extendPF_funF_neqF_rev :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {A2 : 'E^n2} {x0},
    injective f →
    extendPF f (neqF (funF f A2) x0) (neqF A2 x0) → incl (neqF A2 x0) (Rg f).
Proof. move=>> Hf /(extendPF_funF_rev Hf) HA2; easy. Qed.

Lemma extendPF_funF_neqF_equiv :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {A2 : 'E^n2} {x0},
    injective f →
    extendPF f (neqF (funF f A2) x0) (neqF A2 x0) ↔
    incl (neqF A2 x0) (Rg f).
Proof.
move=>> Hf; rewrite (extendPF_funF_equiv Hf).
assert (H : ∀ P Q : Prop, P → P ∧ Q ↔ Q) by tauto.
apply H, eq_sym, funF_neqF.
Qed.

Lemma extendPF_unfunF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (P1 : 'Prop^n1),
    injective f → extendPF f P1 (unfunF f False P1).
Proof.
intros n1 n2 f P1 Hf i2; destruct (unfunF_correct False P1 i2 Hf)
    as [[i1 [<- ->]] | [H1 ->]].
left; ∃ i1; easy.
right; split; [rewrite Rg_compl |]; easy.
Qed.

Lemma extendPF_unfunF_rev :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2},
    injective f → extendPF f P1 P2 → P2 = unfunF f False P1.
Proof.
move=>> Hf HP; extF i2;
    destruct (HP i2) as [[i1 [<- <-]] | [Hi2a Hi2b]].
rewrite (unfunF_correct_l _ i1); easy.
rewrite unfunF_correct_r; [apply notTE | rewrite -Rg_compl]; easy.
Qed.

Lemma extendPF_unfunF_equiv :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2},
    injective f → extendPF f P1 P2 ↔ P2 = unfunF f False P1.
Proof.
intros; split; [apply extendPF_unfunF_rev |
    intros; subst; apply extendPF_unfunF]; easy.
Qed.

Lemma extendPF_unfunF_neqF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (A1 : 'E^n1) x0,
    injective f →
    extendPF f (neqF A1 x0) (neqF (unfunF f x0 A1) x0).
Proof.
move=>> Hf; apply (extendPF_unfunF_equiv Hf), eq_sym, (unfunF_neqF Hf).
Qed.

Lemma extendPF_permutF :
  ∀ {n} {p : [0..n) → [0..n)} (P : 'Prop^n),
    injective p → extendPF p (permutF p P) P.
Proof.
move=>> Hp i; left; ∃ (f_inv (injF_bij Hp) i); split; [| unfold permutF];
    rewrite f_inv_can_r; easy.
Qed.

Lemma extendPF_incrF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    let q1 := proj1_sig (injF_restr_bij_EX Hf) in
    extendPF (f \o q1) (permutF q1 P1) P2.
Proof.
intros n1 n2 f Hf P1 P2 HP q1.
pose (Hq1a := proj1 (proj2_sig (injF_restr_bij_EX Hf))).
pose (Hq1b := proj2 (proj2_sig (injF_restr_bij_EX Hf))).
pose (p1 := f_inv Hq1a); fold q1 in Hq1a, Hq1b.
apply extendPF_unfunF_equiv; [apply (incrF_inj Hq1b) |].
rewrite (extendPF_unfunF_rev Hf HP); extF i2.
destruct (im_dec f i2) as [[k1 <-] | Hi2].
unfold permutF.
rewrite -{2}(f_inv_can_r Hq1a k1) -(comp_correct q1 f); fold p1.
rewrite (unfunF_correct_l _ k1)//
    (unfunF_correct_l _ (p1 k1) _ (incrF_inj Hq1b))//.
unfold p1; rewrite f_inv_can_r; easy.
rewrite !unfunF_correct_r//; intros k1; contradict Hi2.
rewrite not_all_not_ex_equiv; ∃ (q1 k1); auto.
Qed.

End ExtendPF_Facts.

Section WidenF_S_liftF_S_Facts1.

Properties of operators widenF_S/liftF_S/widenF.

Context {E : Type}.

Lemma PAF_ind_l :
  ∀ {n} {P : 'Prop^n.+1}, P ord0 → PAF (liftF_S P) → PAF P.
Proof.
intros n P H0 H1 i; destruct (ord_eq_dec i ord0) as [-> | Hi]; [easy |].
rewrite -(lift_lower_S Hi); apply H1.
Qed.

Lemma PAF_ind_r :
  ∀ {n} {P : 'Prop^n.+1}, PAF (widenF_S P) → P ord_max → PAF P.
Proof.
intros n P H0 H1 i; destruct (ord_eq_dec i ord_max) as [-> | Hi]; [easy |].
rewrite -(widen_narrow_S Hi); apply H0.
Qed.

Lemma widenF_S_invalF :
  ∀ {n1 n2} (A1 : 'E^n1.+1) (A2 : 'E^n2),
    invalF A1 A2 → invalF (widenF_S A1) A2.
Proof. unfold widenF_S; easy. Qed.

Lemma liftF_S_invalF :
  ∀ {n1 n2} (A1 : 'E^n1.+1) (A2 : 'E^n2),
    invalF A1 A2 → invalF (liftF_S A1) A2.
Proof. unfold liftF_S; easy. Qed.

Lemma widenF_S_0 : ∀ {n} (A : 'E^n.+2), widenF_S A ord0 = A ord0.
Proof. intros; unfold widenF_S; rewrite widen_S_0; easy. Qed.

Lemma widenF_S_max :
  ∀ {n} (A : 'E^n.+2), widenF_S A ord_max = A ord_pred_max.
Proof. intros; unfold widenF_S; rewrite widen_S_max; easy. Qed.

Lemma liftF_S_0 : ∀ {n} (A : 'E^n.+2), liftF_S A ord0 = A ord1.
Proof. intros; unfold liftF_S; rewrite lift_S_0; easy. Qed.

Lemma liftF_S_max : ∀ {n} (A : 'E^n.+2), liftF_S A ord_max = A ord_max.
Proof. intros; unfold liftF_S; rewrite lift_S_max; easy. Qed.

Lemma widenF_narrow_S :
  ∀ {n} (A : 'E^n.+1) {i} (H : i ≠ ord_max),
    widenF_S A (narrow_S H) = A i.
Proof. intros; unfold widenF_S; rewrite widen_narrow_S; easy. Qed.

Lemma liftF_lower_S :
  ∀ {n} (A : 'E^n.+1) {i} (H : i ≠ ord0), liftF_S A (lower_S H) = A i.
Proof. intros; unfold liftF_S; rewrite lift_lower_S; easy. Qed.

Lemma widenF_S_reg :
  ∀ {n} (A B : 'E^n.+1), widenF_S A = widenF_S B → eqxFmax A B.
Proof.
move=>> H i Hi; rewrite -!(widenF_narrow_S _ Hi); apply (extF_rev _ _ H).
Qed.

Lemma widenF_S_compat :
  ∀ {n} (A B : 'E^n.+1), eqxFmax A B → widenF_S A = widenF_S B.
Proof. move=>> H; extF; apply H, widen_S_not_last. Qed.

Lemma eqxFn_equiv :
  ∀ {n} (A B : 'E^n.+1), eqxFmax A B ↔ widenF_S A = widenF_S B.
Proof. intros; split; [apply widenF_S_compat | apply widenF_S_reg]. Qed.

Lemma widenF_S_neqxF_compat :
  ∀ {n} (A B : 'E^n.+1), neqxFmax A B → widenF_S A ≠ widenF_S B.
Proof.
move=>>; rewrite contra_not_r_equiv -eqxF_not_equiv; apply widenF_S_reg.
Qed.

Lemma widenF_S_neqxF_reg :
  ∀ {n} (A B : 'E^n.+1), widenF_S A ≠ widenF_S B → neqxFmax A B.
Proof.
move=>>; rewrite contra_not_l_equiv -eqxF_not_equiv; apply widenF_S_compat.
Qed.

Lemma neqxFn_equiv :
  ∀ {n} (A B : 'E^n.+1), neqxFmax A B ↔ widenF_S A ≠ widenF_S B.
Proof.
intros; split; [apply widenF_S_neqxF_compat | apply widenF_S_neqxF_reg].
Qed.

Lemma liftF_S_reg :
  ∀ {n} (A B : 'E^n.+1), liftF_S A = liftF_S B → eqxF0 A B.
Proof.
move=>> H i Hi; rewrite -!(liftF_lower_S _ Hi); apply (extF_rev _ _ H).
Qed.

Lemma liftF_S_compat :
  ∀ {n} (A B : 'E^n.+1), eqxF0 A B → liftF_S A = liftF_S B.
Proof. move=>> H; extF; apply H, lift_S_not_first. Qed.

Lemma eqxF0_equiv :
  ∀ {n} (A B : 'E^n.+1), eqxF0 A B ↔ liftF_S A = liftF_S B.
Proof. intros; split; [apply liftF_S_compat | apply liftF_S_reg]. Qed.

Lemma liftF_S_neqxF_compat :
  ∀ {n} (A B : 'E^n.+1), neqxF0 A B → liftF_S A ≠ liftF_S B.
Proof.
move=>>; rewrite contra_not_r_equiv -eqxF_not_equiv; apply liftF_S_reg.
Qed.

Lemma liftF_S_neqxF_reg :
  ∀ {n} (A B : 'E^n.+1), liftF_S A ≠ liftF_S B → neqxF0 A B.
Proof.
move=>>; rewrite contra_not_l_equiv -eqxF_not_equiv; apply liftF_S_compat.
Qed.

Lemma neqxF0_equiv :
  ∀ {n} (A B : 'E^n.+1), neqxF0 A B ↔ liftF_S A ≠ liftF_S B.
Proof.
intros; split; [apply liftF_S_neqxF_compat | apply liftF_S_neqxF_reg].
Qed.

Lemma castF_widenF_S :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1.+1),
    castF H (widenF_S A1) = widenF_S (castF (eq_S _ _ H) A1).
Proof. intros; extF; unfold widenF_S, castF; f_equal; apply ord_inj; easy. Qed.

Lemma widenF_S_castF :
  ∀ {n1 n2} (H : n1.+1 = n2.+1) (A1 : 'E^n1.+1),
    widenF_S (castF H A1) = castF (eq_add_S _ _ H) (widenF_S A1).
Proof. intros; rewrite castF_widenF_S; f_equal; apply castF_eq_l. Qed.

Lemma castF_liftF_S :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1.+1),
    castF H (liftF_S A1) = liftF_S (castF (eq_S _ _ H) A1).
Proof. intros; extF; unfold liftF_S, castF; f_equal; apply ord_inj; easy. Qed.

Lemma liftF_S_castF :
  ∀ {n1 n2} (H : n1.+1 = n2.+1) (A1 : 'E^n1.+1),
    liftF_S (castF H A1) = castF (eq_add_S _ _ H) (liftF_S A1).
Proof. intros; rewrite castF_liftF_S; f_equal; apply castF_eq_l. Qed.

Lemma widenF_S_concatF :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2.+1),
    widenF_S (castF (addnS n1 n2) (concatF A1 A2)) = concatF A1 (widenF_S A2).
Proof.
intros n1 n2 A1 A2; extF i; unfold widenF_S, widen_S, castF.
destruct (lt_dec i n1) as [Hi | Hi].
rewrite 2!concatF_correct_l; f_equal; apply ord_inj; easy.
rewrite 2!concatF_correct_r; f_equal; apply ord_inj; easy.
Qed.

Lemma liftF_S_concatF :
  ∀ {n1 n2} (A1 : 'E^n1.+1) (A2 : 'E^n2),
    liftF_S (castF (addSn n1 n2) (concatF A1 A2)) = concatF (liftF_S A1) A2.
Proof.
intros n1 n2 A1 A2; extF i; unfold liftF_S, lift_S, castF.
destruct (lt_dec i n1) as [Hi | Hi].
rewrite concatF_correct_l; auto with arith.
intros; rewrite concatF_correct_l; f_equal; apply ord_inj; easy.
rewrite concatF_correct_r; auto with arith.
intros; rewrite concatF_correct_r; f_equal; apply ord_inj; easy.
Qed.

Lemma widenF_0_alt :
  ∀ {n1 n2} (H : n1 ≤ n2.+1) (H0 : 0 < n1) {A2 : 'E^n2.+1},
    widenF H A2 (Ordinal H0) = A2 ord0.
Proof. intros; unfold widenF; f_equal; apply ord_inj; easy. Qed.

Lemma widenF_0 :
  ∀ {n1 n2} (H : n1.+1 ≤ n2.+1) {A2 : 'E^n2.+1},
    widenF H A2 ord0 = A2 ord0.
Proof. intros; rewrite -(widenF_0_alt H (ltn0Sn n1)); easy. Qed.

Lemma widenF_max :
  ∀ {n1 n2} (H : n1.+1 ≤ n2.+1) {A2 : 'E^n2.+1},
    widenF H A2 ord_max = A2 (Ordinal H).
Proof. intros; unfold widenF; f_equal; apply ord_inj; easy. Qed.

Lemma widenF_nil :
  ∀ {n} (A : 'E^n) (x0 : E), widenF (leq_ord ord0) A = (fun⇒ x0).
Proof. intros; extF i; destruct i; easy. Qed.

Lemma widenF_full : ∀ {n} {A : 'E^n}, widenF (leq_ord ord_max) A = A.
Proof. unfold widenF; intros; extF; f_equal; apply ord_inj; easy. Qed.

Lemma widenF_liftF_S :
  ∀ {n1 n2} (H : n1 ≤ n2) (A2 : 'E^n2.+1),
    widenF H (liftF_S A2) = liftF_S (widenF (leqS H) A2).
Proof. intros; extF; unfold widenF, liftF_S; f_equal; apply ord_inj; easy. Qed.

Lemma liftF_S_widenF :
  ∀ {n1 n2} (H : n1.+1 ≤ n2.+1) (A2 : 'E^n2.+1),
    liftF_S (widenF H A2) = widenF (leqS_rev H) (liftF_S A2).
Proof. intros; extF; unfold widenF, liftF_S; f_equal; apply ord_inj; easy. Qed.

Lemma widenF_firstF_eq :
  ∀ {n} (A : 'E^n) (j : [0..n]),
    widenF (leq_ord j) A = firstF (castF_nip A j).
Proof.
intros; unfold firstF, castF_nip, castF, widenF;
    extF; f_equal; apply ord_inj; easy.
Qed.

Lemma liftF_S_charac :
  ∀ {n} (P : 'Prop^n.+1), liftF_S (charac P) = charac (liftF_S P).
Proof.
intros n P; extF i; destruct (in_dec (liftF_S P) i) as [Hi | Hi].
rewrite (charac_is_1 _ _ Hi); apply charac_is_1; easy.
rewrite (charac_is_0 _ _ Hi); apply charac_is_0; easy.
Qed.

Lemma widenF_S_charac :
  ∀ {n} (P : 'Prop^n.+1), widenF_S (charac P) = charac (widenF_S P).
Proof.
intros n P; extF i; destruct (in_dec (widenF_S P) i) as [Hi | Hi].
rewrite (charac_is_1 _ _ Hi); apply charac_is_1; easy.
rewrite (charac_is_0 _ _ Hi); apply charac_is_0; easy.
Qed.

Context {E1 E2 F : Type}.

Lemma widenF_S_mapF :
  ∀ (f : E1 → E2) {n} (A1 : 'E1^n.+1),
    widenF_S (mapF f A1) = mapF f (widenF_S A1).
Proof. easy. Qed.

Lemma widenF_mapF :
  ∀ (f : E → F) {n1 n2} (H : n1 ≤ n2) (A : 'E^n2),
    widenF H (mapF f A) = mapF f (widenF H A).
Proof. easy. Qed.

Lemma liftF_S_mapF :
  ∀ (f : E1 → E2) {n} (A1 : 'E1^n.+1),
    liftF_S (mapF f A1) = mapF f (liftF_S A1).
Proof. easy. Qed.

End WidenF_S_liftF_S_Facts1.

Section WidenF_S_liftF_S_Facts2a.

Context {E : Type}.
Context {P : E → Prop}.

Context {n1 n2 : nat}.
Hypothesis H : n1 ≤ n2.

Lemma widenF_P_compat :
  ∀ {A2 : 'E^n2} (j2 : [0..n2]),
    (∀ (i2 : [0..j2)), P (widenF (leq_ord j2) A2 i2)) →
    ∀ (i1 : [0..n1)), i1 < j2 → P (widenF H A2 i1).
Proof.
intros A2 j2 HA2 i1 Hi1.
replace (widenF _ _ _) with (widenF (leq_ord j2) A2 (Ordinal Hi1)); [easy |].
unfold widenF; f_equal; apply ord_inj; easy.
Qed.

End WidenF_S_liftF_S_Facts2a.

Section WidenF_S_liftF_S_Facts2b.

Context {E : Type}.
Context {P : E → Prop}.

Context {n1 n2 : nat}.
Hypothesis H : n1 ≤ n2.

Lemma widenF_0_P_compat :
  ∀ {A2 : 'E^n2.+1} {j2 : [0..n2.+1]} (Hj2 : j2 ≠ ord0),
    (∀ (i2 : [0..j2)), P (widenF (leq_ord j2) A2 i2)) → P (A2 ord0).
Proof.
intros A2 j2 Hj2 HA2; rewrite -(widenF_0 (ltn0Sn n2)).
apply (widenF_P_compat _ j2 HA2); clear E P A2 HA2.
apply ord_n0_gtn_equiv; easy.
Qed.

Lemma widenF_liftF_S_P_compat :
  ∀ {A2 : 'E^n2.+1} {j2 : [0..n2.+1]} (Hj2 : j2 ≠ ord0),
    (∀ (i2 : [0..j2)), P (widenF (leq_ord j2) A2 i2)) →
    ∀ (i1 : [0..n1)), i1 < lower_S Hj2 → P (widenF H (liftF_S A2) i1).
Proof.
intros A2 j2 Hj2a HA2; apply widenF_P_compat; intros i1.
assert (Hi1 : lift_S i1 < j2).
  rewrite lift_S_correct; destruct i1 as [i1 Hi1]; simpl.
  move: Hi1; rewrite lower_S_correct; apply ltnP_gtnS.
  move: Hj2a ⇒ /ord_neq_compat; easy.
replace (widenF _ _ _) with (widenF (leq_ord j2) A2 (Ordinal Hi1)); [easy |].
unfold widenF, liftF_S; f_equal; apply ord_inj; easy.
Qed.

End WidenF_S_liftF_S_Facts2b.

Section FirstF_lastF_Facts.

Context {E : Type}.

Properties of operators firstF/lastF.

Lemma firstF_compat :
  ∀ {n1 n2} (A B : 'E^(n1 + n2)),
    (∀ i : [0..n1 + n2), (i < n1)%coq_nat → A i = B i) →
    firstF A = firstF B.
Proof. move=>> H; extF i; destruct i; apply H; apply /ltP; easy. Qed.

Lemma lastF_compat :
  ∀ {n1 n2} (A B : 'E^(n1 + n2)),
    (∀ i : [0..n1 + n2), (n1 ≤ i)%coq_nat → A i = B i) →
    lastF A = lastF B.
Proof. move=>> H; extF; apply H; apply /leP; apply leq_addr. Qed.

Lemma firstF_0 : ∀ {n1 n2} (A : 'E^(n1.+1 + n2)), firstF A ord0 = A ord0.
Proof. intros; unfold firstF; f_equal; apply ord_inj; easy. Qed.

Lemma lastF_max :
  ∀ {n1 n2} (A : 'E^(n1 + n2.+1)),
    lastF A ord_max = castF (addnS n1 n2) A ord_max.
Proof. intros; unfold lastF, castF; f_equal; apply ord_inj; easy. Qed.

Lemma firstF_castF :
  ∀ {p} m1 m2 {n1} n2 (H1 : m1 = n1) (Hm : p = m1 + m2) (Hn : p = n1 + n2)
      (A : 'E^p),
    firstF (castF Hn A) = castF H1 (firstF (castF Hm A)).
Proof. intros; extF; unfold firstF, castF; f_equal; apply ord_inj; easy. Qed.

Lemma lastF_castF :
  ∀ {p} m1 m2 n1 {n2} (H2 : m2 = n2) (Hm : p = m1 + m2) (Hn : p = n1 + n2)
      (A : 'E^p),
    lastF (castF Hn A) = castF H2 (lastF (castF Hm A)).
Proof.
intros p m1 m2 n1 n2 H2 Hm Hn A; extF; unfold lastF, castF.
f_equal; apply ord_inj; simpl; subst; f_equal; apply addn_inj_l in Hn; easy.
Qed.

Lemma firstF_Sp1 :
  ∀ {n} (A : 'E^n.+1), firstF (castF_Sp1 A) = widenF_S A.
Proof.
intros; extF; unfold firstF, castF_Sp1, castF, widenF_S;
    f_equal; apply ord_inj; easy.
Qed.

Lemma lastF_Sp1 :
  ∀ {n} (A : 'E^n.+1), lastF (castF_Sp1 A) = singleF (A ord_max).
Proof.
intros; extF; rewrite ord_one singleF_0.
unfold lastF, castF_Sp1, castF; f_equal; apply ord_inj; apply addn0.
Qed.

Lemma firstF_S1p :
  ∀ {n} (A : 'E^n.+1), firstF (castF_S1p A) = singleF (A ord0).
Proof.
intros; extF; rewrite ord_one singleF_0.
unfold firstF, castF_S1p, castF; f_equal; apply ord_inj; easy.
Qed.

Lemma lastF_S1p : ∀ {n} (A : 'E^n.+1), lastF (castF_S1p A) = liftF_S A.
Proof.
intros; extF; unfold lastF, castF_S1p, castF, liftF_S;
    f_equal; apply ord_inj; auto with arith.
Qed.

Lemma firstF_ord_splitS :
  ∀ {n} (A : 'E^n.+1) i0,
    firstF (castF (ord_splitS i0) A) =
    widenF_S (firstF (castF (ordS_splitS i0) A)).
Proof.
intros; extF; unfold firstF, widenF_S, castF; f_equal; apply ord_inj; easy.
Qed.

Lemma firstF_ordS_splitS_last :
  ∀ {n} (A : 'E^n.+1) i0,
    firstF (castF (ordS_splitS i0) A) ord_max = A i0.
Proof. intros; unfold firstF, castF; f_equal; apply ord_inj; easy. Qed.

Lemma lastF_ord_splitS_first :
  ∀ {n} (A : 'E^n.+1) i0, lastF (castF (ord_splitS i0) A) ord0 = A i0.
Proof. intros; unfold lastF, castF; f_equal; apply ord_inj; apply addn0. Qed.

Lemma lastF_ordS_splitS :
  ∀ {n} (A : 'E^n.+1) i0,
    lastF (castF (ordS_splitS i0) A) =
    liftF_S (lastF (castF (ord_splitS i0) A)).
Proof.
intros; extF; unfold lastF, liftF_S, castF; f_equal; apply ord_inj; simpl.
rewrite bump_r; try apply addSnnS; auto with arith.
Qed.

Lemma firstF2 :
  ∀ {n1 n2 n3} (A : 'E^((n1 + n2) + n3)),
    firstF (firstF A) = firstF (castF (eq_sym (addnA n1 n2 n3)) A).
Proof. intros; extF; unfold firstF, castF; f_equal; apply ord_inj; easy. Qed.

Lemma lastF2 :
  ∀ {n1 n2 n3} (A : 'E^(n1 + (n2 + n3))),
    lastF (lastF A) = lastF (castF (addnA n1 n2 n3) A).
Proof. intros; extF; unfold lastF, castF; f_equal; apply ord_inj, addnA. Qed.

Lemma liftF_S_firstF :
  ∀ {n1 n2} (A : 'E^(n1.+1 + n2)),
    liftF_S (firstF A) = firstF (liftF_S (castF (addSn n1 n2) A)).
Proof.
intros; extF; unfold liftF_S, firstF, castF; f_equal; apply ord_inj; easy.
Qed.

Lemma widenF_S_lastF :
  ∀ {n1 n2} (A : 'E^(n1 + n2.+1)),
    widenF_S (lastF A) = lastF (widenF_S (castF (addnS n1 n2) A)).
Proof.
intros; extF; unfold widenF_S, lastF, castF; f_equal; apply ord_inj; easy.
Qed.

Lemma firstF_concatF :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2), firstF (concatF A1 A2) = A1.
Proof.
intros; extF; unfold firstF, concatF; destruct (lt_dec _ _) as [Hi1 | Hi1];
    [rewrite concat_l_first | contradict Hi1; apply /ltP; simpl]; easy.
Qed.

Lemma lastF_concatF :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2), lastF (concatF A1 A2) = A2.
Proof.
intros; extF; unfold lastF, concatF; destruct (lt_dec _ _) as [Hi2 | Hi2].
contradict Hi2; apply Nat.le_ngt; apply /leP; simpl; apply leq_addr.
rewrite concat_r_last; easy.
Qed.

Lemma concatF_splitF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)), A = concatF (firstF A) (lastF A).
Proof.
intros n1 n2 A; extF i.
destruct (lt_dec i n1) as [Hi | Hi]; [unfold firstF | unfold lastF].
rewrite concatF_correct_l; f_equal; apply ord_inj; easy.
rewrite concatF_correct_r; f_equal; apply ord_inj; simpl.
symmetry; apply subnKC; apply /leP; auto with zarith.
Qed.

Lemma concatF_splitF_S1p :
  ∀ {n} (A : 'E^n.+1),
    A = castF_1pS (concatF (singleF (A ord0)) (liftF_S A)).
Proof.
intros; rewrite -firstF_S1p -lastF_S1p -concatF_splitF castF_1pS_S1p; easy.
Qed.

Lemma concatF_splitF_S1p' :
  ∀ {n} (A : 'E^n.+1),
    concatF (singleF (A ord0)) (liftF_S A) = castF_S1p A.
Proof. intros n A; rewrite {3}(concatF_splitF_S1p A) castF_S1p_1pS; easy. Qed.

Lemma concatF_splitF_Sp1 :
  ∀ {n} (A : 'E^n.+1),
    A = castF_p1S (concatF (widenF_S A) (singleF (A ord_max))).
Proof.
intros; rewrite -firstF_Sp1 -lastF_Sp1 -concatF_splitF castF_p1S_Sp1; easy.
Qed.

Lemma concatF_splitF_Sp1' :
  ∀ {n} (A : 'E^n.+1),
    concatF (widenF_S A) (singleF (A ord_max)) = castF_Sp1 A.
Proof. intros n A; rewrite {3}(concatF_splitF_Sp1 A) castF_Sp1_p1S; easy. Qed.

Lemma splitF_compat :
  ∀ {n1 n2} (A B : 'E^(n1 + n2)),
    A = B → firstF A = firstF B ∧ lastF A = lastF B.
Proof. intros; split; f_equal; easy. Qed.

Lemma splitF_reg :
  ∀ {n1 n2} (A B : 'E^(n1 + n2)),
    firstF A = firstF B → lastF A = lastF B → A = B.
Proof.
intros n1 n2 A B H1 H2; rewrite (concatF_splitF A) H1 H2 -concatF_splitF; easy.
Qed.

Lemma extF_splitF :
  ∀ {n n1 n2} (H : n = n1 + n2) (A B : 'E^n),
    firstF (castF H A) = firstF (castF H B) →
    lastF (castF H A) = lastF (castF H B) →
    A = B.
Proof. intros n n1 n2 H A B Hf Hl; apply (castF_inj H), splitF_reg; easy. Qed.

Lemma extF_ind_l :
∀ {n} (A B : 'E^n.+1),
    A ord0 = B ord0 → liftF_S A = liftF_S B → A = B.
Proof. intros; apply extF_liftF_S; easy. Qed.

Lemma extF_ind_r :
∀ {n} (A B : 'E^n.+1),
    widenF_S A = widenF_S B → A ord_max = B ord_max → A = B.
Proof. intros; apply extF_widenF_S; easy. Qed.

Lemma firstF_insertF :
  ∀ {n} i0 x0 (A : 'E^n),
    firstF (castF (ord_splitS i0) (insertF i0 x0 A)) =
    firstF (castF (ord_split i0) A).
Proof.
intros; extF i; destruct i; unfold firstF, castF;
    rewrite insertF_correct_rl; try now apply /ltP.
intro; f_equal; apply ord_inj; easy.
Qed.

Lemma lastF_insertF :
  ∀ {n} i0 x0 (A : 'E^n),
    lastF (castF (ordS_splitS i0) (insertF i0 x0 A)) =
    lastF (castF (ord_split i0) A).
Proof.
intros; extF i; destruct i; unfold lastF, castF;
    rewrite insertF_correct_rr; try now (apply /ltP; apply ltn_addr).
intro; f_equal; apply ord_inj; simpl; rewrite <- addnBAC, subn1; easy.
Qed.

Lemma firstF_skipF :
  ∀ {n} i0 (A : 'E^n.+1),
    firstF (castF (ord_split i0) (skipF i0 A)) =
    firstF (castF (ord_splitS i0) A).
Proof.
intros; extF i; unfold firstF, skipF, castF; f_equal; apply ord_inj; simpl.
apply bump_l; destruct i; apply /ltP; easy.
Qed.

Lemma lastF_skipF :
  ∀ {n} i0 (A : 'E^n.+1),
    lastF (castF (ord_split i0) (skipF i0 A)) =
    lastF (castF (ordS_splitS i0) A).
Proof.
intros; extF; unfold lastF, skipF, castF; f_equal; apply ord_inj; simpl.
apply bump_r, Nat.le_add_r.
Qed.

Lemma firstF_replaceF :
  ∀ {n} i0 x0 (A : 'E^n),
    firstF (castF (ord_split_pred i0) (replaceF i0 x0 A)) =
    firstF (castF (ord_split_pred i0) A).
Proof.
intros n [i0 Hi0] x0 A; apply extF; intros [i Hi]; unfold firstF, castF;
    rewrite replaceF_correct_r; try easy.
apply ord_neq; simpl in *; contradict Hi; rewrite Hi.
move⇒ /ltP H; auto with zarith.
Qed.

Lemma lastF_replaceF :
  ∀ {n} i0 x0 (A : 'E^n),
    lastF (castF (ordS_split i0) (replaceF i0 x0 A)) =
    lastF (castF (ordS_split i0) A).
Proof.
intros n [i0 Hi0] x0 A; extF i; destruct i; unfold lastF, castF;
    rewrite replaceF_correct_r; try easy.
apply ord_neq; simpl; rewrite -plusE; auto with zarith.
Qed.

End FirstF_lastF_Facts.

Section ConcatF_Facts.

Context {E : Type}.

Properties of operator concatF.

Lemma concatF_eq :
  ∀ {n1 n2} (A1 B1 : 'E^n1) (A2 B2 : 'E^n2),
    A1 = B1 → A2 = B2 → concatF A1 A2 = concatF B1 B2.
Proof. intros; f_equal; easy. Qed.

Lemma concatF_inj_l :
  ∀ {n1 n2} (A1 B1 : 'E^n1) (A2 B2 : 'E^n2),
    concatF A1 A2 = concatF B1 B2 → A1 = B1.
Proof.
intros n1 n2 A1 B1 A2 B2 H.
rewrite -(firstF_concatF A1 A2) -(firstF_concatF B1 B2); rewrite H; easy.
Qed.

Lemma concatF_inj_r :
  ∀ {n1 n2} (A1 B1 : 'E^n1) (A2 B2 : 'E^n2),
    concatF A1 A2 = concatF B1 B2 → A2 = B2.
Proof.
intros n1 n2 A1 B1 A2 B2 H.
rewrite -(lastF_concatF A1 A2) -(lastF_concatF B1 B2); rewrite H; easy.
Qed.

Lemma concatF_inj :
  ∀ {n1 n2} (A1 B1 : 'E^n1) (A2 B2 : 'E^n2),
    concatF A1 A2 = concatF B1 B2 → A1 = B1 ∧ A2 = B2.
Proof.
intros n1 n2 A1 B1 A2 B2 H; split;
    [apply (concatF_inj_l _ _ A2 B2) | apply (concatF_inj_r A1 B1)]; easy.
Qed.

Lemma concatF_nextF_compat_l :
  ∀ {n1 n2} {A1 B1 : 'E^n1} (A2 B2 : 'E^n2),
    A1 ≠ B1 → concatF A1 A2 ≠ concatF B1 B2.
Proof.
intros n1 n2 A1 B1 A2 B2 H; contradict H.
destruct (concatF_inj _ _ _ _ H); easy.
Qed.

Lemma concatF_nextF_compat_r :
  ∀ {n1 n2} (A1 B1 : 'E^n1) {A2 B2 : 'E^n2},
    A2 ≠ B2 → concatF A1 A2 ≠ concatF B1 B2.
Proof.
intros n1 n2 A1 B1 A2 B2 H; contradict H.
destruct (concatF_inj _ _ _ _ H); easy.
Qed.

Lemma concatF_nextF_reg :
  ∀ {n1 n2} {A1 B1 : 'E^n1} {A2 B2 : 'E^n2},
    concatF A1 A2 ≠ concatF B1 B2 → A1 ≠ B1 ∨ A2 ≠ B2.
Proof.
move=>> H; apply not_and_or; contradict H; apply concatF_eq; easy.
Qed.

Lemma concatF_nextF_equiv :
  ∀ {n1 n2} {A1 B1 : 'E^n1} {A2 B2 : 'E^n2},
    concatF A1 A2 ≠ concatF B1 B2 ↔ A1 ≠ B1 ∨ A2 ≠ B2.
Proof.
intros; split; [apply concatF_nextF_reg | intros [H | H]];
    [apply concatF_nextF_compat_l | apply concatF_nextF_compat_r]; easy.
Qed.

Lemma concatF_nil_l :
  ∀ {n2} (A1 : 'E^0) (A2 : 'E^n2), concatF A1 A2 = A2.
Proof.
intros n2 A1 A2; unfold concatF; extF; destruct (lt_dec _ _); try easy.
f_equal; apply ord_inj; simpl; auto with arith.
Qed.

Lemma concatF_nil_l' :
  ∀ {n1 n2} (H : n1 = 0) (A1 : 'E^n1) (A2 : 'E^n2),
    concatF A1 A2 = castF (eq_sym (nat_plus_0_l H)) A2.
Proof.
intros; subst; rewrite concatF_nil_l; extF.
unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_nil_r :
  ∀ {n1} (A1 : 'E^n1) (A2 : 'E^0),
    concatF A1 A2 = castF (addn0_sym n1) A1.
Proof.
intros n1 A1 A2; unfold concatF, castF; extF.
destruct (lt_dec _ _) as [Hi | Hi].
f_equal; apply ord_inj; auto with arith.
contradict Hi; destruct i; simpl; apply /ltP; rewrite (addn0_sym n1); easy.
Qed.

Lemma concatF_nil_r' :
  ∀ {n1 n2} (H : n2 = 0) (A1 : 'E^n1) (A2 : 'E^n2),
    concatF A1 A2 = castF (eq_sym (nat_plus_0_r H)) A1.
Proof.
intros; subst; rewrite concatF_nil_r; extF.
unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_first :
  ∀ {n1 n2} (A1 : 'E^n1.+1) (A2 : 'E^n2),
    castF (addSn n1 n2) (concatF A1 A2) ord0 = A1 ord0.
Proof.
intros n1 n2 A1 A2; rewrite -{2}(firstF_concatF A1 A2).
unfold castF, firstF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_last :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2.+1),
    castF (addnS n1 n2) (concatF A1 A2) ord_max = A2 ord_max.
Proof.
intros n1 n2 A1 A2; rewrite -{2}(lastF_concatF A1 A2).
unfold castF, lastF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_constF :
  ∀ {n1 n2} (x : E),
    concatF (constF n1 x) (constF n2 x) = constF (n1 + n2) x.
Proof.
intros; extF i; destruct (lt_dec i n1);
    [rewrite concatF_correct_l | rewrite concatF_correct_r]; easy.
Qed.

Lemma concatF_singleF_2 :
  ∀ (x0 x1 : E), concatF (singleF x0) (singleF x1) = coupleF x0 x1.
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->];
    [rewrite coupleF_0 | rewrite coupleF_1]; easy.
Qed.

Lemma concatF_singleF_1_2 :
  ∀ (x0 x1 x2 : E),
    concatF (singleF x0) (concatF (singleF x1) (singleF x2)) =
        tripleF x0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->];
    [rewrite tripleF_0 | rewrite tripleF_1 | rewrite tripleF_2]; easy.
Qed.

Lemma concatF_singleF_2_1 :
  ∀ (x0 x1 x2 : E),
    concatF (concatF (singleF x0) (singleF x1)) (singleF x2) =
        tripleF x0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->];
    [rewrite tripleF_0 | rewrite tripleF_1 | rewrite tripleF_2]; easy.
Qed.

Lemma concatF_singleF_coupleF :
  ∀ (x0 x1 x2 : E),
    concatF (singleF x0) (coupleF x1 x2) = tripleF x0 x1 x2.
Proof. intros; rewrite -concatF_singleF_1_2 concatF_singleF_2; easy. Qed.

Lemma concatF_coupleF_singleF :
  ∀ (x0 x1 x2 : E),
    concatF (coupleF x0 x1) (singleF x2) = tripleF x0 x1 x2.
Proof. intros; rewrite -concatF_singleF_2_1 concatF_singleF_2; easy. Qed.

Lemma concatF_ub_l :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2), invalF A1 (concatF A1 A2).
Proof.
intros n1 n2 A1 A2 i1; eexists; rewrite -{1}(firstF_concatF A1 A2); easy.
Qed.

Lemma concatF_ub_r :
  ∀ {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2), invalF A2 (concatF A1 A2).
Proof.
intros n1 n2 A1 A2 i2; eexists; rewrite -{1}(lastF_concatF A1 A2); easy.
Qed.

Lemma concatF_lub_invalF :
  ∀ {n1 n2 n} (A1 : 'E^n1) (A2 : 'E^n2) (A : 'E^n),
    invalF A1 A → invalF A2 A → invalF (concatF A1 A2) A.
Proof.
intros n1 n2 n A1 A2 A HA1 HA2 i; destruct (lt_dec i n1).
rewrite concatF_correct_l; apply HA1.
rewrite concatF_correct_r; apply HA2.
Qed.

Lemma concatF_lub_inclF :
  ∀ {n1 n2} PE (A1 : 'E^n1) (A2 : 'E^n2),
    inclF A1 PE → inclF A2 PE → inclF (concatF A1 A2) PE.
Proof.
intros n1 n2 PE A1 A2 H1 H2 i; destruct (lt_dec i n1).
rewrite concatF_correct_l; easy.
rewrite concatF_correct_r; easy.
Qed.

Lemma concatF_inclF_reg_l :
  ∀ {n1 n2 PE} {A1 : 'E^n1} (A2 : 'E^n2),
    inclF (concatF A1 A2) PE → inclF A1 PE.
Proof.
intros n1 n2 PE A1 A2 H i1; rewrite -(firstF_concatF A1 A2); apply H.
Qed.

Lemma concatF_inclF_reg_r :
  ∀ {n1 n2 PE} (A1 : 'E^n1) {A2 : 'E^n2},
    inclF (concatF A1 A2) PE → inclF A2 PE.
Proof.
intros n1 n2 PE A1 A2 H i2; rewrite -(lastF_concatF A1 A2); apply H.
Qed.

Lemma concatF_inclF_equiv :
  ∀ {n1 n2 PE} (A1 : 'E^n1) (A2 : 'E^n2),
    inclF (concatF A1 A2) PE ↔ inclF A1 PE ∧ inclF A2 PE.
Proof.
intros; split; intros H.
split; [apply (concatF_inclF_reg_l _ H) | apply (concatF_inclF_reg_r _ H)].
apply concatF_lub_inclF; easy.
Qed.

Lemma concatFP :
  ∀ (P : E → Prop) {n1 n2} (A1 : 'E^n1) (A2 : 'E^n2),
    (∀ i, P (concatF A1 A2 i)) ↔
    (∀ i1, P (A1 i1)) ∧ (∀ i2, P (A2 i2)).
Proof.
intros P n1 n2 A1 A2; split; intros HA; [split | ]; intros i.
destruct (lt_dec (first_ord n2 i) n1) as [Hi | Hi].
specialize (HA (first_ord n2 i)).
rewrite (concatF_correct_l _ _ _ Hi) concat_l_first in HA; easy.
contradict Hi; destruct i as [i Hi]; apply /ltP; easy.
destruct (lt_dec (last_ord n1 i) n1) as [Hi | Hi].
contradict Hi; destruct i as [i Hi];
    apply Nat.nlt_ge; apply /leP; apply leq_addr.
specialize (HA (last_ord n1 i)).
rewrite (concatF_correct_r _ _ _ Hi) concat_r_last in HA; easy.
destruct HA as [HA1 Ha2], (lt_dec i n1) as [Hi | Hi].
rewrite concatF_correct_l; easy.
rewrite concatF_correct_r; easy.
Qed.

Lemma concatF_castF :
  ∀ {n1 n2 m1 m2} (H1 : n1 = m1) (H2 : n2 = m2) (A1 : 'E^n1) (A2 : 'E^n2),
    concatF (castF H1 A1) (castF H2 A2) =
      castF (f_equal2_plus _ _ _ _ H1 H2) (concatF A1 A2).
Proof.
intros; subst; extF.
rewrite !castF_id; unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_castF_l :
  ∀ {n1 m1 n2} (H1 : n1 = m1) (A1 : 'E^n1) (A2 : 'E^n2),
    concatF (castF H1 A1) A2 =
      castF (f_equal (Nat.add^~ n2) H1) (concatF A1 A2).
Proof.
intros; subst; extF.
rewrite castF_id; unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_castF_r :
  ∀ {n1 n2 m2} (H2 : n2 = m2) (A1 : 'E^n1) (A2 : 'E^n2),
    concatF A1 (castF H2 A2) = castF (f_equal (Nat.add n1) H2) (concatF A1 A2).
Proof.
intros; subst; extF.
rewrite castF_id; unfold castF; f_equal; apply ord_inj; easy.
Qed.

Lemma concatF_assoc_r :
  ∀ {n1 n2 n3} (A1 : 'E^n1) (A2 : 'E^n2) (A3 : 'E^n3),
    concatF A1 (concatF A2 A3) =
        castF (eq_sym (Nat.add_assoc n1 n2 n3)) (concatF (concatF A1 A2) A3).
Proof.
intros n1 n2 n3 A1 A2 A3; extF i; unfold castF.
destruct (lt_dec i n1) as [Hi1 | Hi1].
rewrite concatF_correct_l; try easy.
rewrite concatF_correct_l; try easy.
simpl; auto with arith.
intros K1.
rewrite concatF_correct_l; try easy.
f_equal; apply ord_inj; easy.
rewrite concatF_correct_r; try easy.
destruct (lt_dec i (n1 + n2)%coq_nat) as [Hi2 | Hi2].
rewrite concatF_correct_l; try easy.
simpl; rewrite -minusE; auto with zarith.
intros K1.
rewrite concatF_correct_l; try easy.
rewrite concatF_correct_r; try easy.
f_equal; apply ord_inj; easy.
rewrite concatF_correct_r; try easy.
simpl; rewrite -minusE; auto with zarith.
intros K1.
rewrite concatF_correct_r; try easy.
f_equal; apply ord_inj; simpl.
repeat rewrite -minusE.
apply eq_sym, Nat.sub_add_distr.
Qed.

Lemma concatF_assoc_l :
  ∀ {n1 n2 n3} (A1 : 'E^n1) (A2 : 'E^n2) (A3 : 'E^n3),
    concatF (concatF A1 A2) A3 =
      castF (Nat.add_assoc n1 n2 n3) (concatF A1 (concatF A2 A3)).
Proof. intros; rewrite concatF_assoc_r castF_can; easy. Qed.

Lemma concatF_castF_eq :
  ∀ {n1 m1 n2 m2 l} (A1 : 'E^n1) (B1 : 'E^m1) (A2 : 'E^n2) (B2 : 'E^m2)
      (H1 : n1 + m1= l) (H2 : n2 + m2=l) (H3 : n1 = n2) (H4 : m1 = m2),
    castF H3 A1 = A2 → castF H4 B1 = B2 →
    castF H1 (concatF A1 B1) = castF H2 (concatF A2 B2).
Proof.
intros n1 m1 n2 m2 l A1 B1 A2 B2 H1 H2 H3 H4 M1 M2; extF i; unfold castF.
destruct (lt_dec i n1) as [Hi | Hi].
rewrite 2!concatF_correct_l; try easy.
simpl; rewrite -H3; auto with arith.
intros K1.
rewrite -M1; unfold castF.
f_equal; apply ord_inj; simpl; easy.
rewrite 2!concatF_correct_r; try easy.
simpl; rewrite -H3; auto with arith.
intros K1.
rewrite -M2; unfold castF.
f_equal; apply ord_inj; simpl; rewrite H3; easy.
Qed.

End ConcatF_Facts.

Section InsertF_Facts.

Context {E : Type}.

Properties of operators insertF/insert2F.

Lemma insertF_eq_gen :
  ∀ {n i0 j0} x0 y0 (A B : 'E^n),
    i0 = j0 → x0 = y0 → A = B → insertF i0 x0 A = insertF j0 y0 B.
Proof. intros; f_equal; easy. Qed.

Lemma insertF_eq :
  ∀ {n} i0 x0 y0 (A B : 'E^n),
    x0 = y0 → A = B → insertF i0 x0 A = insertF i0 y0 B.
Proof. intros; f_equal; easy. Qed.

Lemma insertF_inj_l :
  ∀ {n} i0 x0 y0 (A B : 'E^n),
    insertF i0 x0 A = insertF i0 y0 B → x0 = y0.
Proof.
move⇒ n i0 x0 y0 A B /extF_rev H; specialize (H i0); simpl in H.
rewrite → 2!insertF_correct_l in H; easy.
Qed.

Lemma insertF_inj_r :
  ∀ {n} i0 x0 y0 (A B : 'E^n),
    insertF i0 x0 A = insertF i0 y0 B → A = B.
Proof.
move⇒ n i0 x0 y0 A B /extF_rev H; extF i; specialize (H (lift i0 i));
    destruct (lt_dec (lift i0 i) i0) as [Hi' | Hi'].
rewrite 2!insertF_correct_rl in H; replace (narrow_S _) with i in H; try easy.
apply ord_inj; rewrite narrow_S_correct lift_l; try easy.
apply lift_lt_l; easy.
apply Nat.nlt_ge in Hi'; destruct (le_lt_eq_dec _ _ Hi') as [Hi'' | Hi''].
rewrite 2!insertF_correct_rr in H; replace (lower_S _) with i in H; try easy.
apply ord_inj; rewrite lower_S_correct lift_r; try easy.
apply lift_lt_r; easy.
contradict Hi''; apply lift_m.
Qed.

Lemma insertF_inj :
  ∀ {n} i0 x0 y0 (A B : 'E^n),
    insertF i0 x0 A = insertF i0 y0 B → x0 = y0 ∧ A = B.
Proof.
move=>> H; split; [eapply insertF_inj_l | eapply insertF_inj_r]; apply H.
Qed.

Lemma insertF_nextF_compat_l :
  ∀ {n} i0 {x0 y0} (A B : 'E^n),
    x0 ≠ y0 → insertF i0 x0 A ≠ insertF i0 y0 B.
Proof. move=>> H; contradict H; apply insertF_inj in H; easy. Qed.

Lemma insertF_nextF_compat_r :
  ∀ {n} i0 x0 y0 {A B : 'E^n},
    A ≠ B → insertF i0 x0 A ≠ insertF i0 y0 B.
Proof. move=>> H; contradict H; apply insertF_inj in H; easy. Qed.

Lemma insertF_nextF_reg :
  ∀ {n} i0 {x0 y0} {A B : 'E^n},
    insertF i0 x0 A ≠ insertF i0 y0 B → x0 ≠ y0 ∨ A ≠ B.
Proof.
move=>> H; apply not_and_or; contradict H; apply insertF_eq; easy.
Qed.

Lemma insertF_nextF_equiv :
  ∀ {n} i0 {x0 y0} {A B : 'E^n},
    insertF i0 x0 A ≠ insertF i0 y0 B ↔ x0 ≠ y0 ∨ A ≠ B.
Proof.
intros; split; [apply insertF_nextF_reg | intros [H | H]];
    [apply insertF_nextF_compat_l | apply insertF_nextF_compat_r]; easy.
Qed.

Lemma insertF_constF :
  ∀ {n} i0 (x : E), insertF i0 x (constF n x) = constF n.+1 x.
Proof.
intros n i0 x; extF i; destruct (ord_eq_dec i i0).
rewrite → insertF_correct_l, constF_correct; easy.
rewrite insertF_correct_r 2!constF_correct; easy.
Qed.

Lemma insertF_singleF_0 :
  ∀ (x0 x1 : E), insertF0 x0 (singleF x1) = coupleF x0 x1.
Proof.
intros; extF i; destruct (ord2_dec i) as [Hi | Hi]; rewrite Hi.
rewrite coupleF_0; try apply insertF_correct_l; easy.
rewrite coupleF_1; try rewrite insertF_correct_rr; try apply /ltP; easy.
Qed.

Lemma insertF_singleF_1 :
  ∀ (x0 x1 : E), insertFmax x1 (singleF x0) = coupleF x0 x1.
Proof.
intros; extF i; destruct (ord2_dec i) as [Hi | Hi]; rewrite Hi.
rewrite coupleF_0; try rewrite insertF_correct_rl; try apply /ltP; easy.
rewrite coupleF_1; try apply insertF_correct_l; easy.
Qed.

Lemma insertF_coupleF_0 :
  ∀ (x0 x1 x2 : E), insertF0 x0 (coupleF x1 x2) = tripleF x0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite tripleF_0; try apply insertF_correct_l; easy.
rewrite tripleF_1 insertF_correct_rr (extF_compat _ _ ord0);
    [apply coupleF_0 | apply ord_inj; easy].
rewrite tripleF_2 insertF_correct_rr; [now apply /ltP |].
intros; erewrite extF_compat; [apply coupleF_1 | apply ord_inj]; easy.
Qed.

Lemma insertF_coupleF_1 :
  ∀ (x0 x1 x2 : E), insertF ord1 x1 (coupleF x0 x2) = tripleF x0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite tripleF_0 insertF_correct_rl (extF_compat _ _ ord0);
    [apply coupleF_0 | apply ord_inj; easy].
rewrite tripleF_1; try apply insertF_correct_l; easy.
rewrite tripleF_2 insertF_correct_rr (extF_compat _ _ ord_max);
    [apply coupleF_1 | apply ord_inj; easy].
Qed.

Lemma insertF_coupleF_2 :
  ∀ (x0 x1 x2 : E), insertFmax x2 (coupleF x0 x1) = tripleF x0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite tripleF_0; try easy; rewrite insertF_correct_rl; [now apply /ltP |].
intros; erewrite extF_compat; [apply coupleF_0 | apply ord_inj]; easy.
rewrite tripleF_1 insertF_correct_rl (extF_compat _ _ ord_max);
    [apply coupleF_1 | apply ord_inj; easy].
rewrite tripleF_2; try apply insertF_correct_l; easy.
Qed.

Lemma insertF_monot_inclF :
  ∀ (PE : E → Prop) {n} i0 x0 (A : 'E^n),
    inclF A PE → PE x0 → inclF (insertF i0 x0 A) PE.
Proof.
intros PE n i0 x0 A HA Hx0 i; destruct (ord_eq_dec i i0) as [Hi | Hi].
rewrite insertF_correct_l; easy.
rewrite insertF_correct_r; auto.
Qed.

Lemma insertF_monot_invalF_l :
  ∀ {n1 n2} i0 x0 (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A1 A2 → inF x0 A2 → invalF (insertF i0 x0 A1) A2.
Proof.
intros n1 n2 i0 x0 A1 A2 HA [i2 Hi2] i1;
    destruct (ord_eq_dec i1 i0) as [Hi1 | Hi1].
∃ i2; rewrite Hi2; apply insertF_correct_l; easy.
destruct (HA (insert_ord Hi1)) as [k2 Hk2].
∃ k2; rewrite -Hk2; apply insertF_correct_r.
Qed.

Lemma insertF_monot_invalF_r :
  ∀ {n1 n2} i0 x0 (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A1 A2 → invalF A1 (insertF i0 x0 A2).
Proof.
intros n1 n2 i0 x0 A1 A2 HA i1; destruct (HA i1) as [j2 Hj2].
∃ (skip_ord i0 j2); rewrite insertF_correct; easy.
Qed.

Lemma insertF_concatF_0 :
  ∀ {n} x0 (A : 'E^n),
    insertF0 x0 A = castF_1pS (concatF (singleF x0) A).
Proof.
intros; unfold castF_1pS; rewrite castF_id.
extF i; destruct (ord_eq_dec i ord0) as [Hi | Hi].
rewrite Hi insertF_correct_l// concatF_correct_l; easy.
assert (Hi' : ¬ (cast_ord (eq_sym (add1n n)) i < 1)%coq_nat)
    by now rewrite cast_ord_id; apply ord_n0_nlt_equiv.
rewrite insertF_correct_r insert_concat_r_ord_0 concatF_correct_r.
f_equal; apply ord_inj; easy.
Qed.

Lemma insertF_concatF_max :
  ∀ {n} x0 (A : 'E^n),
    insertFmax x0 A = castF_p1S (concatF A (singleF x0)).
Proof.
intros n x0 A; unfold castF_p1S, castF.
extF i; destruct (ord_eq_dec i ord_max) as [Hi | Hi].
rewrite Hi insertF_correct_l// concatF_correct_r singleF_0; easy.
assert (Hi' : (cast_ord (eq_sym (addn1 n)) i < n)%coq_nat)
    by now apply ord_nmax_lt_equiv.
rewrite insertF_correct_r insert_concat_l_ord_max concatF_correct_l; easy.
Qed.

Lemma insertF_concatF_l :
  ∀ {n1 n2} {i0 : [0..n1 + n2]} (H : (i0 ≤ n1)%coq_nat)
      x0 (A1 : 'E^n1) (A2 : 'E^n2),
    insertF i0 x0 (concatF A1 A2) =
      concatF (insertF (@concat_l_ord n1.+1 n2 _ (nat_le_ltS H)) x0 A1) A2.
Proof.
intros n1 n2 i0 H x0 A1 A2; extF i; destruct (lt_dec i n1.+1) as [Hi1 | Hi1].
rewrite concatF_correct_l.
destruct (nat_lt_eq_gt_dec i i0) as [[Hi0 | Hi0] | Hi0].
rewrite 2!insertF_correct_rl concatF_correct_l; try simpl; auto with zarith.
intros; f_equal; apply ord_inj; easy.
rewrite → 2!insertF_correct_l; try apply ord_inj; easy.
assert (Hi1' : (lower_S (ord_n0_gt Hi0) < n1)%coq_nat)
    by (simpl; rewrite -minusE; auto with zarith).
rewrite 2!insertF_correct_rr concatF_correct_l.
f_equal; apply ord_inj; easy.
assert (H0a : (i0 < i)%coq_nat) by auto with zarith.
assert (H0b : i ≠ i0) by (apply ord_neq; auto with zarith).
assert (H' : ¬ (insert_ord H0b < n1)%coq_nat).
  rewrite insert_ord_correct_r; simpl; rewrite -minusE; auto with zarith.
rewrite insertF_correct_r 2!concatF_correct_r.
f_equal; apply ord_inj; simpl; rewrite insert_ord_correct_r; simpl.
rewrite -subnDA add1n; easy.
Qed.

Lemma insertF_concatF_r :
  ∀ {n1 n2} {i0 : [0..n1 + n2]}
      (H : ¬ (cast_ord (eq_sym (addnS n1 n2)) i0 < n1)%coq_nat)
      x0 (A1 : 'E^n1) (A2 : 'E^n2),
    insertF i0 x0 (concatF A1 A2) =
      castF (addnS n1 n2) (concatF A1 (insertF (concat_r_ord H) x0 A2)).
Proof.
intros n1 n2 i0 H x0 A1 A2; unfold castF;
    extF i; destruct (lt_dec i n1) as [Hi1 | Hi1].
assert (H0 : (i < i0)%coq_nat) by (simpl in H; auto with zarith).
rewrite insertF_correct_rl 2!concatF_correct_l; f_equal; apply ord_inj; easy.
rewrite concatF_correct_r.
destruct (nat_lt_eq_gt_dec i (cast_ord (eq_sym (addnS _ _)) i0))
    as [[Hi0 | Hi0] | Hi0].
rewrite 2!insertF_correct_rl;
    try (simpl in *; rewrite -minusE; auto with zarith arith).
intros; rewrite concatF_correct_r; f_equal; apply ord_inj; easy.
rewrite → 2!insertF_correct_l; try apply ord_inj; simpl; try rewrite Hi0; easy.
assert (Hi1' : ¬ (lower_S (ord_n0_gt Hi0) < n1)%coq_nat)
    by (simpl; rewrite -minusE; auto with zarith).
rewrite 2!insertF_correct_rr;
    try (simpl in *; rewrite -minusE; auto with zarith).
intros; rewrite concatF_correct_r; f_equal; apply ord_inj; simpl.
rewrite -minusE; auto with zarith.
Qed.

Lemma insert2F_sym :
  ∀ {n i0 i1 x0 x1} {H10 : i1 ≠ i0} (H01 : i0 ≠ i1) (A : 'E^n),
    insert2F H10 x0 x1 A = insert2F H01 x1 x0 A.
Proof.
intros; rewrite insert2F_correct (insert2F_equiv_def _ _).
do 2 f_equal; apply insert_ord_compat_P.
Qed.

Lemma insert2F_sym_alt :
  ∀ {n i0 i1} {H : i1 ≠ i0} {x0 x1} (A : 'E^n),
    insert2F H x0 x1 A = insert2F (not_eq_sym H) x1 x0 A.
Proof. intros; apply insert2F_sym. Qed.

Lemma insert2F_eq_P :
  ∀ {n i0 i1} (H H' : i1 ≠ i0) x0 x1 (A : 'E^n),
    insert2F H x0 x1 A = insert2F H' x0 x1 A.
Proof. intros; rewrite 2!insert2F_correct insert_ord_compat_P; easy. Qed.

Lemma insert2F_eq_gen :
  ∀ {n i0 i1 j0 j1} (Hi : i1 ≠ i0) (Hj : j1 ≠ j0)
      x0 x1 y0 y1 (A B : 'E^n),
    i0 = j0 → i1 = j1 → x0 = y0 → x1 = y1 → A = B →
    insert2F Hi x0 x1 A = insert2F Hj y0 y1 B.
Proof. intros; subst; apply insert2F_eq_P. Qed.

Lemma insert2F_eq :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 (A B : 'E^n),
    x0 = y0 → x1 = y1 → A = B → insert2F H x0 x1 A = insert2F H y0 y1 B.
Proof. intros; apply insert2F_eq_gen; easy. Qed.

Lemma insert2F_inj_l0 :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 (A B : 'E^n),
    insert2F H x0 x1 A = insert2F H y0 y1 B → x0 = y0.
Proof.
move=>> H; rewrite 2!insert2F_correct in H; eapply insertF_inj_l, H.
Qed.

Lemma insert2F_inj_l1 :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 (A B : 'E^n),
    insert2F H x0 x1 A = insert2F H y0 y1 B → x1 = y1.
Proof.
move=>> H; rewrite 2!insert2F_correct in H; apply insertF_inj_r in H.
eapply insertF_inj_l, H.
Qed.

Lemma insert2F_inj_r :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 (A B : 'E^n),
    insert2F H x0 x1 A = insert2F H y0 y1 B → A = B.
Proof.
move=>> H; rewrite 2!insert2F_correct in H; apply insertF_inj_r in H.
eapply insertF_inj_r, H.
Qed.

Lemma insert2F_inj :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 (A B : 'E^n),
    insert2F H x0 x1 A = insert2F H y0 y1 B → x0 = y0 ∧ x1 = y1 ∧ A = B.
Proof.
move=>> H; repeat split;
    [eapply insert2F_inj_l0 | eapply insert2F_inj_l1 | eapply insert2F_inj_r];
    apply H.
Qed.

Lemma insert2F_nextF_compat_l0 :
  ∀ {n i0 i1} (H : i1 ≠ i0) {x0} x1 {y0} y1 (A B : 'E^n),
    x0 ≠ y0 → insert2F H x0 x1 A ≠ insert2F H y0 y1 B.
Proof. move=>> H; contradict H; apply insert2F_inj_l0 in H; easy. Qed.

Lemma insert2F_nextF_compat_l1 :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 {x1} y0 {y1} (A B : 'E^n),
    x1 ≠ y1 → insert2F H x0 x1 A ≠ insert2F H y0 y1 B.
Proof. move=>> H; contradict H; apply insert2F_inj_l1 in H; easy. Qed.

Lemma insert2F_nextF_compat_r :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 y0 y1 {A B : 'E^n},
    A ≠ B → insert2F H x0 x1 A ≠ insert2F H y0 y1 B.
Proof. move=>> H; contradict H; apply insert2F_inj in H; easy. Qed.

Lemma insert2F_nextF_reg :
  ∀ {n i0 i1} (H : i1 ≠ i0) {x0 x1 y0 y1} {A B : 'E^n},
    insert2F H x0 x1 A ≠ insert2F H y0 y1 B → x0 ≠ y0 ∨ x1 ≠ y1 ∨ A ≠ B.
Proof.
move=>> H; apply not_and3_equiv; contradict H; apply insert2F_eq; easy.
Qed.

Lemma insert2F_nextF_equiv :
  ∀ {n i0 i1} (H : i1 ≠ i0) {x0 x1 y0 y1} {A B : 'E^n},
    insert2F H x0 x1 A ≠ insert2F H y0 y1 B ↔
    x0 ≠ y0 ∨ x1 ≠ y1 ∨ A ≠ B.
Proof.
intros; split; [apply insert2F_nextF_reg | intros [H1 | [H1 | H1]]];
    [apply insert2F_nextF_compat_l0 | apply insert2F_nextF_compat_l1 |
    apply insert2F_nextF_compat_r]; easy.
Qed.

Lemma insert2F_singleF_01 :
  ∀ (x0 x1 x2 : E),
    insert2F ord_1_not_0 x0 x1 (singleF x2) = tripleF x0 x1 x2.
Proof.
intros; rewrite insert2F_correct; replace (insert_ord _) with (@ord0 1).
rewrite insertF_singleF_0 insertF_coupleF_0; easy.
rewrite (insert_ord_correct_r _ ord_lt_0_1); apply ord_inj; easy.
Qed.

Lemma insert2F_singleF_02 :
  ∀ (x0 x1 x2 : E),
    insert2F ord_max_not_0 x0 x2 (singleF x1) = tripleF x0 x1 x2.
Proof.
intros; rewrite insert2F_correct; replace (insert_ord _) with (@ord_max 1).
rewrite insertF_singleF_1 insertF_coupleF_0; easy.
rewrite (insert_ord_correct_r _ ord_lt_0_max); apply ord_inj; easy.
Qed.

Lemma insert2F_singleF_12 :
  ∀ (x0 x1 x2 : E),
    insert2F ord_max_not_1 x1 x2 (singleF x0) = tripleF x0 x1 x2.
Proof.
intros; rewrite insert2F_correct; replace (insert_ord _) with (@ord_max 1).
rewrite insertF_singleF_1 insertF_coupleF_1; easy.
rewrite (insert_ord_correct_r _ ord_lt_1_max); apply ord_inj; easy.
Qed.

End InsertF_Facts.

Section SkipF_Facts.

Context {E : Type}.

Properties of operators skipF/skip2F.

Lemma skipF_constF :
  ∀ {n} i0 (x : E), skipF i0 (constF n.+1 x) = constF n x.
Proof. easy. Qed.

Lemma skipF_concatF :
  ∀ {n} i0 (A : 'E^n.+1),
    castF (ord_split i0) (skipF i0 A) =
      concatF (firstF (castF (ord_splitS i0) A))
              (lastF (castF (ordS_splitS i0) A)).
Proof.
intros; rewrite (concatF_splitF (castF _ (skipF _ _))).
rewrite firstF_skipF lastF_skipF; easy.
Qed.

Lemma skipF_eq_gen :
  ∀ {n} i0 j0 (A B : 'E^n.+1),
    i0 = j0 → eqxF i0 A B → skipF i0 A = skipF j0 B.
Proof.
intros n i0 j0 A B Hi H; rewrite -Hi.
apply (castF_inj (ord_split i0)); rewrite 2!skipF_concatF.
apply concatF_eq; [apply firstF_compat | apply lastF_compat];
    intros; apply H; [apply ord_lt_neq | apply ord_lt_neq_sym]; easy.
Qed.

Lemma skipF_eq :
  ∀ {n} i0 (A B : 'E^n.+1), eqxF i0 A B → skipF i0 A = skipF i0 B.
Proof. intros; apply skipF_eq_gen; easy. Qed.

Lemma skipF_reg :
  ∀ {n} i0 (A B : 'E^n.+1), skipF i0 A = skipF i0 B → eqxF i0 A B.
Proof. move=>> /extF_rev H i Hi; rewrite -(skip_insert_ord Hi); apply H. Qed.

Lemma eqxF_equiv :
  ∀ {n} i0 (A B : 'E^n.+1), eqxF i0 A B ↔ skipF i0 A = skipF i0 B.
Proof. intros; split. intros; apply skipF_eq; easy. apply skipF_reg. Qed.

Lemma skipF_neqxF_compat :
  ∀ {n i0} {A B : 'E^n.+1}, neqxF i0 A B → skipF i0 A ≠ skipF i0 B.
Proof.
move=>>; rewrite contra_not_r_equiv -eqxF_not_equiv; apply skipF_reg.
Qed.

Lemma skipF_neqxF_reg :
  ∀ {n i0} {A B : 'E^n.+1}, skipF i0 A ≠ skipF i0 B → neqxF i0 A B.
Proof.
move=>>; rewrite contra_not_l_equiv -eqxF_not_equiv eqxF_equiv; easy.
Qed.

Lemma neqxF_equiv :
  ∀ {n i0} {A B : 'E^n.+1}, neqxF i0 A B ↔ skipF i0 A ≠ skipF i0 B.
Proof.
intros; split. intros; apply skipF_neqxF_compat; easy. apply skipF_neqxF_reg.
Qed.

Lemma PAF_ind_skipF :
  ∀ {n i0} {P : 'Prop^n.+1}, P i0 → PAF (skipF i0 P) → PAF P.
Proof.
intros n i0 P H0 H1 i; destruct (ord_eq_dec i i0) as [-> | Hi]; [easy |].
rewrite -(skip_insert_ord Hi); apply H1.
Qed.

Lemma extF_skipF :
  ∀ {n} i0 {A B : 'E^n.+1},
    A i0 = B i0 → skipF i0 A = skipF i0 B → A = B.
Proof. move=>>; rewrite -eqxF_equiv; apply eqxF_reg. Qed.

Lemma extF_skipF_equiv :
  ∀ {n} i0 {A B : 'E^n.+1},
    A = B ↔ A i0 = B i0 ∧ skipF i0 A = skipF i0 B.
Proof.
intros n i0 A B; split; intros H; [subst | apply (extF_skipF i0)]; easy.
Qed.

Lemma nextF_skipF :
  ∀ {n} i0 {A B : 'E^n.+1},
    A ≠ B → A i0 ≠ B i0 ∨ skipF i0 A ≠ skipF i0 B.
Proof.
move=>>; rewrite -not_and_equiv -contra_equiv;
    move⇒ [H1 H2]; move: H1 H2; apply extF_skipF.
Qed.

Lemma nextF_skipF_equiv :
  ∀ {n} i0 {A B : 'E^n.+1},
    A ≠ B ↔ A i0 ≠ B i0 ∨ skipF i0 A ≠ skipF i0 B.
Proof.
move=>>; rewrite -not_and_equiv -iff_not_equiv; apply extF_skipF_equiv.
Qed.

Lemma skipF_inj :
  ∀ {n} i0 (A : 'E^n.+1), injective A → injective (skipF i0 A).
Proof. move=>> HA j1 j2 /HA; apply skip_ord_inj. Qed.

Lemma skipF_invalF :
  ∀ {n1 n2} i1 i2 (A1 : 'E^n1.+1) (A2 : 'E^n2.+1),
    injective A1 → A1 i1 = A2 i2 →
    invalF A1 A2 → invalF (skipF i1 A1) (skipF i2 A2).
Proof.
intros n1 n2 i1 i2 A1 A2 HA1 HAi HA j1.
destruct (HA (skip_ord i1 j1)) as [j2 Hj2a].
assert (Hj2b : j2 ≠ i2).
  intros Hj2b; apply (skip_ord_correct_m i1 j1), HA1.
  rewrite HAi -Hj2b; easy.
∃ (insert_ord Hj2b); rewrite skipF_correct; easy.
Qed.

Lemma skipF_invalF_rev :
  ∀ {n1 n2} i1 i2 (A1 : 'E^n1.+1) (A2 : 'E^n2.+1),
    A1 i1 = A2 i2 → invalF (skipF i1 A1) (skipF i2 A2) → invalF A1 A2.
Proof.
intros n1 n2 i1 i2 A1 A2 HAi HA j1.
destruct (ord_eq_dec j1 i1) as [Hj1 | Hj1].
∃ i2; rewrite Hj1; easy.
destruct (HA (insert_ord Hj1)) as [j2 Hj2]; rewrite skipF_correct in Hj2.
∃ (skip_ord i2 j2); easy.
Qed.

Lemma skipF_invalF_equiv :
  ∀ {n1 n2} i1 i2 (A1 : 'E^n1.+1) (A2 : 'E^n2.+1),
    injective A1 → A1 i1 = A2 i2 →
    invalF (skipF i1 A1) (skipF i2 A2) ↔ invalF A1 A2.
Proof.
move=>> HA1 Hi; split; [apply skipF_invalF_rev | apply skipF_invalF]; easy.
Qed.

Lemma skipF_1 : ∀ (A : 'E^1), skipF0 A = fun_from_I_0 E.
Proof. intros; extF i; destruct i; easy. Qed.

Lemma skipF_one :
  ∀ {n} (H : n = 0) (A : 'E^n.+1),
    skipF0 A = castF (eq_sym H) (fun_from_I_0 E).
Proof. intros; subst; rewrite castF_refl; apply skipF_1. Qed.

Lemma skipF_2l : ∀ (A : 'E^2), skipF0 A = singleF (A ord_max).
Proof.
intros; extF; rewrite ord_one singleF_0 skipF_correct_r; [| easy].
rewrite liftF_S_0 ord2_1_max; easy.
Qed.

Lemma skipF_2l0 : ∀ (A : 'E^2), skipF0 A ord0 = A ord_max.
Proof. intros; rewrite skipF_2l; easy. Qed.

Lemma skipF_2r : ∀ (A : 'E^2), skipFmax A = singleF (A ord0).
Proof.
intros; extF; rewrite ord_one singleF_0 skipF_correct_l; try now apply /ltP.
apply widenF_S_0.
Qed.

Lemma skipF_2r0 : ∀ (A : 'E^2), skipFmax A ord0 = A ord0.
Proof. intros; rewrite skipF_2r; easy. Qed.

Lemma skipF_3l : ∀ (A : 'E^3), skipF0 A = coupleF (A ord1) (A ord_max).
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite coupleF_0 skipF_correct_r; [apply liftF_S_0 | easy].
rewrite coupleF_1 skipF_correct_r; [apply liftF_S_max | easy].
Qed.

Lemma skipF_3l0 : ∀ (A : 'E^3), skipF0 A ord0 = A ord1.
Proof. intros; rewrite skipF_3l coupleF_0; easy. Qed.

Lemma skipF_3l1 : ∀ (A : 'E^3), skipF0 A ord_max = A ord_max.
Proof. intros; rewrite skipF_3l coupleF_1; easy. Qed.

Lemma skipF_3m :
  ∀ (A : 'E^3), skipF ord1 A = coupleF (A ord0) (A ord_max).
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite coupleF_0 skipF_correct_l; [apply widenF_S_0 | now apply /ltP].
rewrite coupleF_1 skipF_correct_r; [apply liftF_S_max | now apply /ltP].
Qed.

Lemma skipF_3m0 : ∀ (A : 'E^3), skipF ord1 A ord0 = A ord0.
Proof. intros; rewrite skipF_3m coupleF_0; easy. Qed.

Lemma skipF_3m1 : ∀ (A : 'E^3), skipF ord1 A ord_max = A ord_max.
Proof. intros; rewrite skipF_3m coupleF_1; easy. Qed.

Lemma skipF_3r : ∀ (A : 'E^3), skipFmax A = coupleF (A ord0) (A ord1).
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite coupleF_0 skipF_correct_l; [apply widenF_S_0 | now apply /ltP].
rewrite coupleF_1 skipF_correct_l;
    [rewrite widenF_S_max ord3_1_pred_max | apply /ltP]; easy.
Qed.

Lemma skipF_3r0 : ∀ (A : 'E^3), skipFmax A ord0 = A ord0.
Proof. intros; rewrite skipF_3r coupleF_0; easy. Qed.

Lemma skipF_3r1 : ∀ (A : 'E^3), skipFmax A ord_max = A ord1.
Proof. intros; rewrite skipF_3r coupleF_1; easy. Qed.

Lemma skipF_singleF : ∀ (x0 : E), skipF0 (singleF x0) = fun_from_I_0 E.
Proof. intros; apply skipF_1. Qed.

Lemma skipF_coupleF_l :
  ∀ (x0 x1 : E), skipF0 (coupleF x0 x1) = singleF x1.
Proof. intros; rewrite skipF_2l coupleF_1; easy. Qed.

Lemma skipF_coupleF_r :
  ∀ (x0 x1 : E), skipFmax (coupleF x0 x1) = singleF x0.
Proof. intros; rewrite skipF_2r coupleF_0; easy. Qed.

Lemma skipF_tripleF_l :
  ∀ (x0 x1 x2 : E), skipF0 (tripleF x0 x1 x2) = coupleF x1 x2.
Proof. intros; rewrite skipF_3l tripleF_1 tripleF_2; easy. Qed.

Lemma skipF_tripleF_m :
  ∀ (x0 x1 x2 : E), skipF ord1 (tripleF x0 x1 x2) = coupleF x0 x2.
Proof. intros; rewrite skipF_3m tripleF_0 tripleF_2; easy. Qed.

Lemma skipF_tripleF_r :
  ∀ (x0 x1 x2 : E), skipFmax (tripleF x0 x1 x2) = coupleF x0 x1.
Proof. intros; rewrite skipF_3r tripleF_0 tripleF_1; easy. Qed.

Lemma skipF_not_inF :
  ∀ {n} (A : 'E^n.+1),
    injective A → ∀ i0, ¬ inF (A i0) (skipF i0 A).
Proof.
intros n A HA i0 [j Hj]; destruct (lt_dec j i0) as [H | H].
rewrite skipF_correct_l in Hj; try easy.
apply HA in Hj; rewrite Hj in H; contradict H; apply Nat.lt_irrefl.
rewrite skipF_correct_r in Hj; try easy.
apply HA in Hj; rewrite Hj in H; contradict H; auto.
Qed.

Lemma skipF_not_inF_rev :
  ∀ {n} (A : 'E^n.+1),
    (∀ i0, ¬ inF (A i0) (skipF i0 A)) → injective A.
Proof.
intros n A HA; apply inj_contra_rev; intros i j H.
specialize (HA j); rewrite not_ex_all_not_equiv in HA.
rewrite -(skipF_correct H) eq_sym_equiv; apply HA.
Qed.

Lemma skipF_not_inF_equiv :
  ∀ {n} (A : 'E^n.+1),
    (∀ i0, ¬ inF (A i0) (skipF i0 A)) ↔ injective A.
Proof. intros; split; [apply skipF_not_inF_rev | apply skipF_not_inF]. Qed.

Lemma skipF_monot_l :
  ∀ {n1 n2} i (A1 : 'E^n1.+1) (A2 : 'E^n2),
    invalF A1 A2 → invalF (skipF i A1) A2.
Proof.
intros n1 n2 i A1 A2 HA i1; destruct (HA (skip_ord i i1)) as [j2 Hj2].
∃ j2; easy.
Qed.

Lemma skipF_monot_r :
  ∀ {n1 n2} i (A1 : 'E^n1) (A2 : 'E^n2.+1),
    invalF A1 (skipF i A2) → invalF A1 A2.
Proof.
intros n1 n2 i A1 A2 HA i1; destruct (HA i1) as [j2 Hj2].
∃ (skip_ord i j2); easy.
Qed.

Lemma skipF_insertF :
  ∀ {n} i0 x0 (A : 'E^n), skipF i0 (insertF i0 x0 A) = A.
Proof.
intros n i0 x0 A; extF i; destruct (lt_dec i i0) as [Hi | Hi].
rewrite skipF_correct_l; try easy; unfold widenF_S.
rewrite insertF_correct_rl narrow_widen_S; easy.
assert (Hia : (i0 < lift_S i)%coq_nat)
    by (rewrite lift_S_correct; auto with zarith).
rewrite skipF_correct_r; try easy; unfold liftF_S.
rewrite insertF_correct_rr lower_lift_S; easy.
Qed.

Lemma insertF_skipF :
  ∀ {n} i0 (A : 'E^n.+1), insertF i0 (A i0) (skipF i0 A) = A.
Proof.
intros n i0 A; extF i; destruct (nat_lt_eq_gt_dec i0 i) as [[H0 | H0] | H0].
2: apply ord_inj in H0; rewrite -H0 insertF_correct_l; easy.
assert (H0' : ¬ (lower_S (ord_n0_gt H0) < i0)%coq_nat).
  rewrite lower_S_correct; auto with zarith.
rewrite insertF_correct_rr skipF_correct_r; try easy; apply liftF_lower_S.
assert (H0' : (narrow_S (ord_nmax_lt H0) < i0)%coq_nat)
    by now rewrite narrow_S_correct.
rewrite insertF_correct_rl skipF_correct_l; try easy; apply widenF_narrow_S.
Qed.

Lemma widenF_S_insertF_max :
  ∀ {n} x (A : 'E^n), widenF_S (insertFmax x A) = A.
Proof.
intros; extF; rewrite -(skipF_correct_l ord_max);
    [rewrite skipF_insertF | apply /ltP]; easy.
Qed.

Lemma insertF_max_widenF_S :
  ∀ {n} (A : 'E^n.+1), insertFmax (A ord_max) (widenF_S A) = A.
Proof.
intros; extF i; destruct (ord_eq_dec i ord_max) as [Hi | Hi].
rewrite Hi insertF_correct_l; easy.
apply widenF_S_reg; try rewrite widenF_S_insertF_max; easy.
Qed.

Lemma liftF_S_insertF_0 :
  ∀ {n} x (A : 'E^n), liftF_S (insertF0 x A) = A.
Proof.
intros; extF; rewrite -(skipF_correct_r ord0);
    [rewrite skipF_insertF | apply /ltP]; easy.
Qed.

Lemma insertF_0_liftF_S :
  ∀ {n} (A : 'E^n.+1), insertF0 (A ord0) (liftF_S A) = A.
Proof.
intros; extF i; destruct (ord_eq_dec i ord0) as [Hi | Hi].
rewrite Hi insertF_correct_l; easy.
apply liftF_S_reg; try rewrite liftF_S_insertF_0; easy.
Qed.

Lemma insertF_skipF_comm :
  ∀ {n j0 j1 i0 i1} x1 (A : 'E^n.+1),
    i0 = skip_ord i1 j0 → i1 = skip_ord i0 j1 →
    insertF j1 x1 (skipF j0 A) = skipF i0 (insertF i1 x1 A).
Proof.
intros n j0 j1 i0 i1 x1 A Hi0 Hi1; extF j; unfold insertF, skipF.
destruct (ord_eq_dec j j1) as [Hj1 | Hj1],
    (ord_eq_dec (skip_ord i0 j) i1) as [Hji | Hji]; try easy.
contradict Hji; rewrite Hj1; easy.
contradict Hj1; apply (skip_ord_inj i0); rewrite Hji; easy.
rewrite (skip_insert_ord_gen _ _ _ Hi0 Hi1); easy.
Qed.

Lemma skipF_insertF_comm :
  ∀ {n i0 i1} (Hi : i1 ≠ i0) x0 (A : 'E^n.+1),
    let j1 := insert_ord Hi in
    let j0 := insert_ord (not_eq_sym Hi) in
    skipF i1 (insertF i0 x0 A) = insertF j0 x0 (skipF j1 A).
Proof.
intros n i0 i1 Hi x0 A j1 j0.
assert (Hi0 : i0 = skip_ord i1 j0) by now unfold j0; rewrite skip_insert_ord.
assert (Hi1 : i1 = skip_ord i0 j1) by now unfold j1; rewrite skip_insert_ord.
rewrite (insertF_skipF_comm _ _ Hi1 Hi0); easy.
Qed.

Lemma insertF_Rg :
  ∀ {n} i0 x0, Rg (insertF i0 x0) = fun (A : 'E^n.+1) ⇒ A i0 = x0.
Proof.
intros n i0 x0; apply subset_ext_equiv; split.
intros _ [v _]; apply insertF_correct_l; easy.
intros u Hu; rewrite image_ex; ∃ (skipF i0 u); split; [easy |].
rewrite -Hu insertF_skipF; easy.
Qed.

Lemma skipF_ex :
  ∀ {n} i0 x0 (A : 'E^n), ∃ B, B i0 = x0 ∧ skipF i0 B = A.
Proof.
intros n i0 x0 A; ∃ (insertF i0 x0 A); split.
rewrite insertF_correct_l; easy.
apply skipF_insertF.
Qed.

Lemma skipF_uniq :
  ∀ {n} i0 x0 (A : 'E^n), ∃! B, B i0 = x0 ∧ skipF i0 B = A.
Proof.
intros n i0 x0 A; destruct (skipF_ex i0 x0 A) as [B HB].
∃ B; split; try easy.
intros C [HC0 HC1]; apply (extF_skipF i0); [rewrite HC0 | rewrite HC1]; easy.
Qed.

Lemma skipF_first : ∀ {n} (A : 'E^n.+1), skipF0 A = liftF_S A.
Proof. intros; extF; apply skipF_correct_r; easy. Qed.

Lemma skipF_last : ∀ {n} (A : 'E^n.+1), skipFmax A = widenF_S A.
Proof. intros; extF; apply skipF_correct_l; apply /ltP; easy. Qed.

Lemma skipF_0 :
  ∀ {n i0} {A : 'E^n.+2}, i0 ≠ ord0 → skipF i0 A ord0 = A ord0.
Proof. intros; apply skipF_correct_alt; [| apply skip_ord_0]; easy. Qed.

Lemma skipF_max :
  ∀ {n i0} {A : 'E^n.+2},
    i0 ≠ ord_max → skipF i0 A ord_max = A ord_max.
Proof. intros; apply skipF_correct_alt; [| apply skip_ord_max]; easy. Qed.

Lemma skip2F_eq_P :
  ∀ {n i0 i1} {H : i1 ≠ i0} (H' : i1 ≠ i0) (A : 'E^n.+2),
    skip2F H A = skip2F H' A.
Proof. intros; unfold skip2F; rewrite skip2_ord_compat_P; easy. Qed.

Lemma skip2F_eq_lt :
  ∀ {n} {i0 i1 : [0..n.+1]} (H : (i0 < i1)%coq_nat) (A B : 'E^n.+2),
    eqx2F i0 i1 A B →
    skip2F (ord_lt_neq_sym H) A = skip2F (ord_lt_neq_sym H) B.
Proof.
intros n i0 i1 Hi A B H;
    rewrite 2!skip2F_correct; apply skipF_eq; try easy.
intros j Hj1; destruct (lt_dec j i0) as [Hj2 | Hj2].
rewrite → 2!skipF_correct_l; try easy; apply H; split.
contradict Hj2; apply Nat.nlt_ge; rewrite -Hj2; simpl; easy.
rewrite -(skip_ord_correct_l i0); try easy.
contradict Hj1; apply (skip_ord_inj i0); rewrite skip_insert_ord; easy.
rewrite → 2!skipF_correct_r; try easy; apply H; split.
contradict Hj2; rewrite -Hj2; simpl; rewrite bump_r; auto with arith.
rewrite -(skip_ord_correct_r i0); try easy.
contradict Hj1; apply (skip_ord_inj i0); rewrite skip_insert_ord; easy.
Qed.

Lemma skip2F_eq_gen :
  ∀ {n i0 i1 j0 j1} (Hi : i1 ≠ i0) (Hj : j1 ≠ j0) (A B : 'E^n.+2),
    i0 = j0 → i1 = j1 → eqx2F i0 i1 A B → skip2F Hi A = skip2F Hj B.
Proof.
intros n i0 i1 j0 j1 Hi Hj A B Hi0 Hi1 H; subst j0 j1.
destruct (nat_lt_eq_gt_dec i1 i0) as [[Hia | Hia] | Hia].
2: contradict Hi; apply ord_inj, eq_sym; easy.
rewrite 2!(skip2F_sym (ord_lt_neq_sym Hia)).
apply skip2F_eq_lt, eqx2F_sym_i; easy.
rewrite 2!(skip2F_eq_P (ord_lt_neq_sym Hia)).
apply skip2F_eq_lt; easy.
Qed.

Lemma skip2F_eq :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    eqx2F i0 i1 A B → skip2F H A = skip2F H B.
Proof. intros; apply skip2F_eq_gen; easy. Qed.

Lemma skip2F_reg :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    skip2F H A = skip2F H B → eqx2F i0 i1 A B.
Proof.
move=>> /extF_rev H i [H0 H1]; rewrite -(skip2_insert2_ord _ H0 H1); apply H.
Qed.

Lemma eqx2F_equiv :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    eqx2F i0 i1 A B ↔ skip2F H A = skip2F H B.
Proof. intros; split. intros; apply skip2F_eq; easy. apply skip2F_reg. Qed.

Lemma skip2F_neqx2F_compat :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    neqx2F i0 i1 A B → skip2F H A ≠ skip2F H B.
Proof.
move=>>; rewrite contra_not_r_equiv -eqx2F_not_equiv; apply skip2F_reg.
Qed.

Lemma skip2F_neqx2F_reg :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    skip2F H A ≠ skip2F H B → neqx2F i0 i1 A B.
Proof.
move=>>; rewrite contra_not_l_equiv -eqx2F_not_equiv -eqx2F_equiv; easy.
Qed.

Lemma neqx2F_equiv :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    neqx2F i0 i1 A B ↔ skip2F H A ≠ skip2F H B.
Proof.
intros; split;
    [intros; apply skip2F_neqx2F_compat; easy | apply skip2F_neqx2F_reg].
Qed.

Lemma skip2F_tripleF_01 :
  ∀ (A : 'E^3), skip2F ord_1_not_0 A = singleF (A ord_max).
Proof.
intros; extF; rewrite ord_one singleF_0 skip2F_correct skipF_correct_r.
rewrite liftF_S_0 skipF_3l ord2_1_max coupleF_1; easy.
rewrite (insert_ord_correct_r _ ord_lt_0_1); easy.
Qed.

Lemma skip2F_tripleF_02 :
  ∀ (A : 'E^3), skip2F ord_max_not_0 A = singleF (A ord1).
Proof.
intros; extF; rewrite ord_one singleF_0 skip2F_correct skipF_correct_l.
rewrite widenF_S_0 skipF_3l coupleF_0; easy.
rewrite (insert_ord_correct_r _ ord_lt_0_max) lower_S_correct;
    apply ord_lt_0_pred_max.
Qed.

Lemma skip2F_tripleF_12 :
  ∀ (A : 'E^3), skip2F ord_max_not_1 A = singleF (A ord0).
Proof.
intros; extF; rewrite ord_one singleF_0 skip2F_correct skipF_correct_l.
rewrite widenF_S_0 skipF_3m coupleF_0; easy.
rewrite (insert_ord_correct_r _ ord_lt_1_max) lower_S_correct;
    apply ord_lt_0_pred_max.
Qed.

Lemma PAF_ind_skip2F :
  ∀ {n i0 i1} (H : i1 ≠ i0) {P : 'Prop^n.+2},
    P i0 → P i1 → PAF (skip2F H P) → PAF P.
Proof.
intros n i0 i1 Hi P H0 H1 H2; rewrite skip2F_correct in H2.
rewrite -(skip_insert_ord Hi) in H1.
apply: (PAF_ind_skipF H0) (PAF_ind_skipF H1 H2).
Qed.

Lemma extF_skip2F :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A B : 'E^n.+2),
    A i0 = B i0 → A i1 = B i1 → skip2F H A = skip2F H B → A = B.
Proof. move=>>; rewrite -eqx2F_equiv; apply eqx2F_reg. Qed.

Lemma skip2F_insert2F :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 (A : 'E^n),
    skip2F H (insert2F H x0 x1 A) = A.
Proof.
intros; rewrite skip2F_correct insert2F_correct 2!skipF_insertF; easy.
Qed.

Lemma insert2F_skip2F :
  ∀ {n i0 i1} (H : i1 ≠ i0) (A : 'E^n.+2),
    insert2F H (A i0) (A i1) (skip2F H A) = A.
Proof.
intros n i0 i1 H A.
rewrite skip2F_correct insert2F_correct -(skipF_correct H) !insertF_skipF//.
Qed.

End SkipF_Facts.

Section ReplaceF_Facts.

Context {E : Type}.

Properties of operators replaceF/replace2F.

Lemma replaceF_id : ∀ {n} i0 (A : 'E^n), replaceF i0 (A i0) A = A.
Proof.
intros n i0 A; extF i; destruct (ord_eq_dec i i0) as [Hi | Hi].
rewrite replaceF_correct_l Hi; easy.
rewrite replaceF_correct_r; easy.
Qed.

Lemma replaceF_equiv_def_insertF :
  ∀ {n} i0 x0 (A : 'E^n.+1),
    replaceF i0 x0 A = insertF i0 x0 (skipF i0 A).
Proof.
intros n i0 x0 A; extF i; destruct (ord_eq_dec i i0) as [H0 | H0].
rewrite → replaceF_correct_l, H0, insertF_correct_l; easy.
rewrite replaceF_correct_r; try easy.
destruct (nat_lt_eq_gt_dec i i0) as [[H0a | H0a] | H0a].
2: contradict H0a; apply ord_neq_compat; easy.
rewrite insertF_correct_rl skipF_correct_l; try easy; unfold widenF_S.
rewrite widen_narrow_S; easy.
rewrite insertF_correct_rr skipF_correct_r; unfold liftF_S.
rewrite lift_lower_S; easy.
rewrite lower_S_correct; auto with zarith.
Qed.

Lemma replaceF_equiv_def_skipF :
  ∀ {n} i0 x0 (A : 'E^n),
    replaceF i0 x0 A = skipF (lift_S i0) (insertF (widen_S i0) x0 A).
Proof.
intros n i0 x0 A; extF i; destruct (ord_eq_dec i i0) as [H0 | H0].
rewrite replaceF_correct_l H0; try easy.
assert (Ha : (widen_S i0 < lift_S i0)%coq_nat).
  rewrite widen_S_correct lift_S_correct; apply nat_ltS.
assert (Hb : widen_S i0 ≠ lift_S i0) by apply ord_lt_neq, Ha.
replace i0 with (insert_ord Hb) at 3.
rewrite skipF_correct insertF_correct_l; try easy.
apply ord_inj; rewrite insert_ord_correct_l; easy.
rewrite replaceF_correct_r; try easy.
destruct (nat_lt_eq_gt_dec i i0) as [[H0a | H0a] | H0a].
2: contradict H0a; apply ord_neq_compat; easy.
rewrite skipF_correct_l; try (rewrite lift_S_correct; auto with arith);
    unfold widenF_S; rewrite insertF_correct_rl.
f_equal; apply ord_inj; easy.
assert (Hb : (widen_S i0 < lift_S i)%coq_nat).
  rewrite widen_S_correct lift_S_correct; auto with arith.
rewrite skipF_correct_r; try (now apply Nat.nlt_ge);
    unfold liftF_S; rewrite insertF_correct_rr.
f_equal; apply ord_inj; rewrite lower_S_correct; easy.
Qed.

Lemma replaceF_eq_gen :
  ∀ {n} i0 j0 x0 y0 (A B : 'E^n),
    i0 = j0 → x0 = y0 → eqxF i0 A B → replaceF i0 x0 A = replaceF j0 y0 B.
Proof.
intros n i0 j0 x0 y0 A B Hij Hxy HAB; rewrite -Hxy -Hij.
extF i; destruct (ord_eq_dec i i0).
rewrite → 2!replaceF_correct_l; easy.
rewrite → 2!replaceF_correct_r; auto.
Qed.

Lemma replaceF_eq :
  ∀ {n} i0 x0 y0 (A B : 'E^n),
    x0 = y0 → eqxF i0 A B →replaceF i0 x0 A = replaceF i0 y0 B.
Proof. intros; apply replaceF_eq_gen; easy. Qed.

Lemma replaceF_reg_l :
  ∀ {n i0 x0 y0} (A B : 'E^n),
    replaceF i0 x0 A = replaceF i0 y0 B → x0 = y0.
Proof.
move⇒ n i0 x0 y0 A B /extF_rev H; specialize (H i0); simpl in H.
erewrite 2!replaceF_correct_l in H; easy.
Qed.

Lemma replaceF_reg_r :
  ∀ {n i0 x0 y0} {A B : 'E^n},
    replaceF i0 x0 A = replaceF i0 y0 B → eqxF i0 A B.
Proof.
move=>> /extF_rev H i Hi; specialize (H i); simpl in H.
erewrite 2!replaceF_correct_r in H; easy.
Qed.

Lemma replaceF_reg :
  ∀ {n i0 x0 y0} {A B : 'E^n},
    replaceF i0 x0 A = replaceF i0 y0 B → x0 = y0 ∧ eqxF i0 A B.
Proof.
move=>> H; split; [eapply replaceF_reg_l | eapply replaceF_reg_r]; apply H.
Qed.

Lemma eqxF_replaceF :
  ∀ {n} i0 i1 j1 x1 y1 (A B : 'E^n),
    i1 = j1 → x1 = y1 → eqx2F i0 i1 A B →
    eqxF i0 (replaceF i1 x1 A) (replaceF j1 y1 B).
Proof.
intros n i0 i1 j1 x1 y1 A B Hij Hxy HAB; rewrite -Hxy -Hij.
intros i Hi; destruct (ord_eq_dec i i1).
rewrite → 2!replaceF_correct_l; easy.
rewrite → 2!replaceF_correct_r; auto.
Qed.

Lemma replaceF_neqxF_compat_l :
  ∀ {n} i0 {x0 y0} {A B : 'E^n},
    x0 ≠ y0 → replaceF i0 x0 A ≠ replaceF i0 y0 B.
Proof. move=>>; rewrite -contra_equiv; apply replaceF_reg. Qed.

Lemma replaceF_neqxF_compat_r :
  ∀ {n i0} x0 y0 {A B : 'E^n},
    neqxF i0 A B → replaceF i0 x0 A ≠ replaceF i0 y0 B.
Proof. move=>>; rewrite neqxF_not_equiv -contra_equiv; apply replaceF_reg. Qed.

Lemma replaceF_neqxF_reg :
  ∀ {n i0 x0 y0} {A B : 'E^n},
    replaceF i0 x0 A ≠ replaceF i0 y0 B → x0 ≠ y0 ∨ neqxF i0 A B.
Proof.
move=>>; rewrite neqxF_not_equiv -not_and_equiv -contra_equiv.
intros; apply replaceF_eq; easy.
Qed.

Lemma replaceF_neqxF_equiv :
  ∀ {n i0 x0 y0} {A B : 'E^n},
    replaceF i0 x0 A ≠ replaceF i0 y0 B ↔ x0 ≠ y0 ∨ neqxF i0 A B.
Proof.
intros; split; [apply replaceF_neqxF_reg | intros [H | H]];
    [apply replaceF_neqxF_compat_l | apply replaceF_neqxF_compat_r]; easy.
Qed.

Lemma neqxF_replaceF :
  ∀ {n} i0 i1 x1 y1 (A B : 'E^n),
    neqxF i0 (replaceF i1 x1 A) (replaceF i1 y1 B) →
    x1 ≠ y1 ∨ neqx2F i0 i1 A B.
Proof.
move=>>; rewrite neqxF_not_equiv neqx2F_not_equiv -not_and_equiv -contra_equiv.
intros; apply eqxF_replaceF; easy.
Qed.

Lemma replaceF_constF :
  ∀ {n} i0 (x : E), replaceF i0 x (constF n x) = constF n x.
Proof.
intros n i0 x; extF i; destruct (ord_eq_dec i i0).
rewrite → replaceF_correct_l, constF_correct; easy.
rewrite replaceF_correct_r; easy.
Qed.

Lemma replaceF_singleF_0 :
  ∀ (x0 y0 : E), replaceF0 y0 (singleF x0) = singleF y0.
Proof. intros; extF; rewrite ord_one singleF_0 replaceF_correct_l; easy. Qed.

Lemma replaceF_coupleF_0 :
  ∀ (x0 x1 y0 : E), replaceF0 y0 (coupleF x0 x1) = coupleF y0 x1.
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite → coupleF_0, replaceF_correct_l; easy.
rewrite → replaceF_correct_r, 2!coupleF_1; easy.
Qed.

Lemma replaceF_coupleF_1 :
  ∀ (x0 x1 y1 : E), replaceFmax y1 (coupleF x0 x1) = coupleF x0 y1.
Proof.
intros; extF i; destruct (ord2_dec i) as [-> | ->].
rewrite → replaceF_correct_r, 2!coupleF_0; easy.
rewrite → coupleF_1, replaceF_correct_l; easy.
Qed.

Lemma replaceF_tripleF_0 :
  ∀ (x0 x1 x2 y0 : E),
    replaceF0 y0 (tripleF x0 x1 x2) = tripleF y0 x1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite → tripleF_0, replaceF_correct_l; easy.
rewrite → replaceF_correct_r, 2!tripleF_1; easy.
rewrite → replaceF_correct_r, 2!tripleF_2; easy.
Qed.

Lemma replaceF_tripleF_1 :
  ∀ (x0 x1 x2 y1 : E),
    replaceF ord1 y1 (tripleF x0 x1 x2) = tripleF x0 y1 x2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite → replaceF_correct_r, 2!tripleF_0; easy.
rewrite → tripleF_1, replaceF_correct_l; easy.
rewrite → replaceF_correct_r, 2!tripleF_2; easy.
Qed.

Lemma replaceF_tripleF_2 :
  ∀ (x0 x1 x2 y2 : E),
    replaceFmax y2 (tripleF x0 x1 x2) = tripleF x0 x1 y2.
Proof.
intros; extF i; destruct (ord3_dec i) as [[-> | ->] | ->].
rewrite → replaceF_correct_r, 2!tripleF_0; easy.
rewrite → replaceF_correct_r, 2!tripleF_1; easy.
rewrite → tripleF_2, replaceF_correct_l; easy.
Qed.

Lemma PAF_ind_replaceF :
  ∀ {n i0} p0 {P : 'Prop^n}, P i0 → PAF (replaceF i0 p0 P) → PAF P.
Proof.
intros n i0 p0 P H0 H1 i; destruct (ord_eq_dec i i0) as [-> | Hi]; [easy |].
rewrite -(replaceF_correct_r p0 _ Hi); easy.
Qed.

Lemma extF_replaceF :
  ∀ {n} i0 x0 (A B : 'E^n),
    A i0 = B i0 → replaceF i0 x0 A = replaceF i0 x0 B → A = B.
Proof.
intros n i0 x0 A B H0 H; extF i; destruct (ord_eq_dec i i0) as [Hi | Hi].
rewrite Hi; easy.
rewrite -(replaceF_correct_r x0 A Hi) -(replaceF_correct_r x0 B Hi) H; easy.
Qed.

Lemma skipF_replaceF :
  ∀ {n} i0 x0 (A : 'E^n.+1), skipF i0 (replaceF i0 x0 A) = skipF i0 A.
Proof. move=>>; apply skipF_eq; move=>> H; apply replaceF_correct_r; easy. Qed.

Lemma replace2F_sym :
  ∀ {n i0 i1} x0 x1 (A : 'E^n),
    i1 ≠ i0 → replace2F i0 i1 x0 x1 A = replace2F i1 i0 x1 x0 A.
Proof. move=>>; apply replace2F_equiv_def. Qed.

Lemma replace2F_eq_gen :
  ∀ {n} i0 i1 j0 j1 x0 x1 y0 y1 (A B : 'E^n),
    i0 = j0 → i1 = j1 → x0 = y0 → x1 = y1 → eqx2F i0 i1 A B →
    replace2F i0 i1 x0 x1 A = replace2F j0 j1 y0 y1 B.
Proof.
intros; unfold replace2F; apply replaceF_eq_gen; try easy.
apply eqxF_replaceF; try apply eqx2F_sym_i; easy.
Qed.

Lemma replace2F_eq :
  ∀ {n} i0 i1 x0 x1 y0 y1 (A B : 'E^n),
    x0 = y0 → x1 = y1 → eqx2F i0 i1 A B →
    replace2F i0 i1 x0 x1 A = replace2F i0 i1 y0 y1 B.
Proof. intros; apply replace2F_eq_gen; easy. Qed.

Lemma replace2F_reg_l0 :
  ∀ {n i0 i1} x0 x1 y0 y1 (A B : 'E^n),
    i1 ≠ i0 → replace2F i0 i1 x0 x1 A = replace2F i0 i1 y0 y1 B → x0 = y0.
Proof.
move=>> H; rewrite → 2!replace2F_equiv_def; try easy; apply replaceF_reg_l.
Qed.

Lemma replace2F_reg_l1 :
  ∀ {n} i0 i1 x0 x1 y0 y1 (A B : 'E^n),
    replace2F i0 i1 x0 x1 A = replace2F i0 i1 y0 y1 B → x1 = y1.
Proof. move=>>; apply replaceF_reg_l. Qed.

Lemma replace2F_reg_r :
  ∀ {n} i0 i1 x0 x1 y0 y1 (A B : 'E^n),
    replace2F i0 i1 x0 x1 A = replace2F i0 i1 y0 y1 B → eqx2F i0 i1 A B.
Proof.
move=>> H2 i [Hi0 Hi1]; unfold replace2F in H2.
specialize (replaceF_reg_r H2 i Hi1); intros H1.
erewrite 2!replaceF_correct_r in H1; easy.
Qed.

Lemma replace2F_reg :
  ∀ {n i0 i1} x0 x1 y0 y1 (A B : 'E^n),
    i1 ≠ i0 → replace2F i0 i1 x0 x1 A = replace2F i0 i1 y0 y1 B →
    x0 = y0 ∧ x1 = y1 ∧ eqx2F i0 i1 A B.
Proof.
move=>> Hi; repeat split; [eapply replace2F_reg_l0 |
    eapply replace2F_reg_l1 | eapply replace2F_reg_r]; try apply H; easy.
Qed.

Lemma replace2F_neqxF_compat_l0 :
  ∀ {n i0 i1 x0} x1 {y0} y1 (A B : 'E^n),
    i1 ≠ i0 → x0 ≠ y0 → replace2F i0 i1 x0 x1 A ≠ replace2F i0 i1 y0 y1 B.
Proof. move=>>; rewrite -contra_equiv; apply replace2F_reg_l0. Qed.

Lemma replace2F_neqxF_compat_l1 :
  ∀ {n} i0 i1 x0 {x1} y0 {y1} (A B : 'E^n),
    x1 ≠ y1 → replace2F i0 i1 x0 x1 A ≠ replace2F i0 i1 y0 y1 B.
Proof. move=>>; rewrite -contra_equiv; apply replace2F_reg_l1. Qed.

Lemma replace2F_neqxF_compat_r :
  ∀ {n i0 i1} x0 x1 y0 y1 {A B : 'E^n},
    neqx2F i0 i1 A B → replace2F i0 i1 x0 x1 A ≠ replace2F i0 i1 y0 y1 B.
Proof.
move=>>; rewrite neqx2F_not_equiv -contra_equiv; apply replace2F_reg_r.
Qed.

Lemma replace2F_neqxF_reg :
  ∀ {n x0 x1 y0 y1 i0 i1} {A B : 'E^n},
    replace2F i0 i1 x0 x1 A ≠ replace2F i0 i1 y0 y1 B →
    x0 ≠ y0 ∨ x1 ≠ y1 ∨ neqx2F i0 i1 A B.
Proof.
move=>>; rewrite neqx2F_not_equiv -not_and3_equiv -contra_equiv.
intros; apply replace2F_eq; easy.
Qed.

Lemma replace2F_neqxF_equiv :
  ∀ {n i0 i1 x0 x1 y0 y1} {A B : 'E^n},
    i1 ≠ i0 → replace2F i0 i1 x0 x1 A ≠ replace2F i0 i1 y0 y1 B ↔
    x0 ≠ y0 ∨ x1 ≠ y1 ∨ neqx2F i0 i1 A B.
Proof.
intros; split; [apply replace2F_neqxF_reg | intros [H1 | [H1 | H1]]];
    [apply replace2F_neqxF_compat_l0 | apply replace2F_neqxF_compat_l1 |
    apply replace2F_neqxF_compat_r]; easy.
Qed.

Lemma replace2F_constF :
  ∀ {n} i0 i1 (x : E), replace2F i0 i1 x x (constF n x) = constF n x.
Proof. intros; unfold replace2F; rewrite 2!replaceF_constF; easy. Qed.

Lemma replace2F_coupleF :
  ∀ (x0 x1 y0 y1 : E),
    replace2F ord0 ord_max y0 y1 (coupleF x0 x1) = coupleF y0 y1.
Proof.
intros; unfold replace2F; rewrite replaceF_coupleF_0 replaceF_coupleF_1; easy.
Qed.

Lemma replace2F_tripleF_01 :
  ∀ (x0 x1 x2 y0 y1 : E),
    replace2F ord0 ord1 y0 y1 (tripleF x0 x1 x2) = tripleF y0 y1 x2.
Proof.
intros; unfold replace2F; rewrite replaceF_tripleF_0 replaceF_tripleF_1; easy.
Qed.

Lemma replace2F_tripleF_02 :
  ∀ (x0 x1 x2 y0 y2 : E),
    replace2F ord0 ord_max y0 y2 (tripleF x0 x1 x2) = tripleF y0 x1 y2.
Proof.
intros; unfold replace2F; rewrite replaceF_tripleF_0 replaceF_tripleF_2; easy.
Qed.

Lemma replace2F_tripleF_12 :
  ∀ (x0 x1 x2 y1 y2 : E),
    replace2F ord1 ord_max y1 y2 (tripleF x0 x1 x2) = tripleF x0 y1 y2.
Proof.
intros; unfold replace2F; rewrite replaceF_tripleF_1 replaceF_tripleF_2; easy.
Qed.

Lemma PAF_ind_replace2F :
  ∀ {n i0 i1} p0 p1 {P : 'Prop^n},
    P i0 → P i1 → PAF (replace2F i0 i1 p0 p1 P) → PAF P.
Proof.
intros n i0 i1 p0 p1 P H0 H1 H2; destruct (ord_eq_dec i1 i0) as [Hi | Hi].
rewrite replace2F_correct_eq// in H2; apply (PAF_ind_replaceF p1 H1 H2).
rewrite -(replaceF_correct_r p0 _ Hi) in H1.
apply: (PAF_ind_replaceF p0 H0) (PAF_ind_replaceF _ H1 H2).
Qed.

Lemma extF_replace2F :
  ∀ {n} i0 i1 x0 x1 (A B : 'E^n),
    A i0 = B i0 → A i1 = B i1 →
    replace2F i0 i1 x0 x1 A = replace2F i0 i1 x0 x1 B → A = B.
Proof.
intros n i0 i1 x0 x1 A B H0 H1 H2; extF i.
destruct (ord_eq_dec i i0) as [Hi0 | Hi0]; try now rewrite Hi0.
destruct (ord_eq_dec i i1) as [Hi1 | Hi1]; try now rewrite Hi1.
rewrite -(replace2F_correct_r x0 x1 A Hi0 Hi1)
    -(replace2F_correct_r x0 x1 B Hi0 Hi1) H2; easy.
Qed.

Lemma skip2F_replace2F :
  ∀ {n i0 i1} (H : i1 ≠ i0) x0 x1 (A : 'E^n.+2),
    skip2F H (replace2F i0 i1 x0 x1 A) = skip2F H A.
Proof.
intros; apply skip2F_eq; move=>> [H0 H1]; apply replace2F_correct_r; easy.
Qed.

End ReplaceF_Facts.

Section PermutF_Facts1.

Context {E : Type}.

Properties of operators permutF/revF/moveF/transpF.

Lemma permutF_id_alt : ∀ {n}, permutF (@id [0..n)) = @id 'E^n.
Proof. easy. Qed.

Lemma permutF_id :
  ∀ {n} (p : [0..n) → [0..n)), same_fun p id → permutF p = (@id 'E^n).
Proof. move=>> /fun_ext Hp; subst; easy. Qed.

Lemma permutF_comp :
  ∀ {n} p q (A : 'E^n), permutF q (permutF p A) = permutF (p \o q) A.
Proof. easy. Qed.

Lemma permutF_can :
  ∀ {n p q}, cancel q p → cancel (permutF p) (@permutF E n q).
Proof.
move=>> H A; rewrite permutF_comp; extF; unfold permutF, comp; rewrite H; easy.
Qed.

Lemma permutF_invol :
  ∀ {n p}, involutive p → involutive (@permutF E n p).
Proof. move=>> Hp; move=>>; extF; unfold permutF; rewrite Hp; easy. Qed.

Lemma permutF_inj : ∀ {n p}, injective p → injective (@permutF E n p).
Proof.
move=>> /injF_bij [q _ Hq] A B /extF_rev H; extF i; rewrite -(Hq i); apply H.
Qed.

Lemma permutF_surj : ∀ {n p}, surjective p → surjective (@permutF E n p).
Proof.
move=>> /surjF_bij [q Hq _] A; ∃ (permutF q A); apply permutF_can; easy.
Qed.

Lemma permutF_bij : ∀ {n p}, bijective p → bijective (@permutF E n p).
Proof.
move=>> [q Hq1 Hq2]; apply (Bijective (permutF_can Hq2) (permutF_can Hq1)).
Qed.

Lemma permutF_ext :
  ∀ {n p}, injective p →
    ∀ (A B : 'E^n), permutF p A = permutF p B ↔ A = B.
Proof. intros; split; [apply permutF_inj | intros; subst]; easy. Qed.

Lemma permutF_next :
  ∀ {n p}, injective p →
    ∀ (A B : 'E^n), permutF p A ≠ permutF p B ↔ A ≠ B.
Proof. intros; rewrite -iff_not_equiv; apply permutF_ext; easy. Qed.

Lemma permutF_inj_compat :
  ∀ {n p} (Hp : injective p) {A : 'E^n},
    injective A → injective (permutF p A).
Proof. move=>> Hp A HA i j /HA /Hp; easy. Qed.

Lemma permutF_f_inv_l :
  ∀ {n p} (Hp : bijective p) (A : 'E^n),
    A = permutF (f_inv Hp) (permutF p A).
Proof. intros n p Hp A; rewrite permutF_can //; apply f_inv_can_r. Qed.

Lemma permutF_f_inv_r :
  ∀ {n p} (Hp : bijective p) (A : 'E^n),
    A = permutF p (permutF (f_inv Hp) A).
Proof. intros n p Hp A; rewrite permutF_can //; apply f_inv_can_l. Qed.

Lemma permutF_invalF_l : ∀ {n} p (A : 'E^n), invalF (permutF p A) A.
Proof. intros n p A i; ∃ (p i); easy. Qed.

Lemma permutF_invalF_r :
  ∀ {n} p (A : 'E^n), surjective p → invalF A (permutF p A).
Proof.
move=>> /surj_has_right_inv [q Hq] i;
    ∃ (q i); unfold permutF; rewrite Hq; easy.
Qed.

Lemma castF_permutF :
  ∀ {n1 n2} (H : n1 = n2) p (A1 : 'E^n1),
    castF H (permutF p A1) = permutF (cast_f_ord H p) (castF H A1).
Proof. intros; extF; unfold castF, permutF; f_equal; apply ord_inj; easy. Qed.

Lemma permutF_castF :
  ∀ {n1 n2} (H : n1 = n2) p (A1 : 'E^n1),
    permutF p (castF H A1) = castF H (permutF (cast_f_ord (eq_sym H) p) A1).
Proof.
intros; apply (castF_inj (eq_sym H)); rewrite castF_permutF !castF_can_r; easy.
Qed.

Lemma permutF_liftF_S :
  ∀ {n} p (A : 'E^n.+1),
    liftF_S (permutF p A) = funF (liftF_S p) A.
Proof. easy. Qed.

Lemma permutF_widenF_S :
  ∀ {n} p (A : 'E^n.+1),
    widenF_S (permutF p A) = funF (widenF_S p) A.
Proof. easy. Qed.

Lemma firstF_permutF :
  ∀ {n1 n2} p (A : 'E^(n1 + n2)),
    firstF (permutF p A) = funF (firstF p) A.
Proof. easy. Qed.

Lemma lastF_permutF :
  ∀ {n1 n2} p (A : 'E^(n1 + n2)),
    lastF (permutF p A) = funF (lastF p) A.
Proof. easy. Qed.

Lemma skipF_permutF :
  ∀ {n p} (Hp : injective p) i0 (A : 'E^n.+1),
    skipF i0 (permutF p A) = permutF (skip_f_ord Hp i0) (skipF (p i0) A).
Proof. move=>>; extF; unfold permutF; rewrite skipF_correct; easy. Qed.

Lemma insertF_permutF :
  ∀ {n p} (Hp : injective p) i0 x0 (A : 'E^n),
    insertF i0 x0 (permutF p A) = permutF (insert_f_ord i0 p) (insertF i0 x0 A).
Proof.
intros n p Hp i0 x0 A; apply (extF_skipF i0).
rewrite permutF_correct !insertF_correct_l// insert_f_ord_correct_l; easy.
rewrite skipF_insertF (skipF_permutF (insert_f_ord_inj Hp)).
rewrite insert_f_ord_correct_l// skipF_insertF; f_equal.
extF i; apply (skip_ord_inj (insert_f_ord i0 p i0)).
rewrite skip_f_ord_correct insert_f_ord_correct_l//.
rewrite (insert_f_ord_correct_r (skip_ord_correct_m i0 i)) insert_skip_ord//.
Qed.

Lemma revF_invol : ∀ {n}, involutive (@revF E n).
Proof. intros; apply permutF_invol, rev_ordK. Qed.

Lemma revF_inj : ∀ {n}, injective (@revF E n).
Proof. intros; apply permutF_inj, rev_ord_inj. Qed.

Lemma revF_surj : ∀ {n}, surjective (@revF E n).
Proof. intros; apply permutF_surj, rev_ord_surj. Qed.

Lemma revF_bij : ∀ {n}, bijective (@revF E n).
Proof. intros; apply permutF_bij, rev_ord_bij. Qed.

Lemma revF_ext : ∀ {n} (A B : 'E^n), revF A = revF B ↔ A = B.
Proof. intros; apply permutF_ext, rev_ord_inj. Qed.

Lemma revF_next : ∀ {n} (A B : 'E^n), revF A ≠ revF B ↔ A ≠ B.
Proof. intros; apply permutF_next, rev_ord_inj. Qed.

Lemma revF_inj_compat :
  ∀ {n} {A : 'E^n}, injective A → injective (revF A).
Proof. move=>>; apply permutF_inj_compat, rev_ord_inj. Qed.

Lemma revF_S_0 : ∀ {n} (A : 'E^n.+1), revF A ord0 = A ord_max.
Proof. intros; rewrite revF_correct rev_ord_0; easy. Qed.

Lemma revF_S_r :
  ∀ {n} (A : 'E^n.+1), liftF_S (revF A) = revF (widenF_S A).
Proof.
intros; unfold revF, permutF, liftF_S, widenF_S; extF; f_equal.
apply rev_ord_r.
Qed.

Lemma revF_S_l :
  ∀ {n} (A : 'E^n.+1), widenF_S (revF A) = revF (liftF_S A).
Proof.
intros; unfold revF, permutF, liftF_S, widenF_S; extF; f_equal.
apply rev_ord_l.
Qed.

Lemma revF_S_max : ∀ {n} (A : 'E^n.+1), revF A ord_max = A ord0.
Proof. intros; rewrite revF_correct rev_ord_max; easy. Qed.

Lemma revF_invalF_l : ∀ {n} (A : 'E^n), invalF (revF A) A.
Proof. intros; apply permutF_invalF_l. Qed.

Lemma revF_invalF_r : ∀ {n} (A : 'E^n), invalF A (revF A).
Proof. intros; apply permutF_invalF_r, bij_surj, rev_ord_bij. Qed.

Lemma revF_castF :
  ∀ {n1 n2} (H : n1 = n2) (A1 : 'E^n1),
    revF (castF H A1) = castF H (revF A1).
Proof. intros; unfold revF; rewrite permutF_castF cast_f_rev_ord; easy. Qed.

Lemma firstF_revF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    firstF (revF A) = revF (lastF (castF (addnC n1 n2) A)).
Proof.
intros; extF; unfold firstF, lastF, revF, permutF, castF; f_equal.
apply ord_inj; simpl; rewrite addnC addnBA; easy.
Qed.

Lemma revF_firstF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    revF (firstF A) = lastF (castF (addnC n1 n2) (revF A)).
Proof. intros; apply revF_inj; rewrite -firstF_revF !revF_invol; easy. Qed.

Lemma firstF_lastF_eq_revF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    firstF A = revF (lastF (castF (addnC n1 n2) (revF A))).
Proof. intros; rewrite -revF_firstF revF_invol; easy. Qed.

Lemma revF_lastF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    revF (lastF A) = firstF (castF (addnC n1 n2) (revF A)).
Proof.
intros; rewrite -revF_castF firstF_revF castF_comp castF_id; easy.
Qed.

Lemma lastF_revF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    lastF (revF A) = revF (firstF (castF (addnC n1 n2) A)).
Proof. intros; rewrite revF_firstF revF_castF castF_comp castF_id; easy. Qed.

Lemma lastF_firstF_eq_revF :
  ∀ {n1 n2} (A : 'E^(n1 + n2)),
    lastF A = revF (firstF (castF (addnC n1 n2) (revF A))).
Proof. intros; rewrite -lastF_revF revF_invol; easy. Qed.

Lemma revF_first :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (rev_ord i < j)%coq_nat → P (A j) (B j)) →
    ∀ (j : [0..n)), (j < i)%coq_nat → P (revF A j) (revF B j).
Proof.
move=>> Hi j Hj; rewrite !revF_correct; apply Hi, rev_ord_monot; easy.
Qed.

Lemma revF_last :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (j < rev_ord i)%coq_nat → P (A j) (B j)) →
    ∀ (j : [0..n)), (i < j)%coq_nat → P (revF A j) (revF B j).
Proof.
move=>> Hi j Hj; rewrite !revF_correct; apply Hi, rev_ord_monot; easy.
Qed.

Lemma revF_first_rev :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (j < rev_ord i)%coq_nat → P (revF A j) (revF B j)) →
    ∀ (j : [0..n)), (i < j)%coq_nat → P (A j) (B j).
Proof.
intros P n i A B Hi;
    rewrite -(revF_invol A) -(revF_invol B); apply (revF_last _ i); easy.
Qed.

Lemma revF_last_rev :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (rev_ord i < j)%coq_nat → P (revF A j) (revF B j)) →
    ∀ (j : [0..n)), (j < i)%coq_nat → P (A j) (B j).
Proof.
intros P n i A B Hi;
    rewrite -(revF_invol A) -(revF_invol B); apply (revF_first _ i); easy.
Qed.

Lemma revF_first_equiv :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (j < i)%coq_nat → P (revF A j) (revF B j)) ↔
    (∀ (j : [0..n)), (rev_ord i < j)%coq_nat → P (A j) (B j)).
Proof.
intros P n i A B; split; [| apply revF_first].
intros; apply revF_first_rev with (rev_ord i); [rewrite rev_ordK |]; easy.
Qed.

Lemma revF_last_equiv :
  ∀ (P : E → E → Prop) {n} (i : [0..n)) (A B : 'E^n),
    (∀ (j : [0..n)), (i < j)%coq_nat → P (revF A j) (revF B j)) ↔
    (∀ (j : [0..n)), (j < rev_ord i)%coq_nat → P (A j) (B j)).
Proof.
intros P n i A B; split; [| apply revF_last].
intros; apply revF_last_rev with (rev_ord i); [rewrite rev_ordK |]; easy.
Qed.

Lemma skipF_revF :
  ∀ {n} i0 (A : 'E^n.+1),
    skipF i0 (revF A) = revF (skipF (rev_ord i0) A).
Proof.
intros n i0 A; rewrite (skipF_permutF rev_ord_inj); unfold revF; f_equal.
extF i;
    apply (skip_ord_inj (rev_ord i0)); rewrite skip_f_ord_correct.
apply ord_inj; simpl.
destruct i0 as [i0 Hi0], i as [i Hi]; simpl.
destruct (lt_dec i i0) as [H1 | H1]; [rewrite bump_l// | rewrite bump_r_alt//];
    (destruct (lt_dec (n - i.+1) (n.+1 - i0.+1)) as [H2 | H2];
    [rewrite bump_l// | rewrite bump_r_alt//]).
2: rewrite subSS subnSK; easy.
all: exfalso.
move: H1 H2; rewrite -minusE; lia.
rewrite -minusE in H2; move: Hi ⇒ /ltP; move: Hi0 ⇒ /ltP; lia.
Qed.

Lemma skipF0_revF :
  ∀ {n} (A : 'E^n.+1), skipF0 (revF A) = revF (skipFmax A).
Proof. intros; rewrite skipF_revF rev_ord_0; easy. Qed.

Lemma skipFmax_revF :
  ∀ {n} (A : 'E^n.+1), skipFmax (revF A) = revF (skipF0 A).
Proof. intros; rewrite skipF_revF rev_ord_max; easy. Qed.

Lemma revF_skipF :
  ∀ {n} i0 (A : 'E^n.+1),
    revF (skipF i0 A) = skipF (rev_ord i0) (revF A).
Proof. intros; rewrite skipF_revF rev_ordK; easy. Qed.

Lemma revF_skipF0 :
  ∀ {n} (A : 'E^n.+1), revF (skipF0 A) = skipFmax (revF A).
Proof. intros; rewrite skipFmax_revF; easy. Qed.

Lemma revF_skipFmax :
  ∀ {n} (A : 'E^n.+1), revF (skipFmax A) = skipF0 (revF A).
Proof. intros; rewrite skipF0_revF; easy. Qed.

Lemma insertF_revF :
  ∀ {n} i0 x0 (A : 'E^n),
    insertF i0 x0 (revF A) = revF (insertF (rev_ord i0) x0 A).
Proof.
intros n i0 x0 A; rewrite (insertF_permutF rev_ord_inj); unfold revF.
extF i; rewrite !permutF_correct; destruct (ord_eq_dec i i0) as [-> | Hi].
rewrite insert_f_ord_correct_l// !insertF_correct_l//.
move: (skip_ord_correct_m i0) (inj_contra rev_ord_inj _ _ Hi) ⇒ H1 H2.
rewrite insert_f_ord_correct_r !insertF_correct_r
    insert_skip_ord rev_insert_ord; easy.
Qed.

Lemma insertF0_revF :
  ∀ {n} x0 (A : 'E^n), insertF0 x0 (revF A) = revF (insertFmax x0 A).
Proof. intros; rewrite insertF_revF rev_ord_0; easy. Qed.

Lemma insertFmax_revF :
  ∀ {n} x0 (A : 'E^n), insertFmax x0 (revF A) = revF (insertF0 x0 A).
Proof. intros; rewrite insertF_revF rev_ord_max; easy. Qed.

Lemma revF_insertF :
  ∀ {n} i0 x0 (A : 'E^n),
    revF (insertF i0 x0 A) = insertF (rev_ord i0) x0 (revF A).
Proof. intros; rewrite insertF_revF rev_ordK; easy. Qed.

Lemma revF_insertF0 :
  ∀ {n} x0 (A : 'E^n), revF (insertF0 x0 A) = insertFmax x0 (revF A).
Proof. intros; rewrite insertFmax_revF; easy. Qed.

Lemma revF_insertFmax :
  ∀ {n} x0 (A : 'E^n), revF (insertFmax x0 A) = insertF0 x0 (revF A).
Proof. intros; rewrite insertF0_revF; easy. Qed.

Lemma moveF_inj : ∀ {n} i0 i1, injective (@moveF E n i0 i1).
Proof. intros; apply permutF_inj, move_ord_inj. Qed.

Lemma moveF_surj : ∀ {n} i0 i1, surjective (@moveF E n i0 i1).
Proof. intros; apply permutF_surj, move_ord_surj. Qed.

Lemma moveF_bij : ∀ {n} i0 i1, bijective (@moveF E n i0 i1).
Proof. intros; apply permutF_bij, move_ord_bij. Qed.

Lemma moveF_ext :
  ∀ {n} i0 i1 (A B : 'E^n.+1), moveF i0 i1 A = moveF i0 i1 B ↔ A = B.
Proof. intros; apply permutF_ext, move_ord_inj. Qed.

Lemma moveF_next :
  ∀ {n} i0 i1 (A B : 'E^n.+1), moveF i0 i1 A ≠ moveF i0 i1 B ↔ A ≠ B.
Proof. intros; apply permutF_next, move_ord_inj. Qed.

Lemma moveF_inj_compat :
  ∀ {n} i0 i1 {A : 'E^n.+1}, injective A → injective (moveF i0 i1 A).
Proof. move=>>; apply permutF_inj_compat, move_ord_inj. Qed.

Lemma moveF_invalF_l :
  ∀ {n} i0 i1 (A : 'E^n.+1), invalF (moveF i0 i1 A) A.
Proof. intros; apply permutF_invalF_l. Qed.

Lemma moveF_invalF_r :
  ∀ {n} i0 i1 (A : 'E^n.+1), invalF A (moveF i0 i1 A).
Proof. intros; apply permutF_invalF_r, bij_surj, move_ord_bij. Qed.

Lemma castF_moveF :
  ∀ {n1 n2} (H : n1 = n2) i0 i1 (A1 : 'E^n1.+1),
    let HH := eq_S n1 n2 H in
    castF HH (moveF i0 i1 A1) =
      moveF (cast_ord HH i0) (cast_ord HH i1) (castF HH A1).
Proof.
intros; rewrite castF_permutF; unfold moveF; rewrite cast_f_move_ord; easy.
Qed.

Lemma moveF_castF :
  ∀ {n1 n2} (H : n1 = n2) i0 i1 (A1 : 'E^n1.+1),
    let HH := eq_S n1 n2 H in
    moveF i0 i1 (castF HH A1) =
      castF HH (moveF (cast_ord (eq_sym HH) i0) (cast_ord (eq_sym HH) i1) A1).
Proof.
intros n1 n2 H i0 i1 A1 HH; apply (castF_inj (eq_sym HH)).
rewrite castF_moveF !cast_ord_comp !cast_ord_id; easy.
Qed.

Lemma firstF_moveF :
  ∀ {n1 n2} i0 i1 (A : 'E^(n1 + n2).+1),
    firstF (castF (addSn_sym n1 n2) (moveF i0 i1 A)) =
      funF (firstF (castF (addSn_sym n1 n2) (move_ord i0 i1))) A.
Proof.
intros n1 n2 i0 i1 A; rewrite (firstF_castF n1.+1 n2) !castF_id.
apply: firstF_permutF.
Qed.

Lemma lastF_moveF :
  ∀ {n1 n2} i0 i1 (A : 'E^(n1 + n2).+1),
    lastF (castF (addnS_sym _ _) (moveF i0 i1 A)) =
      funF (lastF (castF (addnS_sym _ _) (move_ord i0 i1))) A.
Proof. easy. Qed.

Lemma skipF_moveF :
  ∀ {n} i0 i1 j0 (A : 'E^n.+1),
    skipF j0 (moveF i0 i1 A) =
      funF (skip_f_ord (move_ord_inj i0 i1) j0) (skipF (move_ord i0 i1 j0) A).
Proof. intros; apply skipF_permutF. Qed.

Lemma moveF_equiv_def :
  ∀ {n} i0 i1 (A : 'E^n.+1),
    moveF i0 i1 A = insertF i1 (A i0) (skipF i0 A).
Proof.
intros n i0 i1 A; extF i; destruct (ord_eq_dec i i1) as [Hi | Hi].
rewrite moveF_correct_l// insertF_correct_l//.
rewrite moveF_correct_r insertF_correct_r//.
Qed.

Lemma transpF_can :
  ∀ {n} i0 i1, cancel (transpF i0 i1) (@transpF E n i1 i0).
Proof. intros; apply permutF_can, transp_ord_can. Qed.

Lemma transpF_invol :
  ∀ {n} i0 i1, involutive (@transpF E n i0 i1).
Proof. intros; apply permutF_invol, transp_ord_invol. Qed.

Lemma transpF_sym :
  ∀ {n} i0 i1, transpF i0 i1 = @transpF E n i1 i0.
Proof. intros; unfold transpF, permutF; rewrite transp_ord_sym; easy. Qed.

Lemma transpF_inj :
  ∀ {n} i0 i1, injective (@transpF E n i0 i1).
Proof. intros; apply permutF_inj, transp_ord_inj. Qed.

Lemma transpF_surj :
  ∀ {n} i0 i1, surjective (@transpF E n i0 i1).
Proof. intros; apply permutF_surj, transp_ord_surj. Qed.

Lemma transpF_bij :
  ∀ {n} i0 i1, bijective (@transpF E n i0 i1).
Proof. intros; apply permutF_bij, transp_ord_bij. Qed.

Lemma transpF_inv :
  ∀ {n} i0 i1 (H : bijective (transpF i0 i1)),
    f_inv H = @transpF E n i1 i0.
Proof.
intros n i0 i1 H; rewrite -(f_inv_uniq_l H (transpF_can i0 i1)); easy.
Qed.

Lemma transpF_ext :
  ∀ {n} i0 i1 (A B : 'E^n.+1),
    transpF i0 i1 A = transpF i0 i1 B ↔ A = B.
Proof. intros; apply permutF_ext, transp_ord_inj. Qed.

Lemma transpF_next :
  ∀ {n} i0 i1 (A B : 'E^n.+1),
    transpF i0 i1 A ≠ transpF i0 i1 B ↔ A ≠ B.
Proof. intros; apply permutF_next, transp_ord_inj. Qed.

Lemma transpF_inj_compat :
  ∀ {n} i0 i1 {A : 'E^n}, injective A → injective (transpF i0 i1 A).
Proof. move=>>; apply permutF_inj_compat, transp_ord_inj. Qed.

Lemma transpF_invalF_l :
  ∀ {n} i0 i1 (A : 'E^n), invalF (transpF i0 i1 A) A.
Proof. intros; apply permutF_invalF_l. Qed.

Lemma transpF_invalF_r :
  ∀ {n} i0 i1 (A : 'E^n), invalF A (transpF i0 i1 A).
Proof. intros; apply permutF_invalF_r, bij_surj, transp_ord_bij. Qed.

Lemma castF_transpF :
  ∀ {n1 n2} (H : n1 = n2) i0 i1 (A1 : 'E^n1),
    castF H (transpF i0 i1 A1) =
      transpF (cast_ord H i0) (cast_ord H i1) (castF H A1).
Proof.
intros; rewrite castF_permutF; unfold transpF; rewrite cast_f_transp_ord; easy.
Qed.

Lemma firstF_transpF :
  ∀ {n1 n2} i0 i1 (A : 'E^(n1 + n2)),
    firstF (transpF i0 i1 A) = funF (firstF (transp_ord i0 i1)) A.
Proof. easy. Qed.

Lemma lastF_transpF :
  ∀ {n1 n2} i0 i1 (A : 'E^(n1 + n2)),
    lastF (transpF i0 i1 A) = funF (lastF (transp_ord i0 i1)) A.
Proof. easy. Qed.

Lemma skipF_transpF :
  ∀ {n} i0 i1 j0 (A : 'E^n.+1),
    skipF j0 (transpF i0 i1 A) =
      funF (skip_f_ord (transp_ord_inj i0 i1) j0)
        (skipF (transp_ord i0 i1 j0) A).
Proof. intros; apply skipF_permutF. Qed.

Lemma transpF_equiv_def :
  ∀ {n} i0 i1 (A : 'E^n),
    transpF i0 i1 A = replace2F i0 i1 (A i1) (A i0) A.
Proof.
intros n i0 i1 A; extF i.
destruct (ord_eq2_dec i i0 i1) as [[Hi | Hi] | [Hi1 Hi2]].
rewrite transpF_correct_l0//; destruct (ord_eq_dec i1 i0) as [H | H].
subst; rewrite replace2F_correct_eq// replaceF_correct_l//.
rewrite replace2F_correct_l0//.
rewrite transpF_correct_l1// replace2F_correct_l1//.
rewrite transpF_correct_r// replace2F_correct_r//.
Qed.

End PermutF_Facts1.

Section PermutF_Facts2.

Context {E : Type}.

Lemma first_f_extendF :
  ∀ {n1 n2} {f : [0..n1) → [0..n1 + n2)} (Hf : injective f),
    ∃ (p : [0..n1 + n2) → [0..n1 + n2)), bijective p ∧ f = firstF p.
Proof.
intros n1 n2 f Hf; destruct (injF_extend_bij_EX Hf) as [p [Hp1 Hp2]]; ∃ p.
split; [easy |].
extF; rewrite Hp2; unfold widenF, firstF; f_equal; apply ord_inj; easy.
Qed.

Lemma last_f_extendF :
  ∀ {n1 n2} {f : [0..n2) → [0..n1 + n2)} (Hf : injective f),
    ∃ (p : [0..n1 + n2) → [0..n1 + n2)), bijective p ∧ f = lastF p.
Proof.
intros n1 n2 f' Hf'; pose (f := revF f').
assert (Hf0 : f' = revF f) by apply eq_sym, revF_invol.
assert (Hf1 : injective f) by now apply revF_inj_compat.
destruct (injF_extend_bij_EX Hf1) as [p [Hp1 Hp2]]; ∃ (revF p).
split; [apply injF_bij, revF_inj_compat, bij_inj; easy |].
extF; unfold lastF; rewrite Hf0 !revF_correct Hp2; unfold widenF; f_equal.
apply ord_inj; simpl; rewrite addnC -addnS subnDA addnK; easy.
Qed.

End PermutF_Facts2.

Section FilterPF_Facts0.

Definition and properties of filterP_f_ord.

Definition filterP_f_ord
    {n1 n2} {P1 : 'Prop^n1} {P2 : 'Prop^n2} (f : [0..n1) → [0..n2))
    {i1} (HP2 : P2 (f i1)) : [0..lenPF P1) → [0..lenPF P2) :=
  fun j1 ⇒ unfilterP_ord HP2 (f (filterP_ord j1)).

Lemma filterP_f_ord_correct :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2}
      {i1} (HP2 : P2 (f i1)) {j1 : [0..lenPF P1)},
    injective f → extendPF f P1 P2 →
    filterP_ord (filterP_f_ord f HP2 j1) = f (filterP_ord j1).
Proof.
intros n1 n2 f P1 P2 i1 HP2 j1 Hf HP; rewrite filterP_unfilterP_ord_in//.
rewrite (extendPF_unfunF_rev Hf HP) (unfunF_correct_l _ (filterP_ord j1) _ Hf);
    [apply filterP_ord_correct | easy].
Qed.

Lemma filterP_f_ord_correct_alt :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {P1 : 'Prop^n1} {P2 : 'Prop^n2}
      {i1} (HP2 : P2 (f i1)) {j1 : [0..lenPF P1)} {j2 : [0..lenPF P2)},
    injective f → extendPF f P1 P2 →
    filterP_f_ord f HP2 j1 = j2 ↔ f (filterP_ord j1) = filterP_ord j2.
Proof.
move=>> Hf HP;
    rewrite -(inj_equiv (filterP_ord_inj _)) (filterP_f_ord_correct _ Hf HP)//.
Qed.

Lemma filterP_f_ord_comp :
  ∀ {n1 n2 n3} {P1 : 'Prop^n1} {P2 : 'Prop^n2} {P3 : 'Prop^n3}
      {f12 : [0..n1) → [0..n2)} {f23 : [0..n2) → [0..n3)}
      {i1} (HP2 : P2 (f12 i1)) {HP3 : P3 (f23 (f12 i1))}
      {j1 : [0..lenPF P1)},
    injective f12 → extendPF f12 P1 P2 →
    filterP_f_ord (f23 \o f12) HP3 j1 =
      filterP_f_ord f23 HP3 (filterP_f_ord f12 HP2 j1).
Proof.
move=>> Hf12 HP12; unfold filterP_f_ord at 2.
rewrite (filterP_f_ord_correct _ Hf12 HP12); easy.
Qed.

Lemma filterP_f_ord_comp_l :
  ∀ {n1 n2} {P1 : 'Prop^n1} {P2 : 'Prop^n2}
      {p1 : [0..n1) → [0..n1)} {f : [0..n1) → [0..n2)}
      {i1} (HP1 : P1 (p1 i1)) {HP2 : P2 (f (p1 i1))}
      {j1 : [0..lenPF (permutF p1 P1))},
    injective p1 →
    filterP_f_ord (f \o p1) HP2 j1 =
      filterP_f_ord f HP2 (filterP_f_ord p1 HP1 j1).
Proof.
move=>> Hp1; apply (filterP_f_ord_comp _ Hp1), (extendPF_permutF _ Hp1).
Qed.

End FilterPF_Facts0.

Section FilterPF_Facts1.

Context {E : Type}.

Properties of operators lenPF/filterPF.

Lemma filterPF_eq :
  ∀ {n} (P : 'Prop^n) (A : 'E^n), filterPF P A = funF filterP_ord A.
Proof. easy. Qed.

Lemma filterPF_nil :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n), n = 0 → filterPF P A = filterPF P B.
Proof.
intros; subst; apply extF; intros [j Hj]; exfalso.
rewrite lenPF_nil in Hj; easy.
Qed.

Lemma filterPF_ext_l_gen :
  ∀ {n1 n2} (Hn : n1 = n2) {P1 : 'Prop^n1} {P2 : 'Prop^n2}
      (HP : iffAF P1 (castF (eq_sym Hn) P2)) (A1 : 'E^n1),
    let A2 := castF Hn A1 in
    filterPF P1 A1 =
      castF (eq_sym (lenPF_ext_gen Hn (iffAF_eq_sym Hn HP))) (filterPF P2 A2).
Proof.
intros n1 n2 Hn P1 P2 HP A1 A2; unfold A2; subst; rewrite castF_id; clear A2.
extF; unfold filterPF, funF, castF; f_equal; apply ord_inj; simpl.
assert (HP' : iffAF P1 P2) by now intro; rewrite HP castF_id.
rewrite eq_sym_involutive (filterP_ord_ext HP')
    (filterP_cast_ord_eq (lenPF_ext _)
      (lenPF_ext_gen _ (iffAF_eq_sym _ _))); easy.
Qed.

Lemma filterPF_ext_l :
  ∀ {n P Q} (H : iffAF P Q) (A : 'E^n),
    filterPF P A = castF (eq_sym (lenPF_ext H)) (filterPF Q A).
Proof.
intros; extF; unfold filterPF, funF, castF; f_equal.
rewrite eq_sym_involutive; apply filterP_ord_ext.
Qed.

Lemma filterPF_ext_r :
  ∀ {n} (P : 'Prop^n) (A B : 'E^n),
    eqPF P A B → filterPF P A = filterPF P B.
Proof. move=>> H; extF; apply H, filterP_ord_correct. Qed.

Lemma filterPF_invalF :
  ∀ {n} P (A : 'E^n), invalF (filterPF P A) A.
Proof. intros n P A j; ∃ (enum_val j); easy. Qed.

Lemma lenPF_castF :
  ∀ {n1 n2} (H : n1 = n2) (P1 : 'Prop^n1), lenPF (castF H P1) = lenPF P1.
Proof. intros; subst; rewrite castF_id//. Qed.

Lemma filterPF_castF_l :
  ∀ {n1 n2} (H : n1 = n2) (P1 : 'Prop^n1) (A2 : 'E^n2),
    filterPF (castF H P1) A2 =
      castF (eq_sym (lenPF_castF H P1)) (filterPF P1 (castF (eq_sym H) A2)).
Proof.
intros n1 n2 H P1 A2; subst.
assert (HP : iffAF (castF (erefl n2) P1) P1) by now rewrite castF_id.
rewrite (filterPF_ext_l HP) eq_sym_refl (castF_refl A2); f_equal; easy.
Qed.

Lemma filterPF_castF_r :
  ∀ {n1 n2} (H : n1 = n2) (P2 : 'Prop^n2) (A1 : 'E^n1),
    filterPF P2 (castF H A1) =
      castF (lenPF_castF (eq_sym H) P2)
            (filterPF (castF (eq_sym H) P2) A1).
Proof.
intros n1 n2 H P2 A1; subst; rewrite eq_sym_refl castF_id.
assert (HP : iffAF (castF (erefl n2) P2) P2) by now rewrite castF_id.
rewrite (filterPF_ext_l HP) castF_comp castF_id; easy.
Qed.

Lemma filterPF_castF :
  ∀ {n1 n2} (H : n1 = n2) (P1 : 'Prop^n1) (A1 : 'E^n1),
    filterPF (castF H P1) (castF H A1) =
      castF (eq_sym (lenPF_castF H P1)) (filterPF P1 A1).
Proof. intros; rewrite filterPF_castF_l castF_can; easy. Qed.

Lemma lenPF_singleF_in : ∀ {P : Prop}, P → lenPF (singleF P) = 1.
Proof. intros; apply lenPF1_in; easy. Qed.

Lemma filterPF_singleF_in :
  ∀ {P : Prop} (HP : P) (A : E),
    filterPF (singleF P) (singleF A) =
      castF (eq_sym (lenPF_singleF_in HP)) (singleF A).
Proof. intros; extF; unfold filterPF, funF; rewrite !singleF_0; easy. Qed.

Lemma lenPF_singleF_out : ∀ {P : Prop}, ¬ P → lenPF (singleF P) = 0.
Proof. intros; apply lenPF1_out; easy. Qed.

Lemma filterPF_singleF_out :
  ∀ {P : Prop} (HP : ¬ P) (A : E) (B0 : 'E^0),
    filterPF (singleF P) (singleF A) =
      castF (eq_sym (lenPF_singleF_out HP)) B0.
Proof.
intros; apply extF; intros [i Hi]; exfalso.
rewrite (lenPF_singleF_out HP) in Hi; easy.
Qed.

Lemma filterPF_ind_l_in :
  ∀ {n} {P : 'Prop^n.+1} (HP : P ord0) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_l_in HP))
        (concatF (singleF (A ord0)) (filterPF (liftF_S P) (liftF_S A))).
Proof.
intros n P HP0 A; apply (castF_inj (lenPF_ind_l_in HP0)); rewrite castF_can.
assert (HP1 : iffAF P (castF_1pS (concatF (singleF (P ord0)) (liftF_S P))))
    by now rewrite -concatF_splitF_S1p.
rewrite (filterPF_ext_l HP1)
    {1}(concatF_splitF_S1p A) filterPF_castF !castF_comp.
pose (P0 := singleF (P ord0)); fold P0; pose (P' := liftF_S P); fold P'.
pose (A0 := singleF (A ord0)); fold A0; pose (A' := liftF_S A); fold A'.
pose (H := eq_trans (eq_sym (lenPF_castF (add1n n) (concatF P0 P')))
             (eq_trans (eq_sym (lenPF_ext HP1)) (lenPF_ind_l_in HP0))).
rewrite (castF_eq_l _ H).
extF j; unfold filterPF, funF, castF.
destruct (lt_dec (filterP_ord (cast_ord (eq_sym H) j)) 1) as [Hj1 | Hj1],
    (lt_dec j 1) as [Hj2 | Hj2].
rewrite !concatF_correct_l !ord_one; easy.
contradict Hj2; move: Hj1; rewrite -!ord0_lt_equiv;
    move⇒ /(filterP_ord_ind_l_in_0_rev HP0) Hj.
rewrite cast_ord_comp cast_ord_0_equiv in Hj; apply ord_inj; easy.
contradict Hj1; move: Hj2; rewrite -!ord0_lt_equiv; move=>> →.
assert (HP0' : concatF P0 P' ord0) by easy.
apply (filterP_ord_ind_l_in_0 HP0'), ord_inj; easy.
rewrite !concatF_correct_r; f_equal; apply ord_inj; simpl; apply addn_is_subn.
assert (H0 : cast_ord (lenPF_ind_l_in HP0) (cast_ord (eq_sym H) j) ≠ ord0).
  rewrite cast_ord_comp; contradict Hj2; apply cast_ord_0_equiv in Hj2.
  simpl; rewrite Hj2; apply Nat.lt_0_1.
rewrite filterP_ord_ind_l_in_n0 lift_S_correct -add1n; f_equal.
assert (HP2 : iffAF (liftF_S (concatF P0 P')) (liftF_S P)).
  unfold P0, P'; rewrite concatF_splitF_S1p'.
  unfold castF_S1p; rewrite castF_id; easy.
rewrite (filterP_ord_ext HP2).
assert (Hj : cast_ord (lenPF_ext HP2) (lower_S H0) = concat_r_ord Hj2)
    by now apply ord_inj.
rewrite Hj; easy.
Qed.

Lemma filterPF_ind_l_out :
  ∀ {n} {P : 'Prop^n.+1} (HP : ¬ P ord0) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_l_out HP)) (filterPF (liftF_S P) (liftF_S A)).
Proof.
intros n P HP A; apply (castF_inj (lenPF_ind_l_out HP)); rewrite castF_can.
assert (HP1 : iffAF P (castF_1pS (concatF (singleF (P ord0)) (liftF_S P))))
    by now rewrite -concatF_splitF_S1p.
rewrite (filterPF_ext_l HP1)
    {1}(concatF_splitF_S1p A) filterPF_castF !castF_comp.
pose (P0 := singleF (P ord0)); fold P0; pose (P' := liftF_S P); fold P'.
pose (A0 := singleF (A ord0)); fold A0; pose (A' := liftF_S A); fold A'.
pose (H := eq_trans (eq_sym (lenPF_castF (add1n n) (concatF P0 P')))
             (eq_trans (eq_sym (lenPF_ext HP1)) (lenPF_ind_l_out HP))).
rewrite (castF_eq_l _ H).
extF j; unfold filterPF, funF, castF.
destruct (lt_dec (filterP_ord (cast_ord (eq_sym H) j)) 1) as [Hj | Hj].
contradict Hj; rewrite -ord0_lt_equiv filterP_ord_ind_l_out.
apply lift_S_not_first.
rewrite concatF_correct_r; f_equal; apply ord_inj; simpl; apply addn_is_subn.
rewrite filterP_ord_ind_l_out lift_S_correct -add1n; f_equal.
assert (HP2 : iffAF (liftF_S (concatF P0 P')) (liftF_S P)).
  unfold P0, P'; rewrite concatF_splitF_S1p'.
  unfold castF_S1p; rewrite castF_id; easy.
rewrite (filterP_ord_ext HP2) !cast_ord_comp cast_ord_id; easy.
Qed.

Lemma lenPF_ind_l :
  ∀ {n} (P : 'Prop^n.+1),
    lenPF P = lenPF (singleF (P ord0)) + lenPF (liftF_S P).
Proof.
intros n P; destruct (classic (P ord0)) as [HP | HP].
rewrite (lenPF_ind_l_in HP) (lenPF_singleF_in HP) //.
rewrite (lenPF_ind_l_out HP) (lenPF_singleF_out HP) //.
Qed.

Lemma filterPF_ind_l :
  ∀ {n} (P : 'Prop^n.+1) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_l P))
        (concatF (filterPF (singleF (P ord0)) (singleF (A ord0)))
                 (filterPF (liftF_S P) (liftF_S A))).
Proof.
intros n P A; destruct (classic (P ord0)) as [HP | HP].
rewrite filterPF_ind_l_in filterPF_singleF_in concatF_castF_l castF_comp.
apply castF_eq_l.
rewrite filterPF_ind_l_out (concatF_nil_l' (lenPF_singleF_out HP)) castF_comp.
apply castF_eq_l.
Qed.

Lemma filterPF_ind_r_in :
  ∀ {n} {P : 'Prop^n.+1} (HP : P ord_max) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_r_in HP))
        (concatF (filterPF (widenF_S P) (widenF_S A)) (singleF (A ord_max))).
Proof.
intros n P HPn A; apply (castF_inj (lenPF_ind_r_in HPn)); rewrite castF_can.
assert (HP1 : iffAF P (castF_p1S (concatF (widenF_S P) (singleF (P ord_max)))))
    by now rewrite -concatF_splitF_Sp1.
rewrite (filterPF_ext_l HP1)
    {1}(concatF_splitF_Sp1 A) filterPF_castF !castF_comp.
pose (P' := widenF_S P); fold P'; pose (Pn := singleF (P ord_max)); fold Pn.
pose (A' := widenF_S A); fold A'; pose (An := singleF (A ord_max)); fold An.
pose (H := eq_trans (eq_sym (lenPF_castF (addn1 n) (concatF P' Pn)))
             (eq_trans (eq_sym (lenPF_ext HP1)) (lenPF_ind_r_in HPn))).
rewrite (castF_eq_l _ H).
extF j; unfold filterPF, funF, castF.
destruct (lt_dec (filterP_ord (cast_ord (eq_sym H) j)) n) as [Hj1 | Hj1],
    (lt_dec j (lenPF P')) as [Hj2 | Hj2].
rewrite !concatF_correct_l; f_equal; apply ord_inj; simpl.
assert (H0 : lenPF (concatF P' Pn) = lenPF P)
    by apply eq_sym, (lenPF_ext_gen (eq_sym (addn1 n))), HP1.
assert (HP1' : ∀ i, concatF P' Pn i ↔ P (cast_ord (addn1 n) i))
  by now intros; rewrite HP1; unfold castF_p1S; rewrite castF_cast_ord.
rewrite (filterP_ord_ext_gen HP1') cast_ord_comp; simpl.
assert (Hn : cast_ord (lenPF_ind_r_in_S HPn)
    (cast_ord (etrans (eq_sym H) (lenPF_ext_gen (addn1 n) HP1')) j) ≠ ord_max).
  rewrite cast_ord_comp; contradict Hj2; apply cast_ord_max_equiv in Hj2.
  rewrite Hj2; apply Nat.lt_irrefl.
rewrite filterP_ord_ind_r_in_nmax widen_S_correct.
assert (Hj : narrow_S Hn = concat_l_ord Hj2) by now apply ord_inj.
rewrite Hj; easy.
contradict Hj1; move: Hj2; rewrite !(cast_ord_val (addn1 _))
    !Nat.nlt_ge -!ord_max_ge_equiv -filterP_cast_ord.
assert (HPn' : concatF P' Pn (cast_ord (eq_sym (addn1 n)) ord_max))
    by now clear H; rewrite (concatF_splitF_Sp1 P) in HPn.
intros Hj; rewrite cast_ord_max_equiv in Hj;
    apply (filterP_ord_ind_r_in_max HPn').
rewrite !cast_ord_comp; apply ord_inj; simpl.
rewrite Hj; apply lenPF_ext; intros i; rewrite concatF_splitF_Sp1'.
unfold castF_Sp1, castF; rewrite cast_ord_comp cast_ord_id; easy.
contradict Hj2; move: Hj1; rewrite !(cast_ord_val (addn1 _))
    !Nat.nlt_ge -!ord_max_ge_equiv -filterP_cast_ord.
assert (HPn' : concatF P' Pn (cast_ord (eq_sym (addn1 n)) ord_max))
    by now clear H; rewrite (concatF_splitF_Sp1 P) in HPn.
move⇒ /(filterP_ord_ind_r_in_max_rev HPn') Hj.
rewrite !cast_ord_comp cast_ord_max_equiv in Hj; apply ord_inj; simpl.
rewrite Hj; apply lenPF_ext; intros i; rewrite concatF_splitF_Sp1'.
unfold castF_Sp1, castF; rewrite cast_ord_comp cast_ord_id; easy.
rewrite !concatF_correct_r !ord_one; easy.
Qed.

Lemma filterPF_ind_r_out :
  ∀ {n} {P : 'Prop^n.+1} (HP : ¬ P ord_max) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_r_out HP)) (filterPF (widenF_S P) (widenF_S A)).
Proof.
intros n P HPn A; apply (castF_inj (lenPF_ind_r_out HPn)); rewrite castF_can.
assert (HP1 : iffAF P (castF_p1S (concatF (widenF_S P) (singleF (P ord_max)))))
    by now rewrite -concatF_splitF_Sp1.
rewrite (filterPF_ext_l HP1)
    {1}(concatF_splitF_Sp1 A) filterPF_castF !castF_comp.
pose (P' := widenF_S P); fold P'; pose (Pn := singleF (P ord_max)); fold Pn.
pose (A' := widenF_S A); fold A'; pose (An := singleF (A ord_max)); fold An.
pose (H := eq_trans (eq_sym (lenPF_castF (addn1 n) (concatF P' Pn)))
             (eq_trans (eq_sym (lenPF_ext HP1)) (lenPF_ind_r_out HPn))).
rewrite (castF_eq_l _ H).
extF j; unfold filterPF, funF, castF.
destruct (lt_dec (filterP_ord (cast_ord (eq_sym H) j)) n) as [Hj | Hj].
rewrite concatF_correct_l; f_equal; apply ord_inj; simpl.
assert (H0 : lenPF (concatF P' Pn) = lenPF P)
    by apply eq_sym, (lenPF_ext_gen (eq_sym (addn1 n))), HP1.
assert (HP1' : ∀ i, concatF P' Pn i ↔ P (cast_ord (addn1 n) i))
  by now intros; rewrite HP1; unfold castF_p1S; rewrite castF_cast_ord.
rewrite (filterP_ord_ext_gen HP1') cast_ord_comp; simpl.
rewrite filterP_ord_ind_r_out widen_S_correct cast_ord_comp cast_ord_id; easy.
apply: (absurd _ _ HPn).
rewrite (cast_ord_val (addn1 _)) Nat.nlt_ge -ord_max_ge_equiv in Hj.
rewrite (concatF_splitF_Sp1 P) -{2}Hj -filterP_cast_ord cast_ord_comp.
apply filterP_ord_correct.
Qed.

Lemma lenPF_ind_r :
  ∀ {n} (P : 'Prop^n.+1),
    lenPF P = lenPF (widenF_S P) + lenPF (singleF (P ord_max)).
Proof.
intros n P; destruct (classic (P ord_max)) as [HP | HP].
rewrite (lenPF_ind_r_in HP) (lenPF_singleF_in HP) //.
rewrite (lenPF_ind_r_out HP) (lenPF_singleF_out HP) addn0 //.
Qed.

Lemma filterPF_ind_r :
  ∀ {n} (P : 'Prop^n.+1) (A : 'E^n.+1),
    filterPF P A =
      castF (eq_sym (lenPF_ind_r P))
        (concatF (filterPF (widenF_S P) (widenF_S A))
                 (filterPF (singleF (P ord_max)) (singleF (A ord_max)))).
Proof.
intros n P A; destruct (classic (P ord_max)) as [HP | HP].
rewrite filterPF_ind_r_in filterPF_singleF_in concatF_castF_r castF_comp.
apply castF_eq_l.
rewrite filterPF_ind_r_out (concatF_nil_r' (lenPF_singleF_out HP)) castF_comp.
apply castF_eq_l.
Qed.

Lemma lenPF_concatF :
  ∀ {n1 n2} (P1 : 'Prop^n1) (P2 : 'Prop^n2),
    lenPF (concatF P1 P2) = lenPF P1 + lenPF P2.
Proof.
intros n1 n2 P1 P2; induction n2 as [| n2 IHn2].
rewrite concatF_nil_r lenPF_castF lenPF_nil addn0; easy.
rewrite -(lenPF_castF (addnS n1 n2)) lenPF_ind_r (lenPF_ind_r P2).
rewrite widenF_S_concatF concatF_last IHn2 addnA; easy.
Qed.

Lemma filterPF_concatF :
  ∀ {n1 n2} P1 P2 (A1 : 'E^n1) (A2 : 'E^n2),
    filterPF (concatF P1 P2) (concatF A1 A2) =
      castF (eq_sym (lenPF_concatF P1 P2))
            (concatF (filterPF P1 A1) (filterPF P2 A2)).
Proof.
intros n1 n2; induction n2 as [| n2 IHn2]; intros P1 P2 A1 A2.
assert (HP : iffAF (concatF P1 P2) (castF (addn0_sym n1) P1))
    by now rewrite concatF_nil_r.
rewrite (filterPF_ext_l HP) (concatF_nil_r A1) filterPF_castF
    (concatF_nil_r' (lenPF_nil P2) (filterPF _ _)).
rewrite !castF_comp; apply castF_eq_l.
apply (castF_inj (eq_sym (lenPF_castF (addnS n1 n2) (concatF P1 P2)))).
rewrite -filterPF_castF filterPF_ind_r (filterPF_ind_r P2) castF_comp.
assert (HP : iffAF (widenF_S (castF (addnS n1 n2) (concatF P1 P2)))
    (concatF P1 (widenF_S P2))) by now rewrite widenF_S_concatF.
rewrite (filterPF_ext_l HP) (widenF_S_concatF A1 A2) IHn2.
assert (HPmax : iffAF (singleF (castF (addnS n1 n2) (concatF P1 P2) ord_max))
    (singleF (P2 ord_max))) by now rewrite concatF_last.
rewrite (filterPF_ext_l HPmax) (concatF_last A1 A2).
rewrite !concatF_castF_l !concatF_castF_r concatF_assoc_l !castF_comp.
apply castF_eq_l.
Qed.

Lemma lenPF_splitF :
  ∀ {n1 n2} (P : 'Prop^(n1 + n2)),
    lenPF P = lenPF (firstF P) + lenPF (lastF P).
Proof. intros; rewrite -lenPF_concatF -concatF_splitF //. Qed.

Lemma filterPF_splitF :
  ∀ {n1 n2} P (A : 'E^(n1 + n2)),
    filterPF P A =
      castF (eq_sym (lenPF_splitF P))
            (concatF (filterPF (firstF P) (firstF A))
                     (filterPF (lastF P) (lastF A))).
Proof.
intros n1 n2 P A.
assert (HP : iffAF P (concatF (firstF P) (lastF P)))
    by now rewrite -concatF_splitF.
rewrite (filterPF_ext_l HP) {1}(concatF_splitF A) filterPF_concatF.
rewrite !castF_comp; apply castF_eq_l.
Qed.

Lemma filterPF_firstF :
  ∀ {n1 n2} P (A : 'E^(n1 + n2)),
    filterPF (firstF P) (firstF A) =
      firstF (castF (lenPF_splitF P) (filterPF P A)).
Proof. intros; rewrite filterPF_splitF castF_can firstF_concatF; easy. Qed.

Lemma filterPF_lastF :
  ∀ {n1 n2} P (A : 'E^(n1 + n2)),
    filterPF (lastF P) (lastF A) =
      lastF (castF (lenPF_splitF P) (filterPF P A)).
Proof. intros; rewrite filterPF_splitF castF_can lastF_concatF; easy. Qed.

Lemma lenPF_permutF :
  ∀ {n p} {P : 'Prop^n}, injective p → lenPF (permutF p P) = lenPF P.
Proof.
intros [| n] p P Hp; [rewrite !lenPF_nil; easy |].
apply (bijS_eq_card p), (injS_surjS_bijS I_S_nonempty);
    [apply funS_correct; easy | move=>> _ _; apply Hp |].
intros i Hi; ∃ (f_inv (injF_bij Hp) i); unfold permutF;
    rewrite f_inv_can_r; easy.
Qed.

Lemma lenPF_permutF_f_inv_l :
  ∀ {n} {p : [0..n) → [0..n)} (Hp : injective p) (P : 'Prop^n),
    let q := f_inv (injF_bij Hp) in
    lenPF (permutF q (permutF p P)) = lenPF P.
Proof. intros; rewrite -permutF_f_inv_l; easy. Qed.

Lemma lenPF_permutF_f_inv_r :
  ∀ {n} {p : [0..n) → [0..n)} (Hp : injective p) (P : 'Prop^n),
    let q := f_inv (injF_bij Hp) in
    lenPF (permutF p (permutF q P)) = lenPF P.
Proof. intros; rewrite -permutF_f_inv_r; easy. Qed.

Lemma filterPF_permutF :
  ∀ {n} {p : [0..n) → [0..n)} {P : 'Prop^n}
      {i0} (HP0 : P (p i0)) {A : 'E^n},
    injective p →
    filterPF (permutF p P) (permutF p A) =
      funF (filterP_f_ord p HP0) (filterPF P A).
Proof.
move=>> Hp; extF; unfold filterPF, permutF, funF; f_equal.
rewrite filterP_f_ord_correct//; apply extendPF_funF, incl_RgF; easy.
Qed.

Lemma lenPF_revF : ∀ {n} {P : 'Prop^n}, lenPF (revF P) = lenPF P.
Proof. intros; apply lenPF_permutF, rev_ord_inj. Qed.

Lemma filterPF_revF :
  ∀ {n} {P : 'Prop^n} {i0} (HP0 : P (rev_ord i0)) {A : 'E^n},
    filterPF (revF P) (revF A) =
      funF (filterP_f_ord (@rev_ord n) HP0) (filterPF P A).
Proof. intros n P i0 HP0 A; apply (filterPF_permutF HP0 rev_ord_inj). Qed.

Lemma lenPF_moveF :
  ∀ {n} i0 i1 {P : 'Prop^n.+1}, lenPF (moveF i0 i1 P) = lenPF P.
Proof. intros; apply lenPF_permutF, move_ord_inj. Qed.

Lemma filterPF_moveF :
  ∀ {n} i0 i1 {P : 'Prop^n.+1} {j0} (HP : P (move_ord i0 i1 j0))
      {A : 'E^n.+1},
    filterPF (moveF i0 i1 P) (moveF i0 i1 A) =
      funF (filterP_f_ord (move_ord i0 i1) HP) (filterPF P A).
Proof.
intros n i0 i1 P j0 HP A; apply (filterPF_permutF HP (move_ord_inj _ _)).
Qed.

Lemma lenPF_transpF :
  ∀ {n} i0 i1 {P : 'Prop^n}, lenPF (transpF i0 i1 P) = lenPF P.
Proof. intros; apply lenPF_permutF, transp_ord_inj. Qed.

Lemma filterPF_transpF :
  ∀ {n} i0 i1 {P : 'Prop^n} {j0} (HP : P (transp_ord i0 i1 j0)) {A : 'E^n},
    filterPF (transpF i0 i1 P) (transpF i0 i1 A) =
      funF (filterP_f_ord (transp_ord i0 i1) HP) (filterPF P A).
Proof.
intros n i0 i1 P j0 HP A; apply (filterPF_permutF HP (transp_ord_inj _ _)).
Qed.

End FilterPF_Facts1.

Section FilterPF_Facts2.

Context {E : Type}.

More properties of operators lenPF/filterPF.

Lemma lenPF_firstF_in :
  ∀ {n1 n2} {P : [0..n1 + n2) → Prop},
    (∀ i1, firstF P i1) → lenPF P = n1 + lenPF (lastF P).
Proof.
intros n1 n2 P HP; induction n1 as [| n1 Hn1].
rewrite {1}(concatF_splitF P) concatF_nil_l (add0n (lenPF _)); easy.
assert (H0 : castF (addSn n1 n2) P ord0)
    by now move: (HP ord0); rewrite firstF_0 (castF_eq _ ord0).
rewrite -(lenPF_castF (addSn n1 n2)) (lenPF_ind_l_in H0) Hn1;
    fold (liftF_S (castF (addSn n1 n2) P)).
rewrite -add1n addnA; f_equal; rewrite -lastF_S1p lastF2.
unfold castF_S1p; rewrite !castF_comp castF_id; easy.
intros; rewrite -liftF_S_firstF; apply HP.
Qed.

Lemma lenPF_firstF_out :
  ∀ {n1 n2} {P : [0..n1 + n2) → Prop},
    (∀ i1, ¬ firstF P i1) → lenPF P = lenPF (lastF P).
Proof.
intros n1 n2 P HP; induction n1 as [| n1 Hn1].
rewrite {1}(concatF_splitF P) concatF_nil_l; easy.
assert (H0 : ¬ castF (addSn n1 n2) P ord0)
    by now move: (HP ord0); rewrite firstF_0 (castF_eq _ ord0).
rewrite -(lenPF_castF (addSn n1 n2)) (lenPF_ind_l_out H0) Hn1;
    fold (liftF_S (castF (addSn n1 n2) P)).
f_equal; rewrite -lastF_S1p lastF2.
unfold castF_S1p; rewrite !castF_comp castF_id; easy.
intros; rewrite -liftF_S_firstF; apply HP.
Qed.

Lemma lenPF_lastF_in :
  ∀ {n1 n2} {P : [0..n1 + n2) → Prop},
    (∀ i2, lastF P i2) → lenPF P = lenPF (firstF P) + n2.
Proof.
intros; rewrite firstF_lastF_eq_revF
    lenPF_revF (addnC (lenPF _)) -lenPF_firstF_in.
rewrite lenPF_castF lenPF_revF; easy.
intro; rewrite -revF_lastF revF_correct; easy.
Qed.

Lemma lenPF_lastF_out :
  ∀ {n1 n2} {P : [0..n1 + n2) → Prop},
    (∀ i2, ¬ lastF P i2) → lenPF P = lenPF (firstF P).
Proof.
intros; rewrite firstF_lastF_eq_revF lenPF_revF -lenPF_firstF_out.
rewrite lenPF_castF lenPF_revF; easy.
intro; rewrite -revF_lastF revF_correct; easy.
Qed.

Lemma lenPF_extendPF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)}
      {P1 : [0..n1) → Prop} {P2 : [0..n2) → Prop},
    injective f → extendPF f P1 P2 → lenPF P1 = lenPF P2.
Proof.
intros n1 n2 f P1 P2 Hf HP.
pose (Hn := eq_sym (subnKC (injF_leq Hf))).
pose (g i1 := cast_ord Hn (f i1)).
assert (Hg1 : ∀ i1, f i1 = cast_ord (eq_sym Hn) (g i1))
    by now intros; rewrite cast_ord_comp cast_ord_id.
assert (Hg2 : injective g) by now unfold g; move=>> /cast_ord_inj /Hf.
destruct (first_f_extendF Hg2) as [p [Hp Hp1]]; move: Hp ⇒ /bij_inj Hp2.
rewrite -(lenPF_castF Hn) -(lenPF_permutF Hp2) lenPF_lastF_out.
apply lenPF_ext; intros i1; destruct (HP (f i1)) as [[j1 [Hj1 Hj2]] | [H _]];
    [| exfalso; rewrite Rg_compl in H; apply (H i1); easy].
apply Hf in Hj1; subst; rewrite Hj2 Hg1 Hp1 firstF_permutF; easy.
intros j; destruct (HP (cast_ord (eq_sym Hn) (p (last_ord n1 j))))
    as [[j1 [Hj _]] | [_ H]]; [exfalso | easy].
rewrite Hg1 Hp1 in Hj; apply cast_ord_inj, Hp2, ord_compat in Hj.
destruct j1 as [j1 Hj1]; simpl in Hj.
move: (leq_addr j n1); rewrite -Hj leqNgt; contradict Hj1; apply /negP; easy.
Qed.

Lemma filterP_ord_Rg_aux1 :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    let H := lenPF_extendPF (incrF_inj Hf) HP in
    Rg (fun j1 : [0..lenPF P1) ⇒ f (filterP_ord j1)) = image f P1.
Proof.
intros; apply subset_ext_equiv; split; intros i2 Hi2.
destruct Hi2 as [j1 _]; apply Im, filterP_ord_correct.
destruct Hi2 as [i1 Hi1].
rewrite -(filterP_unfilterP_ord_in Hi1 i1 Hi1).
apply: (Im _ _ (unfilterP_ord Hi1 i1)); easy.
Qed.

Lemma filterP_ord_Rg_aux2 :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    image f P1 = P2.
Proof.
intros n1 n2 f Hf P1 P2 HP; apply subset_ext_equiv; split; intros i2.
intros [i1 Hi1]; destruct (HP (f i1)) as [[k1 [Hk1a Hk1b]] | [Hk1 _]].
rewrite -Hk1b (incrF_inj Hf _ _ Hk1a); easy.
exfalso; apply Hk1; easy.
destruct (extendPF_funF_rev (incrF_inj Hf) HP) as [-> HP2].
intros Hi2; destruct (HP2 i2 Hi2) as [i1 _]; apply Im; easy.
Qed.

Lemma filterP_ord_Rg_aux3 :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    let H := lenPF_extendPF (incrF_inj Hf) HP in
    P2 = Rg (fun j1 ⇒ filterP_ord (cast_ord H j1)).
Proof.
intros n1 n2 f Hf P1 P2 HP H.
apply subset_ext_equiv; split; intros i2 HP2;
    [| inversion HP2; apply filterP_ord_correct].
destruct (im_dec f i2) as [[i1 <-] | Hi2].
apply Rg_ex; ∃ (cast_ord (eq_sym H) (unfilterP_ord HP2 (f i1))).
rewrite cast_ord_comp cast_ord_id filterP_unfilterP_ord_in; easy.
contradict Hi2; rewrite not_all_not_ex_equiv.
destruct (HP i2) as [[i1 Hi1] | Hi2]; [∃ i1 |]; easy.
Qed.

Lemma filterP_ord_Rg :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    let H := lenPF_extendPF (incrF_inj Hf) HP in
    Rg (fun j1 : [0..lenPF P1) ⇒ f (filterP_ord j1)) =
      Rg (fun j1 ⇒ filterP_ord (cast_ord H j1)).
Proof.
intros n1 n2 f Hf P1 P2 HP H.
rewrite (filterP_ord_Rg_aux1 Hf HP).
rewrite (filterP_ord_Rg_aux2 Hf HP).
apply filterP_ord_Rg_aux3.
Qed.

"w" stands for "when".
Lemma filterP_ord_w_incrF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f) {P1 : 'Prop^n1}
      {P2 : 'Prop^n2} (HP : extendPF f P1 P2),
    let H := lenPF_extendPF (incrF_inj Hf) HP in
    f \o filterP_ord = filterP_ord \o (cast_ord H).
Proof.
intros; apply fun_ext_incrF_Rg.
apply incrF_comp; [apply filterP_ord_incrF | easy].
apply filterP_cast_ord_incrF.
apply filterP_ord_Rg.
Qed.

"w" stands for "when".
Lemma filterP_f_ord_w_incrF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : incrF f)
      {P1 : 'Prop^n1} {P2 : 'Prop^n2} (HP : extendPF f P1 P2)
      {i1} (HP2 : P2 (f i1)),
    let H := lenPF_extendPF (incrF_inj Hf) HP in
    filterP_f_ord f HP2 = cast_ord H.
Proof.
intros n1 n2 f Hf P1 P2 HP i1 HP2 H; extF.
move: (extendPF_unfunF_rev (incrF_inj Hf) HP) ⇒ HP2'; subst.
rewrite (filterP_f_ord_correct_alt _ (incrF_inj Hf) HP) -(comp_correct _ f).
rewrite (filterP_ord_w_incrF Hf HP); easy.
Qed.

Lemma len_neqF_funF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {A2 : 'E^n2} {x0},
    injective f → incl (neqF A2 x0) (Rg f) →
    lenPF (neqF (funF f A2) x0) = lenPF (neqF A2 x0).
Proof.
move=>> Hf HA2; apply (lenPF_extendPF Hf), (extendPF_funF_neqF_equiv Hf); easy.
Qed.

Lemma filterPF_funF :
  ∀ {F : Type} {n1 n2} {f : [0..n1) → [0..n2)}
      {P1 : 'Prop^n1} {P2 : 'Prop^n2}
      {i0} (HP0 : P2 (f i0)) {A2 : 'F^n2},
    injective f → extendPF f P1 P2 →
    filterPF P1 (funF f A2) = funF (filterP_f_ord f HP0) (filterPF P2 A2).
Proof.
move=>> Hf HP; extF; destruct (extendPF_funF_rev Hf HP) as [HP1 HP2]; subst.
unfold filterPF, funF; f_equal.
rewrite filterP_f_ord_correct; easy.
Qed.

Lemma len_neqF_unfunF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {x0} {A1 : 'E^n1},
    injective f → lenPF (neqF A1 x0) = lenPF (neqF (unfunF f x0 A1) x0).
Proof.
move=>> Hf; apply (lenPF_extendPF Hf), (extendPF_unfunF_neqF _ _ Hf).
Qed.

Lemma filterPF_unfunF :
  ∀ {F : Type} {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f)
      {P1 : [0..n1) → Prop} {P2 : [0..n2) → Prop} (HP : extendPF f P1 P2)
      x0 {A1 : 'F^n1} i1,
    P1 i1 →
    let q1 := proj1_sig (injF_restr_bij_EX Hf) in
    let Hq1a := proj1 (proj2_sig (injF_restr_bij_EX Hf)) in
    let Hq1b := bij_inj Hq1a in
    filterPF P2 (unfunF f x0 A1) =
      castF (lenPF_extendPF Hf HP) (castF (lenPF_permutF Hq1b)
        (filterPF (permutF q1 P1) (permutF q1 A1))).
Proof.
intros F n1 n2 f Hf P1 P2 HP x0 A1 i1 HP1 q1 Hq1a Hq1b.
pose (Hq1c := proj2 (proj2_sig (injF_restr_bij_EX Hf)));
    pose (p1 := f_inv Hq1a); fold q1 in Hq1a, Hq1b, Hq1c.
assert (HP1' : P1 (q1 (p1 i1))) by now unfold p1; rewrite f_inv_can_r.
assert (HP2 : P2 (f i1))
    by now rewrite (extendPF_unfunF_rev Hf HP) (unfunF_correct_l _ i1).
assert (HP2' : P2 (f (q1 (p1 i1)))) by now rewrite f_inv_can_r.
rewrite (filterPF_permutF HP1' Hq1b) -{2}(funF_unfunF x0 Hf A1).
rewrite (filterPF_funF HP2' Hf HP).
extF; unfold castF, funF; f_equal.
rewrite -(filterP_f_ord_comp_l HP1' Hq1b).
rewrite (filterP_f_ord_w_incrF Hq1c (extendPF_incrF Hf HP)).
rewrite 2!cast_ord_comp cast_ord_id; easy.
Qed.

End FilterPF_Facts2.

Section FilterPF_Facts3.

Context {E : Type}.

Properties of operators filter_eqF_gen/filter_eqF, and filter_neqF_gen/filter_neqF.

Lemma filter_neqF_gen_funF :
  ∀ {F : Type} {n1 n2} {f : [0..n1) → [0..n2)}
      {x0} {A2 : 'E^n2} {B2 : 'F^n2} {i0} (HP0 : neqF A2 x0 (f i0)),
    injective f → incl (neqF A2 x0) (Rg f) →
    filter_neqF_gen x0 (funF f A2) (funF f B2) =
      funF (filterP_f_ord f HP0) (filter_neqF_gen x0 A2 B2).
Proof.
intros F n1 n2 f x0 A2 B2 i0 HP0 Hf HA2; unfold filter_neqF_gen.
apply (filterPF_funF HP0 Hf), (extendPF_funF_neqF Hf HA2).
Qed.

Lemma filter_neqF_gen_funF_r :
  ∀ {F : Type} {n1 n2} {f : [0..n1) → [0..n2)}
      {x0} y0 {A2 : 'E^n2} {B1 : 'F^n1} {i0} (HP0 : neqF A2 x0 (f i0)),
    injective f → incl (neqF A2 x0) (Rg f) →
    filter_neqF_gen x0 (funF f A2) B1 =
      funF (filterP_f_ord f HP0) (filter_neqF_gen x0 A2 (unfunF f y0 B1)).
Proof. intros; rewrite -filter_neqF_gen_funF// funF_unfunF//. Qed.

Lemma filter_neqF_funF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} {x0} {A2 : 'E^n2}
      {i0} (HP0 : neqF A2 x0 (f i0)),
    injective f → incl (neqF A2 x0) (Rg f) →
    filter_neqF x0 (funF f A2) =
      funF (filterP_f_ord f HP0) (filter_neqF x0 A2).
Proof.
intros n1 n2 f x0 A2 i0 HP0 Hf HA2.
rewrite (filter_neqF_gen_funF_r x0 HP0)// unfunF_funF//; repeat f_equal.
extF i2; destruct (im_dec f i2) as [[i1 <-] | Hi2].
apply maskPF_correct_l; easy.
rewrite -incl_compl_equiv in HA2; specialize (HA2 i2);
    unfold compl, neqF in HA2; rewrite NNPP_equiv in HA2.
rewrite HA2; [ apply maskPF_correct_r |]; rewrite Rg_compl; easy.
Qed.

Lemma filter_neqF_gen_unfunF :
  ∀ {F : Type} {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f)
      x0 y0 (A1 : 'E^n1) (B1 : 'F^n1),
    let q1 := proj1_sig (injF_restr_bij_EX Hf) in
    let Hq1a := proj1 (proj2_sig (injF_restr_bij_EX Hf)) in
    let Hq1b := bij_inj Hq1a in
    filter_neqF_gen x0 (unfunF f x0 A1) (unfunF f y0 B1) =
      castF (len_neqF_unfunF Hf) (castF (lenPF_permutF Hq1b)
        (filter_neqF_gen x0 (permutF q1 A1) (permutF q1 B1))).
Proof.
intros F n1 n2 f Hf x0 y0 A1 B1 q1 Hq1a Hq1b.
destruct (classic (∀ i1, A1 i1 = x0)) as [HA1 | HA1].
apply eqAF_nil; left; apply lenPF0_alt; intuition.
move: HA1 ⇒ /not_all_ex_not_equiv [i0 Hi0].
move: (extendPF_unfunF_neqF A1 x0 Hf) ⇒ HP.
rewrite (filterPF_unfunF Hf HP _ i0);
    [rewrite !castF_comp; apply castF_eq_r |]; easy.
Qed.

Lemma filter_neqF_gen_unfunF_l :
  ∀ {F : Type} (HF : inhabited F) {n1 n2}
      {f : [0..n1) → [0..n2)} (Hf : injective f) x0 (A1 : 'E^n1) (B2 : 'F^n2),
    let q1 := proj1_sig (injF_restr_bij_EX Hf) in
    let Hq1a := proj1 (proj2_sig (injF_restr_bij_EX Hf)) in
    let Hq1b := bij_inj Hq1a in
    filter_neqF_gen x0 (unfunF f x0 A1) B2 =
      castF (len_neqF_unfunF Hf) (castF (lenPF_permutF Hq1b)
        (filter_neqF_gen x0 (permutF q1 A1) (permutF q1 (funF f B2)))).
Proof.
intros F [y0] n1 n2 f Hf x0 A1 B2 q1 Hq1a Hq1b.
rewrite -(filter_neqF_gen_unfunF _ _ y0); apply filterPF_ext_r; intro.
move⇒ /(proj1 contra_equiv (unfunF_correct_r _ _)) /not_all_not_ex [i1 <-].
rewrite unfunF_funF// maskPF_correct_l//.
Qed.

Lemma filter_neqF_unfunF :
  ∀ {n1 n2} {f : [0..n1) → [0..n2)} (Hf : injective f) x0 (A1 : 'E^n1),
    let q1 := proj1_sig (injF_restr_bij_EX Hf) in
    let Hq1a := proj1 (proj2_sig (injF_restr_bij_EX Hf)) in
    let Hq1b := bij_inj Hq1a in
    filter_neqF x0 (unfunF f x0 A1) =
      castF (len_neqF_unfunF Hf) (castF (lenPF_permutF Hq1b)
        (filter_neqF x0 (permutF q1 A1))).
Proof.
intros; rewrite filter_neqF_gen_unfunF_l;
  [rewrite funF_unfunF; easy | apply (inhabits x0)].
Qed.

End FilterPF_Facts3.

Section MapF_Facts.

Properties of operators mapiF/mapF/map2F.

Context {E F G : Type}.

Lemma mapF_nil :
  ∀ {n} (f : E → F) (A : 'E^n) (B : 'F^n), n = 0 → mapF f A = B.
Proof. intros; subst; apply hat0F_unit; easy. Qed.

Lemma mapF_comp :
  ∀ {n} (f : E → F) (g : F → G) (A : 'E^n),
    mapF (g \o f) A = mapF g (mapF f A).
Proof. easy. Qed.

Lemma mapF_eq :
  ∀ {n} (f : E → F) (A B : 'E^n), A = B → mapF f A = mapF f B.
Proof. intros; f_equal; easy. Qed.

Lemma mapF_inj :
  ∀ {n} {f : E → F}, injective f → injective (mapF f : 'E^n → 'F^n).
Proof. move⇒ n f Hf A B /extF_rev H; extF; apply Hf, H. Qed.

Lemma mapF_eq_f :
  ∀ {n} (f g : E → F) (A : 'E^n), f = g → mapF f A = mapF g A.
Proof. intros; f_equal; easy. Qed.

Lemma mapF_inj_f :
  ∀ n (f g : E → F),
    (∀ (A : 'E^n.+1), mapF f A = mapF g A) → f = g.
Proof.
intros n f g H; fun_ext x.
apply (extF_rev _ _ (H (constF n.+1 x)) ord0).
Qed.

Lemma mapF_constF :
  ∀ {n} (f : E → F) x, mapF f (constF n x) = constF n (f x).
Proof. easy. Qed.

Lemma mapF_singleF :
  ∀ (f : E → F) x0, mapF f (singleF x0) = singleF (f x0).
Proof. easy. Qed.

Lemma mapF_coupleF :
  ∀ (f : E → F) x0 x1, mapF f (coupleF x0 x1) = coupleF (f x0) (f x1).
Proof.
intros; extF.
rewrite mapF_correct; unfold coupleF; destruct (ord2_dec _); easy.
Qed.

Lemma mapF_tripleF :
  ∀ (f : E → F) x0 x1 x2,
    mapF f (tripleF x0 x1 x2) = tripleF (f x0) (f x1) (f x2).
Proof.
intros; extF; rewrite mapF_correct; unfold tripleF;
    destruct (ord3_dec _) as [[H | H] | H]; easy.
Qed.

Lemma mapF_inF :
  ∀ {n} (f : E → F) x (A : 'E^n), inF x A → inF (f x) (mapF f A).
Proof. intros n f x A [i Hi]; ∃ i; rewrite Hi; easy. Qed.

Lemma mapF_inclF :
  ∀ {n} (f : E → F) (A : 'E^n) PE,
    inclF A PE → inclF (mapF f A) (image f PE).
Proof. easy. Qed.

Lemma mapF_invalF :
  ∀ {n1 n2} (f : E → F) (A1 : 'E^n1) (A2 : 'E^n2),
    invalF A1 A2 → invalF (mapF f A1) (mapF f A2).
Proof. intros n1 n2 f A1 A2 HA i1; apply mapF_inF; easy. Qed.

Lemma mapF_castF :
  ∀ {n1 n2} (H : n1 = n2) (f : E → F) (A1 : 'E^n1),
    mapF f (castF H A1) = castF H (mapF f A1).
Proof. easy. Qed.

Lemma mapF_firstF :
  ∀ {n1 n2} (f : E → F) (A : 'E^(n1 + n2)),
    mapF f (firstF A) = firstF (mapF f A).
Proof. easy. Qed.

Lemma mapF_lastF :
  ∀ {n1 n2} (f : E → F) (A : 'E^(n1 + n2)),
    mapF f (lastF A) = lastF (mapF f A).
Proof. easy. Qed.

Lemma mapF_concatF :
  ∀ {n1 n2} (f : E → F) (A1 : 'E^n1) (A2 : 'E^n2),
    mapF f (concatF A1 A2) = concatF (mapF f A1) (mapF f A2).
Proof.
intros; extF; rewrite mapF_correct.
unfold concatF; destruct (lt_dec _ _); easy.
Qed.

Lemma mapF_insertF :
  ∀ {n} (f : E → F) i0 x0 (A : 'E^n),
    mapF f (insertF i0 x0 A) = insertF i0 (f x0) (mapF f A).
Proof.
intros; extF; rewrite mapF_correct.
unfold insertF; destruct (ord_eq_dec _ _); easy.
Qed.

Lemma mapF_insert2F :
  ∀ {n} (f : E → F) {i0 i1} (H : i1 ≠ i0) x0 x1 (A : 'E^n),
    mapF f (insert2F H x0 x1 A) = insert2F H (f x0) (f x1) (mapF f A).
Proof. intros; rewrite 2!insert2F_correct 2!mapF_insertF; easy. Qed.

Lemma mapF_skipF :
  ∀ {n} (f : E → F) i0 (A : 'E^n.+1),
    mapF f (skipF i0 A) = skipF i0 (mapF f A).
Proof. easy. Qed.

Lemma mapF_skip2F :
  ∀ {n} (f : E → F) {i0 i1} (H : i1 ≠ i0) (A : 'E^n.+2),
    mapF f (skip2F H A) = skip2F H (mapF f A).
Proof. easy. Qed.

Lemma mapF_replaceF :
  ∀ {n} (f : E → F) i0 x0 (A : 'E^n),
    mapF f (replaceF i0 x0 A) = replaceF i0 (f x0) (mapF f A).
Proof.
intros; rewrite 2!replaceF_equiv_def_skipF mapF_skipF mapF_insertF; easy.
Qed.

Lemma mapF_replace2F :
  ∀ {n} (f : E → F) i0 i1 x0 x1 (A : 'E^n),
    mapF f (replace2F i0 i1 x0 x1 A) =
      replace2F i0 i1 (f x0) (f x1) (mapF f A).
Proof. intros; rewrite 2!mapF_replaceF; easy. Qed.

Lemma map2F_concatF :
  ∀ {n1 n2} (f : E → F → G)
      (A1 : 'E^n1) (B1 : 'F^n1) (A2 : 'E^n2) (B2 : 'F^n2),
    map2F f (concatF A1 A2) (concatF B1 B2) =
      concatF (map2F f A1 B1) (map2F f A2 B2).
Proof.
intros; extF; rewrite map2F_correct.
unfold concatF; destruct (lt_dec _ _); easy.
Qed.

Lemma map2F_skipF :
  ∀ {n} (f : E → F → G) i0 (A : 'E^n.+1) (B : 'F^n.+1),
    map2F f (skipF i0 A) (skipF i0 B) = skipF i0 (map2F f A B).
Proof. easy. Qed.

End MapF_Facts.

Section CompF_Facts.

Properties of operators pullbackF/compF_r/compF.

Context {E F G H : Type}.

Lemma pullbackF_id : ∀ {n} (f : '(F → G)^n), pullbackF id f = f.
Proof. easy. Qed.

Lemma pullbackF_comp :
  ∀ {n} (h : E → F) (g : F → G) (f : '(G → H)^n),
    pullbackF (g \o h) f = pullbackF h (pullbackF g f).
Proof. easy. Qed.

End CompF_Facts.

Section Fun_Def.

Definition FTd (T : Type) (d : nat) := 'T^d → T.
Definition FTdm (T : Type) (d m : nat) := 'T^d → 'T^m.

Definition FRd d := FTd R d.
Definition FRdm d m := FTdm R d m.

End Fun_Def.

Section Fun_Facts1.

Context {E F G : Type}.
Context {n n1 n2 : nat}.
Hypothesis Heq : n1 = n2.
Hypothesis Hle : n1 ≤ n2.

Variable p : [0..n) → [0..n).

Variable f : [0..n) → E → F.
Variable fS : [0..n] → E → F.
Variable f1 : [0..n1) → E → F.
Variable f2 : [0..n2) → E → F.
Variable f12 : [0..n1 + n2) → E → F.
Variable ff : E → F → G.

Lemma fct_castF_eq : ∀ x, castF Heq (f1^~ x) = (castF Heq f1)^~ x.
Proof. easy. Qed.

Lemma fct_widenF_S_eq : ∀ x, widenF_S (fS^~ x) = (widenF_S fS)^~ x.
Proof. easy. Qed.

Lemma fct_liftF_S_eq : ∀ x, liftF_S (fS^~ x) = (liftF_S fS)^~ x.
Proof. easy. Qed.

Lemma fct_widenF_eq : ∀ x, widenF Hle (f2^~ x) = (widenF Hle f2)^~ x.
Proof. easy. Qed.

Lemma fct_firstF_eq : ∀ x, firstF (f12^~ x) = (firstF f12)^~ x.
Proof. easy. Qed.

Lemma fct_lastF_eq : ∀ x, lastF (f12^~ x) = (lastF f12)^~ x.
Proof. easy. Qed.

Lemma fct_concatF_eq :
  ∀ x, concatF (f1^~ x) (f2^~ x) = (concatF f1 f2)^~ x.
Proof.
intros; extF i; destruct (lt_dec i n1) as [Hi | Hi];
    [rewrite !concatF_correct_l | rewrite !concatF_correct_r]; easy.
Qed.

Lemma fct_insertF_eq :
  ∀ i0 f0 x, insertF i0 (f0 x) (f^~ x) = (insertF i0 f0 f)^~ x.
Proof.
intros i0 f0 x; extF i;
    destruct (ord_eq_dec i i0) as [Hi | Hi];
    [rewrite !insertF_correct_l | rewrite !insertF_correct_r]; easy.
Qed.

Lemma fct_skipF_eq : ∀ i0 x, skipF i0 (fS^~ x) = skipF i0 fS ^~ x.
Proof. easy. Qed.

Lemma fct_replaceF_eq :
  ∀ i0 f0 x, replaceF i0 (f0 x) (fS^~ x) = replaceF i0 f0 fS ^~ x.
Proof.
intros i0 f0 x; extF i;
    destruct (ord_eq_dec i i0) as [Hi | Hi];
    [rewrite !replaceF_correct_l | rewrite !replaceF_correct_r]; easy.
Qed.

Lemma fct_permutF_eq : ∀ x, permutF p (f^~ x) = (permutF p f)^~ x.
Proof. easy. Qed.

Lemma fct_mapF_f_eq :
  ∀ x {p} (y : 'E^p), mapF (ff^~ x) y = (mapF ff y)^~ x.
Proof. easy. Qed.

End Fun_Facts1.

Section Fun_Facts2a.

Context {E F : Type}.
Context {PE : E → Prop}.
Variable f : E → F.

Lemma image_inclF :
  ∀ {n} {A : 'E^n}, inclF A PE → inclF (mapF f A) (image f PE).
Proof. intros n A HA i; rewrite mapF_correct; apply Im; easy. Qed.

End Fun_Facts2a.

Section Fun_Facts2b.

Context {E F : Type}.
Context {PF : F → Prop}.
Context {f : E → F}.
Hypothesis Hf : bijective f.

Lemma preimage_inclF :
  ∀ {n} {B : 'F^n},
    inclF B PF → inclF (mapF (f_inv Hf) B) (preimage f PF).
Proof. move=>>; rewrite -f_inv_image; apply image_inclF. Qed.

End Fun_Facts2b.

Section Swap_fun.

Context {E F : Type}.

Definition gather {n} : '(E → F)^n → E → 'F^n := swap.
Definition scatter {n} : (E → 'F^n) → '(E → F)^n := swap.

Lemma gather_eq : ∀ {n} (f : '(E → F)^n) x i, gather f x i = f i x.
Proof. easy. Qed.

Lemma scatter_eq : ∀ {n} (f : E → 'F^n) i x, scatter f i x = f x i.
Proof. easy. Qed.

Lemma gather_scatter : ∀ {n} (f : E → 'F^n), gather (scatter f) = f.
Proof. easy. Qed.

Lemma scatter_gather : ∀ {n} (f : '(E → F)^n), scatter (gather f) = f.
Proof. easy. Qed.

End Swap_fun.

We need to mask seq from MC.
From Coq Require Import List.

Section FF_list.

Context {E : Type}.

Fixpoint to_listF {n:nat} (A:'E^n) : list E :=
   match n as p return (n=p → _) with
   | 0 ⇒ fun⇒ nil
   | S m ⇒ fun H ⇒ cons ((castF H A) ord0)
                 (to_listF (liftF_S (castF H A)))
end erefl.

Lemma to_listF_correct : ∀ {n} (elt: E) (A:'E^n) (i:[0..n)),
    A i = nth i (to_listF A) elt.
Proof.
intros n; induction n; intros elt A i; simpl; [destruct i; easy |].
case (ord_eq_dec i ord0); intros Hi.
rewrite Hi; simpl.
rewrite castF_id; easy.
case_eq (nat_of_ord i).
intros Hi2; exfalso; apply Hi.
apply ord_inj; rewrite Hi2; easy.
intros m Hm; rewrite castF_id.
rewrite <- (liftF_lower_S A Hi).
rewrite (IHn elt).
f_equal.
simpl; rewrite Hm -minusE; now auto with zarith.
Qed.

Lemma to_listF_length : ∀ {n} (A:'E^n),
    length (to_listF A) = n.
Proof.
induction n.
intros A; simpl; easy.
intros A; simpl.
rewrite IHn; easy.
Qed.

Lemma to_listF_castF : ∀ {n m} (H:n=m) A,
   to_listF A = to_listF (castF H A).
Proof.
intros n m H A; subst.
rewrite castF_id; easy.
Qed.

Lemma to_listF_concatF: ∀ {n m} (A:'E^n) (B:'E^m),
  to_listF (concatF A B) = to_listF A ++ to_listF B.
Proof.
intros n m A B; induction n.
simpl; rewrite concatF_nil_l; easy.
simpl; rewrite 2!castF_id.
f_equal.
rewrite concatF_correct_l.
f_equal; apply ord_inj; now simpl.
apply trans_eq with (to_listF (liftF_S (castF (addSn n m) (concatF A B)))).
f_equal.
extF.
unfold liftF_S, castF; f_equal.
apply ord_inj; easy.
rewrite liftF_S_concatF.
apply IHn.
Qed.

Definition of_listF : ∀ l : list E, 'E^(length l) :=
  fun l ⇒ match l with
    | nil ⇒ fun_from_I_0 E
    | elt :: ll ⇒ fun i ⇒ nth i l elt
    end.
Arguments of_listF l : simpl never.
Arguments of_listF l i : simpl never.

Lemma of_listF_correct : ∀ (elt: E) (l:list E)
     (i:[0..length l)),
       nth i l elt = of_listF l i.
Proof.
intros elt l i; destruct l; simpl in i; [destruct i; easy |].
unfold of_listF; apply nth_indep; now apply /ltP.
Qed.

Lemma of_listF_correct' : ∀ (elt: E) (l:list E) (i:nat)
     (H: i < length l),
    nth i l elt = (of_listF l) (Ordinal H).
Proof.
intros elt l i H.
rewrite -(of_listF_correct elt); easy.
Qed.

Lemma of_to_listF : ∀ {n} (A:'E^n),
   A = castF (to_listF_length A) (of_listF (to_listF A)).
Proof.
intros [| n] A; extF i.
destruct i; easy.
rewrite (to_listF_correct (A ord0)) of_listF_correct'; try easy.
rewrite to_listF_length; easy.
Qed.

Lemma of_to_listF' : ∀ {n} (A:'E^n),
   (of_listF (to_listF A))
      = castF (eq_sym (to_listF_length A)) A.
Proof.
intros n A.
apply trans_eq with (castF (eq_sym (to_listF_length A))
    (castF (to_listF_length A) (of_listF (to_listF A)))).
2: f_equal.
2: rewrite <- of_to_listF; easy.
now rewrite castF_can.
Qed.

Lemma to_of_listF : ∀ (l:list E),
     to_listF (of_listF l) = l.
Proof.
intros l.
case l; try easy.
clear l; intros a l.
apply nth_ext with a a.
rewrite to_listF_length; easy.
rewrite to_listF_length; intros n Hn.
rewrite of_listF_correct'.
rewrite to_listF_length.
now apply /ltP.
intros Hm.
rewrite of_to_listF'.
unfold castF; easy.
Qed.

Lemma to_listF_inj : ∀ {n:nat} (A B:'E^n),
   to_listF A = to_listF B → A = B.
Proof.
intros [| n] A B H; extF i.
destruct i; easy.
rewrite (of_to_listF A) (of_to_listF B); unfold castF.
rewrite -(of_listF_correct (A ord0)) -(of_listF_correct (A ord0)).
f_equal; easy.
Qed.

Lemma to_listF_firstn : ∀ {n:nat} (A:'E^n) (i:[0..n)),
  firstn i (to_listF A)
     = to_listF (firstF (castF_nip A (widen_S i))).
Proof.
intros n; induction n; intros A i.
destruct i; easy.
assert (Y : (i ≤ length (to_listF A))%coq_nat).
rewrite to_listF_length.
assert (i < n.+1)%coq_nat; try auto with zarith.
apply /ltP; easy.
simpl; rewrite castF_id.
case (classic (i=ord0)); intros Hi.
rewrite Hi; now simpl.
pose (j:= lower_S Hi).
assert (Hj: nat_of_ord i = (nat_of_ord j).+1).
assert (nat_of_ord i ≠ 0).
intros T; apply Hi.
apply ord_inj; easy.
simpl; rewrite -minusE; auto with zarith.
rewrite →Hj at 1.
apply trans_eq with (A ord0 :: firstn j (to_listF (liftF_S A))).
easy.
rewrite IHn.
replace i with (lift_S j).
simpl.
rewrite castF_id.
f_equal; try easy.
unfold firstF, castF_nip, castF; simpl.
f_equal; apply ord_inj; now simpl.
f_equal; simpl.
unfold firstF, castF_nip, liftF_S, castF; simpl.
extF; f_equal.
apply ord_inj; simpl; easy.
apply ord_inj; simpl; easy.
Qed.

End FF_list.