(*
 * Title:      Z
 * Author:     Bertram Felgenhauer (2016)
 * Author:     Julian Nagele (2016)
 * Author:     Vincent van Oostrom (2016)
 * Author:     Christian Sternagel (2016)
 * Maintainer: Bertram Felgenhauer <bertram.felgenhauer@uibk.ac.at>
 * Maintainer: Juilan Nagele <julian.nagele@uibk.ac.at>
 * Maintainer: Christian Sternagel <c.sternagel@gmail.com>
 * License:    BSD
 *)

section \<open>Combinatory Logic has the Church-Rosser property\<close>

theory CL_Z imports Z
begin

datatype CL = S | K | I | App CL CL ("` _ _" [999, 999] 999)

inductive_set red :: "CL rel" where
  L: "(t, t') \<in> red \<Longrightarrow> (` t u, ` t' u) \<in> red"
| R: "(u, u') \<in> red \<Longrightarrow> (` t u, ` t u') \<in> red"
| S: "(` ` ` S x y z, ` ` x z ` y z) \<in> red"
| K: "(` ` K x y, x) \<in> red"
| I: "(` I x, x) \<in> red"

lemma App_mono:
  "(t, t') \<in> red\<^sup>* \<Longrightarrow> (u, u') \<in> red\<^sup>* \<Longrightarrow> (` t u, ` t' u') \<in> red\<^sup>*"
proof -
  assume "(t, t') \<in> red\<^sup>*" hence "(` t u, ` t' u) \<in> red\<^sup>*"
    by (induct t' rule: rtrancl_induct) (auto intro: rtrancl_into_rtrancl red.intros)
  moreover assume "(u, u') \<in> red\<^sup>*" hence "(` t' u, ` t' u') \<in> red\<^sup>*"
    by (induct u' rule: rtrancl_induct) (auto intro: rtrancl_into_rtrancl red.intros)
  ultimately show ?thesis by auto
qed

fun bullet_app :: "CL \<Rightarrow> CL \<Rightarrow> CL" where
  "bullet_app (` ` S x y) z = ` ` x z ` y z"
| "bullet_app (` K x) y = x"
| "bullet_app I x = x"
| "bullet_app t u = ` t u"

lemma bullet_app_red:
  "(` t u, bullet_app t u) \<in> red\<^sup>="
by (induct t u rule: bullet_app.induct) (auto intro: red.intros)

lemma bullet_app_redsI:
  "(s, ` t u) \<in> red\<^sup>* \<Longrightarrow> (s, bullet_app t u) \<in> red\<^sup>*"
using bullet_app_red[of t u] by auto

lemma bullet_app_redL:
  "(t, t') \<in> red \<Longrightarrow> (bullet_app t u, bullet_app t' u) \<in> red\<^sup>*"
by (induct t u rule: bullet_app.induct)
   (auto 0 6 intro: App_mono bullet_app_redsI elim: red.cases simp only: bullet_app.simps)

lemma bullet_app_redR:
  "(u, u') \<in> red \<Longrightarrow> (bullet_app t u, bullet_app t u') \<in> red\<^sup>*"
by (induct t u rule: bullet_app.induct) (auto intro: App_mono)

lemma bullet_app_mono:
  assumes "(t, t') \<in> red\<^sup>*" "(u, u') \<in> red\<^sup>*" shows "(bullet_app t u, bullet_app t' u') \<in> red\<^sup>*"
proof -
  have "(bullet_app t u, bullet_app t' u) \<in> red\<^sup>*" using assms(1)
    by (induct t' rule: rtrancl_induct) (auto intro: rtrancl_trans bullet_app_redL)
  moreover have "(bullet_app t' u, bullet_app t' u') \<in> red\<^sup>*" using assms(2)
    by (induct u' rule: rtrancl_induct) (auto intro: rtrancl_trans bullet_app_redR)
  ultimately show ?thesis by auto
qed

fun bullet :: "CL \<Rightarrow> CL" where
  "bullet (` t u) = bullet_app (bullet t) (bullet u)"
| "bullet t = t"

lemma bullet_incremental:
  "(t, bullet t) \<in> red\<^sup>*"
by (induct t rule: bullet.induct) (auto intro: App_mono bullet_app_redsI)

interpretation CL:z_property bullet red
proof (unfold_locales, intro conjI)
  fix t u assume "(t, u) \<in> red" thus "(u, bullet t) \<in> red\<^sup>*"
  proof (induct t arbitrary: u rule: bullet.induct)
    case (1 t1 t2) show ?case using 1(3)
      by (cases) (auto intro: 1 App_mono bullet_app_redsI bullet_incremental)
  qed (auto elim: red.cases)
next
  fix t u assume "(t, u) \<in> red" thus "(bullet t, bullet u) \<in> red\<^sup>*"
    by (induct t u rule: red.induct) (auto intro: App_mono bullet_app_mono bullet_app_redsI)
qed

lemmas CR_red = CL.CR

end


theory LamFun
imports "../Nominal2" Quotient_Option
begin

atom_decl name

nominal_datatype lam =
  Var "name"
| App "lam" "lam"
| Lam x::"name" l::"lam"  binds x in l

thm lam.distinct
thm lam.induct
thm lam.exhaust
thm lam.fv_defs
thm lam.bn_defs
thm lam.perm_simps
thm lam.eq_iff
thm lam.fv_bn_eqvt
thm lam.size_eqvt
thm lam.size
thm lam_raw.size
thm lam.fresh

primrec match_Var_raw where
  "match_Var_raw (Var_raw x) = Some x"
| "match_Var_raw (App_raw x y) = None"
| "match_Var_raw (Lam_raw n t) = None"

quotient_definition
  "match_Var :: lam \<Rightarrow> name option"
is match_Var_raw

lemma [quot_respect]: "(alpha_lam_raw ===> op =) match_Var_raw match_Var_raw"
  by (rule, induct_tac a b rule: alpha_lam_raw.induct, simp_all)

lemmas match_Var_simps = match_Var_raw.simps[quot_lifted]

primrec match_App_raw where
  "match_App_raw (Var_raw x) = None"
| "match_App_raw (App_raw x y) = Some (x, y)"
| "match_App_raw (Lam_raw n t) = None"

quotient_definition
  "match_App :: lam \<Rightarrow> (lam \<times> lam) option"
is match_App_raw

lemma [quot_respect]:
  "(alpha_lam_raw ===> option_rel (prod_rel alpha_lam_raw alpha_lam_raw)) match_App_raw match_App_raw"
  by (intro fun_relI, induct_tac a b rule: alpha_lam_raw.induct, simp_all)

lemmas match_App_simps = match_App_raw.simps[quot_lifted]

definition next_name where
  "next_name (s :: 'a :: fs) = (THE x. \<forall>a \<in> supp s. atom x \<noteq> a)"

lemma lam_eq: "Lam a t = Lam b s \<longleftrightarrow> (a \<leftrightarrow> b) \<bullet> t = s"
  apply (simp add: lam.eq_iff Abs_eq_iff alphas)
  sorry

lemma lam_half_inj: "(Lam z s = Lam z sa) = (s = sa)"
  by (auto simp add: lam_eq)

definition
  "match_Lam (S :: 'a :: fs) t = (if (\<exists>n s. (t = Lam n s)) then
    (let z = next_name (S, t) in Some (z, THE s. t = Lam z s)) else None)"

lemma match_Lam_simps:
  "match_Lam S (Var n) = None"
  "match_Lam S (App l r) = None"
  "match_Lam S (Lam z s) = (let n = next_name (S, (Lam z s)) in Some (n, (z \<leftrightarrow> n) \<bullet> s))"
  apply (simp_all add: match_Lam_def lam.distinct)
  apply (auto simp add: lam_eq)
  done

lemma app_some: "match_App x = Some (a, b) \<Longrightarrow> x = App a b"
by (induct x rule: lam.induct) (simp_all add: match_App_simps)

lemma lam_some: "match_Lam S x = Some (n, t) \<Longrightarrow> x = Lam n t"
  apply (induct x rule: lam.induct)
  apply (simp add: match_Lam_simps)
  apply (simp add: match_Lam_simps)
  apply (simp add: match_Lam_simps Let_def lam_eq)
  apply clarify
  done

function subst where
"subst v s t = (
  case match_Var t of Some n \<Rightarrow> if n = v then s else Var n | None \<Rightarrow>
  case match_App t of Some (l, r) \<Rightarrow> App (subst v s l) (subst v s r) | None \<Rightarrow>
  case match_Lam (v, s) t of Some (n, s') \<Rightarrow> Lam n (subst v s s') | None \<Rightarrow> undefined)"
print_theorems
thm subst_rel.intros[no_vars]
by pat_completeness auto

termination apply (relation "measure (\<lambda>(_, _, t). size t)")
  apply auto[1]
  apply (case_tac a) apply simp
  apply (frule lam_some) apply (simp add: lam.size)
  apply (case_tac a) apply simp
  apply (frule app_some) apply (simp add: lam.size)
  apply (case_tac a) apply simp
  apply (frule app_some) apply (simp add: lam.size)
done

lemma subst_eqs:
  "subst y s (Var x) = (if x = y then s else (Var x))"
  "subst y s (App l r) = App (subst y s l) (subst y s r)"
  "subst y s (Lam x t) =
    (let n = next_name ((y, s), Lam x t) in Lam n (subst y s ((x \<leftrightarrow> n) \<bullet> t)))"
  apply (subst subst.simps)
  apply (simp only: match_Var_simps option.simps)
  apply (subst subst.simps)
  apply (simp only: match_App_simps option.simps prod.simps match_Var_simps)
  apply (subst subst.simps)
  apply (simp only: match_App_simps option.simps prod.simps match_Var_simps match_Lam_simps Let_def)
  done

lemma subst_eqvt:
  "p \<bullet> (subst v s t) = subst (p \<bullet> v) (p \<bullet> s) (p \<bullet> t)"
  proof (induct v s t rule: subst.induct)
    case (1 v s t)
    show ?case proof (cases t rule: lam.exhaust)
      fix n
      assume "t = Var n"
      then show ?thesis by (simp add: match_Var_simps)
    next
      fix l r
      assume "t = App l r"
      then show ?thesis
        apply (simp only: subst_eqs lam.perm_simps)
        apply (subst 1(2)[of "(l, r)" "l" "r"])
        apply (simp add: match_Var_simps)
        apply (simp add: match_App_simps)
        apply (rule refl)
        apply (subst 1(3)[of "(l, r)" "l" "r"])
        apply (simp add: match_Var_simps)
        apply (simp add: match_App_simps)
        apply (rule refl)
        apply (rule refl)
        done
    next
      fix n t'
      assume "t = Lam n t'"
      then show ?thesis
        apply (simp only: subst_eqs lam.perm_simps Let_def)
        apply (subst 1(1))
        apply (simp add: match_Var_simps)
        apply (simp add: match_App_simps)
        apply (simp add: match_Lam_simps Let_def)
        apply (rule refl)
        (* next_name is not equivariant :( *)
        apply (simp only: lam_eq)
        sorry
    qed
  qed

lemma subst_eqvt':
  "p \<bullet> (subst v s t) = subst (p \<bullet> v) (p \<bullet> s) (p \<bullet> t)"
  apply (induct t arbitrary: v s rule: lam.induct)
  apply (simp only: subst_eqs lam.perm_simps eqvts)
  apply (simp only: subst_eqs lam.perm_simps)
  apply (simp only: subst_eqs lam.perm_simps Let_def)
  apply (simp only: lam_eq)
  sorry

lemma subst_eq3:
  "atom x \<sharp> (y, s) \<Longrightarrow> subst y s (Lam x t) = Lam x (subst y s t)"
  apply (simp only: subst_eqs Let_def)
  apply (simp only: lam_eq)
  (* p # y   p # s   subst_eqvt *)
  (* p \<bullet> -p \<bullet> (subst y s t) = subst y s t *)
  sorry

end


theory Sorted
imports Main
        "../Sorting"
        HardL

begin

lemma leqRev [] : "\<not> leq r t \<Longrightarrow> leq t r" (* without other lemmas, requires schemes *)
by hipster_induct_schemes (*
apply(induction r rule: leq.induct)
apply(simp_all)
done*)

lemma leqLeqEq : "\<lbrakk> leq r t ; leq t r \<rbrakk> \<Longrightarrow> r = t"
by (hipster_induct_schemes) (* without other lemmas, requires schemes *)

lemma insSorted : "insert r (isort ts) = isort (insert r ts)"
apply(induction ts rule: isort.induct)
apply(simp_all add: leqRev leqLeqEq)
(* case with r = Z *)
oops

lemma insTwiceComm : "insert r (insert t ts) = insert t (insert r ts)"
(* case with r = Z *)
oops

lemma isortIds : "sorted ts \<Longrightarrow> isort ts = ts"
by hipster_induct_schemes (*
apply(induction ts rule: sorted.induct)
apply(simp_all)
done*)

lemma insSortInvarZ : "sorted ts \<Longrightarrow> sorted (insert Z ts)"
by (hipster_induct_simp_metis)
(* alternative: apply(case_tac ts) apply(simp_all) done *)

setup\<open>Hip_Tac_Ops.toggle_full_types @{context} ;\<close>
setup\<open>Hip_Tac_Ops.set_metis_to @{context} 1000;\<close>
ML\<open>
Hip_Tac_Ops.metis_to @{context} ;
Hip_Tac_Ops.use_full_types @{context};\<close>

lemma insSortInvar : "sorted ts \<Longrightarrow> sorted (insert t ts)"
by (hipster_induct_schemes sorted.simps insert.simps Nat.exhaust leqRev list.exhaust)
(*
by (hipster_induct_schemes sorted.simps leqRev)
apply(induction ts rule: sorted.induct)
apply(simp_all)
apply(metis sorted.simps insert.simps leqRev)
sledgehammer
by (metis (full_types) leqRev)*)

lemma isortSorts : "sorted (isort ts)"
by (hipster_induct_simp_metis insSortInvar)
(* started as:  apply(induction ts rule: isort.induct)  apply(simp_all) apply(simp add: insSortInvar) *)

lemma isortIdsP : "sorted ts \<Longrightarrow> sorted (isort ts)"
by (hipster_induct_simp_metis isortSorts)

lemma kerIsort : "isort (isort ts) = isort ts"
by (hipster_induct_simp_metis isortSorts isortIds)
(* apply(induction ts rule: isort.induct) apply(simp_all add: isortIds) *)

lemma insMinInsorted : "sorted ts \<Longrightarrow> isort (insert Z ts) = insert Z ts"
by (hipster_induct_simp_metis isortIds insSortInvar)

lemma insSomeInsorted : "sorted ts \<Longrightarrow> isort (insert t ts) = insert t ts"
by (hipster_induct_simp_metis isortIds insSortInvar)

lemma postOverPrecond: "sorted (insert t ts) \<Longrightarrow> sorted ts"
apply(induction t ts rule: insert.induct)
apply(simp_all add: leqRev leqLeqEq)
oops

end


header {* Monad Class *}

theory Monad
imports Functor
begin

subsection {* Class definition *}

text {* In Haskell, class \emph{Monad} is defined as follows: *}

text_raw {*
\begin{verbatim}
class Monad m where
  return :: a -> m a
  (>>=) :: m a -> (a -> m b) -> m b
\end{verbatim}
*}

text {* We formalize class @{text monad} in a manner similar to the
@{text functor} class: We fix monomorphic versions of the class
constants, replacing type variables with @{text udom}, and assume
monomorphic versions of the class axioms. *}

text {* Because the monad laws imply the composition rule for @{text
fmap}, we declare @{text prefunctor} as the superclass, and separately
prove a subclass relationship with @{text functor}. *}

class monad = prefunctor +
  fixes returnU :: "udom \<rightarrow> udom\<cdot>'a::tycon"
  fixes bindU :: "udom\<cdot>'a \<rightarrow> (udom \<rightarrow> udom\<cdot>'a) \<rightarrow> udom\<cdot>'a"
  assumes fmapU_eq_bindU:
    "\<And>f xs. fmapU\<cdot>f\<cdot>xs = bindU\<cdot>xs\<cdot>(\<Lambda> x. returnU\<cdot>(f\<cdot>x))"
  assumes bindU_returnU:
    "\<And>f x. bindU\<cdot>(returnU\<cdot>x)\<cdot>f = f\<cdot>x"
  assumes bindU_bindU:
    "\<And>xs f g. bindU\<cdot>(bindU\<cdot>xs\<cdot>f)\<cdot>g = bindU\<cdot>xs\<cdot>(\<Lambda> x. bindU\<cdot>(f\<cdot>x)\<cdot>g)"

instance monad \<subseteq> "functor"
proof
  fix f g :: "udom \<rightarrow> udom" and xs :: "udom\<cdot>'a"
  show "fmapU\<cdot>f\<cdot>(fmapU\<cdot>g\<cdot>xs) = fmapU\<cdot>(\<Lambda> x. f\<cdot>(g\<cdot>x))\<cdot>xs"
    by (simp add: fmapU_eq_bindU bindU_bindU bindU_returnU)
qed

text {* As with @{text fmap}, we define the polymorphic @{text return}
and @{text bind} by coercion from the monomorphic @{text returnU} and
@{text bindU}. *}

definition return :: "'a \<rightarrow> 'a\<cdot>'m::monad"
  where "return = coerce\<cdot>(returnU :: udom \<rightarrow> udom\<cdot>'m)"

definition bind :: "'a\<cdot>'m::monad \<rightarrow> ('a \<rightarrow> 'b\<cdot>'m) \<rightarrow> 'b\<cdot>'m"
  where "bind = coerce\<cdot>(bindU :: udom\<cdot>'m \<rightarrow> _)"

abbreviation bind_syn :: "'a\<cdot>'m::monad \<Rightarrow> ('a \<rightarrow> 'b\<cdot>'m) \<Rightarrow> 'b\<cdot>'m" (infixl "\<guillemotright>=" 55)
  where "m \<guillemotright>= f \<equiv> bind\<cdot>m\<cdot>f"

subsection {* Naturality of bind and return *}

text {* The three class axioms imply naturality properties of @{text
returnU} and @{text bindU}, i.e., that both commute with @{text
fmapU}. *}

lemma fmapU_returnU [coerce_simp]:
  "fmapU\<cdot>f\<cdot>(returnU\<cdot>x) = returnU\<cdot>(f\<cdot>x)"
by (simp add: fmapU_eq_bindU bindU_returnU)

lemma bindU_fmapU:
  "bindU\<cdot>(fmapU\<cdot>f\<cdot>xs)\<cdot>k = bindU\<cdot>xs\<cdot>(\<Lambda> x. k\<cdot>(f\<cdot>x))"
by (simp add: fmapU_eq_bindU bindU_returnU bindU_bindU)

subsection {* Polymorphic versions of class assumptions *}

lemma monad_fmap:
  fixes xs :: "'a\<cdot>'m::monad" and f :: "'a \<rightarrow> 'b"
  shows "fmap\<cdot>f\<cdot>xs = xs \<guillemotright>= (\<Lambda> x. return\<cdot>(f\<cdot>x))"
unfolding bind_def return_def fmap_def
by (simp add: coerce_simp fmapU_eq_bindU bindU_returnU)

lemma monad_left_unit [simp]: "(return\<cdot>x \<guillemotright>= f) = (f\<cdot>x)"
unfolding bind_def return_def
by (simp add: coerce_simp bindU_returnU)

lemma bind_bind:
  fixes m :: "'a\<cdot>'m::monad"
  shows "((m \<guillemotright>= f) \<guillemotright>= g) = (m \<guillemotright>= (\<Lambda> x. f\<cdot>x \<guillemotright>= g))"
unfolding bind_def
by (simp add: coerce_simp bindU_bindU)

subsection {* Derived rules *}

text {* The following properties can be derived using only the
abstract monad laws. *}

lemma monad_right_unit [simp]: "(m \<guillemotright>= return) = m"
 apply (subgoal_tac "fmap\<cdot>ID\<cdot>m = m")
  apply (simp only: monad_fmap)
  apply (simp add: eta_cfun)
 apply simp
done

lemma fmap_return: "fmap\<cdot>f\<cdot>(return\<cdot>x) = return\<cdot>(f\<cdot>x)"
by (simp add: monad_fmap)

lemma fmap_bind: "fmap\<cdot>f\<cdot>(bind\<cdot>xs\<cdot>k) = bind\<cdot>xs\<cdot>(\<Lambda> x. fmap\<cdot>f\<cdot>(k\<cdot>x))"
by (simp add: monad_fmap bind_bind)

lemma bind_fmap: "bind\<cdot>(fmap\<cdot>f\<cdot>xs)\<cdot>k = bind\<cdot>xs\<cdot>(\<Lambda> x. k\<cdot>(f\<cdot>x))"
by (simp add: monad_fmap bind_bind)

text {* Bind is strict in its first argument, if its second argument
is a strict function. *}

lemma bind_strict:
  assumes "k\<cdot>\<bottom> = \<bottom>" shows "\<bottom> \<guillemotright>= k = \<bottom>"
proof -
  have "\<bottom> \<guillemotright>= k \<sqsubseteq> return\<cdot>\<bottom> \<guillemotright>= k"
    by (intro monofun_cfun below_refl minimal)
  thus "\<bottom> \<guillemotright>= k = \<bottom>"
    by (simp add: assms)
qed

lemma congruent_bind:
  "(\<forall>m. m \<guillemotright>= k1 = m \<guillemotright>= k2) = (k1 = k2)"
 apply (safe, rule cfun_eqI)
 apply (drule_tac x="return\<cdot>x" in spec, simp)
done

subsection {* Laws for join *}

definition join :: "('a\<cdot>'m)\<cdot>'m \<rightarrow> 'a\<cdot>'m::monad"
  where "join \<equiv> \<Lambda> m. m \<guillemotright>= (\<Lambda> x. x)"

lemma join_return: "join\<cdot>(return\<cdot>xs) = xs"
by (simp add: join_def)

lemma join_fmap_return: "join\<cdot>(fmap\<cdot>return\<cdot>xs) = xs"
by (simp add: join_def monad_fmap eta_cfun bind_bind)

lemma join_fmap_join: "join\<cdot>(fmap\<cdot>join\<cdot>xsss) = join\<cdot>(join\<cdot>xsss)"
by (simp add: join_def monad_fmap bind_bind)

lemma bind_def2: "m \<guillemotright>= k = join\<cdot>(fmap\<cdot>k\<cdot>m)"
by (simp add: join_def monad_fmap eta_cfun bind_bind)

subsection {* Equivalence of monad laws and fmap/join laws *}

lemma "(return\<cdot>x \<guillemotright>= f) = (f\<cdot>x)"
by (simp only: bind_def2 fmap_return join_return)

lemma "(m \<guillemotright>= return) = m"
by (simp only: bind_def2 join_fmap_return)

lemma "((m \<guillemotright>= f) \<guillemotright>= g) = (m \<guillemotright>= (\<Lambda> x. f\<cdot>x \<guillemotright>= g))"
 apply (simp only: bind_def2)
 apply (subgoal_tac "join\<cdot>(fmap\<cdot>g\<cdot>(join\<cdot>(fmap\<cdot>f\<cdot>m))) =
    join\<cdot>(fmap\<cdot>join\<cdot>(fmap\<cdot>(fmap\<cdot>g)\<cdot>(fmap\<cdot>f\<cdot>m)))")
  apply (simp add: fmap_fmap)
 apply (simp add: join_fmap_join join_fmap_fmap)
done

subsection {* Simplification of coercions *}

text {* We configure rewrite rules that push coercions inwards, and
reduce them to coercions on simpler types. *}

lemma coerce_return [coerce_simp]:
  "COERCE('a\<cdot>'m,'b\<cdot>'m::monad)\<cdot>(return\<cdot>x) = return\<cdot>(COERCE('a,'b)\<cdot>x)"
by (simp add: coerce_functor fmap_return)

lemma coerce_bind [coerce_simp]:
  fixes m :: "'a\<cdot>'m::monad" and k :: "'a \<rightarrow> 'b\<cdot>'m"
  shows "COERCE('b\<cdot>'m,'c\<cdot>'m)\<cdot>(m \<guillemotright>= k) = m \<guillemotright>= (\<Lambda> x. COERCE('b\<cdot>'m,'c\<cdot>'m)\<cdot>(k\<cdot>x))"
by (simp add: coerce_functor fmap_bind)

lemma bind_coerce [coerce_simp]:
  fixes m :: "'a\<cdot>'m::monad" and k :: "'b \<rightarrow> 'c\<cdot>'m"
  shows "COERCE('a\<cdot>'m,'b\<cdot>'m)\<cdot>m \<guillemotright>= k = m \<guillemotright>= (\<Lambda> x. k\<cdot>(COERCE('a,'b)\<cdot>x))"
by (simp add: coerce_functor bind_fmap)

end


(*  Title:      ZF/Constructible/Datatype_absolute.thy
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
*)

section {*Absoluteness Properties for Recursive Datatypes*}

theory Datatype_absolute imports Formula WF_absolute begin

subsection{*The lfp of a continuous function can be expressed as a union*}

definition
  directed :: "i=>o" where
   "directed(A) == A\<noteq>0 & (\<forall>x\<in>A. \<forall>y\<in>A. x \<union> y \<in> A)"

definition
  contin :: "(i=>i) => o" where
   "contin(h) == (\<forall>A. directed(A) \<longrightarrow> h(\<Union>A) = (\<Union>X\<in>A. h(X)))"

lemma bnd_mono_iterates_subset: "[|bnd_mono(D, h); n \<in> nat|] ==> h^n (0) \<subseteq> D"
apply (induct_tac n)
 apply (simp_all add: bnd_mono_def, blast)
done

lemma bnd_mono_increasing [rule_format]:
     "[|i \<in> nat; j \<in> nat; bnd_mono(D,h)|] ==> i \<le> j \<longrightarrow> h^i(0) \<subseteq> h^j(0)"
apply (rule_tac m=i and n=j in diff_induct, simp_all)
apply (blast del: subsetI
             intro: bnd_mono_iterates_subset bnd_monoD2 [of concl: h])
done

lemma directed_iterates: "bnd_mono(D,h) ==> directed({h^n (0). n\<in>nat})"
apply (simp add: directed_def, clarify)
apply (rename_tac i j)
apply (rule_tac x="i \<union> j" in bexI)
apply (rule_tac i = i and j = j in Ord_linear_le)
apply (simp_all add: subset_Un_iff [THEN iffD1] le_imp_subset
                     subset_Un_iff2 [THEN iffD1])
apply (simp_all add: subset_Un_iff [THEN iff_sym] bnd_mono_increasing
                     subset_Un_iff2 [THEN iff_sym])
done

lemma contin_iterates_eq:
    "[|bnd_mono(D, h); contin(h)|]
     ==> h(\<Union>n\<in>nat. h^n (0)) = (\<Union>n\<in>nat. h^n (0))"
apply (simp add: contin_def directed_iterates)
apply (rule trans)
apply (rule equalityI)
 apply (simp_all add: UN_subset_iff)
 apply safe
 apply (erule_tac [2] natE)
  apply (rule_tac a="succ(x)" in UN_I)
   apply simp_all
apply blast
done

lemma lfp_subset_Union:
     "[|bnd_mono(D, h); contin(h)|] ==> lfp(D,h) \<subseteq> (\<Union>n\<in>nat. h^n(0))"
apply (rule lfp_lowerbound)
 apply (simp add: contin_iterates_eq)
apply (simp add: contin_def bnd_mono_iterates_subset UN_subset_iff)
done

lemma Union_subset_lfp:
     "bnd_mono(D,h) ==> (\<Union>n\<in>nat. h^n(0)) \<subseteq> lfp(D,h)"
apply (simp add: UN_subset_iff)
apply (rule ballI)
apply (induct_tac n, simp_all)
apply (rule subset_trans [of _ "h(lfp(D,h))"])
 apply (blast dest: bnd_monoD2 [OF _ _ lfp_subset])
apply (erule lfp_lemma2)
done

lemma lfp_eq_Union:
     "[|bnd_mono(D, h); contin(h)|] ==> lfp(D,h) = (\<Union>n\<in>nat. h^n(0))"
by (blast del: subsetI
          intro: lfp_subset_Union Union_subset_lfp)

subsubsection{*Some Standard Datatype Constructions Preserve Continuity*}

lemma contin_imp_mono: "[|X\<subseteq>Y; contin(F)|] ==> F(X) \<subseteq> F(Y)"
apply (simp add: contin_def)
apply (drule_tac x="{X,Y}" in spec)
apply (simp add: directed_def subset_Un_iff2 Un_commute)
done

lemma sum_contin: "[|contin(F); contin(G)|] ==> contin(\<lambda>X. F(X) + G(X))"
by (simp add: contin_def, blast)

lemma prod_contin: "[|contin(F); contin(G)|] ==> contin(\<lambda>X. F(X) * G(X))"
apply (subgoal_tac "\<forall>B C. F(B) \<subseteq> F(B \<union> C)")
 prefer 2 apply (simp add: Un_upper1 contin_imp_mono)
apply (subgoal_tac "\<forall>B C. G(C) \<subseteq> G(B \<union> C)")
 prefer 2 apply (simp add: Un_upper2 contin_imp_mono)
apply (simp add: contin_def, clarify)
apply (rule equalityI)
 prefer 2 apply blast
apply clarify
apply (rename_tac B C)
apply (rule_tac a="B \<union> C" in UN_I)
 apply (simp add: directed_def, blast)
done

lemma const_contin: "contin(\<lambda>X. A)"
by (simp add: contin_def directed_def)

lemma id_contin: "contin(\<lambda>X. X)"
by (simp add: contin_def)

subsection {*Absoluteness for "Iterates"*}

definition
  iterates_MH :: "[i=>o, [i,i]=>o, i, i, i, i] => o" where
   "iterates_MH(M,isF,v,n,g,z) ==
        is_nat_case(M, v, \<lambda>m u. \<exists>gm[M]. fun_apply(M,g,m,gm) & isF(gm,u),
                    n, z)"

definition
  is_iterates :: "[i=>o, [i,i]=>o, i, i, i] => o" where
    "is_iterates(M,isF,v,n,Z) ==
      \<exists>sn[M]. \<exists>msn[M]. successor(M,n,sn) & membership(M,sn,msn) &
                       is_wfrec(M, iterates_MH(M,isF,v), msn, n, Z)"

definition
  iterates_replacement :: "[i=>o, [i,i]=>o, i] => o" where
   "iterates_replacement(M,isF,v) ==
      \<forall>n[M]. n\<in>nat \<longrightarrow>
         wfrec_replacement(M, iterates_MH(M,isF,v), Memrel(succ(n)))"

lemma (in M_basic) iterates_MH_abs:
  "[| relation1(M,isF,F); M(n); M(g); M(z) |]
   ==> iterates_MH(M,isF,v,n,g,z) \<longleftrightarrow> z = nat_case(v, \<lambda>m. F(g`m), n)"
by (simp add: nat_case_abs [of _ "\<lambda>m. F(g ` m)"]
              relation1_def iterates_MH_def)

lemma (in M_basic) iterates_imp_wfrec_replacement:
  "[|relation1(M,isF,F); n \<in> nat; iterates_replacement(M,isF,v)|]
   ==> wfrec_replacement(M, \<lambda>n f z. z = nat_case(v, \<lambda>m. F(f`m), n),
                       Memrel(succ(n)))"
by (simp add: iterates_replacement_def iterates_MH_abs)

theorem (in M_trancl) iterates_abs:
  "[| iterates_replacement(M,isF,v); relation1(M,isF,F);
      n \<in> nat; M(v); M(z); \<forall>x[M]. M(F(x)) |]
   ==> is_iterates(M,isF,v,n,z) \<longleftrightarrow> z = iterates(F,n,v)"
apply (frule iterates_imp_wfrec_replacement, assumption+)
apply (simp add: wf_Memrel trans_Memrel relation_Memrel nat_into_M
                 is_iterates_def relation2_def iterates_MH_abs
                 iterates_nat_def recursor_def transrec_def
                 eclose_sing_Ord_eq nat_into_M
         trans_wfrec_abs [of _ _ _ _ "\<lambda>n g. nat_case(v, \<lambda>m. F(g`m), n)"])
done

lemma (in M_trancl) iterates_closed [intro,simp]:
  "[| iterates_replacement(M,isF,v); relation1(M,isF,F);
      n \<in> nat; M(v); \<forall>x[M]. M(F(x)) |]
   ==> M(iterates(F,n,v))"
apply (frule iterates_imp_wfrec_replacement, assumption+)
apply (simp add: wf_Memrel trans_Memrel relation_Memrel nat_into_M
                 relation2_def iterates_MH_abs
                 iterates_nat_def recursor_def transrec_def
                 eclose_sing_Ord_eq nat_into_M
         trans_wfrec_closed [of _ _ _ "\<lambda>n g. nat_case(v, \<lambda>m. F(g`m), n)"])
done

subsection {*lists without univ*}

lemmas datatype_univs = Inl_in_univ Inr_in_univ
                        Pair_in_univ nat_into_univ A_into_univ

lemma list_fun_bnd_mono: "bnd_mono(univ(A), \<lambda>X. {0} + A*X)"
apply (rule bnd_monoI)
 apply (intro subset_refl zero_subset_univ A_subset_univ
              sum_subset_univ Sigma_subset_univ)
apply (rule subset_refl sum_mono Sigma_mono | assumption)+
done

lemma list_fun_contin: "contin(\<lambda>X. {0} + A*X)"
by (intro sum_contin prod_contin id_contin const_contin)

text{*Re-expresses lists using sum and product*}
lemma list_eq_lfp2: "list(A) = lfp(univ(A), \<lambda>X. {0} + A*X)"
apply (simp add: list_def)
apply (rule equalityI)
 apply (rule lfp_lowerbound)
  prefer 2 apply (rule lfp_subset)
 apply (clarify, subst lfp_unfold [OF list_fun_bnd_mono])
 apply (simp add: Nil_def Cons_def)
 apply blast
txt{*Opposite inclusion*}
apply (rule lfp_lowerbound)
 prefer 2 apply (rule lfp_subset)
apply (clarify, subst lfp_unfold [OF list.bnd_mono])
apply (simp add: Nil_def Cons_def)
apply (blast intro: datatype_univs
             dest: lfp_subset [THEN subsetD])
done

text{*Re-expresses lists using "iterates", no univ.*}
lemma list_eq_Union:
     "list(A) = (\<Union>n\<in>nat. (\<lambda>X. {0} + A*X) ^ n (0))"
by (simp add: list_eq_lfp2 lfp_eq_Union list_fun_bnd_mono list_fun_contin)

definition
  is_list_functor :: "[i=>o,i,i,i] => o" where
    "is_list_functor(M,A,X,Z) ==
        \<exists>n1[M]. \<exists>AX[M].
         number1(M,n1) & cartprod(M,A,X,AX) & is_sum(M,n1,AX,Z)"

lemma (in M_basic) list_functor_abs [simp]:
     "[| M(A); M(X); M(Z) |] ==> is_list_functor(M,A,X,Z) \<longleftrightarrow> (Z = {0} + A*X)"
by (simp add: is_list_functor_def singleton_0 nat_into_M)

subsection {*formulas without univ*}

lemma formula_fun_bnd_mono:
     "bnd_mono(univ(0), \<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
apply (rule bnd_monoI)
 apply (intro subset_refl zero_subset_univ A_subset_univ
              sum_subset_univ Sigma_subset_univ nat_subset_univ)
apply (rule subset_refl sum_mono Sigma_mono | assumption)+
done

lemma formula_fun_contin:
     "contin(\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
by (intro sum_contin prod_contin id_contin const_contin)

text{*Re-expresses formulas using sum and product*}
lemma formula_eq_lfp2:
    "formula = lfp(univ(0), \<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
apply (simp add: formula_def)
apply (rule equalityI)
 apply (rule lfp_lowerbound)
  prefer 2 apply (rule lfp_subset)
 apply (clarify, subst lfp_unfold [OF formula_fun_bnd_mono])
 apply (simp add: Member_def Equal_def Nand_def Forall_def)
 apply blast
txt{*Opposite inclusion*}
apply (rule lfp_lowerbound)
 prefer 2 apply (rule lfp_subset, clarify)
apply (subst lfp_unfold [OF formula.bnd_mono, simplified])
apply (simp add: Member_def Equal_def Nand_def Forall_def)
apply (elim sumE SigmaE, simp_all)
apply (blast intro: datatype_univs dest: lfp_subset [THEN subsetD])+
done

text{*Re-expresses formulas using "iterates", no univ.*}
lemma formula_eq_Union:
     "formula =
      (\<Union>n\<in>nat. (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)) ^ n (0))"
by (simp add: formula_eq_lfp2 lfp_eq_Union formula_fun_bnd_mono
              formula_fun_contin)

definition
  is_formula_functor :: "[i=>o,i,i] => o" where
    "is_formula_functor(M,X,Z) ==
        \<exists>nat'[M]. \<exists>natnat[M]. \<exists>natnatsum[M]. \<exists>XX[M]. \<exists>X3[M].
          omega(M,nat') & cartprod(M,nat',nat',natnat) &
          is_sum(M,natnat,natnat,natnatsum) &
          cartprod(M,X,X,XX) & is_sum(M,XX,X,X3) &
          is_sum(M,natnatsum,X3,Z)"

lemma (in M_basic) formula_functor_abs [simp]:
     "[| M(X); M(Z) |]
      ==> is_formula_functor(M,X,Z) \<longleftrightarrow>
          Z = ((nat*nat) + (nat*nat)) + (X*X + X)"
by (simp add: is_formula_functor_def)

subsection{*@{term M} Contains the List and Formula Datatypes*}

definition
  list_N :: "[i,i] => i" where
    "list_N(A,n) == (\<lambda>X. {0} + A * X)^n (0)"

lemma Nil_in_list_N [simp]: "[] \<in> list_N(A,succ(n))"
by (simp add: list_N_def Nil_def)

lemma Cons_in_list_N [simp]:
     "Cons(a,l) \<in> list_N(A,succ(n)) \<longleftrightarrow> a\<in>A & l \<in> list_N(A,n)"
by (simp add: list_N_def Cons_def)

text{*These two aren't simprules because they reveal the underlying
list representation.*}
lemma list_N_0: "list_N(A,0) = 0"
by (simp add: list_N_def)

lemma list_N_succ: "list_N(A,succ(n)) = {0} + A * (list_N(A,n))"
by (simp add: list_N_def)

lemma list_N_imp_list:
  "[| l \<in> list_N(A,n); n \<in> nat |] ==> l \<in> list(A)"
by (force simp add: list_eq_Union list_N_def)

lemma list_N_imp_length_lt [rule_format]:
     "n \<in> nat ==> \<forall>l \<in> list_N(A,n). length(l) < n"
apply (induct_tac n)
apply (auto simp add: list_N_0 list_N_succ
                      Nil_def [symmetric] Cons_def [symmetric])
done

lemma list_imp_list_N [rule_format]:
     "l \<in> list(A) ==> \<forall>n\<in>nat. length(l) < n \<longrightarrow> l \<in> list_N(A, n)"
apply (induct_tac l)
apply (force elim: natE)+
done

lemma list_N_imp_eq_length:
      "[|n \<in> nat; l \<notin> list_N(A, n); l \<in> list_N(A, succ(n))|]
       ==> n = length(l)"
apply (rule le_anti_sym)
 prefer 2 apply (simp add: list_N_imp_length_lt)
apply (frule list_N_imp_list, simp)
apply (simp add: not_lt_iff_le [symmetric])
apply (blast intro: list_imp_list_N)
done

text{*Express @{term list_rec} without using @{term rank} or @{term Vset},
neither of which is absolute.*}
lemma (in M_trivial) list_rec_eq:
  "l \<in> list(A) ==>
   list_rec(a,g,l) =
   transrec (succ(length(l)),
      \<lambda>x h. Lambda (list(A),
                    list_case' (a,
                           \<lambda>a l. g(a, l, h ` succ(length(l)) ` l)))) ` l"
apply (induct_tac l)
apply (subst transrec, simp)
apply (subst transrec)
apply (simp add: list_imp_list_N)
done

definition
  is_list_N :: "[i=>o,i,i,i] => o" where
    "is_list_N(M,A,n,Z) ==
      \<exists>zero[M]. empty(M,zero) &
                is_iterates(M, is_list_functor(M,A), zero, n, Z)"

definition
  mem_list :: "[i=>o,i,i] => o" where
    "mem_list(M,A,l) ==
      \<exists>n[M]. \<exists>listn[M].
       finite_ordinal(M,n) & is_list_N(M,A,n,listn) & l \<in> listn"

definition
  is_list :: "[i=>o,i,i] => o" where
    "is_list(M,A,Z) == \<forall>l[M]. l \<in> Z \<longleftrightarrow> mem_list(M,A,l)"

subsubsection{*Towards Absoluteness of @{term formula_rec}*}

consts   depth :: "i=>i"
primrec
  "depth(Member(x,y)) = 0"
  "depth(Equal(x,y))  = 0"
  "depth(Nand(p,q)) = succ(depth(p) \<union> depth(q))"
  "depth(Forall(p)) = succ(depth(p))"

lemma depth_type [TC]: "p \<in> formula ==> depth(p) \<in> nat"
by (induct_tac p, simp_all)

definition
  formula_N :: "i => i" where
    "formula_N(n) == (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)) ^ n (0)"

lemma Member_in_formula_N [simp]:
     "Member(x,y) \<in> formula_N(succ(n)) \<longleftrightarrow> x \<in> nat & y \<in> nat"
by (simp add: formula_N_def Member_def)

lemma Equal_in_formula_N [simp]:
     "Equal(x,y) \<in> formula_N(succ(n)) \<longleftrightarrow> x \<in> nat & y \<in> nat"
by (simp add: formula_N_def Equal_def)

lemma Nand_in_formula_N [simp]:
     "Nand(x,y) \<in> formula_N(succ(n)) \<longleftrightarrow> x \<in> formula_N(n) & y \<in> formula_N(n)"
by (simp add: formula_N_def Nand_def)

lemma Forall_in_formula_N [simp]:
     "Forall(x) \<in> formula_N(succ(n)) \<longleftrightarrow> x \<in> formula_N(n)"
by (simp add: formula_N_def Forall_def)

text{*These two aren't simprules because they reveal the underlying
formula representation.*}
lemma formula_N_0: "formula_N(0) = 0"
by (simp add: formula_N_def)

lemma formula_N_succ:
     "formula_N(succ(n)) =
      ((nat*nat) + (nat*nat)) + (formula_N(n) * formula_N(n) + formula_N(n))"
by (simp add: formula_N_def)

lemma formula_N_imp_formula:
  "[| p \<in> formula_N(n); n \<in> nat |] ==> p \<in> formula"
by (force simp add: formula_eq_Union formula_N_def)

lemma formula_N_imp_depth_lt [rule_format]:
     "n \<in> nat ==> \<forall>p \<in> formula_N(n). depth(p) < n"
apply (induct_tac n)
apply (auto simp add: formula_N_0 formula_N_succ
                      depth_type formula_N_imp_formula Un_least_lt_iff
                      Member_def [symmetric] Equal_def [symmetric]
                      Nand_def [symmetric] Forall_def [symmetric])
done

lemma formula_imp_formula_N [rule_format]:
     "p \<in> formula ==> \<forall>n\<in>nat. depth(p) < n \<longrightarrow> p \<in> formula_N(n)"
apply (induct_tac p)
apply (simp_all add: succ_Un_distrib Un_least_lt_iff)
apply (force elim: natE)+
done

lemma formula_N_imp_eq_depth:
      "[|n \<in> nat; p \<notin> formula_N(n); p \<in> formula_N(succ(n))|]
       ==> n = depth(p)"
apply (rule le_anti_sym)
 prefer 2 apply (simp add: formula_N_imp_depth_lt)
apply (frule formula_N_imp_formula, simp)
apply (simp add: not_lt_iff_le [symmetric])
apply (blast intro: formula_imp_formula_N)
done

text{*This result and the next are unused.*}
lemma formula_N_mono [rule_format]:
  "[| m \<in> nat; n \<in> nat |] ==> m\<le>n \<longrightarrow> formula_N(m) \<subseteq> formula_N(n)"
apply (rule_tac m = m and n = n in diff_induct)
apply (simp_all add: formula_N_0 formula_N_succ, blast)
done

lemma formula_N_distrib:
  "[| m \<in> nat; n \<in> nat |] ==> formula_N(m \<union> n) = formula_N(m) \<union> formula_N(n)"
apply (rule_tac i = m and j = n in Ord_linear_le, auto)
apply (simp_all add: subset_Un_iff [THEN iffD1] subset_Un_iff2 [THEN iffD1]
                     le_imp_subset formula_N_mono)
done

definition
  is_formula_N :: "[i=>o,i,i] => o" where
    "is_formula_N(M,n,Z) ==
      \<exists>zero[M]. empty(M,zero) &
                is_iterates(M, is_formula_functor(M), zero, n, Z)"

definition
  mem_formula :: "[i=>o,i] => o" where
    "mem_formula(M,p) ==
      \<exists>n[M]. \<exists>formn[M].
       finite_ordinal(M,n) & is_formula_N(M,n,formn) & p \<in> formn"

definition
  is_formula :: "[i=>o,i] => o" where
    "is_formula(M,Z) == \<forall>p[M]. p \<in> Z \<longleftrightarrow> mem_formula(M,p)"

locale M_datatypes = M_trancl +
 assumes list_replacement1:
   "M(A) ==> iterates_replacement(M, is_list_functor(M,A), 0)"
  and list_replacement2:
   "M(A) ==> strong_replacement(M,
         \<lambda>n y. n\<in>nat & is_iterates(M, is_list_functor(M,A), 0, n, y))"
  and formula_replacement1:
   "iterates_replacement(M, is_formula_functor(M), 0)"
  and formula_replacement2:
   "strong_replacement(M,
         \<lambda>n y. n\<in>nat & is_iterates(M, is_formula_functor(M), 0, n, y))"
  and nth_replacement:
   "M(l) ==> iterates_replacement(M, %l t. is_tl(M,l,t), l)"

subsubsection{*Absoluteness of the List Construction*}

lemma (in M_datatypes) list_replacement2':
  "M(A) ==> strong_replacement(M, \<lambda>n y. n\<in>nat & y = (\<lambda>X. {0} + A * X)^n (0))"
apply (insert list_replacement2 [of A])
apply (rule strong_replacement_cong [THEN iffD1])
apply (rule conj_cong [OF iff_refl iterates_abs [of "is_list_functor(M,A)"]])
apply (simp_all add: list_replacement1 relation1_def)
done

lemma (in M_datatypes) list_closed [intro,simp]:
     "M(A) ==> M(list(A))"
apply (insert list_replacement1)
by  (simp add: RepFun_closed2 list_eq_Union
               list_replacement2' relation1_def
               iterates_closed [of "is_list_functor(M,A)"])

text{*WARNING: use only with @{text "dest:"} or with variables fixed!*}
lemmas (in M_datatypes) list_into_M = transM [OF _ list_closed]

lemma (in M_datatypes) list_N_abs [simp]:
     "[|M(A); n\<in>nat; M(Z)|]
      ==> is_list_N(M,A,n,Z) \<longleftrightarrow> Z = list_N(A,n)"
apply (insert list_replacement1)
apply (simp add: is_list_N_def list_N_def relation1_def nat_into_M
                 iterates_abs [of "is_list_functor(M,A)" _ "\<lambda>X. {0} + A*X"])
done

lemma (in M_datatypes) list_N_closed [intro,simp]:
     "[|M(A); n\<in>nat|] ==> M(list_N(A,n))"
apply (insert list_replacement1)
apply (simp add: is_list_N_def list_N_def relation1_def nat_into_M
                 iterates_closed [of "is_list_functor(M,A)"])
done

lemma (in M_datatypes) mem_list_abs [simp]:
     "M(A) ==> mem_list(M,A,l) \<longleftrightarrow> l \<in> list(A)"
apply (insert list_replacement1)
apply (simp add: mem_list_def list_N_def relation1_def list_eq_Union
                 iterates_closed [of "is_list_functor(M,A)"])
done

lemma (in M_datatypes) list_abs [simp]:
     "[|M(A); M(Z)|] ==> is_list(M,A,Z) \<longleftrightarrow> Z = list(A)"
apply (simp add: is_list_def, safe)
apply (rule M_equalityI, simp_all)
done

subsubsection{*Absoluteness of Formulas*}

lemma (in M_datatypes) formula_replacement2':
  "strong_replacement(M, \<lambda>n y. n\<in>nat & y = (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))^n (0))"
apply (insert formula_replacement2)
apply (rule strong_replacement_cong [THEN iffD1])
apply (rule conj_cong [OF iff_refl iterates_abs [of "is_formula_functor(M)"]])
apply (simp_all add: formula_replacement1 relation1_def)
done

lemma (in M_datatypes) formula_closed [intro,simp]:
     "M(formula)"
apply (insert formula_replacement1)
apply  (simp add: RepFun_closed2 formula_eq_Union
                  formula_replacement2' relation1_def
                  iterates_closed [of "is_formula_functor(M)"])
done

lemmas (in M_datatypes) formula_into_M = transM [OF _ formula_closed]

lemma (in M_datatypes) formula_N_abs [simp]:
     "[|n\<in>nat; M(Z)|]
      ==> is_formula_N(M,n,Z) \<longleftrightarrow> Z = formula_N(n)"
apply (insert formula_replacement1)
apply (simp add: is_formula_N_def formula_N_def relation1_def nat_into_M
                 iterates_abs [of "is_formula_functor(M)" _
                                  "\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)"])
done

lemma (in M_datatypes) formula_N_closed [intro,simp]:
     "n\<in>nat ==> M(formula_N(n))"
apply (insert formula_replacement1)
apply (simp add: is_formula_N_def formula_N_def relation1_def nat_into_M
                 iterates_closed [of "is_formula_functor(M)"])
done

lemma (in M_datatypes) mem_formula_abs [simp]:
     "mem_formula(M,l) \<longleftrightarrow> l \<in> formula"
apply (insert formula_replacement1)
apply (simp add: mem_formula_def relation1_def formula_eq_Union formula_N_def
                 iterates_closed [of "is_formula_functor(M)"])
done

lemma (in M_datatypes) formula_abs [simp]:
     "[|M(Z)|] ==> is_formula(M,Z) \<longleftrightarrow> Z = formula"
apply (simp add: is_formula_def, safe)
apply (rule M_equalityI, simp_all)
done

subsection{*Absoluteness for @{text \<epsilon>}-Closure: the @{term eclose} Operator*}

text{*Re-expresses eclose using "iterates"*}
lemma eclose_eq_Union:
     "eclose(A) = (\<Union>n\<in>nat. Union^n (A))"
apply (simp add: eclose_def)
apply (rule UN_cong)
apply (rule refl)
apply (induct_tac n)
apply (simp add: nat_rec_0)
apply (simp add: nat_rec_succ)
done

definition
  is_eclose_n :: "[i=>o,i,i,i] => o" where
    "is_eclose_n(M,A,n,Z) == is_iterates(M, big_union(M), A, n, Z)"

definition
  mem_eclose :: "[i=>o,i,i] => o" where
    "mem_eclose(M,A,l) ==
      \<exists>n[M]. \<exists>eclosen[M].
       finite_ordinal(M,n) & is_eclose_n(M,A,n,eclosen) & l \<in> eclosen"

definition
  is_eclose :: "[i=>o,i,i] => o" where
    "is_eclose(M,A,Z) == \<forall>u[M]. u \<in> Z \<longleftrightarrow> mem_eclose(M,A,u)"

locale M_eclose = M_datatypes +
 assumes eclose_replacement1:
   "M(A) ==> iterates_replacement(M, big_union(M), A)"
  and eclose_replacement2:
   "M(A) ==> strong_replacement(M,
         \<lambda>n y. n\<in>nat & is_iterates(M, big_union(M), A, n, y))"

lemma (in M_eclose) eclose_replacement2':
  "M(A) ==> strong_replacement(M, \<lambda>n y. n\<in>nat & y = Union^n (A))"
apply (insert eclose_replacement2 [of A])
apply (rule strong_replacement_cong [THEN iffD1])
apply (rule conj_cong [OF iff_refl iterates_abs [of "big_union(M)"]])
apply (simp_all add: eclose_replacement1 relation1_def)
done

lemma (in M_eclose) eclose_closed [intro,simp]:
     "M(A) ==> M(eclose(A))"
apply (insert eclose_replacement1)
by  (simp add: RepFun_closed2 eclose_eq_Union
               eclose_replacement2' relation1_def
               iterates_closed [of "big_union(M)"])

lemma (in M_eclose) is_eclose_n_abs [simp]:
     "[|M(A); n\<in>nat; M(Z)|] ==> is_eclose_n(M,A,n,Z) \<longleftrightarrow> Z = Union^n (A)"
apply (insert eclose_replacement1)
apply (simp add: is_eclose_n_def relation1_def nat_into_M
                 iterates_abs [of "big_union(M)" _ "Union"])
done

lemma (in M_eclose) mem_eclose_abs [simp]:
     "M(A) ==> mem_eclose(M,A,l) \<longleftrightarrow> l \<in> eclose(A)"
apply (insert eclose_replacement1)
apply (simp add: mem_eclose_def relation1_def eclose_eq_Union
                 iterates_closed [of "big_union(M)"])
done

lemma (in M_eclose) eclose_abs [simp]:
     "[|M(A); M(Z)|] ==> is_eclose(M,A,Z) \<longleftrightarrow> Z = eclose(A)"
apply (simp add: is_eclose_def, safe)
apply (rule M_equalityI, simp_all)
done

subsection {*Absoluteness for @{term transrec}*}

text{* @{prop "transrec(a,H) \<equiv> wfrec(Memrel(eclose({a})), a, H)"} *}

definition
  is_transrec :: "[i=>o, [i,i,i]=>o, i, i] => o" where
   "is_transrec(M,MH,a,z) ==
      \<exists>sa[M]. \<exists>esa[M]. \<exists>mesa[M].
       upair(M,a,a,sa) & is_eclose(M,sa,esa) & membership(M,esa,mesa) &
       is_wfrec(M,MH,mesa,a,z)"

definition
  transrec_replacement :: "[i=>o, [i,i,i]=>o, i] => o" where
   "transrec_replacement(M,MH,a) ==
      \<exists>sa[M]. \<exists>esa[M]. \<exists>mesa[M].
       upair(M,a,a,sa) & is_eclose(M,sa,esa) & membership(M,esa,mesa) &
       wfrec_replacement(M,MH,mesa)"

text{*The condition @{term "Ord(i)"} lets us use the simpler
  @{text "trans_wfrec_abs"} rather than @{text "trans_wfrec_abs"},
  which I haven't even proved yet. *}
theorem (in M_eclose) transrec_abs:
  "[|transrec_replacement(M,MH,i);  relation2(M,MH,H);
     Ord(i);  M(i);  M(z);
     \<forall>x[M]. \<forall>g[M]. function(g) \<longrightarrow> M(H(x,g))|]
   ==> is_transrec(M,MH,i,z) \<longleftrightarrow> z = transrec(i,H)"
by (simp add: trans_wfrec_abs transrec_replacement_def is_transrec_def
       transrec_def eclose_sing_Ord_eq wf_Memrel trans_Memrel relation_Memrel)

theorem (in M_eclose) transrec_closed:
     "[|transrec_replacement(M,MH,i);  relation2(M,MH,H);
        Ord(i);  M(i);
        \<forall>x[M]. \<forall>g[M]. function(g) \<longrightarrow> M(H(x,g))|]
      ==> M(transrec(i,H))"
by (simp add: trans_wfrec_closed transrec_replacement_def is_transrec_def
        transrec_def eclose_sing_Ord_eq wf_Memrel trans_Memrel relation_Memrel)

text{*Helps to prove instances of @{term transrec_replacement}*}
lemma (in M_eclose) transrec_replacementI:
   "[|M(a);
      strong_replacement (M,
                  \<lambda>x z. \<exists>y[M]. pair(M, x, y, z) &
                               is_wfrec(M,MH,Memrel(eclose({a})),x,y))|]
    ==> transrec_replacement(M,MH,a)"
by (simp add: transrec_replacement_def wfrec_replacement_def)

subsection{*Absoluteness for the List Operator @{term length}*}
text{*But it is never used.*}

definition
  is_length :: "[i=>o,i,i,i] => o" where
    "is_length(M,A,l,n) ==
       \<exists>sn[M]. \<exists>list_n[M]. \<exists>list_sn[M].
        is_list_N(M,A,n,list_n) & l \<notin> list_n &
        successor(M,n,sn) & is_list_N(M,A,sn,list_sn) & l \<in> list_sn"

lemma (in M_datatypes) length_abs [simp]:
     "[|M(A); l \<in> list(A); n \<in> nat|] ==> is_length(M,A,l,n) \<longleftrightarrow> n = length(l)"
apply (subgoal_tac "M(l) & M(n)")
 prefer 2 apply (blast dest: transM)
apply (simp add: is_length_def)
apply (blast intro: list_imp_list_N nat_into_Ord list_N_imp_eq_length
             dest: list_N_imp_length_lt)
done

text{*Proof is trivial since @{term length} returns natural numbers.*}
lemma (in M_trivial) length_closed [intro,simp]:
     "l \<in> list(A) ==> M(length(l))"
by (simp add: nat_into_M)

subsection {*Absoluteness for the List Operator @{term nth}*}

lemma nth_eq_hd_iterates_tl [rule_format]:
     "xs \<in> list(A) ==> \<forall>n \<in> nat. nth(n,xs) = hd' (tl'^n (xs))"
apply (induct_tac xs)
apply (simp add: iterates_tl_Nil hd'_Nil, clarify)
apply (erule natE)
apply (simp add: hd'_Cons)
apply (simp add: tl'_Cons iterates_commute)
done

lemma (in M_basic) iterates_tl'_closed:
     "[|n \<in> nat; M(x)|] ==> M(tl'^n (x))"
apply (induct_tac n, simp)
apply (simp add: tl'_Cons tl'_closed)
done

text{*Immediate by type-checking*}
lemma (in M_datatypes) nth_closed [intro,simp]:
     "[|xs \<in> list(A); n \<in> nat; M(A)|] ==> M(nth(n,xs))"
apply (case_tac "n < length(xs)")
 apply (blast intro: nth_type transM)
apply (simp add: not_lt_iff_le nth_eq_0)
done

definition
  is_nth :: "[i=>o,i,i,i] => o" where
    "is_nth(M,n,l,Z) ==
      \<exists>X[M]. is_iterates(M, is_tl(M), l, n, X) & is_hd(M,X,Z)"

lemma (in M_datatypes) nth_abs [simp]:
     "[|M(A); n \<in> nat; l \<in> list(A); M(Z)|]
      ==> is_nth(M,n,l,Z) \<longleftrightarrow> Z = nth(n,l)"
apply (subgoal_tac "M(l)")
 prefer 2 apply (blast intro: transM)
apply (simp add: is_nth_def nth_eq_hd_iterates_tl nat_into_M
                 tl'_closed iterates_tl'_closed
                 iterates_abs [OF _ relation1_tl] nth_replacement)
done

subsection{*Relativization and Absoluteness for the @{term formula} Constructors*}

definition
  is_Member :: "[i=>o,i,i,i] => o" where
     --{* because @{term "Member(x,y) \<equiv> Inl(Inl(\<langle>x,y\<rangle>))"}*}
    "is_Member(M,x,y,Z) ==
        \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inl(M,u,Z)"

lemma (in M_trivial) Member_abs [simp]:
     "[|M(x); M(y); M(Z)|] ==> is_Member(M,x,y,Z) \<longleftrightarrow> (Z = Member(x,y))"
by (simp add: is_Member_def Member_def)

lemma (in M_trivial) Member_in_M_iff [iff]:
     "M(Member(x,y)) \<longleftrightarrow> M(x) & M(y)"
by (simp add: Member_def)

definition
  is_Equal :: "[i=>o,i,i,i] => o" where
     --{* because @{term "Equal(x,y) \<equiv> Inl(Inr(\<langle>x,y\<rangle>))"}*}
    "is_Equal(M,x,y,Z) ==
        \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inr(M,p,u) & is_Inl(M,u,Z)"

lemma (in M_trivial) Equal_abs [simp]:
     "[|M(x); M(y); M(Z)|] ==> is_Equal(M,x,y,Z) \<longleftrightarrow> (Z = Equal(x,y))"
by (simp add: is_Equal_def Equal_def)

lemma (in M_trivial) Equal_in_M_iff [iff]: "M(Equal(x,y)) \<longleftrightarrow> M(x) & M(y)"
by (simp add: Equal_def)

definition
  is_Nand :: "[i=>o,i,i,i] => o" where
     --{* because @{term "Nand(x,y) \<equiv> Inr(Inl(\<langle>x,y\<rangle>))"}*}
    "is_Nand(M,x,y,Z) ==
        \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inr(M,u,Z)"

lemma (in M_trivial) Nand_abs [simp]:
     "[|M(x); M(y); M(Z)|] ==> is_Nand(M,x,y,Z) \<longleftrightarrow> (Z = Nand(x,y))"
by (simp add: is_Nand_def Nand_def)

lemma (in M_trivial) Nand_in_M_iff [iff]: "M(Nand(x,y)) \<longleftrightarrow> M(x) & M(y)"
by (simp add: Nand_def)

definition
  is_Forall :: "[i=>o,i,i] => o" where
     --{* because @{term "Forall(x) \<equiv> Inr(Inr(p))"}*}
    "is_Forall(M,p,Z) == \<exists>u[M]. is_Inr(M,p,u) & is_Inr(M,u,Z)"

lemma (in M_trivial) Forall_abs [simp]:
     "[|M(x); M(Z)|] ==> is_Forall(M,x,Z) \<longleftrightarrow> (Z = Forall(x))"
by (simp add: is_Forall_def Forall_def)

lemma (in M_trivial) Forall_in_M_iff [iff]: "M(Forall(x)) \<longleftrightarrow> M(x)"
by (simp add: Forall_def)

subsection {*Absoluteness for @{term formula_rec}*}

definition
  formula_rec_case :: "[[i,i]=>i, [i,i]=>i, [i,i,i,i]=>i, [i,i]=>i, i, i] => i" where
    --{* the instance of @{term formula_case} in @{term formula_rec}*}
   "formula_rec_case(a,b,c,d,h) ==
        formula_case (a, b,
                \<lambda>u v. c(u, v, h ` succ(depth(u)) ` u,
                              h ` succ(depth(v)) ` v),
                \<lambda>u. d(u, h ` succ(depth(u)) ` u))"

text{*Unfold @{term formula_rec} to @{term formula_rec_case}.
     Express @{term formula_rec} without using @{term rank} or @{term Vset},
neither of which is absolute.*}
lemma (in M_trivial) formula_rec_eq:
  "p \<in> formula ==>
   formula_rec(a,b,c,d,p) =
   transrec (succ(depth(p)),
             \<lambda>x h. Lambda (formula, formula_rec_case(a,b,c,d,h))) ` p"
apply (simp add: formula_rec_case_def)
apply (induct_tac p)
   txt{*Base case for @{term Member}*}
   apply (subst transrec, simp add: formula.intros)
  txt{*Base case for @{term Equal}*}
  apply (subst transrec, simp add: formula.intros)
 txt{*Inductive step for @{term Nand}*}
 apply (subst transrec)
 apply (simp add: succ_Un_distrib formula.intros)
txt{*Inductive step for @{term Forall}*}
apply (subst transrec)
apply (simp add: formula_imp_formula_N formula.intros)
done

subsubsection{*Absoluteness for the Formula Operator @{term depth}*}

definition
  is_depth :: "[i=>o,i,i] => o" where
    "is_depth(M,p,n) ==
       \<exists>sn[M]. \<exists>formula_n[M]. \<exists>formula_sn[M].
        is_formula_N(M,n,formula_n) & p \<notin> formula_n &
        successor(M,n,sn) & is_formula_N(M,sn,formula_sn) & p \<in> formula_sn"

lemma (in M_datatypes) depth_abs [simp]:
     "[|p \<in> formula; n \<in> nat|] ==> is_depth(M,p,n) \<longleftrightarrow> n = depth(p)"
apply (subgoal_tac "M(p) & M(n)")
 prefer 2 apply (blast dest: transM)
apply (simp add: is_depth_def)
apply (blast intro: formula_imp_formula_N nat_into_Ord formula_N_imp_eq_depth
             dest: formula_N_imp_depth_lt)
done

text{*Proof is trivial since @{term depth} returns natural numbers.*}
lemma (in M_trivial) depth_closed [intro,simp]:
     "p \<in> formula ==> M(depth(p))"
by (simp add: nat_into_M)

subsubsection{*@{term is_formula_case}: relativization of @{term formula_case}*}

definition
 is_formula_case ::
    "[i=>o, [i,i,i]=>o, [i,i,i]=>o, [i,i,i]=>o, [i,i]=>o, i, i] => o" where
  --{*no constraint on non-formulas*}
  "is_formula_case(M, is_a, is_b, is_c, is_d, p, z) ==
      (\<forall>x[M]. \<forall>y[M]. finite_ordinal(M,x) \<longrightarrow> finite_ordinal(M,y) \<longrightarrow>
                      is_Member(M,x,y,p) \<longrightarrow> is_a(x,y,z)) &
      (\<forall>x[M]. \<forall>y[M]. finite_ordinal(M,x) \<longrightarrow> finite_ordinal(M,y) \<longrightarrow>
                      is_Equal(M,x,y,p) \<longrightarrow> is_b(x,y,z)) &
      (\<forall>x[M]. \<forall>y[M]. mem_formula(M,x) \<longrightarrow> mem_formula(M,y) \<longrightarrow>
                     is_Nand(M,x,y,p) \<longrightarrow> is_c(x,y,z)) &
      (\<forall>x[M]. mem_formula(M,x) \<longrightarrow> is_Forall(M,x,p) \<longrightarrow> is_d(x,z))"

lemma (in M_datatypes) formula_case_abs [simp]:
     "[| Relation2(M,nat,nat,is_a,a); Relation2(M,nat,nat,is_b,b);
         Relation2(M,formula,formula,is_c,c); Relation1(M,formula,is_d,d);
         p \<in> formula; M(z) |]
      ==> is_formula_case(M,is_a,is_b,is_c,is_d,p,z) \<longleftrightarrow>
          z = formula_case(a,b,c,d,p)"
apply (simp add: formula_into_M is_formula_case_def)
apply (erule formula.cases)
   apply (simp_all add: Relation1_def Relation2_def)
done

lemma (in M_datatypes) formula_case_closed [intro,simp]:
  "[|p \<in> formula;
     \<forall>x[M]. \<forall>y[M]. x\<in>nat \<longrightarrow> y\<in>nat \<longrightarrow> M(a(x,y));
     \<forall>x[M]. \<forall>y[M]. x\<in>nat \<longrightarrow> y\<in>nat \<longrightarrow> M(b(x,y));
     \<forall>x[M]. \<forall>y[M]. x\<in>formula \<longrightarrow> y\<in>formula \<longrightarrow> M(c(x,y));
     \<forall>x[M]. x\<in>formula \<longrightarrow> M(d(x))|] ==> M(formula_case(a,b,c,d,p))"
by (erule formula.cases, simp_all)

subsubsection {*Absoluteness for @{term formula_rec}: Final Results*}

definition
  is_formula_rec :: "[i=>o, [i,i,i]=>o, i, i] => o" where
    --{* predicate to relativize the functional @{term formula_rec}*}
   "is_formula_rec(M,MH,p,z)  ==
      \<exists>dp[M]. \<exists>i[M]. \<exists>f[M]. finite_ordinal(M,dp) & is_depth(M,p,dp) &
             successor(M,dp,i) & fun_apply(M,f,p,z) & is_transrec(M,MH,i,f)"

text{*Sufficient conditions to relativize the instance of @{term formula_case}
      in @{term formula_rec}*}
lemma (in M_datatypes) Relation1_formula_rec_case:
     "[|Relation2(M, nat, nat, is_a, a);
        Relation2(M, nat, nat, is_b, b);
        Relation2 (M, formula, formula,
           is_c, \<lambda>u v. c(u, v, h`succ(depth(u))`u, h`succ(depth(v))`v));
        Relation1(M, formula,
           is_d, \<lambda>u. d(u, h ` succ(depth(u)) ` u));
        M(h) |]
      ==> Relation1(M, formula,
                         is_formula_case (M, is_a, is_b, is_c, is_d),
                         formula_rec_case(a, b, c, d, h))"
apply (simp (no_asm) add: formula_rec_case_def Relation1_def)
apply (simp add: formula_case_abs)
done

text{*This locale packages the premises of the following theorems,
      which is the normal purpose of locales.  It doesn't accumulate
      constraints on the class @{term M}, as in most of this deveopment.*}
locale Formula_Rec = M_eclose +
  fixes a and is_a and b and is_b and c and is_c and d and is_d and MH
  defines
      "MH(u::i,f,z) ==
        \<forall>fml[M]. is_formula(M,fml) \<longrightarrow>
             is_lambda
         (M, fml, is_formula_case (M, is_a, is_b, is_c(f), is_d(f)), z)"

  assumes a_closed: "[|x\<in>nat; y\<in>nat|] ==> M(a(x,y))"
      and a_rel:    "Relation2(M, nat, nat, is_a, a)"
      and b_closed: "[|x\<in>nat; y\<in>nat|] ==> M(b(x,y))"
      and b_rel:    "Relation2(M, nat, nat, is_b, b)"
      and c_closed: "[|x \<in> formula; y \<in> formula; M(gx); M(gy)|]
                     ==> M(c(x, y, gx, gy))"
      and c_rel:
         "M(f) ==>
          Relation2 (M, formula, formula, is_c(f),
             \<lambda>u v. c(u, v, f ` succ(depth(u)) ` u, f ` succ(depth(v)) ` v))"
      and d_closed: "[|x \<in> formula; M(gx)|] ==> M(d(x, gx))"
      and d_rel:
         "M(f) ==>
          Relation1(M, formula, is_d(f), \<lambda>u. d(u, f ` succ(depth(u)) ` u))"
      and fr_replace: "n \<in> nat ==> transrec_replacement(M,MH,n)"
      and fr_lam_replace:
           "M(g) ==>
            strong_replacement
              (M, \<lambda>x y. x \<in> formula &
                  y = \<langle>x, formula_rec_case(a,b,c,d,g,x)\<rangle>)"

lemma (in Formula_Rec) formula_rec_case_closed:
    "[|M(g); p \<in> formula|] ==> M(formula_rec_case(a, b, c, d, g, p))"
by (simp add: formula_rec_case_def a_closed b_closed c_closed d_closed)

lemma (in Formula_Rec) formula_rec_lam_closed:
    "M(g) ==> M(Lambda (formula, formula_rec_case(a,b,c,d,g)))"
by (simp add: lam_closed2 fr_lam_replace formula_rec_case_closed)

lemma (in Formula_Rec) MH_rel2:
     "relation2 (M, MH,
             \<lambda>x h. Lambda (formula, formula_rec_case(a,b,c,d,h)))"
apply (simp add: relation2_def MH_def, clarify)
apply (rule lambda_abs2)
apply (rule Relation1_formula_rec_case)
apply (simp_all add: a_rel b_rel c_rel d_rel formula_rec_case_closed)
done

lemma (in Formula_Rec) fr_transrec_closed:
    "n \<in> nat
     ==> M(transrec
          (n, \<lambda>x h. Lambda(formula, formula_rec_case(a, b, c, d, h))))"
by (simp add: transrec_closed [OF fr_replace MH_rel2]
              nat_into_M formula_rec_lam_closed)

text{*The main two results: @{term formula_rec} is absolute for @{term M}.*}
theorem (in Formula_Rec) formula_rec_closed:
    "p \<in> formula ==> M(formula_rec(a,b,c,d,p))"
by (simp add: formula_rec_eq fr_transrec_closed
              transM [OF _ formula_closed])

theorem (in Formula_Rec) formula_rec_abs:
  "[| p \<in> formula; M(z)|]
   ==> is_formula_rec(M,MH,p,z) \<longleftrightarrow> z = formula_rec(a,b,c,d,p)"
by (simp add: is_formula_rec_def formula_rec_eq transM [OF _ formula_closed]
              transrec_abs [OF fr_replace MH_rel2] depth_type
              fr_transrec_closed formula_rec_lam_closed eq_commute)

end


section \<open>Agents and Messages\<close>

theory Message imports Main begin

datatype agent = Friend nat | Spy

subsection \<open>Public Key Cryptography\<close>

type_synonym key = nat

lemma insert_Key_singleton: "insert (Key K) H = Key ` {K} \<union> H" by blast

lemma insert_Key_image: "insert (Key K) (Key`KK \<union> C) = Key ` (insert K KK) \<union> C" by blast

consts pubK :: "agent \<Rightarrow> key"
axiomatization where
  inj_pubK:        "inj pubK"

lemmas [iff] = inj_pubK [THEN inj_eq]

lemma pubK_image_eq[simp]: "(pubK x \<in> pubK`A) = (x\<in>A)"
  by auto

consts invKey :: "key \<Rightarrow> key"
axiomatization where
  invKey [simp]: "invKey (invKey K) = K"

lemma inj_inv [simp]: "inj invKey"
proof (rule ccontr)
  assume "\<not> inj invKey"
  hence "\<exists>k k'. k\<noteq>k' \<and> invKey k=invKey k'" by (meson injI)
  then obtain k k' where "k\<noteq>k'" and "invKey k=invKey k'" by blast
  hence "invKey (invKey k) = invKey (invKey k')" by simp
  with invKey have "k=k'" by simp
  with `k\<noteq>k'` show False ..
qed

lemmas [iff] = inj_inv [THEN inj_eq]

lemma invKey_image_eq[simp]: "(invKey x \<in> invKey ` A) = (x\<in>A)"
  by auto

abbreviation priK :: "agent \<Rightarrow> key"
  where "priK x  \<equiv>  invKey (pubK x)"
axiomatization where
  priK_neq_pubK:   "priK A \<noteq> pubK B"

declare o_def [simp]
lemma inj_priK: "inj priK" using inj_compose[OF inj_inv inj_pubK]
  by simp
lemmas [iff] = inj_priK [THEN inj_eq]
lemmas [iff] = priK_neq_pubK priK_neq_pubK [THEN not_sym]

lemma priK_pubK_image_eq[simp]: "(priK x \<notin> pubK`A)"
  by auto

subsection \<open>Messages\<close>

datatype msg = Agent  agent
             | Nonce  nat
             | Key    key
             | MPair  msg msg
             | Crypt  key msg

lemma Friend_image_eq [simp]: "(Friend x \<in> Friend`A) = (x\<in>A)"
  by auto

lemma Key_image_eq [simp]: "(Key x \<in> Key`A) = (x\<in>A)"
  by auto

lemma Nonce_Key_image_eq [simp]: "(Nonce x \<notin> Key`A)"
  by auto

lemma Crypt_notin_image_Key [simp]: "Crypt K X \<notin> Key ` A"
  by auto

abbreviation initState::"msg set" where
"initState \<equiv> insert (Key (priK Spy)) (Key ` range pubK)"

syntax
  "_MTuple"      :: "['a, args] \<Rightarrow> 'a * 'b"       ("(2\<lbrace>_,/ _\<rbrace>)")
translations
  "\<lbrace>x, y, z\<rbrace>"   == "\<lbrace>x, \<lbrace>y, z\<rbrace>\<rbrace>"
  "\<lbrace>x, y\<rbrace>"      == "CONST MPair x y"

subsubsection \<open>Keys useful for decryption\<close>

definition keysFor :: "msg set \<Rightarrow> key set" where
  "keysFor H == invKey ` {K. \<exists>X. Crypt K X \<in> H}"

lemma keysFor_empty [simp]: "keysFor {} = {}"
by (unfold keysFor_def, blast)

lemma keysFor_Un [simp]: "keysFor (H \<union> H') = keysFor H \<union> keysFor H'"
by (unfold keysFor_def, blast)

lemma keysFor_UN [simp]: "keysFor (\<Union>i\<in>A. H i) = (\<Union>i\<in>A. keysFor (H i))"
by (unfold keysFor_def, blast)

lemma keysFor_mono: "G \<subseteq> H \<Longrightarrow> keysFor(G) \<subseteq> keysFor(H)"
by (unfold keysFor_def, blast)

lemma keysFor_insert_Agent [simp]: "keysFor (insert (Agent A) H) = keysFor H"
by (unfold keysFor_def, auto)

lemma keysFor_insert_Nonce [simp]: "keysFor (insert (Nonce N) H) = keysFor H"
by (unfold keysFor_def, auto)

lemma keysFor_insert_Key [simp]: "keysFor (insert (Key K) H) = keysFor H"
by (unfold keysFor_def, auto)

lemma keysFor_insert_MPair [simp]: "keysFor (insert \<lbrace>X,Y\<rbrace> H) = keysFor H"
by (unfold keysFor_def, auto)

lemma keysFor_insert_Crypt [simp]:
    "keysFor (insert (Crypt K X) H) = insert (invKey K) (keysFor H)"
by (unfold keysFor_def, auto)

lemma keysFor_image_Key [simp]: "keysFor (Key`E) = {}"
by (unfold keysFor_def, auto)

lemma Crypt_imp_invKey_keysFor: "Crypt K X \<in> H \<Longrightarrow> invKey K \<in> keysFor H"
by (unfold keysFor_def, blast)

subsubsection \<open>Parts\<close>

inductive_set
  parts :: "msg set \<Rightarrow> msg set"
  for H :: "msg set"
  where
    Inj [intro]:               "X \<in> H \<Longrightarrow> X \<in> parts H"
  | Fst:         "\<lbrace>X,Y\<rbrace>   \<in> parts H \<Longrightarrow> X \<in> parts H"
  | Snd:         "\<lbrace>X,Y\<rbrace>   \<in> parts H \<Longrightarrow> Y \<in> parts H"
  | Body:        "Crypt K X \<in> parts H \<Longrightarrow> X \<in> parts H"

lemma parts_mono:
  assumes "G \<subseteq> H"
  shows "parts(G) \<subseteq> parts(H)"
proof
  fix x
  assume "x \<in> parts(G)"
  then show "x \<in> parts(H)"
  proof (induction rule: parts.induct)
    case (Inj X)
    then show ?case using assms by auto
  next
    case (Fst X Y)
    then show ?case using parts.Fst by simp
  next
    case (Snd X Y)
    then show ?case using parts.Snd by simp
  next
    case (Body K X)
    then show ?case using parts.Body by simp
  qed
qed

lemma MPair_parts: "\<lbrakk> \<lbrace>X,Y\<rbrace> \<in> parts H; \<lbrakk> X \<in> parts H; Y \<in> parts H \<rbrakk> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
by (blast dest: parts.Fst parts.Snd)

declare MPair_parts [elim!]  parts.Body [dest!]

lemma parts_increasing: "H \<subseteq> parts(H)" by blast

lemmas parts_insertI = subset_insertI [THEN parts_mono, THEN subsetD]

lemma parts_empty [simp]: "parts {} = {}"
proof
    show "parts {} \<subseteq> {}"
    proof
      fix x
      assume "x \<in> parts {}"
      hence False
      proof (induction rule: parts.induct)
        fix X assume "X \<in> {}" thus False by simp
      next
        fix X Y assume "\<lbrace>X, Y\<rbrace> \<in> parts {}" and False thus False ..
      next
        fix X Y assume "\<lbrace>X, Y\<rbrace> \<in> parts {}" and "False" thus False ..
      next
        fix K X assume "Crypt K X \<in> parts {}" and False thus False ..
      qed
      thus "x \<in> {}" ..
    qed
  next
    show "{} \<subseteq> parts {}" ..
qed

lemma parts_emptyE [elim!]: "X\<in> parts{} \<Longrightarrow> P" by simp

lemma parts_singleton: "X\<in> parts H \<Longrightarrow> \<exists>Y\<in>H. X\<in> parts {Y}" by (erule parts.induct, fast+)

lemma parts_Un_subset1: "parts(G) \<union> parts(H) \<subseteq> parts(G \<union> H)"
by (intro Un_least parts_mono Un_upper1 Un_upper2)

lemma parts_Un_subset2: "parts(G \<union> H) \<subseteq> parts(G) \<union> parts(H)"
proof
  fix x
  assume "x \<in> parts (G \<union> H)"
  thus "x \<in> parts G \<union> parts H"
  proof (induction rule: parts.induct)
    case (Inj X)
    then show ?case by auto
  next
    case (Fst X Y)
    then show ?case by auto
  next
    case (Snd X Y)
    then show ?case by auto
  next
    case (Body K X)
    then show ?case by auto
  qed
qed

lemma parts_Un [simp]: "parts(G \<union> H) = parts(G) \<union> parts(H)"
by (intro equalityI parts_Un_subset1 parts_Un_subset2)

lemma parts_insert: "parts (insert X H) = parts {X} \<union> parts H"
proof -
  from insert_is_Un have "insert X H = {X} \<union> H" by simp
  thus ?thesis by (simp only: parts_Un)
qed

lemma parts_insert2: "parts (insert X (insert Y H)) = parts {X} \<union> parts {Y} \<union> parts H"
proof -
  have "parts (insert X (insert Y H)) = parts {X} \<union> parts (insert Y H)" using parts_insert[of "X" "insert Y H"] by simp
  moreover have "parts (insert Y H) = parts {Y} \<union> parts H" using parts_insert by blast
  ultimately show ?thesis by auto
qed

lemma parts_UN_subset1: "(\<Union>x\<in>A. parts(H x)) \<subseteq> parts(\<Union>x\<in>A. H x)"
by (intro UN_least parts_mono UN_upper)

lemma parts_UN_subset2: "parts(\<Union>x\<in>A. H x) \<subseteq> (\<Union>x\<in>A. parts(H x))"
proof
  fix x
  assume "x \<in> parts (\<Union> (H ` A))"
  thus "x \<in> (\<Union>x\<in>A. parts (H x))"
  proof (induction rule: parts.induct)
    case (Inj X)
    then show ?case by auto
  next
    case (Fst X Y)
    then show ?case by auto
  next
    case (Snd X Y)
    then show ?case by auto
  next
    case (Body K X)
    then show ?case by auto
  qed
qed

lemma parts_UN [simp]: "parts(\<Union>x\<in>A. H x) = (\<Union>x\<in>A. parts(H x))"
by (intro equalityI parts_UN_subset1 parts_UN_subset2)

lemmas in_parts_UnE = parts_Un [THEN equalityD1, THEN subsetD, THEN UnE]

declare in_parts_UnE [elim!]

lemma parts_insert_subset: "insert X (parts H) \<subseteq> parts(insert X H)"
by (blast intro: parts_mono [THEN [2] rev_subsetD])

lemma parts_partsD [dest!]: "X \<in> parts (parts H) \<Longrightarrow> X\<in> parts H"
by (erule parts.induct, blast+)

lemma parts_idem [simp]: "parts (parts H) = parts H"
by blast

lemma parts_subset_iff [simp]: "(parts G \<subseteq> parts H) = (G \<subseteq> parts H)"
proof
    assume "parts G \<subseteq> parts H"
    thus "G \<subseteq> parts H" by auto
  next
    assume "G \<subseteq> parts H"
    show "parts G \<subseteq> parts H"
      using \<open>G \<subseteq> parts H\<close> parts_mono by auto
qed

lemma parts_trans: "[| X\<in> parts G;  G \<subseteq> parts H |] ==> X\<in> parts H"
by (drule parts_mono, blast)

lemma parts_cut: "\<lbrakk> Y\<in> parts (insert X G);  X\<in> parts H \<rbrakk> \<Longrightarrow> Y\<in> parts (G \<union> H)"
by (blast intro: parts_trans)

lemma parts_cut_eq [simp]: "X\<in> parts H ==> parts (insert X H) = parts H"
by (force dest!: parts_cut intro: parts_insertI)

lemmas parts_insert_eq_I = equalityI [OF subsetI parts_insert_subset]

lemma parts_insert_Agent [simp]: "parts (insert (Agent agt) H) = insert (Agent agt) (parts H)"
proof (rule parts_insert_eq_I)
  fix x
  assume "x \<in> parts (insert (Agent agt) H)"
  thus "x \<in> insert (Agent agt) (parts H)"
  proof (induction rule:parts.induct)
    case (Inj X)
    then show ?case by auto
  next
    case (Fst X Y)
    then show ?case by auto
  next
    case (Snd X Y)
    then show ?case by auto
  next
    case (Body K X)
    then show ?case by auto
  qed
qed

lemma parts_insert_Nonce [simp]: "parts (insert (Nonce N) H) = insert (Nonce N) (parts H)"
proof (rule parts_insert_eq_I)
  fix x
  assume "x \<in> parts (insert (Nonce N) H)"
  thus "x \<in> insert (Nonce N) (parts H)"
  proof (induction rule:parts.induct)
    case (Inj X)
    then show ?case by auto
  next
    case (Fst X Y)
    then show ?case by auto
  next
    case (Snd X Y)
    then show ?case by auto
  next
    case (Body K X)
    then show ?case by auto
  qed
qed

lemma parts_insert_Key [simp]: "parts (insert (Key K) H) = insert (Key K) (parts H)"
proof (rule parts_insert_eq_I)
  fix x
  assume "x \<in> parts (insert (Key K) H)"
  thus "x \<in> insert (Key K) (parts H)"
  proof (induction rule:parts.induct)
    case (Inj X)
    then show ?case by auto
  next
    case (Fst X Y)
    then show ?case by auto
  next
    case (Snd X Y)
    then show ?case by auto
  next
    case (Body K X)
    then show ?case by auto
  qed
qed

lemma parts_insert_Crypt [simp]: "parts (insert (Crypt K X) H) = insert (Crypt K X) (parts (insert X H))"
proof
  show "parts (insert (Crypt K X) H) \<subseteq> insert (Crypt K X) (parts (insert X H))"
  proof
    fix x
    assume "x \<in> parts (insert (Crypt K X) H)"
    thus "x \<in> insert (Crypt K X) (parts (insert X H))"
    proof (induction rule:parts.induct)
      case (Inj X)
      then show ?case by auto
    next
      case (Fst X Y)
      then show ?case by auto
    next
      case (Snd X Y)
      then show ?case by auto
    next
      case (Body K X)
      then show ?case by auto
    qed
  qed
next
  show "insert (Crypt K X) (parts (insert X H)) \<subseteq> parts (insert (Crypt K X) H)"
  proof
    fix x
    assume "x \<in> insert (Crypt K X) (parts (insert X H))"
    thus "x \<in> parts (insert (Crypt K X) H)"
    proof
      assume "x = Crypt K X"
      thus "x \<in> parts (insert (Crypt K X) H)" by (simp add: parts.Inj)
    next
      assume "x \<in> parts (insert X H)"
      thus "x \<in> parts (insert (Crypt K X) H)"
      proof (induction rule:parts.induct)
        case (Inj X)
        then show ?case by (metis insert_iff parts.simps)
      next
        case (Fst X Y)
        then show ?case by auto
      next
        case (Snd X Y)
        then show ?case by auto
      next
        case (Body K X)
        then show ?case by auto
      qed
    qed
  qed
qed

lemma parts_insert_MPair [simp]: "parts (insert \<lbrace>X,Y\<rbrace> H) = insert \<lbrace>X,Y\<rbrace> (parts (insert X (insert Y H)))"
proof
  show "parts (insert \<lbrace>X,Y\<rbrace> H) \<subseteq> insert \<lbrace>X,Y\<rbrace> (parts (insert X (insert Y H)))"
  proof
    fix x
    assume "x \<in>parts (insert \<lbrace>X,Y\<rbrace> H)"
    thus "x \<in> insert \<lbrace>X,Y\<rbrace> (parts (insert X (insert Y H)))"
    proof (induction rule:parts.induct)
      case (Inj X)
      then show ?case by auto
    next
      case (Fst X Y)
      then show ?case by auto
    next
      case (Snd X Y)
      then show ?case by auto
    next
      case (Body K X)
      then show ?case by auto
    qed
  qed
next
  show "insert \<lbrace>X,Y\<rbrace> (parts (insert X (insert Y H))) \<subseteq> parts (insert \<lbrace>X,Y\<rbrace> H)"
  proof
    fix x
    assume "x \<in> insert \<lbrace>X,Y\<rbrace> (parts (insert X (insert Y H)))"
    thus "x \<in> parts (insert \<lbrace>X,Y\<rbrace> H)"
    proof
      assume "x = \<lbrace>X,Y\<rbrace>"
      thus "x \<in> parts (insert \<lbrace>X,Y\<rbrace> H)" by (simp add: parts.Inj)
    next
      assume "x \<in> parts (insert X (insert Y H))"
      thus "x \<in> parts (insert \<lbrace>X,Y\<rbrace> H)"
      proof (induction rule:parts.induct)
        case (Inj X)
        then show ?case by (metis insert_iff parts.simps)
      next
        case (Fst X Y)
        then show ?case by auto
      next
        case (Snd X Y)
        then show ?case by auto
      next
        case (Body K X)
        then show ?case by auto
      qed
    qed
  qed
qed

lemma parts_image_Key [simp]: "parts (Key`N) = Key`N"
proof
  show "parts (Key ` N) \<subseteq> Key ` N"
  proof
    fix x
    assume "x \<in> parts (Key ` N)"
    thus "x \<in> Key ` N"
    proof (induction rule:parts.induct)
      case (Inj X)
      then show ?case by simp
    next
      case (Fst X Y)
      then show ?case by auto
    next
      case (Snd X Y)
      then show ?case by auto
    next
      case (Body K X)
      then show ?case by auto
    qed
  qed
  next
  show "Key ` N \<subseteq> parts (Key ` N)" by auto
qed

lemma msg_Nonce_supply: "\<exists>N. \<forall>n. N\<le>n \<longrightarrow> Nonce n \<notin> parts {msg}"
proof (induction)
  case (Agent x)
  then show ?case by simp
next
  case (Nonce x)
  have "\<forall>n\<ge>Suc x. Nonce n \<notin> parts {Nonce x}" by simp
  thus ?case by blast
next
  case (Key x)
  then show ?case by simp
next
  case (MPair x1a x2a)
  then obtain n1 n2 where "\<forall>n\<ge>n1. Nonce n \<notin> parts {x1a}"
  and "\<forall>n\<ge>n2. Nonce n \<notin> parts {x2a}" by auto
  hence "\<forall>n\<ge>n1 + n2. Nonce n \<notin> parts {x1a} \<and> Nonce n \<notin> parts {x2a}" by simp
  moreover have "parts {x1a, x2a} = parts {x1a} \<union> parts {x2a}" using parts_insert by blast
  ultimately show ?case by auto
next
  case (Crypt x1a x2a)
  then show ?case by simp
qed

lemma parts_insert_subset_Un: "X \<in> G \<Longrightarrow> parts(insert X H) \<subseteq> parts G \<union> parts H"
by (rule subset_trans [OF parts_mono parts_Un_subset2], blast)

end


(*
 * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *)

theory RangeMap_Test
imports
  Lib.RangeMap
  Main
begin

section \<open>Examples for RangeMap\<close>

experiment begin

subsection \<open>Basic usage\<close>

local_setup \<open>
RangeMap.define_map
  (* Specification of names to generate. "name_opts_default" generates qualified names
     starting with the given string i.e. "test_map.foo". *)
  (RangeMap.name_opts_default "test_map")

  (* List of ((start key, end key), value).
     Key ranges include the start but not the end points. *)
  [((@{cterm "1::nat"}, @{cterm "2::nat"}), @{cterm "True"}),
   ((@{cterm "2::nat"}, @{cterm "3::nat"}), @{cterm "False"}),
   ((@{cterm "5::nat"}, @{cterm "8::nat"}), @{cterm "True \<and> False"}),
   (* Key ranges are allowed to be empty, such as this one *)
   ((@{cterm "8::nat"}, @{cterm "8::nat"}), @{cterm "True \<or> False"}),
   (* Test for input preservation; see below *)
   ((@{cterm "if True then 8::nat else undefined"}, @{cterm "6 + 7 :: nat"}),
    @{cterm "False \<longrightarrow> True"})
  ]

  (* Key and value types. *)
  @{typ nat} @{typ bool}
  (* Key comparison solver. This should be a conversion that converts
     key comparisons to True or False. *)
  (Simplifier.rewrite @{context})
\<close>
print_theorems

text \<open>
  Generated definitions for lookup tree and list:
\<close>
thm test_map.tree_def test_map.list_def

text \<open>
  Lookup theorems:
\<close>
thm test_map.lookups

text \<open>
  We also get lookup theorems for the start of each range (if nonempty).
  These can be useful simplifier equations in some scenarios.
\<close>
thm test_map.start_lookups

text \<open>
  Test that user-provided key terms are preserved verbatim.
\<close>
ML \<open>
@{assert}
  (Thm.prop_of @{thm test_map.lookups(5)[where x=x]}
   = @{term "Trueprop (((if True then 8 else undefined) \<le> x \<and> x < 6 + 7)
             = (RangeMap.lookup_range_tree test_map.tree x
                = Some ((if True then 8 else undefined, 6 + 7), False \<longrightarrow> True)))"});
\<close>

text \<open>
  The domain is just the union of all key ranges.
  In practice, maps tend to have many contiguous ranges. RangeMap also
  generates a "compact" domain theorem that merges adjacent ranges.
\<close>
thm test_map.domain test_map.domain_compact

subsection \<open>Test that empty maps are supported\<close>

local_setup \<open>
RangeMap.define_map
  (RangeMap.name_opts_default "empty_test")
  []
  @{typ int} @{typ int}
  Thm.reflexive (* unused *)
\<close>
print_theorems

subsection \<open>Stress testing\<close>
ML \<open>
fun range_map_test_legendre n =
  let fun P q x = if q*q > x then true else x mod q <> 0 andalso P (q+1) x;
  in (1 upto n)
     |> map (fn k => ((k*k, (k+1)*(k+1)), hd (filter (P 2) (k*k+1 upto (k+1)*(k+1))))) end;

fun range_map_test_legendre_ct T n =
  let fun cterm_of_num T n = Thm.cterm_of @{context} (HOLogic.mk_number T n);
  in range_map_test_legendre n
     |> map (fn ((ks, ke), v) => ((cterm_of_num T ks, cterm_of_num T ke), cterm_of_num T v)) end;
\<close>

local_setup \<open>
RangeMap.define_map
  (RangeMap.name_opts_default "legendre_10")
  (range_map_test_legendre_ct @{typ nat} 10)
  @{typ nat} @{typ nat}
  (FP_Eval.eval_conv @{context} (FP_Eval.make_rules @{thms rel_simps} []))
\<close>

local_setup \<open>
RangeMap.define_map
  (RangeMap.name_opts_default "legendre_100")
  (range_map_test_legendre_ct @{typ nat} 100)
  @{typ nat} @{typ nat}
  (FP_Eval.eval_conv @{context} (FP_Eval.make_rules @{thms rel_simps} []))
\<close>

local_setup \<open>
RangeMap.define_map
  (RangeMap.name_opts_default "legendre_1000")
  (range_map_test_legendre_ct @{typ nat} 1000)
  @{typ nat} @{typ nat}
  (FP_Eval.eval_conv @{context} (FP_Eval.make_rules @{thms rel_simps} []))
\<close>

(* Example of proof using range map *)
lemma legendre_100_values:
  "RangeMap.lookup_range_tree legendre_100.tree k = Some ((start, end), v)
   \<Longrightarrow> start < v \<and> v < end"
  (* Convert to list and expand *)
  apply (subst (asm) legendre_100.tree_list_lookup_eq)
  apply (drule RangeMap.range_map_of_Some)
  apply (clarsimp simp: legendre_100.list_def)
  apply linarith
  done

end

end


theory PreferenceLogicTestsApp1                (*Benzmüller & Fuenmayor, 2020*)
   imports PreferenceLogicBasics
begin

(*****Application-specific tests for the value ontology****)
(* EE variant (\<and>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>E\<^sub>E (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<and>B) \<^bold>\<prec>\<^sub>E\<^sub>E A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>E\<^sub>E B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>E\<^sub>E (C\<^bold>\<and>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>E\<^sub>E (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>E\<^sub>E B)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>E\<^sub>E A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(* EE variant (\<or>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>E\<^sub>E (A\<^bold>\<or>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<^bold>\<prec>\<^sub>E\<^sub>E A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>E\<^sub>E B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>E\<^sub>E (C\<^bold>\<or>B))\<rfloor>" by blast
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>E\<^sub>E (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>E\<^sub>E B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>E\<^sub>E A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast
(* total EE variant (\<and>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>E\<^sub>E (A\<^bold>\<and>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<and>B) \<prec>\<^sub>E\<^sub>E A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>E\<^sub>E B) \<^bold>\<rightarrow> (A \<prec>\<^sub>E\<^sub>E (C\<^bold>\<and>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>E\<^sub>E (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>E\<^sub>E B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<and>B) \<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> (B \<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast
(* total EE variant (\<or>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>E\<^sub>E (A\<^bold>\<or>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<or>B) \<prec>\<^sub>E\<^sub>E A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>E\<^sub>E B) \<^bold>\<rightarrow> (A \<prec>\<^sub>E\<^sub>E (C\<^bold>\<or>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>E\<^sub>E (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>E\<^sub>E B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<or>B) \<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> (B \<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>E\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<prec>\<^sub>E\<^sub>E A)\<rfloor>" by blast

(* AE variant (\<and>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>A\<^sub>E (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>E A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>E B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>E (C\<^bold>\<and>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>E (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>E B)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>A\<^sub>E A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast
(* AE variant (\<or>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>A\<^sub>E (A\<^bold>\<or>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>E A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>E B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>E (C\<^bold>\<or>B))\<rfloor>" by blast
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>E (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>E B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>E A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(* total AE variant (\<and>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>A\<^sub>E (A\<^bold>\<and>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>E A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>E B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>E (C\<^bold>\<and>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>E (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>E B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast
(* total AE variant (\<or>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>A\<^sub>E (A\<^bold>\<or>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>E A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>E B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>E (C\<^bold>\<or>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>E (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>E B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>A\<^sub>E A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>E A)\<rfloor>" by blast

(* AA variant (\<and>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>A\<^sub>A (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>A B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>A\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<^bold>\<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
(*--------------------------------------------------*)
lemma "\<lfloor>A \<prec>\<^sub>A\<^sub>A (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
lemma "\<lfloor>(A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
(* AA variant (\<or>)*)
lemma "\<lfloor>A \<^bold>\<prec>\<^sub>A\<^sub>A (A\<^bold>\<or>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<^bold>\<prec>\<^sub>A\<^sub>A B)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<^bold>\<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>(B \<^bold>\<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<^bold>\<prec>\<^sub>A\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(*---------------------------------------------------*)
lemma "\<lfloor>A \<prec>\<^sub>A\<^sub>A (A\<^bold>\<or>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A B)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>(B \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(* total AA variant (\<and>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>A\<^sub>A (A\<^bold>\<and>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
(* total AA variant (\<or>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<prec>\<^sub>A\<^sub>A (A\<^bold>\<or>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>A B) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B))\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<prec>\<^sub>A\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<prec>\<^sub>A\<^sub>A B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> (B \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<prec>\<^sub>A\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<prec>\<^sub>A\<^sub>A A)\<rfloor>" by blast

(* EA variant (\<and>)*)
lemma "\<not>\<lfloor>(A\<^bold>\<and>B) \<^bold>\<succ>\<^sub>E\<^sub>A A\<rfloor>" using rBR by blast
lemma "\<lfloor>A \<^bold>\<succ>\<^sub>E\<^sub>A (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<^bold>\<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<^bold>\<succ>\<^sub>E\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<and>B) \<^bold>\<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<^bold>\<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>(A \<^bold>\<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<^bold>\<succ>\<^sub>E\<^sub>A B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<^bold>\<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
(*--------------------------------------------*)
lemma "\<lfloor>(A\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>A \<succ>\<^sub>E\<^sub>A (A\<^bold>\<and>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<succ>\<^sub>E\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
(* EA variant (\<or>)*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<^bold>\<succ>\<^sub>E\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<not>\<lfloor>A \<^bold>\<succ>\<^sub>E\<^sub>A (A\<^bold>\<or>B)\<rfloor>" using rBR by blast
lemma "\<lfloor>(B \<^bold>\<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<^bold>\<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>((C\<^bold>\<or>B) \<^bold>\<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<^bold>\<succ>\<^sub>E\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(A \<^bold>\<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<^bold>\<succ>\<^sub>E\<^sub>A B)\<rfloor>" by blast
lemma "\<lfloor>(A \<^bold>\<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<^bold>\<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(*------------------------------------------------*)
lemma "\<lfloor>(A\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>A \<succ>\<^sub>E\<^sub>A (A\<^bold>\<or>B)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>(B \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A)\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
lemma "\<lfloor>((C\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "\<lfloor>(A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A B)\<rfloor>" by blast
lemma "\<lfloor>(A \<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B))\<rfloor>" nitpick[satisfy] nitpick oops (*contingent*)
(* total EA variant (\<and>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<succ>\<^sub>E\<^sub>A (A\<^bold>\<and>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<and>B) \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B)) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<and>B))\<rfloor>" by blast
(* total EA variant (\<or>)*)
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>A \<succ>\<^sub>E\<^sub>A (A\<^bold>\<or>B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(B \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> ((C\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>((C\<^bold>\<or>B) \<succ>\<^sub>E\<^sub>A A) \<^bold>\<rightarrow> (B \<succ>\<^sub>E\<^sub>A A)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B)) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A B)\<rfloor>" by blast
lemma "is_total SBR \<Longrightarrow> \<lfloor>(A \<succ>\<^sub>E\<^sub>A B) \<^bold>\<rightarrow> (A \<succ>\<^sub>E\<^sub>A (C\<^bold>\<or>B))\<rfloor>" by blast
end


theory weird_nat_op_assoc
imports Main
        "$HIPSTER_HOME/IsaHipster"
begin

datatype Nat = Z | S "Nat"

fun op :: "Nat => Nat => Nat => Nat => Nat" where
"op (Z) y (Z) x2 = x2"
| "op (Z) y (S x3) x2 = op Z y x3 (S x2)"
| "op (S x4) y (Z) x2 = op x4 y y x2"
| "op (S x4) y (S c) x2 = op (S x4) y c (S x2)"

(*hipster op *)

theorem x0 :
  "!! (a :: Nat) (b :: Nat) (c :: Nat) (d :: Nat) (e :: Nat) .
     (op (op a b Z Z) c d e) = (op a (op b c Z Z) d e)"
  by (tactic \<open>Subgoal.FOCUS_PARAMS (K (Tactic_Data.hard_tac @{context})) @{context} 1\<close>)

end