(** * Norm: Normalization of STLC *)

Set Warnings "-notation-overridden,-parsing".
From Coq Require Import Lists.List.
From Coq Require Import Strings.String.
From PLF Require Import Maps.
From PLF Require Import Smallstep.

Hint Constructors multi : core.

(* Chapter written and maintained by Andrew Tolmach *)

(** This optional chapter is based on chapter 12 of _Types and
    Programming Languages_ (Pierce).  It may be useful to look at the
    two together, as that chapter includes explanations and informal
    proofs that are not repeated here.

    In this chapter, we consider another fundamental theoretical
    property of the simply typed lambda-calculus: the fact that the
    evaluation of a well-typed program is guaranteed to halt in a
    finite number of steps---i.e., every well-typed term is
    _normalizable_.

    Unlike the type-safety properties we have considered so far, the
    normalization property does not extend to full-blown programming
    languages, because these languages nearly always extend the simply
    typed lambda-calculus with constructs, such as general
    recursion (see the [MoreStlc] chapter) or recursive types, that
    can be used to write nonterminating programs.  However, the issue
    of normalization reappears at the level of _types_ when we
    consider the metatheory of polymorphic versions of the lambda
    calculus such as System F-omega: in this system, the language of
    types effectively contains a copy of the simply typed
    lambda-calculus, and the termination of the typechecking algorithm
    will hinge on the fact that a "normalization" operation on type
    expressions is guaranteed to terminate.

    Another reason for studying normalization proofs is that they are
    some of the most beautiful---and mind-blowing---mathematics to be
    found in the type theory literature, often (as here) involving the
    fundamental proof technique of _logical relations_.

    The calculus we shall consider here is the simply typed
    lambda-calculus over a single base type [bool] and with
    pairs. We'll give most details of the development for the basic
    lambda-calculus terms treating [bool] as an uninterpreted base
    type, and leave the extension to the boolean operators and pairs
    to the reader.  Even for the base calculus, normalization is not
    entirely trivial to prove, since each reduction of a term can
    duplicate redexes in subterms. *)

(** **** Exercise: 2 stars, standard (norm_fail)

    Where do we fail if we attempt to prove normalization by a
    straightforward induction on the size of a well-typed term? *)

(*

I'm guessing here with pure intuition.

Lambda abstraction and application can make a term duplicated several
times. This could make the final term size larger than the input term
size.

 *)

(* Do not modify the following line: *)
Definition manual_grade_for_norm_fail : option (nat*string) := None.
(** [] *)

(** **** Exercise: 5 stars, standard, especially useful (norm)

    The best ways to understand an intricate proof like this is
    are (1) to help fill it in and (2) to extend it.  We've left out some
    parts of the following development, including some proofs of lemmas
    and the all the cases involving products and conditionals.  Fill them
    in. *)

(* Do not modify the following line: *)
Definition manual_grade_for_norm : option (nat*string) := None.
(** [] *)

(* ################################################################# *)
(** * Language *)

(** We begin by repeating the relevant language definition, which is
    similar to those in the [MoreStlc] chapter, plus supporting
    results including type preservation and step determinism.  (We
    won't need progress.)  You may just wish to skip down to the
    Normalization section... *)

(* ----------------------------------------------------------------- *)
(** *** Syntax and Operational Semantics *)

Inductive ty : Type :=
  | Ty_Bool : ty
  | Ty_Arrow : ty -> ty -> ty
  | Ty_Prod  : ty -> ty -> ty
.

Inductive tm : Type :=
    (* pure STLC *)
  | tm_var : string -> tm
  | tm_app : tm -> tm -> tm
  | tm_abs : string -> ty -> tm -> tm
    (* booleans *)
  | tm_true : tm
  | tm_false : tm
  | tm_if : tm -> tm -> tm -> tm
    (* pairs *)
  | tm_pair : tm -> tm -> tm
  | tm_fst : tm -> tm
  | tm_snd : tm -> tm.

Declare Custom Entry stlc.

Notation "<{ e }>" := e (e custom stlc at level 99).
Notation "( x )" := x (in custom stlc, x at level 99).
Notation "x" := x (in custom stlc at level 0, x constr at level 0).
Notation "S -> T" := (Ty_Arrow S T) (in custom stlc at level 50, right associativity).
Notation "x y" := (tm_app x y) (in custom stlc at level 1, left associativity).
Notation "\ x : t , y" :=
  (tm_abs x t y) (in custom stlc at level 90, x at level 99,
                     t custom stlc at level 99,
                     y custom stlc at level 99,
                     left associativity).
Coercion tm_var : string >-> tm.

Notation "{ x }" := x (in custom stlc at level 1, x constr).

Notation "'Bool'" := Ty_Bool (in custom stlc at level 0).
Notation "'if' x 'then' y 'else' z" :=
  (tm_if x y z) (in custom stlc at level 89,
                    x custom stlc at level 99,
                    y custom stlc at level 99,
                    z custom stlc at level 99,
                    left associativity).
Notation "'true'"  := true (at level 1).
Notation "'true'"  := tm_true (in custom stlc at level 0).
Notation "'false'"  := false (at level 1).
Notation "'false'"  := tm_false (in custom stlc at level 0).

Notation "X * Y" :=
  (Ty_Prod X Y) (in custom stlc at level 2, X custom stlc, Y custom stlc at level 0).
Notation "( x ',' y )" := (tm_pair x y) (in custom stlc at level 0,
                                                x custom stlc at level 99,
                                                y custom stlc at level 99).
Notation "t '.fst'" := (tm_fst t) (in custom stlc at level 0).
Notation "t '.snd'" := (tm_snd t) (in custom stlc at level 0).

(* ----------------------------------------------------------------- *)
(** *** Substitution *)

Reserved Notation "'[' x ':=' s ']' t" (in custom stlc at level 20, x constr).

Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
  match t with
  | tm_var y =>
      if eqb_string x y then s else t
  | <{ \ y : T, t1 }> =>
      if eqb_string x y then t else <{ \y:T, [x:=s] t1 }>
  | <{t1 t2}> =>
      <{ ([x:=s]t1) ([x:=s]t2)}>
  | <{true}> =>
      <{true}>
  | <{false}> =>
      <{false}>
  | <{if t1 then t2 else t3}> =>
      <{if ([x:=s] t1) then ([x:=s] t2) else ([x:=s] t3)}>
  | <{(t1, t2)}> =>
      <{( ([x:=s] t1), ([x:=s] t2) )}>
  | <{t0.fst}> =>
      <{ ([x:=s] t0).fst}>
  | <{t0.snd}> =>
      <{ ([x:=s] t0).snd}>
  end

  where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc).

(* ----------------------------------------------------------------- *)
(** *** Reduction *)

Inductive value : tm -> Prop :=
  | v_abs : forall x T2 t1,
      value <{\x:T2, t1}>
  | v_true :
      value <{true}>
  | v_false :
      value <{false}>
  | v_pair : forall v1 v2,
      value v1 ->
      value v2 ->
      value <{(v1, v2)}>.

Hint Constructors value : core.

Reserved Notation "t '-->' t'" (at level 40).

Inductive step : tm -> tm -> Prop :=
  | ST_AppAbs : forall x T2 t1 v2,
         value v2 ->
         <{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
  | ST_App1 : forall t1 t1' t2,
         t1 --> t1' ->
         <{t1 t2}> --> <{t1' t2}>
  | ST_App2 : forall v1 t2 t2',
         value v1 ->
         t2 --> t2' ->
         <{v1 t2}> --> <{v1  t2'}>
  | ST_IfTrue : forall t1 t2,
      <{if true then t1 else t2}> --> t1
  | ST_IfFalse : forall t1 t2,
      <{if false then t1 else t2}> --> t2
  | ST_If : forall t1 t1' t2 t3,
      t1 --> t1' ->
      <{if t1 then t2 else t3}> --> <{if t1' then t2 else t3}>
  | ST_Pair1 : forall t1 t1' t2,
        t1 --> t1' ->
        <{ (t1,t2) }> --> <{ (t1' , t2) }>
  | ST_Pair2 : forall v1 t2 t2',
        value v1 ->
        t2 --> t2' ->
        <{ (v1, t2) }> -->  <{ (v1, t2') }>
  | ST_Fst1 : forall t0 t0',
        t0 --> t0' ->
        <{ t0.fst }> --> <{ t0'.fst }>
  | ST_FstPair : forall v1 v2,
        value v1 ->
        value v2 ->
        <{ (v1,v2).fst }> --> v1
  | ST_Snd1 : forall t0 t0',
        t0 --> t0' ->
        <{ t0.snd }> --> <{ t0'.snd }>
  | ST_SndPair : forall v1 v2,
        value v1 ->
        value v2 ->
        <{ (v1,v2).snd }> --> v2

where "t '-->' t'" := (step t t').

Hint Constructors step : core.

Notation multistep := (multi step).
Notation "t1 '-->*' t2" := (multistep t1 t2) (at level 40).

Notation step_normal_form := (normal_form step).

Lemma value__normal : forall t, value t -> step_normal_form t.
Proof with eauto.
  intros t H; induction H; intros [t' ST]; inversion ST...
Qed.

(* ----------------------------------------------------------------- *)
(** *** Typing *)

Definition context := partial_map ty.

Reserved Notation "Gamma '|-' t '\in' T" (at level 40,
                                          t custom stlc, T custom stlc at level 0).

Inductive has_type : context -> tm -> ty -> Prop :=
  (* Same as before: *)
  (* pure STLC *)
  | T_Var : forall Gamma x T1,
      Gamma x = Some T1 ->
      Gamma |- x \in T1
  | T_Abs : forall Gamma x T1 T2 t1,
    (x |-> T2 ; Gamma) |- t1 \in T1 ->
      Gamma |- \x:T2, t1 \in (T2 -> T1)
  | T_App : forall T1 T2 Gamma t1 t2,
      Gamma |- t1 \in (T2 -> T1) ->
      Gamma |- t2 \in T2 ->
      Gamma |- t1 t2 \in T1
  | T_True : forall Gamma,
       Gamma |- true \in Bool
  | T_False : forall Gamma,
       Gamma |- false \in Bool
  | T_If : forall t1 t2 t3 T1 Gamma,
       Gamma |- t1 \in Bool ->
       Gamma |- t2 \in T1 ->
       Gamma |- t3 \in T1 ->
       Gamma |- if t1 then t2 else t3 \in T1
  | T_Pair : forall Gamma t1 t2 T1 T2,
      Gamma |- t1 \in T1 ->
      Gamma |- t2 \in T2 ->
      Gamma |- (t1, t2) \in (T1 * T2)
  | T_Fst : forall Gamma t0 T1 T2,
      Gamma |- t0 \in (T1 * T2) ->
      Gamma |- t0.fst \in T1
  | T_Snd : forall Gamma t0 T1 T2,
      Gamma |- t0 \in (T1 * T2) ->
      Gamma |- t0.snd \in T2

where "Gamma '|-' t '\in' T" := (has_type Gamma t T).

Hint Constructors has_type : core.

Hint Extern 2 (has_type _ (app _ _) _) => eapply T_App; auto : core.
Hint Extern 2 (_ = _) => compute; reflexivity : core.

(* ================================================================= *)
(** ** Weakening *)

(** The weakening lemma is proved as in pure STLC. *)

Lemma weakening : forall Gamma Gamma' t T,
     inclusion Gamma Gamma' ->
     Gamma  |- t \in T  ->
     Gamma' |- t \in T.
Proof.
  intros Gamma Gamma' t T H Ht.
  generalize dependent Gamma'.
  induction Ht; eauto using inclusion_update.
Qed.

Lemma weakening_empty : forall Gamma t T,
     empty |- t \in T  ->
     Gamma |- t \in T.
Proof.
  intros Gamma t T.
  eapply weakening.
  discriminate.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Substitution *)

Lemma substitution_preserves_typing : forall Gamma x U t v T,
  (x |-> U ; Gamma) |- t \in T ->
  empty |- v \in U   ->
  Gamma |- [x:=v]t \in T.
Proof with eauto.
  intros Gamma x U t v T Ht Hv.
  generalize dependent Gamma. generalize dependent T.
  induction t; intros T Gamma H;
  (* in each case, we'll want to get at the derivation of H *)
    inversion H; clear H; subst; simpl; eauto.
  - (* var *)
    rename s into y. destruct (eqb_stringP x y); subst.
    + (* x=y *)
      rewrite update_eq in H2.
      injection H2 as H2; subst.
      apply weakening_empty. assumption.
    + (* x<>y *)
      apply T_Var. rewrite update_neq in H2; auto.
  - (* abs *)
    rename s into y, t into S.
    destruct (eqb_stringP x y); subst; apply T_Abs.
    + (* x=y *)
      rewrite update_shadow in H5. assumption.
    + (* x<>y *)
      apply IHt.
      rewrite update_permute; auto.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Preservation *)

 Theorem preservation : forall t t' T,
   empty |- t \in T  ->
   t --> t'  ->
   empty |- t' \in T.
Proof with eauto.
intros t t' T HT. generalize dependent t'.
remember empty as Gamma.
induction HT;
  intros t' HE; subst; inversion HE; subst...
- (* T_App *)
  inversion HE; subst...
  + (* ST_AppAbs *)
    apply substitution_preserves_typing with T2...
    inversion HT1...
- (* T_Fst *)
  inversion HT...
- (* T_Snd *)
  inversion HT...
Qed.

(* ----------------------------------------------------------------- *)
(** *** Context Invariance *)

Inductive appears_free_in : string -> tm -> Prop :=
  | afi_var : forall (x : string),
      appears_free_in x <{ x }>
  | afi_app1 : forall x t1 t2,
      appears_free_in x t1 -> appears_free_in x <{ t1 t2 }>
  | afi_app2 : forall x t1 t2,
      appears_free_in x t2 -> appears_free_in x <{ t1 t2 }>
  | afi_abs : forall x y T11 t12,
        y <> x  ->
        appears_free_in x t12 ->
        appears_free_in x <{ \y : T11, t12 }>
  (* booleans *)
  | afi_test0 : forall x t0 t1 t2,
      appears_free_in x t0 ->
      appears_free_in x <{ if t0 then t1 else t2 }>
  | afi_test1 : forall x t0 t1 t2,
      appears_free_in x t1 ->
      appears_free_in x <{ if t0 then t1 else t2 }>
  | afi_test2 : forall x t0 t1 t2,
      appears_free_in x t2 ->
      appears_free_in x <{ if t0 then t1 else t2 }>
  (* pairs *)
  | afi_pair1 : forall x t1 t2,
      appears_free_in x t1 ->
      appears_free_in x <{ (t1, t2) }>
  | afi_pair2 : forall x t1 t2,
      appears_free_in x t2 ->
      appears_free_in x <{ (t1 , t2) }>
  | afi_fst : forall x t,
      appears_free_in x t ->
      appears_free_in x <{ t.fst }>
  | afi_snd : forall x t,
      appears_free_in x t ->
      appears_free_in x <{ t.snd }>
.

Hint Constructors appears_free_in : core.

Definition closed (t:tm) :=
  forall x, ~ appears_free_in x t.

Lemma context_invariance : forall Gamma Gamma' t S,
     Gamma |- t \in S  ->
     (forall x, appears_free_in x t -> Gamma x = Gamma' x)  ->
     Gamma' |- t \in S.
Proof.
  intros.
  generalize dependent Gamma'.
  induction H; intros; eauto 12.
  - (* T_Var *)
    apply T_Var. rewrite <- H0; auto.
  - (* T_Abs *)
    apply T_Abs.
    apply IHhas_type. intros x1 Hafi.
    (* the only tricky step... *)
    destruct (eqb_stringP x x1); subst.
    + rewrite update_eq.
      rewrite update_eq.
      reflexivity.
    + rewrite update_neq; [| assumption].
      rewrite update_neq; [| assumption].
      auto.
Qed.

Lemma free_in_context : forall x t T Gamma,
   appears_free_in x t ->
   Gamma |- t \in T ->
   exists T', Gamma x = Some T'.
Proof with eauto.
  intros x t T Gamma Hafi Htyp.
  induction Htyp; inversion Hafi; subst...
  - (* T_Abs *)
    destruct IHHtyp as [T' Hctx]... exists T'.
    unfold update, t_update in Hctx.
    rewrite false_eqb_string in Hctx...
Qed.

Corollary typable_empty__closed : forall t T,
    empty |- t \in T  ->
    closed t.
Proof.
  intros. unfold closed. intros x H1.
  destruct (free_in_context _ _ _ _ H1 H) as [T' C].
  discriminate C.  Qed.

(* ----------------------------------------------------------------- *)
(** *** Determinism *)

Lemma step_deterministic :
   deterministic step.
Proof with eauto.
   unfold deterministic.
   intros t t' t'' E1 E2.
   generalize dependent t''.
   induction E1; intros t'' E2; inversion E2; subst; clear E2...
  (* ST_AppAbs *)
   - inversion H3.
   - exfalso; apply value__normal in H...
   (* ST_App1 *)
   - inversion E1.
   -  f_equal...
   - exfalso; apply value__normal in H1...
   (* ST_App2 *)
   - exfalso; apply value__normal in H3...
   - exfalso; apply value__normal in H...
   - f_equal...
   - (* ST_IfTrue *)
       inversion H3.
   - (* ST_IfFalse *)
       inversion H3.
   (* ST_If *)
   - inversion E1.
   - inversion E1.
   - f_equal...
   (* ST_Pair1 *)
   - f_equal...
   - exfalso; apply value__normal in H1...
   (* ST_Pair2 *)
   - exfalso; apply value__normal in H...
   - f_equal...
   (* ST_Fst1 *)
   - f_equal...
   - exfalso.
     inversion E1; subst.
     + apply value__normal in H0...
     + apply value__normal in H1...
   (* ST_FstPair *)
   - exfalso.
     inversion H2; subst.
     + apply value__normal in H...
     + apply value__normal in H0...
   (* ST_Snd1 *)
   - f_equal...
   - exfalso.
     inversion E1; subst.
     + apply value__normal in H0...
     + apply value__normal in H1...
   (* ST_SndPair *)
   - exfalso.
     inversion H2; subst.
     + apply value__normal in H...
     + apply value__normal in H0...
Qed.

(* ################################################################# *)
(** * Normalization *)

(** Now for the actual normalization proof.

    Our goal is to prove that every well-typed term reduces to a
    normal form.  In fact, it turns out to be convenient to prove
    something slightly stronger, namely that every well-typed term
    reduces to a _value_.  This follows from the weaker property
    anyway via Progress (why?) but otherwise we don't need Progress,
    and we didn't bother re-proving it above.

    Here's the key definition: *)

Definition halts  (t:tm) : Prop :=  exists t', t -->* t' /\  value t'.

(** A trivial fact: *)

Lemma value_halts : forall v, value v -> halts v.
Proof.
  intros v H. unfold halts.
  exists v. split.
  - apply multi_refl.
  - assumption.
Qed.

(** The key issue in the normalization proof (as in many proofs by
    induction) is finding a strong enough induction hypothesis.  To
    this end, we begin by defining, for each type [T], a set [R_T] of
    closed terms of type [T].  We will specify these sets using a
    relation [R] and write [R T t] when [t] is in [R_T]. (The sets
    [R_T] are sometimes called _saturated sets_ or _reducibility
    candidates_.)

    Here is the definition of [R] for the base language:

    - [R bool t] iff [t] is a closed term of type [bool] and [t] halts
      in a value

    - [R (T1 -> T2) t] iff [t] is a closed term of type [T1 -> T2] and
      [t] halts in a value _and_ for any term [s] such that [R T1 s],
      we have [R T2 (t s)]. *)

(** This definition gives us the strengthened induction hypothesis that we
    need.  Our primary goal is to show that all _programs_ ---i.e., all
    closed terms of base type---halt.  But closed terms of base type can
    contain subterms of functional type, so we need to know something
    about these as well.  Moreover, it is not enough to know that these
    subterms halt, because the application of a normalized function to a
    normalized argument involves a substitution, which may enable more
    reduction steps.  So we need a stronger condition for terms of
    functional type: not only should they halt themselves, but, when
    applied to halting arguments, they should yield halting results.

    The form of [R] is characteristic of the _logical relations_ proof
    technique.  (Since we are just dealing with unary relations here, we
    could perhaps more properly say _logical properties_.)  If we want to
    prove some property [P] of all closed terms of type [A], we proceed by
    proving, by induction on types, that all terms of type [A] _possess_
    property [P], all terms of type [A->A] _preserve_ property [P], all
    terms of type [(A->A)->(A->A)] _preserve the property of preserving_
    property [P], and so on.  We do this by defining a family of
    properties, indexed by types.  For the base type [A], the property is
    just [P].  For functional types, it says that the function should map
    values satisfying the property at the input type to values satisfying
    the property at the output type.

    When we come to formalize the definition of [R] in Coq, we hit a
    problem.  The most obvious formulation would be as a parameterized
    Inductive proposition like this:

      Inductive R : ty -> tm -> Prop :=
      | R_bool : forall b t, empty |- t \in Bool ->
                      halts t ->
                      R Bool t
      | R_arrow : forall T1 T2 t, empty |- t \in (Arrow T1 T2) ->
                      halts t ->
                      (forall s, R T1 s -> R T2 (app t s)) ->
                      R (Arrow T1 T2) t.

    Unfortunately, Coq rejects this definition because it violates the
    _strict positivity requirement_ for inductive definitions, which says
    that the type being defined must not occur to the left of an arrow in
    the type of a constructor argument. Here, it is the third argument to
    [R_arrow], namely [(forall s, R T1 s -> R TS (app t s))], and
    specifically the [R T1 s] part, that violates this rule.  (The
    outermost arrows separating the constructor arguments don't count when
    applying this rule; otherwise we could never have genuinely inductive
    properties at all!)  The reason for the rule is that types defined
    with non-positive recursion can be used to build non-terminating
    functions, which as we know would be a disaster for Coq's logical
    soundness. Even though the relation we want in this case might be
    perfectly innocent, Coq still rejects it because it fails the
    positivity test.

    Fortunately, it turns out that we _can_ define [R] using a
    [Fixpoint]: *)

Fixpoint R (T:ty) (t:tm) : Prop :=
  empty |- t \in T /\ halts t /\
  (match T with
   | <{ Bool }>  => True
   | <{ T1 -> T2 }> => (forall s, R T1 s -> R T2 <{t s}> )
   (* ... edit the next line when dealing with products *)
   | <{ T1 * T2 }> => (forall t1 t2, t -->* <{(t1, t2)}> -> R T1 t1 /\ R T2 t2)
   end).

(** As immediate consequences of this definition, we have that every
    element of every set [R_T] halts in a value and is closed with type
    [t] :*)

Lemma R_halts : forall {T} {t}, R T t -> halts t.
Proof.
  intros.
  destruct T; unfold R in H; destruct H as [_ [H _]]; assumption.
Qed.

Lemma R_typable_empty : forall {T} {t}, R T t -> empty |- t \in T.
Proof.
  intros.
  destruct T; unfold R in H; destruct H as [H _]; assumption.
Qed.

(** Now we proceed to show the main result, which is that every
    well-typed term of type [T] is an element of [R_T].  Together with
    [R_halts], that will show that every well-typed term halts in a
    value.  *)

(* ================================================================= *)
(** **  Membership in [R_T] Is Invariant Under Reduction *)

(** We start with a preliminary lemma that shows a kind of strong
    preservation property, namely that membership in [R_T] is _invariant_
    under reduction. We will need this property in both directions,
    i.e., both to show that a term in [R_T] stays in [R_T] when it takes a
    forward step, and to show that any term that ends up in [R_T] after a
    step must have been in [R_T] to begin with.

    First of all, an easy preliminary lemma. Note that in the forward
    direction the proof depends on the fact that our language is
    determinstic. This lemma might still be true for nondeterministic
    languages, but the proof would be harder! *)

Lemma step_preserves_halting :
  forall t t', (t --> t') -> (halts t <-> halts t').
Proof.
 intros t t' ST.  unfold halts.
 split.
 - (* -> *)
  intros [t'' [STM V]].
  destruct STM.
   + exfalso; apply value__normal in V; eauto.
   + rewrite (step_deterministic _ _ _ ST H). exists z. split; assumption.
 - (* <- *)
  intros [t'0 [STM V]].
  exists t'0. split; eauto.
Qed.

(** Now the main lemma, which comes in two parts, one for each
    direction.  Each proceeds by induction on the structure of the type
    [T]. In fact, this is where we make fundamental use of the
    structure of types.

    One requirement for staying in [R_T] is to stay in type [T]. In the
    forward direction, we get this from ordinary type Preservation. *)

Lemma step_preserves_R : forall T t t', (t --> t') -> R T t -> R T t'.
Proof.
 induction T;  intros t t' E Rt; unfold R; fold R; unfold R in Rt; fold R in Rt;
               destruct Rt as [typable_empty_t [halts_t RRt]].
  (* Bool *)
  split. eapply preservation; eauto.
  split. apply (step_preserves_halting _ _ E); eauto.
  auto.
  (* Arrow *)
  split. eapply preservation; eauto.
  split. apply (step_preserves_halting _ _ E); eauto.
  intros.
  eapply IHT2.
  apply  ST_App1. apply E.
  apply RRt; auto.
  (* Pair *)
  split. eapply preservation; eauto.
  split. eapply step_preserves_halting in E. destruct E. auto.
  intros. apply RRt.
  eapply multi_step. apply E. auto.
Qed.

(** The generalization to multiple steps is trivial: *)

Lemma multistep_preserves_R : forall T t t',
  (t -->* t') -> R T t -> R T t'.
Proof.
  intros T t t' STM; induction STM; intros.
  assumption.
  apply IHSTM. eapply step_preserves_R. apply H. assumption.
Qed.

(** In the reverse direction, we must add the fact that [t] has type
   [T] before stepping as an additional hypothesis. *)

Lemma preservation_multistep : forall t t' T,
  (empty |- t \in T) ->
  (t -->* t') ->
  (empty |- t' \in T).
Proof.
  intros.
  induction H0; auto.
  apply IHmulti. eapply preservation. apply H. apply H0.
Qed.

Lemma step_preserves_R' : forall T t t',
  empty |- t \in T -> (t --> t') -> R T t' -> R T t.
Proof.
  intros.
  generalize dependent H0. generalize dependent H1.
  generalize dependent t. generalize dependent t'.
  induction T; intros.
  - (* Bool *)
   simpl in *. destruct H1. destruct H2.
   split; auto. split; auto. apply step_preserves_halting in H0.
   destruct H0. auto.

  - (* Arrow *)
   simpl in *.
   destruct H1. destruct H2.
   split; auto.
   split. apply step_preserves_halting in H0. destruct H0; auto.
   intros.

   assert (empty |- s \in T1). unfold R in H4. destruct T1; destruct H4; auto.
   apply H3 in H4.
   eapply IHT2; eauto.

  - (* Pair *)
   simpl in *. destruct H1. destruct H2.
   split; auto. split; auto.
   eapply step_preserves_halting. apply H0. apply H2.
   intros. inversion H4; subst; clear H4.
   inversion H; subst; clear H.
   + inversion H0; subst; clear H0;
       pose proof H3 _ _ (multi_refl _ _) as HR; destruct HR; clear H3;
       split.
     * eapply IHT1; eauto.
     * auto.
     * auto.
     * eapply IHT2; eauto.
   + pose proof step_deterministic _ _ _ H0 H5; subst.
     apply H3 in H6. auto.
Qed.

Lemma multistep_preserves_R' : forall T t t',
  empty |- t \in T -> (t -->* t') -> R T t' -> R T t.
Proof.
  intros T t t' HT STM.
  induction STM; intros.
    assumption.
    eapply step_preserves_R'.  assumption. apply H. apply IHSTM.
    eapply preservation;  eauto. auto.
Qed.

(* ================================================================= *)
(** ** Closed Instances of Terms of Type [t] Belong to [R_T] *)

(** Now we proceed to show that every term of type [T] belongs to
    [R_T].  Here, the induction will be on typing derivations (it would be
    surprising to see a proof about well-typed terms that did not
    somewhere involve induction on typing derivations!).  The only
    technical difficulty here is in dealing with the abstraction case.
    Since we are arguing by induction, the demonstration that a term
    [abs x T1 t2] belongs to [R_(T1->T2)] should involve applying the
    induction hypothesis to show that [t2] belongs to [R_(T2)].  But
    [R_(T2)] is defined to be a set of _closed_ terms, while [t2] may
    contain [x] free, so this does not make sense.

    This problem is resolved by using a standard trick to suitably
    generalize the induction hypothesis: instead of proving a statement
    involving a closed term, we generalize it to cover all closed
    _instances_ of an open term [t].  Informally, the statement of the
    lemma will look like this:

    If [x1:T1,..xn:Tn |- t : T] and [v1,...,vn] are values such that
    [R T1 v1], [R T2 v2], ..., [R Tn vn], then
    [R T ([x1:=v1][x2:=v2]...[xn:=vn]t)].

    The proof will proceed by induction on the typing derivation
    [x1:T1,..xn:Tn |- t : T]; the most interesting case will be the one
    for abstraction. *)

(* ----------------------------------------------------------------- *)
(** *** Multisubstitutions, Multi-Extensions, and Instantiations *)

(** However, before we can proceed to formalize the statement and
    proof of the lemma, we'll need to build some (rather tedious)
    machinery to deal with the fact that we are performing _multiple_
    substitutions on term [t] and _multiple_ extensions of the typing
    context.  In particular, we must be precise about the order in which
    the substitutions occur and how they act on each other.  Often these
    details are simply elided in informal paper proofs, but of course Coq
    won't let us do that. Since here we are substituting closed terms, we
    don't need to worry about how one substitution might affect the term
    put in place by another.  But we still do need to worry about the
    _order_ of substitutions, because it is quite possible for the same
    identifier to appear multiple times among the [x1,...xn] with
    different associated [vi] and [Ti].

    To make everything precise, we will assume that environments are
    extended from left to right, and multiple substitutions are performed
    from right to left.  To see that this is consistent, suppose we have
    an environment written as [...,y:bool,...,y:nat,...]  and a
    corresponding term substitution written as [...[y:=(tbool
    true)]...[y:=(const 3)]...t].  Since environments are extended from
    left to right, the binding [y:nat] hides the binding [y:bool]; since
    substitutions are performed right to left, we do the substitution
    [y:=(const 3)] first, so that the substitution [y:=(tbool true)] has
    no effect. Substitution thus correctly preserves the type of the term.

    With these points in mind, the following definitions should make sense.

    A _multisubstitution_ is the result of applying a list of
    substitutions, which we call an _environment_. *)

Definition env := list (string * tm).

Fixpoint msubst (ss:env) (t:tm) : tm :=
match ss with
| nil => t
| ((x,s)::ss') => msubst ss' <{ [x:=s]t }>
end.

(** We need similar machinery to talk about repeated extension of a
    typing context using a list of (identifier, type) pairs, which we
    call a _type assignment_. *)

Definition tass := list (string * ty).

Fixpoint mupdate (Gamma : context) (xts : tass) :=
  match xts with
  | nil => Gamma
  | ((x,v)::xts') => update (mupdate Gamma xts') x v
  end.

(** We will need some simple operations that work uniformly on
    environments and type assigments *)

Fixpoint lookup {X:Set} (k : string) (l : list (string * X))
              : option X :=
  match l with
    | nil => None
    | (j,x) :: l' =>
      if eqb_string j k then Some x else lookup k l'
  end.

Fixpoint drop {X:Set} (n:string) (nxs:list (string * X))
            : list (string * X) :=
  match nxs with
    | nil => nil
    | ((n',x)::nxs') =>
        if eqb_string n' n then drop n nxs'
        else (n',x)::(drop n nxs')
  end.

(** An _instantiation_ combines a type assignment and a value
    environment with the same domains, where corresponding elements are
    in R. *)

Inductive instantiation :  tass -> env -> Prop :=
| V_nil :
    instantiation nil nil
| V_cons : forall x T v c e,
    value v -> R T v ->
    instantiation c e ->
    instantiation ((x,T)::c) ((x,v)::e).

(** We now proceed to prove various properties of these definitions. *)

(* ----------------------------------------------------------------- *)
(** *** More Substitution Facts *)

(** First we need some additional lemmas on (ordinary) substitution. *)

Lemma vacuous_substitution : forall  t x,
     ~ appears_free_in x t  ->
     forall t', <{ [x:=t']t }> = t.
Proof with eauto.
  intros.
  generalize dependent t'.
  induction t; intros; simpl...

  - destruct (eqb_string x s) eqn:?; auto.
    rewrite eqb_string_true_iff in Heqb. subst.
    assert (appears_free_in s s) by auto. congruence.

  - assert (~appears_free_in x t1) by auto.
    assert (~appears_free_in x t2) by auto.
    rewrite IHt1; auto. rewrite IHt2; auto.

  - destruct (eqb_stringP s x); subst; auto.
    rewrite <- eqb_string_refl. reflexivity.

    assert (~appears_free_in x t0).
    { intro. assert (appears_free_in x <{\s:t, t0}>); auto. }
    rewrite IHt; auto.

    assert (x <> s) by auto. apply eqb_string_false_iff in H1.
    rewrite H1. auto.

  - assert (~ appears_free_in x t1); auto.
    assert (~ appears_free_in x t2); auto.
    assert (~ appears_free_in x t3); auto.
    rewrite IHt1; auto.
    rewrite IHt2; auto.
    rewrite IHt3; auto.

  - assert (~ appears_free_in x t1); auto.
    assert (~ appears_free_in x t2); auto.
    rewrite IHt1; auto.
    rewrite IHt2; auto.

  - assert (~ appears_free_in x t); auto.
    rewrite IHt; auto.

  - assert (~ appears_free_in x t); auto.
    rewrite IHt; auto.
Qed.

Lemma subst_closed: forall t,
     closed t  ->
     forall x t', <{ [x:=t']t }> = t.
Proof.
  intros. apply vacuous_substitution. apply H.  Qed.

Lemma subst_not_afi : forall t x v,
    closed v ->  ~ appears_free_in x <{ [x:=v]t }>.
Proof with eauto.  (* rather slow this way *)
  unfold closed, not.
  induction t; intros x v P A; simpl in A.
    - (* var *)
     destruct (eqb_stringP x s)...
     inversion A; subst. auto.
    - (* app *)
     inversion A; subst...
    - (* abs *)
     destruct (eqb_stringP x s)...
     + inversion A; subst...
     + inversion A; subst...
    - (* tru *)
     inversion A.
    - (* fls *)
     inversion A.
    - (* test *)
     inversion A; subst...
    - (* pair *)
     inversion A; subst...
    - (* fst *)
     inversion A; subst...
    - (* snd *)
     inversion A; subst...
Qed.

Lemma duplicate_subst : forall t' x t v,
  closed v -> <{ [x:=t]([x:=v]t') }> = <{ [x:=v]t' }>.
Proof.
  intros. eapply vacuous_substitution. apply subst_not_afi. assumption.
Qed.

Lemma swap_subst : forall t x x1 v v1,
    x <> x1 ->
    closed v -> closed v1 ->
    <{ [x1:=v1]([x:=v]t) }> = <{ [x:=v]([x1:=v1]t) }>.
Proof with eauto.
 induction t; intros; simpl; try auto.
  - (* var *)
   destruct (eqb_stringP x s); destruct (eqb_stringP x1 s).
   + subst. exfalso...
   + subst. simpl. rewrite <- eqb_string_refl. apply subst_closed...
   + subst. simpl. rewrite <- eqb_string_refl. rewrite subst_closed...
   + simpl. rewrite false_eqb_string... rewrite false_eqb_string...

  - (* app *)
   rewrite IHt1; auto.
   rewrite IHt2; auto.

  - (* abs *)
   destruct (eqb_stringP x s); destruct (eqb_stringP x1 s); subst; simpl;
     try rewrite <- eqb_string_refl;
     auto.
   + apply eqb_string_false_iff in n. rewrite n. reflexivity.
   + apply eqb_string_false_iff in n. rewrite n. reflexivity.
   + apply eqb_string_false_iff in n. apply eqb_string_false_iff in n0.
     rewrite n. rewrite n0.
     f_equal. auto.

  - (* if *)
   rewrite IHt1; auto.
   rewrite IHt2; auto.
   rewrite IHt3; auto.

  - (* pair *)
   rewrite IHt1; auto.
   rewrite IHt2; auto.

  - (* fst *)
   rewrite IHt; auto.

  - (* snd *)
   rewrite IHt; auto.
Qed.
(* ----------------------------------------------------------------- *)
(** *** Properties of Multi-Substitutions *)

Lemma msubst_closed: forall t, closed t -> forall ss, msubst ss t = t.
Proof.
  induction ss.
    reflexivity.
    destruct a. simpl. rewrite subst_closed; assumption.
Qed.

(** Closed environments are those that contain only closed terms. *)

Fixpoint closed_env (env:env) :=
  match env with
  | nil => True
  | (x,t)::env' => closed t /\ closed_env env'
  end.

(** Next come a series of lemmas charcterizing how [msubst] of closed terms
    distributes over [subst] and over each term form *)

Lemma subst_msubst: forall env x v t, closed v -> closed_env env ->
    msubst env <{ [x:=v]t }> = <{ [x:=v]  { msubst (drop x env) t }  }> .
Proof.
  induction env0; intros; auto.
  destruct a. simpl.
  inversion H0.
  destruct (eqb_stringP s x).
  - subst. rewrite duplicate_subst; auto.
  - simpl. rewrite swap_subst; eauto.
Qed.

Lemma msubst_var:  forall ss x, closed_env ss ->
   msubst ss (tm_var x) =
   match lookup x ss with
   | Some t => t
   | None => tm_var x
  end.
Proof.
  induction ss; intros.
    reflexivity.
    destruct a.
     simpl. destruct (eqb_string s x).
      apply msubst_closed. inversion H; auto.
      apply IHss. inversion H; auto.
Qed.

Lemma msubst_abs: forall ss x T t,
  msubst ss <{ \ x : T, t }> = <{ \x : T, {msubst (drop x ss) t} }>.
Proof.
  induction ss; intros.
    reflexivity.
    destruct a.
      simpl. destruct (eqb_string s x); simpl; auto.
Qed.

Lemma msubst_app : forall ss t1 t2,
    msubst ss <{ t1 t2 }> = <{ {msubst ss t1} ({msubst ss t2}) }>.
Proof.
 induction ss; intros.
   reflexivity.
   destruct a.
    simpl. rewrite <- IHss. auto.
Qed.

(** You'll need similar functions for the other term constructors. *)

Lemma msubst_if : forall ss t1 t2 t3,
    msubst ss <{ if t1 then t2 else t3 }> =
    <{ if {msubst ss t1} then {msubst ss t2} else {msubst ss t3} }>.
Proof.
 induction ss; intros.
   reflexivity.
   destruct a.
    simpl. rewrite <- IHss. auto.
Qed.

Lemma msubst_pair : forall ss t1 t2,
    msubst ss <{ (t1, t2) }> = <{ ({msubst ss t1}, {msubst ss t2}) }>.
Proof.
 induction ss; intros.
   reflexivity.
   destruct a.
   simpl. rewrite <- IHss. auto.
Qed.

Lemma msubst_fst : forall ss t,
    msubst ss <{ t.fst }> = <{ {msubst ss t}.fst }>.
Proof.
 induction ss; intros.
   reflexivity.
   destruct a.
   simpl. rewrite <- IHss. auto.
Qed.

Lemma msubst_snd : forall ss t,
    msubst ss <{ t.snd }> = <{ {msubst ss t}.snd }>.
Proof.
 induction ss; intros.
   reflexivity.
   destruct a.
   simpl. rewrite <- IHss. auto.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Properties of Multi-Extensions *)

(** We need to connect the behavior of type assignments with that of
    their corresponding contexts. *)

Lemma mupdate_lookup : forall (c : tass) (x:string),
    lookup x c = (mupdate empty c) x.
Proof.
  induction c; intros.
    auto.
    destruct a. unfold lookup, mupdate, update, t_update. destruct (eqb_string s x); auto.
Qed.

Lemma mupdate_drop : forall (c: tass) Gamma x x',
      mupdate Gamma (drop x c) x'
    = if eqb_string x x' then Gamma x' else mupdate Gamma c x'.
Proof.
  induction c; intros.
  - destruct (eqb_stringP x x'); auto.
  - destruct a. simpl.
    destruct (eqb_stringP s x).
    + subst. rewrite IHc.
      unfold update, t_update. destruct (eqb_stringP x x'); auto.
    + simpl. unfold update, t_update. destruct (eqb_stringP s x'); auto.
      subst. rewrite false_eqb_string; congruence.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Properties of Instantiations *)

(** These are strightforward. *)

Lemma instantiation_domains_match: forall {c} {e},
    instantiation c e ->
    forall {x} {T},
      lookup x c = Some T -> exists t, lookup x e = Some t.
Proof.
  intros c e V. induction V; intros x0 T0 C.
    solve_by_invert.
    simpl in *.
    destruct (eqb_string x x0); eauto.
Qed.

Lemma instantiation_env_closed : forall c e,
  instantiation c e -> closed_env e.
Proof.
  intros c e V; induction V; intros.
    econstructor.
    unfold closed_env. fold closed_env.
    split; [|assumption].
    eapply typable_empty__closed. eapply R_typable_empty. eauto.
Qed.

Lemma instantiation_R : forall c e,
    instantiation c e ->
    forall x t T,
      lookup x c = Some T ->
      lookup x e = Some t -> R T t.
Proof.
  intros c e V. induction V; intros x' t' T' G E.
    solve_by_invert.
    unfold lookup in *.  destruct (eqb_string x x').
      inversion G; inversion E; subst.  auto.
      eauto.
Qed.

Lemma instantiation_drop : forall c env,
    instantiation c env ->
    forall x, instantiation (drop x c) (drop x env).
Proof.
  intros c e V. induction V.
    intros.  simpl.  constructor.
    intros. unfold drop.
    destruct (eqb_string x x0); auto. constructor; eauto.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Congruence Lemmas on Multistep *)

(** We'll need just a few of these; add them as the demand arises. *)

Lemma multistep_App2 : forall v t t',
  value v -> (t -->* t') -> <{ v t }> -->* <{ v t' }>.
Proof.
  intros v t t' V STM. induction STM.
   apply multi_refl.
   eapply multi_step.
     apply ST_App2; eauto.  auto.
Qed.

Lemma multistep_IfTrue : forall t1 t2 t3 t2',
    (t1 -->* <{true}>) ->
    (t2 -->* t2') ->
    <{ if t1 then t2 else t3 }> -->* <{ t2' }>.
Proof.
  intros.
  remember <{true}>.
  induction H.
  - subst. econstructor. auto. auto.
  - pose proof IHmulti Heqt. eauto.
Qed.

Lemma multistep_IfFalse : forall t1 t2 t3 t2',
    (t1 -->* <{false}>) ->
    (t2 -->* t2') ->
    <{ if t1 then t3 else t2 }> -->* <{ t2' }>.
Proof.
  intros.
  remember <{false}>.
  induction H.
  - subst. econstructor. auto. auto.
  - pose proof IHmulti Heqt. eauto.
Qed.

Lemma multistep_Pair : forall t1 t2 t1' t2',
    (t1 -->* t1') ->
    value t1' ->
    (t2 -->* t2') ->
    value t2' ->
    <{ (t1, t2) }> -->* <{ (t1', t2') }>
    /\ value <{(t1', t2')}>.
Proof.
  intros.
  induction H; induction H1.
  - split. eapply multi_refl. auto.
  - split. eapply multi_step.
    apply ST_Pair2. auto. apply H.
    apply IHmulti; auto.
    apply IHmulti; auto.
  - split. eapply multi_step.
    apply ST_Pair1. apply H.
    apply IHmulti; auto.
    apply IHmulti; auto.
  - split. eapply multi_step.
    apply ST_Pair1. apply H.
    apply IHmulti; auto.
    apply IHmulti; auto.
Qed.

Lemma multistep_Pair_decompose : forall t1 t2 t1' t2',
    (<{ (t1, t2) }> -->* <{ (t1', t2') }>) ->
    (t1 -->* t1') /\ (t2 -->* t2').
Proof.
  intros.
  remember <{(t1, t2)}> as l;
    remember <{(t1', t2')}> as r.
  generalize dependent t1.
  generalize dependent t2.
  generalize dependent t1'.
  generalize dependent t2'.
  induction H; intros; subst.
  - inversion Heql; subst. auto.
  - pose proof IHmulti _ _ eq_refl; clear IHmulti.
    inversion H; subst; clear H.
    + destruct (H1 _ _ eq_refl).
      split; try solve [eapply multi_step; eauto]; assumption.
    + destruct (H1 _ _ eq_refl).
      split; try solve [eapply multi_step; eauto]; assumption.
Qed.

Lemma multistep_Fst : forall t t1 t2,
    (t -->* <{(t1, t2)}>) ->
    value t1 ->
    value t2 ->
    (<{t.fst}> -->* t1).
Proof.
  intros.
  remember <{(t1, t2)}> as t'.
  generalize dependent t1.
  generalize dependent t2.
  induction H; intros; subst.
  + eapply multi_step.
    apply ST_FstPair; auto. eauto.
  + eapply multi_step.
    apply ST_Fst1.
    apply H.
    eauto.
Qed.

Lemma multistep_Snd : forall t t1 t2,
    (t -->* <{(t1, t2)}>) ->
    value t1 ->
    value t2 ->
    (<{t.snd}> -->* t2).
Proof.
  intros.
  remember <{(t1, t2)}> as t'.
  generalize dependent t1.
  generalize dependent t2.
  induction H; intros; subst.
  + eapply multi_step.
    apply ST_SndPair; auto. eauto.
  + eapply multi_step.
    apply ST_Snd1.
    apply H.
    eauto.
Qed.

Lemma canonical_forms_bool : forall t,
  empty |- t \in Bool ->
  value t ->
  (t = <{true}>) \/ (t = <{false}>).
Proof.
  intros t HT HVal.
  destruct HVal; auto;
  inversion HT.
Qed.
(* FILL IN HERE *)

(* ----------------------------------------------------------------- *)
(** *** The R Lemma *)

(** We can finally put everything together.

    The key lemma about preservation of typing under substitution can
    be lifted to multi-substitutions: *)

Lemma msubst_preserves_typing : forall c e,
     instantiation c e ->
     forall Gamma t S, (mupdate Gamma c) |- t \in S ->
     Gamma |- { (msubst e t) } \in S.
Proof.
  induction 1; intros.
    simpl in H. simpl. auto.
    simpl in H2.  simpl.
    apply IHinstantiation.
    eapply substitution_preserves_typing; eauto.
    apply (R_typable_empty H0).
Qed.

(** And at long last, the main lemma. *)

Lemma msubst_R : forall c env t T,
    (mupdate empty c) |- t \in T ->
    instantiation c env ->
    R T (msubst env t).
Proof.
  intros c env0 t T HT V.
  generalize dependent env0.
  (* We need to generalize the hypothesis a bit before setting up the induction. *)
  remember (mupdate empty c) as Gamma.
  assert (forall x, Gamma x = lookup x c).
    intros. rewrite HeqGamma. rewrite mupdate_lookup. auto.
  clear HeqGamma.
  generalize dependent c.
  induction HT; intros.

  - (* T_Var *)
   rewrite H0 in H. destruct (instantiation_domains_match V H) as [t P].
   eapply instantiation_R; eauto.
   rewrite msubst_var.  rewrite P. auto. eapply instantiation_env_closed; eauto.

  - (* T_Abs *)
    rewrite msubst_abs.
    (* We'll need variants of the following fact several times, so its simplest to
       establish it just once. *)
    assert (WT : empty |- \x : T2, {msubst (drop x env0) t1 } \in (T2 -> T1) ).
    { eapply T_Abs. eapply msubst_preserves_typing.
      { eapply instantiation_drop; eauto. }
      eapply context_invariance.
      { apply HT. }
      intros.
      unfold update, t_update. rewrite mupdate_drop. destruct (eqb_stringP x x0).
      + auto.
      + rewrite H.
        clear - c n. induction c.
        simpl.  rewrite false_eqb_string; auto.
        simpl. destruct a.  unfold update, t_update.
        destruct (eqb_string s x0); auto. }
    unfold R. fold R. split.
       auto.
     split. apply value_halts. apply v_abs.
     intros.
     destruct (R_halts H0) as [v [P Q]].
     pose proof (multistep_preserves_R _ _ _ P H0).
     apply multistep_preserves_R' with (msubst ((x,v)::env0) t1).
       eapply T_App. eauto.
       apply R_typable_empty; auto.
       eapply multi_trans.  eapply multistep_App2; eauto.
       eapply multi_R.
       simpl.  rewrite subst_msubst.
       eapply ST_AppAbs; eauto.
       eapply typable_empty__closed.
       apply (R_typable_empty H1).
       eapply instantiation_env_closed; eauto.
       eapply (IHHT ((x,T2)::c)).
          intros. unfold update, t_update, lookup. destruct (eqb_string x x0); auto.
       constructor; auto.

  - (* T_App *)
    rewrite msubst_app.
    destruct (IHHT1 c H env0 V) as [_ [_ P1]].
    pose proof (IHHT2 c H env0 V) as P2.  fold R in P1.  auto.

  - (* T_Bool: true *)
    simpl.
    split; try split; auto.
    + rewrite msubst_closed; auto; unfold closed; intro. intro. inversion H0.
    + rewrite msubst_closed; auto using value_halts.
      unfold closed; intro. intro. inversion H0.

  - (* T_Bool: false *)
    simpl.
    split; try split; auto.
    + rewrite msubst_closed; auto; unfold closed; intro. intro. inversion H0.
    + rewrite msubst_closed; auto using value_halts.
      unfold closed; intro. intro. inversion H0.

  - (* T_If *)
    pose proof (IHHT1 _ H _ V) as H1.
    pose proof (IHHT2 _ H _ V) as H2.
    pose proof (IHHT3 _ H _ V) as H3.
    destruct H1 as [? [? ?]].
    clear IHHT1 IHHT2 IHHT3.

    rewrite msubst_if.
    destruct H1.
    destruct H1.

    remember (msubst env0 t1) as t1'.
    remember (msubst env0 t2) as t2'.
    remember (msubst env0 t3) as t3'.
    clear Heqt1' Heqt2' Heqt3'.
    pose proof preservation_multistep _ _ _ H0 H1.

    apply canonical_forms_bool in H6; auto.
    destruct H6; subst.

    + apply multistep_preserves_R' with (t' := t2'); eauto.
      * constructor; auto;
          destruct T1; simpl in *; destruct H2; destruct H3; auto.
      * eauto using multistep_IfTrue.
    + apply multistep_preserves_R' with (t' := t3'); eauto.
      * constructor; auto;
          destruct T1; simpl in *; destruct H2; destruct H3; auto.
      * eauto using multistep_IfFalse.

  - (* T_Pair *)
    pose proof (IHHT1 _ H _ V) as H1.
    pose proof (IHHT2 _ H _ V) as H2.
    clear IHHT1 IHHT2 H V.

    rewrite msubst_pair.
    remember (msubst env0 t1) as t1'.
    remember (msubst env0 t2) as t2'.
    clear Heqt1' Heqt2'.

    simpl.
    split; try split.

    + constructor; auto;
      destruct T1; simpl in *; destruct H1;
        destruct T2; simpl in *; destruct H2;
          auto.
    + pose proof R_halts H1.
      pose proof R_halts H2.
      destruct H as [? [? ?]].
      destruct H0 as [? [? ?]].
      exists <{(x, x0)}>. apply multistep_Pair; auto.
    + intros.
      destruct (multistep_Pair_decompose _ _ _ _ H).
      split; eauto using multistep_preserves_R.

  - (* Fst *)
   rewrite msubst_fst.
   pose proof IHHT _ H _ V as HR; clear IHHT H V.
   destruct HR as [HT' [Hh HR]].
   remember (msubst env0 t0) as t; clear Heqt.
   inversion Hh. destruct H.
   pose proof (preservation_multistep _ _ _ HT' H).
   inversion H1; subst; inversion H0; subst; clear H0; clear H1.
   destruct (HR _ _ H).

   apply multistep_preserves_R' with (t' := t1); eauto;
     apply multistep_Fst with (t2 := t2); auto.

  - (* Snd *)
   rewrite msubst_snd.
   pose proof IHHT _ H _ V as HR; clear IHHT H V.
   destruct HR as [HT' [Hh HR]].
   remember (msubst env0 t0) as t; clear Heqt.
   inversion Hh. destruct H.
   pose proof (preservation_multistep _ _ _ HT' H).
   inversion H1; subst; inversion H0; subst; clear H0; clear H1.
   destruct (HR _ _ H).

   apply multistep_preserves_R' with (t' := t2); eauto;
     apply multistep_Snd with (t1 := t1); auto.
Qed.

(* ----------------------------------------------------------------- *)
(** *** Normalization Theorem *)

(** And the final theorem: *)

Theorem normalization : forall t T, empty |- t \in T -> halts t.
Proof.
  intros.
  replace t with (msubst nil t) by reflexivity.
  apply (@R_halts T).
  apply (msubst_R nil); eauto.
  eapply V_nil.
Qed.

(* 2020-09-09 21:08 *)


(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2018       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
(****************************************************************************)
(*                                                                          *)
(*                         Naive Set Theory in Coq                          *)
(*                                                                          *)
(*                     INRIA                        INRIA                   *)
(*              Rocquencourt                        Sophia-Antipolis        *)
(*                                                                          *)
(*                                 Coq V6.1                                 *)
(*									    *)
(*			         Gilles Kahn 				    *)
(*				 Gerard Huet				    *)
(*									    *)
(*									    *)
(*                                                                          *)
(* Acknowledgments: This work was started in July 1993 by F. Prost. Thanks  *)
(* to the Newton Institute for providing an exceptional work environment    *)
(* in Summer 1995. Several developments by E. Ledinot were an inspiration.  *)
(****************************************************************************)

Require Export Finite_sets.
Require Export Constructive_sets.
Require Export Classical.
Require Export Classical_sets.
Require Export Powerset.
Require Export Powerset_facts.
Require Export Powerset_Classical_facts.
Require Export Gt.
Require Export Lt.

Section Finite_sets_facts.
  Variable U : Type.

  Lemma finite_cardinal :
    forall X:Ensemble U, Finite U X ->  exists n : nat, cardinal U X n.
  Proof.
    induction 1 as [| A _ [n H]].
    exists 0; auto with sets.
    exists (S n); auto with sets.
  Qed.

  Lemma cardinal_finite :
    forall (X:Ensemble U) (n:nat), cardinal U X n -> Finite U X.
  Proof.
    induction 1; auto with sets.
  Qed.

  Theorem Add_preserves_Finite :
    forall (X:Ensemble U) (x:U), Finite U X -> Finite U (Add U X x).
  Proof.
    intros X x H'.
    elim (classic (In U X x)); intro H'0; auto with sets.
    rewrite (Non_disjoint_union U X x); auto with sets.
  Qed.

  Theorem Singleton_is_finite : forall x:U, Finite U (Singleton U x).
  Proof.
    intro x; rewrite <- (Empty_set_zero U (Singleton U x)).
    change (Finite U (Add U (Empty_set U) x)); auto with sets.
  Qed.

  Theorem Union_preserves_Finite :
    forall X Y:Ensemble U, Finite U X -> Finite U Y -> Finite U (Union U X Y).
  Proof.
    intros X Y H; induction H as [|A Fin_A Hind x].
    rewrite (Empty_set_zero U Y). trivial.
    intros.
    rewrite (Union_commutative U (Add U A x) Y).
    rewrite <- (Union_add U Y A x).
    rewrite (Union_commutative U Y A).
    apply Add_preserves_Finite.
    apply Hind. assumption.
  Qed.

  Lemma Finite_downward_closed :
    forall A:Ensemble U,
      Finite U A -> forall X:Ensemble U, Included U X A -> Finite U X.
  Proof.
    intros A H'; elim H'; auto with sets.
    intros X H'0.
    rewrite (less_than_empty U X H'0); auto with sets.
    intros; elim Included_Add with U X A0 x; auto with sets.
    destruct 1 as [A' [H5 H6]].
    rewrite H5; auto with sets.
  Qed.

  Lemma Intersection_preserves_finite :
    forall A:Ensemble U,
      Finite U A -> forall X:Ensemble U, Finite U (Intersection U X A).
  Proof.
    intros A H' X; apply Finite_downward_closed with A; auto with sets.
  Qed.

  Lemma cardinalO_empty :
    forall X:Ensemble U, cardinal U X 0 -> X = Empty_set U.
  Proof.
    intros X H; apply (cardinal_invert U X 0); trivial with sets.
  Qed.

  Lemma inh_card_gt_O :
    forall X:Ensemble U, Inhabited U X -> forall n:nat, cardinal U X n -> n > 0.
  Proof.
    induction 1 as [x H'].
    intros n H'0.
    elim (gt_O_eq n); auto with sets.
    intro H'1; generalize H'; generalize H'0.
    rewrite <- H'1; intro H'2.
    rewrite (cardinalO_empty X); auto with sets.
    intro H'3; elim H'3.
  Qed.

  Lemma card_soustr_1 :
    forall (X:Ensemble U) (n:nat),
      cardinal U X n ->
      forall x:U, In U X x -> cardinal U (Subtract U X x) (pred n).
  Proof.
    intros X n H'; elim H'.
    intros x H'0; elim H'0.
    clear H' n X.
    intros X n H' H'0 x H'1 x0 H'2.
    elim (classic (In U X x0)).
    intro H'4; rewrite (add_soustr_xy U X x x0).
    elim (classic (x = x0)).
    intro H'5.
    absurd (In U X x0); auto with sets.
    rewrite <- H'5; auto with sets.
    intro H'3; try assumption.
    cut (S (pred n) = pred (S n)).
    intro H'5; rewrite <- H'5.
    apply card_add; auto with sets.
    red; intro H'6; elim H'6.
    intros H'7 H'8; try assumption.
    elim H'1; auto with sets.
    unfold pred at 2; symmetry .
    apply S_pred with (m := 0).
    change (n > 0).
    apply inh_card_gt_O with (X := X); auto with sets.
    apply Inhabited_intro with (x := x0); auto with sets.
    red; intro H'3.
    apply H'1.
    elim H'3; auto with sets.
    rewrite H'3; auto with sets.
    elim (classic (x = x0)).
    intro H'3; rewrite <- H'3.
    cut (Subtract U (Add U X x) x = X); auto with sets.
    intro H'4; rewrite H'4; auto with sets.
    intros H'3 H'4; try assumption.
    absurd (In U (Add U X x) x0); auto with sets.
    red; intro H'5; try exact H'5.
    lapply (Add_inv U X x x0); tauto.
  Qed.

  Lemma cardinal_is_functional :
    forall (X:Ensemble U) (c1:nat),
      cardinal U X c1 ->
      forall (Y:Ensemble U) (c2:nat), cardinal U Y c2 -> X = Y -> c1 = c2.
  Proof.
    intros X c1 H'; elim H'.
    intros Y c2 H'0; elim H'0; auto with sets.
    intros A n H'1 H'2 x H'3 H'5.
    elim (not_Empty_Add U A x); auto with sets.
    clear H' c1 X.
    intros X n H' H'0 x H'1 Y c2 H'2.
    elim H'2.
    intro H'3.
    elim (not_Empty_Add U X x); auto with sets.
    clear H'2 c2 Y.
    intros X0 c2 H'2 H'3 x0 H'4 H'5.
    elim (classic (In U X0 x)).
    intro H'6; apply f_equal.
    apply H'0 with (Y := Subtract U (Add U X0 x0) x).
    elimtype (pred (S c2) = c2); auto with sets.
    apply card_soustr_1; auto with sets.
    rewrite <- H'5.
    apply Sub_Add_new; auto with sets.
    elim (classic (x = x0)).
    intros H'6 H'7; apply f_equal.
    apply H'0 with (Y := X0); auto with sets.
    apply Simplify_add with (x := x); auto with sets.
    pattern x at 2; rewrite H'6; auto with sets.
    intros H'6 H'7.
    absurd (Add U X x = Add U X0 x0); auto with sets.
    clear H'0 H' H'3 n H'5 H'4 H'2 H'1 c2.
    red; intro H'.
    lapply (Extension U (Add U X x) (Add U X0 x0)); auto with sets.
    clear H'.
    intro H'; red in H'.
    elim H'; intros H'0 H'1; red in H'0; clear H' H'1.
    absurd (In U (Add U X0 x0) x); auto with sets.
    lapply (Add_inv U X0 x0 x); [ intuition | apply (H'0 x); apply Add_intro2 ].
  Qed.

  Lemma cardinal_Empty : forall m:nat, cardinal U (Empty_set U) m -> 0 = m.
  Proof.
    intros m Cm; generalize (cardinal_invert U (Empty_set U) m Cm).
    elim m; auto with sets.
    intros; elim H0; intros; elim H1; intros; elim H2; intros.
    elim (not_Empty_Add U x x0 H3).
  Qed.

  Lemma cardinal_unicity :
    forall (X:Ensemble U) (n:nat),
      cardinal U X n -> forall m:nat, cardinal U X m -> n = m.
  Proof.
    intros; apply cardinal_is_functional with X X; auto with sets.
  Qed.

  Lemma card_Add_gen :
    forall (A:Ensemble U) (x:U) (n n':nat),
      cardinal U A n -> cardinal U (Add U A x) n' -> n' <= S n.
  Proof.
    intros A x n n' H'.
    elim (classic (In U A x)).
    intro H'0.
    rewrite (Non_disjoint_union U A x H'0).
    intro H'1; cut (n = n').
    intro E; rewrite E; auto with sets.
    apply cardinal_unicity with A; auto with sets.
    intros H'0 H'1.
    cut (n' = S n).
    intro E; rewrite E; auto with sets.
    apply cardinal_unicity with (Add U A x); auto with sets.
  Qed.

  Lemma incl_st_card_lt :
    forall (X:Ensemble U) (c1:nat),
      cardinal U X c1 ->
      forall (Y:Ensemble U) (c2:nat),
	cardinal U Y c2 -> Strict_Included U X Y -> c2 > c1.
  Proof.
    intros X c1 H'; elim H'.
    intros Y c2 H'0; elim H'0; auto with sets arith.
    intro H'1.
    elim (Strict_Included_strict U (Empty_set U)); auto with sets arith.
    clear H' c1 X.
    intros X n H' H'0 x H'1 Y c2 H'2.
    elim H'2.
    intro H'3; elim (not_SIncl_empty U (Add U X x)); auto with sets arith.
    clear H'2 c2 Y.
    intros X0 c2 H'2 H'3 x0 H'4 H'5; elim (classic (In U X0 x)).
    intro H'6; apply gt_n_S.
    apply H'0 with (Y := Subtract U (Add U X0 x0) x).
    elimtype (pred (S c2) = c2); auto with sets arith.
    apply card_soustr_1; auto with sets arith.
    apply incl_st_add_soustr; auto with sets arith.
    elim (classic (x = x0)).
    intros H'6 H'7; apply gt_n_S.
    apply H'0 with (Y := X0); auto with sets arith.
    apply sincl_add_x with (x := x0).
    rewrite <- H'6; auto with sets arith.
    pattern x0 at 1; rewrite <- H'6; trivial with sets arith.
    intros H'6 H'7; red in H'5.
    elim H'5; intros H'8 H'9; try exact H'8; clear H'5.
    red in H'8.
    generalize (H'8 x).
    intro H'5; lapply H'5; auto with sets arith.
    intro H; elim Add_inv with U X0 x0 x; auto with sets arith.
    intro; absurd (In U X0 x); auto with sets arith.
    intro; absurd (x = x0); auto with sets arith.
  Qed.

  Lemma incl_card_le :
    forall (X Y:Ensemble U) (n m:nat),
      cardinal U X n -> cardinal U Y m -> Included U X Y -> n <= m.
  Proof.
    intros; elim Included_Strict_Included with U X Y; auto with sets arith; intro.
    cut (m > n); auto with sets arith.
    apply incl_st_card_lt with (X := X) (Y := Y); auto with sets arith.
    generalize H0; rewrite <- H2; intro.
    cut (n = m).
    intro E; rewrite E; auto with sets arith.
    apply cardinal_unicity with X; auto with sets arith.
  Qed.

  Lemma G_aux :
    forall P:Ensemble U -> Prop,
      (forall X:Ensemble U,
	Finite U X ->
	(forall Y:Ensemble U, Strict_Included U Y X -> P Y) -> P X) ->
      P (Empty_set U).
  Proof.
    intros P H'; try assumption.
    apply H'; auto with sets.
    clear H'; auto with sets.
    intros Y H'; try assumption.
    red in H'.
    elim H'; intros H'0 H'1; try exact H'1; clear H'.
    lapply (less_than_empty U Y); [ intro H'3; try exact H'3 | assumption ].
    elim H'1; auto with sets.
  Qed.

  Lemma Generalized_induction_on_finite_sets :
    forall P:Ensemble U -> Prop,
      (forall X:Ensemble U,
	Finite U X ->
	(forall Y:Ensemble U, Strict_Included U Y X -> P Y) -> P X) ->
      forall X:Ensemble U, Finite U X -> P X.
  Proof.
    intros P H'0 X H'1.
    generalize P H'0; clear H'0 P.
    elim H'1.
    intros P H'0.
    apply G_aux; auto with sets.
    clear H'1 X.
    intros A H' H'0 x H'1 P H'3.
    cut (forall Y:Ensemble U, Included U Y (Add U A x) -> P Y); auto with sets.
    generalize H'1.
    apply H'0.
    intros X K H'5 L Y H'6; apply H'3; auto with sets.
    apply Finite_downward_closed with (A := Add U X x); auto with sets.
    intros Y0 H'7.
    elim (Strict_inclusion_is_transitive_with_inclusion U Y0 Y (Add U X x));
      auto with sets.
    intros H'2 H'4.
    elim (Included_Add U Y0 X x);
      [ intro H'14
	| intro H'14; elim H'14; intros A' E; elim E; intros H'15 H'16; clear E H'14
	| idtac ]; auto with sets.
    elim (Included_Strict_Included U Y0 X); auto with sets.
    intro H'9; apply H'5 with (Y := Y0); auto with sets.
    intro H'9; rewrite H'9.
    apply H'3; auto with sets.
    intros Y1 H'8; elim H'8.
    intros H'10 H'11; apply H'5 with (Y := Y1); auto with sets.
    elim (Included_Strict_Included U A' X); auto with sets.
    intro H'8; apply H'5 with (Y := A'); auto with sets.
    rewrite <- H'15; auto with sets.
    intro H'8.
    elim H'7.
    intros H'9 H'10; apply H'10 || elim H'10; try assumption.
    generalize H'6.
    rewrite <- H'8.
    rewrite <- H'15; auto with sets.
  Qed.

End Finite_sets_facts.


(* --------------------------------------------------------------------
 * Copyright (c) - 2017--2021 - Xavier Allamigeon <xavier.allamigeon at inria.fr>
 * Copyright (c) - 2017--2021 - Ricardo D. Katz <katz@cifasis-conicet.gov.ar>
 * Copyright (c) - 2019--2021 - Pierre-Yves Strub <pierre-yves@strub.nu>
 *
 * Distributed under the terms of the CeCILL-B-V1 license
 * -------------------------------------------------------------------- *)

(* -------------------------------------------------------------------- *)

(* ==================================================================== *)
(* Parts taken from dft.v:                                              *)
(* (c) Copyright 2015, CRI, MINES ParisTech, PSL Research University    *)
(* ==================================================================== *)

From mathcomp Require Import all_ssreflect.
From mathcomp Require Import bigop ssralg ssrnum zmodp matrix vector fingroup perm order.
Require Import extra_matrix.
Import Order.Theory.

Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.

Local Open Scope ring_scope.
Import GRing.Theory Num.Theory.

Section InnerProduct.
Variable n : nat.
Variable R : realFieldType.
Implicit Types u v w : 'cV[R]_n.

(* Inner product notation as in classfun *)
(* Reserved Notation "'[ u , v ]" *)
(*   (at level 80, format "'[hv' ''[' u , '/ '  v ] ']'"). *)

(* XXX: Issues with naming. Why vdot, why not dotu as in addr? *)

(* Inner product specialized to vectors of algebraic numbers *)
Definition vdot u v := \sum_i u i 0 * (v i 0).

(* Protected&reversed version for the Additive, etc... instances *)
Definition vdotr_head k u v := let: tt := k in vdot v u.
Definition vnorm_head k u   := let: tt := k in vdot u u.

Notation "''[' u , v ]" := (vdot u v) : ring_scope.
(* Recall: This is the squared norm *)
Notation "''[|' u |]^2" := '[u, u] : ring_scope.

Notation vdotr := (vdotr_head tt).
Notation vnorm := (vnorm_head tt).

Lemma vdotrE u v : vdotr v u = '[u, v].
Proof. by []. Qed.

(* Relation to matrix multiplication *)
Lemma vdot_def (u v : 'cV_n) : '[u,v]%:M = v^T *m u.
Proof.
  rewrite [_*m_]mx11_scalar; apply/congr1; rewrite !mxE.
by apply: eq_bigr => k _; rewrite !mxE mulrC.
Qed.

Lemma vdotr_is_linear u : linear (vdotr u : 'cV_n -> R^o).
Proof.
  move=> a v w.
  rewrite linear_sum -big_split //=; apply: eq_bigr => x _ /=.
    by rewrite !mxE mulrDl -mulrA.
Qed.

Canonical vdotr_additive u := Additive (vdotr_is_linear u).
Canonical vdotr_linear   u := Linear   (vdotr_is_linear u).

Lemma vdot0l u : '[0,u] = 0.
Proof. by rewrite -vdotrE linear0. Qed.
Lemma vdotNl u v : '[- u, v] = - '[u, v].
Proof. by rewrite -vdotrE linearN. Qed.
Lemma vdotDl u v w : '[u + v, w] = '[u, w] + '[v, w].
Proof. by rewrite -vdotrE linearD. Qed.
Lemma vdotBl u v w : '[u - v, w] = '[u, w] - '[v, w].
Proof. by rewrite -vdotrE linearB. Qed.
Lemma vdotMnl u v m : '[u *+ m, v] = '[u, v] *+ m.
Proof. by rewrite -!vdotrE linearMn. Qed.
Lemma vdotZl a u v : '[a *: u, v] = a * '[u, v].
Proof. by rewrite -vdotrE linearZ. Qed.

Lemma vdotC u v : '[u,v] = '[v,u].
Proof.
  by apply: eq_bigr => i _; rewrite mulrC.
Qed.

Lemma vdotr_delta_mx u i : '[u, delta_mx i 0] = u i 0.
Proof.
rewrite /vdot (bigD1 i _) //= mxE eq_refl mulr1.
suff ->: \sum_(j < n | j != i) u j 0 * ((delta_mx i 0):'cV_n) j 0 = 0 by apply:addr0.
by apply: big1 => j; rewrite mxE; move/negbTE ->; rewrite mulr0.
Qed.

Lemma vdotl_delta_mx u i : '[delta_mx i 0, u] = u i 0.
Proof.
by rewrite vdotC vdotr_delta_mx.
Qed.

Lemma vdotr_const_mx1 u : '[u, const_mx 1] = \sum_i u i 0.
Proof.
rewrite /vdot.
apply: eq_bigr => i _.
- by rewrite mxE mulr1.
Qed.

Lemma vdotBr u v w : '[u, v - w] = '[u, v] - '[u, w].
Proof. by rewrite !(vdotC u) vdotBl. Qed.
Canonical vdot_additive u := Additive (vdotBr u).

Lemma vdot0r u : '[u,0] = 0.
Proof. exact: raddf0. Qed.
Lemma vdotNr u v : '[u, - v] = - '[u, v].
Proof. exact: raddfN. Qed.
Lemma vdotDr u v w : '[u, v + w] = '[u, v] + '[u, w].
Proof. exact: raddfD. Qed.
Lemma vdotMnr u v m : '[u, v *+ m] = '[u, v] *+ m.
Proof. exact: raddfMn. Qed.
Lemma vdotZr a u v : '[u, a *: v] = a * '[u, v].
Proof. by rewrite !(vdotC u) vdotZl. Qed.

Lemma vdot_perm (s: 'S_n) u v :
  '[row_perm s u, row_perm s v] = '[u,v].
Proof.
suff: '[row_perm s u, row_perm s v]%:M = '[u,v]%:M :> 'M_1.
- by move/matrixP/(_ 0 0); rewrite !mxE.
- rewrite 2!vdot_def; exact: mulmx_tr_row_perm.
Qed.

Lemma vdot_sumDl (I: eqType) r (P: pred I) F x : '[\sum_(i <- r | P i) F i, x] = \sum_(i <- r | P i) '[F i, x].
Proof.
apply: (big_morph (fun y => '[y,x])).
- move => y y'; exact: vdotDl.
- exact: vdot0l.
Qed.

(* Order properties *)
Lemma vnorm_ge0 x : 0 <= '[| x |]^2.
Proof. by rewrite sumr_ge0 // => i _; exact: sqr_ge0. Qed.

Lemma vnorm_eq0 u : ('[| u |]^2 == 0) = (u == 0).
Proof.
apply/idP/eqP=> [|->]; last by rewrite vdot0r.
move/eqP/psumr_eq0P=> /= u0; apply/matrixP=> i j.
apply/eqP; rewrite ord1 !mxE -sqrf_eq0 expr2.
by rewrite u0 // => y _; exact: sqr_ge0.
Qed.

Lemma vnorm_gt0 u : ('[| u |]^2 > 0) = (u != 0).
Proof. by rewrite lt_def vnorm_ge0 vnorm_eq0 andbT. Qed.

End InnerProduct.

Notation "''[' u , v ]" := (vdot u v) : ring_scope.
(* Recall: This is the squared norm *)
Notation "''[|' u |]^2" := '[u, u] : ring_scope.

Section Extra.

Variable m n p : nat.
Variable R : realFieldType.

Lemma vdot_mulmx (A : 'M[R]_(n,p)) u v : '[u, A *m v] = '[A^T *m u, v].
Proof.
suffices: '[u, A *m v]%:M = ('[A^T *m u, v]%:M : 'M_1)
  by move/matrixP/(_ 0 0); rewrite !mxE //=.
by rewrite 2!vdot_def trmx_mul mulmxA.
Qed.

Lemma vdot_col_mx  (x y : 'cV[R]_n) (v w : 'cV[R]_p) :
  '[col_mx x v , col_mx y w] = '[ x , y] + '[v , w].
Proof.
rewrite /vdot big_split_ord /=.
by apply: congr2; apply: eq_bigr => i _; rewrite 2?col_mxEu 2?col_mxEd.
Qed.

Lemma vdot_castmx (epn : n = p) (u:'cV[R]_n) v : '[castmx (epn, erefl 1%N) u, castmx (epn, erefl 1%N) v] = '[u,v].
Proof.
suff: (castmx (epn, erefl 1%N) v)^T *m (castmx (epn, erefl 1%N) u) = v^T *m u.
- by rewrite -2!vdot_def; move/matrixP/(_ 0 0); rewrite !mxE /=.
- rewrite trmx_cast /= -{2}(esymK epn).
  have {1}->: erefl 1%N = esym (erefl 1%N) by done.
  by rewrite mulmx_cast esymK -{2}(esymK epn) castmx_id castmxKV.
Qed.

Lemma row_vdot (A : 'M[R]_(n,p)) x i : '[(row i A)^T, x] = ((A *m x) i 0).
Proof.
rewrite mxE.
apply: eq_bigr => j _.
by rewrite !mxE.
Qed.

Lemma matrix_vdot (A : 'M[R]_(n,p)) (B : 'M[R]_(p,m)) i j :
  '[(row i A)^T, col j B] = (A *m B) i j.
Proof.
rewrite mxE.
apply: eq_bigr=> k _.
by rewrite !mxE.
Qed.

Lemma vdot1 (v: 'cV[R]_1) : '[1%:M, v] = v 0 0.
Proof.
apply/(scalar_mx_inj (ltn0Sn 0)).
by rewrite vdotC vdot_def trmx1 mul1mx -mx11_scalar.
Qed.

End Extra.

Section Orthogonal.

Context {R : realFieldType} {n : nat}.

Let E := [vectType R of 'cV[R]_n].

Implicit Type (V : {vspace E}) (x : 'cV[R]_n).

Definition orth_fun0 V x := (* WARNING: this is not the orthogonal projection,
                            * however this function is bijective over V *)
  let B := vbasis V in
  \sum_(i < \dim V) '[B`_i, x] *: B`_i.

Lemma orth_fun_lmorph V : lmorphism (orth_fun0 V).
Proof.
split.
- move => x y; rewrite /orth_fun0.
  by under eq_big do [ | rewrite vdotBr scalerBl]; rewrite sumrB.
- move => x y; rewrite /orth_fun0.
  by under eq_big do [ | rewrite vdotZr -scalerA]; rewrite scaler_sumr.
Qed.

Fact orth_fun_key : unit. Proof. by []. Qed.
Definition orth_fun V := linfun (Linear (orth_fun_lmorph V)).

Definition orthv V := lker (orth_fun V).

Notation "V ^OC " := (orthv V) (at level 8, format "V ^OC") : vspace_scope.

Lemma orthvP {V x} :
  reflect (forall y, y \in V -> '[y,x] = 0) (x \in V^OC)%VS.
Proof.
set B := vbasis V.
have equiv :
  (forall i: 'I_\dim V, '[B`_i, x] = 0) <-> (forall y, y \in V -> '[y,x] = 0).
- split => [h y | h i].
  + move/coord_vbasis ->.
    rewrite vdot_sumDl; apply/big1 => i _; rewrite vdotZl.
    by move/(_ i): h ->; rewrite mulr0.
  + apply/h/vbasis_mem/mem_nth.
    by rewrite size_tuple.
apply/(equivP _ equiv)/(iffP idP) => [| H]; last first.
- rewrite memv_ker lfunE /=; apply/eqP.
  by apply/big1 => i _; move/(_ i): H ->; rewrite scale0r.
- rewrite memv_ker lfunE /= => /eqP.
  by move/basis_free/freeP/(_ (fun i => '[B`_i,x])): (vbasisP V).
Qed.

Lemma direct_orthvP V :
  (V :&: V^OC = 0)%VS.
Proof.
apply/eqP; rewrite -subv0; apply/subvP => x.
rewrite memv_cap memv0 => /andP [x_in_V /orthvP/(_ _ x_in_V)]/eqP.
by rewrite vnorm_eq0.
Qed.

Lemma limg_orthv_fun V : limg (orth_fun V) = V.
Proof.
have h: (orth_fun V @: V = V)%VS.
- apply/eqP; rewrite -(geq_leqif (dimv_leqif_eq _)).
  + by rewrite (limg_dim_eq (direct_orthvP _)).
  + apply/subvP => x /memv_imgP [y _ ->].
    rewrite lfunE /=; apply/memv_suml => i _.
    apply/memvZ/vbasis_mem/mem_nth; by rewrite size_tuple.
- apply/subv_anti; apply/andP; split.
  + apply/subvP => x /memv_imgP [y _ ->].
    rewrite lfunE /=; apply/memv_suml => i _.
    apply/memvZ/vbasis_mem/mem_nth; by rewrite size_tuple.
  + by rewrite -{1}h limgS ?subvf.
Qed.

Lemma dim_orthv V : (\dim V^OC = n - \dim V)%N.
Proof.
move: (limg_ker_dim (orth_fun V) (fullv)).
rewrite capfv dimvf /Vector.dim /= muln1 limg_orthv_fun => {2}<-.
by rewrite addnK.
Qed.

Lemma orthv_spanP (p : nat) (Y : p.-tuple 'cV[R]_n) x :
  reflect (forall y, y \in Y -> '[y,x] = 0) (x \in (span Y)^OC)%VS.
Proof.
apply/(iffP orthvP) => [h y | h y].
- move/memv_span; exact: h.
- move/coord_span ->; rewrite vdot_sumDl; apply/big1 => i _; rewrite vdotZl.
  by move/(_ Y`_i): h ->; rewrite ?mulr0 // mem_nth ?size_tuple.
Qed.

Lemma orthK : involutive orthv.
Proof.
move => V; symmetry; apply/eqP; rewrite -(geq_leqif (dimv_leqif_eq _)).
- rewrite 2!dim_orthv subKn //.
  by move: (dimvS (subvf V)); rewrite dimvf /Vector.dim /= muln1.
- apply/subvP => x x_in_V; apply/orthvP => y /orthvP/(_ _ x_in_V).
  by rewrite vdotC.
Qed.

Lemma orthS : {mono orthv : V W /~ (V <= W)%VS }.
Proof.
suff h: forall V W, (V <= W)%VS -> (W^OC <= V^OC)%VS.
- move => V W; apply/idP/idP;
  first rewrite -{2}[V]orthK -{2}[W]orthK; exact: h.
- move => V W /subvP V_sub_W.
  apply/subvP => x /orthvP h'.
  apply/orthvP => y /V_sub_W; exact: h'.
Qed.

End Orthogonal.

Notation "V ^OC " := (orthv V) (at level 8, format "V ^OC") : vspace_scope.


(* ea *)

Structure Group : Type := const_kozos
{
  A :> Set;

  op : A -> A -> A ;
  inv : A -> A ;
  z : A ;

  op_assoc : forall a b c, op a (op b c) = op (op a b) c;
  op_z : forall a, op a z = a /\ op z a = a ;
  op_inverse : forall a, op a (inv a) = z /\ op (inv a) a = z
}.

(* hf *)

Inductive Fin : nat -> Set :=
  | fzero : forall {p:nat}, Fin (S p)
  | fsucc : forall {p:nat}, Fin p -> Fin (S p).

Check @fzero 0.
Check @fzero 1.
Check @fsucc 1 (@fzero 0).
Check @fzero 2.
Check @fsucc 2 (@fzero 1).
Check @fsucc 2 (@fsucc 1 (@fzero 0)).

Lemma Fin_0_all_v1 : forall (P:Fin 0 -> Prop) (x:Fin 0), P x.
Proof.
  intros P x.
  exact match x as x' return P x with  (* TODO: understand *)
          | @fzero _ => match x with end
          | @fsucc _ _ => match x with end
        end.
Qed.

(* cf. https://coq.inria.fr/library/Coq.Vectors.Fin.html: case0, rectS *) (* TODO: understand *)

Definition Fin_0_all (P:Fin 0 -> Type) (x: Fin 0): P x :=
  match x with | fzero | fsucc _ => fun devil => False_rect (@IDProp) devil end.

Definition Fin_ind_succ (P:forall {p:nat}, Fin (S p) -> Type) (Pz: forall p, @P p fzero)
  (Ps : forall {p:nat} (x:Fin (S p)), P x -> P (fsucc x)) : forall {p:nat} (x: Fin (S p)), P x :=
  fix Fin_ind_succ_fix {p:nat} (x: Fin (S p)): P x :=
    match x with
      | @fzero q => Pz q
      | @fsucc 0 y => Fin_0_all (fun f => P (fsucc f)) y
      | @fsucc (S k) y => Ps y (Fin_ind_succ_fix y)
    end.

Lemma Fin_ind_succ_test : forall (p:nat) (x:Fin (S p)), x = fzero \/ exists y, x = fsucc y.
Proof.
  apply Fin_ind_succ with (P:=fun p x => x = fzero \/ exists y, x = fsucc y).
  intro p.
  left.
  reflexivity.
  intros p x IH.
  destruct IH as [IH1 | IH2].
  right.
  exists x.
  reflexivity.
  destruct IH2 as [y IH2'].
  right.
  exists x.
  reflexivity.
Qed.

Definition Fin_cases := Fin_ind_succ_test.

Lemma Fin_1_all_fzero : forall (x:Fin 1), x = fzero.
Proof.
  intro x.
  assert (x = fzero \/ exists y, x = fsucc y) as L.
  apply Fin_cases.
  destruct L as [L1 | L2].
  assumption.
  destruct L2 as [y L2'].
  assert (x <> fsucc y) as L3.
  apply Fin_0_all with (P:=fun y => x <> fsucc y).
  contradiction.
Qed.

(* cf. https://coq.inria.fr/library/Coq.Vectors.Fin.html: FS_inj *) (* TODO: understand *)

Definition Fin_fsucc_inj_magic {n:nat} (x y:Fin n) (eq: fsucc x = fsucc y) : x = y :=
  match eq in _ = a return
    match a as a' in Fin m return match m with | 0 => Prop | S n' => Fin n' -> Prop end with
      | fzero => fun _ => True
      | fsucc y => fun x' => x' = y
    end x
  with
    eq_refl => eq_refl
  end.

Lemma Fin_fsucc_inj : forall {n:nat} (x y:Fin n), fsucc x = fsucc y -> x = y.
Proof.
  intros n x y.
  apply Fin_fsucc_inj_magic.
Qed.

(*
  let z := @fzero in let s := @fsucc in

  Fin 4: (z 3)   (s 3 (z 2))   (s 3 (s 2 (z 1)))   (s 3 (s 2 (s 1 (z 0))))
                 ^             ^                   ^
                /             /                   /
               / s 3         / s 3               / s 3
              /             /                   /
  Fin 3: (z 2)   (s 2 (z 1))   (s 2 (s 1 (z 0)))
                 ^             ^
                /             /
               / s 2         / s 2
              /             /
  Fin 2: (z 1)   (s 1 (z 0))
                 ^
                /
               / s 1
              /
  Fin 1: (z 0)
  Fin 0: -
*)

Fixpoint succ {n:nat} (x:Fin n) : Fin n := (* n not used :( *)
  match x with
    | @fzero 0 => @fzero 0
    | @fzero (S q) => @fsucc (S q) (@fzero q)
    | @fsucc (S q) y => let s := succ y in match s return Fin (S (S q)) with
                                            | @fzero r => @fzero (S q)
                                            | @fsucc r z => @fsucc (S q) s
                                          end
    | @fsucc 0 y => match y with end
  end.

Eval compute in succ (@fzero 0).
Eval compute in succ (@fzero 1).
Eval compute in succ (@fsucc 1 (@fzero 0)).
Eval compute in succ (@fzero 2).
Eval compute in succ (@fsucc 2 (@fzero 1)).
Eval compute in succ (@fsucc 2 (@fsucc 1 (@fzero 0))).
Eval compute in succ (@fzero 3).
Eval compute in succ (@fsucc 3 (@fzero 2)).
Eval compute in succ (@fsucc 3 (@fsucc 2 (@fzero 1))).
Eval compute in succ (@fsucc 3 (@fsucc 2 (@fsucc 1 (@fzero 0)))).

Fixpoint iter {A:Set} (n:nat) (f:A->A) (x:A) :=
  match n with
    | 0 => x
    | S p => f (iter p f x)
  end.

Eval compute in Nat.add 0 1.

Eval compute in iter 0 (Nat.add 1) 0.
Eval compute in iter 1 (Nat.add 1) 0.
Eval compute in iter 2 (Nat.add 1) 0.

Definition Z_n (n:nat) := Fin n.

Definition z_1_0 := @fzero 0.
Definition z_2_0 := @fzero 1.
Definition z_2_1 := @fsucc 1 (@fzero 0).
Definition z_3_0 := @fzero 2.
Definition z_3_1 := @fsucc 2 (@fzero 1).
Definition z_3_2 := @fsucc 2 (@fsucc 1 (@fzero 0)).

Fixpoint label {n:nat} (x:Z_n n) : nat := (* n not used :( *)
  match x with
    | @fzero _ => 0
    | @fsucc (S q) y => S (@label (S q) y)
    | @fsucc 0 y => match y with end
  end.

Eval compute in label z_1_0.
Eval compute in label z_2_0.
Eval compute in label z_2_1.
Eval compute in label z_3_0.
Eval compute in label z_3_1.
Eval compute in label z_3_2.

Definition Z_n_op {p:nat} (x:Z_n (S p)) (y:Z_n (S p)) : Z_n (S p) := iter (label y) succ x.

Eval compute in Z_n_op z_3_0 z_3_0.
Eval compute in Z_n_op z_3_0 z_3_1.
Eval compute in Z_n_op z_3_1 z_3_2.

Definition Z_n_inv {p:nat} (x:Z_n (S p)) : Z_n (S p) := iter ((S p) - label x) succ (@fzero p).

Eval compute in Z_n_inv z_3_0.
Eval compute in Z_n_inv z_3_1.
Eval compute in Z_n_inv z_3_2.

Lemma iter_succ {A:Set} : forall (p:nat) (f:A->A) (x:A), iter (S p) f x = f (iter p f x).
Proof.
  intros p f x.
  case p.
  simpl.
  reflexivity.
  simpl.
  reflexivity.
Qed.

Lemma Z_n_label_fzero : forall {p:nat}, label (@fzero p) = 0.
Proof.
  simpl.
  reflexivity.
Qed.

Lemma Z_n_label_fsucc : forall {p:nat} (x:Z_n (S p)), label (fsucc x) = S (label x).
Proof.
  apply Fin_ind_succ with (P:=fun p x => label (fsucc x) = S (label x)).
  simpl.
  reflexivity.
  simpl.
  reflexivity.
Qed.

Definition Z_n_label_last_pred {p:nat} (x:Z_n (S p)) := succ x = fzero -> label x = p.

Lemma Z_n_label_last : forall {p:nat} (x:Z_n (S p)), Z_n_label_last_pred x. (* not working in-line *)
Proof.
  apply Fin_ind_succ with (P:=fun p x => Z_n_label_last_pred x).
  unfold Z_n_label_last_pred.
  destruct p.
  simpl.
  reflexivity.
  simpl.
  discriminate.
  unfold Z_n_label_last_pred.
  intros p x IH.
  assert (succ x = fzero <-> succ (fsucc x) = fzero) as L. {
    split.
    simpl.
    intro H.
    rewrite H.
    reflexivity.

    simpl.
    intro H.
    assert (succ x = fzero \/ exists y, succ x = fsucc y) as H3.
    apply Fin_cases.
    destruct H3 as [H3_1 | H3_2].
    assumption.
    destruct H3_2 as [y H3_2].
    rewrite H3_2 in H.
    assert (fsucc (fsucc y) <> fzero) as T.
    discriminate.
    contradiction.
  }
  intro H.
  assert (succ x = fzero) as H2.
  apply L.
  assumption.
  rewrite Z_n_label_fsucc.
  rewrite IH.
  reflexivity.
  assumption.
Qed.

Require Import Lia. (* Omega deprecated *)

Definition Z_n_label_before_last_pred {p:nat} (x:Z_n (S p)) :=
  (exists y, succ x = fsucc y) -> label x < p.

Lemma Z_n_label_before_last : forall {p:nat} (x:Z_n (S p)), Z_n_label_before_last_pred x.
Proof.
  apply Fin_ind_succ with (P:=fun p x => Z_n_label_before_last_pred x). (* not working in-line *)
  unfold Z_n_label_before_last_pred.
  destruct p.
  intro Abs.
  destruct Abs as [y Abs].
  assert (succ fzero <> fsucc y) as Abs2.
  apply Fin_0_all with (P:=fun x => succ fzero <> fsucc x).
  contradiction.
  intro H.
  rewrite Z_n_label_fzero.
  lia.
  unfold Z_n_label_before_last_pred.
  intros p x IH H.
  simpl.
  destruct H as [y H].
  assert ((exists y, succ x = fsucc y) <-> exists y, succ (fsucc x) = fsucc y) as L. {
    split.
    simpl.
    intro H2.
    destruct H2 as [z H2].
    rewrite H2.
    exists (fsucc z).
    reflexivity.
    simpl.
    intro H2.
    destruct H2 as [z H2].
    assert (succ x = fzero \/ exists y, succ x = fsucc y) as H3.
    apply Fin_cases.
    destruct H3 as [H3_1 | H3_2].
    rewrite H3_1 in H2.
    rename H2 into Abs.
    assert (fzero <> fsucc z) as T.
    discriminate.
    contradiction.
    destruct H3_2 as [w H3_2].
    exists w.
    assumption.
  }
  assert (exists y, succ (fsucc x) = fsucc y) as H2.
  exists y.
  assumption.
  assert (exists y, succ x = fsucc y) as H3.
  apply L.
  assumption.
  assert (label x < p) as H4.
  apply IH.
  assumption.
  lia.
Qed.

Lemma Z_n_label_bounded : forall {p:nat} (x:Z_n (S p)), label x < S p.
Proof.
  intros p x.
  assert (succ x = fzero \/ exists y, succ x = fsucc y) as H.
  apply Fin_cases.
  destruct H as [H1 | H2].
  assert (label x = p) as H2.
  apply Z_n_label_last.
  assumption.
  rewrite H2.
  lia.
  assert (label x < p) as H3.
  apply Z_n_label_before_last.
  assumption.
  rewrite H3.
  lia.
Qed.

Lemma Z_n_label_inj : forall {p:nat} (x y:Z_n (S p)), label x = label y -> x = y.
Proof.
  intros p x y.
  induction p.
  assert (x = fzero) as Hx. { apply Fin_1_all_fzero. }
  assert (y = fzero) as Hy. { apply Fin_1_all_fzero. }
  rewrite Hx.
  rewrite Hy.
  reflexivity.
  assert (x = fzero \/ exists x', x = fsucc x') as Hx. { apply Fin_cases. }
  destruct Hx as [Hx1 | Hx2].
  rewrite Hx1.
  assert (y = fzero \/ exists y', y = fsucc y') as Hy. { apply Fin_cases. }
  destruct Hy as [Hy1 | Hy2].
  rewrite Hy1.
  reflexivity.
  destruct Hy2 as [y' Hy2].
  rewrite Hy2.
  intro Abs.
  assert (label (@fzero p) <> label (fsucc y')) as T.
  rewrite Z_n_label_fzero.
  rewrite Z_n_label_fsucc.
  lia.
  contradiction.

  destruct Hx2 as [x' Hx2].
  rewrite Hx2.
  assert (y = fzero \/ exists y', y = fsucc y') as Hy. { apply Fin_cases. }
  destruct Hy as [Hy1 | Hy2].
  rewrite Hy1.
  intro Abs.
  assert (label (fsucc x') <> label (@fzero p)) as T.
  rewrite Z_n_label_fzero.
  rewrite Z_n_label_fsucc.
  lia.
  contradiction.
  destruct Hy2 as [y' Hy2].
  rewrite Hy2.
  repeat rewrite Z_n_label_fsucc.
  intro H.
  assert (label x' = label y') as H2.
  lia.
  assert (x' = y') as H3.
  apply IHp.
  assumption.
  rewrite H3.
  reflexivity.
Qed.

Notation "n % m" := (Nat.modulo n m) (at level 20) : type_scope.

Lemma Z_n_label_succ : forall {p:nat} (x:Z_n (S p)), label (succ x) = (S (label x)) % (S p).
Proof.
  apply Fin_ind_succ with (P:=fun p x => label (succ x) = (S (label x)) % (S p)).
  intro p.
  rewrite Z_n_label_fzero.
  destruct p.
  simpl.
  reflexivity.
  assert (label (succ (@fzero (S p))) = 1) as H. {
    simpl.
    reflexivity.
  }
  rewrite H.
  Check PeanoNat.Nat.mod_small.
  assert (forall n m:nat, n = m <-> m = n) as L. { lia. }
  rewrite L.
  apply PeanoNat.Nat.mod_small.
  lia.
  intros p x IH.
  destruct p.
  assert (x = fzero) as H2.
  apply Fin_1_all_fzero.
  rewrite H2.
  simpl.
  reflexivity.

  assert (succ x = fzero \/ exists y, succ x = fsucc y) as H.
  apply Fin_cases.
  destruct H as [H1 | H2].
  assert (succ (fsucc x) = fzero) as H2.
  simpl.
  rewrite H1.
  reflexivity.
  rewrite H2.
  rewrite Z_n_label_fsucc.
  rewrite @Z_n_label_last with (p:=S p).
  rewrite Z_n_label_fzero.
  assert (forall n m:nat, n = m <-> m = n) as L. { lia. }
  rewrite L.
  apply PeanoNat.Nat.mod_same.
  lia.
  assumption.

  destruct H2 as [y H2].
  assert (succ (fsucc x) = fsucc (fsucc y)) as H3.
  simpl.
  rewrite H2.
  reflexivity.
  rewrite H3.
  rewrite <- H2.
  repeat rewrite Z_n_label_fsucc.
  rewrite IH.
  assert (forall n m, n < S m -> (S ((S n) % (S (S m))) = (S (S n)) % (S (S (S m))))) as L. {
    intros n m C.
    assert ((S n) % (S (S m)) = S n) as S1.
    assert (S n < S (S m)) as C'.
    lia.
    apply PeanoNat.Nat.mod_small.
    assumption.
    assert ((S (S n)) % (S (S (S m))) = S (S n)) as S2.
    assert (S (S n) < S (S (S m))) as C'.
    lia.
    apply PeanoNat.Nat.mod_small.
    assumption.
    rewrite S1.
    rewrite S2.
    reflexivity.
  }
  rewrite L.
  reflexivity.
  apply @Z_n_label_before_last with (p:=S p).
  exists y.
  assumption.
Qed.

Lemma nat_succ_mod_idemp_r : forall a p:nat, (S (a % (S p))) % (S p) = (S a) % (S p).
Proof.
  intros a p.
  assert (forall n, S n = 1 + n) as L. { lia. }
  rewrite L.
  rewrite L with (n:=a).
  rewrite PeanoNat.Nat.add_mod_idemp_r.
  reflexivity.
  lia.
Qed.

Lemma Z_n_op_label {p:nat} : forall (x y:Z_n (S p)),
  label (Z_n_op x y) = (label x + label y) % (S p).
Proof.
  intros x y.
  unfold Z_n_op.
  set (k:=label y).
  induction k.
  assert (label (iter 0 succ x) = label x) as H.
  simpl.
  reflexivity.
  rewrite H.
  assert (label x + 0 = label x) as H2.
  lia.
  rewrite H2.
  assert (forall n m:nat, n = m <-> m = n) as L. { lia. }
  rewrite L.
  apply PeanoNat.Nat.mod_small.
  apply Z_n_label_bounded.
  rewrite iter_succ.
  rewrite Z_n_label_succ.
  rewrite IHk.
  set (l:=label x).
  assert (l + S k = S (l + k)) as L. { lia. }
  rewrite L.
  apply nat_succ_mod_idemp_r.
Qed.

Lemma Z_n_inv_label {p:nat} : forall (x:Z_n (S p)), label (Z_n_inv x) = ((S p) - label x) % (S p).
Proof.
  intro x.
  unfold Z_n_inv.
  set (k:=label x).
  assert ((S p) - k = S (p - k)) as H. {
    assert (k <= p) as H. {
      assert (S k <= S p) as H. { apply Z_n_label_bounded. }
      lia.
    }
    lia.
  }
  rewrite H.
  rewrite iter_succ.
  rewrite Z_n_label_succ.
  set (m:=p - k).
  assert (label (iter m succ (@fzero p)) = m % (S p)) as H2. {
    induction m.
    simpl.
    lia.
    rewrite iter_succ.
    assert (label (succ (iter m succ (@fzero p))) = (S (label (iter m succ (@fzero p))) % (S p))) as H2. {
      rewrite Z_n_label_succ.
      reflexivity.
    }
    rewrite H2.
    rewrite IHm.
    apply nat_succ_mod_idemp_r.
  }
  rewrite H2.
  apply nat_succ_mod_idemp_r.
Qed.

Theorem Z_n_group (p:nat) : Group.
Proof.
  apply (const_kozos (Z_n (S p)) (@Z_n_op p) (@Z_n_inv p) (@fzero p)).
  intros a b c.
  - assert (forall x y z n, n<>0 -> (x + ((y + z) % (n))) % (n) = (((x + y) % (n)) + z) % (n)) as L. {
      intros x y z n H.
      rewrite PeanoNat.Nat.add_mod_idemp_r.
      rewrite PeanoNat.Nat.add_mod_idemp_l.
      rewrite PeanoNat.Nat.add_assoc.
      reflexivity.
      assumption.
      assumption.
    }
    assert (label (Z_n_op a (Z_n_op b c)) = (label a + ((label b + label c) % (S p))) % (S p)) as S1.
    repeat rewrite Z_n_op_label.
    reflexivity.
    assert (label (Z_n_op (Z_n_op a b) c) = ((label a + label b) % (S p) + label c) % (S p)) as S2.
    repeat rewrite Z_n_op_label.
    reflexivity.
    rewrite L in S1.
    assert (label (Z_n_op a (Z_n_op b c)) = label (Z_n_op (Z_n_op a b) c)) as H.
    rewrite <- S2 in S1.
    rename S1 into H.
    clear S2.
    assumption.
    apply Z_n_label_inj.
    assumption.
    lia.
  - intro a.
    split.
    + assert (label (Z_n_op a fzero) = label a) as H.
      rewrite Z_n_op_label.
      rewrite Z_n_label_fzero.
      assert (label a + 0 = label a) as H. { lia. }
      rewrite H.
      apply PeanoNat.Nat.mod_small.
      apply Z_n_label_bounded.
      apply Z_n_label_inj.
      assumption.
    + assert (label (Z_n_op fzero a) = label a) as H.
      rewrite Z_n_op_label.
      rewrite Z_n_label_fzero.
      assert (0 + label a = label a) as H. { lia. }
      rewrite H.
      apply PeanoNat.Nat.mod_small.
      apply Z_n_label_bounded.
      apply Z_n_label_inj.
      assumption.
  - intro a.
    split.
    + assert (label (Z_n_op a (Z_n_inv a)) = label (@fzero p)) as H.
      rewrite Z_n_op_label.
      rewrite Z_n_label_fzero.
      rewrite Z_n_inv_label.
      assert (label a < S p) as H. { apply Z_n_label_bounded. }
      set (l:=label a).
      set (n:=S p).
      assert (n <> 0) as H2. { lia. }
      assert (l < n) as H'. { assumption. }
      assert (forall j m, m <> 0 -> j <= m -> (j + (m - j) % (m)) % (m) = 0) as L. {
        intros j m H3 H4.
        assert (j = 0 \/ j > 0) as H5. { lia. }
        destruct H5 as [H5_1 | H5_2].
        rewrite H5_1.
        assert (m - 0 = m) as L. { lia. }
        rewrite L.
        rewrite PeanoNat.Nat.mod_same.
        apply PeanoNat.Nat.mod_0_l.
        assumption.
        assumption.
        set (k:=m - j).
        assert (k < m) as H6. { lia. }
        rewrite PeanoNat.Nat.mod_small with (a:=k).
        subst k.
        assert (j + (m - j) = (m - j) + j) as H7. { lia. }
        rewrite H7.
        rewrite PeanoNat.Nat.sub_add.
        apply PeanoNat.Nat.mod_same.
        assumption.
        assumption.
        assumption.
      }
      apply L.
      assumption.
      lia.
      apply Z_n_label_inj.
      assumption.
    + assert (label (Z_n_op (Z_n_inv a) a) = label (@fzero p)) as H.
      rewrite Z_n_op_label.
      rewrite Z_n_label_fzero.
      rewrite Z_n_inv_label.
      assert (label a < S p) as H. { apply Z_n_label_bounded. }
      set (l:=label a).
      set (n:=S p).
      assert (n <> 0) as H2. { lia. }
      assert (l < n) as H'. { assumption. }
      assert (forall j m, m <> 0 -> j <= m -> ((m - j) % (m) + j) % (m) = 0) as L. {
        intros j m H3 H4.
        assert (j = 0 \/ j > 0) as H5. { lia. }
        destruct H5 as [H5_1 | H5_2].
        rewrite H5_1.
        assert (m - 0 = m) as L. { lia. }
        rewrite L.
        rewrite PeanoNat.Nat.mod_same.
        apply PeanoNat.Nat.mod_0_l.
        assumption.
        assumption.
        set (k:=m - j).
        assert (k < m) as H6. { lia. }
        rewrite PeanoNat.Nat.mod_small with (a:=k).
        subst k.
        rewrite PeanoNat.Nat.sub_add.
        apply PeanoNat.Nat.mod_same.
        assumption.
        assumption.
        assumption.
      }
      apply L.
      assumption.
      lia.
      apply Z_n_label_inj.
      assumption.
Qed.