theory Lappend_append_assoc
  imports Main "$HIPSTER_HOME/IsaHipster"
    Lappend
    To_Llist
begin
setup Tactic_Data.set_coinduct_sledgehammer

(*cohipster lappend append to_llist*)
(*produced this in ca. 65 seconds*)
lemma lemma_af [thy_expl]: "lappend (to_llist y) (to_llist z) = to_llist (y @ z)"
  apply (induct y arbitrary: z)
  apply (simp add: lemma_aa)
  apply (simp add: lemma_ac)
  done

end


theory Prelude_ListNoNumbers__E5
imports "$HETS_ISABELLE_LIB/MainHC"
uses "$HETS_ISABELLE_LIB/prelude"
begin

setup "Header.initialize
       [\"Comp1\", \"IdDef\", \"FlipDef\", \"FstDef\", \"SndDef\",
        \"CurryDef\", \"UncurryDef\", \"NotFalse\", \"NotTrue\",
        \"AndFalse\", \"AndTrue\", \"AndSym\", \"OrDef\", \"OtherwiseDef\",
        \"NotFalse1\", \"NotTrue1\", \"notNot1\", \"notNot2\",
        \"EqualTDef\", \"EqualSymDef\", \"EqualReflex\", \"EqualTransT\",
        \"DiffDef\", \"DiffSymDef\", \"DiffTDef\", \"DiffFDef\", \"TE1\",
        \"TE2\", \"TE3\", \"TE4\", \"IBE1\", \"IBE2\", \"IBE3\", \"IBE4\",
        \"IBE5\", \"IBE6\", \"IBE7\", \"IBE8\", \"IUE1\", \"IUE2\",
        \"IOE01\", \"IOE02\", \"IOE03\", \"IOE04\", \"IOE05\", \"IOE06\",
        \"IOE07\", \"IOE08\", \"IOE09\", \"LeIrreflexivity\",
        \"LeTAsymmetry\", \"LeTTransitive\", \"LeTTotal\", \"GeDef\",
        \"GeIrreflexivity\", \"GeTAsymmetry\", \"GeTTransitive\",
        \"GeTTotal\", \"LeqDef\", \"LeqReflexivity\", \"LeqTTransitive\",
        \"LeqTTotal\", \"GeqDef\", \"GeqReflexivity\", \"GeqTTransitive\",
        \"GeqTTotal\", \"EqTSOrdRel\", \"EqFSOrdRel\", \"EqTOrdRel\",
        \"EqFOrdRel\", \"EqTOrdTSubstE\", \"EqTOrdFSubstE\",
        \"EqTOrdTSubstD\", \"EqTOrdFSubstD\", \"LeTGeFEqFRel\",
        \"LeFGeTEqTRel\", \"LeTGeTRel\", \"LeFGeFRel\", \"LeqTGetTRel\",
        \"LeqFGetFRel\", \"GeTLeTRel\", \"GeFLeFRel\", \"GeqTLeqTRel\",
        \"GeqFLeqFRel\", \"LeqTGeFRel\", \"LeqFGeTRel\", \"GeTLeFEqFRel\",
        \"GeFLeTEqTRel\", \"GeqTLeFRel\", \"GeqFLeTRel\",
        \"LeqTLeTEqTRel\", \"LeqFLeFEqFRel\", \"GeqTGeTEqTRel\",
        \"GeqFGeFEqFRel\", \"LeTGeqFRel\", \"GeTLeqFRel\", \"LeLeqDiff\",
        \"CmpLTDef\", \"CmpEQDef\", \"CmpGTDef\", \"MaxYDef\", \"MaxXDef\",
        \"MinXDef\", \"MinYDef\", \"MaxSym\", \"MinSym\", \"TO1\", \"TO3\",
        \"TO4\", \"TO5\", \"IOO13\", \"IOO14\", \"IOO15\", \"IOO16\",
        \"IOO17\", \"IOO18\", \"IOO19\", \"IOO20\", \"IOO21\", \"IOO22\",
        \"IOO23\", \"IOO24\", \"IOO25\", \"IOO26\", \"IOO27\", \"IOO28\",
        \"IOO29\", \"IOO30\", \"IOO31\", \"IOO32\", \"IOO33\", \"IBO5\",
        \"IBO6\", \"IBO7\", \"IBO8\", \"IBO9\", \"IBO10\", \"IBO11\",
        \"IBO12\", \"IUO01\", \"IUO02\", \"IUO03\", \"IUO04\", \"IUO05\",
        \"IUO06\", \"IUO07\", \"NotDefHead\", \"HeadDef\", \"NotDefTail\",
        \"TailDef\", \"FoldrNil\", \"FoldrCons\", \"FoldlNil\",
        \"FoldlCons\", \"MapNil\", \"MapCons\", \"XPlusXPlusNil\",
        \"XPlusXPlusCons\", \"FilterNil\", \"FilterConsT\",
        \"FilterConsF\", \"ZipNil\", \"ZipConsNil\", \"ZipConsCons\",
        \"UnzipNil\", \"UnzipCons\", \"ILE01\", \"ILE02\", \"ILO01\",
        \"ILO02\", \"ILO03\", \"ILO04\", \"ILO05\", \"ILO06\", \"ILO07\",
        \"ILO08\", \"ILO09\", \"ILO10\", \"ILO11\", \"ILO12\", \"ILO13\",
        \"ILO14\", \"ILO15\", \"ILO16\", \"ILO17\", \"ILO18\", \"ILO19\",
        \"ILO20\", \"ILO21\", \"ILO22\", \"FoldlDecomp\", \"MapDecomp\",
        \"MapFunctor\", \"FilterProm\", \"InitNil\", \"InitConsNil\",
        \"InitConsCons\", \"LastNil\", \"LastConsNil\", \"LastConsCons\",
        \"NullNil\", \"NullCons\", \"ReverseNil\", \"ReverseCons\",
        \"Foldr1Nil\", \"Foldr1ConsNil\", \"Foldr1ConsCons\",
        \"Foldl1Nil\", \"Foldl1ConsNil\", \"Foldl1ConsCons\", \"ScanlNil\",
        \"ScanlCons\", \"Scanl1Nil\", \"Scanl1Cons\", \"ScanrNil\",
        \"ScanrCons\", \"Scanr1Nil\", \"Scanr1ConsNil\",
        \"Scanr1ConsCons\", \"ScanlProperty\", \"ScanrProperty\"]"

typedecl Unit

datatype Bool = X_False ("False''") | X_True ("True''")
datatype Ordering = EQ | GT | LT
datatype 'a List = X_Cons 'a "'a List" | X_Nil ("Nil''")

consts
X__XAmpXAmp__X :: "Bool => Bool => Bool" ("(_/ &&/ _)" [54,54] 52)
X__XEqXEq__X :: "'a => 'a => Bool" ("(_/ ==''/ _)" [54,54] 52)
X__XGtXEq__X :: "'a => 'a => Bool" ("(_/ >=''/ _)" [54,54] 52)
X__XGt__X :: "'a => 'a => Bool" ("(_/ >''/ _)" [54,54] 52)
X__XLtXEq__X :: "'a => 'a => Bool" ("(_/ <=''/ _)" [54,54] 52)
X__XLt__X :: "'a => 'a => Bool" ("(_/ <''/ _)" [54,54] 52)
X__XPlusXPlus__X :: "'a List => 'a List => 'a List" ("(_/ ++''/ _)" [54,54] 52)
X__XSlashXEq__X :: "'a => 'a => Bool" ("(_/ '/=/ _)" [54,54] 52)
X__XVBarXVBar__X :: "Bool => Bool => Bool" ("(_/ ||/ _)" [54,54] 52)
X__o__X :: "('b => 'c) * ('a => 'b) => 'a => 'c"
X_curry :: "('a * 'b => 'c) => 'a => 'b => 'c"
X_filter :: "('a => Bool) => 'a List => 'a List"
X_flip :: "('a => 'b => 'c) => 'b => 'a => 'c"
X_foldl :: "('a => 'b => 'a) => 'a => 'b List => 'a partial"
X_foldr :: "('a => 'b => 'b) => 'b => 'a List => 'b partial"
X_fst :: "'a => 'b => 'a" ("fst''/'(_,/ _')" [3,3] 999)
X_head :: "'a List => 'a partial" ("head/'(_')" [3] 999)
X_id :: "'a => 'a" ("id''/'(_')" [3] 999)
X_init :: "'a List => 'a List partial" ("init/'(_')" [3] 999)
X_last :: "'a List => 'a partial" ("last''/'(_')" [3] 999)
X_map :: "('a => 'b) => 'a List => 'b List"
X_max :: "'a => 'a => 'a"
X_min :: "'a => 'a => 'a"
X_null :: "'a List => Bool" ("null''/'(_')" [3] 999)
X_reverse :: "'a List => 'a List" ("reverse/'(_')" [3] 999)
X_snd :: "'a => 'b => 'b" ("snd''/'(_,/ _')" [3,3] 999)
X_tail :: "'a List => 'a List partial" ("tail/'(_')" [3] 999)
X_unzip :: "('a * 'b) List => 'a List * 'b List" ("unzip/'(_')" [3] 999)
X_zip :: "'a List => 'b List => ('a * 'b) List"
compare :: "'a => 'a => Ordering"
foldl1 :: "('a => 'a => 'a) => 'a List => 'a partial"
foldr1 :: "('a => 'a => 'a) => 'a List => 'a partial"
notH__X :: "Bool => Bool" ("(notH/ _)" [56] 56)
otherwiseH :: "Bool"
scanl :: "('a => 'b => 'a) => 'a => 'b List => 'a List"
scanl1 :: "('a => 'a => 'a) => 'a List => 'a List"
scanr :: "('a => 'b => 'b) => 'b => 'a List => 'b List"
scanr1 :: "('a => 'a => 'a) => 'a List => 'a List"
uncurry :: "('a => 'b => 'c) => 'a * 'b => 'c"

axioms
Comp1 [rule_format] :
"ALL (f :: 'b => 'c).
 ALL (g :: 'a => 'b). ALL (y :: 'a). X__o__X (f, g) y = f (g y)"

IdDef [rule_format] : "ALL (x :: 'a). id'(x) = x"

FlipDef [rule_format] :
"ALL (f :: 'a => 'b => 'c).
 ALL (x :: 'a). ALL (y :: 'b). X_flip f y x = f x y"

FstDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'b). fst'(x, y) = x"

SndDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'b). snd'(x, y) = y"

CurryDef [rule_format] :
"ALL (g :: 'a * 'b => 'c).
 ALL (x :: 'a). ALL (y :: 'b). X_curry g x y = g (x, y)"

UncurryDef [rule_format] :
"ALL (f :: 'a => 'b => 'c).
 ALL (x :: 'a). ALL (y :: 'b). uncurry f (x, y) = f x y"

NotFalse [rule_format] : "notH False' = True'"

NotTrue [rule_format] : "notH True' = False'"

AndFalse [rule_format] : "ALL (x :: Bool). False' && x = False'"

AndTrue [rule_format] : "ALL (x :: Bool). True' && x = x"

AndSym [rule_format] :
"ALL (x :: Bool). ALL (y :: Bool). x && y = y && x"

OrDef [rule_format] :
"ALL (x :: Bool).
 ALL (y :: Bool). x || y = notH (notH x && notH y)"

OtherwiseDef [rule_format] : "otherwiseH = True'"

NotFalse1 [rule_format] :
"ALL (x :: Bool). notH x = True' = (x = False')"

NotTrue1 [rule_format] :
"ALL (x :: Bool). notH x = False' = (x = True')"

notNot1 [rule_format] :
"ALL (x :: Bool). (~ x = True') = (notH x = True')"

notNot2 [rule_format] :
"ALL (x :: Bool). (~ x = False') = (notH x = False')"

EqualTDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x = y --> x ==' y = True'"

EqualSymDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x ==' y = y ==' x"

EqualReflex [rule_format] : "ALL (x :: 'a). x ==' x = True'"

EqualTransT [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a).
 x ==' y = True' & y ==' z = True' --> x ==' z = True'"

DiffDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x /= y = notH (x ==' y)"

DiffSymDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x /= y = y /= x"

DiffTDef [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). x /= y = True' = (notH (x ==' y) = True')"

DiffFDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x /= y = False' = (x ==' y = True')"

TE1 [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x ==' y = False' --> ~ x = y"

TE2 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). notH (x ==' y) = True' = (x ==' y = False')"

TE3 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). notH (x ==' y) = False' = (x ==' y = True')"

TE4 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). (~ x ==' y = True') = (x ==' y = False')"

IBE1 [rule_format] : "True' ==' True' = True'"

IBE2 [rule_format] : "False' ==' False' = True'"

IBE3 [rule_format] : "False' ==' True' = False'"

IBE4 [rule_format] : "True' ==' False' = False'"

IBE5 [rule_format] : "True' /= False' = True'"

IBE6 [rule_format] : "False' /= True' = True'"

IBE7 [rule_format] : "notH (True' ==' False') = True'"

IBE8 [rule_format] : "notH notH (True' ==' False') = False'"

IUE1 [rule_format] : "() ==' () = True'"

IUE2 [rule_format] : "() /= () = False'"

IOE01 [rule_format] : "LT ==' LT = True'"

IOE02 [rule_format] : "EQ ==' EQ = True'"

IOE03 [rule_format] : "GT ==' GT = True'"

IOE04 [rule_format] : "LT ==' EQ = False'"

IOE05 [rule_format] : "LT ==' GT = False'"

IOE06 [rule_format] : "EQ ==' GT = False'"

IOE07 [rule_format] : "LT /= EQ = True'"

IOE08 [rule_format] : "LT /= GT = True'"

IOE09 [rule_format] : "EQ /= GT = True'"

LeIrreflexivity [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x ==' y = True' --> x <' y = False'"

LeTAsymmetry [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <' y = True' --> y <' x = False'"

LeTTransitive [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). x <' y = True' & y <' z = True' --> x <' z = True'"

LeTTotal [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). (x <' y = True' | y <' x = True') | x ==' y = True'"

GeDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >' y = y <' x"

GeIrreflexivity [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x ==' y = True' --> x >' y = False'"

GeTAsymmetry [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >' y = True' --> y >' x = False'"

GeTTransitive [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). (x >' y) && (y >' z) = True' --> x >' z = True'"

GeTTotal [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). ((x >' y) || (y >' x)) || (x ==' y) = True'"

LeqDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <=' y = (x <' y) || (x ==' y)"

LeqReflexivity [rule_format] : "ALL (x :: 'a). x <=' x = True'"

LeqTTransitive [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). (x <=' y) && (y <=' z) = True' --> x <=' z = True'"

LeqTTotal [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). (x <=' y) && (y <=' x) = x ==' y"

GeqDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >=' y = (x >' y) || (x ==' y)"

GeqReflexivity [rule_format] : "ALL (x :: 'a). x >=' x = True'"

GeqTTransitive [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). (x >=' y) && (y >=' z) = True' --> x >=' z = True'"

GeqTTotal [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). (x >=' y) && (y >=' x) = x ==' y"

EqTSOrdRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x ==' y = True' = (x <' y = False' & x >' y = False')"

EqFSOrdRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x ==' y = False' = (x <' y = True' | x >' y = True')"

EqTOrdRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x ==' y = True' = (x <=' y = True' & x >=' y = True')"

EqFOrdRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x ==' y = False' = (x <=' y = True' | x >=' y = True')"

EqTOrdTSubstE [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). x ==' y = True' & y <' z = True' --> x <' z = True'"

EqTOrdFSubstE [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a).
 x ==' y = True' & y <' z = False' --> x <' z = False'"

EqTOrdTSubstD [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a). x ==' y = True' & z <' y = True' --> z <' x = True'"

EqTOrdFSubstD [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a).
 x ==' y = True' & z <' y = False' --> z <' x = False'"

LeTGeFEqFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x <' y = True' = (x >' y = False' & x ==' y = False')"

LeFGeTEqTRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x <' y = False' = (x >' y = True' | x ==' y = True')"

LeTGeTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <' y = True' = (y >' x = True')"

LeFGeFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <' y = False' = (y >' x = False')"

LeqTGetTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <=' y = True' = (y >=' x = True')"

LeqFGetFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). x <=' y = False' = (y >=' x = False')"

GeTLeTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >' y = True' = (y <' x = True')"

GeFLeFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >' y = False' = (y <' x = False')"

GeqTLeqTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >=' y = True' = (y <=' x = True')"

GeqFLeqFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). x >=' y = False' = (y <=' x = False')"

LeqTGeFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <=' y = True' = (x >' y = False')"

LeqFGeTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <=' y = False' = (x >' y = True')"

GeTLeFEqFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x >' y = True' = (x <' y = False' & x ==' y = False')"

GeFLeTEqTRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x >' y = False' = (x <' y = True' | x ==' y = True')"

GeqTLeFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >=' y = True' = (x <' y = False')"

GeqFLeTRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >=' y = False' = (x <' y = True')"

LeqTLeTEqTRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x <=' y = True' = (x <' y = True' | x ==' y = True')"

LeqFLeFEqFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x <=' y = False' = (x <' y = False' & x ==' y = False')"

GeqTGeTEqTRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x >=' y = True' = (x >' y = True' | x ==' y = True')"

GeqFGeFEqFRel [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 x >=' y = False' = (x >' y = False' & x ==' y = False')"

LeTGeqFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <' y = True' = (x >=' y = False')"

GeTLeqFRel [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x >' y = True' = (x <=' y = False')"

LeLeqDiff [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). x <' y = (x <=' y) && (x /= y)"

CmpLTDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). compare x y ==' LT = x <' y"

CmpEQDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). compare x y ==' EQ = x ==' y"

CmpGTDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). compare x y ==' GT = x >' y"

MaxYDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_max x y ==' y = x <=' y"

MaxXDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_max x y ==' x = y <=' x"

MinXDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_min x y ==' x = x <=' y"

MinYDef [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_min x y ==' y = y <=' x"

MaxSym [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_max x y ==' y = X_max y x ==' y"

MinSym [rule_format] :
"ALL (x :: 'a). ALL (y :: 'a). X_min x y ==' y = X_min y x ==' y"

TO1 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a).
 (x ==' y = True' | x <' y = True') = (x <=' y = True')"

TO3 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). notH notH (x <' y) = True' | notH (x <' y) = True'"

TO4 [rule_format] :
"ALL (x :: 'a).
 ALL (y :: 'a). x <' y = True' --> notH (x ==' y) = True'"

TO5 [rule_format] :
"ALL (w :: 'a).
 ALL (x :: 'a).
 ALL (y :: 'a).
 ALL (z :: 'a).
 (x <' y = True' & y <' z = True') & z <' w = True' -->
 x <' w = True'"

IOO13 [rule_format] : "LT <' EQ = True'"

IOO14 [rule_format] : "EQ <' GT = True'"

IOO15 [rule_format] : "LT <' GT = True'"

IOO16 [rule_format] : "LT <=' EQ = True'"

IOO17 [rule_format] : "EQ <=' GT = True'"

IOO18 [rule_format] : "LT <=' GT = True'"

IOO19 [rule_format] : "EQ >=' LT = True'"

IOO20 [rule_format] : "GT >=' EQ = True'"

IOO21 [rule_format] : "GT >=' LT = True'"

IOO22 [rule_format] : "EQ >' LT = True'"

IOO23 [rule_format] : "GT >' EQ = True'"

IOO24 [rule_format] : "GT >' LT = True'"

IOO25 [rule_format] : "X_max LT EQ ==' EQ = True'"

IOO26 [rule_format] : "X_max EQ GT ==' GT = True'"

IOO27 [rule_format] : "X_max LT GT ==' GT = True'"

IOO28 [rule_format] : "X_min LT EQ ==' LT = True'"

IOO29 [rule_format] : "X_min EQ GT ==' EQ = True'"

IOO30 [rule_format] : "X_min LT GT ==' LT = True'"

IOO31 [rule_format] : "compare LT LT ==' EQ = True'"

IOO32 [rule_format] : "compare EQ EQ ==' EQ = True'"

IOO33 [rule_format] : "compare GT GT ==' EQ = True'"

IBO5 [rule_format] : "False' <' True' = True'"

IBO6 [rule_format] : "False' >=' True' = False'"

IBO7 [rule_format] : "True' >=' False' = True'"

IBO8 [rule_format] : "True' <' False' = False'"

IBO9 [rule_format] : "X_max False' True' ==' True' = True'"

IBO10 [rule_format] : "X_min False' True' ==' False' = True'"

IBO11 [rule_format] : "compare True' True' ==' EQ = True'"

IBO12 [rule_format] : "compare False' False' ==' EQ = True'"

IUO01 [rule_format] : "() <=' () = True'"

IUO02 [rule_format] : "() <' () = False'"

IUO03 [rule_format] : "() >=' () = True'"

IUO04 [rule_format] : "() >' () = False'"

IUO05 [rule_format] : "X_max () () ==' () = True'"

IUO06 [rule_format] : "X_min () () ==' () = True'"

IUO07 [rule_format] : "compare () () ==' EQ = True'"

NotDefHead [rule_format] : "~ defOp (head(Nil'))"

HeadDef [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List). head(X_Cons x xs) = makePartial x"

NotDefTail [rule_format] : "~ defOp (tail(Nil'))"

TailDef [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List). tail(X_Cons x xs) = makePartial xs"

FoldrNil [rule_format] :
"ALL (f :: 'a => 'b => 'b).
 ALL (s :: 'b). X_foldr f s Nil' = makePartial s"

FoldrCons [rule_format] :
"ALL (f :: 'a => 'b => 'b).
 ALL (s :: 'b).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 X_foldr f s (X_Cons x xs) =
 restrictOp (makePartial (f x (makeTotal (X_foldr f s xs))))
 (defOp (X_foldr f s xs))"

FoldlNil [rule_format] :
"ALL (g :: 'a => 'b => 'a).
 ALL (t :: 'a). X_foldl g t Nil' = makePartial t"

FoldlCons [rule_format] :
"ALL (g :: 'a => 'b => 'a).
 ALL (t :: 'a).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_foldl g t (X_Cons z zs) = X_foldl g (g t z) zs"

MapNil [rule_format] : "ALL (h :: 'a => 'b). X_map h Nil' = Nil'"

MapCons [rule_format] :
"ALL (h :: 'a => 'b).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 X_map h (X_Cons x xs) = X_Cons (h x) (X_map h xs)"

XPlusXPlusNil [rule_format] : "ALL (l :: 'a List). Nil' ++' l = l"

XPlusXPlusCons [rule_format] :
"ALL (l :: 'a List).
 ALL (x :: 'a).
 ALL (xs :: 'a List). X_Cons x xs ++' l = X_Cons x (xs ++' l)"

FilterNil [rule_format] :
"ALL (p :: 'a => Bool). X_filter p Nil' = Nil'"

FilterConsT [rule_format] :
"ALL (p :: 'a => Bool).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 p x = True' -->
 X_filter p (X_Cons x xs) = X_Cons x (X_filter p xs)"

FilterConsF [rule_format] :
"ALL (p :: 'a => Bool).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 p x = False' --> X_filter p (X_Cons x xs) = X_filter p xs"

ZipNil [rule_format] : "ALL (l :: 'a List). X_zip Nil' l = Nil'"

ZipConsNil [rule_format] :
"ALL (l :: 'a List).
 ALL (x :: 'a).
 ALL (xs :: 'a List). l = Nil' --> X_zip (X_Cons x xs) l = Nil'"

ZipConsCons [rule_format] :
"ALL (l :: 'a List).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 ALL (y :: 'a).
 ALL (ys :: 'a List).
 l = X_Cons y ys -->
 X_zip (X_Cons x xs) l = X_Cons (x, y) (X_zip xs ys)"

UnzipNil [rule_format] : "unzip(Nil') = (Nil', Nil')"

UnzipCons [rule_format] :
"ALL (ps :: ('a * 'b) List).
 ALL (x :: 'a).
 ALL (z :: 'b).
 unzip(X_Cons (x, z) ps) =
 (let (ys, zs) = unzip(ps) in (X_Cons x ys, X_Cons z zs))"

ILE01 [rule_format] : "Nil' ==' Nil' = True'"

ILE02 [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List).
 ALL (y :: 'a).
 ALL (ys :: 'a List).
 X_Cons x xs ==' X_Cons y ys = (x ==' y) && (xs ==' ys)"

ILO01 [rule_format] : "Nil' <' Nil' = False'"

ILO02 [rule_format] : "Nil' <=' Nil' = True'"

ILO03 [rule_format] : "Nil' >' Nil' = False'"

ILO04 [rule_format] : "Nil' >=' Nil' = True'"

ILO05 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 z <' w = True' --> X_Cons z zs <' X_Cons w ws = True'"

ILO06 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 z ==' w = True' --> X_Cons z zs <' X_Cons w ws = zs <' ws"

ILO07 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 z <' w = False' & z ==' w = False' -->
 X_Cons z zs <' X_Cons w ws = False'"

ILO08 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons z zs <=' X_Cons w ws =
 (X_Cons z zs <' X_Cons w ws) || (X_Cons z zs ==' X_Cons w ws)"

ILO09 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons z zs >' X_Cons w ws = X_Cons w ws <' X_Cons z zs"

ILO10 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons z zs >=' X_Cons w ws =
 (X_Cons z zs >' X_Cons w ws) || (X_Cons z zs ==' X_Cons w ws)"

ILO11 [rule_format] : "compare Nil' Nil' ==' EQ = Nil' ==' Nil'"

ILO12 [rule_format] : "compare Nil' Nil' ==' LT = Nil' <' Nil'"

ILO13 [rule_format] : "compare Nil' Nil' ==' GT = Nil' >' Nil'"

ILO14 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 compare (X_Cons z zs) (X_Cons w ws) ==' EQ =
 X_Cons z zs ==' X_Cons w ws"

ILO15 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 compare (X_Cons z zs) (X_Cons w ws) ==' LT =
 X_Cons z zs <' X_Cons w ws"

ILO16 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 compare (X_Cons z zs) (X_Cons w ws) ==' GT =
 X_Cons z zs >' X_Cons w ws"

ILO17 [rule_format] : "X_max Nil' Nil' ==' Nil' = Nil' <=' Nil'"

ILO18 [rule_format] : "X_min Nil' Nil' ==' Nil' = Nil' <=' Nil'"

ILO19 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons z zs <=' X_Cons w ws =
 X_max (X_Cons z zs) (X_Cons w ws) ==' X_Cons w ws"

ILO20 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons w ws <=' X_Cons z zs =
 X_max (X_Cons z zs) (X_Cons w ws) ==' X_Cons z zs"

ILO21 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons z zs <=' X_Cons w ws =
 X_min (X_Cons z zs) (X_Cons w ws) ==' X_Cons z zs"

ILO22 [rule_format] :
"ALL (w :: 'b).
 ALL (ws :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 X_Cons w ws <=' X_Cons z zs =
 X_min (X_Cons z zs) (X_Cons w ws) ==' X_Cons w ws"

FoldlDecomp [rule_format] :
"ALL (e :: 'a).
 ALL (i :: 'a => 'b => 'a).
 ALL (ts :: 'b List).
 ALL (ys :: 'b List).
 X_foldl i e (ys ++' ts) =
 restrictOp (X_foldl i (makeTotal (X_foldl i e ys)) ts)
 (defOp (X_foldl i e ys))"

MapDecomp [rule_format] :
"ALL (f :: 'a => 'b).
 ALL (xs :: 'a List).
 ALL (zs :: 'a List).
 X_map f (xs ++' zs) = X_map f xs ++' X_map f zs"

MapFunctor [rule_format] :
"ALL (f :: 'a => 'b).
 ALL (g :: 'b => 'c).
 ALL (xs :: 'a List).
 X_map (X__o__X (g, f)) xs = X_map g (X_map f xs)"

FilterProm [rule_format] :
"ALL (f :: 'a => 'b).
 ALL (p :: 'b => Bool).
 ALL (xs :: 'a List).
 X_filter p (X_map f xs) = X_map f (X_filter (X__o__X (p, f)) xs)"

InitNil [rule_format] : "~ defOp (init(Nil'))"

InitConsNil [rule_format] :
"ALL (x :: 'a). init(X_Cons x Nil') = makePartial Nil'"

InitConsCons [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List).
 init(X_Cons x xs) =
 restrictOp (makePartial (X_Cons x (makeTotal (init(xs)))))
 (defOp (init(xs)))"

LastNil [rule_format] : "~ defOp (last'(Nil'))"

LastConsNil [rule_format] :
"ALL (x :: 'a). last'(X_Cons x Nil') = makePartial x"

LastConsCons [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List). last'(X_Cons x xs) = last'(xs)"

NullNil [rule_format] : "null'(Nil') = True'"

NullCons [rule_format] :
"ALL (x :: 'a). ALL (xs :: 'a List). null'(X_Cons x xs) = False'"

ReverseNil [rule_format] : "reverse(Nil') = Nil'"

ReverseCons [rule_format] :
"ALL (x :: 'a).
 ALL (xs :: 'a List).
 reverse(X_Cons x xs) = reverse(xs) ++' X_Cons x Nil'"

Foldr1Nil [rule_format] :
"ALL (f :: 'a => 'a => 'a). ~ defOp (foldr1 f Nil')"

Foldr1ConsNil [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a). foldr1 f (X_Cons x Nil') = makePartial x"

Foldr1ConsCons [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 foldr1 f (X_Cons x xs) =
 restrictOp (makePartial (f x (makeTotal (foldr1 f xs))))
 (defOp (foldr1 f xs))"

Foldl1Nil [rule_format] :
"ALL (f :: 'a => 'a => 'a). ~ defOp (foldl1 f Nil')"

Foldl1ConsNil [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a). foldl1 f (X_Cons x Nil') = makePartial x"

Foldl1ConsCons [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 foldl1 f (X_Cons x xs) =
 restrictOp (makePartial (f x (makeTotal (foldr1 f xs))))
 (defOp (foldr1 f xs))"

ScanlNil [rule_format] :
"ALL (g :: 'a => 'b => 'a).
 ALL (q :: 'a).
 ALL (ys :: 'b List). ys = Nil' --> scanl g q ys = X_Cons q Nil'"

ScanlCons [rule_format] :
"ALL (g :: 'a => 'b => 'a).
 ALL (q :: 'a).
 ALL (ys :: 'b List).
 ALL (z :: 'b).
 ALL (zs :: 'b List).
 ys = X_Cons z zs --> scanl g q ys = X_Cons q (scanl g (g q z) zs)"

Scanl1Nil [rule_format] :
"ALL (f :: 'a => 'a => 'a). scanl1 f Nil' = Nil'"

Scanl1Cons [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a).
 ALL (xs :: 'a List). scanl1 f (X_Cons x xs) = scanl f x xs"

ScanrNil [rule_format] :
"ALL (h :: 'a => 'b => 'b).
 ALL (z :: 'b). scanr h z Nil' = X_Cons z Nil'"

ScanrCons [rule_format] :
"ALL (h :: 'a => 'b => 'b).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 ALL (y :: 'b).
 ALL (ys :: 'b List).
 ALL (z :: 'b).
 X_Cons y ys = scanr h z xs -->
 scanr h z (X_Cons x xs) = X_Cons (h x y) (X_Cons y ys)"

Scanr1Nil [rule_format] :
"ALL (f :: 'a => 'a => 'a). scanr1 f Nil' = Nil'"

Scanr1ConsNil [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (x :: 'a). scanr1 f (X_Cons x Nil') = X_Cons x Nil'"

Scanr1ConsCons [rule_format] :
"ALL (f :: 'a => 'a => 'a).
 ALL (q :: 'a).
 ALL (qs :: 'a List).
 ALL (x :: 'a).
 ALL (xs :: 'a List).
 X_Cons q qs = scanr1 f xs -->
 scanr1 f (X_Cons x xs) = X_Cons (f x q) (X_Cons q qs)"

declare Comp1 [simp]
declare IdDef [simp]
declare FlipDef [simp]
declare FstDef [simp]
declare SndDef [simp]
declare CurryDef [simp]
declare UncurryDef [simp]
declare NotFalse [simp]
declare NotTrue [simp]
declare AndFalse [simp]
declare AndTrue [simp]
declare EqualReflex [simp]
declare IBE1 [simp]
declare IBE2 [simp]
declare IBE3 [simp]
declare IBE4 [simp]
declare IBE5 [simp]
declare IBE6 [simp]
declare IBE7 [simp]
declare IBE8 [simp]
declare IOE01 [simp]
declare IOE02 [simp]
declare IOE03 [simp]
declare IOE04 [simp]
declare IOE05 [simp]
declare IOE06 [simp]
declare IOE07 [simp]
declare IOE08 [simp]
declare IOE09 [simp]
declare LeIrreflexivity [simp]
declare LeTAsymmetry [simp]
declare GeIrreflexivity [simp]
declare GeTAsymmetry [simp]
declare GeTTransitive [simp]
declare GeTTotal [simp]
declare LeqReflexivity [simp]
declare LeqTTransitive [simp]
declare LeqTTotal [simp]
declare GeqReflexivity [simp]
declare GeqTTransitive [simp]
declare GeqTTotal [simp]
declare CmpLTDef [simp]
declare CmpEQDef [simp]
declare CmpGTDef [simp]
declare MaxYDef [simp]
declare MaxXDef [simp]
declare MinXDef [simp]
declare MinYDef [simp]
declare TO4 [simp]
declare IOO13 [simp]
declare IOO14 [simp]
declare IOO15 [simp]
declare IOO16 [simp]
declare IOO17 [simp]
declare IOO18 [simp]
declare IOO19 [simp]
declare IOO20 [simp]
declare IOO21 [simp]
declare IOO22 [simp]
declare IOO23 [simp]
declare IOO24 [simp]
declare IOO25 [simp]
declare IOO26 [simp]
declare IOO27 [simp]
declare IOO28 [simp]
declare IOO29 [simp]
declare IOO30 [simp]
declare IOO31 [simp]
declare IOO32 [simp]
declare IOO33 [simp]
declare IBO5 [simp]
declare IBO6 [simp]
declare IBO7 [simp]
declare IBO8 [simp]
declare IBO9 [simp]
declare IBO10 [simp]
declare IBO11 [simp]
declare IBO12 [simp]
declare IUO05 [simp]
declare IUO06 [simp]
declare IUO07 [simp]
declare NotDefHead [simp]
declare HeadDef [simp]
declare NotDefTail [simp]
declare TailDef [simp]
declare FoldrNil [simp]
declare FoldlNil [simp]
declare MapNil [simp]
declare XPlusXPlusNil [simp]
declare FilterNil [simp]
declare FilterConsF [simp]
declare ZipNil [simp]
declare ILE01 [simp]
declare ILO01 [simp]
declare ILO02 [simp]
declare ILO03 [simp]
declare ILO04 [simp]
declare ILO05 [simp]
declare ILO06 [simp]
declare ILO11 [simp]
declare ILO12 [simp]
declare ILO13 [simp]
declare ILO14 [simp]
declare ILO15 [simp]
declare ILO16 [simp]
declare ILO17 [simp]
declare ILO18 [simp]
declare InitNil [simp]
declare InitConsNil [simp]
declare LastNil [simp]
declare LastConsNil [simp]
declare LastConsCons [simp]
declare NullNil [simp]
declare NullCons [simp]
declare ReverseNil [simp]
declare Foldr1Nil [simp]
declare Foldr1ConsNil [simp]
declare Foldl1Nil [simp]
declare Foldl1ConsNil [simp]
declare Scanl1Nil [simp]
declare Scanl1Cons [simp]
declare ScanrNil [simp]
declare Scanr1Nil [simp]
declare Scanr1ConsNil [simp]

theorem ScanlProperty :
"ALL (g :: 'a => 'b => 'a).
 ALL (x :: 'a).
 ALL (ys :: 'b List). last'(scanl g x ys) = X_foldl g x ys"
by (auto)

setup "Header.record \"ScanlProperty\""

theorem ScanrProperty :
"ALL (h :: 'a => 'b => 'b).
 ALL (xs :: 'a List).
 ALL (y :: 'b). head(scanr h y xs) = X_foldr h y xs"
by (auto)

setup "Header.record \"ScanrProperty\""

end


section \<open>\isaheader{Additions to Distinct Lists}\<close>
theory Dlist_add
  imports
  "HOL-Library.Dlist"
  "../../Automatic_Refinement/Lib/Misc"
  "../Iterator/SetIteratorOperations"
begin

primrec dlist_remove1' :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
where
  "dlist_remove1' x z [] = z"
| "dlist_remove1' x z (y # ys)
  = (if x = y then z @ ys else dlist_remove1' x (y # z) ys)"

definition dlist_remove' :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist"
where "dlist_remove' a xs = Dlist (dlist_remove1' a [] (list_of_dlist xs))"

lemma distinct_remove1': "distinct (xs @ ys) \<Longrightarrow>
  distinct (dlist_remove1' x xs ys)"
  by(induct ys arbitrary: xs) simp_all

lemma set_dlist_remove1': "distinct ys \<Longrightarrow>
  set (dlist_remove1' x xs ys) = set xs \<union> (set ys - {x})"
  by(induct ys arbitrary: xs) auto

lemma list_of_dlist_remove' [simp, code abstract]:
  "list_of_dlist (dlist_remove' a xs) = dlist_remove1' a [] (list_of_dlist xs)"
by(simp add: dlist_remove'_def distinct_remove1')

lemma dlist_remove'_correct:
  "y \<in> set (list_of_dlist (dlist_remove' x xs))
  \<longleftrightarrow> (if x = y then False else y \<in> set (list_of_dlist xs))"
  by(simp add: dlist_remove'_def
    Dlist.member_def List.member_def set_dlist_remove1')

definition dlist_iteratei :: "'a dlist \<Rightarrow> ('a, 'b) set_iterator"
  where "dlist_iteratei xs = foldli (list_of_dlist xs)"

lemma dlist_iteratei_correct:
  "set_iterator (dlist_iteratei xs) (set (list_of_dlist xs))"
using distinct_list_of_dlist[of xs]
      set_iterator_foldli_correct[of "list_of_dlist xs"]
unfolding Dlist.member_def List.member_def dlist_iteratei_def
by simp

lemma dlist_member_empty: "(set (list_of_dlist Dlist.empty)) = {}"
  by(simp add: Dlist.empty_def)

lemma dlist_member_insert [simp]: "set (list_of_dlist (Dlist.insert x xs))
  = insert x (set (list_of_dlist xs))"
  by(simp add: Dlist.insert_def Dlist.member_def )

lemma dlist_finite_member [simp, intro!]: "finite (set (list_of_dlist xs))"
by(simp add: member_def )

end


           (*-------------------------------------------*
            |        CSP-Prover on Isabelle2004         |
            |               November 2004               |
            |                   June 2005  (modified)   |
            |                   July 2005  (modified)   |
            |                                           |
            |        CSP-Prover on Isabelle2005         |
            |                October 2005  (modified)   |
            |                                           |
            |        CSP-Prover on Isabelle2009         |
            |                   June 2009  (modified)   |
            |                                           |
            |        CSP-Prover on Isabelle2013         |
            |                   June 2013  (modified)   |
            |                                           |
            |        CSP-Prover on Isabelle2016         |
            |                  April 2016  (modified)   |
            |                                           |
            |        Yoshinao Isobe (AIST JAPAN)        |
            *-------------------------------------------*)

theory Infra_set
imports Infra_list
begin

(*****************************************************
               Small lemmas for Set
 *****************************************************)

lemma notin_subset: "[| S <= T ; a ~: T |] ==> a ~: S"
by (auto)

lemma notin_set_butlast: "a ~: set s ==> a ~: set (butlast s)"
apply (rule notin_subset[of _ "set s"])
by (simp_all add: butlast_subseteq)

lemma in_set_butlast: "a : set (butlast s) ==> a : set s"
apply (erule contrapos_pp)
by (simp add: notin_set_butlast)

lemma in_image_set: "x : f ` X = (EX y. x = f y & y:X)"
by (auto)

lemma inj_image_diff_dist: "inj f ==> f ` (A - B) = f ` A - f ` B"
by (auto simp add: inj_on_def)

lemma inj_image_Int_dist: "inj f ==> f ` (A Int B) = f ` A Int f ` B"
by (auto simp add: inj_on_def)

lemma subsetE: "[| A <= B ; [| ALL x:A. x:B |] ==> R |] ==> R"
by (auto)

lemma Un_sym: "X Un Y = Y Un X"
by (auto)

lemma Int_subset_eq:"A <= B ==> A Int B = A"
by (auto)

lemma Int_insert_eq: "A Int insert x A = A"
by (auto)

lemma in_Union_EX: "x : Union Xs = (EX X. x:X & X:Xs)"
by (auto)

lemma subset_or_not_subset:
 "ALL A B. ((A::'a set) <= B) | (B <= A) | (EX a b. a : A & a ~: B & b ~: A & b : B)"
by (auto)

lemma not_subset_iff:
  "(~ (A::'a set) < B) = ((B <= A) | (EX a b. a : A & a ~: B & b ~: A & b : B))"
by (auto)

lemma Union_snd_Un:
  "Union (snd ` f ` (I1 Un I2))=
   Union (snd ` f ` I1) Un Union (snd ` f ` I2)"
by (auto)

lemma Union_snd_set_Un:
  "Union (snd ` (set s Un set t)) = Union (snd ` set s) Un Union (snd ` set t)"
by (auto)

lemma neq_Set_EX1: "EX a:A. a~:B ==> A ~= B"
by (auto)

lemma neq_Set_EX2: "EX a:A. a~:B ==> B ~= A"
by (auto)

(*****************************
          finite set
 *****************************)

lemma Union_index_fun:
   "ALL i:I1. PXf2 (f i) = PXf1 i
    ==> Union (snd ` PXf2 ` f ` I1) = Union (snd ` PXf1 ` I1)"
apply (rule)
apply (rule, simp)
apply (rule, simp)
done

lemma finite_pair_set1:
   "[| finite F1 ;ALL j:F1. finite (F2 j) |]
       ==> finite {(i, j)|i j. j : F1 & i : F2 j}"
(*
apply (induct set: Finites)     Isabelle 2005
*)
apply (induct set: finite)     (* Isabelle 2007 *)
apply (simp)
apply (subgoal_tac
   "{(i, j). (j = x | j : F) & i : F2 j}
  = (%i. (i, x)) ` (F2 x) Un {(i, j). j : F & i : F2 j}")
apply (simp)
by (auto)

lemmas finite_pair_set2 = finite_pair_set1[simplified]

lemmas finite_pair_set = finite_pair_set1 finite_pair_set2

(* finite set --> EX max *)

lemma nonempty_finite_set_exists_max_fun_subset:
  "[| finite I ; I ~= {} |]
    ==> EX J. J <= I & J ~= {} & (ALL j:J. ALL i:I. ~((f::'a => 'b::order) j < f i))"
(*
apply (induct set: Finites)     Isabelle 2005
*)
apply (induct set: finite)     (* Isabelle 2007 *)
apply (simp_all)

(* {} *)
 apply (case_tac "F = {}")
 apply (simp_all)

(* {x} *)
 apply (rule_tac x="{x}" in exI)
 apply (simp)

(* step case *)
 apply (elim conjE exE)
 apply (case_tac "EX j:J. (f j < f x)")
 apply (rule_tac x="(J - {j. f j < f x}) Un {x}" in exI)
 apply (simp)
 apply (rule conjI)
 apply (fast)
 apply (intro ballI)
 apply (elim bexE)
 apply (drule_tac x="j" in bspec, simp)
 apply (drule_tac x="i" in bspec, simp)
 apply (case_tac "~ f x < f i", simp)
 apply (simp)
 apply (rotate_tac -2)
 apply (erule contrapos_np)
 apply (force)

 apply (rule_tac x="J" in exI)
 apply (fast)
done

lemma nonempty_finite_set_exists_max_fun:
  "[| finite I ; I ~= {} |]
    ==> EX j:I. ALL i:I. ~((f::'a => 'b::order) j < f i)"
apply (insert nonempty_finite_set_exists_max_fun_subset[of I f])
apply (simp)
apply (elim conjE exE)
apply (subgoal_tac "EX j. j:J")
apply (elim exE)
apply (rule_tac x="j" in bexI)
apply (auto)
done

(* finite set --> EX min *)

lemma nonempty_finite_set_exists_min_fun_subset:
  "[| finite I ; I ~= {} |]
    ==> EX J. J <= I & J ~= {} & (ALL j:J. ALL i:I. ~((f::'a => 'b::order) i < f j))"
(*
apply (induct set: Finites)     Isabelle 2005
*)
apply (induct set: finite)     (* Isabelle 2007 *)
apply (simp_all)

(* {} *)
 apply (case_tac "F = {}")
 apply (simp_all)

(* {x} *)
 apply (rule_tac x="{x}" in exI)
 apply (simp)

(* step case *)
 apply (elim conjE exE)
 apply (case_tac "EX j:J. (f x < f j)")
 apply (rule_tac x="(J - {j. f x < f j}) Un {x}" in exI)
 apply (simp)
 apply (rule conjI)
 apply (fast)
 apply (intro ballI)
 apply (elim bexE)
 apply (drule_tac x="j" in bspec, simp)
 apply (drule_tac x="i" in bspec, simp)
 apply (case_tac "~ f i < f x", simp)
 apply (simp)
 apply (rotate_tac -2)
 apply (erule contrapos_np)
 apply (force)

 apply (rule_tac x="J" in exI)
 apply (fast)
done

lemma nonempty_finite_set_exists_min_fun:
  "[| finite I ; I ~= {} |]
    ==> EX j:I. ALL i:I. ~((f::'a => 'b::order) i < f j)"
apply (insert nonempty_finite_set_exists_min_fun_subset[of I f])
apply (simp)
apply (elim conjE exE)
apply (subgoal_tac "EX j. j:J")
apply (elim exE)
apply (rule_tac x="j" in bexI)
apply (auto)
done

(*** cardinality of the set of a list ***)

lemma card_set_eq_length:
  "(card (set s) = length s) =
   (ALL i. i < length s --> (ALL j. j < length s & s!i = s!j --> i = j))"
apply (induct_tac s)
apply (auto)

apply (insert nat_zero_or_Suc)
apply (rotate_tac -1)
apply (frule_tac x="i" in spec)
apply (drule_tac x="j" in spec)
apply (elim disjE exE)
apply (simp_all)
apply (simp add: card_insert_if)
apply (simp add: card_insert_if)
apply (force)

apply (simp add: card_insert)
apply (subgoal_tac "set list - {a} = set list")
apply (simp)
apply (case_tac "a ~: set list")
apply (simp)
apply (simp)
apply (simp add: in_set_conv_nth)
apply (elim conjE exE)
apply (rotate_tac 2)
apply (drule_tac x="0" in spec)
apply (simp)
apply (rotate_tac -1)
apply (drule_tac x="Suc i" in spec)
apply (simp)

apply (case_tac "a ~: set list")
apply (simp)
apply (simp add: card_insert_if)
apply (subgoal_tac "card (set list) <= (length list)")
apply (simp)
apply (simp (no_asm_use) add: card_length)

apply (drule_tac x="Suc i" in spec)
apply (simp)
apply (rotate_tac -1)
apply (drule_tac x="Suc j" in spec)
apply (simp)
done

(*****************************************************
   Small lemmas for isListOf (Finite Set <--> List)
 *****************************************************)

 (* Isabelle 2013
consts
  isListOf :: "'a list => 'a set => bool"       ("_ isListOf _" [55,55] 55)

defs
  isListOf_def :
    "s isListOf X  ==
     X = set s & (card (set s) = length s)"
*)

definition
  isListOf :: "'a list => 'a set => bool"       ("_ isListOf _" [55,55] 55)

  where
  isListOf_def :
    "s isListOf X  ==
     X = set s & (card (set s) = length s)"

lemma isListOf_EX: "(finite X) ==> EX s. s isListOf X"
apply (simp add: isListOf_def)
apply (rule finite_induct[of _ "(%X. EX s. X = set s & card (set s) = length s)"])
                        (* for Isabelle 2008 *)
apply (simp_all)

apply (elim conjE exE)
apply (simp)
apply (rule_tac x="x # s" in exI)
apply (simp)
done

(* finite_list: "finite A ==> EX l. set l = A" *)
(* finite_set : "finite (set xs)" *)

lemma isListOf_set_eq:
  "x isListOf X ==> set x = X"
by (simp add: isListOf_def)

lemma set_SOME_isListOf: "(finite S) ==> set (SOME t. t isListOf S) = S"
apply (insert isListOf_EX[of S])
apply (simp)
apply (erule exE)
apply (rule someI2)
apply (simp)
apply (simp add: isListOf_set_eq)
done

lemma isListOf_nonemptyset:
  "[| X ~= {} ; x isListOf X |] ==> x ~= []"
by (simp add: isListOf_def)

lemma isListOf_index_to_nth:
  "Is isListOf I ==> ALL i:I. EX n. n<length Is & i = Is ! n"
apply (rule ballI)
apply (simp add: isListOf_def)
apply (elim conjE)
apply (simp add: set_nth)
done

lemma isListOf_nth_in_index:
  "[| Is isListOf I ; n<length Is |] ==> Is ! n : I"
by (simp add: isListOf_def)

lemma isListOf_index_to_nthE:
  "[| Is isListOf I ;
   [| Is isListOf I ; ALL i:I. EX n. n<length Is & i = Is ! n |] ==> R |]
   ==> R"
by (simp add: isListOf_index_to_nth)

lemma isListOf_nth_in_indexE:
  "[| Is isListOf I ; [| Is isListOf I ; ALL n. n<length Is --> Is ! n : I |] ==> R |]
   ==> R"
by (simp add: isListOf_def)

lemma isListOf_THE_nth:
  "[| Is isListOf I ; n < length Is|]
   ==> (THE m. Is ! m = Is ! n & m < length Is) = n"
apply (rule the_equality)
apply (simp)
apply (simp add: isListOf_def)
apply (simp add: card_set_eq_length)
done

(*** nil ***)

lemma isListOf_emptyset_to_nil[simp]: "(Is isListOf {}) = (Is = [])"
apply (auto simp add: isListOf_def)
done

lemma isListOf_nil_to_emptyset[simp]: "([] isListOf X) = (X = {})"
by (simp add: isListOf_def)

(*** one ***)

lemma isListOf_oneset_to_onelist_ALL: "ALL t a. t isListOf {a} --> t = [a]"
apply (rule allI)
apply (induct_tac t)
apply (simp add: isListOf_def)

apply (intro allI impI)
apply (insert list_nil_or_unnil)
apply (drule_tac x="list" in spec)
apply (elim exE disjE)
apply (simp add: isListOf_def)

apply (simp add: isListOf_def)
apply (elim conjE)
apply (simp)
apply (simp add: card_insert)
apply (subgoal_tac "set s - {aa} = {}")
apply (simp_all)
done

lemma isListOf_oneset_to_onelist[simp]: "(t isListOf {a}) = (t = [a])"
apply (auto simp add: isListOf_oneset_to_onelist_ALL)
apply (simp add: isListOf_def)
done

lemma isListOf_onelist_to_oneset[simp]: "([a] isListOf X) = (X = {a})"
apply (simp add: isListOf_def)
done

(* --------------------------------------------------- *
               addition for CSP-Prover 5
 * --------------------------------------------------- *)

(* --------------------------------------------------- *
   convenient lemmas for practical verification in CSP
 * --------------------------------------------------- *)

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

lemma event_notin_channel_map:
   "ALL x:X. a ~= c x ==> a ~: c ` X"
by (auto)

lemma event_in_channel_map_inj:
   "inj c ==> (c v : c ` X) = (v:X)"
by (auto simp add: inj_on_def)

lemma event_in_channel_map:
   "v:X ==> c v : c ` X"
by (simp)

lemma event_notin_channel_range:
   "ALL x. a ~= c x ==> a ~: range c"
by (auto)

lemma event_in_channel_range:
   "c v : range c"
by (simp)

lemmas event_notin_channel =
       event_in_channel_map_inj
       event_notin_channel_map
       event_notin_channel_range

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

(* channel_Int_event_eq *)

lemma channel_Int_event_eq_map_left:
  "inj c ==> (c ` X) Int {c v} = (c ` (X Int {v}))"
by (auto simp add: inj_on_def)

lemma channel_Int_event_eq_map_right:
  "inj c ==> {c v} Int (c ` X) = (c ` (X Int {v}))"
by (auto simp add: inj_on_def)

lemma set_Int_single_in_left:
  "a:A ==> A Int {a} = {a}"
by (auto)

lemma set_Int_single_in_right:
  "a:A ==> {a} Int A = {a}"
by (auto)

(* channel_Int_event_not *)

lemma set_Int_single_notin_left:
  "a~:A ==> A Int {a} = {}"
by (simp)

lemma set_Int_single_notin_right:
  "a~:A ==> {a} Int A = {}"
by (simp)

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

lemmas channel_Int_event =
       channel_Int_event_eq_map_right
       channel_Int_event_eq_map_left
       set_Int_single_in_left
       set_Int_single_in_right

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

(* channel_Int_channel_eq *)

lemma channel_Int_channel_eq:
  "inj c ==> (c ` X) Int (c ` Y) = (c ` (X Int Y))"
by (auto simp: inj_on_def)

(* channel_Int_channel_neq *)

lemma channel_Int_channel_neq_map:
  "[| ALL x:X. ALL y:Y. a x ~= b y |] ==> (a ` X) Int (b ` Y) = {}"
by (auto)

lemma channel_Int_channel_neq_range:
  "[| ALL x y. a x ~= b y |] ==> (range a) Int (range b) = {}"
by (auto)

lemmas channel_Int_channel_neq =
       channel_Int_channel_neq_map
       channel_Int_channel_neq_range

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

lemmas channel_Int_channel =
       channel_Int_channel_eq
       channel_Int_channel_neq

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

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

lemma channel_Un_channel:
  "inj c ==> (c ` X) Un (c ` Y) = (c ` (X Un Y))"
by (auto simp: inj_on_def)

(* -------------------------------------------- *
                  Insert Int
 * -------------------------------------------- *)

(* event_insert_Int *)

lemma event_insert_Int_event_left:
  "a ~= b | b ~= a ==> (insert a A) Int {b} = A Int {b}"
by (auto)

lemma event_insert_Int_event_right:
  "a ~= b | b ~= a ==> {b} Int (insert a A) = {b} Int A"
by (auto)

lemma event_insert_Int_channel_notin_left:
  "a ~: B ==> (insert a A) Int B = A Int B"
by (auto)

lemma event_insert_Int_channel_notin_right:
  "a ~: B ==> B Int (insert a A) = B Int A"
by (auto)

lemma event_insert_Int_channel_in_left:
  "a : B ==> (insert a A) Int B = insert a (A Int B)"
by (auto)

lemma event_insert_Int_channel_in_right:
  "a : B ==> B Int (insert a A) = insert a (B Int A)"
by (auto)

lemma event_insert_Int_insert_channel_in:
  "a:B ==>
   (insert a A) Int (insert b B) =
    insert a (A Int (insert b B))"
by (auto)

lemma event_insert_Int_insert_channel_notin:
  "[| a~= b | b~=a ; a~:B |] ==>
   (insert a A) Int (insert b B) =
    (A Int (insert b B))"
by (auto)

lemmas event_insert_Int =
       event_insert_Int_event_left
       event_insert_Int_event_right
       event_insert_Int_channel_notin_left
       event_insert_Int_channel_notin_right
       event_insert_Int_channel_in_left
       event_insert_Int_channel_in_right
       event_insert_Int_insert_channel_in
       event_insert_Int_insert_channel_notin

(* -------------------------------------------- *
                  Insert Un
 * -------------------------------------------- *)

(* event_insert_Un *)

lemma event_insert_in:
  "a : A ==> (insert a A) = A"
by (auto)

(* lemma subset_insertI: "A <= insert a A" *)

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

(* event_insert_Int *)

lemma event_diff_in:
  "a : A ==> {a} - A = {}"
by (simp)

lemma event_diff_notin:
  "a ~: A ==> {a} - A = {a}"
by (auto)

lemma event_diff_notin_map:
  "[| inj c ; v ~: X |] ==> {c v} - (c ` X) = {c v}"
by (auto simp add: inj_on_def)

lemmas event_diff =
       event_diff_notin
       event_diff_notin_map

(* channel *)

lemma channel_diff_eq_map:
  "inj c ==> (c ` X) - (c ` Y) = c ` (X-Y)"
by (auto simp add: inj_on_def)

lemma channel_diff_neq_map:
  "ALL x:X. ALL y:Y. a x ~= b y ==> (a ` X) - (b ` Y) = (a ` X)"
by (auto)

lemma channel_diff_neq_range:
  "ALL x. ALL y. a x ~= b y ==> range a - range b = range a"
by (auto)

lemmas channel_diff =
       channel_diff_eq_map
       channel_diff_neq_map
       channel_diff_neq_range

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

lemma Un_diff_dist_right: "(A Un B) - C = (A-C) Un (B-C)"
apply (auto)
done

lemma Un_diff_dist_left: "C - (A Un B) = (C-A) Int (C-B)"
apply (auto)
done

lemma insert_diff_dist_right_in:
  "a:C ==> (insert a A) - C = A-C"
by (simp)

lemma insert_diff_dist_right_notin:
  "a~:C ==> (insert a A) - C = {a} Un (A-C)"
by (auto)

lemma insert_diff_dist_left_eq_event:
  "{a} - (insert a A) = {}"
by (simp)

lemma insert_diff_dist_left_eq_map:
  "inj c ==> (c ` X) - (insert (c v) A) = (c ` (X-{v})) Int ((c ` X)-A)"
by (auto simp add: inj_on_def)

lemma insert_diff_dist_left_eq_range:
 "inj c ==> (range c) - (insert (c v) A) = (c ` (UNIV-{v})) Int ((range c)-A)"
by (auto simp add: inj_on_def)

lemma insert_diff_dist_left_neq_event:
  "a ~= b | b ~= a ==> {a} - (insert b A) = {a}-A"
by (auto)

lemma insert_diff_dist_left_neq:
  "a ~: X ==> X - (insert a A) = X - A"
by (auto)

lemmas event_insert_diff_dist =
      Un_diff_dist_right
      Un_diff_dist_left
      insert_diff_dist_right_in
      insert_diff_dist_right_notin
      insert_diff_dist_left_eq_event
      insert_diff_dist_left_eq_map
      insert_diff_dist_left_eq_range
      insert_diff_dist_left_neq_event
      insert_diff_dist_left_neq
      event_diff
      channel_diff

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

lemmas dist_event_set = Int_Un_distrib Int_Un_distrib2
                        event_insert_diff_dist

lemmas simp_event_set = event_insert_in
                        channel_Int_event
                        channel_Int_channel
                        channel_Un_channel
                        event_insert_Int
                        event_notin_channel
                        dist_event_set

(*                      Un_assoc     (A Un B) Un C = A Un(B Un C) *)

end


(*
Title:  Allen's qualitative temporal calculus
Author:  Fadoua Ghourabi (fadouaghourabi@gmail.com)
Affiliation: Ochanomizu University, Japan
*)

theory nest

imports
    Main  jointly_exhaustive examples
    "HOL-Eisbach.Eisbach_Tools"

begin

section \<open>Nests\<close>
text\<open>Nests are sets of intervals that share a meeting point. We define relation before between nests that give the ordering properties of points.\<close>

subsection \<open>Definitions\<close>

type_synonym 'a nest = "'a set"

definition (in arelations) BEGIN :: "'a \<Rightarrow> 'a nest"
where "BEGIN i  = {j | j. (j,i) \<in> ov \<union> s \<union> m \<union> f^-1 \<union> d^-1 \<union> e \<union> s^-1}"

definition (in arelations) END :: "'a \<Rightarrow> 'a nest"
where "END i  = {j | j. (j,i) \<in> e \<union> ov^-1 \<union> s^-1 \<union> d^-1 \<union> f \<union> f^-1 \<union> m^-1}"

definition (in arelations) NEST ::"'a nest \<Rightarrow> bool"
where "NEST S \<equiv> \<exists>i. \<I> i \<and> (S = BEGIN i \<or> S = END i)"

definition (in arelations) before :: "'a nest \<Rightarrow> 'a nest \<Rightarrow> bool" (infix "\<lless>" 100)
where "before N M \<equiv> NEST N \<and> NEST M \<and> (\<exists>n m. \<^cancel>\<open>\<I> m \<and> \<I> n \<and>\<close> n \<in> N \<and> m \<in> M \<and> (n,m) \<in> b)"

subsection \<open>Properties of Nests\<close>

lemma intv1:
assumes "\<I> i"
shows "i \<in> BEGIN i"
unfolding BEGIN_def
by (simp add:e assms)

lemma intv2:
assumes "\<I> i"
shows "i \<in> END i"
unfolding END_def
by (simp add: e assms)

lemma NEST_nonempty:
assumes "NEST S"
shows "S \<noteq> {}"
using assms unfolding NEST_def
by (insert intv1 intv2, auto)

lemma NEST_BEGIN:
assumes "\<I> i"
shows "NEST (BEGIN i)"
using NEST_def assms by auto

lemma NEST_END:
assumes "\<I> i"
shows "NEST (END i)"
using NEST_def assms by auto

lemma before:
assumes a:"\<I> i"
shows "BEGIN i \<lless> END i"
proof -

    obtain p where pi:"(p,i) \<in> m"
    using  a M3 m by blast
    then have p:"p \<in> BEGIN i" using BEGIN_def by auto

    obtain q where qi:"(q,i) \<in> m^-1"
    using a M3 m by blast
    then have q:"q \<in> END i" using END_def by auto

    from pi qi have c1:"(p,q) \<in> b" using b  m
    by blast

    with  c1 p q assms show ?thesis  by (auto simp:NEST_def  before_def)

qed

lemma  meets:
fixes i j
 assumes "\<I> i" and "\<I> j"
shows "(i,j) \<in> m = ((END i) = (BEGIN j))"
proof
  assume ij:"(i,j) \<in> m" then have ibj:"i \<in> (BEGIN j)" unfolding BEGIN_def by auto
  from ij have ji:"(j,i) \<in> m^-1" by simp
  then have jeo:"j \<in> (END i)" unfolding END_def by simp
  show "((END i) = (BEGIN j))"
  proof
      {fix x::"'a" assume a:"x \<in> (END i)"
      then have asimp:"(x,i) \<in> e \<union>  ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> m\<inverse> \<union> f^-1"
      unfolding END_def by auto
      then have "x \<in> (BEGIN j)" using ij eovisidifmifiOm
      by (auto simp:BEGIN_def)
      }
      thus conc1:"END i \<subseteq> BEGIN j" by auto
   next
     {fix x assume b:"x \<in> (BEGIN j)"
     then have bsimp:"(x,j) \<in> ov \<union> s\<union> m  \<union> f^-1 \<union> d^-1 \<union> e \<union> s^-1"
     unfolding BEGIN_def by auto
     then have "x \<in> (END i)" using ij ovsmfidiesiOmi
     by (auto simp:END_def)
     }thus conc2:"BEGIN j \<subseteq> END i" by auto
  qed
next
  assume a0:"END (i::'a) = BEGIN (j::'a)" show "(i,j) \<in> m"
  proof (rule ccontr)
     assume a:"(i,j) \<notin> m" then have "\<not>i\<parallel>j" using m by auto
     from a have "(i,j) \<in> b  \<union> ov  \<union> s \<union> d \<union> f^-1 \<union> e \<union> f  \<union> s^-1  \<union> d^-1 \<union> ov^-1 \<union> m^-1  \<union> b^-1" using assms JE by auto
     thus False
     proof (auto)
       {assume ij:"(i,j) \<in> e"
        obtain p where ip:"i\<parallel>p" using M3 assms(1)  by auto
        then  have pi:"(p,i)\<in> m^-1" using m by auto
        then have "p \<in> END i" using END_def by auto
        with a0 have pj:"p \<in> (BEGIN j) " by auto
        from ij pi have "(p,j) \<in> m^-1"  by (simp add: e)
        with pj show   ?thesis
        apply (auto simp:BEGIN_def)
        using m_rules by auto[7] }
       next
       {assume ij: "(j,i) \<in> m"
        obtain p where ip:"i\<parallel>p" using M3  assms(1) by auto
        then  have pi:"(p,i)\<in> m^-1" using m by auto
        then have "p \<in> END i" using END_def by auto
        with a0 have pj:"p \<in> (BEGIN j) " by auto
        from ij have "(i,j) \<in> m^-1" by simp
        with  pi have "(p,j) \<in> b^-1" using cmimi  by auto
        with pj show   ?thesis
        apply (auto simp:BEGIN_def)
          using b_rules by auto
        }

       next

       {assume ij:"(i,j)\<in> b"
        have ii:"(i,i)\<in>e" and "i \<in> END i" using assms  intv2 e by auto
        with a0 have j:"i \<in> BEGIN j" by simp
        with ij  show   ?thesis
        apply (auto simp:BEGIN_def)
          using b_rules by auto
         }

        next

        { assume ji:"(j,i) \<in> b" then have ij:"(i,j) \<in> b^-1" by simp
          have ii:"(i,i)\<in>e" and "i \<in> END i" using assms intv2 e by auto
          with a0 have j:"i \<in> BEGIN j" by simp
          with ij  show   ?thesis
          apply (auto simp:BEGIN_def)
            using b_rules by auto}

        next

        {assume ij:"(i,j)\<in>ov"
         then obtain u v::"'a" where iu:"i\<parallel>u" and uv:"u\<parallel>v" and uv:"u\<parallel>v" using ov by blast
         from iu have "u \<in> END i" using m END_def by auto
         with a0 have u:"u \<in> BEGIN j" by simp
         from iu have "(u,i) \<in> m^-1" using m by auto
         with ij have uj:"(u,j) \<in> ov^-1 \<union> d \<union> f" using covim by auto
         show ?thesis using u uj
         apply (auto simp:BEGIN_def)
           using ov_rules eovi apply auto[9]
            using s_rules apply auto[2]
              using d_rules apply auto[5]
                using f_rules by auto[5]
        }

        next

        {assume "(j,i) \<in> ov" then have  ij:"(i,j)\<in>ov^-1" by simp let ?p = i
        from ij have pi:"(?p, i) \<in> e" by (simp add:e)
        from ij have pj:"(?p,j) \<in> ov^-1" by simp
        from pi have "?p \<in> END i" using END_def by auto
        with a0 have "?p \<in> (BEGIN j) " by auto
        with pj show ?thesis
        apply (auto simp:BEGIN_def)
          using ov_rules by auto
        }
        next
        {assume ij:"(i,j) \<in> s"
         then obtain p q t where ip:"i\<parallel>p" and pq:"p\<parallel>q" and  jq:"j\<parallel>q" and ti:"t\<parallel>i" and tj:"t\<parallel>j" using s by blast
         from ip have "(p,i) \<in> m^-1" using m by auto
         then have "p \<in> END i" using END_def by auto
         with a0 have p:"p \<in> BEGIN j" by simp
         from ti ip pq tj jq have "(p,j) \<in> f" using f by blast
         with p show ?thesis
         apply (auto simp:BEGIN_def)
           using f_rules by auto

         }
         next
         {assume "(j,i) \<in> s" then have  ij:"(i,j) \<in> s^-1" by simp
          then obtain u v where ju:"j\<parallel>u" and uv:"u\<parallel>v" and iv:"i\<parallel>v" using s by blast
          from iv have "(v,i) \<in> m^-1" using m by blast
          then have "v \<in> END i" using END_def by auto
          with a0 have v:"v \<in> BEGIN j" by simp
          from ju uv have "(v,j) \<in> b^-1" using b by auto
          with v show ?thesis
          apply (auto simp:BEGIN_def)
            using b_rules by auto}
         next
         {assume ij:"(i,j) \<in> f"
          have "(i,i) \<in> e" and "i \<in> END i"
          by (simp add: e)  (auto simp: assms intv2 )
          with a0 have "i \<in> BEGIN j" by simp
          with ij show ?thesis
          apply (auto simp:BEGIN_def)
           using f_rules by auto
         }
         next
         {assume "(j,i) \<in> f" then have "(i,j)\<in>f^-1" by simp
          then obtain u where ju:"j\<parallel>u" and iu:"i\<parallel>u" using f by auto
          then have ui:"(u,i) \<in> m^-1" and "u \<in> END i"
          apply (simp add: converse.intros m)
          using END_def iu m by auto
          with a0 have ubj:"u \<in> BEGIN j" by simp
          from ju have "(u,j) \<in> m^-1"  by (simp add: converse.intros m)
          with ubj show  ?thesis
          apply (auto simp:BEGIN_def)
            using m_rules by auto
         }
         next
         {assume ij:"(i,j) \<in> d" then
          have "(i,i) \<in> e" and "i \<in> END i" using assms e by (blast, simp add:  intv2)
          with a0 have "i \<in> BEGIN j" by simp
          with ij show ?thesis
          apply (auto simp:BEGIN_def)
            using d_rules by auto}
          next
          {assume ji:"(j,i) \<in> d" then have "(i,j) \<in> d^-1" using d by simp
           then obtain u v where ju:"j\<parallel>u" and uv:"u\<parallel>v" and iv:"i\<parallel>v" using d using ji by blast
           then have "(v,i) \<in> m^-1" and "v \<in> END i" using m END_def by auto
           with a0 ju uv have  vj:"(v,j) \<in> b^-1" and  "v \<in> BEGIN j" using b by auto
           with vj show ?thesis
           apply (auto simp:BEGIN_def)
             using b_rules by auto}

        qed
    qed
qed

lemma starts:
fixes i j
assumes "\<I> i" and "\<I> j"
shows "((i,j) \<in> s \<union> s^-1 \<union> e) = (BEGIN i = BEGIN j)"
proof
   assume a3:"(i,j) \<in> s \<union> s^-1 \<union> e" show "BEGIN i = BEGIN j"
   proof -
    { fix x assume "x \<in> BEGIN i" then have "(x,i) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
    hence "x \<in> BEGIN j" using a3 ovsmfidiesiOssie
    by (auto simp:BEGIN_def)
    } note c1 = this

    { fix x assume "x \<in> BEGIN j" then have xj:"(x,j) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
    then have "x \<in> BEGIN i"
    apply (insert converseI[OF a3] xj)
    apply (subst (asm) converse_Un)+
    apply (subst (asm) converse_converse)
    using ovsmfidiesiOssie
    by (auto simp:BEGIN_def)
    } note c2 = this

    from c1 have "BEGIN i \<subseteq> BEGIN j" by auto
    moreover with c2 have "BEGIN j \<subseteq> BEGIN i" by auto
    ultimately show ?thesis by auto
 qed
next
   assume a4:"BEGIN i = BEGIN j"
   with assms have "i \<in> BEGIN j" and "j \<in> BEGIN i" using intv1 by auto
   then have ij:"(i,j) \<in> ov \<union> s \<union> m \<union> f^-1 \<union> d^-1 \<union> e \<union> s^-1" and ji:"(j,i) \<in>  ov \<union> s \<union> m \<union> f^-1 \<union> d^-1 \<union> e \<union> s^-1"
   unfolding BEGIN_def by auto
   then have ijov:"(i,j) \<notin> ov"
    apply auto
    using ov_rules  by auto

   from ij ji have ijm:"(i,j) \<notin> m"
   apply (simp_all, elim disjE, simp_all)
    using ov_rules apply auto[13]
      using s_rules apply auto[11]
        using m_rules apply auto[9]
          using f_rules apply auto[7]
            using d_rules apply auto[5]
              using m_rules by auto[4]

   from ij ji have  ijfi:"(i,j) \<notin> f^-1"
   apply (simp_all, elim disjE, simp_all)
    using ov_rules apply auto[13]
     using s_rules apply auto[11]
      using m_rules apply auto[9]
          using f_rules apply auto[7]
            using d_rules apply auto[5]
              using f_rules by auto[4]

   from ij ji have  ijdi:"(i,j) \<notin> d^-1"
   apply (simp_all, elim disjE, simp_all)
    using ov_rules apply auto[13]
     using s_rules apply auto[11]
      using m_rules apply auto[9]
          using f_rules apply auto[7]
            using d_rules apply auto[5]
              using d_rules by auto[4]

   from ij ijm ijov ijfi ijdi show "(i, j) \<in> s \<union> s\<inverse> \<union> e" by auto

qed

lemma xj_set:"x \<in> {a |a. (a, j) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>} = ((x,j) \<in>  e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>)"
by blast

lemma ends:
fixes i j
assumes "\<I> i" and "\<I> j"
shows "((i,j) \<in> f \<union> f^-1 \<union> e) = (END i = END j)"
proof
   assume a3:"(i,j) \<in> f \<union> f^-1 \<union> e" show "END i = END j"
   proof -
    { fix x assume "x \<in> END i" then have "(x,i) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
      then have "x \<in> END j" using a3 unfolding END_def
      apply (subst xj_set)
      using ceovisidiffimi_ffie_simp by simp
     } note c1 =this

    { fix x assume "x \<in> END j" then have "(x,j) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
      then have "x \<in> END i" using a3 unfolding END_def
      by (metis Un_iff ceovisidiffimi_ffie_simp converse_iff eei mem_Collect_eq)
     }  note c2 = this

    from c1 have "END i \<subseteq> END j" by auto
    moreover with c2 have "END j \<subseteq> END i" by auto
    ultimately show ?thesis by auto
  qed (*} note conc = this *)
  next
  assume a4:"END i = END j"
  with assms have "i \<in> END j" and "j \<in> END i" using intv2 by auto
  then have ij:"(i,j) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" and ji:"(j,i) \<in>  e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>"
  unfolding END_def by auto
  then  have ijov:"(i,j) \<notin> ov^-1"
  apply (simp_all, elim disjE, simp_all)
   using eov es ed efi ef em eov  apply auto[13]
    using ov_rules apply auto[11]
     using s_rules apply auto[9]
      using d_rules apply auto[7]
        using f_rules apply auto[8]
          using movi by auto

  from ij ji have  ijm:"(i,j) \<notin> m^-1"
  apply (simp_all, elim disjE, simp_all)
    using m_rules by auto

  from ij ji have  ijfi:"(i,j) \<notin> s^-1"
  apply (simp_all, elim disjE, simp_all)
    using s_rules by auto

  from ij ji have  ijdi:"(i,j) \<notin> d^-1"
  apply (simp_all, elim disjE, simp_all)
    using d_rules by auto

  from ij ijm ijov ijfi ijdi  show "(i, j) \<in> f \<union> f\<inverse> \<union> e"  by auto
qed

lemma before_irrefl:
fixes a
shows "\<not> a \<lless> a"
proof (rule ccontr, auto)
  assume a0:"a \<lless> a"
  then have "NEST a"  unfolding before_def by auto
  then obtain i where   i:"a = BEGIN i \<or> a = END i" unfolding NEST_def by auto
  from i show False
  proof
      assume "a = BEGIN i"
      with a0 have "BEGIN i \<lless> BEGIN i" by simp
      then obtain p q where "p\<in> BEGIN i" and "q \<in> BEGIN i" and b:"(p,q) \<in> b" unfolding before_def by auto
      then  have a1:"(p,i) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" and a2:"(i,q) \<in> ov^-1 \<union> s^-1 \<union> m^-1 \<union> f \<union> d \<union> e \<union> s" unfolding BEGIN_def
      apply auto
      using eei apply fastforce
      by (simp add: e)+
      with b show False
      using piiq[of p i q]
      apply auto
        using  b_rules using disjoint_iff_not_equal by auto
      next
      assume "a = END i"
      with a0 have "END i \<lless> END i" by simp
      then obtain p q where "p\<in> END i" and "q \<in> END i" and b:"(p,q) \<in> b" unfolding before_def by auto
      then  have a1:"(p,i) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" and a2:"(q,i) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def
      by auto
      with b show False
      apply (subst (asm) converse_iff[THEN sym])
      using cbi_alpha1ialpha4mi neq_bi_alpha1ialpha4mi relcomp.relcompI subsetCE by blast
     qed
qed

lemma BEGIN_before:
fixes i j
assumes "\<I> i" and "\<I> j"
shows "BEGIN i \<lless> BEGIN j = ((i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>)"
proof

     assume a3:"BEGIN i \<lless> BEGIN j"
     from a3 obtain p q where pa:"p \<in> BEGIN i" and qc:"q \<in> BEGIN j" and pq:"(p,q) \<in> b" unfolding before_def by auto
     then obtain r where "p\<parallel>r" and "r\<parallel>q" using b by auto
     then have pr:"(p,r) \<in> m" and rq:"(r,q) \<in> m" using m by auto
     from pa  have pi:"(p,i) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
     moreover with pr have "(r,p) \<in> m^-1" by simp
     ultimately have "(r,i) \<in> d \<union> f \<union> ov^-1 \<union> e \<union> f^-1 \<union> m^-1 \<union> b^-1 \<union> s \<union> s^-1"
     using cmiov cmis cmim cmifi cmidi  cmisi
     apply ( simp_all,elim disjE, auto)
        by (simp add: e)

     then have ir:"(i,r) \<in> d^-1 \<union> f^-1 \<union> ov \<union> e \<union> f \<union> m \<union> b \<union> s^-1 \<union> s"
     by (metis (mono_tags, lifting) converseD converse_Un converse_converse eei)

     from qc  have "(q,j) \<in>  ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
     with rq have rj:"(r,j) \<in> b \<union> s \<union> m "
     using m_ovsmfidiesi using contra_subsetD relcomp.relcompI by blast

     with ir have c1:"(i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse> \<union> d \<union> e \<union> s \<union> s\<inverse>"
     using difibs by blast
     {assume "(i,j) \<in> s\<or> (i,j)\<in>s^-1 \<or> (i,j) \<in> e" then have "BEGIN i = BEGIN j"
      using starts Un_iff assms(1) assms(2) by blast
      with a3 have False  by (simp add: before_irrefl)}

      from c1 have c1':"(i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse> \<union> d "
      using \<open>(i, j) \<in> s \<or> (i, j) \<in> s\<inverse> \<or> (i, j) \<in> e \<Longrightarrow> False\<close> by blast

     {assume "(i,j) \<in> d" with pi have "(p,j) \<in> e \<union>  s \<union> d \<union> ov \<union> ov^-1 \<union> s^-1 \<union> f \<union> f^-1  \<union> d^-1"
      using ovsmfidiesi_d using relcomp.relcompI subsetCE by blast
      with pq have "(q,j) \<in>  b^-1 \<union> d \<union> f \<union> ov^-1 \<union> m^-1"
      apply (subst (asm) converse_iff[THEN sym])
      using cbi_esdovovisiffidi by blast
      with qc have False unfolding BEGIN_def
      apply (subgoal_tac "(q, j) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>")
        prefer 2
        apply simp
          using neq_beta2i_alpha2alpha5m by auto
      }

     with c1' show "((i, j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>)"   by auto
   next
      assume "(i, j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>"
      then show "BEGIN i \<lless> BEGIN j"
      proof  ( simp_all,elim disjE, simp_all)
        assume "(i,j) \<in> b" thus ?thesis using intv1  using before_def NEST_BEGIN assms by metis
        next
        assume iu:"(i,j) \<in> m"
        obtain l where li:"(l,i) \<in> m" using M3 m meets_wd assms by blast
        with iu have "(l,j) \<in> b" using cmm by auto
        moreover from li  have "l \<in> (BEGIN i)" using BEGIN_def by auto
        ultimately show ?thesis using intv1  before_def NEST_BEGIN assms by blast
        next
        assume iu:"(i,j) \<in> ov"
        obtain l where li:"(l,i) \<in> m" using M3 m meets_wd assms by blast
        with iu have "(l,j) \<in> b" using cmov by auto
        moreover from li have "l \<in> (BEGIN i)" using BEGIN_def by auto
        ultimately show ?thesis using intv1 before_def NEST_BEGIN assms by blast
        next
        assume iu:"(j,i) \<in> f"
        obtain l where li:"(l,i) \<in> m" using M3 m meets_wd assms by blast
        with iu have "(l,j) \<in> b" using cmfi by auto
        moreover from li have "l \<in> (BEGIN i)" using BEGIN_def by auto
        ultimately show ?thesis using intv1  before_def NEST_BEGIN assms by blast
        next
        assume iu:"(j,i) \<in> d"
        obtain l where li:"(l,i) \<in> m" using M3 m meets_wd assms by blast
        with iu have "(l,j) \<in> b" using cmdi by auto
        moreover from li have "l \<in> (BEGIN i)" using BEGIN_def by auto
        ultimately show ?thesis using intv1 before_def NEST_BEGIN assms by blast

     qed
qed

lemma BEGIN_END_before:
fixes i j
assumes "\<I> i" and "\<I> j"
shows  "BEGIN i \<lless> END j = ((i,j) \<in> e \<union> b \<union> m \<union> ov \<union> ov^-1 \<union> s \<union> s^-1 \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>) "
proof
     assume a3:"BEGIN i \<lless> END j"
     then obtain p q where pa:"p \<in> BEGIN i" and qc:"q \<in> END j" and pq:"(p,q) \<in> b" unfolding before_def by auto
     then obtain r where "p\<parallel>r" and "r\<parallel>q" using b by auto
     then have pr:"(p,r) \<in> m" and rq:"(r,q) \<in> m" using m by auto
     from pa  have pi:"(p,i) \<in> ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
     moreover with pr have "(r,p) \<in> m^-1" by simp
     ultimately have "(r,i) \<in> d \<union> f \<union> ov^-1 \<union> e \<union> f^-1 \<union> m^-1 \<union> b^-1 \<union> s \<union> s^-1" using cmiov cmis cmim cmifi cmidi e cmisi
     by ( simp_all,elim disjE, auto simp:e)

     then have ir:"(i,r) \<in> d^-1 \<union> f^-1 \<union> ov \<union> e \<union> f \<union> m \<union> b \<union> s^-1 \<union> s"
     by (metis (mono_tags, lifting) converseD converse_Un converse_converse eei)

     from qc  have "(q,j) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
     with rq have rj:"(r,j) \<in> m \<union> ov \<union> s \<union> d \<union> b \<union> f^-1 \<union> f \<union> e" using cm_alpha1ialpha4mi by blast

     with ir show  c1:"(i,j) \<in> e \<union> b \<union> m \<union> ov \<union> ov^-1 \<union> s \<union> s^-1 \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>"
     using difimov by blast
     next
     assume a4:"(i, j) \<in> e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>"
     then show "BEGIN i \<lless> END j"
     proof  ( simp_all,elim disjE, simp_all)
           assume "(i,j) \<in> e"
           obtain l k where l:"l\<parallel>i" and "i\<parallel>k" using M3 meets_wd assms  by blast
           with \<open>(i,j) \<in> e\<close> have k:"j\<parallel>k" by (simp add: e)
           from l k have "(l,i) \<in> m" and "(k,j) \<in> m^-1" using m by auto
           then  have "l \<in> BEGIN i" and "k \<in> END j" using BEGIN_def END_def by auto
           moreover from l \<open>i\<parallel>k\<close> have "(l,k) \<in> b" using b by auto
           ultimately show ?thesis using before_def assms NEST_BEGIN NEST_END  by blast
          next
           assume "(i,j) \<in> b"
           then show ?thesis using before_def assms NEST_BEGIN NEST_END intv1[of i] intv2[of j] by auto
          next
            assume "(i,j) \<in> m"
            obtain l where "l\<parallel>i" using M3 assms by blast
            then have "l\<in>BEGIN i" using m BEGIN_def by auto
            moreover from \<open>(i,j)\<in>m\<close> \<open>l\<parallel>i\<close> have "(l,j) \<in> b" using b m by blast
            ultimately show ?thesis using intv2[of j] assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(i,j) \<in> ov"
           then obtain l k where li:"l\<parallel>i" and lk:"l\<parallel>k" and ku:"k\<parallel>j" using ov by blast
           from li have "l \<in> BEGIN i" using m BEGIN_def by auto
           moreover from lk ku have "(l,j) \<in> b" using b by auto
           ultimately show ?thesis using intv2[of j] assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(j,i) \<in> ov"
           then obtain l k v where uv:"j\<parallel>v" and lk:"l\<parallel>k" and kv:"k\<parallel>v" and li:"l\<parallel>i" using ov by blast
           from li have "l \<in> BEGIN i" using m BEGIN_def by auto
           moreover from uv have "v \<in> END j" using m END_def by auto
           moreover from lk kv have "(l,v) \<in> b" using b by auto
           ultimately show ?thesis using  assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(i,j) \<in> s"
           then obtain v v' where iv:"i\<parallel>v" and vvp:"v\<parallel>v'" and "j\<parallel>v'" using s by blast
           then have "v' \<in> END j" using END_def m by auto
           moreover from iv vvp have "(i,v') \<in> b" using b by auto
           ultimately show ?thesis using intv1[of i]  assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(j,i) \<in> s"
           then obtain l v where li:"l\<parallel>i" and lu:"l\<parallel>j" and "j\<parallel>v" using s by blast
           then have "v \<in> END j" using m END_def by auto
           moreover from li have "l \<in> BEGIN i" using m BEGIN_def by auto
           moreover from lu \<open>j\<parallel>v\<close> have "(l,v) \<in> b" using b by auto
           ultimately show ?thesis using  assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(i,j) : f"
           then obtain l v where li:"l\<parallel>i" and iv:"i\<parallel>v" and "j\<parallel>v" using f by blast
           then have "v \<in> END j" using m END_def by auto
           moreover from li have "l \<in> BEGIN i" using m BEGIN_def by auto
           moreover from iv li have "(l,v) \<in> b" using b by auto
           ultimately show ?thesis using assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(j,i) \<in> f"
           then obtain l v where li:"l\<parallel>i" and iv:"i\<parallel>v" and "j\<parallel>v" using f by blast
           then have "v \<in> END j" using m END_def by auto
           moreover from li have "l \<in> BEGIN i" using m BEGIN_def by auto
           moreover from iv li have "(l,v) \<in> b" using b by auto
           ultimately show ?thesis using  assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(i,j) : d"
           then obtain k v where ik:"i\<parallel>k" and kv:"k\<parallel>v" and "j\<parallel>v" using d by blast
           then have "v \<in> END j" using END_def m by auto
           moreover from ik kv have "(i,v) \<in> b" using b by auto
           ultimately show ?thesis using intv1[of i]  assms NEST_BEGIN NEST_END before_def by blast
          next
           assume "(j,i) \<in> d"
           then obtain l k where "l\<parallel>i" and lk:"l\<parallel>k" and ku:"k\<parallel>j" using d by blast
           then have "l \<in> BEGIN i" using BEGIN_def m by auto
           moreover from lk ku have "(l,j) \<in> b" using b by auto
           ultimately show ?thesis using intv2[of j] assms NEST_BEGIN NEST_END before_def by blast
       qed
qed

lemma END_BEGIN_before:
fixes i j
assumes "\<I> i" and "\<I> j"
shows  "END i \<lless> BEGIN j =  ((i,j) \<in> b) "
proof
     assume a3:"END i \<lless> BEGIN j"
     from a3 obtain p q where pa:"p \<in> END i" and qc:"q \<in> BEGIN j" and pq:"(p,q) \<in> b" unfolding before_def by auto
     then obtain r where "p\<parallel>r" and "r\<parallel>q" using b by auto
     then have pr:"(p,r) \<in> m" and rq:"(r,q) \<in> m" using m by auto
     from pa  have pi:"(p,i) \<in>  e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
     moreover with pr have "(r,p) \<in> m^-1" by simp
     ultimately have "(r,i) \<in> m^-1 \<union> b^-1" using e cmiovi cmisi cmidi cmif cmifi cmimi
     by ( simp_all,elim disjE, auto simp:e)

     then have ir:"(i,r) \<in> m \<union> b" by simp

     from qc  have "(q,j) \<in>  ov \<union> s \<union> m \<union> f\<inverse> \<union> d\<inverse> \<union> e \<union> s\<inverse>" unfolding BEGIN_def by auto
     with rq have rj:"(r,j) \<in> b \<union> m " using cmov cms cmm cmfi cmdi e cmsi
     by (simp_all, elim disjE, auto simp:e)

     with ir show "(i,j) \<in> b" using cmb cmm cbm cbb by auto

   next
     assume "(i,j) \<in> b" thus "END i \<lless> BEGIN j" using intv1[of j] intv2[of i] assms before_def NEST_END NEST_BEGIN by auto
qed

lemma END_END_before:
fixes i j
assumes "\<I> i" and "\<I> j"
shows "END i \<lless> END j = ((i,j) \<in> b \<union> m \<union> ov \<union> s \<union> d) "
proof
     assume a3:"END i \<lless> END j"
     from a3 obtain p q where pa:"p \<in> END i" and qc:"q \<in> END j" and pq:"(p,q) \<in> b" unfolding before_def by auto
     then obtain r where "p\<parallel>r" and "r\<parallel>q" using b by auto
     then have pr:"(p,r) \<in> m" and rq:"(r,q) \<in> m" using m by auto
     from pa  have pi:"(p,i) \<in>  e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
     moreover with pr have "(r,p) \<in> m^-1" by simp
     ultimately have "(r,i) \<in> m^-1 \<union> b^-1" using e cmiovi cmisi cmidi cmif cmifi cmimi
     by ( simp_all,elim disjE, auto simp:e)

     then have ir:"(i,r) \<in> m \<union> b" by simp

     from qc  have "(q,j) \<in> e \<union> ov\<inverse> \<union> s\<inverse> \<union> d\<inverse> \<union> f \<union> f\<inverse> \<union> m\<inverse>" unfolding END_def by auto
     with rq have rj:"(r,j) \<in> m \<union> ov \<union> s \<union> d \<union> b \<union> f^-1 \<union> e \<union> f " using e cmovi cmsi cmdi cmf cmfi cmmi
     by (simp_all, elim disjE, auto simp:e)

     with ir show "(i,j) \<in> b \<union> m \<union> ov \<union> s \<union> d" using cmm cmov cms cmd cmb cmfi e cmf cbm cbov cbs cbd cbb cbfi cbf
     by (simp_all, elim disjE, auto simp:e)
    next
     assume "(i, j) \<in> b \<union> m \<union> ov \<union> s \<union> d"
     then show "END i \<lless> END j"
     proof  ( simp_all,elim disjE, simp_all)
        assume "(i,j) \<in> b" thus ?thesis using intv2[of i] intv2[of j] assms NEST_END before_def by blast
       next
        assume "(i,j) \<in> m"
        obtain v where "j\<parallel>v" using M3 assms by blast
        with \<open>(i,j) \<in> m\<close> have "(i,v) \<in>b" using b m by blast
        moreover from \<open>j\<parallel>v\<close> have "v \<in> END j" using m END_def by auto
        ultimately show ?thesis using intv2[of i] assms NEST_END before_def by blast
       next
        assume "(i,j) : ov"
        then obtain v v' where iv:"i\<parallel>v" and vvp:"v\<parallel>v'" and "j\<parallel>v'" using ov by blast
        then have "v' \<in> END j" using m END_def by auto
        moreover from iv vvp have "(i,v') \<in> b" using b by auto
        ultimately show ?thesis using intv2[of i] assms NEST_END before_def by blast
       next
        assume "(i,j) \<in> s"
        then obtain v v' where iv:"i\<parallel>v" and vvp:"v\<parallel>v'" and "j\<parallel>v'" using s by blast
        then have "v' \<in> END j" using m END_def by auto
        moreover from iv vvp have "(i,v') \<in> b" using b by auto
        ultimately show ?thesis using intv2[of i] assms NEST_END before_def by blast
       next
        assume "(i,j) \<in> d"
        then obtain v v' where iv:"i\<parallel>v" and vvp:"v\<parallel>v'" and "j\<parallel>v'" using d by blast
        then have "v' \<in> END j" using m END_def by auto
        moreover from iv vvp have "(i,v') \<in> b" using b by auto
        ultimately show ?thesis using intv2[of i] assms NEST_END before_def by blast
       qed
qed

lemma overlaps:
assumes "\<I> i" and "\<I> j"
shows "(i,j) \<in> ov = ((BEGIN i \<lless> BEGIN j) \<and> (BEGIN j \<lless> END i) \<and> (END i \<lless> END j))"
proof

    assume a:"(i,j) \<in> ov"
    then obtain n t q  u v where nt:"n\<parallel>t" and tj:"t\<parallel>j" and tq:"t\<parallel>q" and qu:"q\<parallel>u" and  iu:"i\<parallel>u" and uv:"u\<parallel>v" and jv:"j\<parallel>v" and "n\<parallel>i" using ov by blast
    then have ni:"(n,i) \<in> m"  using  m  by blast
    then have  n:"n \<in> BEGIN i" unfolding BEGIN_def by auto
    from nt tj have nj:"(n,j) \<in> b" using b by auto
    have  "j \<in> BEGIN j" using assms(2) by (simp add: intv1)
    with assms n nj  have c1:"BEGIN i \<lless> BEGIN j" unfolding before_def using NEST_BEGIN by blast

    from tj have a1:"(t,j) \<in> m" and a2:"t \<in> BEGIN j" using m BEGIN_def by auto
    from iu have "(u,i) \<in> m^-1" and "u \<in> END i" using m END_def by auto
    with assms  tq qu a2 have c2:"BEGIN j \<lless> END i"  unfolding before_def using b  NEST_BEGIN NEST_END by blast

    have "i \<in> END i" by (simp add: assms  intv2)
    moreover with jv  have "v \<in> END j" using m END_def by auto
    moreover with iu uv have "(i,v) \<in> b" using b by auto
    ultimately have c3:"END i \<lless> END j" using assms NEST_END before_def by blast

    show "((BEGIN i \<lless> BEGIN j) \<and> (BEGIN j \<lless> END i) \<and> (END i \<lless> END j))" using c1 c2 c3 by simp
  next
    assume a0:"((BEGIN i \<lless> BEGIN j) \<and> (BEGIN j \<lless> END i) \<and> (END i \<lless> END j))"
    then have "(i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse> \<and> (i,j) \<in> e \<union> b^-1 \<union> m^-1 \<union> ov^-1 \<union> ov \<union> s^-1 \<union> s \<union> f^-1 \<union> f \<union> d^-1 \<union> d
                                                                                \<and> (i,j) \<in> b \<union> m \<union> ov \<union> s \<union> d"
    using BEGIN_before BEGIN_END_before END_END_before assms
    by (metis (no_types, lifting) converseD converse_Un converse_converse eei)
    then have "(i,j) \<in>  (b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>) \<inter> (e \<union> b^-1 \<union> m^-1 \<union> ov^-1 \<union> ov \<union> s^-1 \<union> s \<union> f^-1 \<union> f \<union> d^-1 \<union> d) \<inter> (b \<union> m \<union> ov \<union> s \<union> d)"
    by (auto)
    then show "(i,j) \<in> ov"
    using inter_ov by blast

qed

subsection \<open>Ordering of nests\<close>

class  strict_order =
fixes ls::"'a nest \<Rightarrow> 'a nest \<Rightarrow> bool"
assumes
  irrefl:"\<not> ls a a" and
  trans:"ls a c \<Longrightarrow> ls c g \<Longrightarrow> ls a g" and
  asym:"ls a c \<Longrightarrow> \<not> ls c a"

class total_strict_order = strict_order +
assumes trichotomy: "a = c \<Longrightarrow> (\<not> (ls a  c) \<and> \<not> (ls c  a))"

interpretation nest:total_strict_order "(\<lless>) "
proof
{ fix a::"'a nest"
show "\<not> a \<lless> a"
by (simp add: before_irrefl) } note  irrefl_nest = this

{fix a c::"'a nest"
assume  "a = c"
show "\<not> a \<lless> c \<and> \<not> c \<lless> a"
by (simp add: \<open>a = c\<close> irrefl_nest)} note trichotomy_nest = this

{fix a c g::"'a nest"
assume a:"a \<lless> c" and c:" c \<lless> g"
show " a \<lless> g"
proof -
   from a c have na:"NEST a"  and nc:"NEST c" and ng:"NEST g" unfolding before_def by auto
   from na obtain i where  i:"a = BEGIN i \<or> a = END i" and wdi:"\<I> i" unfolding NEST_def by auto
   from nc obtain j where  j:"c = BEGIN j \<or> c = END j" and wdj:"\<I> j" unfolding NEST_def by auto
   from ng obtain u where  u:"g = BEGIN u \<or> g = END u" and wdu:"\<I> u" unfolding NEST_def by auto
   from i j u show ?thesis
   proof (elim disjE, auto)
     assume abi:"a = BEGIN i" and  cbj:"c = BEGIN j" and  gbu:"g = BEGIN u"
     from abi cbj a wdi wdj have "(i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse> " using BEGIN_before by auto
     moreover from cbj gbu c wdj wdu have "(j,u) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>" using BEGIN_before by auto

     ultimately have c1:"(i,u) \<in> b \<union> m \<union> ov \<union> f^-1 \<union> d^-1"
     using cbeta2_beta2 by blast

     then have "a \<lless> g" by (simp add: BEGIN_before abi gbu wdi wdu)

     thus "BEGIN i \<lless> BEGIN u" using abi gbu by auto
    next
     assume  abi:"a = BEGIN i" and  cbj:"c = BEGIN j" and  geu:"g = END u"
     from abi cbj a wdi wdj have "(i,j) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse> " using BEGIN_before by auto
     moreover from cbj geu c wdj wdu have "(j,u) :  e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>" using BEGIN_END_before by auto
     ultimately have "(i,u) \<in> e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>"
     using cbeta2_gammabm by blast

     then have "a \<lless> g"
     by (simp add: BEGIN_END_before abi geu wdi wdj wdu)

     thus "BEGIN i \<lless> END u" using abi geu by auto
    next
     assume abi:"a = BEGIN i" and  cej:"c = END j" and  gbu:"g = BEGIN u"
     from abi cej a wdi wdj  have ij:"(i,j) :  e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>" using BEGIN_END_before by auto
     from cej gbu c wdj wdu  have "(j,u) \<in> b " using END_BEGIN_before by auto
     with ij have "(i,u) \<in> b \<union> m \<union> ov \<union> f^-1 \<union> d^-1"
     using ebmovovissifsiddib by ( auto)

     thus "BEGIN i \<lless> BEGIN u"
     by (simp add: BEGIN_before abi gbu wdi wdu)
    next
     assume abi:"a = BEGIN i" and  cej:"c = END j" and  geu:"g = END u"
     with a  have "(i,j) \<in>  e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>"
     using BEGIN_END_before wdi wdj by auto
     moreover from cej geu c wdj wdu  have "(j,u) \<in> b \<union> m \<union> ov \<union> s \<union> d"
     using END_END_before by auto
     ultimately have "(i,u) \<in> b \<union> m \<union> ov \<union> s \<union> d \<union> f^-1 \<union> d^-1 \<union> ov^-1 \<union> s\<inverse> \<union> f \<union> e"
     using ebmovovissiffiddibmovsd by blast

     thus "BEGIN i \<lless> END u" using BEGIN_END_before wdi wdu  by auto
    next
     assume aei:"a = END i" and cbj:"c = BEGIN j" and gbu:"g = BEGIN u"
     from a  aei cbj wdi wdj have "(i,j) \<in> b"
     using END_BEGIN_before by auto
     moreover from c  cbj gbu wdj wdu have "(j,u) \<in> b \<union> m \<union> ov \<union> f\<inverse> \<union> d\<inverse>"
     using BEGIN_before by auto
     ultimately have "(i,u) : b" using cbb cbm cbov cbfi cbdi
     by (simp_all, elim disjE, auto)
     thus "END i \<lless> BEGIN u" using END_BEGIN_before wdi wdu  by auto
    next
     assume  aei:"a = END i" and cbj:"c = BEGIN j" and  geu:"g = END u"
     from a  aei cbj wdi wdj have "(i,j) \<in> b"
     using END_BEGIN_before by auto
     moreover from c  cbj geu wdj wdu have "(j,u) \<in> e \<union> b \<union> m \<union> ov \<union> ov\<inverse> \<union> s \<union> s\<inverse> \<union> f \<union> f\<inverse> \<union> d \<union> d\<inverse>"
     using BEGIN_END_before by auto
     ultimately have "(i,u) \<in> b \<union> m \<union> ov \<union> s \<union> d"
     using bebmovovissiffiddi by blast
     thus "END i \<lless> END u" using END_END_before wdi wdu by auto
    next
     assume aei:"a = END i" and cej:"c = END j" and  gbu:"g = BEGIN u"
     from aei cej wdi wdj have "(i,j) \<in> b \<union> m \<union> ov \<union> s \<union> d" using END_END_before a by auto
     moreover from cej gbu c wdj wdu have "(j,u) \<in> b" using END_BEGIN_before by auto
     ultimately have "(i,u) \<in> b"
     using cbb cmb covb csb cdb
     by (simp_all, elim disjE, auto)
     thus "END i \<lless> BEGIN u" using END_BEGIN_before wdi wdu  by auto
    next
     assume aei:"a = END i" and cej:"c = END j" and geu:"g = END u"
     from aei cej wdi wdj  have "(i,j) \<in> b \<union> m \<union> ov \<union> s \<union> d" using  END_END_before a by auto
     moreover from cej geu c wdj wdu have "(j,u) \<in>  b \<union> m \<union> ov \<union> s \<union> d" using END_END_before by auto
     ultimately have "(i,u) \<in> b \<union> m \<union> ov \<union> s \<union> d"
     using calpha1_alpha1 by auto
     thus "END i \<lless> END u" using END_END_before wdi wdu by auto
   qed
qed} note trans_nest = this

{ fix a c::"'a nest"
  assume a:"a \<lless> c"
  show "\<not> c \<lless> a"
  apply (rule ccontr, auto)
  using a  irrefl_nest trans_nest by blast}
qed

end