Theory NatTrans

(*
Author: Alexander Katovsky
*)

section "Natural Transformation"

theory NatTrans
imports Functors
begin

record ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans = 
  NTDom :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) Functor" 
  NTCod :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) Functor" 
  NatTransMap :: "'o1  'm2"

abbreviation
  NatTransApp :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans  'o1  'm2" (infixr "$$" 70) where
  "NatTransApp η X  (NatTransMap η) X"

definition  "NTCatDom η  CatDom (NTDom η)"
definition  "NTCatCod η  CatCod (NTCod η)"

locale NatTransExt = 
  fixes η :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" (structure)
  assumes  NTExt : "NatTransMap η  extensional (Obj (NTCatDom η))"

locale NatTransP = 
  fixes η :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" (structure)
  assumes NatTransFtor:   "Functor (NTDom η)"
  and     NatTransFtor2:  "Functor (NTCod η)"
  and     NatTransFtorDom:   "NTCatDom η = CatDom (NTCod η)"
  and     NatTransFtorCod:   "NTCatCod η = CatCod (NTDom η)"
  and    NatTransMapsTo:  "X  objNTCatDom η 
                           (η $$ X) mapsNTCatCod η((NTDom η) @@ X) to ((NTCod η) @@ X)"
  and    NatTrans:  "f mapsNTCatDom ηX to Y  
                     ((NTDom η) ## f) ;;NTCatCod η(η $$ Y) = (η $$ X) ;;NTCatCod η((NTCod η) ## f)"

locale NatTrans = NatTransP + NatTransExt

lemma [simp]: "NatTrans η  NatTransP η"
by(simp add: NatTrans_def)

definition MakeNT :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans  ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" where
"MakeNT η  
      NTDom = NTDom η , 
      NTCod = NTCod η , 
      NatTransMap = restrict (NatTransMap η) (Obj (NTCatDom η))
  "

definition 
  nt_abbrev ("NT _ : _  _" [81]) where
  "NT f : F  G  (NatTrans f)  (NTDom f = F)  (NTCod f = G)"

lemma nt_abbrevE[elim]: "NT f : F  G ; (NatTrans f) ; (NTDom f = F) ; (NTCod f = G)  R  R"
by (auto simp add: nt_abbrev_def)

lemma MakeNT: "NatTransP η  NatTrans (MakeNT η)"
  by(auto simp add: NatTransP_def NatTrans_def MakeNT_def NTCatDom_def NTCatCod_def Category.MapsToObj
    NatTransExt_def)

lemma MakeNT_comp: "X  Obj (NTCatDom f)  (MakeNT f) $$ X = f $$ X"
by (simp add: MakeNT_def)

lemma MakeNT_dom: "NTCatDom f = NTCatDom (MakeNT f)"
by (simp add: NTCatDom_def MakeNT_def)

lemma MakeNT_cod: "NTCatCod f = NTCatCod (MakeNT f)"
by (simp add: NTCatCod_def MakeNT_def)

lemma MakeNTApp: "X  Obj (NTCatDom (MakeNT f))  f $$ X = (MakeNT f) $$ X"
by(simp add: MakeNT_def NTCatDom_def)

lemma NatTransMapsTo:
  assumes "NT η : F  G" and "X  Obj (CatDom F)" 
  shows "η $$ X mapsCatCod G(F @@ X) to (G @@ X)"
proof-
  have NTP: "NatTransP η" using assms by auto
  have NTC: "NTCatCod η = CatCod G" using assms by (auto simp add: NTCatCod_def)
  have NTD: "NTCatDom η = CatDom F" using assms by (auto simp add: NTCatDom_def)
  hence Obj: "X  Obj (NTCatDom η)" using assms by simp
  have DF: "NTDom η = F" and CG: "NTCod η = G" using assms by auto
  have NTmapsTo: "η $$ X mapsNTCatCod η((NTDom η) @@ X) to ((NTCod η) @@ X)"
    using NTP Obj by (simp add: NatTransP.NatTransMapsTo)
  thus ?thesis using NTC NTD DF CG by simp
qed

definition  
   NTCompDefined :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans 
                       ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans  bool" (infixl "≈>∙" 65) where
  "NTCompDefined η1 η2  NatTrans η1  NatTrans η2  NTCatDom η2 = NTCatDom η1  
                         NTCatCod η2 = NTCatCod η1  NTCod η1 = NTDom η2"

lemma NTCompDefinedE[elim]: "η1 ≈>∙ η2 ; NatTrans η1 ; NatTrans η2 ; NTCatDom η2 = NTCatDom η1 ; 
                         NTCatCod η2 = NTCatCod η1 ; NTCod η1 = NTDom η2  R  R"
by (simp add: NTCompDefined_def)

lemma NTCompDefinedI: "NatTrans η1 ; NatTrans η2 ; NTCatDom η2 = NTCatDom η1 ; 
                         NTCatCod η2 = NTCatCod η1 ; NTCod η1 = NTDom η2  η1 ≈>∙ η2"
by (simp add: NTCompDefined_def)

lemma NatTransExt0:
  assumes "NTDom η1 = NTDom η2" and "NTCod η1 = NTCod η2"
  and     "X . X  Obj (NTCatDom η1)  η1 $$ X = η2 $$ X"
  and     "NatTransMap η1  extensional (Obj (NTCatDom η1))"
  and     "NatTransMap η2  extensional (Obj (NTCatDom η2))"
  shows   "η1 = η2"
proof-
  have "NatTransMap η1 = NatTransMap η2"
  proof(rule extensionalityI [of "NatTransMap η1" "Obj (NTCatDom η1)"])
    show "NatTransMap η1  extensional (Obj (NTCatDom η1))" using assms by simp
    have "NTCatDom η1 = NTCatDom η2" using assms by (simp add: NTCatDom_def)
    moreover have "NatTransMap η2  extensional (Obj (NTCatDom η2))" using assms by simp
    ultimately show "NatTransMap η2  extensional (Obj (NTCatDom η1))" by simp
    {fix X assume "X  Obj (NTCatDom η1)" thus "η1 $$ X = η2 $$ X" using assms by simp}
  qed
  thus ?thesis using assms by (simp)
qed

lemma NatTransExt':
  assumes "NTDom η1' = NTDom η2'" and "NTCod η1' = NTCod η2'"
  and     "X . X  Obj (NTCatDom η1')  η1' $$ X = η2' $$ X"
  shows   "MakeNT η1' = MakeNT η2'"
proof(rule NatTransExt0)
  show "NatTransMap (MakeNT η1')  extensional (Obj (NTCatDom (MakeNT η1')))" and
    "NatTransMap (MakeNT η2')  extensional (Obj (NTCatDom (MakeNT η2')))" using assms
    by(simp add: MakeNT_def NTCatDom_def NTCatCod_def NatTransExt_def)+
  show "NTDom (MakeNT η1') = NTDom (MakeNT η2')" and 
    "NTCod (MakeNT η1') = NTCod (MakeNT η2')" using assms by (simp add: MakeNT_def)+
  {
    fix X assume 1: "X  Obj (NTCatDom (MakeNT η1'))" 
    show "(MakeNT η1') $$ X = (MakeNT η2') $$ X"
    proof-
      have "NTCatDom (MakeNT η1') = NTCatDom (MakeNT η2')" using assms by(simp add: NTCatDom_def MakeNT_def)
      hence 2: "X  Obj (NTCatDom (MakeNT η2'))" using 1 by simp 
      have "(NTCatDom η1') = (NTCatDom (MakeNT η1'))" by (rule MakeNT_dom)
      hence "X  Obj (NTCatDom η1')" using 1 assms by simp 
      hence "η1' $$ X = η2' $$ X" using assms by simp
      moreover have "η1' $$ X = (MakeNT η1') $$ X" using 1 assms by (simp add: MakeNTApp)
      moreover have "η2' $$ X = (MakeNT η2') $$ X" using 2 assms by (simp add: MakeNTApp)
      ultimately have "(MakeNT η1') $$ X = (MakeNT η2') $$ X" by simp
      thus ?thesis using assms by simp
    qed
  }
qed

lemma NatTransExt:
  assumes "NatTrans η1" and "NatTrans η2" and "NTDom η1 = NTDom η2" and "NTCod η1 = NTCod η2"
  and     "X . X  Obj (NTCatDom η1)  η1 $$ X = η2 $$ X"
  shows   "η1 = η2"
proof-
  have "NatTransMap η1  extensional (Obj (NTCatDom η1))" and
       "NatTransMap η2  extensional (Obj (NTCatDom η2))" using assms
  by(simp only: NatTransExt_def NatTrans_def)+
  thus ?thesis using assms by (simp add: NatTransExt0)
qed

definition
  IdNatTrans' :: "('o1, 'o2, 'm1, 'm2, 'a1, 'a2) Functor  ('o1, 'o2, 'm1, 'm2, 'a1, 'a2) NatTrans" where
  "IdNatTrans' F  
      NTDom = F , 
      NTCod = F , 
      NatTransMap = λ X . idCatCod F(F @@ X)
  "

definition "IdNatTrans F  MakeNT(IdNatTrans' F)"

lemma IdNatTrans_map: "X  objCatDom F (IdNatTrans F) $$ X = idCatCod F(F @@ X)"
by(auto simp add: IdNatTrans_def IdNatTrans'_def MakeNT_comp MakeNT_def NTCatDom_def)

lemmas IdNatTrans_defs = IdNatTrans_def IdNatTrans'_def MakeNT_def IdNatTrans_map NTCatCod_def NTCatDom_def

lemma IdNatTransNatTrans': "Functor F  NatTransP(IdNatTrans' F)"
proof(auto simp add:NatTransP_def IdNatTrans'_def NTCatDom_def NTCatCod_def Category.Simps 
        PreFunctor.FunctorId2 functor_simps Functor.FunctorMapsTo)
  {
    fix f X Y
    assume a: "Functor F" and b: "f mapsCatDom FX to Y"
    show "(F ## f) ;;CatCod F(idCatCod F(F @@ Y)) = (idCatCod F(F @@ X)) ;;CatCod F(F ## f)"
    proof-
      have 1: "Category (CatCod F)" using a by simp
      have "F ## f mapsCatCod F(F @@ X) to (F @@ Y)" using a b by (auto simp add: Functor.FunctorMapsTo)
      hence 2: "F ## f  morCatCod F⇙" and 3: "codCatCod F(F ## f) = (F @@ Y)" 
        and 4: "domCatCod F(F ## f) = (F @@ X)" by auto
      have "(F ## f) ;;CatCod F(idCatCod F(F @@ Y)) = (F ## f) ;;CatCod F(idCatCod F(codCatCod F(F ## f)))"
        using 3 by simp
      also have "... = F ## f" using 1 2 by (auto simp add: Category.Cidr)
      also have "... = (idCatCod F(domCatCod F(F ## f))) ;;CatCod F(F ## f)" 
        using 1 2 by (auto simp add: Category.Cidl)
      also have "... = (idCatCod F(F @@ X)) ;;CatCod F(F ## f)" using 4 by simp
      finally show ?thesis .
    qed
  }
qed

lemma IdNatTransNatTrans: "Functor F  NatTrans (IdNatTrans F)"
by (simp add: IdNatTransNatTrans' IdNatTrans_def MakeNT)

definition
  NatTransComp' :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans 
                   ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans 
                   ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" (infixl "∙1" 75) where
  "NatTransComp' η1 η2 = 
      NTDom = NTDom η1 , 
      NTCod = NTCod η2 , 
      NatTransMap = λ X . (η1 $$ X) ;;NTCatCod η1(η2 $$ X)
  "


definition NatTransComp (infixl "" 75) where "η1  η2  MakeNT(η1 ∙1 η2)"

lemma NatTransComp_Comp1: "x  Obj (NTCatDom f) ; f ≈>∙ g  (f  g) $$ x = (f $$ x) ;;NTCatCod g(g $$ x)"
by(auto simp add: NatTransComp_def NatTransComp'_def MakeNT_def NTCatCod_def NTCatDom_def)

lemma NatTransComp_Comp2: "x  Obj (NTCatDom f) ; f ≈>∙ g  (f  g) $$ x = (f $$ x) ;;NTCatCod f(g $$ x)"
by(auto simp add: NatTransComp_def NatTransComp'_def MakeNT_def NTCatCod_def NTCatDom_def)

lemmas NatTransComp_defs = NatTransComp_def NatTransComp'_def MakeNT_def 
  NatTransComp_Comp1  NTCatCod_def NTCatDom_def

lemma [simp]: "η1 ≈>∙ η2  NatTrans η1" by auto
lemma [simp]: "η1 ≈>∙ η2  NatTrans η2" by auto
lemma NTCatDom:        "η1 ≈>∙ η2  NTCatDom η1 = NTCatDom η2" by auto
lemma NTCatCod:        "η1 ≈>∙ η2  NTCatCod η1 = NTCatCod η2" by auto
lemma [simp]: "η1 ≈>∙ η2  NTCatDom (η1 ∙1 η2) = NTCatDom η1" by (auto simp add: NatTransComp'_def NTCatDom_def)
lemma [simp]: "η1 ≈>∙ η2  NTCatCod (η1 ∙1 η2) = NTCatCod η1" by (auto simp add: NatTransComp'_def NTCatCod_def)
lemma [simp]: "η1 ≈>∙ η2  NTCatDom (η1  η2) = NTCatDom η1" by (auto simp add: NatTransComp_defs)
lemma [simp]: "η1 ≈>∙ η2  NTCatCod (η1  η2) = NTCatCod η1" by (auto simp add: NatTransComp_defs)
lemma [simp]: "NatTrans η  Category(NTCatDom η)" by (simp add:  NatTransP.NatTransFtor NTCatDom_def)
lemma [simp]: "NatTrans η  Category(NTCatCod η)" by (simp add:  NatTransP.NatTransFtor2 NTCatCod_def)
lemma DDDC: assumes "NatTrans f" shows "CatDom (NTDom f) = CatDom (NTCod f)" 
proof-
  have "CatDom (NTDom f) = NTCatDom f" by (simp add: NTCatDom_def)
  thus ?thesis using assms by (simp add: NatTransP.NatTransFtorDom)
qed
lemma CCCD: assumes "NatTrans f" shows "CatCod (NTCod f) = CatCod (NTDom f)" 
proof-
  have "CatCod (NTCod f) = NTCatCod f" by (simp add: NTCatCod_def)
  thus ?thesis using assms by (simp add: NatTransP.NatTransFtorCod)
qed

lemma IdNatTransCompDefDom: "NatTrans f  (IdNatTrans (NTDom f)) ≈>∙ f"
apply(rule NTCompDefinedI)
apply(simp_all add: IdNatTransNatTrans NatTransP.NatTransFtor)
apply(simp_all add: IdNatTrans_defs CCCD)
done

lemma IdNatTransCompDefCod: "NatTrans f  f ≈>∙ (IdNatTrans (NTCod f))"
apply(rule NTCompDefinedI)
apply(simp_all add: IdNatTransNatTrans NatTransP.NatTransFtor2)
apply(simp_all add: IdNatTrans_defs DDDC)
done

lemma NatTransCompDefCod:
  assumes "NatTrans η" and "f mapsNTCatDom ηX to Y"
  shows "(η $$ X) ≈>NTCatCod η(NTCod η ## f)"
proof(rule CompDefinedI)
  have b: "X  objNTCatDom η⇙" and c: "Y  objNTCatDom η⇙" using assms by (auto simp add: Category.MapsToObj)
  have d: "(η $$ X) mapsNTCatCod η((NTDom η) @@ X) to ((NTCod η) @@ X)" using assms b 
    by (simp add: NatTransP.NatTransMapsTo)
  thus "η $$ X  morNTCatCod η⇙" by auto
  have "f mapsCatDom (NTCod η)X to Y" using assms by (simp add: NatTransP.NatTransFtorDom)
  hence e: "NTCod η ## f mapsCatCod (NTCod η)(NTCod η @@ X) to (NTCod η @@ Y)" using assms
    by (simp add: FunctorM.FunctorCompM NatTransP.NatTransFtor2)
  thus "NTCod η ## f  morNTCatCod η⇙" by (auto simp add: NTCatCod_def)
  have "codNTCatCod η(η $$ X) = (NTCod η @@ X)" using d by auto
  also have "... = domCatCod (NTCod η)(NTCod η ## f)" using e by auto
  finally show "codNTCatCod η(η $$ X) = domNTCatCod η(NTCod η ## f)" by (auto simp add: NTCatCod_def)
qed

lemma NatTransCompDefDom:
  assumes "NatTrans η" and "f mapsNTCatDom ηX to Y"
  shows "(NTDom η ## f)  ≈>NTCatCod η(η $$ Y)"
proof(rule CompDefinedI)
  have b: "X  objNTCatDom η⇙" and c: "Y  objNTCatDom η⇙" using assms by (auto simp add: Category.MapsToObj)
  have d: "(η $$ Y) mapsNTCatCod η((NTDom η) @@ Y) to ((NTCod η) @@ Y)" using assms c
    by (simp add: NatTransP.NatTransMapsTo)
  thus "η $$ Y  morNTCatCod η⇙" by auto
  have "f mapsCatDom (NTDom η)X to Y" using assms by (simp add: NTCatDom_def)
  hence e: "NTDom η ## f mapsCatCod (NTDom η)(NTDom η @@ X) to (NTDom η @@ Y)" using assms
    by (simp add: FunctorM.FunctorCompM NatTransP.NatTransFtor)
  thus "NTDom η ## f  morNTCatCod η⇙" using assms by (auto simp add: NatTransP.NatTransFtorCod) 
  have "domNTCatCod η(η $$ Y) = (NTDom η @@ Y)" using d by auto
  also have "... = codCatCod (NTDom η)(NTDom η ## f)" using e by auto
  finally show "codNTCatCod η(NTDom η ## f) = domNTCatCod η(η $$ Y)" 
    using assms by (auto simp add: NatTransP.NatTransFtorCod)
qed

lemma NatTransCompCompDef:
  assumes "η1 ≈>∙ η2" and "X  objNTCatDom η1⇙"
  shows "(η1 $$ X) ≈>NTCatCod η1(η2 $$ X)"
proof(rule CompDefinedI)
  have 1: "(η1 $$ X) mapsNTCatCod η1((NTDom η1) @@ X) to ((NTCod η1) @@ X)" using assms
    by (simp add: NatTransP.NatTransMapsTo)
  have "NTCatCod η1 = NTCatCod η2" using assms by auto
  hence 2: "(η2 $$ X) mapsNTCatCod η1((NTDom η2) @@ X) to ((NTCod η2) @@ X)" using assms
    by (simp add: NatTransP.NatTransMapsTo NTCatDom)
  show "η1 $$ X  morNTCatCod η1⇙" 
    and "η2 $$ X  morNTCatCod η1⇙"  using 1 2 by auto
  have "codNTCatCod η1(η1 $$ X) = ((NTCod η1) @@ X)" using 1 by auto
  also have "... = ((NTDom η2) @@ X)" using assms by auto
  finally show "codNTCatCod η1(η1 $$ X) = domNTCatCod η1(η2 $$ X)" using 2 by auto
qed
 
lemma NatTransCompNatTrans': 
  assumes "η1 ≈>∙ η2"
  shows   "NatTransP (η1 ∙1 η2)"
proof(auto simp add: NatTransP_def)
  show "Functor (NTDom (η1 ∙1 η2))" and "Functor (NTCod (η1 ∙1 η2))" using assms
    by (auto simp add: NatTransComp'_def NatTransP.NatTransFtor NatTransP.NatTransFtor2)
  show "NTCatDom (η1 ∙1 η2) = CatDom (NTCod (η1 ∙1 η2))" and
       "NTCatCod (η1 ∙1 η2) = CatCod (NTDom (η1 ∙1 η2))"
  proof (auto simp add: NatTransComp'_def NTCatCod_def NTCatDom_def)
    have "CatDom (NTDom η1) = NTCatDom η1" by (simp add: NTCatDom_def)
    thus "CatDom (NTDom η1) = CatDom (NTCod η2)" using assms by (auto simp add: NatTransP.NatTransFtorDom)
    have "CatCod (NTCod η2) = NTCatCod η2" by (simp add: NTCatCod_def)
    thus "CatCod (NTCod η2) = CatCod (NTDom η1)" using assms by (auto simp add: NatTransP.NatTransFtorCod)
  qed
  {
    fix X assume aa: "X  objNTCatDom (η1 ∙1 η2)⇙"
    show "(η1 ∙1 η2) $$ X mapsNTCatCod (η1 ∙1 η2)NTDom (η1 ∙1 η2) @@ X to NTCod (η1 ∙1 η2) @@ X"
    proof-
      have "X  objNTCatDom η1⇙" and "NatTrans η1" using assms aa by simp+
      hence "(η1 $$ X) mapsNTCatCod η1((NTDom η1) @@ X) to ((NTCod η1) @@ X)" 
        by (simp add: NatTransP.NatTransMapsTo)
      moreover have "(η2 $$ X) mapsNTCatCod η1((NTCod η1) @@ X) to ((NTCod η2) @@ X)" 
      proof-
        have "X  objNTCatDom η2⇙" and "NatTrans η2" using assms aa by auto
        hence "(η2 $$ X) mapsNTCatCod η2((NTDom η2) @@ X) to ((NTCod η2) @@ X)" 
          by (simp add: NatTransP.NatTransMapsTo)
        thus ?thesis using assms by auto
      qed
      ultimately have "(η1 $$ X) ;;NTCatCod η1(η2 $$ X) mapsNTCatCod η1((NTDom η1) @@ X) to ((NTCod η2) @@ X)"
        using assms by (simp add: Category.Ccompt)
      thus ?thesis using assms by (auto simp add: NatTransComp'_def NTCatCod_def)
    qed
  }
  {
    fix f X Y assume a: "f maps(NTCatDom (η1 ∙1 η2))X to Y"
    show "(NTDom (η1 ∙1 η2) ## f) ;;NTCatCod (η1 ∙1 η2)(η1 ∙1 η2 $$ Y) =
       ((η1 ∙1 η2) $$ X) ;;NTCatCod (η1 ∙1 η2)(NTCod (η1 ∙1 η2) ## f)"
    proof-
      have b: "X  objNTCatDom η1⇙" and c: "Y  objNTCatDom η1⇙" using assms a by (auto simp add: Category.MapsToObj)
      have "((NTDom η1) ## f) ;;NTCatCod η1((η1 $$ Y) ;;NTCatCod η1(η2 $$ Y)) = 
            (((NTDom η1) ## f) ;;NTCatCod η1(η1 $$ Y)) ;;NTCatCod η1(η2 $$ Y)" 
      proof-
        have "((NTDom η1) ## f) ≈>NTCatCod η1(η1 $$ Y)" using assms a by (auto simp add: NatTransCompDefDom)
        moreover have "(η1 $$ Y) ≈>NTCatCod η1(η2 $$ Y)" using assms by (simp add: NatTransCompCompDef c)
        ultimately show ?thesis using assms by (simp add: Category.Cassoc)
      qed
      also have "... = ((η1 $$ X) ;;NTCatCod η1((NTDom η2) ## f)) ;;NTCatCod η1(η2 $$ Y)"
        using assms a by (auto simp add: NatTransP.NatTrans)
      also have "... = (η1 $$ X) ;;NTCatCod η1(((NTDom η2) ## f) ;;NTCatCod η1(η2 $$ Y))" 
      proof-
        have "(η1 $$ X) ≈>NTCatCod η1((NTCod η1) ## f)" using assms a by (simp add: NatTransCompDefCod)
        moreover have "((NTDom η2) ## f) ≈>NTCatCod η1(η2 $$ Y)" using assms a 
          by (simp add: NatTransCompDefDom NTCatDom NTCatCod)
        ultimately show ?thesis using assms by (auto simp add: Category.Cassoc)
      qed
      also have "... = (η1 $$ X) ;;NTCatCod η1((η2 $$ X) ;;NTCatCod η1((NTCod η2) ## f))" 
        using assms a by (simp add: NatTransP.NatTrans NTCatDom NTCatCod)
      also have "... = (η1 $$ X) ;;NTCatCod η1(η2 $$ X) ;;NTCatCod η1((NTCod η2) ## f)"
      proof-
        have "(η1 $$ X) ≈>NTCatCod η1(η2 $$ X)" using assms by (simp add: NatTransCompCompDef b)
        moreover have "(η2 $$ X) ≈>NTCatCod η1((NTCod η2) ## f)" using assms a 
          by (simp add: NatTransCompDefCod NTCatCod NTCatDom)
        ultimately show ?thesis using assms by (simp add: Category.Cassoc)
      qed
      finally show ?thesis using assms by (auto simp add: NatTransComp'_def NTCatCod_def)
    qed
  }
qed

lemma NatTransCompNatTrans: "η1 ≈>∙ η2  NatTrans (η1  η2)"
by (simp add: NatTransCompNatTrans' NatTransComp_def MakeNT)

definition
  CatExp' :: "('o1,'m1,'a) Category_scheme  ('o2,'m2,'b) Category_scheme  
                     (('o1, 'o2, 'm1, 'm2, 'a, 'b) Functor, 
                      ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans) Category"  where
  "CatExp' A B  
      Category.Obj = {F . Ftor F : A  B} , 
      Category.Mor = {η . NatTrans η  NTCatDom η = A  NTCatCod η = B} , 
      Category.Dom = NTDom , 
      Category.Cod = NTCod , 
      Category.Id  = IdNatTrans , 
      Category.Comp = λf g. (f  g)
  "

definition "CatExp A B  MakeCat(CatExp' A B)"

lemma IdNatTransMapL: 
  assumes NT: "NatTrans f"
  shows "IdNatTrans (NTDom f)  f = f"
proof(rule NatTransExt)
  show "NatTrans f" using assms .
  show "NatTrans (IdNatTrans (NTDom f)  f)" using NT 
    by (simp add: NatTransP.NatTransFtor IdNatTransNatTrans IdNatTransCompDefDom NatTransCompNatTrans)
  show "NTDom (IdNatTrans (NTDom f)  f) = NTDom f" and
    "NTCod (IdNatTrans (NTDom f)  f) = NTCod f" by (simp add: IdNatTrans_defs NatTransComp_defs)+
  {
    fix x assume aa: "x  Obj (NTCatDom (IdNatTrans (NTDom f)  f))"
    show "(IdNatTrans (NTDom f)  f) $$ x = f $$ x"
    proof-
      have XObj: "x  Obj(NTCatDom f)" using aa by (simp add: IdNatTrans_defs NatTransComp_defs)
      have fMap: "f $$ x mapsNTCatCod fNTDom f @@ x to NTCod f @@ x" using NT XObj
        by (simp add: NatTransP.NatTransMapsTo)
      have "(IdNatTrans (NTDom f)  f) $$ x = (IdNatTrans (NTDom f) $$ x) ;;NTCatCod f(f $$ x)" 
      proof(rule NatTransComp_Comp1)
        show "x  objNTCatDom (IdNatTrans (NTDom f))⇙" using XObj by (simp add: IdNatTrans_defs)
        show "IdNatTrans (NTDom f) ≈>∙ f" using NT by (simp add: IdNatTransCompDefDom)
      qed
      also have "... = idNTCatCod f(domNTCatCod f(f $$ x)) ;;NTCatCod f(f $$ x)" 
        using XObj NT fMap by (auto simp add: IdNatTrans_map NTCatDom_def CCCD NTCatCod_def)
      also have "... = f $$ x"  
      proof-
        have "f $$ x  morNTCatCod f⇙" using fMap by (auto)
        thus ?thesis using NT by (simp add: Category.Cidl)
      qed
      finally show ?thesis .
    qed
  }
qed

lemma IdNatTransMapR: 
  assumes NT: "NatTrans f"
  shows "f  IdNatTrans (NTCod f) = f" 
proof(rule NatTransExt)
  show "NatTrans f" using assms .
  show "NatTrans (f  IdNatTrans (NTCod f))" using NT 
    by (simp add: NatTransP.NatTransFtor IdNatTransNatTrans IdNatTransCompDefCod NatTransCompNatTrans)
  show "NTDom (f  IdNatTrans (NTCod f)) = NTDom f" and
    "NTCod (f  IdNatTrans (NTCod f)) = NTCod f" by (simp add: IdNatTrans_defs NatTransComp_defs)+
  {
    fix x assume aa: "x  Obj (NTCatDom (f  IdNatTrans (NTCod f)))"
    show "(f  IdNatTrans (NTCod f)) $$ x = f $$ x" 
    proof-
      have XObj: "x  Obj(NTCatDom f)" using aa by (simp add:  NatTransComp_defs)
      have fMap: "f $$ x mapsNTCatCod fNTDom f @@ x to NTCod f @@ x" using NT XObj
        by (simp add: NatTransP.NatTransMapsTo)
      have "(f  IdNatTrans (NTCod f)) $$ x = (f $$ x) ;;NTCatCod f(IdNatTrans (NTCod f) $$ x)"
        using XObj NT by (auto simp add: NatTransComp_Comp2 IdNatTransCompDefCod)
      also have "... = (f $$ x) ;;NTCatCod f(idNTCatCod f(codNTCatCod f(f $$ x)))"
      proof-
        have "x  objCatDom (NTCod f)⇙" using XObj NT by (simp add: IdNatTrans_defs DDDC)
        moreover have "(codNTCatCod f(f $$ x)) = (NTCod f) @@ x" using fMap by auto
        ultimately have "(IdNatTrans (NTCod f) $$ x) = (idNTCatCod f(codNTCatCod f(f $$ x)))" 
          by (simp add: IdNatTrans_map NTCatCod_def)
        thus ?thesis by simp
      qed
      also have "... = f $$ x" 
      proof-
        have "f $$ x  morNTCatCod f⇙" using fMap by (auto)
        thus ?thesis using NT by (simp add: Category.Cidr)
      qed
      finally show ?thesis .
    qed
  }
qed

lemma NatTransCompDefined:
  assumes "f ≈>∙ g" and "g ≈>∙ h" 
  shows "(f  g) ≈>∙ h" and "f ≈>∙ (g  h)"
proof-
  show "(f  g) ≈>∙ h"
  proof(rule NTCompDefinedI)
    show "NatTrans (f  g)" and "NatTrans h" using assms by (auto simp add: NatTransCompNatTrans)
    have "NTCatDom f = NTCatDom h" using assms by (simp add: NTCatDom)
    thus "NTCatDom h = NTCatDom (f  g)" by (simp add: NatTransComp_defs)
    have "NTCatCod h = NTCatCod g" using assms by (simp add: NTCatCod)
    thus "NTCatCod h = NTCatCod (f  g)" by ( simp add: NatTransComp_defs)
    show "NTCod (f  g) = NTDom h" using assms by (auto simp add: NatTransComp_defs)
  qed
  show "f ≈>∙ (g  h)"
  proof(rule NTCompDefinedI)
    show "NatTrans f" and "NatTrans (g  h)" using assms by (auto simp add: NatTransCompNatTrans)
    have "NTCatDom f = NTCatDom g" using assms by (simp add: NTCatDom)
    thus "NTCatDom (g  h) = NTCatDom f" by (simp add: NatTransComp_defs)
    have "NTCatCod h = NTCatCod f" using assms by (simp add: NTCatCod)
    thus "NTCatCod (g  h) = NTCatCod f" by ( simp add: NatTransComp_defs)
    show "NTCod f = NTDom (g  h)" using assms by (auto simp add: NatTransComp_defs)
  qed
qed

lemma NatTransCompAssoc:
  assumes "f ≈>∙ g" and "g ≈>∙ h" 
  shows "(f  g)  h = f  (g  h)"
proof(rule NatTransExt)
  show "NatTrans ((f  g)  h)" using assms by (simp add: NatTransCompNatTrans NatTransCompDefined)
  show "NatTrans (f  (g  h))" using assms by (simp add: NatTransCompNatTrans NatTransCompDefined)
  show "NTDom (f  g  h) = NTDom (f  (g  h))" and "NTCod (f  g  h) = NTCod (f  (g  h))"
    by(simp add: NatTransComp_defs)+
  {
    fix x assume aa: "x  objNTCatDom (f  g  h)⇙" show "((f  g)  h) $$ x = (f  (g  h)) $$ x"
    proof-
      have ntd1: "NTCatDom (f  g) = NTCatDom (f  g  h)" and ntd2: "NTCatDom f = NTCatDom (f  g  h)" 
        using assms by (simp add: NatTransCompDefined)+
      have obj1: "x  Obj (NTCatDom f)" using aa ntd2 by simp
      have  1: "(f  g) $$ x = (f $$ x) ;;NTCatCod h(g $$ x)" and 
            2: "(g  h) $$ x = (g $$ x) ;;NTCatCod h(h $$ x)" using obj1
        using assms by (auto simp add: NatTransComp_Comp1)
      have "((f  g)  h) $$ x = ((f  g) $$ x) ;;NTCatCod h(h $$ x)"
      proof(rule NatTransComp_Comp1)
        show "x  objNTCatDom (f  g)⇙" using aa ntd1 by simp
        show "f  g ≈>∙ h" using assms by (simp add: NatTransCompDefined)
      qed
      also have "... = ((f $$ x) ;;NTCatCod h(g $$ x)) ;;NTCatCod h(h $$ x)" using 1 by simp
      also have "... = (f $$ x) ;;NTCatCod h((g $$ x) ;;NTCatCod h(h $$ x))" 
      proof-
        have 1: "NTCatCod h = NTCatCod f" and 2: "NTCatCod h = NTCatCod g" using assms by (simp add: NTCatCod)+
        hence "(f $$ x) ≈>NTCatCod h(g $$ x)" using obj1 assms by (simp add: NatTransCompCompDef) 
        moreover have "(g $$ x) ≈>NTCatCod h(h $$ x)" using obj1 assms 2 by (simp add: NatTransCompCompDef NTCatDom)
        moreover have "Category (NTCatCod h)" using assms by auto
        ultimately show ?thesis by (simp add: Category.Cassoc)
      qed
      also have "... = (f $$ x) ;;NTCatCod h((g  h) $$ x)" using 2 by simp
      also have "... = (f  (g  h)) $$ x"
      proof-
        have "NTCatCod f = NTCatCod h" using assms by (simp add: NTCatCod)
        moreover have "(f  (g  h)) $$ x = (f $$ x) ;;NTCatCod f((g  h) $$ x)"
        proof(rule NatTransComp_Comp2)
          show "x  objNTCatDom f⇙" using obj1 assms by (simp add: NTCatDom)
          show "f ≈>∙ g  h" using assms by (simp add: NatTransCompDefined)
        qed
        ultimately show ?thesis by simp
      qed
      finally show ?thesis .
    qed
  }
qed

lemma CatExpCatAx: 
  assumes "Category A" and "Category B"
  shows "Category_axioms (CatExp' A B)"
proof(auto simp add: Category_axioms_def)
  {
    fix f assume "f  morCatExp' A B⇙" 
    thus "domCatExp' A Bf  objCatExp' A B⇙" and 
         "codCatExp' A Bf  objCatExp' A B⇙" 
      by(auto simp add: CatExp'_def NatTransP.NatTransFtor 
        NatTransP.NatTransFtor2 NatTransP.NatTransFtorDom NatTransP.NatTransFtorCod DDDC CCCD functor_abbrev_def)
  }
  {
    fix F assume a: "F  objCatExp' A B⇙" 
    show "idCatExp' A BF mapsCatExp' A BF to F" 
    proof(rule MapsToI)
      have "Ftor F : A  B" using a by (simp add: CatExp'_def)
      thus "idCatExp' A BF  morCatExp' A B⇙" 
        apply(simp add: CatExp'_def NTCatDom_def NTCatCod_def IdNatTransNatTrans functor_abbrev_def)
        apply(simp add: IdNatTrans_defs)
        done
      show "domCatExp' A B(idCatExp' A BF) = F" by (simp add: CatExp'_def IdNatTrans_defs)
      show "codCatExp' A B(idCatExp' A BF) = F" by (simp add: CatExp'_def IdNatTrans_defs)
    qed
  }
  {
    fix f assume a: "f  morCatExp' A B⇙" 
    show "(idCatExp' A B(domCatExp' A Bf)) ;;CatExp' A Bf = f" and
         "f ;;CatExp' A B(idCatExp' A B(codCatExp' A Bf)) = f" 
    proof(simp_all add: CatExp'_def)
      have NT: "NatTrans f" using a by (simp add: CatExp'_def)
      show "IdNatTrans (NTDom f)  f = f" using NT by (simp add:IdNatTransMapL)
      show "f  IdNatTrans (NTCod f) = f" using NT by (simp add:IdNatTransMapR)
    qed
  }
  {
    fix f g h assume aa: "f ≈>CatExp' A Bg" and bb: "g ≈>CatExp' A Bh"
    {
      fix f g assume "f ≈>CatExp' A Bg" hence "f ≈>∙ g"
        apply(simp only: NTCompDefined_def)
        by (auto simp add:  CatExp'_def)
    }
    hence "f ≈>∙ g" and "g ≈>∙ h" using aa bb by auto
    thus "f ;;CatExp' A Bg ;;CatExp' A Bh = f ;;CatExp' A B(g ;;CatExp' A Bh)" 
    by(simp add: CatExp'_def NatTransCompAssoc)
  }
  {
    fix f g X Y Z assume a: "f mapsCatExp' A BX to Y" and b: "g mapsCatExp' A BY to Z"
    show "f ;;CatExp' A Bg mapsCatExp' A BX to Z" 
    proof(rule MapsToI, auto simp add: CatExp'_def)
      have nt1: "NatTrans f" and cd1: "NTCatDom f = A"
        and cc1: "NTCatCod f = B" and d1: "NTDom f = X" and c1: "NTCod f = Y"
        using a by (auto simp add: CatExp'_def)
      moreover have nt2: "NatTrans g" and cd2: "NTCatDom g = A" 
        and cc2: "NTCatCod g = B" and d2: "NTDom g = Y" and c2: "NTCod g = Z"
        using b by (auto simp add: CatExp'_def)
      ultimately have Comp: "f ≈>∙ g" by(auto intro: NTCompDefinedI)
      thus "NatTrans (f  g)" by (simp add: NatTransCompNatTrans)
      show "NTCatDom (f  g) = A" using Comp cd1 by (simp add: NTCatDom)
      show "NTCatCod (f  g) = B" using Comp cc2 by (simp add: NTCatCod)
      show "NTDom (f  g) = X" using d1 by (simp add: NatTransComp_defs)
      show "NTCod (f  g) = Z" using c2 by (simp add: NatTransComp_defs)
    qed
  }
qed

lemma CatExpCat: "Category A ; Category B  Category (CatExp A B)"
by(simp add: CatExpCatAx CatExp_def MakeCat)

lemmas CatExp_defs = CatExp_def CatExp'_def MakeCat_def

lemma CatExpDom: "f  Mor (CatExp A B)  domCatExp A Bf = NTDom f"
by (simp add: CatExp_defs)

lemma CatExpCod: "f  Mor (CatExp A B)  codCatExp A Bf = NTCod f"
by (simp add: CatExp_defs)

lemma CatExpId: "X  Obj (CatExp A B)  Id (CatExp A B) X = IdNatTrans X"
by (simp add: CatExp_defs)

lemma CatExpNatTransCompDef: assumes "f ≈>CatExp A Bg" shows "f ≈>∙ g"
proof-
  have 1: "f ≈>CatExp' A Bg" using assms by (simp add: CatExp_def MakeCatCompDef)
  show "f ≈>∙ g"
  proof(rule NTCompDefinedI)
    show "NatTrans f" using 1 by (auto simp add: CatExp'_def)
    show "NatTrans g" using 1 by (auto simp add: CatExp'_def)
    show "NTCatDom g = NTCatDom f" using 1 by (auto simp add: CatExp'_def)
    show "NTCatCod g = NTCatCod f" using 1 by (auto simp add: CatExp'_def)
    show "NTCod f = NTDom g" using 1 by (auto simp add: CatExp'_def)
  qed
qed

lemma CatExpDist:
  assumes "X  Obj A" and "f ≈>CatExp A Bg"
  shows "(f ;;CatExp A Bg) $$ X = (f $$ X) ;;B(g $$ X)"
proof-
  have "f  Mor (CatExp' A B)" using assms by (auto simp add: CatExp_def MakeCatMor)
  hence 1: "NTCatDom f = A" and 2: "NTCatCod f = B" by (simp add: CatExp'_def)+
  hence 4: "X  Obj (NTCatDom f)" using assms by simp
  have 3: "f ≈>∙ g" using assms(2) by (simp add: CatExpNatTransCompDef)
  have "(f ;;CatExp A Bg) $$ X = (f ;;CatExp' A Bg) $$ X" using assms(2) by (simp add: CatExp_def MakeCatComp2)
  also have "... = (f  g) $$ X" by (simp add: CatExp'_def)
  also have "... = (f $$ X) ;;B(g $$ X)" using 4 2 3 by (simp add: NatTransComp_Comp2[of X f g])
  finally show ?thesis .
qed    

lemma CatExpMorNT: "f  Mor (CatExp A B)  NatTrans f"
by (simp add: CatExp_defs)

end