(*
Inductive In (k : K) : Context K T ->  Prop :=
| In_hd : forall k' t' c, K_eq k k' = true -> In k (Ctxt k' t' c)
| In_tl : forall k' t' c, K_eq k k' = false -> In k c -> In k (Ctxt k' t' c).
Hint Constructors In.

Inductive  NoDup : Context K T -> Prop :=
| NoDup_dot  : NoDup (Ctxt_dot K T)
| NoDup_ctxt : forall k t c,
                 NoDup c ->
                 ~In k c ->
                 NoDup (Ctxt k t c).
Hint Constructors NoDup.

Function ink (c : Context K T) (k: K) : bool :=
  match c with
    | Ctxt_dot => false
    | (Ctxt k' t' c') =>
      match K_eq k k' with
        | true  => true
        | false => (ink c' k)
      end
  end.
*)

(*

Function inkt (c : Context K T) (k: K) (t : T) : bool :=
  match c with
    | Ctxt_dot => false
    | (Ctxt k' t' c') =>
      match K_eq k k' with
        | true => match T_eq t t' with | true => true | false => (inkt c' k t) end
        | false => (inkt c' k t)
      end
  end.

Function extends (c c' : Context K T) : bool :=
  match c with
    | Ctxt_dot => true
    | Ctxt k t c'' =>
      if inkt c' k t
      then extends c'' c'
      else false
  end.

*)

Lemma NoDup_inkt_absurd:
  forall k t t' c,
    NoDup (Ctxt k t c) ->
    inkt c k t' = true ->
    False.
Proof.
  intros k t t' c NoDupder inktder.
  functional induction (inkt c k t').
  inversion inktder.
  apply beq_tvar_eq in e0.
  subst.
  inversion NoDupder; try assumption.
  crush.
  apply beq_tvar_eq in e0.
  subst.
  inversion NoDupder; try assumption.
  crush.
  apply IHb in inktder.
  inversion inktder.
  inversion NoDupder.
  subst.
  constructor; try assumption.
  inversion H1; try assumption.
  inversion NoDupder.
  subst.
  crush.
Qed.

Lemma inkt_t_weakening:
  forall c k t, inkt c k t = true -> ink c k = true.
Proof.
  intros.
  Context_ind_cases(induction c) Case.
 Case  "(Ctxt_dot K T)".
  crush.
 Case  "(Ctxt k t c)".
  crush.
  case_eq (K_eq k k0).
  SCase " K_eq k k0 = true".
   intros.
   reflexivity.
  SCase " K_eq k k0 = false".
   intros.
   rewrite H0 in H.
   apply IHc in H.
   assumption.
Qed.

Lemma map_some_weakening:
  forall c c' t v,  map c v = Some t -> extends c c' = true ->  map c' v = Some t.
Proof.
  intros.
  functional induction (map c v).
  inversion H.
  inversion H.
  subst.
  apply beq_tvar_eq in e0.
  subst.
  unfold extends in H0.
  fold extends in H0.
  case_eq (inkt c' k' t).
  intros.
  rewrite H1 in H0.
  apply inkt_map_agreement; try assumption.
  intros.
  rewrite H1 in H0.
  inversion H0.
  apply IHo in H; try assumption.
  unfold extends in H0.
  fold extends in H0.
  case_eq (inkt c' k' t').
  intros.
  rewrite H1 in H0; try assumption.
  intros.
  rewrite H1 in H0; try assumption.
  inversion H0.
Qed.

Functional Scheme map_ind2 := Induction for map Sort Prop.

Function map' (c : Context K T) (k: K) : option T :=
  match k, c with
    | k'', (Ctxt k' t' c') =>
      if K_eq k'' k'
      then Some t'
      else (map' c' k)
    | _, Ctxt_dot => None
  end.

Function nodup' (c : (Context K T)) : bool :=
  match c with
    | Ctxt_dot => true
    | (Ctxt k' t' c') =>
      match map' c' k' with
        | Some t  => false
        | None  => nodup' c'
      end
end.

Function extends' (c c' : Context K T) : bool :=
  match c with
    | Ctxt_dot => true
    | Ctxt k t c'' =>
      match map' c' k with
       | Some t => extends' c'' c'
       | None => false
      end
  end.

Fixpoint map'' (c : Context K T) (k: K) : option T :=
  match k, c with
    | k'', (Ctxt k' t' c') =>
      if K_eq k'' k'
      then Some t'
      else (map'' c' k)
    | _, Ctxt_dot => None
  end.

Functional Scheme map''_ind := Induction for map'' Sort Prop.

Function extends'' (c c' : Context K T) : bool :=
  match c with
    | Ctxt_dot => true
    | Ctxt k t c'' =>
      match map'' c' k with
       | Some t => extends'' c'' c'
       | None => false
      end
  end.

Fixpoint nodup'' (c : (Context K T)) : bool :=
  match c with
    | Ctxt_dot => true
    | (Ctxt k' t' c') =>
      match map'' c' k' with
        | Some _  => false
        | None  => nodup'' c'
      end
end.

Lemma map_extends_some_agreement_fschem_ind:
  forall c c',
   extends'' c c' = true ->
   nodup'' c = true ->
   nodup'' c' = true ->
   forall v t,
     map'' c v  = Some t ->
     map'' c' v = Some t.
Proof.
  intros c c' ext nodupc nodupc' v t map''cv.
  functional induction (map'' c v).
  admit.
  admit. (* bad *)
  admit. (* ? *)
Admitted.

Lemma map_extends_some_agreement_fschem_ind_diff_order:
  forall c v t,
   map'' c v  = Some t ->
   nodup'' c = true ->
   forall c',
     extends'' c c' = true ->
     nodup'' c' = true ->
     map'' c' v = Some t.
Proof.
  intros.
  functional induction (map'' c v).
  admit.
  admit. (* bad *)
  admit. (* ? *)
Admitted.

(* https://stackoverflow.com/questions/20554493/coq-induction-over-functions-without-losing-information *)

Lemma map''_extends_some_agreement_remember:
  forall c v t,
   map'' c v  = Some t ->
   nodup'' c = true ->
   forall c',
     extends'' c c' = true ->
     nodup'' c' = true ->
     map'' c' v = Some t.
Proof.
  intros.
  remember (map'' c v) as F.
  functional induction (map'' c v).
  admit. (* bad *)
  admit. (* bad *)
  admit. (* ? *)
Admitted.

Lemma map_extends_some_agreement_remember:
  forall c v t,
   map c v  = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros.
  remember (map c v) as F.
  induction F.
  admit. (* bad *)
  admit. (* bad *)
Admitted.

Lemma map_extends_some_agreement_remember_as:
  forall c v t,
   map c v  = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros.
  (* pose (Z := (map c v)). No *)
  set (map c v).
  functional induction (map c v).
  admit. (* bad *)
  admit. (* bad *)
  admit. (* ? *)
Admitted.

Lemma map_extends_some_agreement_remember_revert:
  forall c v t,
   map c v  = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros.
  remember (map c v = Some t) as Fn.
  functional induction (map c v).
  admit. (* bad *)
  admit. (* bad *)
  admit. (* ? *)
Admitted.

Lemma map_extends_some_agreement_1:
  forall c v t,
   map c v  = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros.
  destruct (map c v).
  admit. (* bad *)
  admit. (* bad *)
Admitted.

Lemma map_extends_some_agreement_2:
  forall c v t,
   map c v  = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros.
  induction c, v, (map c v) using map_ind.
  admit. (* bad *)
  admit. (* bad *)
  admit.
Admitted.

Function map3 (k: K) (c : Context K T) : option T :=
  match k, c with
    | k'', (Ctxt k' t' c') =>
      match K_eq k'' k' with
      | true => Some t'
      | false => (map3 k c')
      end
    | _, Ctxt_dot => None
end.

Function extends3 (c c' : Context K T) : bool :=
  match c with
    | Ctxt_dot => true
    | Ctxt k t c'' =>
      match map3 k c'  with
       | Some t => extends'' c'' c'
       | None => false
      end
  end.

Function nodup3 (c : (Context K T)) : bool :=
  match c with
    | Ctxt_dot => true
    | (Ctxt k' t' c') =>
      match map3 k' c' with
        | Some _  => false
        | None  => nodup3 c'
      end
end.

Lemma map_extends_some_agreement_3:
  forall c v t,
   map3 v c = Some t ->
   nodup3 c = true ->
   forall c',
     extends3 c c' = true ->
     nodup3 c' = true ->
     map3 v c' = Some t.
Proof.
  intros.
  functional induction (map3 v c).
  admit. (* bad. *)
  admit. (* good, is it argument order or the match ? *)
  admit.
Admitted.

Lemma map_extends_some_agreement_4:
  forall c v t,
   map3 v c = Some t ->
   nodup3 c = true ->
   forall c',
     extends3 c c' = true ->
     nodup3 c' = true ->
     map3 v c' = Some t.
Proof.
  intros.
  remember (map3 v c) as HailMary.
  induction v, c, HailMary using map3_ind.
  admit. (* ? *)
  admit. (* ? *)
  admit.
Admitted.

Lemma map_extends_some_agreement_5:
  forall c v t,
   map c v = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros c v t.
  remember (map c v = Some t) as mapcv.
  induction c, v, mapcv using map_ind.
  intros.
  rewrite Heqmapcv in H.
  inversion H.

  intros.
  rewrite Heqmapcv in H.
  move H after H2.
  move H1 after H2.
  move e0 after H2.
  move H0 after c'0.
  apply beq_tvar_eq in e0.
  rewrite e0.
  rewrite e0 in H.
  clear e.
  clear Heqmapcv.
  clear e0.
  pose proof H as H'.
  unfold map in H'.
  fold map in H'.
  rewrite beq_tvar_refl in H'.
  inversion H'.
  subst.
  clear H'.

  induction c'0.

  admit. (* Still stuck. *)

(*
  intros.
  apply extends_l_strengthen with (k:= k') (t:= t') in H1.
  unfold nodup in H0.
  fold nodup in H0.
  case_eq(map c' k'); intros.
  rewrite H3 in H0.
  inversion H0.
  unfold map in Heqmapcv.
  fold map in Heqmapcv.
  rewrite e0 in Heqmapcv.
  rewrite H3 in H0.
  apply IHmapcv with (c'0:= c'0) in Heqmapcv; try assumption.
*)
Admitted.

Lemma map_extends_some_agreement_6:
  forall c v o t,
   map c v = o ->
   nodup c = true ->
   o = Some t ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = o.
Proof.
  intros c v o t.
  functional induction (map c v).
  admit. (* bad *)
  admit. (* bad *)
  admit. (* bad *)
Qed.

Lemma map_extends_some_agreement_7:
  forall c v t,
   map c v = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros c v t.
  functional induction (map c v); try solve [crush].
  intros.
  inversion H.
  apply beq_tvar_eq in e0.
  subst.

Lemma map_extends_some_agreement_8_extends:
  forall c v t,
    map c v = Some t ->
    forall c',
      extends c c' = true ->
      nodup c = true ->
      nodup c' = true ->
      map c' v = Some t.
Proof.
  intros c v t mapcvt c'.
  pose proof mapcvt as X.
  functional induction (extends c c'); try solve [crush].
  intros.
  inversion H0.
  case_eq(map c'' k); intros; rewrite H2 in H3.
  inversion H3.

  unfold map in mapcvt.
  fold map in mapcvt.
  case_eq(K_eq v k); intros; rewrite H4 in mapcvt.
  apply beq_tvar_eq in H4.
  subst.
  inversion mapcvt.
  subst.
  admit. (* need to invert but the context is consistent. *)

  apply IHb in mapcvt; try assumption.
Qed.

Lemma map_extends_some_agreement_8_map':
  forall c c',
    extends' c c' = true ->
    nodup' c = true ->
    nodup' c' = true ->
    forall v t,
      map' c v = Some t ->
      map' c' v = Some t.
Proof.
  intros c c'.
  functional induction (extends' c c'); try solve [crush].
  intros.
  inversion H0.
  case_eq(map' c'' k); intros; rewrite H3 in H4.
  inversion H4.

  apply IHb with (v:= v) (t:= t1) in H; try assumption.
  case_eq(K_eq v k); intros.
  apply beq_tvar_eq in H5.
  subst.
  admit. (* need to invert but the context is consistent. *)

  unfold map' in H2.
  fold map' in H2.
  rewrite H5 in H2.
  assumption.
Qed.

Inductive maptr: (Context K T) -> K -> T -> Prop :=
  | maptr1:
      forall c k t,
        map c k = Some t ->
        maptr c k t.

Function maptf (c : Context K T) (k: K) (t : T): bool :=
  match c with
    | Ctxt_dot => false
    | (Ctxt k' t' c') =>
      match K_eq k k' with
        | true  => beq_tau t t'
        | false => (maptf c' k t)
      end
  end.

Lemma map_extends_some_agreement_9_maptf:
  forall c v t,
   maptf c v t = true ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros c v t.
  remember (maptf c v t = true) as X.
  functional induction (maptf c v t); try solve [crush].
  intros.
  admit.  (* bad *)
  admit.
Admitted.

(* Totally useless. *)
Lemma map_extends_some_agreement_10_maptr:
  forall c v  t,
   maptr c v t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros c v t maptrder.
  induction maptrder.
Admitted.

(* The last approach is to build the map relation, prove it equivalent
and induct on it. *)

Inductive mapits: (Context K T) -> K -> T -> Prop :=
  | mapits_top     : forall c k t,
                       mapits (Ctxt k t c) k t
  | mapits_recurse : forall k' c k t t',
                       mapits c k t ->
                       mapits (Ctxt k' t' c) k t.

Lemma map_extends_some_agreement_11_mapits:
  forall c v  t,
   mapits c v t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' v = Some t.
Proof.
  intros c v t mapitder.
  induction mapitder.
  intros.
  admit.
  intros.
  (* perhaps *)
Admitted.

(* Or I can go the way that worked for getD, functional induction but
 an extends relation. *)

Inductive extendsr : (Context K T) -> (Context K T) -> Prop :=
  | extendsr_empty   : forall c,
                         extendsr empty c
  | extendsr_left  :
      forall c c' k t,
        map c' k = Some t ->
        extendsr c c' ->
        extendsr (Ctxt k t c) c'.

Lemma map_extends_some_agreement_12_extendsr:
  forall c k  t,
   map c k = Some t ->
   nodup c = true ->
   forall c',
     nodup c' = true ->
     extendsr c c' ->
     map c' k = Some t.
Proof.
  intros c k t.
  functional induction (map c k).
  intros.
  inversion H.
  Case "Some k0 = k0".
   intros.
   inversion H.
   clear H.
   pose proof e0 as e0'.
   apply beq_tvar_eq in e0.
   subst.
   inversion H2.
   assumption.
  Case "getD d' alpha = Some k".
   intros.
   inversion H0.
   inversion H2.
   subst.
   apply IHo with (c'0 := c'0) in H; try assumption.
   case_eq(map c' k'); intros; rewrite H3 in H4.
   inversion H4.
   assumption.
Qed.

Lemma map_extends_some_agreement_12_functional_inversion_on_extends:
  forall c k  t,
   map c k = Some t ->
   nodup c = true ->
   forall c',
     extends c c' = true ->
     nodup c' = true ->
     map c' k = Some t.
Proof.
  intros c v t.
  intros.
  pose proof H as H'.
  functional induction (map c v).
  inversion H.

  apply beq_tvar_eq in e0.
  inversion H'.
  subst.
  clear H'.
  clear H.
  functional inversion H1.
  subst.

  admit.
(*
  assert (Z: map c'0 k' = Some t).
  admit.
  rewrite H3 in Z.
  inversion Z.
*)

  apply extends_l_strengthen with (k:= k') (t:= t') in H1.
  unfold nodup in H0.
  fold nodup in H0.
  case_eq(map c'0 k'); intros; rewrite H3 in H0.
  inversion H0.
  apply IHo in H; try assumption.
Admitted.

Lemma extends_iff_extendsr:
  forall c c',
    extendsr c c' <-> extends c c' = true.
Proof.
  intros c c'.
  split.
  intros extendsrder.
  induction extendsrder.
  apply empty_extended_by_all.
  unfold extends.
  fold extends.
  rewrite H.
  assumption.

(*
  intros extendsder.
  functional induction (extends c c').
  constructor.
  apply IHb in extendsder.
  constructor; try assumption.
  admit.
  inversion extendsder.
*)
  induction c.
  intros.
  constructor.
  intros.
  unfold extends in H.
  fold extends in H.
  case_eq(map c' k); intros; rewrite H0 in H.
  apply IHc in H.
  constructor.
  admit.
  assumption.
  inversion H.
Qed.

Lemma extends_r_strengthen_add:
  forall c c',
    nodup c = true ->
    extends c c' = true ->
    forall k t,
      nodup (add c'k t) = true ->
      extends c (add c' k t) = true.
Proof.
  intros c c'.
  functional induction (extends c c'); try solve [crush].
  intros.
  pose proof H0 as H0'.
  unfold nodup in H0.
  fold nodup in H0.
  pose proof H as H'.
  unfold nodup in H.
  fold nodup in H.
  case_eq(map c'' k); intros; rewrite H2 in H; try solve[inversion H].
  unfold extends.
  fold extends.
  case_eq(map (add c' k0 t0) k); intros.
  case_eq(T_eq t t1); intros.
  apply IHb with (k:= k0) (t:= t0) in H; try assumption.
  apply beq_tau_eq in e1.
  subst.
  case_eq(K_eq k0 k); intros.
  apply beq_tvar_eq in H5.
  subst.

  admit.
  move e0 after H3.
(*
  apply beq_tau_eq in e1.
  subst.
  case_eq(K_eq k0 k); intros.
  apply beq_tvar_eq in H4.
  subst.
  rewrite e0 in H1.
  inversion H1.
*)
  admit.

(* before nodup
  intros c c'.
  functional induction (extends c c'); try solve [crush].
  intros.
  apply IHb with (k:= k0) (t:= t) in H.
  unfold extends.
  fold extends.
  case_eq (map (add c' k0 t0) k); intros; try assumption.
  apply map_add_disagreement_absurd with (k':= k0) (t':= t1) in e0; try assumption.
  inversion e0.
  apply beq_tau_eq in e1.
  subst.
  admit. (* Just looks false, probably need a nodup. *)
  case_eq(K_eq k0 k); intros.
  apply beq_tvar_eq in H1.
  subst.
  (* H0 should invert. *)
  admit.
  apply beq_tau_eq in e1.
  subst.
  (* e0 and H0 should invert. *)
*)
Admitted.

Lemma extends_r_strengthen_add':
  forall c c',
    nodup c = true ->
    nodup c' = true ->
    extends c c' = true ->
    forall k t,
      map c' k = None ->
      extends c (add c' k t) = true.
Proof.
  intros c c'.
  functional induction (extends c c'); try solve [crush].
  intros.
  unfold nodup in H.
  fold nodup in H.
  case_eq(map c'' k); intros; rewrite H3 in H; try solve[inversion H].
  apply IHb with (k:= k0) (t:= t0) in H; try assumption.
  unfold extends.
  fold extends.
  case_eq(map (add c' k0 t0) k); intros.
  case_eq(T_eq t t1); intros.
  apply beq_tau_eq in e1.
  subst.
  admit.
  apply beq_tau_eq in e1.
  subst.
  admit.
  move e0 before H4.
  admit.
Admitted.

Lemma extends_r_strengthen_add'':
  forall c c',
    nodup c = true ->
    nodup c' = true ->
    extends c c' = true ->
    forall k t,
      map c' k = None ->
      extends c (add c' k t) = true.
Proof.
  intros c.
  induction c; try solve [crush].
  intros.
  pose proof H as H'.
  unfold nodup in H.
  fold nodup in H.
  case_eq(map c k); intros; rewrite H3 in *; try inversion H.
  unfold extends in H1.
  fold extends in H1.
  case_eq(map c' k); intros; rewrite H4 in H1; try solve[inversion H1].
  case_eq(T_eq t t1); intros; rewrite H6 in H1; try solve[inversion H1].
  unfold extends.
  fold extends.
  case_eq(map (add c' k0 t0) k); intros.
  case_eq(T_eq t t2); intros.
  specialize (IHc c' H H0 H1 k0 t0 H2).
  rewrite H5.
  assumption.
  (* two invalidations. *)
  case_eq(K_eq k0 k); intros.
  unfold nodup in H'.
  fold nodup in H'.
  rewrite H3 in H'.
  admit.
  admit.
Admitted.


(* (c) Copyright 2006-2016 Microsoft Corporation and Inria.                  *)
(* Distributed under the terms of CeCILL-B.                                  *)
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp
Require Import ssrfun ssrbool eqtype ssrnat seq choice fintype.
From mathcomp
Require Import ssralg finset fingroup morphism perm action.

(*****************************************************************************)
(* This file clones the entire ssralg hierachy for finite types; this allows *)
(* type inference to function properly on expressions that mix combinatorial *)
(* and algebraic operators (e.g., [set x + y | x in A, y in A]).             *)
(*   finZmodType, finRingType, finComRingType, finUnitRingType,              *)
(*   finComUnitRingType, finIdomType, finFieldType finLmodType,              *)
(*   finLalgType finAlgType finUnitAlgType                                   *)
(*      == the finite counterparts of zmodType, etc.                         *)
(* Note that a finFieldType is canonically decidable. All these structures   *)
(* can be derived using [xxxType of T] forms, e.g., if R has both canonical  *)
(* finType and ringType structures, then                                     *)
(*     Canonical R_finRingType := Eval hnf in [finRingType of R].            *)
(* declares the derived finRingType structure for R. As the implementation   *)
(* of the derivation is somewhat involved, the Eval hnf normalization is     *)
(* strongly recommended.                                                     *)
(*   This file also provides direct tie-ins with finite group theory:        *)
(*  [baseFinGroupType of R for +%R] == the (canonical) additive group        *)
(*      [finGroupType of R for +%R]    structures for R                      *)
(*                         {unit R} == the type of units of R, which has a   *)
(*                                     canonical group structure.            *)
(*                FinRing.unit R Ux == the element of {unit R} corresponding *)
(*                                     to x, where Ux : x \in GRing.unit.    *)
(*                           'U%act == the action by right multiplication of *)
(*                                     {unit R} on R, via FinRing.unit_act.  *)
(*                                     (This is also a group action.)        *)
(*****************************************************************************)

Local Open Scope ring_scope.

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

Module FinRing.

Local Notation mixin_of T b := (Finite.mixin_of (EqType T b)).

Section Generic.

(* Implicits *)
Variables (type base_type : Type) (class_of base_of : Type -> Type).
Variable to_choice : forall T, base_of T -> Choice.class_of T.
Variable base_sort : base_type -> Type.

(* Explicits *)
Variable Pack : forall T, class_of T -> Type -> type.
Variable Class : forall T b, mixin_of T (to_choice b) -> class_of T.
Variable base_class : forall bT, base_of (base_sort bT).

Definition gen_pack T :=
  fun bT b & phant_id (base_class bT) b =>
  fun fT m & phant_id (Finite.class fT) (Finite.Class m) =>
  Pack (@Class T b m) T.

End Generic.

Arguments gen_pack [type base_type class_of base_of to_choice base_sort].
Local Notation fin_ c := (@Finite.Class _ c c).
Local Notation do_pack pack T := (pack T _ _ id _ _ id).
Import GRing.Theory.

Definition groupMixin V := FinGroup.Mixin (@addrA V) (@add0r V) (@addNr V).
Local Notation base_group T vT fT :=
  (@FinGroup.PackBase T (groupMixin vT) (Finite.class fT)).
Local Notation fin_group B V := (@FinGroup.Pack B (@addNr V)).

Module Zmodule.

Section ClassDef.

Record class_of M :=
  Class { base : GRing.Zmodule.class_of M; mixin : mixin_of M base }.
Local Coercion base : class_of >-> GRing.Zmodule.class_of.
Local Coercion mixin : class_of >-> mixin_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.Zmodule.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.

Definition join_finType := @Finite.Pack zmodType (fin_ xclass) xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group zmodType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.Zmodule.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Canonical join_finType.
Notation finZmodType := type.
Notation "[ 'finZmodType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finZmodType'  'of'  T ]") : form_scope.
Coercion baseFinGroupType : type >-> FinGroup.base_type.
Canonical baseFinGroupType.
Coercion finGroupType : type >-> FinGroup.type.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
Notation "[ 'baseFinGroupType' 'of' R 'for' +%R ]" :=
    (BaseFinGroupType R (groupMixin _))
  (at level 0, format "[ 'baseFinGroupType'  'of'  R  'for'  +%R ]")
    : form_scope.
Notation "[ 'finGroupType' 'of' R 'for' +%R ]" :=
    (@FinGroup.clone R _ (finGroupType _) id _ id)
  (at level 0, format "[ 'finGroupType'  'of'  R  'for'  +%R ]") : form_scope.
End Exports.

End Zmodule.
Import Zmodule.Exports.

Section AdditiveGroup.

Variable U : finZmodType.
Implicit Types x y : U.

Lemma zmod1gE : 1%g = 0 :> U.            Proof. by []. Qed.
Lemma zmodVgE x : x^-1%g = - x.          Proof. by []. Qed.
Lemma zmodMgE x y : (x * y)%g = x + y.   Proof. by []. Qed.
Lemma zmodXgE n x : (x ^+ n)%g = x *+ n. Proof. by []. Qed.
Lemma zmod_mulgC x y : commute x y.      Proof. exact: GRing.addrC. Qed.
Lemma zmod_abelian (A : {set U}) : abelian A.
Proof. by apply/centsP=> x _ y _; apply: zmod_mulgC. Qed.

End AdditiveGroup.

Module Ring.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.Ring.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := Zmodule.Class (mixin c).
Local Coercion base : class_of >-> GRing.Ring.class_of.
Local Coercion base2 : class_of >-> Zmodule.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.Ring.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) cT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass cT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition join_finType := @Finite.Pack ringType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack ringType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group ringType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Import Exports.
Coercion base : class_of >-> GRing.Ring.class_of.
Coercion base2 : class_of >-> Zmodule.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Canonical join_finType.
Canonical join_finZmodType.
Notation finRingType := type.
Notation "[ 'finRingType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finRingType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

Section Unit.

Variable R : finRingType.

Definition is_inv (x y : R) := (x * y == 1) && (y * x == 1).
Definition unit := [qualify a x : R | [exists y, is_inv x y]].
Definition inv x := odflt x (pick (is_inv x)).

Lemma mulVr : {in unit, left_inverse 1 inv *%R}.
Proof.
rewrite /inv => x Ux; case: pickP => [y | no_y]; last by case/pred0P: Ux.
by case/andP=> _; move/eqP.
Qed.

Lemma mulrV : {in unit, right_inverse 1 inv *%R}.
Proof.
rewrite /inv => x Ux; case: pickP => [y | no_y]; last by case/pred0P: Ux.
by case/andP; move/eqP.
Qed.

Lemma intro_unit x y : y * x = 1 /\ x * y = 1 -> x \is a unit.
Proof.
by case=> yx1 xy1; apply/existsP; exists y; rewrite /is_inv xy1 yx1 !eqxx.
Qed.

Lemma invr_out : {in [predC unit], inv =1 id}.
Proof.
rewrite /inv => x nUx; case: pickP => // y invxy.
by case/existsP: nUx; exists y.
Qed.

Definition UnitMixin := GRing.UnitRing.Mixin mulVr mulrV intro_unit invr_out.

End Unit.

End Ring.
Import Ring.Exports.

Module ComRing.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.ComRing.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := Ring.Class (mixin c).
Local Coercion base : class_of >-> GRing.ComRing.class_of.
Local Coercion base2 : class_of >-> Ring.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.ComRing.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition comRingType := @GRing.ComRing.Pack cT xclass xT.
Definition join_finType := @Finite.Pack comRingType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack comRingType xclass xT.
Definition join_finRingType := @Ring.Pack comRingType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group comRingType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.ComRing.class_of.
Coercion base2 : class_of >-> Ring.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion comRingType : type >-> GRing.ComRing.type.
Canonical comRingType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finRingType.
Notation finComRingType := FinRing.ComRing.type.
Notation "[ 'finComRingType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finComRingType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End ComRing.
Import ComRing.Exports.

Module UnitRing.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.UnitRing.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := Ring.Class (mixin c).
Local Coercion base : class_of >-> GRing.UnitRing.class_of.
Local Coercion base2 : class_of >-> Ring.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.UnitRing.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition unitRingType := @GRing.UnitRing.Pack cT xclass xT.

Definition join_finType := @Finite.Pack unitRingType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack unitRingType xclass xT.
Definition join_finRingType := @Ring.Pack unitRingType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group unitRingType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.UnitRing.class_of.
Coercion base2 : class_of >-> Ring.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion unitRingType : type >-> GRing.UnitRing.type.
Canonical unitRingType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finRingType.
Notation finUnitRingType := FinRing.UnitRing.type.
Notation "[ 'finUnitRingType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finUnitRingType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End UnitRing.
Import UnitRing.Exports.

Section UnitsGroup.

Variable R : finUnitRingType.

Inductive unit_of (phR : phant R) := Unit (x : R) of x \is a GRing.unit.
Bind Scope group_scope with unit_of.

Let phR := Phant R.
Local Notation uT := (unit_of phR).
Implicit Types u v : uT.
Definition uval u := let: Unit x _ := u in x.

Canonical unit_subType := [subType for uval].
Definition unit_eqMixin := Eval hnf in [eqMixin of uT by <:].
Canonical unit_eqType := Eval hnf in EqType uT unit_eqMixin.
Definition unit_choiceMixin := [choiceMixin of uT by <:].
Canonical unit_choiceType := Eval hnf in ChoiceType uT unit_choiceMixin.
Definition unit_countMixin := [countMixin of uT by <:].
Canonical unit_countType := Eval hnf in CountType uT unit_countMixin.
Canonical unit_subCountType := Eval hnf in [subCountType of uT].
Definition unit_finMixin := [finMixin of uT by <:].
Canonical unit_finType := Eval hnf in FinType uT unit_finMixin.
Canonical unit_subFinType := Eval hnf in [subFinType of uT].

Definition unit1 := Unit phR (@GRing.unitr1 _).
Lemma unit_inv_proof u : (val u)^-1 \is a GRing.unit.
Proof. by rewrite GRing.unitrV ?(valP u). Qed.
Definition unit_inv u := Unit phR (unit_inv_proof u).
Lemma unit_mul_proof u v : val u * val v \is a GRing.unit.
Proof. by rewrite (GRing.unitrMr _ (valP u)) ?(valP v). Qed.
Definition unit_mul u v := Unit phR (unit_mul_proof u v).
Lemma unit_muluA : associative unit_mul.
Proof. by move=> u v w; apply: val_inj; apply: GRing.mulrA. Qed.
Lemma unit_mul1u : left_id unit1 unit_mul.
Proof. by move=> u; apply: val_inj; apply: GRing.mul1r. Qed.
Lemma unit_mulVu : left_inverse unit1 unit_inv unit_mul.
Proof. by move=> u; apply: val_inj; apply: GRing.mulVr (valP u). Qed.

Definition unit_GroupMixin := FinGroup.Mixin unit_muluA unit_mul1u unit_mulVu.
Canonical unit_baseFinGroupType :=
  Eval hnf in BaseFinGroupType uT unit_GroupMixin.
Canonical unit_finGroupType := Eval hnf in FinGroupType unit_mulVu.

Lemma val_unit1 : val (1%g : uT) = 1. Proof. by []. Qed.
Lemma val_unitM x y : val (x * y : uT)%g = val x * val y. Proof. by []. Qed.
Lemma val_unitV x : val (x^-1 : uT)%g = (val x)^-1. Proof. by []. Qed.
Lemma val_unitX n x : val (x ^+ n : uT)%g = val x ^+ n.
Proof. by case: n; last by elim=> //= n ->. Qed.

Definition unit_act x u := x * val u.
Lemma unit_actE x u : unit_act x u = x * val u. Proof. by []. Qed.

Canonical unit_action :=
  @TotalAction _ _ unit_act (@GRing.mulr1 _) (fun _ _ _ => GRing.mulrA _ _ _).
Lemma unit_is_groupAction : @is_groupAction _ R setT setT unit_action.
Proof.
move=> u _ /=; rewrite inE; apply/andP; split.
  by apply/subsetP=> x _; rewrite inE.
by apply/morphicP=> x y _ _; rewrite !actpermE /= [_ u]GRing.mulrDl.
Qed.
Canonical unit_groupAction := GroupAction unit_is_groupAction.

End UnitsGroup.

Module Import UnitsGroupExports.
Bind Scope group_scope with unit_of.
Canonical unit_subType.
Canonical unit_eqType.
Canonical unit_choiceType.
Canonical unit_countType.
Canonical unit_subCountType.
Canonical unit_finType.
Canonical unit_subFinType.
Canonical unit_baseFinGroupType.
Canonical unit_finGroupType.
Canonical unit_action.
Canonical unit_groupAction.
End UnitsGroupExports.

Notation unit R Ux := (Unit (Phant R) Ux).

Module ComUnitRing.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.ComUnitRing.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := ComRing.Class (mixin c).
Definition base3 R (c : class_of R) := @UnitRing.Class R (base c) (mixin c).
Local Coercion base : class_of >-> GRing.ComUnitRing.class_of.
Local Coercion base2 : class_of >-> ComRing.class_of.
Local Coercion base3 : class_of >-> UnitRing.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.ComUnitRing.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition comRingType := @GRing.ComRing.Pack cT xclass xT.
Definition finComRingType := @ComRing.Pack cT xclass xT.
Definition unitRingType := @GRing.UnitRing.Pack cT xclass xT.
Definition finUnitRingType := @UnitRing.Pack cT xclass xT.
Definition comUnitRingType := @GRing.ComUnitRing.Pack cT xclass xT.

Definition join_finType := @Finite.Pack comUnitRingType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack comUnitRingType xclass xT.
Definition join_finRingType := @Ring.Pack comUnitRingType xclass xT.
Definition join_finComRingType := @ComRing.Pack comUnitRingType xclass xT.
Definition join_finUnitRingType := @UnitRing.Pack comUnitRingType xclass xT.
Definition ujoin_finComRingType := @ComRing.Pack unitRingType xclass xT.
Definition cjoin_finUnitRingType := @UnitRing.Pack comRingType xclass xT.
Definition fcjoin_finUnitRingType := @UnitRing.Pack finComRingType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group comUnitRingType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.ComUnitRing.class_of.
Coercion base2 : class_of >-> ComRing.class_of.
Coercion base3 : class_of >-> UnitRing.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion comRingType : type >-> GRing.ComRing.type.
Canonical comRingType.
Coercion finComRingType : type >-> ComRing.type.
Canonical finComRingType.
Coercion unitRingType : type >-> GRing.UnitRing.type.
Canonical unitRingType.
Coercion finUnitRingType : type >-> UnitRing.type.
Canonical finUnitRingType.
Coercion comUnitRingType : type >-> GRing.ComUnitRing.type.
Canonical comUnitRingType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finRingType.
Canonical join_finComRingType.
Canonical join_finUnitRingType.
Canonical ujoin_finComRingType.
Canonical cjoin_finUnitRingType.
Canonical fcjoin_finUnitRingType.
Notation finComUnitRingType := FinRing.ComUnitRing.type.
Notation "[ 'finComUnitRingType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finComUnitRingType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End ComUnitRing.
Import ComUnitRing.Exports.

Module IntegralDomain.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.IntegralDomain.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := ComUnitRing.Class (mixin c).
Local Coercion base : class_of >-> GRing.IntegralDomain.class_of.
Local Coercion base2 : class_of >-> ComUnitRing.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.IntegralDomain.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition comRingType := @GRing.ComRing.Pack cT xclass xT.
Definition finComRingType := @ComRing.Pack cT xclass xT.
Definition unitRingType := @GRing.UnitRing.Pack cT xclass xT.
Definition finUnitRingType := @UnitRing.Pack cT xclass xT.
Definition comUnitRingType := @GRing.ComUnitRing.Pack cT xclass xT.
Definition finComUnitRingType := @ComUnitRing.Pack cT xclass xT.
Definition idomainType := @GRing.IntegralDomain.Pack cT xclass xT.

Definition join_finType := @Finite.Pack idomainType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack idomainType xclass xT.
Definition join_finRingType := @Ring.Pack idomainType xclass xT.
Definition join_finUnitRingType := @UnitRing.Pack idomainType xclass xT.
Definition join_finComRingType := @ComRing.Pack idomainType xclass xT.
Definition join_finComUnitRingType := @ComUnitRing.Pack idomainType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group idomainType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.IntegralDomain.class_of.
Coercion base2 : class_of >-> ComUnitRing.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion comRingType : type >-> GRing.ComRing.type.
Canonical comRingType.
Coercion finComRingType : type >-> ComRing.type.
Canonical finComRingType.
Coercion unitRingType : type >-> GRing.UnitRing.type.
Canonical unitRingType.
Coercion finUnitRingType : type >-> UnitRing.type.
Canonical finUnitRingType.
Coercion comUnitRingType : type >-> GRing.ComUnitRing.type.
Canonical comUnitRingType.
Coercion finComUnitRingType : type >-> ComUnitRing.type.
Canonical finComUnitRingType.
Coercion idomainType : type >-> GRing.IntegralDomain.type.
Canonical idomainType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finRingType.
Canonical join_finComRingType.
Canonical join_finUnitRingType.
Canonical join_finComUnitRingType.
Notation finIdomainType := FinRing.IntegralDomain.type.
Notation "[ 'finIdomainType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finIdomainType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End IntegralDomain.
Import IntegralDomain.Exports.

Module Field.

Section ClassDef.

Record class_of R :=
  Class { base : GRing.Field.class_of R; mixin : mixin_of R base }.
Definition base2 R (c : class_of R) := IntegralDomain.Class (mixin c).
Local Coercion base : class_of >-> GRing.Field.class_of.
Local Coercion base2 : class_of >-> IntegralDomain.class_of.

Structure type := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Definition pack := gen_pack Pack Class GRing.Field.class.
Variable cT : type.
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition comRingType := @GRing.ComRing.Pack cT xclass xT.
Definition finComRingType := @ComRing.Pack cT xclass xT.
Definition unitRingType := @GRing.UnitRing.Pack cT xclass xT.
Definition finUnitRingType := @UnitRing.Pack cT xclass xT.
Definition comUnitRingType := @GRing.ComUnitRing.Pack cT xclass xT.
Definition finComUnitRingType := @ComUnitRing.Pack cT xclass xT.
Definition idomainType := @GRing.IntegralDomain.Pack cT xclass xT.
Definition finIdomainType := @IntegralDomain.Pack cT xclass xT.
Definition fieldType := @GRing.Field.Pack cT xclass xT.

Definition join_finType := @Finite.Pack fieldType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack fieldType xclass xT.
Definition join_finRingType := @Ring.Pack fieldType xclass xT.
Definition join_finUnitRingType := @UnitRing.Pack fieldType xclass xT.
Definition join_finComRingType := @ComRing.Pack fieldType xclass xT.
Definition join_finComUnitRingType := @ComUnitRing.Pack fieldType xclass xT.
Definition join_finIdomainType := @IntegralDomain.Pack fieldType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group fieldType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.Field.class_of.
Coercion base2 : class_of >-> IntegralDomain.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion comRingType : type >-> GRing.ComRing.type.
Canonical comRingType.
Coercion finComRingType : type >-> ComRing.type.
Canonical finComRingType.
Coercion unitRingType : type >-> GRing.UnitRing.type.
Canonical unitRingType.
Coercion finUnitRingType : type >-> UnitRing.type.
Canonical finUnitRingType.
Coercion comUnitRingType : type >-> GRing.ComUnitRing.type.
Canonical comUnitRingType.
Coercion finComUnitRingType : type >-> ComUnitRing.type.
Canonical finComUnitRingType.
Coercion idomainType : type >-> GRing.IntegralDomain.type.
Canonical idomainType.
Coercion finIdomainType : type >-> IntegralDomain.type.
Canonical finIdomainType.
Coercion fieldType : type >-> GRing.Field.type.
Canonical fieldType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finRingType.
Canonical join_finComRingType.
Canonical join_finUnitRingType.
Canonical join_finComUnitRingType.
Canonical join_finIdomainType.
Notation finFieldType := FinRing.Field.type.
Notation "[ 'finFieldType' 'of' T ]" := (do_pack pack T)
  (at level 0, format "[ 'finFieldType'  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End Field.
Import Field.Exports.

Section DecideField.

Variable F : Field.type.

Fixpoint sat e f :=
  match f with
  | GRing.Bool b => b
  | t1 == t2 => (GRing.eval e t1 == GRing.eval e t2)%bool
  | GRing.Unit t => GRing.eval e t \is a GRing.unit
  | f1 /\ f2 => sat e f1 && sat e f2
  | f1 \/ f2 => sat e f1 || sat e f2
  | f1 ==> f2 => (sat e f1 ==> sat e f2)%bool
  | ~ f1 => ~~ sat e f1
  | ('exists 'X_k, f1) => [exists x : F, sat (set_nth 0%R e k x) f1]
  | ('forall 'X_k, f1) => [forall x : F, sat (set_nth 0%R e k x) f1]
  end%T.

Lemma decidable : GRing.DecidableField.axiom sat.
Proof.
move=> e f; elim: f e;
  try by move=> f1 IH1 f2 IH2 e /=; case IH1; case IH2; constructor; tauto.
- by move=> b e; apply: idP.
- by move=> t1 t2 e; apply: eqP.
- by move=> t e; apply: idP.
- by move=> f IH e /=; case: IH; constructor.
- by move=> i f IH e; apply: (iffP existsP) => [] [x fx]; exists x; apply/IH.
by move=> i f IH e; apply: (iffP forallP) => f_ x; apply/IH.
Qed.

Definition DecidableFieldMixin := DecFieldMixin decidable.

End DecideField.

Module DecField.

Section Joins.

Variable cT : Field.type.
Let xT := let: Field.Pack T _ _ := cT in T.
Let xclass : Field.class_of xT := Field.class cT.

Definition type := Eval hnf in DecFieldType cT (DecidableFieldMixin cT).
Definition finType := @Finite.Pack type (fin_ xclass) xT.
Definition finZmodType := @Zmodule.Pack type xclass xT.
Definition finRingType := @Ring.Pack type xclass xT.
Definition finUnitRingType := @UnitRing.Pack type xclass xT.
Definition finComRingType := @ComRing.Pack type xclass xT.
Definition finComUnitRingType := @ComUnitRing.Pack type xclass xT.
Definition finIdomainType := @IntegralDomain.Pack type xclass xT.
Definition baseFinGroupType := base_group type finZmodType finZmodType.
Definition finGroupType := fin_group baseFinGroupType cT.

End Joins.

Module Exports.
Coercion type : Field.type >-> GRing.DecidableField.type.
Canonical type.
Canonical finType.
Canonical finZmodType.
Canonical finRingType.
Canonical finUnitRingType.
Canonical finComRingType.
Canonical finComUnitRingType.
Canonical finIdomainType.
Canonical baseFinGroupType.
Canonical finGroupType.
End Exports.

End DecField.

Module Lmodule.

Section ClassDef.

Variable R : ringType.

Record class_of M :=
  Class { base : GRing.Lmodule.class_of R M; mixin : mixin_of M base }.
Definition base2 R (c : class_of R) := Zmodule.Class (mixin c).
Local Coercion base : class_of >-> GRing.Lmodule.class_of.
Local Coercion base2 : class_of >-> Zmodule.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (phR : phant R) (cT : type phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Definition pack := gen_pack (Pack phR) Class (@GRing.Lmodule.class R phR).
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition lmodType := @GRing.Lmodule.Pack R phR cT xclass xT.
Definition join_finType := @Finite.Pack lmodType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack lmodType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group lmodType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Import Exports.
Coercion base : class_of >-> GRing.Lmodule.class_of.
Coercion base2 : class_of >-> Zmodule.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion lmodType : type >-> GRing.Lmodule.type.
Canonical lmodType.
Canonical join_finType.
Canonical join_finZmodType.
Notation finLmodType R := (FinRing.Lmodule.type (Phant R)).
Notation "[ 'finLmodType' R 'of' T ]" := (do_pack (@pack _ (Phant R)) T)
  (at level 0, format "[ 'finLmodType'  R  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End Lmodule.
Import Lmodule.Exports.

Module Lalgebra.

Section ClassDef.

Variable R : ringType.

Record class_of M :=
  Class { base : GRing.Lalgebra.class_of R M; mixin : mixin_of M base }.
Definition base2 M (c : class_of M) := Ring.Class (mixin c).
Definition base3 M (c : class_of M) := @Lmodule.Class _ _ (base c) (mixin c).
Local Coercion base : class_of >-> GRing.Lalgebra.class_of.
Local Coercion base2 : class_of >-> Ring.class_of.
Local Coercion base3 : class_of >-> Lmodule.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (phR : phant R) (cT : type phR).
Definition pack := gen_pack (Pack phR) Class (@GRing.Lalgebra.class R phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition lmodType := @GRing.Lmodule.Pack R phR cT xclass xT.
Definition finLmodType := @Lmodule.Pack R phR cT xclass xT.
Definition lalgType := @GRing.Lalgebra.Pack R phR cT xclass xT.

Definition join_finType := @Finite.Pack lalgType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack lalgType xclass xT.
Definition join_finLmodType := @Lmodule.Pack R phR lalgType xclass xT.
Definition join_finRingType := @Ring.Pack lalgType xclass xT.
Definition rjoin_finLmodType := @Lmodule.Pack R phR ringType xclass xT.
Definition ljoin_finRingType := @Ring.Pack lmodType xclass xT.
Definition fljoin_finRingType := @Ring.Pack finLmodType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group lalgType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.Lalgebra.class_of.
Coercion base2 : class_of >-> Ring.class_of.
Coercion base3 : class_of >-> Lmodule.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion lmodType : type >-> GRing.Lmodule.type.
Canonical lmodType.
Coercion finLmodType : type >-> Lmodule.type.
Canonical finLmodType.
Coercion lalgType : type >-> GRing.Lalgebra.type.
Canonical lalgType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finLmodType.
Canonical join_finRingType.
Canonical rjoin_finLmodType.
Canonical ljoin_finRingType.
Canonical fljoin_finRingType.
Notation finLalgType R := (FinRing.Lalgebra.type (Phant R)).
Notation "[ 'finLalgType' R 'of' T ]" := (do_pack (@pack _ (Phant R)) T)
  (at level 0, format "[ 'finLalgType'  R  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End Lalgebra.
Import Lalgebra.Exports.

Module Algebra.

Section ClassDef.

Variable R : ringType.

Record class_of M :=
  Class { base : GRing.Algebra.class_of R M; mixin : mixin_of M base }.
Definition base2 M (c : class_of M) := Lalgebra.Class (mixin c).
Local Coercion base : class_of >-> GRing.Algebra.class_of.
Local Coercion base2 : class_of >->Lalgebra.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (phR : phant R) (cT : type phR).
Definition pack := gen_pack (Pack phR) Class (@GRing.Algebra.class R phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition lmodType := @GRing.Lmodule.Pack R phR cT xclass xT.
Definition finLmodType := @Lmodule.Pack R phR cT xclass xT.
Definition lalgType := @GRing.Lalgebra.Pack R phR cT xclass xT.
Definition finLalgType := @Lalgebra.Pack R phR cT xclass xT.
Definition algType := @GRing.Algebra.Pack R phR cT xclass xT.

Definition join_finType := @Finite.Pack algType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack algType xclass xT.
Definition join_finRingType := @Ring.Pack algType xclass xT.
Definition join_finLmodType := @Lmodule.Pack R phR algType xclass xT.
Definition join_finLalgType := @Lalgebra.Pack R phR algType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group algType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.Algebra.class_of.
Coercion base2 : class_of >-> Lalgebra.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion lmodType : type >-> GRing.Lmodule.type.
Canonical lmodType.
Coercion finLmodType : type >-> Lmodule.type.
Canonical finLmodType.
Coercion lalgType : type >-> GRing.Lalgebra.type.
Canonical lalgType.
Coercion finLalgType : type >-> Lalgebra.type.
Canonical finLalgType.
Coercion algType : type >-> GRing.Algebra.type.
Canonical algType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finLmodType.
Canonical join_finRingType.
Canonical join_finLalgType.
Notation finAlgType R := (type (Phant R)).
Notation "[ 'finAlgType' R 'of' T ]" := (do_pack (@pack _ (Phant R)) T)
  (at level 0, format "[ 'finAlgType'  R  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End Algebra.
Import Algebra.Exports.

Module UnitAlgebra.

Section ClassDef.

Variable R : unitRingType.

Record class_of M :=
  Class { base : GRing.UnitAlgebra.class_of R M; mixin : mixin_of M base }.
Definition base2 M (c : class_of M) := Algebra.Class (mixin c).
Definition base3 M (c : class_of M) := @UnitRing.Class _ (base c) (mixin c).

Local Coercion base : class_of >-> GRing.UnitAlgebra.class_of.
Local Coercion base2 : class_of >-> Algebra.class_of.
Local Coercion base3 : class_of >-> UnitRing.class_of.

Structure type (phR : phant R) := Pack {sort; _ : class_of sort; _ : Type}.
Local Coercion sort : type >-> Sortclass.
Variables (phR : phant R) (cT : type phR).
Definition pack := gen_pack (Pack phR) Class (@GRing.UnitAlgebra.class R phR).
Definition class := let: Pack _ c _ as cT' := cT return class_of cT' in c.
Let xT := let: Pack T _ _ := cT in T.
Notation xclass := (class : class_of xT).

Definition eqType := @Equality.Pack cT xclass xT.
Definition choiceType := @Choice.Pack cT xclass xT.
Definition countType := @Countable.Pack cT (fin_ xclass) xT.
Definition finType := @Finite.Pack cT (fin_ xclass) xT.
Definition zmodType := @GRing.Zmodule.Pack cT xclass xT.
Definition finZmodType := @Zmodule.Pack cT xclass xT.
Definition ringType := @GRing.Ring.Pack cT xclass xT.
Definition finRingType := @Ring.Pack cT xclass xT.
Definition unitRingType := @GRing.UnitRing.Pack cT xclass xT.
Definition finUnitRingType := @UnitRing.Pack cT xclass xT.
Definition lmodType := @GRing.Lmodule.Pack R phR cT xclass xT.
Definition finLmodType := @Lmodule.Pack R phR cT xclass xT.
Definition lalgType := @GRing.Lalgebra.Pack R phR cT xclass xT.
Definition finLalgType := @Lalgebra.Pack R phR cT xclass xT.
Definition algType := @GRing.Algebra.Pack R phR cT xclass xT.
Definition finAlgType := @Algebra.Pack R phR cT xclass xT.
Definition unitAlgType := @GRing.UnitAlgebra.Pack R phR cT xclass xT.

Definition join_finType := @Finite.Pack unitAlgType (fin_ xclass) xT.
Definition join_finZmodType := @Zmodule.Pack unitAlgType xclass xT.
Definition join_finRingType := @Ring.Pack unitAlgType xclass xT.
Definition join_finUnitRingType := @UnitRing.Pack unitAlgType xclass xT.
Definition join_finLmodType := @Lmodule.Pack R phR unitAlgType xclass xT.
Definition join_finLalgType := @Lalgebra.Pack R phR unitAlgType xclass xT.
Definition join_finAlgType := @Algebra.Pack R phR unitAlgType xclass xT.
Definition  ljoin_finUnitRingType := @UnitRing.Pack lmodType xclass xT.
Definition fljoin_finUnitRingType := @UnitRing.Pack finLmodType xclass xT.
Definition  njoin_finUnitRingType := @UnitRing.Pack lalgType xclass xT.
Definition fnjoin_finUnitRingType := @UnitRing.Pack finLalgType xclass xT.
Definition  ajoin_finUnitRingType := @UnitRing.Pack algType xclass xT.
Definition fajoin_finUnitRingType := @UnitRing.Pack finAlgType xclass xT.
Definition ujoin_finLmodType := @Lmodule.Pack R phR unitRingType xclass xT.
Definition ujoin_finLalgType := @Lalgebra.Pack R phR unitRingType xclass xT.
Definition ujoin_finAlgType := @Algebra.Pack R phR unitRingType xclass xT.

Definition baseFinGroupType := base_group cT zmodType finType.
Definition finGroupType := fin_group baseFinGroupType zmodType.
Definition join_baseFinGroupType := base_group unitAlgType zmodType finType.
Definition join_finGroupType := fin_group join_baseFinGroupType zmodType.

End ClassDef.

Module Exports.
Coercion base : class_of >-> GRing.UnitAlgebra.class_of.
Coercion base2 : class_of >-> Algebra.class_of.
Coercion base3 : class_of >-> UnitRing.class_of.
Coercion sort : type >-> Sortclass.
Bind Scope ring_scope with sort.
Coercion eqType : type >-> Equality.type.
Canonical eqType.
Coercion choiceType : type >-> Choice.type.
Canonical choiceType.
Coercion countType : type >-> Countable.type.
Canonical countType.
Coercion finType : type >-> Finite.type.
Canonical finType.
Coercion zmodType : type >-> GRing.Zmodule.type.
Canonical zmodType.
Coercion finZmodType : type >-> Zmodule.type.
Canonical finZmodType.
Coercion ringType : type >-> GRing.Ring.type.
Canonical ringType.
Coercion finRingType : type >-> Ring.type.
Canonical finRingType.
Coercion unitRingType : type >-> GRing.UnitRing.type.
Canonical unitRingType.
Coercion finUnitRingType : type >-> UnitRing.type.
Canonical finUnitRingType.
Coercion lmodType : type >-> GRing.Lmodule.type.
Canonical lmodType.
Coercion finLmodType : type >-> Lmodule.type.
Canonical finLmodType.
Coercion lalgType : type >-> GRing.Lalgebra.type.
Canonical lalgType.
Coercion finLalgType : type >-> Lalgebra.type.
Canonical finLalgType.
Coercion algType : type >-> GRing.Algebra.type.
Canonical algType.
Coercion finAlgType : type >-> Algebra.type.
Canonical finAlgType.
Coercion unitAlgType : type >-> GRing.UnitAlgebra.type.
Canonical unitAlgType.
Canonical join_finType.
Canonical join_finZmodType.
Canonical join_finLmodType.
Canonical join_finRingType.
Canonical join_finLalgType.
Canonical join_finAlgType.
Canonical ljoin_finUnitRingType.
Canonical fljoin_finUnitRingType.
Canonical njoin_finUnitRingType.
Canonical fnjoin_finUnitRingType.
Canonical ajoin_finUnitRingType.
Canonical fajoin_finUnitRingType.
Canonical ujoin_finLmodType.
Canonical ujoin_finLalgType.
Canonical ujoin_finAlgType.
Notation finUnitAlgType R := (type (Phant R)).
Notation "[ 'finUnitAlgType' R 'of' T ]" := (do_pack (@pack _ (Phant R)) T)
  (at level 0, format "[ 'finUnitAlgType'  R  'of'  T ]") : form_scope.
Canonical baseFinGroupType.
Canonical finGroupType.
Canonical join_baseFinGroupType.
Canonical join_finGroupType.
End Exports.

End UnitAlgebra.
Import UnitAlgebra.Exports.

Module Theory.

Definition zmod1gE := zmod1gE.
Definition zmodVgE := zmodVgE.
Definition zmodMgE := zmodMgE.
Definition zmodXgE := zmodXgE.
Definition zmod_mulgC := zmod_mulgC.
Definition zmod_abelian := zmod_abelian.
Definition val_unit1 := val_unit1.
Definition val_unitM := val_unitM.
Definition val_unitX := val_unitX.
Definition val_unitV := val_unitV.
Definition unit_actE := unit_actE.

End Theory.

End FinRing.

Import FinRing.
Export Zmodule.Exports Ring.Exports ComRing.Exports.
Export UnitRing.Exports UnitsGroupExports ComUnitRing.Exports.
Export IntegralDomain.Exports Field.Exports DecField.Exports.
Export Lmodule.Exports Lalgebra.Exports Algebra.Exports UnitAlgebra.Exports.

Notation "{ 'unit' R }" := (unit_of (Phant R))
  (at level 0, format "{ 'unit'  R }") : type_scope.
Prenex Implicits FinRing.uval.
Notation "''U'" := (unit_action _) (at level 8) : action_scope.
Notation "''U'" := (unit_groupAction _) (at level 8) : groupAction_scope.


(**
    Authors: Steve Zdancewic and Karl Mazurak and Jianzhou Zhao.
*)

Require Export Metatheory.

(* ********************************************************************** *)
(** * ## Syntax (pre-terms) *)

Inductive typ : Set :=
  | typ_bvar : nat -> typ
  | typ_fvar : atom -> typ
  | typ_arrow : typ -> typ -> typ
  | typ_all : typ -> typ
  | typ_bang : typ -> typ
  | typ_with : typ -> typ -> typ
.

Inductive exp : Set :=
  | exp_bvar : nat -> exp
  | exp_fvar : atom -> exp
  | exp_abs : typ -> exp -> exp
  | exp_app : exp -> exp -> exp
  | exp_tabs : exp -> exp
  | exp_tapp : exp -> typ -> exp
  | exp_bang : exp -> exp
  | exp_let : exp -> exp -> exp
  | exp_apair : exp -> exp -> exp
  | exp_fst : exp -> exp
  | exp_snd : exp -> exp
.

Coercion typ_bvar : nat >-> typ.
Coercion typ_fvar : atom >-> typ.
Coercion exp_bvar : nat >-> exp.
Coercion exp_fvar : atom >-> exp.

(* ********************************************************************** *)
(** * ## Opening terms *)

Fixpoint open_tt_rec (K : nat) (U : typ) (T : typ)  {struct T} : typ :=
  match T with
  | typ_bvar J => if K == J then U else (typ_bvar J)
  | typ_fvar X => typ_fvar X
  | typ_arrow T1 T2 => typ_arrow (open_tt_rec K U T1) (open_tt_rec K U T2)
  | typ_all T1 => typ_all (open_tt_rec (S K) U T1)
  | typ_bang T1 => typ_bang (open_tt_rec K U T1)
  | typ_with T1 T2 => typ_with (open_tt_rec K U T1) (open_tt_rec K U T2)
  end.

Fixpoint open_te_rec (K : nat) (U : typ) (e : exp) {struct e} : exp :=
  match e with
  | exp_bvar i => exp_bvar i
  | exp_fvar x => exp_fvar x
  | exp_abs V e1 => exp_abs (open_tt_rec K U V)  (open_te_rec K U e1)
  | exp_app e1 e2 => exp_app (open_te_rec K U e1) (open_te_rec K U e2)
  | exp_tabs e1 => exp_tabs (open_te_rec (S K) U e1)
  | exp_tapp e1 V => exp_tapp (open_te_rec K U e1) (open_tt_rec K U V)
  | exp_bang e1 => exp_bang  (open_te_rec K U e1)
  | exp_let e1 e2 => exp_let  (open_te_rec K U e1) (open_te_rec K U e2)
  | exp_apair e1 e2 => exp_apair  (open_te_rec K U e1) (open_te_rec K U e2)
  | exp_fst e1 => exp_fst  (open_te_rec K U e1)
  | exp_snd e1 => exp_snd  (open_te_rec K U e1)
  end.

Fixpoint open_ee_rec (k : nat) (f : exp) (e : exp)  {struct e} : exp :=
  match e with
  | exp_bvar i => if k == i then f else (exp_bvar i)
  | exp_fvar x => exp_fvar x
  | exp_abs V e1 => exp_abs V (open_ee_rec (S k) f e1)
  | exp_app e1 e2 => exp_app (open_ee_rec k f e1) (open_ee_rec k f e2)
  | exp_tabs e1 => exp_tabs (open_ee_rec k f e1)
  | exp_tapp e1 V => exp_tapp (open_ee_rec k f e1) V
  | exp_bang e1 => exp_bang  (open_ee_rec k f e1)
  | exp_let e1 e2 => exp_let  (open_ee_rec k f e1) (open_ee_rec (S k) f e2)
  | exp_apair e1 e2 => exp_apair  (open_ee_rec k f e1) (open_ee_rec k f e2)
  | exp_fst e1 => exp_fst  (open_ee_rec k f e1)
  | exp_snd e1 => exp_snd  (open_ee_rec k f e1)
  end.

Definition open_tt T U := open_tt_rec 0 U T.
Definition open_te e U := open_te_rec 0 U e.
Definition open_ee e1 e2 := open_ee_rec 0 e2 e1.

(* ********************************************************************** *)
(** * ## Local closure *)

Inductive type : typ -> Prop :=
  | type_var : forall X,
      type (typ_fvar X)
  | type_arrow : forall T1 T2,
      type T1 ->
      type T2 ->
      type (typ_arrow T1 T2)
  | type_all : forall L T1,
      (forall X : atom, X `notin` L -> type (open_tt T1 X)) ->
      type (typ_all T1)
  | type_bang : forall T1,
      type T1 ->
      type (typ_bang T1)
  | type_with : forall T1 T2,
      type T1 ->
      type T2 ->
      type (typ_with T1 T2)
.

Inductive expr : exp -> Prop :=
  | expr_var : forall x,
      expr (exp_fvar x)
  | expr_abs : forall L T e1,
      type T ->
      (forall x : atom, x `notin` L -> expr (open_ee e1 x)) ->
      expr (exp_abs T e1)
  | expr_app : forall e1 e2,
      expr e1 ->
      expr e2 ->
      expr (exp_app e1 e2)
  | expr_tabs : forall L e1,
      (forall X : atom, X `notin` L -> expr (open_te e1 X)) ->
      expr (exp_tabs e1)
  | expr_tapp : forall e1 V,
      expr e1 ->
      type V ->
      expr (exp_tapp e1 V)
  | expr_bang : forall e1,
      expr e1 ->
      expr (exp_bang e1)
  | expr_let : forall L e1 e2,
      expr e1 ->
      (forall x : atom, x `notin` L -> expr (open_ee e2 x)) ->
      expr (exp_let e1 e2)
  | expr_apair : forall e1 e2,
      expr e1 ->
      expr e2 ->
      expr (exp_apair e1 e2)
  | expr_fst : forall e1,
      expr e1 ->
      expr (exp_fst e1)
  | expr_snd : forall e1,
      expr e1 ->
      expr (exp_snd e1)
.

(* ********************************************************************** *)
(** * ## Environments *)

Inductive binding : Set :=
  | bind_kn :  binding
  | bind_typ : typ -> binding.

Notation env := (list (atom * binding)).
Notation empty := (@nil (atom * binding)).

Inductive lbinding : Set :=
  | lbind_typ : typ -> lbinding.

Notation lenv := (list (atom * lbinding)).
Notation lempty := (@nil (atom * lbinding)).

(* ********************************************************************** *)
(** * ## Well-formedness *)

Inductive wf_typ : env -> typ -> Prop :=
  | wf_typ_var : forall E (X : atom),
      uniq E ->
      binds X bind_kn E ->
      wf_typ E (typ_fvar X)
  | wf_typ_arrow : forall E T1 T2,
      wf_typ E T1 ->
      wf_typ E T2 ->
      wf_typ E (typ_arrow T1 T2)
  | wf_typ_all : forall L E T1,
      (forall X : atom, X `notin` L ->
        wf_typ ([(X, bind_kn)] ++ E) (open_tt T1 X)) ->
      wf_typ E (typ_all T1)
  | wf_typ_bang : forall E T1,
      wf_typ E T1 ->
      wf_typ E (typ_bang T1)
  | wf_typ_with : forall E T1 T2,
      wf_typ E T1 ->
      wf_typ E T2 ->
      wf_typ E (typ_with T1 T2)
  .

Inductive wf_env : env -> Prop :=
  | wf_env_empty :
      wf_env empty
  | wf_env_kn : forall (E : env) (X : atom),
      wf_env E ->
      X `notin` dom E ->
      wf_env ([(X, bind_kn)] ++ E)
  | wf_env_typ : forall (E : env) (x : atom) (T : typ),
      wf_env E ->
      wf_typ E T ->
      x `notin` dom E ->
      wf_env ([(x, bind_typ T)] ++ E).

Inductive wf_lenv : env -> lenv -> Prop :=
  | wf_lenv_empty :  forall (E:env),
      wf_env E ->
      wf_lenv E lempty
  | wf_lenv_typ : forall (E:env) (D:lenv) (x:atom) (T:typ),
      wf_lenv E D ->
      x `notin` dom E ->
      x `notin` dom D ->
      wf_typ E T ->
      wf_lenv E ([(x, lbind_typ T)] ++ D).

(* ********************************************************************** *)
(** * ## Linear Context Splitting *)

Inductive lenv_split : env -> lenv -> lenv -> lenv -> Prop :=
  | lenv_split_empty: forall E,
       wf_env E ->
       lenv_split E lempty lempty lempty
  | lenv_split_left: forall E D1 D2 D3 x T,
       lenv_split E D1 D2 D3 ->
       x `notin` dom E ->
       x `notin` dom D3 ->
       wf_typ E T ->
       lenv_split E ([(x, lbind_typ T)]++D1) D2 ([(x, lbind_typ T)]++D3)
  | lenv_split_right: forall E D1 D2 D3 x T,
       lenv_split E D1 D2 D3 ->
       x `notin` dom E ->
       x `notin` dom D3 ->
       wf_typ E T ->
       lenv_split E D1 ([(x, lbind_typ T)]++D2) ([(x, lbind_typ T)]++D3).

(* ********************************************************************** *)
(** * ## Values *)

Inductive value : exp -> Prop :=
  | value_abs : forall T e1,
      expr (exp_abs T e1) ->
      value (exp_abs T e1)
  | value_tabs : forall e1,
      expr (exp_tabs e1) ->
      value (exp_tabs e1)
  | value_bang : forall e1,
      expr e1 ->
      value (exp_bang e1)
  | value_apair : forall e1 e2,
      expr e1 ->
      expr e2 ->
      value (exp_apair e1 e2)
.

(* ********************************************************************** *)
(** * ## Typing *)

Inductive typing : env -> lenv -> exp -> typ -> Prop :=
  | typing_var : forall E x T,
      wf_env E ->
      binds x (bind_typ T) E ->
      typing E lempty (exp_fvar x) T
  | typing_lvar : forall E x T,
      wf_lenv E [(x, lbind_typ T)] ->
      typing E [(x, lbind_typ T)] (exp_fvar x) T
  | typing_abs : forall L E D T1 e1 T2,
      wf_typ E T1->
      (forall x : atom, x `notin` L ->
        typing E ([(x, lbind_typ T1)] ++ D) (open_ee e1 x) T2) ->
      typing E D (exp_abs T1 e1) (typ_arrow T1 T2)
  | typing_app : forall T1 E D1 D2 D3 e1 e2 T2,
      typing E D1 e1 (typ_arrow T1 T2) ->
      typing E D2 e2 T1 ->
      lenv_split E D1 D2 D3 ->
      typing E D3 (exp_app e1 e2) T2
  | typing_tabs : forall L E D e1 T1,
      (forall X : atom, X `notin` L ->
        typing ([(X, bind_kn)] ++ E) D (open_te e1 X) (open_tt T1 X)) ->
      typing E D (exp_tabs e1) (typ_all T1)
  | typing_tapp : forall E D e1 T T1,
      typing E D e1 (typ_all T1) ->
      wf_typ E T ->
      typing E D (exp_tapp e1 T) (open_tt T1 T)
  | typing_bang : forall E e T,
      typing E lempty e T ->
      typing E lempty (exp_bang e) (typ_bang T)
  | typing_let : forall L E D1 D2 D3 T1 e1 e2 T2,
      typing E D1 e1 (typ_bang T1) ->
      (forall x : atom, x `notin` L ->
        typing ([(x, bind_typ T1)] ++ E) D2 (open_ee e2 x) T2) ->
      lenv_split E D1 D2 D3 ->
      typing E D3 (exp_let e1 e2) T2
  | typing_apair : forall E D e1 e2 T1 T2,
      typing E D e1 T1 ->
      typing E D e2 T2 ->
      typing E D (exp_apair e1 e2) (typ_with T1 T2)
  | typing_fst : forall E D e T1 T2,
      typing E D e (typ_with T1 T2) ->
      typing E D (exp_fst e) T1
  | typing_snd : forall E D e T1 T2,
      typing E D e (typ_with T1 T2) ->
      typing E D (exp_snd e) T2
.

(* ********************************************************************** *)
(** * ## Reduction *)

Inductive red : exp -> exp -> Prop :=
  | red_app : forall e1 e1' e2,
      expr e2 ->
      red e1 e1' ->
      red (exp_app e1 e2) (exp_app e1' e2)
  | red_tapp : forall e1 e1' V,
      type V ->
      red e1 e1' ->
      red (exp_tapp e1 V) (exp_tapp e1' V)
  | red_abs : forall T e1 e2,
      expr (exp_abs T e1) ->
      expr e2 ->
      red (exp_app (exp_abs T e1) e2) (open_ee e1 e2)
  | red_tabs : forall e1 T,
      expr (exp_tabs e1) ->
      type T ->
      red (exp_tapp (exp_tabs e1) T) (open_te e1 T)
  | red_let_cong : forall e1 e1' e2,
      expr (exp_let e1 e2) ->
      red e1 e1' ->
      red (exp_let e1 e2) (exp_let e1' e2)
  | red_let_beta : forall e1 e2,
      expr (exp_let (exp_bang e1) e2) ->
      red (exp_let (exp_bang e1) e2) (open_ee e2 e1)
  | red_fst_cong : forall e e',
      red e e' ->
      red (exp_fst e) (exp_fst e')
  | red_fst_beta : forall e1 e2,
      expr e1 ->
      expr e2 ->
      red (exp_fst (exp_apair e1 e2)) e1
  | red_snd_cong : forall e e',
      red e e' ->
      red (exp_snd e) (exp_snd e')
  | red_snd_beta : forall e1 e2,
      expr e1 ->
      expr e2 ->
      red (exp_snd (exp_apair e1 e2)) e2
.

(* ********************************************************************** *)
(** * ## Automation *)

(** We declare most constructors as [Hint]s to be used by the [auto]
    and [eauto] tactics.  We exclude constructors from the typing
    relations that use cofinite quantification.  It is unlikely that
    [eauto] will find an instantiation for the finite set [L], and in
    those cases, [eauto] can take some time to fail. (A priori, this
    is not obvious.  In practice, one adds as hints all constructors
    and then later removes some constructors when they cause proof
    search to take too long.) *)

Hint Constructors type expr wf_typ wf_env wf_lenv value red lenv_split.
Hint Resolve typing_var typing_lvar typing_app typing_tapp typing_bang typing_apair typing_fst typing_snd.

(* ********************************************************************** *)
(** * ## Cases Tactic *)

Tactic Notation "typ_cases" tactic(first) tactic(c) :=
  first;
  [ c "typ_bvar" |  c "typ_fvar" |
    c "typ_arrow" | c "typ_all" | c "typ_bang" | c "typ_with" ].

Tactic Notation "exp_cases" tactic(first) tactic(c) :=
  first;
  [ c "exp_bvar" |  c "exp_fvar" |
    c "exp_abs" | c "exp_app" |
    c "exp_tabs" | c "exp_tapp" |
    c "exp_bang" | c "exp_let" |
    c "exp_apair" | c "exp_fst" | c "exp_snd" ].

Tactic Notation "type_cases" tactic(first) tactic(c) :=
  first;
  [ c "type_var" | c "type_arrow" | c "type_all" | c "type_bang" | c "type_with" ].

Tactic Notation "expr_cases" tactic(first) tactic(c) :=
  first;
  [ c "expr_var" |
    c "expr_abs" | c "expr_app" |
    c "expr_tabs" | c "expr_tapp" |
    c "expr_bang" | c "expr_let" |
    c "expr_apair" | c "expr_fst" | c "expr_snd" ].

Tactic Notation "wf_typ_cases" tactic(first) tactic(c) :=
  first;
  [ c "wf_typ_var" | c "wf_typ_arrow" | c "wf_typ_all" | c "wf_typ_bang" | c "wf_typ_with" ].

Tactic Notation "wf_env_cases" tactic(first) tactic(c) :=
  first;
  [ c "wf_env_empty" | c "wf_env_kn" | c "wf_env_typ" ].

Tactic Notation "wf_lenv_cases" tactic(first) tactic(c) :=
  first;
  [ c "wf_lenv_empty" | c "wf_lenv_typ" ].

Tactic Notation "lenv_split_cases" tactic(first) tactic(c) :=
  first;
  [ c "lenv_split_empty" | c "lenv_split_left" | c "lenv_split_right" ].

Tactic Notation "value_cases" tactic(first) tactic(c) :=
  first;
  [ c "value_abs" | c "value_tabs" | c "value_bang"| c "value_apair" ].

Tactic Notation "red_cases" tactic(first) tactic(c) :=
  first;
  [ c "red_app" |
    c "red_tapp" | c "red_abs" | c "red_tabs" |
    c "red_let_cong" | c "red_let_beta" |
    c "red_fst_cong" | c "red_fst_beta" |
    c "red_snd_cong" | c "red_snd_beta" ].

Tactic Notation "typing_cases" tactic(first) tactic(c) :=
  first;
  [ c "typing_var" | c "typing_lvar" |
    c "typing_abs" | c "typing_app" |
    c "typing_tabs" | c "typing_tapp" |
    c "typing_bang" | c "typing_let" |
    c "typing_apair" | c "typing_fst" | c "typing_snd" ].


Require Export SystemFR.AnnotatedTactics.
Require Export SystemFR.Judgments.
Require Export SystemFR.ErasedEquivalentElim.

Lemma annotated_equivalent_elim:
  forall Θ Γ t1 t2 T,
    wf T 0 ->
    subset (fv T) (support Γ) ->
    [[ Θ; Γ ⊨ t1 ≡ t2 ]] ->
    [[ Θ; Γ ⊨ t1: T ]] ->
    [[ Θ; Γ ⊨ t2: T ]].
Proof.
  intros; eapply open_reducible_equivalent_elim;
    steps;
    side_conditions.
Qed.


Theorem split_then_solve : (True /\ True) /\ True.
Proof.
  split.
  + split.
    - exact I.
    - exact I.
  + exact I.
Qed.