(*
 * Copyright 2014, NICTA
 *
 * This software may be distributed and modified according to the terms of
 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
 * See "LICENSE_BSD2.txt" for details.
 *
 * @TAG(NICTA_BSD)
 *)

(* Author: Thomas Sewell *)

section "Enumeration Instances for Words"

theory Word_Enum
imports Enumeration Word_Lib
begin

instantiation word :: (len) enum
begin

definition
  "(enum_class.enum :: ('a :: len) word list) \<equiv> map of_nat [0 ..< 2 ^ len_of TYPE('a)]"

definition
  "enum_class.enum_all (P :: ('a :: len) word \<Rightarrow> bool) \<longleftrightarrow> Ball UNIV P"

definition
  "enum_class.enum_ex (P :: ('a :: len) word \<Rightarrow> bool) \<longleftrightarrow> Bex UNIV P"

instance
  apply (intro_classes)
     apply (simp add: enum_word_def)
     apply force
    apply (simp add: distinct_map enum_word_def)
    apply (rule subset_inj_on, rule word_unat.Abs_inj_on)
    apply (clarsimp simp add: unats_def)
   apply (simp add: enum_all_word_def)
  apply (simp add: enum_ex_word_def)
  done

end

lemma fromEnum_unat[simp]: "fromEnum (x :: ('a :: len) word) = unat x"
  apply (subgoal_tac "x \<in> set enum")
   defer
   apply simp
  apply (unfold fromEnum_def enum_word_def)
  apply (subgoal_tac "ALL n. n < 2 ^ len_of TYPE('a) --> (map of_nat [0..< 2 ^ len_of TYPE('a)] ! n) = x --> n = unat x")
   apply (subgoal_tac "(map of_nat [0..< 2 ^ len_of TYPE('a)]) ! (the_index (map of_nat [0..< 2 ^ len_of TYPE('a)]) x) = x")
    apply (subgoal_tac "the_index (map of_nat [0..< 2 ^ len_of TYPE('a)]) x < 2 ^ len_of TYPE('a)")
     apply simp
    apply (subgoal_tac "the_index (map of_nat [0..< 2 ^ len_of TYPE('a)]) x < length (map of_nat [0..< 2 ^ len_of TYPE('a)])")
     apply simp
    apply (rule the_index_bounded)
    apply (simp add: enum_word_def)
   apply (rule nth_the_index)
   apply (simp add: enum_word_def)
  apply safe
  apply (simp add: unat_of_nat)
  done

lemma length_word_enum: "length (enum :: ('a :: len) word list) = 2 ^ len_of TYPE('a)"
  apply (simp add: enum_word_def)
  done

lemma toEnum_of_nat[simp]: "n < 2 ^ len_of TYPE('a) \<Longrightarrow> ((toEnum n) :: ('a :: len) word) = of_nat n"
  apply (subst toEnum_def)
  apply (simp add: length_word_enum)
  apply (subst enum_word_def)
  apply simp
  done

declare of_nat_diff [simp]

lemma "(maxBound :: ('a :: len) word) = -1"
  apply (unfold maxBound_def enum_word_def)
  apply (subst last_map)
   apply simp
  apply simp
  done

lemma "(minBound :: ('a :: len) word) = 0"
  apply (unfold minBound_def enum_word_def)
  apply (simp add: hd_conv_nth)
  done

instantiation word :: (len) enumeration_both
begin

definition
  enum_alt_word_def: "enum_alt \<equiv> alt_from_ord (enum :: ('a :: len) word list)"

instance
  by (intro_classes, simp add: enum_alt_word_def)

end

definition
  upto_enum_step :: "('a :: len) word \<Rightarrow> 'a word \<Rightarrow> 'a word \<Rightarrow> 'a word list" ("[_ , _ .e. _]")
where
  "upto_enum_step a b c \<equiv>
      if c < a then [] else map (\<lambda>x. a + x * (b - a)) [0 .e. (c - a) div (b - a)]"
  (* in the wraparound case, bad things happen. *)

lemma maxBound_word:
  "(maxBound::'a::len word) = -1"
  by (simp add: maxBound_def enum_word_def last_map)

lemma minBound_word:
  "(minBound::'a::len word) = 0"
  by (simp add: minBound_def enum_word_def upt_conv_Cons)

lemma maxBound_max_word:
  "(maxBound::'a::len word) = max_word"
  apply (subst maxBound_word)
  apply (subst max_word_minus [symmetric])
  apply (rule refl)
  done

end


(*  Title:      HOL/Lifting.thy
    Author:     Brian Huffman and Ondrej Kuncar
    Author:     Cezary Kaliszyk and Christian Urban
*)

section \<open>Lifting package\<close>

theory Lifting
imports Equiv_Relations Transfer
keywords
  "parametric" and
  "print_quot_maps" "print_quotients" :: diag and
  "lift_definition" :: thy_goal and
  "setup_lifting" "lifting_forget" "lifting_update" :: thy_decl
begin

subsection \<open>Function map\<close>

context includes lifting_syntax
begin

lemma map_fun_id:
  "(id ---> id) = id"
  by (simp add: fun_eq_iff)

subsection \<open>Quotient Predicate\<close>

definition
  "Quotient R Abs Rep T \<longleftrightarrow>
     (\<forall>a. Abs (Rep a) = a) \<and>
     (\<forall>a. R (Rep a) (Rep a)) \<and>
     (\<forall>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s) \<and>
     T = (\<lambda>x y. R x x \<and> Abs x = y)"

lemma QuotientI:
  assumes "\<And>a. Abs (Rep a) = a"
    and "\<And>a. R (Rep a) (Rep a)"
    and "\<And>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s"
    and "T = (\<lambda>x y. R x x \<and> Abs x = y)"
  shows "Quotient R Abs Rep T"
  using assms unfolding Quotient_def by blast

context
  fixes R Abs Rep T
  assumes a: "Quotient R Abs Rep T"
begin

lemma Quotient_abs_rep: "Abs (Rep a) = a"
  using a unfolding Quotient_def
  by simp

lemma Quotient_rep_reflp: "R (Rep a) (Rep a)"
  using a unfolding Quotient_def
  by blast

lemma Quotient_rel:
  "R r r \<and> R s s \<and> Abs r = Abs s \<longleftrightarrow> R r s" \<comment> \<open>orientation does not loop on rewriting\<close>
  using a unfolding Quotient_def
  by blast

lemma Quotient_cr_rel: "T = (\<lambda>x y. R x x \<and> Abs x = y)"
  using a unfolding Quotient_def
  by blast

lemma Quotient_refl1: "R r s \<Longrightarrow> R r r"
  using a unfolding Quotient_def
  by fast

lemma Quotient_refl2: "R r s \<Longrightarrow> R s s"
  using a unfolding Quotient_def
  by fast

lemma Quotient_rel_rep: "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
  using a unfolding Quotient_def
  by metis

lemma Quotient_rep_abs: "R r r \<Longrightarrow> R (Rep (Abs r)) r"
  using a unfolding Quotient_def
  by blast

lemma Quotient_rep_abs_eq: "R t t \<Longrightarrow> R \<le> op= \<Longrightarrow> Rep (Abs t) = t"
  using a unfolding Quotient_def
  by blast

lemma Quotient_rep_abs_fold_unmap:
  assumes "x' \<equiv> Abs x" and "R x x" and "Rep x' \<equiv> Rep' x'"
  shows "R (Rep' x') x"
proof -
  have "R (Rep x') x" using assms(1-2) Quotient_rep_abs by auto
  then show ?thesis using assms(3) by simp
qed

lemma Quotient_Rep_eq:
  assumes "x' \<equiv> Abs x"
  shows "Rep x' \<equiv> Rep x'"
by simp

lemma Quotient_rel_abs: "R r s \<Longrightarrow> Abs r = Abs s"
  using a unfolding Quotient_def
  by blast

lemma Quotient_rel_abs2:
  assumes "R (Rep x) y"
  shows "x = Abs y"
proof -
  from assms have "Abs (Rep x) = Abs y" by (auto intro: Quotient_rel_abs)
  then show ?thesis using assms(1) by (simp add: Quotient_abs_rep)
qed

lemma Quotient_symp: "symp R"
  using a unfolding Quotient_def using sympI by (metis (full_types))

lemma Quotient_transp: "transp R"
  using a unfolding Quotient_def using transpI by (metis (full_types))

lemma Quotient_part_equivp: "part_equivp R"
by (metis Quotient_rep_reflp Quotient_symp Quotient_transp part_equivpI)

end

lemma identity_quotient: "Quotient (op =) id id (op =)"
unfolding Quotient_def by simp

text \<open>TODO: Use one of these alternatives as the real definition.\<close>

lemma Quotient_alt_def:
  "Quotient R Abs Rep T \<longleftrightarrow>
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
    (\<forall>b. T (Rep b) b) \<and>
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y)"
apply safe
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (simp (no_asm_use) only: Quotient_def, fast)
apply (rule QuotientI)
apply simp
apply metis
apply simp
apply (rule ext, rule ext, metis)
done

lemma Quotient_alt_def2:
  "Quotient R Abs Rep T \<longleftrightarrow>
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
    (\<forall>b. T (Rep b) b) \<and>
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs y) \<and> T y (Abs x))"
  unfolding Quotient_alt_def by (safe, metis+)

lemma Quotient_alt_def3:
  "Quotient R Abs Rep T \<longleftrightarrow>
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and>
    (\<forall>x y. R x y \<longleftrightarrow> (\<exists>z. T x z \<and> T y z))"
  unfolding Quotient_alt_def2 by (safe, metis+)

lemma Quotient_alt_def4:
  "Quotient R Abs Rep T \<longleftrightarrow>
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and> R = T OO conversep T"
  unfolding Quotient_alt_def3 fun_eq_iff by auto

lemma Quotient_alt_def5:
  "Quotient R Abs Rep T \<longleftrightarrow>
    T \<le> BNF_Def.Grp UNIV Abs \<and> BNF_Def.Grp UNIV Rep \<le> T\<inverse>\<inverse> \<and> R = T OO T\<inverse>\<inverse>"
  unfolding Quotient_alt_def4 Grp_def by blast

lemma fun_quotient:
  assumes 1: "Quotient R1 abs1 rep1 T1"
  assumes 2: "Quotient R2 abs2 rep2 T2"
  shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2) (T1 ===> T2)"
  using assms unfolding Quotient_alt_def2
  unfolding rel_fun_def fun_eq_iff map_fun_apply
  by (safe, metis+)

lemma apply_rsp:
  fixes f g::"'a \<Rightarrow> 'c"
  assumes q: "Quotient R1 Abs1 Rep1 T1"
  and     a: "(R1 ===> R2) f g" "R1 x y"
  shows "R2 (f x) (g y)"
  using a by (auto elim: rel_funE)

lemma apply_rsp':
  assumes a: "(R1 ===> R2) f g" "R1 x y"
  shows "R2 (f x) (g y)"
  using a by (auto elim: rel_funE)

lemma apply_rsp'':
  assumes "Quotient R Abs Rep T"
  and "(R ===> S) f f"
  shows "S (f (Rep x)) (f (Rep x))"
proof -
  from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
  then show ?thesis using assms(2) by (auto intro: apply_rsp')
qed

subsection \<open>Quotient composition\<close>

lemma Quotient_compose:
  assumes 1: "Quotient R1 Abs1 Rep1 T1"
  assumes 2: "Quotient R2 Abs2 Rep2 T2"
  shows "Quotient (T1 OO R2 OO conversep T1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2) (T1 OO T2)"
  using assms unfolding Quotient_alt_def4 by fastforce

lemma equivp_reflp2:
  "equivp R \<Longrightarrow> reflp R"
  by (erule equivpE)

subsection \<open>Respects predicate\<close>

definition Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
  where "Respects R = {x. R x x}"

lemma in_respects: "x \<in> Respects R \<longleftrightarrow> R x x"
  unfolding Respects_def by simp

lemma UNIV_typedef_to_Quotient:
  assumes "type_definition Rep Abs UNIV"
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
  shows "Quotient (op =) Abs Rep T"
proof -
  interpret type_definition Rep Abs UNIV by fact
  from Abs_inject Rep_inverse Abs_inverse T_def show ?thesis
    by (fastforce intro!: QuotientI fun_eq_iff)
qed

lemma UNIV_typedef_to_equivp:
  fixes Abs :: "'a \<Rightarrow> 'b"
  and Rep :: "'b \<Rightarrow> 'a"
  assumes "type_definition Rep Abs (UNIV::'a set)"
  shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
by (rule identity_equivp)

lemma typedef_to_Quotient:
  assumes "type_definition Rep Abs S"
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
  shows "Quotient (eq_onp (\<lambda>x. x \<in> S)) Abs Rep T"
proof -
  interpret type_definition Rep Abs S by fact
  from Rep Abs_inject Rep_inverse Abs_inverse T_def show ?thesis
    by (auto intro!: QuotientI simp: eq_onp_def fun_eq_iff)
qed

lemma typedef_to_part_equivp:
  assumes "type_definition Rep Abs S"
  shows "part_equivp (eq_onp (\<lambda>x. x \<in> S))"
proof (intro part_equivpI)
  interpret type_definition Rep Abs S by fact
  show "\<exists>x. eq_onp (\<lambda>x. x \<in> S) x x" using Rep by (auto simp: eq_onp_def)
next
  show "symp (eq_onp (\<lambda>x. x \<in> S))" by (auto intro: sympI simp: eq_onp_def)
next
  show "transp (eq_onp (\<lambda>x. x \<in> S))" by (auto intro: transpI simp: eq_onp_def)
qed

lemma open_typedef_to_Quotient:
  assumes "type_definition Rep Abs {x. P x}"
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
  shows "Quotient (eq_onp P) Abs Rep T"
  using typedef_to_Quotient [OF assms] by simp

lemma open_typedef_to_part_equivp:
  assumes "type_definition Rep Abs {x. P x}"
  shows "part_equivp (eq_onp P)"
  using typedef_to_part_equivp [OF assms] by simp

lemma type_definition_Quotient_not_empty: "Quotient (eq_onp P) Abs Rep T \<Longrightarrow> \<exists>x. P x"
unfolding eq_onp_def by (drule Quotient_rep_reflp) blast

lemma type_definition_Quotient_not_empty_witness: "Quotient (eq_onp P) Abs Rep T \<Longrightarrow> P (Rep undefined)"
unfolding eq_onp_def by (drule Quotient_rep_reflp) blast

text \<open>Generating transfer rules for quotients.\<close>

context
  fixes R Abs Rep T
  assumes 1: "Quotient R Abs Rep T"
begin

lemma Quotient_right_unique: "right_unique T"
  using 1 unfolding Quotient_alt_def right_unique_def by metis

lemma Quotient_right_total: "right_total T"
  using 1 unfolding Quotient_alt_def right_total_def by metis

lemma Quotient_rel_eq_transfer: "(T ===> T ===> op =) R (op =)"
  using 1 unfolding Quotient_alt_def rel_fun_def by simp

lemma Quotient_abs_induct:
  assumes "\<And>y. R y y \<Longrightarrow> P (Abs y)" shows "P x"
  using 1 assms unfolding Quotient_def by metis

end

text \<open>Generating transfer rules for total quotients.\<close>

context
  fixes R Abs Rep T
  assumes 1: "Quotient R Abs Rep T" and 2: "reflp R"
begin

lemma Quotient_left_total: "left_total T"
  using 1 2 unfolding Quotient_alt_def left_total_def reflp_def by auto

lemma Quotient_bi_total: "bi_total T"
  using 1 2 unfolding Quotient_alt_def bi_total_def reflp_def by auto

lemma Quotient_id_abs_transfer: "(op = ===> T) (\<lambda>x. x) Abs"
  using 1 2 unfolding Quotient_alt_def reflp_def rel_fun_def by simp

lemma Quotient_total_abs_induct: "(\<And>y. P (Abs y)) \<Longrightarrow> P x"
  using 1 2 unfolding Quotient_alt_def reflp_def by metis

lemma Quotient_total_abs_eq_iff: "Abs x = Abs y \<longleftrightarrow> R x y"
  using Quotient_rel [OF 1] 2 unfolding reflp_def by simp

end

text \<open>Generating transfer rules for a type defined with \<open>typedef\<close>.\<close>

context
  fixes Rep Abs A T
  assumes type: "type_definition Rep Abs A"
  assumes T_def: "T \<equiv> (\<lambda>(x::'a) (y::'b). x = Rep y)"
begin

lemma typedef_left_unique: "left_unique T"
  unfolding left_unique_def T_def
  by (simp add: type_definition.Rep_inject [OF type])

lemma typedef_bi_unique: "bi_unique T"
  unfolding bi_unique_def T_def
  by (simp add: type_definition.Rep_inject [OF type])

(* the following two theorems are here only for convinience *)

lemma typedef_right_unique: "right_unique T"
  using T_def type Quotient_right_unique typedef_to_Quotient
  by blast

lemma typedef_right_total: "right_total T"
  using T_def type Quotient_right_total typedef_to_Quotient
  by blast

lemma typedef_rep_transfer: "(T ===> op =) (\<lambda>x. x) Rep"
  unfolding rel_fun_def T_def by simp

end

text \<open>Generating the correspondence rule for a constant defined with
  \<open>lift_definition\<close>.\<close>

lemma Quotient_to_transfer:
  assumes "Quotient R Abs Rep T" and "R c c" and "c' \<equiv> Abs c"
  shows "T c c'"
  using assms by (auto dest: Quotient_cr_rel)

text \<open>Proving reflexivity\<close>

lemma Quotient_to_left_total:
  assumes q: "Quotient R Abs Rep T"
  and r_R: "reflp R"
  shows "left_total T"
using r_R Quotient_cr_rel[OF q] unfolding left_total_def by (auto elim: reflpE)

lemma Quotient_composition_ge_eq:
  assumes "left_total T"
  assumes "R \<ge> op="
  shows "(T OO R OO T\<inverse>\<inverse>) \<ge> op="
using assms unfolding left_total_def by fast

lemma Quotient_composition_le_eq:
  assumes "left_unique T"
  assumes "R \<le> op="
  shows "(T OO R OO T\<inverse>\<inverse>) \<le> op="
using assms unfolding left_unique_def by blast

lemma eq_onp_le_eq:
  "eq_onp P \<le> op=" unfolding eq_onp_def by blast

lemma reflp_ge_eq:
  "reflp R \<Longrightarrow> R \<ge> op=" unfolding reflp_def by blast

text \<open>Proving a parametrized correspondence relation\<close>

definition POS :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
"POS A B \<equiv> A \<le> B"

definition  NEG :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
"NEG A B \<equiv> B \<le> A"

lemma pos_OO_eq:
  shows "POS (A OO op=) A"
unfolding POS_def OO_def by blast

lemma pos_eq_OO:
  shows "POS (op= OO A) A"
unfolding POS_def OO_def by blast

lemma neg_OO_eq:
  shows "NEG (A OO op=) A"
unfolding NEG_def OO_def by auto

lemma neg_eq_OO:
  shows "NEG (op= OO A) A"
unfolding NEG_def OO_def by blast

lemma POS_trans:
  assumes "POS A B"
  assumes "POS B C"
  shows "POS A C"
using assms unfolding POS_def by auto

lemma NEG_trans:
  assumes "NEG A B"
  assumes "NEG B C"
  shows "NEG A C"
using assms unfolding NEG_def by auto

lemma POS_NEG:
  "POS A B \<equiv> NEG B A"
  unfolding POS_def NEG_def by auto

lemma NEG_POS:
  "NEG A B \<equiv> POS B A"
  unfolding POS_def NEG_def by auto

lemma POS_pcr_rule:
  assumes "POS (A OO B) C"
  shows "POS (A OO B OO X) (C OO X)"
using assms unfolding POS_def OO_def by blast

lemma NEG_pcr_rule:
  assumes "NEG (A OO B) C"
  shows "NEG (A OO B OO X) (C OO X)"
using assms unfolding NEG_def OO_def by blast

lemma POS_apply:
  assumes "POS R R'"
  assumes "R f g"
  shows "R' f g"
using assms unfolding POS_def by auto

text \<open>Proving a parametrized correspondence relation\<close>

lemma fun_mono:
  assumes "A \<ge> C"
  assumes "B \<le> D"
  shows   "(A ===> B) \<le> (C ===> D)"
using assms unfolding rel_fun_def by blast

lemma pos_fun_distr: "((R ===> S) OO (R' ===> S')) \<le> ((R OO R') ===> (S OO S'))"
unfolding OO_def rel_fun_def by blast

lemma functional_relation: "right_unique R \<Longrightarrow> left_total R \<Longrightarrow> \<forall>x. \<exists>!y. R x y"
unfolding right_unique_def left_total_def by blast

lemma functional_converse_relation: "left_unique R \<Longrightarrow> right_total R \<Longrightarrow> \<forall>y. \<exists>!x. R x y"
unfolding left_unique_def right_total_def by blast

lemma neg_fun_distr1:
assumes 1: "left_unique R" "right_total R"
assumes 2: "right_unique R'" "left_total R'"
shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S')) "
  using functional_relation[OF 2] functional_converse_relation[OF 1]
  unfolding rel_fun_def OO_def
  apply clarify
  apply (subst all_comm)
  apply (subst all_conj_distrib[symmetric])
  apply (intro choice)
  by metis

lemma neg_fun_distr2:
assumes 1: "right_unique R'" "left_total R'"
assumes 2: "left_unique S'" "right_total S'"
shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S'))"
  using functional_converse_relation[OF 2] functional_relation[OF 1]
  unfolding rel_fun_def OO_def
  apply clarify
  apply (subst all_comm)
  apply (subst all_conj_distrib[symmetric])
  apply (intro choice)
  by metis

subsection \<open>Domains\<close>

lemma composed_equiv_rel_eq_onp:
  assumes "left_unique R"
  assumes "(R ===> op=) P P'"
  assumes "Domainp R = P''"
  shows "(R OO eq_onp P' OO R\<inverse>\<inverse>) = eq_onp (inf P'' P)"
using assms unfolding OO_def conversep_iff Domainp_iff[abs_def] left_unique_def rel_fun_def eq_onp_def
fun_eq_iff by blast

lemma composed_equiv_rel_eq_eq_onp:
  assumes "left_unique R"
  assumes "Domainp R = P"
  shows "(R OO op= OO R\<inverse>\<inverse>) = eq_onp P"
using assms unfolding OO_def conversep_iff Domainp_iff[abs_def] left_unique_def eq_onp_def
fun_eq_iff is_equality_def by metis

lemma pcr_Domainp_par_left_total:
  assumes "Domainp B = P"
  assumes "left_total A"
  assumes "(A ===> op=) P' P"
  shows "Domainp (A OO B) = P'"
using assms
unfolding Domainp_iff[abs_def] OO_def bi_unique_def left_total_def rel_fun_def
by (fast intro: fun_eq_iff)

lemma pcr_Domainp_par:
assumes "Domainp B = P2"
assumes "Domainp A = P1"
assumes "(A ===> op=) P2' P2"
shows "Domainp (A OO B) = (inf P1 P2')"
using assms unfolding rel_fun_def Domainp_iff[abs_def] OO_def
by (fast intro: fun_eq_iff)

definition rel_pred_comp :: "('a => 'b => bool) => ('b => bool) => 'a => bool"
where "rel_pred_comp R P \<equiv> \<lambda>x. \<exists>y. R x y \<and> P y"

lemma pcr_Domainp:
assumes "Domainp B = P"
shows "Domainp (A OO B) = (\<lambda>x. \<exists>y. A x y \<and> P y)"
using assms by blast

lemma pcr_Domainp_total:
  assumes "left_total B"
  assumes "Domainp A = P"
  shows "Domainp (A OO B) = P"
using assms unfolding left_total_def
by fast

lemma Quotient_to_Domainp:
  assumes "Quotient R Abs Rep T"
  shows "Domainp T = (\<lambda>x. R x x)"
by (simp add: Domainp_iff[abs_def] Quotient_cr_rel[OF assms])

lemma eq_onp_to_Domainp:
  assumes "Quotient (eq_onp P) Abs Rep T"
  shows "Domainp T = P"
by (simp add: eq_onp_def Domainp_iff[abs_def] Quotient_cr_rel[OF assms])

end

(* needed for lifting_def_code_dt.ML (moved from Lifting_Set) *)
lemma right_total_UNIV_transfer:
  assumes "right_total A"
  shows "(rel_set A) (Collect (Domainp A)) UNIV"
  using assms unfolding right_total_def rel_set_def Domainp_iff by blast

subsection \<open>ML setup\<close>

ML_file "Tools/Lifting/lifting_util.ML"

named_theorems relator_eq_onp
  "theorems that a relator of an eq_onp is an eq_onp of the corresponding predicate"
ML_file "Tools/Lifting/lifting_info.ML"

(* setup for the function type *)
declare fun_quotient[quot_map]
declare fun_mono[relator_mono]
lemmas [relator_distr] = pos_fun_distr neg_fun_distr1 neg_fun_distr2

ML_file "Tools/Lifting/lifting_bnf.ML"
ML_file "Tools/Lifting/lifting_term.ML"
ML_file "Tools/Lifting/lifting_def.ML"
ML_file "Tools/Lifting/lifting_setup.ML"
ML_file "Tools/Lifting/lifting_def_code_dt.ML"

lemma pred_prod_beta: "pred_prod P Q xy \<longleftrightarrow> P (fst xy) \<and> Q (snd xy)"
by(cases xy) simp

lemma pred_prod_split: "P (pred_prod Q R xy) \<longleftrightarrow> (\<forall>x y. xy = (x, y) \<longrightarrow> P (Q x \<and> R y))"
by(cases xy) simp

hide_const (open) POS NEG

end


(*  Title:       Lifting Definition Option
    Author:      René Thiemann       <rene.thiemann@uibk.ac.at>
    Maintainer:  René Thiemann
    License:     LGPL
*)

(*
Copyright 2014 René Thiemann

This file is part of IsaFoR/CeTA.

IsaFoR/CeTA is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

IsaFoR/CeTA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with IsaFoR/CeTA. If not, see <http://www.gnu.org/licenses/>.
*)
theory Lifting_Definition_Option_Examples
imports
  Main
begin

section \<open>Examples\<close>

subsection \<open>A simple restricted type without type-parameters\<close>

typedef "restricted" = "{ i :: int. i mod 2 = 0}" morphisms base "restricted"
  by (intro exI[of _ 4]) auto
setup_lifting type_definition_restricted

text \<open>Let us start with just using a sufficient criterion for testing for even numbers,
  without actually generating them, i.e., where the generator is just the identity function.\<close>
lift_definition(code_dt) restricted_of_simple :: "int \<Rightarrow> restricted option" is
  "\<lambda> x :: int. if x \<in> {0, 2, 4, 6} then Some x else None" by auto

text \<open>We can also take several input arguments for the test, and generate a more complex value.\<close>
lift_definition(code_dt) restricted_of_many_args :: "nat \<Rightarrow> int \<Rightarrow> bool \<Rightarrow> restricted option" is
  "\<lambda> x y (b :: bool). if int x + y = 5 then Some ((int x + 1) * (y + 1)) else None"
by clarsimp presburger

text \<open>No problem to use type parameters.\<close>
lift_definition(code_dt) restricted_of_poly :: "'b list \<Rightarrow> restricted option" is
  "\<lambda> xs :: 'b list. if length xs = 2 then Some (int (length (xs))) else None" by auto

subsection \<open>Examples with type-parameters in the restricted type.\<close>

typedef 'f restrictedf = "{ xs :: 'f list. length xs < 3}" morphisms basef restrictedf
  by (intro exI[of _ Nil]) auto
setup_lifting type_definition_restrictedf

text \<open>It does not matter, if we take the same or different type-parameters in the lift-definition.\<close>
lift_definition(code_dt) test1 :: "'g \<Rightarrow> nat \<Rightarrow> 'g restrictedf option" is
  "\<lambda> (e :: 'g) x. if x < 2 then Some (replicate x e) else None" by auto

lift_definition(code_dt) test2 :: "'f \<Rightarrow> nat \<Rightarrow> 'f restrictedf option" is
  "\<lambda> (e :: 'f) x. if x < 2 then Some (replicate x e) else None" by auto

text \<open>Tests with multiple type-parameters.\<close>

typedef ('a,'f) restr = "{ (xs :: 'a list,ys :: 'f list) . length xs = length ys}"
  morphisms base' restr
  by (rule exI[of _ "([], [])"], auto)
setup_lifting type_definition_restr

lift_definition(code_dt) restr_of_pair :: "'g \<Rightarrow> 'e list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> ('e,nat) restr option" is
  "\<lambda> (z :: 'g) (xs :: 'e list) (y :: nat) n. if length xs = n then Some (xs,replicate n y) else None"
by auto

subsection \<open>Example from \isafor/\ceta\<close>

text \<open>An argument filter is a mapping @{term \<pi>} from n-ary function symbols into
lists of positions, i.e., where each position is between 0 and n-1. In \isafor,
(Isabelle's Formalization of Rewriting) and \ceta
\cite{DBLP:conf/tphol/ThiemannS09}, the corresponding certifier for
term rewriting related properties,
this is modelled as follows, where a partial argument filter in a map is extended to a full one
by means of an default filter.\<close>

typedef 'f af = "{ (\<pi> :: 'f \<times> nat \<Rightarrow> nat list). (\<forall> f n. set (\<pi> (f,n)) \<subseteq> {0 ..< n})}"
  morphisms af Abs_af by (rule exI[of _ "\<lambda> _. []"], auto)

setup_lifting type_definition_af

type_synonym 'f af_impl = "(('f \<times> nat) \<times> nat list)list"

fun fun_of_map_fun :: "('a \<Rightarrow> 'b option) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)" where
  "fun_of_map_fun m f a = (case m a of Some b \<Rightarrow> b | None \<Rightarrow> f a)"

lift_definition(code_dt) af_of :: "'f af_impl \<Rightarrow> 'f af option" is
  "\<lambda> s :: 'f af_impl. if (\<forall> fidx \<in> set s. (\<forall> i \<in> set (snd fidx). i < snd (fst fidx)))
     then Some (fun_of_map_fun (map_of s) (\<lambda> (f,n). [0 ..< n])) else None"
using map_of_SomeD by (fastforce split: option.splits)

subsection \<open>Code generation tests and derived theorems\<close>
export_code
  restricted_of_many_args
  restricted_of_simple
  restricted_of_poly
  test1
  test2
  restr_of_pair
  af_of
in Haskell

lemma restricted_of_simple_Some:
  "restricted_of_simple x = Some r \<Longrightarrow> base r = x"
using restricted_of_simple.rep_eq[of x]
apply (split if_splits)
apply (simp_all only: option.map option.inject option.simps(3))
done

end


(*
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 \<Rightarrow> 'm2"

abbreviation
  NatTransApp :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans \<Rightarrow> 'o1 \<Rightarrow> 'm2" (infixr "$$" 70) where
  "NatTransApp \<eta> X \<equiv> (NatTransMap \<eta>) X"

definition  "NTCatDom \<eta> \<equiv> CatDom (NTDom \<eta>)"
definition  "NTCatCod \<eta> \<equiv> CatCod (NTCod \<eta>)"

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

locale NatTransP =
  fixes \<eta> :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" (structure)
  assumes NatTransFtor:   "Functor (NTDom \<eta>)"
  and     NatTransFtor2:  "Functor (NTCod \<eta>)"
  and     NatTransFtorDom:   "NTCatDom \<eta> = CatDom (NTCod \<eta>)"
  and     NatTransFtorCod:   "NTCatCod \<eta> = CatCod (NTDom \<eta>)"
  and    NatTransMapsTo:  "X \<in> obj\<^bsub>NTCatDom \<eta>\<^esub> \<Longrightarrow>
                           (\<eta> $$ X) maps\<^bsub>NTCatCod \<eta>\<^esub> ((NTDom \<eta>) @@ X) to ((NTCod \<eta>) @@ X)"
  and    NatTrans:  "f maps\<^bsub>NTCatDom \<eta>\<^esub> X to Y \<Longrightarrow>
                     ((NTDom \<eta>) ## f) ;;\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ Y) = (\<eta> $$ X) ;;\<^bsub>NTCatCod \<eta>\<^esub> ((NTCod \<eta>) ## f)"

locale NatTrans = NatTransP + NatTransExt

definition MakeNT :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans \<Rightarrow> ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" where
"MakeNT \<eta> \<equiv> \<lparr>
      NTDom = NTDom \<eta> ,
      NTCod = NTCod \<eta> ,
      NatTransMap = restrict (NatTransMap \<eta>) (Obj (NTCatDom \<eta>))
  \<rparr>"

definition
  nt_abbrev ("NT _ : _ \<Longrightarrow> _" [81]) where
  "NT f : F \<Longrightarrow> G \<equiv> (NatTrans f) \<and> (NTDom f = F) \<and> (NTCod f = G)"

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

lemma MakeNT: "NatTransP \<eta> \<Longrightarrow> NatTrans (MakeNT \<eta>)"
  by(auto simp add: NatTransP_def NatTrans_def MakeNT_def NTCatDom_def NTCatCod_def Category.MapsToObj
    NatTransExt_def)

lemma MakeNT_comp: "X \<in> Obj (NTCatDom f) \<Longrightarrow> (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 \<in> Obj (NTCatDom (MakeNT f)) \<Longrightarrow> f $$ X = (MakeNT f) $$ X"
by(simp add: MakeNT_def NTCatDom_def)

lemma NatTransMapsTo:
  assumes "NT \<eta> : F \<Longrightarrow> G" and "X \<in> Obj (CatDom F)"
  shows "\<eta> $$ X maps\<^bsub>CatCod G \<^esub>(F @@ X) to (G @@ X)"
proof-
  have NTP: "NatTransP \<eta>" using assms by auto
  have NTC: "NTCatCod \<eta> = CatCod G" using assms by (auto simp add: NTCatCod_def)
  have NTD: "NTCatDom \<eta> = CatDom F" using assms by (auto simp add: NTCatDom_def)
  hence Obj: "X \<in> Obj (NTCatDom \<eta>)" using assms by simp
  have DF: "NTDom \<eta> = F" and CG: "NTCod \<eta> = G" using assms by auto
  have NTmapsTo: "\<eta> $$ X maps\<^bsub>NTCatCod \<eta> \<^esub>((NTDom \<eta>) @@ X) to ((NTCod \<eta>) @@ 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
                      \<Rightarrow> ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans \<Rightarrow> bool" (infixl "\<approx>>\<bullet>" 65) where
  "NTCompDefined \<eta>1 \<eta>2 \<equiv> NatTrans \<eta>1 \<and> NatTrans \<eta>2 \<and> NTCatDom \<eta>2 = NTCatDom \<eta>1 \<and>
                         NTCatCod \<eta>2 = NTCatCod \<eta>1 \<and> NTCod \<eta>1 = NTDom \<eta>2"

lemma NTCompDefinedE[elim]: "\<lbrakk>\<eta>1 \<approx>>\<bullet> \<eta>2 ; \<lbrakk>NatTrans \<eta>1 ; NatTrans \<eta>2 ; NTCatDom \<eta>2 = NTCatDom \<eta>1 ;
                         NTCatCod \<eta>2 = NTCatCod \<eta>1 ; NTCod \<eta>1 = NTDom \<eta>2\<rbrakk> \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
by (simp add: NTCompDefined_def)

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

lemma NatTransExt:
  assumes "NatTrans \<eta>1" and "NatTrans \<eta>2" and "NTDom \<eta>1 = NTDom \<eta>2" and "NTCod \<eta>1 = NTCod \<eta>2"
  and     "\<And>X . X \<in> Obj (NTCatDom \<eta>1) \<Longrightarrow> \<eta>1 $$ X = \<eta>2 $$ X"
  shows   "\<eta>1 = \<eta>2"
proof-
  have "NatTransMap \<eta>1 \<in> extensional (Obj (NTCatDom \<eta>1))" and
       "NatTransMap \<eta>2 \<in> extensional (Obj (NTCatDom \<eta>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 \<Rightarrow> ('o1, 'o2, 'm1, 'm2, 'a1, 'a2) NatTrans" where
  "IdNatTrans' F \<equiv> \<lparr>
      NTDom = F ,
      NTCod = F ,
      NatTransMap = \<lambda> X . id\<^bsub>CatCod F\<^esub> (F @@ X)
  \<rparr>"

definition "IdNatTrans F \<equiv> MakeNT(IdNatTrans' F)"

lemma IdNatTrans_map: "X \<in> obj\<^bsub>CatDom F\<^esub> \<Longrightarrow> (IdNatTrans F) $$ X = id\<^bsub>CatCod F\<^esub> (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 \<Longrightarrow> 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 maps\<^bsub>CatDom F\<^esub> X to Y"
    show "(F ## f) ;;\<^bsub>CatCod F\<^esub> (id\<^bsub>CatCod F\<^esub> (F @@ Y)) = (id\<^bsub>CatCod F\<^esub> (F @@ X)) ;;\<^bsub>CatCod F\<^esub> (F ## f)"
    proof-
      have 1: "Category (CatCod F)" using a by simp
      have "F ## f maps\<^bsub>CatCod F\<^esub> (F @@ X) to (F @@ Y)" using a b by (auto simp add: Functor.FunctorMapsTo)
      hence 2: "F ## f \<in> mor\<^bsub>CatCod F\<^esub>" and 3: "cod\<^bsub>CatCod F\<^esub> (F ## f) = (F @@ Y)"
        and 4: "dom\<^bsub>CatCod F\<^esub> (F ## f) = (F @@ X)" by auto
      have "(F ## f) ;;\<^bsub>CatCod F\<^esub> (id\<^bsub>CatCod F\<^esub> (F @@ Y)) = (F ## f) ;;\<^bsub>CatCod F\<^esub> (id\<^bsub>CatCod F\<^esub> (cod\<^bsub>CatCod F\<^esub> (F ## f)))"
        using 3 by simp
      also have "... = F ## f" using 1 2 by (auto simp add: Category.Cidr)
      also have "... = (id\<^bsub>CatCod F\<^esub> (dom\<^bsub>CatCod F\<^esub> (F ## f))) ;;\<^bsub>CatCod F\<^esub> (F ## f)"
        using 1 2 by (auto simp add: Category.Cidl)
      also have "... = (id\<^bsub>CatCod F\<^esub> (F @@ X)) ;;\<^bsub>CatCod F\<^esub> (F ## f)" using 4 by simp
      finally show ?thesis .
    qed
  }
qed

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

definition
  NatTransComp' :: "('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans \<Rightarrow>
                   ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans \<Rightarrow>
                   ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans" (infixl "\<bullet>1" 75) where
  "NatTransComp' \<eta>1 \<eta>2 = \<lparr>
      NTDom = NTDom \<eta>1 ,
      NTCod = NTCod \<eta>2 ,
      NatTransMap = \<lambda> X . (\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X)
  \<rparr>"

definition NatTransComp (infixl "\<bullet>" 75) where "\<eta>1 \<bullet> \<eta>2 \<equiv> MakeNT(\<eta>1 \<bullet>1 \<eta>2)"

lemma NatTransComp_Comp1: "\<lbrakk>x \<in> Obj (NTCatDom f) ; f \<approx>>\<bullet> g\<rbrakk> \<Longrightarrow> (f \<bullet> g) $$ x = (f $$ x) ;;\<^bsub>NTCatCod g\<^esub> (g $$ x)"
by(auto simp add: NatTransComp_def NatTransComp'_def MakeNT_def NTCatCod_def NTCatDom_def)

lemma NatTransComp_Comp2: "\<lbrakk>x \<in> Obj (NTCatDom f) ; f \<approx>>\<bullet> g\<rbrakk> \<Longrightarrow> (f \<bullet> g) $$ x = (f $$ x) ;;\<^bsub>NTCatCod f\<^esub> (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]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NatTrans \<eta>1" by auto
lemma [simp]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NatTrans \<eta>2" by auto
lemma NTCatDom:        "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatDom \<eta>1 = NTCatDom \<eta>2" by auto
lemma NTCatCod:        "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatCod \<eta>1 = NTCatCod \<eta>2" by auto
lemma [simp]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatDom (\<eta>1 \<bullet>1 \<eta>2) = NTCatDom \<eta>1" by (auto simp add: NatTransComp'_def NTCatDom_def)
lemma [simp]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatCod (\<eta>1 \<bullet>1 \<eta>2) = NTCatCod \<eta>1" by (auto simp add: NatTransComp'_def NTCatCod_def)
lemma [simp]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatDom (\<eta>1 \<bullet> \<eta>2) = NTCatDom \<eta>1" by (auto simp add: NatTransComp_defs)
lemma [simp]: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NTCatCod (\<eta>1 \<bullet> \<eta>2) = NTCatCod \<eta>1" by (auto simp add: NatTransComp_defs)
lemma [simp]: "NatTrans \<eta> \<Longrightarrow> Category(NTCatDom \<eta>)" by (simp add:  NatTransP.NatTransFtor NTCatDom_def)
lemma [simp]: "NatTrans \<eta> \<Longrightarrow> Category(NTCatCod \<eta>)" 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 \<Longrightarrow> (IdNatTrans (NTDom f)) \<approx>>\<bullet> f"
apply(rule NTCompDefinedI)
apply(simp_all add: IdNatTransNatTrans NatTransP.NatTransFtor)
apply(simp_all add: IdNatTrans_defs CCCD)
done

lemma IdNatTransCompDefCod: "NatTrans f \<Longrightarrow> f \<approx>>\<bullet> (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 \<eta>" and "f maps\<^bsub>NTCatDom \<eta>\<^esub> X to Y"
  shows "(\<eta> $$ X) \<approx>>\<^bsub>NTCatCod \<eta>\<^esub> (NTCod \<eta> ## f)"
proof(rule CompDefinedI)
  have b: "X \<in> obj\<^bsub>NTCatDom \<eta>\<^esub>" and c: "Y \<in> obj\<^bsub>NTCatDom \<eta>\<^esub>" using assms by (auto simp add: Category.MapsToObj)
  have d: "(\<eta> $$ X) maps\<^bsub>NTCatCod \<eta>\<^esub> ((NTDom \<eta>) @@ X) to ((NTCod \<eta>) @@ X)" using assms b
    by (simp add: NatTransP.NatTransMapsTo)
  thus "\<eta> $$ X \<in> mor\<^bsub>NTCatCod \<eta>\<^esub>" by auto
  have "f maps\<^bsub>CatDom (NTCod \<eta>)\<^esub> X to Y" using assms by (simp add: NatTransP.NatTransFtorDom)
  hence e: "NTCod \<eta> ## f maps\<^bsub>CatCod (NTCod \<eta>)\<^esub> (NTCod \<eta> @@ X) to (NTCod \<eta> @@ Y)" using assms
    by (simp add: FunctorM.FunctorCompM NatTransP.NatTransFtor2)
  thus "NTCod \<eta> ## f \<in> mor\<^bsub>NTCatCod \<eta>\<^esub>" by (auto simp add: NTCatCod_def)
  have "cod\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ X) = (NTCod \<eta> @@ X)" using d by auto
  also have "... = dom\<^bsub>CatCod (NTCod \<eta>)\<^esub> (NTCod \<eta> ## f)" using e by auto
  finally show "cod\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ X) = dom\<^bsub>NTCatCod \<eta>\<^esub> (NTCod \<eta> ## f)" by (auto simp add: NTCatCod_def)
qed

lemma NatTransCompDefDom:
  assumes "NatTrans \<eta>" and "f maps\<^bsub>NTCatDom \<eta>\<^esub> X to Y"
  shows "(NTDom \<eta> ## f)  \<approx>>\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ Y)"
proof(rule CompDefinedI)
  have b: "X \<in> obj\<^bsub>NTCatDom \<eta>\<^esub>" and c: "Y \<in> obj\<^bsub>NTCatDom \<eta>\<^esub>" using assms by (auto simp add: Category.MapsToObj)
  have d: "(\<eta> $$ Y) maps\<^bsub>NTCatCod \<eta>\<^esub> ((NTDom \<eta>) @@ Y) to ((NTCod \<eta>) @@ Y)" using assms c
    by (simp add: NatTransP.NatTransMapsTo)
  thus "\<eta> $$ Y \<in> mor\<^bsub>NTCatCod \<eta>\<^esub>" by auto
  have "f maps\<^bsub>CatDom (NTDom \<eta>)\<^esub> X to Y" using assms by (simp add: NTCatDom_def)
  hence e: "NTDom \<eta> ## f maps\<^bsub>CatCod (NTDom \<eta>)\<^esub> (NTDom \<eta> @@ X) to (NTDom \<eta> @@ Y)" using assms
    by (simp add: FunctorM.FunctorCompM NatTransP.NatTransFtor)
  thus "NTDom \<eta> ## f \<in> mor\<^bsub>NTCatCod \<eta>\<^esub>" using assms by (auto simp add: NatTransP.NatTransFtorCod)
  have "dom\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ Y) = (NTDom \<eta> @@ Y)" using d by auto
  also have "... = cod\<^bsub>CatCod (NTDom \<eta>)\<^esub> (NTDom \<eta> ## f)" using e by auto
  finally show "cod\<^bsub>NTCatCod \<eta>\<^esub> (NTDom \<eta> ## f) = dom\<^bsub>NTCatCod \<eta>\<^esub> (\<eta> $$ Y)"
    using assms by (auto simp add: NatTransP.NatTransFtorCod)
qed

lemma NatTransCompCompDef:
  assumes "\<eta>1 \<approx>>\<bullet> \<eta>2" and "X \<in> obj\<^bsub>NTCatDom \<eta>1\<^esub>"
  shows "(\<eta>1 $$ X) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X)"
proof(rule CompDefinedI)
  have 1: "(\<eta>1 $$ X) maps\<^bsub>NTCatCod \<eta>1\<^esub> ((NTDom \<eta>1) @@ X) to ((NTCod \<eta>1) @@ X)" using assms
    by (simp add: NatTransP.NatTransMapsTo)
  have "NTCatCod \<eta>1 = NTCatCod \<eta>2" using assms by auto
  hence 2: "(\<eta>2 $$ X) maps\<^bsub>NTCatCod \<eta>1\<^esub> ((NTDom \<eta>2) @@ X) to ((NTCod \<eta>2) @@ X)" using assms
    by (simp add: NatTransP.NatTransMapsTo NTCatDom)
  show "\<eta>1 $$ X \<in> mor\<^bsub>NTCatCod \<eta>1\<^esub>"
    and "\<eta>2 $$ X \<in> mor\<^bsub>NTCatCod \<eta>1\<^esub>"  using 1 2 by auto
  have "cod\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>1 $$ X) = ((NTCod \<eta>1) @@ X)" using 1 by auto
  also have "... = ((NTDom \<eta>2) @@ X)" using assms by auto
  finally show "cod\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>1 $$ X) = dom\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X)" using 2 by auto
qed

lemma NatTransCompNatTrans':
  assumes "\<eta>1 \<approx>>\<bullet> \<eta>2"
  shows   "NatTransP (\<eta>1 \<bullet>1 \<eta>2)"
proof(auto simp add: NatTransP_def)
  show "Functor (NTDom (\<eta>1 \<bullet>1 \<eta>2))" and "Functor (NTCod (\<eta>1 \<bullet>1 \<eta>2))" using assms
    by (auto simp add: NatTransComp'_def NatTransP.NatTransFtor NatTransP.NatTransFtor2)
  show "NTCatDom (\<eta>1 \<bullet>1 \<eta>2) = CatDom (NTCod (\<eta>1 \<bullet>1 \<eta>2))" and
       "NTCatCod (\<eta>1 \<bullet>1 \<eta>2) = CatCod (NTDom (\<eta>1 \<bullet>1 \<eta>2))"
  proof (auto simp add: NatTransComp'_def NTCatCod_def NTCatDom_def)
    have "CatDom (NTDom \<eta>1) = NTCatDom \<eta>1" by (simp add: NTCatDom_def)
    thus "CatDom (NTDom \<eta>1) = CatDom (NTCod \<eta>2)" using assms by (auto simp add: NatTransP.NatTransFtorDom)
    have "CatCod (NTCod \<eta>2) = NTCatCod \<eta>2" by (simp add: NTCatCod_def)
    thus "CatCod (NTCod \<eta>2) = CatCod (NTDom \<eta>1)" using assms by (auto simp add: NatTransP.NatTransFtorCod)
  qed
  {
    fix X assume aa: "X \<in> obj\<^bsub>NTCatDom (\<eta>1 \<bullet>1 \<eta>2)\<^esub>"
    show "(\<eta>1 \<bullet>1 \<eta>2) $$ X maps\<^bsub>NTCatCod (\<eta>1 \<bullet>1 \<eta>2)\<^esub> NTDom (\<eta>1 \<bullet>1 \<eta>2) @@ X to NTCod (\<eta>1 \<bullet>1 \<eta>2) @@ X"
    proof-
      have "X \<in> obj\<^bsub>NTCatDom \<eta>1\<^esub>" and "NatTrans \<eta>1" using assms aa by simp+
      hence "(\<eta>1 $$ X) maps\<^bsub>NTCatCod \<eta>1\<^esub> ((NTDom \<eta>1) @@ X) to ((NTCod \<eta>1) @@ X)"
        by (simp add: NatTransP.NatTransMapsTo)
      moreover have "(\<eta>2 $$ X) maps\<^bsub>NTCatCod \<eta>1\<^esub> ((NTCod \<eta>1) @@ X) to ((NTCod \<eta>2) @@ X)"
      proof-
        have "X \<in> obj\<^bsub>NTCatDom \<eta>2\<^esub>" and "NatTrans \<eta>2" using assms aa by auto
        hence "(\<eta>2 $$ X) maps\<^bsub>NTCatCod \<eta>2\<^esub> ((NTDom \<eta>2) @@ X) to ((NTCod \<eta>2) @@ X)"
          by (simp add: NatTransP.NatTransMapsTo)
        thus ?thesis using assms by auto
      qed
      ultimately have "(\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X) maps\<^bsub>NTCatCod \<eta>1\<^esub> ((NTDom \<eta>1) @@ X) to ((NTCod \<eta>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\<^bsub>(NTCatDom (\<eta>1 \<bullet>1 \<eta>2))\<^esub> X to Y"
    show "(NTDom (\<eta>1 \<bullet>1 \<eta>2) ## f) ;;\<^bsub>NTCatCod (\<eta>1 \<bullet>1 \<eta>2)\<^esub> (\<eta>1 \<bullet>1 \<eta>2 $$ Y) =
       ((\<eta>1 \<bullet>1 \<eta>2) $$ X) ;;\<^bsub>NTCatCod (\<eta>1 \<bullet>1 \<eta>2)\<^esub> (NTCod (\<eta>1 \<bullet>1 \<eta>2) ## f)"
    proof-
      have b: "X \<in> obj\<^bsub>NTCatDom \<eta>1\<^esub>" and c: "Y \<in> obj\<^bsub>NTCatDom \<eta>1\<^esub>" using assms a by (auto simp add: Category.MapsToObj)
      have "((NTDom \<eta>1) ## f) ;;\<^bsub>NTCatCod \<eta>1\<^esub> ((\<eta>1 $$ Y) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ Y)) =
            (((NTDom \<eta>1) ## f) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>1 $$ Y)) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ Y)"
      proof-
        have "((NTDom \<eta>1) ## f) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>1 $$ Y)" using assms a by (auto simp add: NatTransCompDefDom)
        moreover have "(\<eta>1 $$ Y) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub>  (\<eta>2 $$ Y)" using assms by (simp add: NatTransCompCompDef c)
        ultimately show ?thesis using assms by (simp add: Category.Cassoc)
      qed
      also have "... = ((\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> ((NTDom \<eta>2) ## f)) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ Y)"
        using assms a by (auto simp add: NatTransP.NatTrans)
      also have "... = (\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (((NTDom \<eta>2) ## f) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ Y))"
      proof-
        have "(\<eta>1 $$ X) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> ((NTCod \<eta>1) ## f)" using assms a by (simp add: NatTransCompDefCod)
        moreover have "((NTDom \<eta>2) ## f) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>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 "... = (\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> ((\<eta>2 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> ((NTCod \<eta>2) ## f))"
        using assms a by (simp add: NatTransP.NatTrans NTCatDom NTCatCod)
      also have "... = (\<eta>1 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X) ;;\<^bsub>NTCatCod \<eta>1\<^esub> ((NTCod \<eta>2) ## f)"
      proof-
        have "(\<eta>1 $$ X) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> (\<eta>2 $$ X)" using assms by (simp add: NatTransCompCompDef b)
        moreover have "(\<eta>2 $$ X) \<approx>>\<^bsub>NTCatCod \<eta>1\<^esub> ((NTCod \<eta>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: "\<eta>1 \<approx>>\<bullet> \<eta>2 \<Longrightarrow> NatTrans (\<eta>1 \<bullet> \<eta>2)"
by (simp add: NatTransCompNatTrans' NatTransComp_def MakeNT)

definition
  CatExp' :: "('o1,'m1,'a) Category_scheme \<Rightarrow> ('o2,'m2,'b) Category_scheme \<Rightarrow>
                     (('o1, 'o2, 'm1, 'm2, 'a, 'b) Functor,
                      ('o1, 'o2, 'm1, 'm2, 'a, 'b) NatTrans) Category"  where
  "CatExp' A B \<equiv> \<lparr>
      Category.Obj = {F . Ftor F : A \<longrightarrow> B} ,
      Category.Mor = {\<eta> . NatTrans \<eta> \<and> NTCatDom \<eta> = A \<and> NTCatCod \<eta> = B} ,
      Category.Dom = NTDom ,
      Category.Cod = NTCod ,
      Category.Id  = IdNatTrans ,
      Category.Comp = \<lambda>f g. (f \<bullet> g)
  \<rparr>"

definition "CatExp A B \<equiv> MakeCat(CatExp' A B)"

lemma IdNatTransMapL:
  assumes NT: "NatTrans f"
  shows "IdNatTrans (NTDom f) \<bullet> f = f"
proof(rule NatTransExt)
  show "NatTrans f" using assms .
  show "NatTrans (IdNatTrans (NTDom f) \<bullet> f)" using NT
    by (simp add: NatTransP.NatTransFtor IdNatTransNatTrans IdNatTransCompDefDom NatTransCompNatTrans)
  show "NTDom (IdNatTrans (NTDom f) \<bullet> f) = NTDom f" and
    "NTCod (IdNatTrans (NTDom f) \<bullet> f) = NTCod f" by (simp add: IdNatTrans_defs NatTransComp_defs)+
  {
    fix x assume aa: "x \<in> Obj (NTCatDom (IdNatTrans (NTDom f) \<bullet> f))"
    show "(IdNatTrans (NTDom f) \<bullet> f) $$ x = f $$ x"
    proof-
      have XObj: "x \<in> Obj(NTCatDom f)" using aa by (simp add: IdNatTrans_defs NatTransComp_defs)
      have fMap: "f $$ x maps\<^bsub>NTCatCod f\<^esub> NTDom f @@ x to NTCod f @@ x" using NT XObj
        by (simp add: NatTransP.NatTransMapsTo)
      have "(IdNatTrans (NTDom f) \<bullet> f) $$ x = (IdNatTrans (NTDom f) $$ x) ;;\<^bsub>NTCatCod f \<^esub>(f $$ x)"
      proof(rule NatTransComp_Comp1)
        show "x \<in> obj\<^bsub>NTCatDom (IdNatTrans (NTDom f))\<^esub>" using XObj by (simp add: IdNatTrans_defs)
        show "IdNatTrans (NTDom f) \<approx>>\<bullet> f" using NT by (simp add: IdNatTransCompDefDom)
      qed
      also have "... = id\<^bsub>NTCatCod f\<^esub> (dom\<^bsub>NTCatCod f\<^esub> (f $$ x)) ;;\<^bsub>NTCatCod f \<^esub>(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 \<in> mor\<^bsub>NTCatCod f\<^esub>" 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 \<bullet> IdNatTrans (NTCod f) = f"
proof(rule NatTransExt)
  show "NatTrans f" using assms .
  show "NatTrans (f \<bullet> IdNatTrans (NTCod f))" using NT
    by (simp add: NatTransP.NatTransFtor IdNatTransNatTrans IdNatTransCompDefCod NatTransCompNatTrans)
  show "NTDom (f \<bullet> IdNatTrans (NTCod f)) = NTDom f" and
    "NTCod (f \<bullet> IdNatTrans (NTCod f)) = NTCod f" by (simp add: IdNatTrans_defs NatTransComp_defs)+
  {
    fix x assume aa: "x \<in> Obj (NTCatDom (f \<bullet> IdNatTrans (NTCod f)))"
    show "(f \<bullet> IdNatTrans (NTCod f)) $$ x = f $$ x"
    proof-
      have XObj: "x \<in> Obj(NTCatDom f)" using aa by (simp add:  NatTransComp_defs)
      have fMap: "f $$ x maps\<^bsub>NTCatCod f\<^esub> NTDom f @@ x to NTCod f @@ x" using NT XObj
        by (simp add: NatTransP.NatTransMapsTo)
      have "(f \<bullet> IdNatTrans (NTCod f)) $$ x = (f $$ x) ;;\<^bsub>NTCatCod f\<^esub> (IdNatTrans (NTCod f) $$ x)"
        using XObj NT by (auto simp add: NatTransComp_Comp2 IdNatTransCompDefCod)
      also have "... = (f $$ x) ;;\<^bsub>NTCatCod f\<^esub> (id\<^bsub>NTCatCod f\<^esub> (cod\<^bsub>NTCatCod f\<^esub> (f $$ x)))"
      proof-
        have "x \<in> obj\<^bsub>CatDom (NTCod f)\<^esub>" using XObj NT by (simp add: IdNatTrans_defs DDDC)
        moreover have "(cod\<^bsub>NTCatCod f\<^esub> (f $$ x)) = (NTCod f) @@ x" using fMap by auto
        ultimately have "(IdNatTrans (NTCod f) $$ x) = (id\<^bsub>NTCatCod f\<^esub> (cod\<^bsub>NTCatCod f\<^esub> (f $$ x)))"
          by (simp add: IdNatTrans_map NTCatCod_def)
        thus ?thesis by simp
      qed
      also have "... = f $$ x"
      proof-
        have "f $$ x \<in> mor\<^bsub>NTCatCod f\<^esub>" using fMap by (auto)
        thus ?thesis using NT by (simp add: Category.Cidr)
      qed
      finally show ?thesis .
    qed
  }
qed

lemma NatTransCompDefined:
  assumes "f \<approx>>\<bullet> g" and "g \<approx>>\<bullet> h"
  shows "(f \<bullet> g) \<approx>>\<bullet> h" and "f \<approx>>\<bullet> (g \<bullet> h)"
proof-
  show "(f \<bullet> g) \<approx>>\<bullet> h"
  proof(rule NTCompDefinedI)
    show "NatTrans (f \<bullet> 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 \<bullet> g)" by (simp add: NatTransComp_defs)
    have "NTCatCod h = NTCatCod g" using assms by (simp add: NTCatCod)
    thus "NTCatCod h = NTCatCod (f \<bullet> g)" by ( simp add: NatTransComp_defs)
    show "NTCod (f \<bullet> g) = NTDom h" using assms by (auto simp add: NatTransComp_defs)
  qed
  show "f \<approx>>\<bullet> (g \<bullet> h)"
  proof(rule NTCompDefinedI)
    show "NatTrans f" and "NatTrans (g \<bullet> h)" using assms by (auto simp add: NatTransCompNatTrans)
    have "NTCatDom f = NTCatDom g" using assms by (simp add: NTCatDom)
    thus "NTCatDom (g \<bullet> h) = NTCatDom f" by (simp add: NatTransComp_defs)
    have "NTCatCod h = NTCatCod f" using assms by (simp add: NTCatCod)
    thus "NTCatCod (g \<bullet> h) = NTCatCod f" by ( simp add: NatTransComp_defs)
    show "NTCod f = NTDom (g \<bullet> h)" using assms by (auto simp add: NatTransComp_defs)
  qed
qed

lemma NatTransCompAssoc:
  assumes "f \<approx>>\<bullet> g" and "g \<approx>>\<bullet> h"
  shows "(f \<bullet> g) \<bullet> h = f \<bullet> (g \<bullet> h)"
proof(rule NatTransExt)
  show "NatTrans ((f \<bullet> g) \<bullet> h)" using assms by (simp add: NatTransCompNatTrans NatTransCompDefined)
  show "NatTrans (f \<bullet> (g \<bullet> h))" using assms by (simp add: NatTransCompNatTrans NatTransCompDefined)
  show "NTDom (f \<bullet> g \<bullet> h) = NTDom (f \<bullet> (g \<bullet> h))" and "NTCod (f \<bullet> g \<bullet> h) = NTCod (f \<bullet> (g \<bullet> h))"
    by(simp add: NatTransComp_defs)+
  {
    fix x assume aa: "x \<in> obj\<^bsub>NTCatDom (f \<bullet> g \<bullet> h)\<^esub>" show "((f \<bullet> g) \<bullet> h) $$ x = (f \<bullet> (g \<bullet> h)) $$ x"
    proof-
      have ntd1: "NTCatDom (f \<bullet> g) = NTCatDom (f \<bullet> g \<bullet> h)" and ntd2: "NTCatDom f = NTCatDom (f \<bullet> g \<bullet> h)"
        using assms by (simp add: NatTransCompDefined)+
      have obj1: "x \<in> Obj (NTCatDom f)" using aa ntd2 by simp
      have  1: "(f \<bullet> g) $$ x = (f $$ x) ;;\<^bsub>NTCatCod h\<^esub> (g $$ x)" and
            2: "(g \<bullet> h) $$ x = (g $$ x) ;;\<^bsub>NTCatCod h\<^esub> (h $$ x)" using obj1
        using assms by (auto simp add: NatTransComp_Comp1)
      have "((f \<bullet> g) \<bullet> h) $$ x = ((f \<bullet> g) $$ x) ;;\<^bsub>NTCatCod h\<^esub> (h $$ x)"
      proof(rule NatTransComp_Comp1)
        show "x \<in> obj\<^bsub>NTCatDom (f \<bullet> g)\<^esub>" using aa ntd1 by simp
        show "f \<bullet> g \<approx>>\<bullet> h" using assms by (simp add: NatTransCompDefined)
      qed
      also have "... = ((f $$ x) ;;\<^bsub>NTCatCod h\<^esub> (g $$ x)) ;;\<^bsub>NTCatCod h\<^esub> (h $$ x)" using 1 by simp
      also have "... = (f $$ x) ;;\<^bsub>NTCatCod h\<^esub> ((g $$ x) ;;\<^bsub>NTCatCod h\<^esub> (h $$ x))"
      proof-
        have 1: "NTCatCod h = NTCatCod f" and 2: "NTCatCod h = NTCatCod g" using assms by (simp add: NTCatCod)+
        hence "(f $$ x) \<approx>>\<^bsub>NTCatCod h\<^esub> (g $$ x)" using obj1 assms by (simp add: NatTransCompCompDef)
        moreover have "(g $$ x) \<approx>>\<^bsub>NTCatCod h\<^esub> (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) ;;\<^bsub>NTCatCod h\<^esub> ((g \<bullet> h) $$ x)" using 2 by simp
      also have "... = (f \<bullet> (g \<bullet> h)) $$ x"
      proof-
        have "NTCatCod f = NTCatCod h" using assms by (simp add: NTCatCod)
        moreover have "(f \<bullet> (g \<bullet> h)) $$ x = (f $$ x) ;;\<^bsub>NTCatCod f\<^esub> ((g \<bullet> h) $$ x)"
        proof(rule NatTransComp_Comp2)
          show "x \<in> obj\<^bsub>NTCatDom f\<^esub>" using obj1 assms by (simp add: NTCatDom)
          show "f \<approx>>\<bullet> g \<bullet> 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 \<in> mor\<^bsub>CatExp' A B\<^esub>"
    thus "dom\<^bsub>CatExp' A B\<^esub> f \<in> obj\<^bsub>CatExp' A B\<^esub>" and
         "cod\<^bsub>CatExp' A B\<^esub> f \<in> obj\<^bsub>CatExp' A B\<^esub>"
      by(auto simp add: CatExp'_def NatTransP.NatTransFtor
        NatTransP.NatTransFtor2 NatTransP.NatTransFtorDom NatTransP.NatTransFtorCod DDDC CCCD functor_abbrev_def)
  }
  {
    fix F assume a: "F \<in> obj\<^bsub>CatExp' A B\<^esub>"
    show "id\<^bsub>CatExp' A B\<^esub> F maps\<^bsub>CatExp' A B\<^esub> F to F"
    proof(rule MapsToI)
      have "Ftor F : A \<longrightarrow> B" using a by (simp add: CatExp'_def)
      thus "id\<^bsub>CatExp' A B\<^esub> F \<in> mor\<^bsub>CatExp' A B\<^esub>"
        apply(simp add: CatExp'_def NTCatDom_def NTCatCod_def IdNatTransNatTrans functor_abbrev_def)
        apply(simp add: IdNatTrans_defs)
        done
      show "dom\<^bsub>CatExp' A B\<^esub> (id\<^bsub>CatExp' A B\<^esub> F) = F" by (simp add: CatExp'_def IdNatTrans_defs)
      show "cod\<^bsub>CatExp' A B\<^esub> (id\<^bsub>CatExp' A B\<^esub> F) = F" by (simp add: CatExp'_def IdNatTrans_defs)
    qed
  }
  {
    fix f assume a: "f \<in> mor\<^bsub>CatExp' A B\<^esub>"
    show "(id\<^bsub>CatExp' A B\<^esub> (dom\<^bsub>CatExp' A B\<^esub> f)) ;;\<^bsub>CatExp' A B\<^esub> f = f" and
         "f ;;\<^bsub>CatExp' A B\<^esub> (id\<^bsub>CatExp' A B\<^esub> (cod\<^bsub>CatExp' A B\<^esub> f)) = f"
    proof(simp_all add: CatExp'_def)
      have NT: "NatTrans f" using a by (simp add: CatExp'_def)
      show "IdNatTrans (NTDom f) \<bullet> f = f" using NT by (simp add:IdNatTransMapL)
      show "f \<bullet> IdNatTrans (NTCod f) = f" using NT by (simp add:IdNatTransMapR)
    qed
  }
  {
    fix f g h assume aa: "f \<approx>>\<^bsub>CatExp' A B\<^esub> g" and bb: "g \<approx>>\<^bsub>CatExp' A B\<^esub> h"
    {
      fix f g assume "f \<approx>>\<^bsub>CatExp' A B\<^esub> g" hence "f \<approx>>\<bullet> g"
        apply(simp only: NTCompDefined_def)
        by (auto simp add:  CatExp'_def)
    }
    hence "f \<approx>>\<bullet> g" and "g \<approx>>\<bullet> h" using aa bb by auto
    thus "f ;;\<^bsub>CatExp' A B\<^esub> g ;;\<^bsub>CatExp' A B\<^esub> h = f ;;\<^bsub>CatExp' A B\<^esub> (g ;;\<^bsub>CatExp' A B\<^esub> h)"
    by(simp add: CatExp'_def NatTransCompAssoc)
  }
  {
    fix f g X Y Z assume a: "f maps\<^bsub>CatExp' A B\<^esub> X to Y" and b: "g maps\<^bsub>CatExp' A B\<^esub> Y to Z"
    show "f ;;\<^bsub>CatExp' A B\<^esub> g maps\<^bsub>CatExp' A B\<^esub> X 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 \<approx>>\<bullet> g" by(auto intro: NTCompDefinedI)
      thus "NatTrans (f \<bullet> g)" by (simp add: NatTransCompNatTrans)
      show "NTCatDom (f \<bullet> g) = A" using Comp cd1 by (simp add: NTCatDom)
      show "NTCatCod (f \<bullet> g) = B" using Comp cc2 by (simp add: NTCatCod)
      show "NTDom (f \<bullet> g) = X" using d1 by (simp add: NatTransComp_defs)
      show "NTCod (f \<bullet> g) = Z" using c2 by (simp add: NatTransComp_defs)
    qed
  }
qed

lemma CatExpCat: "\<lbrakk>Category A ; Category B\<rbrakk> \<Longrightarrow> Category (CatExp A B)"
by(simp add: CatExpCatAx CatExp_def MakeCat)

lemmas CatExp_defs = CatExp_def CatExp'_def MakeCat_def

lemma CatExpDom: "f \<in> Mor (CatExp A B) \<Longrightarrow> dom\<^bsub>CatExp A B\<^esub> f = NTDom f"
by (simp add: CatExp_defs)

lemma CatExpCod: "f \<in> Mor (CatExp A B) \<Longrightarrow> cod\<^bsub>CatExp A B\<^esub> f = NTCod f"
by (simp add: CatExp_defs)

lemma CatExpId: "X \<in> Obj (CatExp A B) \<Longrightarrow> Id (CatExp A B) X = IdNatTrans X"
by (simp add: CatExp_defs)

lemma CatExpNatTransCompDef: assumes "f \<approx>>\<^bsub>CatExp A B\<^esub> g" shows "f \<approx>>\<bullet> g"
proof-
  have 1: "f \<approx>>\<^bsub>CatExp' A B\<^esub> g" using assms by (simp add: CatExp_def MakeCatCompDef)
  show "f \<approx>>\<bullet> 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 \<in> Obj A" and "f \<approx>>\<^bsub>CatExp A B\<^esub> g"
  shows "(f ;;\<^bsub>CatExp A B\<^esub> g) $$ X = (f $$ X) ;;\<^bsub>B\<^esub> (g $$ X)"
proof-
  have "f \<in> 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 \<in> Obj (NTCatDom f)" using assms by simp
  have 3: "f \<approx>>\<bullet> g" using assms(2) by (simp add: CatExpNatTransCompDef)
  have "(f ;;\<^bsub>CatExp A B\<^esub> g) $$ X = (f ;;\<^bsub>CatExp' A B\<^esub> g) $$ X" using assms(2) by (simp add: CatExp_def MakeCatComp2)
  also have "... = (f \<bullet> g) $$ X" by (simp add: CatExp'_def)
  also have "... = (f $$ X) ;;\<^bsub>B\<^esub> (g $$ X)" using 4 2 3 by (simp add: NatTransComp_Comp2[of X f g])
  finally show ?thesis .
qed

lemma CatExpMorNT: "f \<in> Mor (CatExp A B) \<Longrightarrow> NatTrans f"
by (simp add: CatExp_defs)

end


theory Delete_Opt_Ext
  imports
    Delete_Opt
    Balance_Opt_LR_Ext
    Combine_Opt_Ext
    "../../Assertion_Tree_Lookup"
    "../../Utilities_Ext"
begin

context rbt_impl
begin

lemma del_opt_correct_ext':
  "
  llvm_htriple
  (rbt_assn_ext t {} ti ** \<upharpoonleft>key_assn k ki ** \<up>(is_rbt_node (rbt_of t)))
  (del_opt ki ti)
  (\<lambda>ti_res. EXS t_res.
    rbt_assn_ext t_res {} ti_res **
    \<upharpoonleft>key_assn k ki **
    \<up>(rbt_of t_res = rbt_del_ad k (rbt_of t)) **
    ctx(rbt_sorted (rbt_of t_res)) **
    \<up>((ptr_of_key t ti)(k := None) \<subseteq>\<^sub>m ptr_of_key t_res ti_res) **
    \<up>((value_of_key t)(k := None) \<subseteq>\<^sub>m value_of_key t_res)
  )
  "
  supply sep_context_pureI[fri_red_rules]
proof(induct k "rbt_of t" arbitrary: t ki ti rule: rbt_del_ad.induct)
  case (1 k)
  then show ?case
    apply (subst del_opt.simps)
    apply vcg
    done
next
  case (2 k c l kc vc r)

  from 2(5) show ?case
    apply (subst del_opt.simps)
    apply vcg

    subgoal for al ar ci li ki vi ri asf ra sa (*k < kc*)
      apply (cases ra)
      subgoal (*ra = 0*)
        supply 2(2)[simplified ctx_def, vcg_rules]

        supply rbt_del_ad_correct[simp]
        supply rbt_del_rbt_less[intro]
        supply rbt_del_rbt_sorted[intro]

        by vcg_vok

      subgoal (*ra = 1*)
        supply 2(1)[simplified ctx_def, vcg_rules]
        supply rbt_del_rbt_less[simp]
        supply rbt_del_ad_correct[simp]

        by vcg_vok
      done

    subgoal for al ar ci li ki vi ri asf ra s (*kc \<le> k*)
      apply vcg
      subgoal for rb sa (*kc < k*)
              apply (cases rb)
      subgoal (*rb = 0*)
        supply 2(4)[simplified ctx_def, vcg_rules]

        supply rbt_del_ad_correct[simp]
        supply rbt_del_rbt_greater[intro]
        supply rbt_del_rbt_sorted[intro]

        by vcg_vok

      subgoal (*rb = 1*)
        supply 2(3)[simplified ctx_def, vcg_rules]
        supply rbt_del_rbt_greater[simp]
        supply rbt_del_ad_correct[simp]
        by vcg_vok
      done
      subgoal (*kc = k*) by vcg_vok
    done
  done
qed

lemmas del_opt_ext_correct = del_opt_correct_ext'[simplified ctx_def rbt_del_ad_correct]

lemma delete_opt_ext_correct:
  "
  llvm_htriple
  (\<upharpoonleft>key_assn k ki ** rbt_assn_ext t {} ti ** \<up>(is_rbt_node (rbt_of t)))
  (delete_opt ki ti)
  (\<lambda>ti_res. EXS t_res.
    rbt_assn_ext t_res {} ti_res **
    \<upharpoonleft>key_assn k ki **
    \<up>(rbt_of t_res = (rbt_delete k (rbt_of t))) **
    ctx(rbt_sorted (rbt_of t_res)) **
    \<up>((ptr_of_key t ti)(k := None) \<subseteq>\<^sub>m ptr_of_key t_res ti_res) **
    \<up>((value_of_key t)(k := None) \<subseteq>\<^sub>m value_of_key t_res)
  )
"
  unfolding delete_opt_def rbt_delete_def paint_def
  supply
    del_opt_ext_correct[vcg_rules]
    sep_context_pureI[isep_red]

  apply vcg
  subgoal
    apply vcg_compat
    apply (sepEwith auto)

(*rbt_of =*)
     apply (cases "rbt_del k (rbt_of t)")
      apply simp_all

    apply sep
     apply (metis rbt_sorted.simps(2))

    apply (sepwith \<open>solves pok_solver | solves vok_solver\<close>)
    done
  done

lemmas [vcg_rules] = delete_opt_ext_correct[simplified ctx_def]

end

end