Theory Spec_Framework

(*TODO: it is possible and would be good if we separate the entire system into a pure BI logic with reasoning,
  and that applied over a semantic framework together with IDE-CP, but I have no time now.
*)

chapter โ€นSpecification Frameworkโ€บ

theory Spec_Framework
  imports Phi_BI.Phi_BI "Phi_Semantics_Framework.Phi_Semantics_Framework"
  keywords "fiction_space"  :: thy_goal
  abbrevs "<shifts>" = "๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ"
    and   "<val>" = "๐—๐–บ๐—…"
    and   "<vals>" = "๐—๐–บ๐—…๐—Œ"
    and   "<typeof>" = "๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ"
begin

subsubsection โ€นConfigurationโ€บ

declare [[ฯ†reason_default_pattern โ€นValid_Proc ?Fโ€บ โ‡’ โ€นValid_Proc ?Fโ€บ (100)]]
declare Valid_Proc_bind[ฯ†reason 1200]
declare ฯ†arg.simps[ฯ†safe_simp] ฯ†arg.sel[ฯ†safe_simp]

optional_translations (do_notation)
  "_do_then t" <= "_do_bind (_constrain _idtdummy TY) t"
  "_do_cons A (_do_cons B C)" <= "_do_cons (_do_cons A B) C"

syntax "_ฯ†V3" :: โ€นlogic โ‡’ logicโ€บ ("_'(3')")
       "_ฯ†V4" :: โ€นlogic โ‡’ logicโ€บ ("_'(4')")
       "_ฯ†V5" :: โ€นlogic โ‡’ logicโ€บ ("_'(5')")
       "_ฯ†V6" :: โ€นlogic โ‡’ logicโ€บ ("_'(6')")
       "_ฯ†V7" :: โ€นlogic โ‡’ logicโ€บ ("_'(7')")

optional_translations (do_notation)
  "x(2)" <= "x(2)(1)"
  "x(3)" <= "x(2)(2)"
  "x(4)" <= "x(2)(3)"
  "x(5)" <= "x(2)(4)"
  "x(6)" <= "x(2)(5)"
  "x(7)" <= "x(2)(6)"

  "x" <= "CONST ฯ†arg x"

print_translation โ€น[
  (const_syntaxโ€นbind_doโ€บ, fn _ => (
    fn (*[A, Abs B] =>
          Const(const_syntaxโ€นbind_doโ€บ, dummyT)
            $ A
            $ (case Syntax_Trans.atomic_abs_tr' B
                 of (f,x) => Const(syntax_constโ€น_absโ€บ, dummyT) $ f $ x)
     |*) [A, Const ("_abs", _) $ _ $ _] => raise Match
     | [_, Abs _] => raise Match
     | [A, B] =>
          Const(const_syntaxโ€นbind_doโ€บ, dummyT)
            $ A
            $ (case Syntax_Trans.atomic_abs_tr' ("_", dummyT, Term.incr_boundvars 1 B $ Bound 0)
                 of (f,x) => Const(syntax_constโ€น_absโ€บ, dummyT) $ f $ x) ))
]โ€บ

(*
term โ€นฯ†V_fst xโ€บ
term โ€นฯ†V_snd xโ€บ
term โ€นฮปx. ฯ†V_fst (ฯ†V_snd x)โ€บ
term โ€นฮปx. (ฯ†V_snd (ฯ†V_snd x))โ€บ
term โ€นฮปx. ฯ†V_fst (ฯ†V_snd (ฯ†V_snd x))โ€บ
term โ€นฮปx. ฯ†V_snd (ฯ†V_snd (ฯ†V_snd x))โ€บ
term โ€นฮปx. ฯ†V_fst (ฯ†V_snd (ฯ†V_snd (ฯ†V_snd x)))โ€บ
term โ€นฮปx. ฯ†V_snd (ฯ†V_snd (ฯ†V_snd (ฯ†V_snd x)))โ€บ
term โ€นฮปx. ฯ†V_snd (ฯ†V_fst (ฯ†V_snd (ฯ†V_snd x)))โ€บ
term โ€นฮปx. ฯ†V_snd (ฯ†V_snd (ฯ†V_snd (ฯ†V_snd (ฯ†V_snd x))))โ€บ
*)

section โ€นFictionโ€บ

unspecified_type FIC
unspecified_type FIC_N

type_synonym fiction = โ€นFIC_N โ‡’ FICโ€บ
type_synonym 'a assertion = โ€น'a BIโ€บ
type_synonym assn = โ€นfiction BIโ€บ
type_synonym rassn = โ€นresource BIโ€บ
type_synonym 'T fiction_entry = "(FIC_N, FIC, 'T) Resource_Space.kind"

setup โ€นSign.mandatory_path "FIC"โ€บ

consts DOMAIN :: โ€นFIC_N โ‡’ FIC sep_homo_setโ€บ

debt_axiomatization sort: โ€นOFCLASS(FIC, sep_algebra_class)โ€บ

setup โ€นSign.parent_pathโ€บ

instance FIC :: sep_algebra using FIC.sort .

instantiation FIC :: sep_carrier_1 begin
definition mul_carrier_FIC :: โ€นFIC โ‡’ boolโ€บ where โ€นmul_carrier_FIC = (ฮป_. True)โ€บ
  โ€• โ€นAs a type specially defined to represent the representation of fictions, it can be noisy-free.โ€บ
instance by (standard; simp add: mul_carrier_FIC_def)
end

consts INTERPRET :: โ€นFIC_N โ‡’ (FIC, resource) unital_homo_interpโ€บ

interpretation FIC: fictional_space FIC.DOMAIN INTERPRET .

definition "INTERP_RES fic โ‰ก BI_lift RES.SPACE โŠ“ FIC.INTERP fic ๐—Œ๐—Ž๐–ป๐—ƒ fic โˆˆ FIC.SPACE"
  โ€• โ€นInterpret a fictionโ€บ

lemma In_INTERP_RES:
  โ€นr โŠจ INTERP_RES fic โŸท r โˆˆ RES.SPACE โˆง fic โˆˆ FIC.SPACE โˆง r โŠจ FIC.INTERP ficโ€บ
  unfolding INTERP_RES_def by simp blast

definition INTERP_SPEC :: โ€นassn โ‡’ rassnโ€บ
  โ€• โ€นInterpret a fictional specificationโ€บ
  where "INTERP_SPEC T = BI_Monad_Comb (INTERP_RES `I T)"

lemma INTERP_SPEC:
  โ€นres โŠจ INTERP_SPEC T โŸท (โˆƒfic. fic โŠจ T โˆง res โŠจ INTERP_RES fic)โ€บ
  unfolding INTERP_SPEC_def
  by simp blast

lemma INTERP_SPEC_sub[intro, simp]: โ€นA โ‰ค B โŸน INTERP_SPEC A โ‰ค INTERP_SPEC Bโ€บ
  unfolding INTERP_SPEC_def less_eq_BI_iff by simp blast

lemma INTERP_SPEC_plus[iff]:
  โ€นINTERP_SPEC (A + B) = INTERP_SPEC A + INTERP_SPEC Bโ€บ
  unfolding BI_eq_iff
  by (simp add: INTERP_SPEC) blast

lemma INTERP_SPEC_0[iff]:
  โ€น INTERP_SPEC 0 = 0 โ€บ
  unfolding BI_eq_iff
  by (simp add: INTERP_SPEC)

(*
lemma INTERP_SPEC_empty[intro, simp]:
  โ€นS = {} โŸน INTERP_SPEC S = {}โ€บ
  unfolding INTERP_SPEC_def set_eq_iff by simp

lemma INTERP_SPEC_0[simp]:
  โ€นINTERP_SPEC 0  = 0โ€บ
  โ€นINTERP_SPEC {} = {}โ€บ
  unfolding INTERP_SPEC_def zero_set_def by simp+
*)

ML_file โ€นlibrary/spec_framework/fiction_space.MLโ€บ
ML_file โ€นlibrary/spec_framework/fiction_space_more.MLโ€บ

ML โ€นFiction_Space.define_command command_keywordโ€นfiction_spaceโ€บ "extend fictions"โ€บ

(*
lemma INTERP_mult:
  โ€น Fic_Space f1
โŸน Fic_Space f2
โŸน dom1 r1 โˆฉ dom1 r2 = {}
โŸน dom1 f1 โˆฉ dom1 f2 = {}
โŸน r1 โˆˆ โ„ INTERP f1
โŸน r2 โˆˆ โ„ INTERP f2
โŸน f1 ## f2
โŸน r1 * r2 โˆˆ โ„ INTERP (f1 * f2) โˆง r1 ## r2โ€บ
  unfolding INTERP_def Fic_Space_def
  by (simp add: dom1_sep_mult_disjoint times_fun prod.union_disjoint
                disjoint_dom1_eq_1[of f1 f2],
      meson dom1_disjoint_sep_disj times_set_I) *)


section โ€นSpecification of Valueโ€บ

type_synonym value_assertion = โ€นVAL BIโ€บ

subsection โ€นPrimitive ฯ†-Typesโ€บ

subsubsection โ€นValueโ€บ

definition Val :: โ€นVAL ฯ†arg โ‡’ (VAL, 'a) ฯ† โ‡’ ('x::one, 'a) ฯ†โ€บ ("๐—๐–บ๐—…[_] _" [22,22] 21)
  where โ€นVal val T = (ฮปx. 1 ๐—Œ๐—Ž๐–ป๐—ƒ ฯ†arg.dest val โŠจ (x โฆ‚ T))โ€บ

definition Vals :: โ€นVAL list ฯ†arg โ‡’ (VAL list, 'a) ฯ† โ‡’ ('x::one, 'a) ฯ†โ€บ ("๐—๐–บ๐—…๐—Œ[_] _" [22,22] 21)
  where โ€นVals vals T = (ฮปx. 1 ๐—Œ๐—Ž๐–ป๐—ƒ ฯ†arg.dest vals โŠจ (x โฆ‚ T))โ€บ

lemma Val_expn [simp, ฯ†expns]:
  โ€นv โŠจ (x โฆ‚ Val val T) โŸท v = 1 โˆง ฯ†arg.dest val โŠจ (x โฆ‚ T)โ€บ
  unfolding Val_def ฯ†Type_def by simp

lemma Vals_expn [simp, ฯ†expns]:
  โ€นv โŠจ (x โฆ‚ Vals val T) โŸท v = 1 โˆง ฯ†arg.dest val โŠจ (x โฆ‚ T)โ€บ
  unfolding Vals_def ฯ†Type_def by simp

lemma Val_inh_rewr:
  โ€นSatisfiable (x โฆ‚ Val val T) โ‰ก ฯ†arg.dest val โŠจ (x โฆ‚ T)โ€บ
  unfolding Satisfiable_def by clarsimp

lemma Vals_inh_rewr:
  โ€นSatisfiable (x โฆ‚ Vals val T) โ‰ก ฯ†arg.dest val โŠจ (x โฆ‚ T)โ€บ
  unfolding Satisfiable_def by clarsimp


paragraph โ€นSyntaxโ€บ

consts anonymous :: 'a

syntax val_syntax  :: "logic โ‡’ logic" ("๐—๐–บ๐—… _"  [22] 21)
       vals_syntax :: "logic โ‡’ logic" ("๐—๐–บ๐—…๐—Œ _" [22] 21)

translations
  "๐—๐–บ๐—… x โฆ‚ T" => "x โฆ‚ CONST Val (CONST anonymous) T"
  "๐—๐–บ๐—… T" => "CONST Val (CONST anonymous) T"
  "๐—๐–บ๐—…๐—Œ x โฆ‚ T" => "x โฆ‚ CONST Vals (CONST anonymous) T"
  "๐—๐–บ๐—…๐—Œ T" => "CONST Vals (CONST anonymous) T"

ML_file โ€นlibrary/syntax/value.MLโ€บ


subsubsection โ€นAbnormalโ€บ

definition AbnormalObj :: โ€นVAL ฯ†arg โ‡’ (VAL, 'a) ฯ† โ‡’ ('x::one, 'a) ฯ†โ€บ
  where โ€นAbnormalObj โ‰ก Valโ€บ

(*TODO: more about exception

Any exception object will be specified by this type. and by this we clarify the difference
between a value and an exception object.
Then in exception specs, any Val is senseless and will be removed.*)


subsection โ€นSemantic Typeโ€บ

consts Type_Of_syntax :: โ€น'a โ‡’ TYโ€บ ("๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ")

definition Semantic_Type :: โ€น(VAL, 'x) ฯ† โ‡’ TY โ‡’ boolโ€บ
  where โ€นSemantic_Type T TY โ‰ก (โˆ€x v. v โŠจ (x โฆ‚ T) โŸถ v โˆˆ Well_Type TY) โ€บ

definition Semantic_Type' :: โ€นVAL BI โ‡’ TY โ‡’ boolโ€บ
  where โ€นSemantic_Type' A TY โ‰ก (โˆ€v. v โŠจ A โŸถ v โˆˆ Well_Type TY) โ€บ

definition SType_Of :: โ€น(VAL, 'x) ฯ† โ‡’ TYโ€บ
  where โ€นSType_Of T = (
      if Inhabited T โˆง (โˆƒTY. Semantic_Type T TY)
      then (@TY. Semantic_Type T TY)
      else ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ )โ€บ

definition SType_Of' :: โ€นVAL BI โ‡’ TYโ€บ
  where โ€นSType_Of' A = (
      if Satisfiable A โˆง (โˆƒTY. Semantic_Type' A TY)
      then (@TY. Semantic_Type' A TY)
      else ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ )โ€บ

adhoc_overloading Type_Of_syntax SType_Of SType_Of'

lemma SType_Of'_implies_SType_Of:
  โ€น (โ‹€x. ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (x โฆ‚ T) = TY)
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T = TYโ€บ
  unfolding SType_Of_def SType_Of'_def Inhabited_def Semantic_Type_def Semantic_Type'_def
  by (auto, smt (verit) Satisfiable_def Well_Type_unique tfl_some,
            smt (verit, best) tfl_some)

lemma SType_Of'_implies_SType_Of''':
  โ€น Abstract_Domain T D
โŸน Abstract_DomainL T DL
โŸน (โ‹€x. D x โˆจ (โˆ€y. ยฌ DL y) โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (x โฆ‚ T) = TY)
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T = TYโ€บ
  unfolding SType_Of_def SType_Of'_def Inhabited_def Abstract_DomainL_def
            Action_Tag_def ๐—‹ESC_def ๐—‹EIF_def Abstract_Domain_def Satisfiable_def
            Semantic_Type_def Semantic_Type'_def
  by (auto,
      smt (verit, ccfv_SIG) Well_Type_unique someI,
      smt (verit, ccfv_SIG) someI)

lemma SType_Of_not_poison:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T = TY โˆง TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โŸท Inhabited T โˆง (โˆ€x v. v โŠจ (x โฆ‚ T) โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding SType_Of_def Inhabited_def Satisfiable_def
            Semantic_Type_def Semantic_Type'_def
  by (auto, smt (verit, best) someI2_ex, insert Well_Type_disjoint, blast)

lemma SType_Of'_not_poison:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ A = TY โˆง TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โŸท Satisfiable A โˆง (โˆ€v. v โŠจ A โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding SType_Of'_def Satisfiable_def Semantic_Type_def Semantic_Type'_def
  by (auto, smt (verit, best) someI2_ex, insert Well_Type_disjoint, blast)


(*
subsubsection โ€นSingle Valueโ€บ

definition Semantic_Type :: โ€น(VAL,'x) ฯ† โ‡’ TY โ‡’ boolโ€บ
  where โ€นSemantic_Type T TY โŸท (โˆ€x v. v โŠจ (x โฆ‚ T) โŸถ v โˆˆ Well_Type TY)โ€บ

definition Semantic_Type' :: "value_assertion โ‡’ TY โ‡’ bool"
  where โ€นSemantic_Type' S TY โŸท (โˆ€v. v โŠจ S โŸถ v โˆˆ Well_Type TY)โ€บ
  โ€• โ€นValues specified by โ€นSโ€บ are all of semantic type โ€นTYโ€บ.โ€บ

(*definition ฯ†ฯ†SemType :: "(VAL, 'a) ฯ† โ‡’ ('a โ‡’ bool) โ‡’ ('a โ‡’ TY) โ‡’ bool"
  where โ€นฯ†ฯ†SemType T D TY โ‰ก (โˆ€x. D x โŸถ ฯ†SemType (x โฆ‚ T) (TY x))โ€บ*)

declare [[
  ฯ†reason_default_pattern โ€นSemantic_Type ?T _โ€บ  โ‡’ โ€นSemantic_Type ?T _โ€บ  (100)
                      and โ€นSemantic_Type' ?A _โ€บ โ‡’ โ€นSemantic_Type' ?A _โ€บ (100)
]]
*)

ฯ†reasoner_group ฯ†sem_type_infer_all = (100, [1, 2000]) for (โ€นSType_Of T = _ @tag ๐’œinferโ€บ, โ€นSType_Of' T = _ @tag ๐’œinferโ€บ )โ€นโ€บ
            and ฯ†sem_type_infer_fallback = (1, [1, 1]) in ฯ†sem_type_infer_all โ€นโ€บ
            and ฯ†sem_type_infer_brute = (10, [10,20]) in ฯ†sem_type_infer_all
                                                     and > ฯ†sem_type_infer_fallback โ€นโ€บ
            and ฯ†sem_type_infer_cut = (1000, [1000, 1030]) in ฯ†sem_type_infer_all โ€นโ€บ
            and ฯ†sem_type_infer_derived = (50, [50,60]) in ฯ†sem_type_infer_all
                                                       and > ฯ†sem_type_infer_brute โ€นโ€บ

ฯ†reasoner_group Semantic_Type_all = (100, [10, 2000]) for (โ€นSemantic_Type _ _โ€บ, โ€นSemantic_Type' _ _โ€บ) โ€นโ€บ
  and Semantic_Type = (1000, [1000,1030]) in Semantic_Type_all โ€นโ€บ
  and Semantic_Type_default = (50, [30,80]) in Semantic_Type_all โ€นโ€บ
  and Semantic_Type_fallback = (15, [10,20]) in Semantic_Type_all and < Semantic_Type_default โ€นโ€บ

declare [[
    ฯ†reason_default_pattern โ€นSemantic_Type ?T _โ€บ โ‡’ โ€นSemantic_Type ?T ?varโ€บ (100)
                        and โ€นSemantic_Type' ?T _โ€บ โ‡’ โ€นSemantic_Type' ?T ?varโ€บ (100),
    ฯ†default_reasoner_group โ€นSemantic_Type _ _โ€บ : %Semantic_Type (100),
    ฯ†default_reasoner_group โ€นSemantic_Type' _ _โ€บ : %Semantic_Type (100),

    ฯ†reason_default_pattern โ€นSType_Of  ?T = _ @tag ๐’œinferโ€บ โ‡’ โ€นSType_Of  ?T = _ @tag ๐’œinferโ€บ (100)
                        and โ€นSType_Of' ?T = _ @tag ๐’œinferโ€บ โ‡’ โ€นSType_Of' ?T = _ @tag ๐’œinferโ€บ (100),
    ฯ†default_reasoner_group โ€นSType_Of  ?T = _ @tag ๐’œinferโ€บ : %ฯ†sem_type_infer_cut (100),
    ฯ†default_reasoner_group โ€นSType_Of' ?T = _ @tag ๐’œinferโ€บ : %ฯ†sem_type_infer_cut (100)
]]


(*
ฯ†reasoner_group ฯ†sem_type = (100, [0, 3000]) for (โ€นSType_Of T = TY @tag ๐’œinferโ€บ)
      โ€นgiving the semantic type of the concrete value satisfying the given assertion or ฯ†-typeโ€บ
  and ฯ†sem_type_fail = (0, [0,0]) in ฯ†sem_type
      โ€นfailuresโ€บ
  and ฯ†sem_type_brute = (2, [2,2]) in ฯ†sem_type > ฯ†sem_type_fail
      โ€นreducing to concrete level, used only when deriving rules.โ€บ
  and ฯ†sem_type_failback = (3, [3,3]) in ฯ†sem_type > ฯ†sem_type_brute
      โ€นsystem usageโ€บ
  and ฯ†sem_type_assertion = (10, [10,10]) in ฯ†sem_type and > ฯ†sem_type_failback
      โ€นasserting the given with a semantic type if anyโ€บ
(*and ฯ†sem_type_ฯ†typ = (10, [10,10]) in ฯ†sem_type and > ฯ†sem_type_fail
      โ€นโ€นฯ†SemTypeโ€บ -> โ€นฯ†ฯ†SemTypeโ€บโ€บ*)
  and ฯ†sem_type_derived = (50, [50,50]) in ฯ†sem_type and > ฯ†sem_type_assertion
      โ€นderived rulesโ€บ
  and ฯ†sem_type_cut = (1000, [1000,1030]) in ฯ†sem_type and > ฯ†sem_type_derived
      โ€นcutting rulesโ€บ
  and ฯ†sem_type_red = (2200, [2200,2500]) in ฯ†sem_type and > ฯ†sem_type_cut
      โ€นreduction and evaluationโ€บ

ฯ†reasoner_group sem_typ_chk = (80, [80,90]) > lambda_unify_all โ€นโ€บ
  *)

(*lemma ฯ†SemType_unique:
  โ€น S โ‰  {}
โŸน ฯ†SemType S T1
โŸน ฯ†SemType S T2
โŸน T1 = T2โ€บ
  unfolding ฯ†SemType_def subset_iff
  using Well_Type_unique by blast *)

(* definition SemTyp_Of :: โ€นVAL set โ‡’ TYโ€บ
  where โ€นSemTyp_Of S = (@TY. ฯ†SemType S TY)โ€บ

lemma SemTyp_Of_I[intro!, simp]:
  โ€นS โ‰  {} โŸน ฯ†SemType S TY โŸน SemTyp_Of S = TYโ€บ
  unfolding SemTyp_Of_def
  using ฯ†SemType_unique by blast *)

(*

lemma [ฯ†reason 100]:
  โ€น (โ‹€x. ฯ†SemType (x โฆ‚ T) TY)
โŸน ฯ†ฯ†SemType T TYโ€บ
  .. *)

paragraph โ€นBasic Rulesโ€บ

lemma [ฯ†reason %ฯ†sem_type_infer_fallback]:
  โ€น ๐—Œ๐—‚๐—†๐—‰๐—…๐—‚๐–ฟ๐—’ TY : ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ A
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ A = TY @tag ๐’œinfer โ€บ
  for A :: โ€นVAL BIโ€บ
  unfolding Action_Tag_def Simplify_def
  by blast

lemma ฯ†SemType_Itself_brute:
  โ€น ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ v โˆˆ Well_Type TY
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (v โฆ‚ Itself) = TY @tag ๐’œinfer โ€บ
  unfolding SType_Of'_def Inhabited_def Satisfiable_def Premise_def Action_Tag_def
            Semantic_Type_def Semantic_Type'_def
  by (auto, insert Well_Type_unique, blast)

lemma ฯ†sem_type_by_sat:
  โ€น ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ ((โˆ€v. v โŠจ S โŸถ v โˆˆ Well_Type TY) โˆง (ยฌ Satisfiable S โŸถ TY = ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡))
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ S = TY @tag ๐’œinfer โ€บ
  unfolding Premise_def ๐—‹Guard_def SType_Of'_def Satisfiable_def Action_Tag_def
            Semantic_Type_def Semantic_Type'_def
  by (auto simp: split_ifs, insert Well_Type_unique, blast)
  

(*
lemma ฯ†sem_type_brute_EIF':
  โ€น ๐—‹EIF (Semantic_Type' S TY) (โˆ€v. v โŠจ S โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding ๐—‹EIF_def Semantic_Type'_def
  by blast

lemma ฯ†sem_type_brute_EIF:
  โ€น ๐—‹EIF (Semantic_Type T TY) (โˆ€x v. v โŠจ (x โฆ‚ T) โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding ๐—‹EIF_def Semantic_Type_def
  by blast
*)

bundle ฯ†sem_type_sat_EIF = ฯ†sem_type_by_sat[ฯ†reason default %ฯ†sem_type_infer_brute]
                        (* ฯ†sem_type_brute_EIF[ฯ†reason %extract_pure]
                           ฯ†sem_type_brute_EIF'[ฯ†reason %extract_pure]  *)
                           ฯ†SemType_Itself_brute[ฯ†reason %ฯ†sem_type_infer_cut]

(*
lemma [ฯ†reason default %ฯ†sem_type_failback]:
  โ€น ๐—€๐—Ž๐–บ๐—‹๐–ฝ Semantic_Type T TY
โŸน Semantic_Type' (x โฆ‚ T) TY โ€บ
  unfolding Semantic_Type'_def Semantic_Type_def ๐—‹Guard_def
  by simp
*)

paragraph โ€นOver Logic Connectivesโ€บ

lemma ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ_plus[simp]:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T = ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ U
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (T + U) = ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T โ€บ
  for T :: โ€นVAL BIโ€บ
  unfolding SType_Of'_def Inhabited_def Satisfiable_def subset_iff Semantic_Type_def Semantic_Type'_def
  using Well_Type_unique by (clarsimp, smt (z3) someI)

lemma [ฯ†reason add]:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T = TY1 @tag ๐’œinfer
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ U = TY2 @tag ๐’œinfer
โŸน ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ TY1 = TY2
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (T + U) = TY1 @tag ๐’œinfer โ€บ
  for T :: โ€นVAL BIโ€บ
  unfolding Action_Tag_def Premise_def
  by simp

lemma ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ_bot[simp]:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ โŠฅBI = ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โ€บ
  unfolding SType_Of'_def
  by auto

lemma [ฯ†reason add]:
  โ€น ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ P โŸถ ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ X = TY @tag ๐’œinfer
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (X ๐—Œ๐—Ž๐–ป๐—ƒ P) = (if P then TY else ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡) @tag ๐’œinfer โ€บ
  unfolding Action_Tag_def Premise_def
  by auto

lemma ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ_๐—Œ๐—Ž๐–ป๐—ƒ[simp]:
  โ€น ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (X ๐—Œ๐—Ž๐–ป๐—ƒ P) = (if P then ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ X else ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡) โ€บ
  by auto

lemma [ฯ†reason add]:
  โ€น (โ‹€x. ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (X x) = TY @tag ๐’œinfer)
โŸน ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (ExBI X) = TY @tag ๐’œinfer โ€บ
  unfolding Action_Tag_def SType_Of'_def Inhabited_def Satisfiable_def subset_iff            
  by (auto,
      metis (no_types, lifting) ExBI_expn Semantic_Type'_def Well_Type_unique verit_sko_ex',
      metis ExBI_expn Satisfaction_def Semantic_Type'_def tfl_some)

subsubsection โ€นAuxiliary Reasonersโ€บ

paragraph โ€นCheck Type Literalโ€บ

definition โ€นIs_Type_Literal (TY::TY) โ‰ก Trueโ€บ

ฯ†reasoner_group Is_Type_Literal = (1000, [10, 2000]) for โ€นIs_Type_Literal TYโ€บ โ€นโ€บ
    and Is_Type_Literal_default = (10, [10, 20]) in Is_Type_Literal โ€นโ€บ

declare [[ ฯ†default_reasoner_group โ€นIs_Type_Literal _โ€บ : %Is_Type_Literal (100) ]]

lemma Is_Type_Literal_I[intro!]: โ€นIs_Type_Literal Xโ€บ
  unfolding Is_Type_Literal_def ..

ฯ†reasoner_ML Is_Type_Literal_Free default %Is_Type_Literal_default (โ€นIs_Type_Literal _โ€บ) = โ€น
  fn (_,(ctxt,sequent)) =>
    case Thm.major_prem_of sequent
      of Trueprop $ (Const(const_nameโ€นIs_Type_Literalโ€บ, _) $ (Const(const_nameโ€นSType_Ofโ€บ, _) $ Free _)) =>
          Seq.single (ctxt, @{thm' Is_Type_Literal_I} RS sequent)
       | _ => Seq.empty
โ€บ

paragraph โ€นUnfolding โ€น๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ Tโ€บโ€บ

ฯ†reasoner_group eval_sem_typ = (100, [75, 2000]) > lambda_unify__default
                                โ€นUnfolding โ€น๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ Tโ€บ exhausitively, with checking the result is not a poison.โ€บ

(*
lemma Semantic_Type_alt_def:
  โ€น Semantic_Type T TY โŸท Inhabited T โˆง (โˆ€x v. v โŠจ (x โฆ‚ T) โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding Semantic_Type_def
  by (smt (verit, del_insts) SType_Of_not_poison Bot_Satisfiable Inhabited_def Int_iff SType_Of_def
      Satisfaction_def Well_Type_disjoint ฯ†Bot.expansion ฯ†Bot.unfold ฯ†Type_eqI bot_eq_BI_bot someI) 

lemma Semantic_Type'_alt_def:
  โ€น Semantic_Type' A TY โŸท Satisfiable A โˆง (โˆ€v. v โŠจ A โŸถ v โˆˆ Well_Type TY) โ€บ
  unfolding Semantic_Type'_def
  using SType_Of'_not_poison
  by (smt (verit, best) Action_Tag_def Premise_def ฯ†sem_type_by_sat)
*)



(*
paragraph โ€นConversion From Strong to Weakโ€บ

(* ML_file โ€นlibrary/spec_framework/semantic_type.MLโ€บ *)

*) 

subsubsection โ€นReasoningโ€บ

lemma [ฯ†reason default %Semantic_Type_fallback+5]:
  โ€น Abstract_Domain T D
โŸน (๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ Ex D โŸน ๐—Œ๐—‚๐—†๐—‰๐—…๐—‚๐–ฟ๐—’ TY : ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T)
โ€• โ€นIs_Type_Literal TY ๐—ˆ๐—‹ ๐–ฟ๐–บ๐—‚๐—… TEXT(โ€นFail to evaluateโ€บ (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T))โ€บ
โŸน ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ (Ex D โŸถ TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡) ๐—ˆ๐—‹ ๐–ฟ๐–บ๐—‚๐—… TEXT(โ€นFail to evaluateโ€บ (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T) โ€น: fail to showโ€บ (TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡))
โŸน Semantic_Type T TY โ€บ
  unfolding Semantic_Type_def Simplify_def Premise_def OR_FAIL_def Abstract_Domain_def Premise_def
  by (auto simp add: Satisfiable_def ๐—‹EIF_def, metis SType_Of_not_poison)

lemma [ฯ†reason default %Semantic_Type_fallback for โ€นSemantic_Type _ _โ€บ
                                            except โ€นSemantic_Type _ ?varโ€บ]:
  โ€น Semantic_Type T TY'
โŸน ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ TY = TY' ๐—ˆ๐—‹ ๐–ฟ๐–บ๐—‚๐—… TEXT(โ€นExpectingโ€บ (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T) โ€นto beโ€บ TY โ€นbut actuallyโ€บ TY')
โŸน Semantic_Type T TY โ€บ
  unfolding OR_FAIL_def Premise_def
  by simp


lemma [ฯ†reason default %Semantic_Type_fallback+5]:
  โ€น Semantic_Type T TY
โŸน Semantic_Type' (x โฆ‚ T) TY โ€บ
  unfolding Semantic_Type'_def Semantic_Type_def
  by auto

lemma [ฯ†reason default %Semantic_Type_fallback for โ€นSemantic_Type' _ _โ€บ
                                            except โ€นSemantic_Type' _ ?varโ€บ]:
  โ€น Semantic_Type' A TY'
โŸน ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ TY = TY' ๐—ˆ๐—‹ ๐–ฟ๐–บ๐—‚๐—… TEXT(โ€นExpectingโ€บ (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ A) โ€นto beโ€บ TY โ€นbut actuallyโ€บ TY')
โŸน Semantic_Type' A TY โ€บ
  unfolding OR_FAIL_def Premise_def
  by simp

lemma [ฯ†reason default %Semantic_Type_default]:
  โ€น ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ (v โˆˆ Well_Type TY)
โŸน Semantic_Type' (v โฆ‚ Itself) TY โ€บ
  unfolding Premise_def Semantic_Type'_def
  by auto

lemma [ฯ†reason add]:
  โ€น (๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ P โŸน Semantic_Type' A TY)
โŸน Semantic_Type' (A ๐—Œ๐—Ž๐–ป๐—ƒ P) TY โ€บ
  unfolding Semantic_Type'_def
  by auto

lemma [ฯ†reason add]:
  โ€น (โ‹€x. Semantic_Type' (A x) TY)
โŸน Semantic_Type' (โˆƒ*x. A x) TY โ€บ
  unfolding Semantic_Type'_def
  by auto


subsubsection โ€นMultiple Valuesโ€บ

definition Well_Typed_Vals :: โ€นTY list โ‡’ 'a::VALs ฯ†arg setโ€บ
  where โ€นWell_Typed_Vals TYs = {vs. list_all2 (ฮปv T. v โˆˆ Well_Type T) (to_vals (ฯ†arg.dest vs)) TYs}โ€บ

definition Semantic_Types :: โ€น('a::VALs ฯ†arg โ‡’ assn) โ‡’ TY list โ‡’ boolโ€บ
  where โ€นSemantic_Types spec TYs = (โˆ€v. Satisfiable (spec v) โŸถ v โˆˆ Well_Typed_Vals TYs)โ€บ

definition โ€นSemantic_Types_i = Semantic_Typesโ€บ

declare [[
      ฯ†reason_default_pattern โ€นSemantic_Types_i ?S _โ€บ โ‡’ โ€นSemantic_Types_i ?S _โ€บ (100)
                          and โ€นSemantic_Types   ?S _โ€บ โ‡’ โ€นSemantic_Types   ?S _โ€บ (100),
      ฯ†default_reasoner_group โ€นSemantic_Types   _ _โ€บ : %ฯ†sem_type_infer_cut (100),
      ฯ†default_reasoner_group โ€นSemantic_Types_i _ _โ€บ : %ฯ†sem_type_infer_cut (100)
]]

lemma [ฯ†reason add]:
  โ€น (โ‹€v. S v ๐—‚๐—†๐—‰๐—…๐—‚๐–พ๐—Œ P v)
โŸน ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ (โˆƒx. P x) โŸถ Semantic_Types_i S TYs
โŸน Semantic_Types S TYs โ€บ
  unfolding Semantic_Types_i_def Semantic_Types_def Premise_def ๐—‹EIF_def
  by auto

(*
subsubsection โ€นSemantic Typeofโ€บ

definition SType_Of :: โ€น(VAL, 'x) ฯ† โ‡’ TYโ€บ
  where โ€นSType_Of T = (
      if Inhabited T
      then (@TY. Semantic_Type T TY)
      else ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡)โ€บ




lemma
  โ€น Semantic_Type T TY
โŸน Semantic_Type T (SType_Of T) โ€บ
  unfolding SType_Of_def
  apply auto
  apply (simp add: someI)
  by (meson Inhabited_def Satisfiable_I Semantic_Type_def)



lemma SType_Of_unfold:
  โ€น Semantic_Type T TY
โŸน Inhabited T
โŸน SType_Of T โ‰ก TY โ€บ
  unfolding Semantic_Type_def Inhabited_def SType_Of_def atomize_eq Satisfiable_def
  using Well_Type_unique
  by (clarsimp, smt (verit, del_insts) Satisfiable_def someI)

ML_file โ€นlibrary/tools/unfold_typeof.MLโ€บ
*)

(*
subsubsection โ€นGeneralized Semantic Typeof --- using Syntax Inference onlyโ€บ

definition Generalized_Semantic_Type :: โ€น'any โ‡’ TY โ‡’ boolโ€บ
  where โ€นGeneralized_Semantic_Type T TY โ‰ก Trueโ€บ
  โ€• โ€นmerely providing a syntactical inference that may help certain inferences like inferring
      the semantic type of a memory partial object as that used in the inference for โ€น๐—‰๐—ˆ๐—‚๐—‡๐—๐–พ๐—‹-๐—ˆ๐–ฟโ€บโ€บ

declare [[ ฯ†reason_default_pattern
    โ€นGeneralized_Semantic_Type ?T _โ€บ โ‡’ โ€นGeneralized_Semantic_Type ?T _โ€บ (100)
]]

ฯ†reasoner_group generalized_sematic_type = (100, [1,3000]) โ€นGeneralized_Semantic_Typeโ€บ
            and generalized_sematic_type_cut = (1000, [1000,1030]) in generalized_sematic_type โ€นcutโ€บ
            and generalized_sematic_type_fallback = (10, [10,10]) in generalized_sematic_type โ€นfallbackโ€บ

lemma [ฯ†reason default %generalized_sematic_type_fallback]:
  โ€น (SYNTACTIC_MODE โŸน Semantic_Type T TY)
โŸน Generalized_Semantic_Type T TYโ€บ
  unfolding Generalized_Semantic_Type_def ..

lemma Semantic_Type_by_Synt_Sugar:
  โ€น ๐—€๐—Ž๐–บ๐—‹๐–ฝ SYNTACTIC_MODE
โŸน Semantic_Type T (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T) โ€บ
  unfolding ๐—‹Guard_def SYNTACTIC_MODE_def by blast

bundle Semantic_Type_by_Synt_Sugar =
          Semantic_Type_by_Synt_Sugar[ฯ†reason default %Semantic_Type_fallback-5]
*)

subsection โ€นZero Valueโ€บ

definition Semantic_Zero_Val :: "TY โ‡’ (VAL,'a) ฯ† โ‡’ 'a โ‡’ bool"
  where "Semantic_Zero_Val ty T x โŸท (ty โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โŸถ (โˆƒv. Zero ty = Some v โˆง v โŠจ (x โฆ‚ T)))"

declare [[ฯ†reason_default_pattern โ€นSemantic_Zero_Val _ ?T _โ€บ โ‡’ โ€นSemantic_Zero_Val _ ?T ?varzโ€บ (100) ]]

ฯ†reasoner_group semantic_zero_val_all = (100, [0, 3000]) for โ€นSemantic_Zero_Val TY T xโ€บ
    โ€นgiving the semantic zero value on the abstraction sideโ€บ
  and semantic_zero_val_fail = (0, [0,0]) in semantic_zero_val_all
    โ€นfailureโ€บ
  and semantic_zero_val_fallback = (10, [1,20]) in semantic_zero_val_all and > semantic_zero_val_fail
    โ€นreducing to semantic level, only used in deriving rulesโ€บ
  and semantic_zero_val_cut = (1000, [1000, 1000]) in semantic_zero_val_all
    โ€นcutting rulesโ€บ
  and semantic_zero_val_derived = (50, [50,50]) in semantic_zero_val_all
                                               and < semantic_zero_val_cut
                                               and > semantic_zero_val_fallback
    โ€นderived rulesโ€บ

subsubsection โ€นBasic Rulesโ€บ

lemma [ฯ†reason default %semantic_zero_val_fail]:
  โ€น FAIL TEXT(โ€นDon't know any semantic zero value satisfying ฯ†-typeโ€บ T)
โŸน Semantic_Zero_Val TY T x โ€บ
  unfolding FAIL_def
  by blast

lemma [ฯ†reason %extract_pure]:
  โ€น Abstract_Domain T P
โŸน ๐—‹EIF (Semantic_Zero_Val TY T x) (TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โŸถ P x) โ€บ
  unfolding Abstract_Domain_def Semantic_Zero_Val_def ๐—‹EIF_def Satisfiable_def
  by blast

(*lemma Semantic_Zero_Val_brute:
  โ€น ๐—€๐—Ž๐–บ๐—‹๐–ฝ ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ (โˆƒv. Zero TY = Some v โˆง v โŠจ (x โฆ‚ T))
โŸน Semantic_Zero_Val TY T x โ€บ
  unfolding Semantic_Zero_Val_def ๐—‹Guard_def Premise_def
  by blast
*)

lemma Semantic_Zero_Val_EIF_sat:
  โ€น ๐—‹EIF (Semantic_Zero_Val TY T x) (TY โ‰  ๐—‰๐—ˆ๐—‚๐—Œ๐—ˆ๐—‡ โŸถ (โˆƒv. Zero TY = Some v โˆง v โŠจ (x โฆ‚ T))) โ€บ
  unfolding ๐—‹EIF_def Semantic_Zero_Val_def
  by blast

bundle Semantic_Zero_Val_EIF_brute = (*Semantic_Zero_Val_brute[ฯ†reason default %semantic_zero_val_brute]*)
                                     Semantic_Zero_Val_EIF_sat[ฯ†reason %extract_pure+10]

(*
lemma [ฯ†reason %semantic_zero_val_fallback for โ€นSemantic_Zero_Val _ _ _โ€บ
                                        except โ€นSemantic_Zero_Val ?var _ _โ€บ ]:
  โ€น Semantic_Zero_Val TY' U z
โŸน ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ TY = TY'
โŸน Semantic_Zero_Val TY  U z โ€บ
  unfolding Premise_def
  by simp
*)

lemma [ฯ†reason %semantic_zero_val_cut for โ€นSemantic_Zero_Val (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ (_ :: (VAL,_) ฯ†)) _ _โ€บ ]:
  โ€น ๐—€๐—Ž๐–บ๐—‹๐–ฝ ๐—Œ๐—‚๐—†๐—‰๐—…๐—‚๐–ฟ๐—’[๐–ผ๐—๐–บ๐—‡๐—€๐–พ๐–ฝ default] TY : ๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T
โŸน Semantic_Zero_Val TY U z
โŸน Semantic_Zero_Val (๐—๐—’๐—‰๐–พ๐—ˆ๐–ฟ T) U z โ€บ
  for T :: โ€น(VAL,'x) ฯ†โ€บ
  unfolding ๐—‹Guard_def Simplify_def
  by simp


subsection โ€นEqualityโ€บ

definition ฯ†Equal :: "(VAL,'a) ฯ† โ‡’ ('a โ‡’ 'a โ‡’ bool) โ‡’ ('a โ‡’ 'a โ‡’ bool) โ‡’ bool"
  where "ฯ†Equal T can_eq eq โŸท (โˆ€p1 p2 x1 x2 res.
    can_eq x1 x2 โˆง p1 โŠจ (x1 โฆ‚ T) โˆง p2 โŠจ (x2 โฆ‚ T)
      โŸถ Can_EqCompare res p1 p2 โˆง (EqCompare p1 p2 = eq x1 x2))"

declare [[ฯ†reason_default_pattern โ€นฯ†Equal ?TY ?can_eq ?eqโ€บ โ‡’ โ€นฯ†Equal ?TY _ _โ€บ (100) ]]


subsection โ€นFunctionalโ€บ

definition Is_Functional :: โ€น'a BI โ‡’ boolโ€บ
  where โ€นIs_Functional S โŸท (โˆ€x y. x โŠจ S โˆง y โŠจ S โŸถ x = y)โ€บ

definition Functionality :: โ€น('c,'a) ฯ† โ‡’ ('a โ‡’ bool) โ‡’ boolโ€บ
  where โ€นFunctionality T p โŸท (โˆ€x u v. p x โˆง u โŠจ (x โฆ‚ T) โˆง v โŠจ (x โฆ‚ T) โŸถ u = v)โ€บ
  โ€• โ€นA lower bound of the set in which ฯ†-type assertions are functionalโ€บ

declare [[ฯ†reason_default_pattern โ€นIs_Functional ?Sโ€บ โ‡’ โ€นIs_Functional ?Sโ€บ (100)
                              and โ€นFunctionality ?T _โ€บ โ‡’ โ€นFunctionality ?T _โ€บ (100)]]

ฯ†reasoner_group ฯ†functionality_all = (%cutting, [1,3000]) for (โ€นIs_Functional Sโ€บ, โ€นFunctionality T pโ€บ)
    โ€นAll reasoning rules giving functionality of ฯ†-types, meaning if the assertion uniquely fixes
     a concrete object for each abstract object.โ€บ
  and ฯ†functionality = (%cutting, [%cutting, %cutting+30]) in ฯ†functionality_all
    โ€นDefault rules are cuttingโ€บ
  and derived_ฯ†functionality = (50, [40,60]) in ฯ†functionality_all and < ฯ†functionality
    โ€นDerived rulesโ€บ
  and ฯ†functional_to_functionality = (10, [10,10]) in ฯ†functionality_all and < derived_ฯ†functionality
    โ€นTrunning โ€นIs_Functional (x : T)โ€บ to โ€นFunctionality T pโ€บโ€บ
  and ฯ†functionality_brute = (2, [2,2]) in ฯ†functionality_all and < ฯ†functional_to_functionality
    โ€นreducing to concrete semantics, and only used in deriving rulesโ€บ


subsubsection โ€นBasic Rulesโ€บ

lemma functional_concretize:
  โ€น Functionality T ((=) x)
โŸน v โŠจ (x โฆ‚ T)
โŸน v = concretize T x โ€บ
  unfolding concretize_def ฯ†Type_def Functionality_def
  by (simp add: some_equality)

lemma concretize_eq:
  โ€น Abstract_DomainL T ((=) x)
โŸน Functionality U ((=) y)
โŸน x โฆ‚ T ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ y โฆ‚ U
โŸน concretize T x = concretize U y โ€บ
  unfolding Transformation_def Abstract_DomainL_def ๐—‹ESC_def
  by (simp add: concretize_SAT functional_concretize)


(*deprecated*)
lemma Is_Functional_premise_extraction:
  โ€นIs_Functional S โ‰ก (โˆ€u v. u โŠจ S โˆง v โŠจ S โŸถ u = v) โˆง Trueโ€บ
  unfolding Is_Functional_def atomize_eq
  by blast

(*deprecated*)
lemma Functionality_premise_extraction:
  โ€นFunctionality T P โ‰ก (โˆ€x u v. P x โˆง u โŠจ (x โฆ‚ T) โˆง v โŠจ (x โฆ‚ T) โŸถ u = v) โˆง Trueโ€บ
  unfolding Functionality_def atomize_eq
  by blast

lemma Functionality_sub:
  โ€น (โˆ€x. P' x โŸถ P x)
โŸน Functionality T P
โŸน Functionality T P' โ€บ
  unfolding Functionality_def
  by auto

(* lemma Is_Functional_alt:
  โ€นIs_Functional S โŸท (S = {} โˆจ (โˆƒx. S = {x}))โ€บ
  unfolding Is_Functional_def by blast *)

lemma Is_Functional_I[intro!]:
  โ€น (โ‹€x y. x โŠจ A โŸน y โŠจ A โŸน x = y)
โŸน Is_Functional A โ€บ
  unfolding Is_Functional_def by blast

lemma Is_Functional_imp:
  โ€น S ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ S'
โŸน Is_Functional S'
โŸน Is_Functional Sโ€บ
  unfolding Transformation_def Is_Functional_def
  by blast

lemma [ฯ†reason no explorative backtrack 0]:
  โ€น TRACE_FAIL TEXT(โ€นFail to showโ€บ S โ€นis functionalโ€บ)
โŸน Is_Functional Sโ€บ
  unfolding TRACE_FAIL_def
  by blast

lemma [ฯ†reason default %ฯ†functional_to_functionality]:
  โ€น Functionality T p
โŸน ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ p x
โŸน Is_Functional (x โฆ‚ T)โ€บ
  unfolding Premise_def Is_Functional_def Functionality_def
  by simp


lemma Is_Functional_brute:
  โ€น ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ (โˆ€u v. u โŠจ S โˆง v โŠจ S โŸถ u = v)
โŸน Is_Functional S โ€บ
  unfolding Is_Functional_def Premise_def
  by blast

lemma Is_Functional_EIF_brute:
  โ€น ๐—‹EIF (Is_Functional S) (โˆ€u v. u โŠจ S โˆง v โŠจ S โŸถ u = v) โ€บ
  unfolding ๐—‹EIF_def Is_Functional_def
  by blast

lemma Functionality_EIF_brute:
  โ€น ๐—‹EIF (Functionality T D) (โˆ€x u v. D x โˆง u โŠจ (x โฆ‚ T) โˆง v โŠจ (x โฆ‚ T) โŸถ u = v) โ€บ
  unfolding ๐—‹EIF_def Functionality_def
  by blast

bundle Is_Functional_brute = Is_Functional_brute[ฯ†reason default %ฯ†functionality_brute]
                             Is_Functional_EIF_brute[ฯ†reason %extract_pure+10]
                             Functionality_EIF_brute[ฯ†reason %extract_pure+10]


subsubsection โ€นBasic Types and Connectivesโ€บ

lemma [ฯ†reason %ฯ†functionality]:
  โ€นFunctionality Itself (ฮป_. True)โ€บ
  unfolding Functionality_def
  by clarsimp

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Is_Functional 1 โ€บ
  unfolding Is_Functional_def
  by simp

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Is_Functional 0 โ€บ
  unfolding Is_Functional_def
  by simp

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Is_Functional A
โŸน Is_Functional B
โŸน Is_Functional (A โŠ“ B) โ€บ
  unfolding Is_Functional_def
  by simp

lemma [ฯ†reason %ฯ†functionality]:
  โ€น (๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ P โŸน Is_Functional S)
โŸน Is_Functional (S ๐—Œ๐—Ž๐–ป๐—ƒ P) โ€บ
  unfolding Is_Functional_def Premise_def
  by simp

(* โ€• โ€นderived automatically laterโ€บ
lemma [ฯ†reason %ฯ†functionality]:
  โ€น Functionality T pT
โŸน Functionality U pU
โŸน Functionality (T โˆ— U) (ฮป(x,y). pT x โˆง pU y)โ€บ
  unfolding Functionality_def
  by clarsimp blast
*)

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Is_Functional A
โŸน Is_Functional B
โŸน Is_Functional (A * B)โ€บ
  unfolding Is_Functional_def set_eq_iff
  by (simp add: set_mult_expn, blast)

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Is_Functional A
โŸน Is_Functional B
โŸน Is_Functional (A ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ B)โ€บ
  unfolding Is_Functional_def set_eq_iff REMAINS_def
  by (simp add: set_mult_expn, blast)

lemma [ฯ†reason %ฯ†functionality]:
  โ€น Functionality T pT
โŸน Functionality U pU
โŸน Functionality (T โœผ U) (ฮป(x,y). pT x โˆง pU y)โ€บ
  unfolding Functionality_def ฯ†Prod'_def
  by clarsimp blast

lemma [ฯ†reason %ฯ†functionality]:
  โ€น (โ‹€iโˆˆS. Is_Functional (A i))
โŸน Is_Functional (โœฑiโˆˆS. A i) โ€บ
  unfolding Is_Functional_def Mul_Quant_def atomize_Ball Ball_def
proof clarsimp
  fix x y
  assume prems: โ€นโˆ€x. x โˆˆ S โŸถ (โˆ€xa y. xa โŠจ A x โˆง y โŠจ A x โŸถ xa = y)โ€บ (is ?A)
                โ€นx โŠจ prod A Sโ€บ (is ?B)
                โ€นy โŠจ prod A Sโ€บ (is ?C)
     and โ€นfinite Sโ€บ
  have โ€น?A โˆง ?B โˆง ?C โŸถ x = yโ€บ
    by (induct arbitrary: x y rule: finite_induct[OF โ€นfinite Sโ€บ]; clarsimp; blast)
  then show โ€นx = yโ€บ
    using prems by blast
qed


subsection โ€นCarrier Set of Separation Algebraโ€บ

definition Within_Carrier_Set :: โ€น'c::sep_carrier BI โ‡’ boolโ€บ
  where โ€นWithin_Carrier_Set A โŸท (โˆ€v. v โŠจ A โŸถ mul_carrier v)โ€บ

definition Carrier_Set :: โ€น('c::sep_carrier,'a) ฯ† โ‡’ ('a โ‡’ bool) โ‡’ boolโ€บ
  where โ€นCarrier_Set T D โŸท (โˆ€x. D x โŸถ Within_Carrier_Set (x โฆ‚ T))โ€บ

declare [[ฯ†reason_default_pattern
      โ€นWithin_Carrier_Set ?Aโ€บ โ‡’ โ€นWithin_Carrier_Set ?Aโ€บ (100)
  and โ€นCarrier_Set ?T _โ€บ โ‡’ โ€นCarrier_Set ?T _โ€บ (100)
]]

ฯ†reasoner_group carrier_set = (100, [10,3000]) for โ€นWithin_Carrier_Set Aโ€บ
    โ€นRules reasoning if the given assertion โ€นAโ€บ with all its concrete objects falls in the carrier set of the separation algebraโ€บ
 and carrier_set_cut = (1000, [1000, 1030]) for โ€นWithin_Carrier_Set Aโ€บ in carrier_set
    โ€นCutting rulesโ€บ
 and carrier_set_red = (2500, [2500, 2530]) for โ€นWithin_Carrier_Set Aโ€บ in carrier_set and > carrier_set_cut
    โ€นLiteral Reductionโ€บ
 and derived_carrier_set = (50, [50,50]) for (โ€นWithin_Carrier_Set Aโ€บ, โ€นCarrier_Set T Dโ€บ)
                                          in carrier_set and < carrier_set_cut
    โ€นDerived rulesโ€บ
 and carrier_set_to_within_carrier_set = (10, [10,10]) for (โ€นWithin_Carrier_Set Aโ€บ, โ€นCarrier_Set T Dโ€บ)
                                                        in carrier_set and < derived_carrier_set
    โ€นTurning โ€นWithin_Carrier_Set (x : T)โ€บ to โ€นCarrier_Set T Dโ€บโ€บ

subsubsection โ€นExtracting Pure Factsโ€บ

context begin

private lemma EIF_Within_Carrier_Set:
  โ€น ๐—‹EIF (Within_Carrier_Set A) (โˆ€v. v โŠจ A โŸถ mul_carrier v) โ€บ
  unfolding Within_Carrier_Set_def ๐—‹EIF_def
  by blast

private lemma ESC_Within_Carrier_Set:
  โ€น ๐—‹ESC (โˆ€v. v โŠจ A โŸถ mul_carrier v) (Within_Carrier_Set A) โ€บ
  unfolding Within_Carrier_Set_def ๐—‹ESC_def
  by blast

private lemma EIF_Carrier_Set:
  โ€น ๐—‹EIF (Carrier_Set T D) (โˆ€x v. D x โˆง v โŠจ (x โฆ‚ T) โŸถ mul_carrier v) โ€บ
  unfolding Carrier_Set_def Within_Carrier_Set_def ๐—‹EIF_def
  by blast

private lemma ESC_Carrier_Set:
  โ€น ๐—‹ESC (โˆ€x v. D x โˆง v โŠจ (x โฆ‚ T) โŸถ mul_carrier v) (Carrier_Set T D) โ€บ
  unfolding Carrier_Set_def Within_Carrier_Set_def ๐—‹ESC_def
  by blast

bundle extracting_Carrier_Set_sat =
          EIF_Within_Carrier_Set [ฯ†reason %extract_pure_sat]
          ESC_Within_Carrier_Set [ฯ†reason %extract_pure_sat]
          EIF_Carrier_Set [ฯ†reason %extract_pure_sat]
          ESC_Carrier_Set [ฯ†reason %extract_pure_sat]

end


subsubsection โ€นRules for Logical Connectivesโ€บ

lemma [ฯ†reason default %carrier_set_to_within_carrier_set]:
  โ€น Carrier_Set T P
โŸน ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ P x
โŸน Within_Carrier_Set (x โฆ‚ T) โ€บ
  unfolding Carrier_Set_def Premise_def
  by blast

lemma [ฯ†reason %carrier_set_cut]:
  โ€น Within_Carrier_Set A
โŸน Within_Carrier_Set B
โŸน Within_Carrier_Set (A * B)โ€บ
  unfolding Within_Carrier_Set_def
  by (clarsimp simp add: mul_carrier_closed)

text โ€นThough the above rule is reasonable enough, it is not reversible, essentially because
  the set of concrete objects satisfying โ€นA * Bโ€บ is far smaller than either of that satisfying โ€นAโ€บ or โ€นBโ€บ.โ€บ

lemma
  โ€นWithin_Carrier_Set (A * B) โŸน Within_Carrier_Set A โˆง Within_Carrier_Set Bโ€บ
  oops

lemma [ฯ†reason %carrier_set_cut]:
  โ€น (โ‹€iโˆˆI. Within_Carrier_Set (A i))
โŸน Within_Carrier_Set (โœฑiโˆˆI. A i) โ€บ
  for A :: โ€น'i โ‡’ 'c :: {sep_algebra,sep_carrier_1} BIโ€บ
  unfolding Within_Carrier_Set_def Mul_Quant_def meta_Ball_def Premise_def
proof clarsimp
  fix v
  assume โ€นfinite Iโ€บ
  show โ€น(โ‹€x. x โˆˆ I โŸน โˆ€v. v โŠจ A x โŸถ mul_carrier v) โŸน v โŠจ prod A I โŸน mul_carrier vโ€บ
    by (induct arbitrary: v rule: finite_induct[OF โ€นfinite Iโ€บ]; clarsimp; metis mul_carrier_closed)
qed

(*
lemma โ€• โ€นderived automatically laterโ€บ
  โ€น Carrier_Set T P
โŸน Carrier_Set U Q
โŸน Carrier_Set (T โˆ— U) (pred_prod P Q)โ€บ
  unfolding Carrier_Set_def Within_Carrier_Set_def
  by (clarsimp simp add: mul_carrier_closed)
*)

lemma [ฯ†reason %carrier_set_cut]:
  โ€น Within_Carrier_Set A
โŸน Within_Carrier_Set B
โŸน Within_Carrier_Set (A + B)โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp

lemma โ€• โ€นThe above rule is reversibleโ€บ
  โ€น Within_Carrier_Set (A + B) โŸน Within_Carrier_Set A โˆง Within_Carrier_Set B โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp

lemma [ฯ†reason %carrier_set_cut]:
  โ€น (โ‹€x. Within_Carrier_Set (A x))
โŸน Within_Carrier_Set (ExBI A) โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp

lemma โ€• โ€นThe above rule is reversibleโ€บ
  โ€น Within_Carrier_Set (ExBI A) โŸน Within_Carrier_Set (A x) โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp blast

lemma [ฯ†reason %carrier_set_cut]:
  โ€น Within_Carrier_Set A
โŸน Within_Carrier_Set B
โŸน Within_Carrier_Set (A โŠ“ B)โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp


text โ€นThe above rule is also not reversible. Essentially the rules for conjunctive connectives are not
  reversible due to the same reason as โ€น*โ€บ. โ€บ

lemma โ€น Within_Carrier_Set (A โŠ“ B) โŸน Within_Carrier_Set A โˆง Within_Carrier_Set B โ€บ
  oops

lemma [ฯ†reason %carrier_set_cut]:
  โ€น (๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ P โŸน Within_Carrier_Set A)
โŸน Within_Carrier_Set (A ๐—Œ๐—Ž๐–ป๐—ƒ P)โ€บ
  unfolding Within_Carrier_Set_def
  by clarsimp

lemma [ฯ†reason %carrier_set_cut]:
  โ€น Within_Carrier_Set 0 โ€บ
  unfolding Within_Carrier_Set_def
  by simp

lemma [ฯ†reason %carrier_set_cut]:
  โ€น Within_Carrier_Set (1 :: 'a::sep_carrier_1 BI) โ€บ
  unfolding Within_Carrier_Set_def
  by simp

paragraph โ€นCase Splitโ€บ

subparagraph โ€นReductionโ€บ

lemma [ฯ†reason %carrier_set_red]:
  โ€น Within_Carrier_Set (P a)
โŸน Within_Carrier_Set (case_sum P Q (Inl a)) โ€บ
  by simp

lemma [ฯ†reason %carrier_set_red]:
  โ€น Within_Carrier_Set (Q b)
โŸน Within_Carrier_Set (case_sum P Q (Inr b)) โ€บ
  by simp

subparagraph โ€นCase Splitโ€บ

lemma [ฯ†reason %carrier_set_cut]:
  โ€น (โ‹€a. ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ x = Inl a โŸน Within_Carrier_Set (P a))
โŸน (โ‹€b. ๐–ผ๐—ˆ๐—‡๐–ฝ๐—‚๐—๐—‚๐—ˆ๐—‡ x = Inr b โŸน Within_Carrier_Set (Q b))
โŸน Within_Carrier_Set (case_sum P Q x) โ€บ
  unfolding Premise_def
  by (cases x; clarsimp)

text โ€นAlso not reversible in non-trivial cases.โ€บ

subsubsection โ€นRules for Basic ฯ†-Typesโ€บ

lemma [ฯ†reason 1000]:
  โ€นCarrier_Set Itself mul_carrierโ€บ
  unfolding Carrier_Set_def Within_Carrier_Set_def
  by simp

lemma [ฯ†reason 1000]:
  โ€นCarrier_Set (โ—‹ :: ('c::sep_carrier_1, unit) ฯ†) (ฮป_. True)โ€บ
  unfolding Carrier_Set_def Within_Carrier_Set_def
  by clarsimp


subsection โ€นSeparationally Functionalโ€บ โ€• โ€นA weaker and more general conceptโ€บ

definition Sep_Functional :: โ€น'a::sep_magma BI โ‡’ boolโ€บ
  where โ€นSep_Functional S โŸท (โˆ€x y. x โŠจ S โˆง y โŠจ S โˆง x ## y โŸถ x = y) โ€บ

declare [[ฯ†reason_default_pattern โ€นSep_Functional ?Sโ€บ โ‡’ โ€นSep_Functional ?Sโ€บ (100)]]

lemma [ฯ†reason no explorative backtrack 1]:
  โ€น Is_Functional S
โŸน Sep_Functional S โ€บ
  unfolding Sep_Functional_def Is_Functional_def
  by simp

lemma [ฯ†reason no explorative backtrack 0]:
  โ€น TRACE_FAIL TEXT(โ€นFail to showโ€บ S โ€นis separationally functionalโ€บ)
โŸน Sep_Functional Sโ€บ
  unfolding TRACE_FAIL_def
  by blast

lemma [ฯ†reason 1200]:
  โ€น Sep_Functional A
โŸน Sep_Functional B
โŸน Sep_Functional (A โŠ“ B) โ€บ
  unfolding Sep_Functional_def
  by simp

lemma [ฯ†reason 1200]:
  โ€น Sep_Functional A
โŸน Sep_Functional B
โŸน Sep_Functional (A * B)โ€บ
  for A :: โ€น'a::sep_ab_semigroup BIโ€บ
  unfolding Sep_Functional_def
  by (clarsimp; metis sep_disj_commute sep_disj_multD1 sep_disj_multD2)

lemma [ฯ†reason 1200]:
  โ€น Sep_Functional (fst x โฆ‚ T)
โŸน Sep_Functional (snd x โฆ‚ U)
โŸน Sep_Functional (x โฆ‚ T โˆ— U)โ€บ
  for T :: โ€น('c::sep_ab_semigroup, 'a) ฯ†โ€บ
  unfolding Sep_Functional_def
  by (cases x; clarsimp;
      metis sep_disj_commute sep_disj_multD1 sep_disj_multD2)

(*
lemma [ฯ†reason 1200]:
  โ€น (โ‹€iโˆˆI. Sep_Functional (A i))
โŸน Sep_Functional (โœฑiโˆˆI. A i) โ€บ
  unfolding Mul_Quant_def atomize_Ball Ball_def
proof clarsimp
  *)



subsection โ€นReflexive Separationโ€บ

text โ€นpropโ€นx ## xโ€บ is used to represented โ€นxโ€บ is in the carrier set in some algebras like permission algebra.โ€บ

definition Sep_Reflexive :: โ€น'a::sep_magma BI โ‡’ boolโ€บ
  where โ€นSep_Reflexive S โŸท (โˆ€u. u โŠจ S โŸถ u ## u) โ€บ

lemma [ฯ†reason 1000]:
  โ€น Sep_Reflexive A
โŸน Sep_Reflexive B
โŸน Sep_Reflexive (A * B) โ€บ
  for A :: โ€น'a::sep_refl BIโ€บ
  unfolding Sep_Reflexive_def
  by (clarsimp simp add: sep_refl_mult_I)

lemma [ฯ†reason 1000]:
  โ€น Sep_Reflexive (fst x โฆ‚ T)
โŸน Sep_Reflexive (snd x โฆ‚ U)
โŸน Sep_Reflexive (x โฆ‚ T โˆ— U) โ€บ
  for T :: โ€น('c::sep_refl, 'a) ฯ†โ€บ
  unfolding Sep_Reflexive_def
  by (clarsimp simp add: sep_refl_mult_I)

lemma [ฯ†reason 1000]:
  โ€น Sep_Reflexive A
โŸน Sep_Reflexive B
โŸน Sep_Reflexive (A + B) โ€บ
  unfolding Sep_Reflexive_def
  by clarsimp

lemma [ฯ†reason 1000]:
  โ€น (โ‹€x. Sep_Reflexive (A x))
โŸน Sep_Reflexive (ExBI A) โ€บ
  unfolding Sep_Reflexive_def
  by clarsimp

lemma [ฯ†reason 1000]:
  โ€น Sep_Reflexive A
โŸน Sep_Reflexive B
โŸน Sep_Reflexive (A โŠ“ B) โ€บ
  unfolding Sep_Reflexive_def
  by clarsimp

lemma [ฯ†reason 1000]:
  โ€น Sep_Reflexive A
โŸน Sep_Reflexive (A ๐—Œ๐—Ž๐–ป๐—ƒ P) โ€บ
  unfolding Sep_Reflexive_def
  by clarsimp


section โ€นSpecification of Monadic Statesโ€บ

definition StrictState :: "('ret ฯ†arg โ‡’ rassn)
                          โ‡’ (ABNM โ‡’ rassn)
                          โ‡’ 'ret comp BI"
  where "StrictState T E = BI {s. case s of Success val x โ‡’ x โŠจ T val
                                    | Abnormal val x โ‡’ x โŠจ E val
                                    | Invalid โ‡’ False
                                    | NonTerm โ‡’ False
                                    | AssumptionBroken โ‡’ False
                  }"

definition LooseState  :: "('ret ฯ†arg โ‡’ rassn)
                          โ‡’ (ABNM โ‡’ rassn)
                          โ‡’ 'ret comp BI"
  where  "LooseState T E = BI {s. case s of Success val x โ‡’ x โŠจ T val
                                    | Abnormal val x โ‡’ x โŠจ E val
                                    | Invalid โ‡’ False
                                    | NonTerm โ‡’ True
                                    | AssumptionBroken โ‡’ True
                  }"

lemma StrictState_expn[iff]:
        "Success vs x โŠจ StrictState T E โ‰ก x โŠจ T vs"
        "Abnormal v x โŠจ StrictState T E โ‰ก x โŠจ E v"
        "ยฌ (Invalid โŠจ StrictState T E)"
        "ยฌ (NonTerm โŠจ StrictState T E)"
        "ยฌ (AssumptionBroken โŠจ StrictState T E)"
        "ยฌ Itself Invalid โ‰ค StrictState T E"
        "ยฌ Itself NonTerm โ‰ค StrictState T E"
        "ยฌ Itself AssumptionBroken โ‰ค StrictState T E"
  and LooseState_expn[iff]:
        "Success vs x โŠจ LooseState T E โ‰ก x โŠจ T vs"
        "Abnormal v x โŠจ LooseState T E โ‰ก x โŠจ E v"
        "ยฌ (Invalid โŠจ LooseState T E)"
        "(NonTerm โŠจ LooseState T E)"
        "(AssumptionBroken โŠจ LooseState T E)"
        "ยฌ Itself Invalid โ‰ค LooseState T E"
        "Itself NonTerm โ‰ค LooseState T E"
        "Itself AssumptionBroken โ‰ค LooseState T E"
  by (simp_all add: StrictState_def LooseState_def less_eq_BI_iff)

lemma LooseState_expn' :
    "x โŠจ LooseState T E โŸท x = NonTerm
                 โˆจ x = AssumptionBroken
                 โˆจ (โˆƒx' v. x = Success v x' โˆง x' โŠจ T v)
                 โˆจ (โˆƒx' v. x = Abnormal v x' โˆง x' โŠจ E v)"
  by (cases x) simp_all

lemma StrictState_elim[elim]:
    "s โŠจ StrictState T E
โŸน (โ‹€x v. s = Success v x โŸน x โŠจ T v โŸน C)
โŸน (โ‹€x v. s = Abnormal v x โŸน x โŠจ E v โŸน C)
โŸน C" by (cases s) auto

lemma StrictState_intro[intro]:
    " x โŠจ T v โŸน Success v x โŠจ StrictState T E"
    " x โŠจ E a โŸน Abnormal a x โŠจ StrictState T E"
  by simp_all

lemma LooseState_E[elim]:
    "s โŠจ LooseState T E
โŸน (โ‹€x v. s = Success v x โŸน x โŠจ T v โŸน C)
โŸน (โ‹€x v. s = Abnormal v x โŸน x โŠจ E v โŸน C)
โŸน (s = NonTerm โŸน C)
โŸน (s = AssumptionBroken โŸน C)
โŸน C"
  by (cases s) auto

lemma LooseState_I[intro]:
  "x โŠจ T v โŸน Success v x โŠจ LooseState T E"
  "x โŠจ E a โŸน Abnormal a x โŠจ LooseState T E"
  "NonTerm โŠจ LooseState T E"
  "AssumptionBroken โŠจ LooseState T E" (*XXXXXXXXX TODO ERR*)
  by simp_all

lemma LooseState_upgrade:
  "s โŠจ LooseState T E โŸน s โ‰  AssumptionBroken โŸน s โ‰  NonTerm โŸน s โŠจ StrictState T E"
  by (cases s) auto

lemma StrictState_degrade: "s โŠจ StrictState T E โŸน s โŠจ LooseState T E" by (cases s) auto

lemma LooseState_introByStrict:
  "(s โ‰  AssumptionBroken โŸน s โ‰  NonTerm โŸน s โŠจ StrictState T E) โŸน s โŠจ LooseState T E"
  by (cases s) auto

lemma StrictState_subset:
  โ€น(โ‹€v. A v โ‰ค A' v) โŸน (โ‹€v. E v โ‰ค E' v) โŸน StrictState A E โ‰ค StrictState A' E'โ€บ
  unfolding less_eq_BI_iff StrictState_def by simp

lemma StrictState_subset'[intro]:
  โ€น(โ‹€v. โˆ€s. s โŠจ A v โŸถ s โŠจ A' v) โŸน (โ‹€v. โˆ€s. s โŠจ E v โŸถ s โŠจ E' v) โŸน s โŠจ StrictState A E โŸน s โŠจ StrictState A' E'โ€บ
  unfolding StrictState_def by (cases s; simp)

lemma LooseState_subset:
  โ€น(โ‹€v. A v โ‰ค A' v) โŸน (โ‹€v. E v โ‰ค E' v) โŸน LooseState A E โ‰ค LooseState A' E'โ€บ
  unfolding less_eq_BI_iff LooseState_def by simp

lemma LooseState_subset'[intro]:
  โ€น(โ‹€v. โˆ€s. s โŠจ A v โŸถ s โŠจ A' v) โŸน (โ‹€v. โˆ€s. s โŠจ E v โŸถ s โŠจ E' v) โŸน s โŠจ LooseState A E โŸน s โŠจ LooseState A' E'โ€บ
  unfolding LooseState_def by (cases s; simp)


lemma LooseState_plus[iff]:
(*  โ€นLooseState (A + B) E   = LooseState A E + LooseState B Eโ€บ *)
  โ€นLooseState X (ฮปv. EA v + EB v) = LooseState X EA + LooseState X EBโ€บ
  unfolding BI_eq_iff LooseState_def by simp_all

lemma StrictState_plus[iff]:
(*  โ€นStrictState (A + B) E   = StrictState A E  + StrictState B Eโ€บ *)
  โ€นStrictState X (ฮปv. EA v + EB v) = StrictState X EA + StrictState X EBโ€บ
  unfolding BI_eq_iff StrictState_def by simp_all

abbreviation โ€นVoid โ‰ก (1::assn)โ€บ


section โ€นSpecification of Fictional Resourceโ€บ

declare INTERP_SPEC[ฯ†expns]

lemma  INTERP_SPEC_subj[ฯ†expns]:
  โ€น INTERP_SPEC (S ๐—Œ๐—Ž๐–ป๐—ƒ P) = (INTERP_SPEC S ๐—Œ๐—Ž๐–ป๐—ƒ P) โ€บ
  unfolding INTERP_SPEC_def by (simp add: BI_eq_iff, blast)

lemma  INTERP_SPEC_ex[ฯ†expns]:
  โ€น INTERP_SPEC (ExBI S) = (โˆƒ* x. INTERP_SPEC (S x)) โ€บ
  unfolding INTERP_SPEC_def by (simp add: BI_eq_iff, blast)

abbreviation COMMA :: โ€นassn โ‡’ assn โ‡’ assnโ€บ ("_โŸ/ _" [17,16] 16)
  where โ€นCOMMA โ‰ก (*)โ€บ


section โ€นSpecification of Computationโ€บ

definition ฯ†Procedure :: "'ret proc
                        โ‡’ assn
                        โ‡’ ('ret ฯ†arg โ‡’ assn)
                        โ‡’ (ABNM โ‡’ assn)
                        โ‡’ bool"
  where "ฯ†Procedure f T U E โŸท
    (โˆ€comp R. comp โŠจ INTERP_SPEC (T * R) โŸถ
        BI_lift (f comp) โ‰ค LooseState (ฮปv. INTERP_SPEC (U v * R)) (ฮปv. INTERP_SPEC (E v * R)))"

abbreviation ฯ†Procedure_no_exception
  where โ€นฯ†Procedure_no_exception f T U โ‰ก ฯ†Procedure f T U 0โ€บ

notation (input)
         ฯ†Procedure ("๐—‰๐—‹๐—ˆ๐–ผ (2_)/ (0โฆƒ _/ โŸผ _ โฆ„)/ ๐—๐—๐—‹๐—ˆ๐—๐—Œ (_)/ " [10,2,2,100] 100)
     and ฯ†Procedure_no_exception ("๐—‰๐—‹๐—ˆ๐–ผ (2_)/ โฆƒ (2_) โŸผ/ (2_) โฆ„/ " [10,2,2] 100)

notation ฯ†Procedure_no_exception
            ("(โ€นconsistentโ€บ๐—‰๐—‹๐—ˆ๐–ผ (_)/ ๐—‚๐—‡๐—‰๐—Ž๐— (_)/ ๐—ˆ๐—Ž๐—๐—‰๐—Ž๐— (_))" [2,2,2] 100)
     and ฯ†Procedure
            ("(โ€นconsistentโ€บ๐—‰๐—‹๐—ˆ๐–ผ (_)/ ๐—‚๐—‡๐—‰๐—Ž๐— (_)/ ๐—ˆ๐—Ž๐—๐—‰๐—Ž๐— (_)/ ๐—๐—๐—‹๐—ˆ๐—๐—Œ (_))" [2,2,2,100] 100)

translations
  "CONST ฯ†Procedure p X Y E" <= "CONST ฯ†Procedure (_do_block p) X Y E"
  "CONST ฯ†Procedure_no_exception p X Y" <= "CONST ฯ†Procedure_no_exception (_do_block p) X Y"

lemma ฯ†Procedure_alt:
  โ€น๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ T โŸผ U โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸท (โˆ€comp r. comp โŠจ INTERP_SPEC (T * Itself r)
        โŸถ BI_lift (f comp) โ‰ค LooseState (ฮปv. INTERP_SPEC (U v * Itself r)) (ฮปv. INTERP_SPEC (E v * Itself r)))โ€บ
  apply rule
  apply ((unfold ฯ†Procedure_def)[1], blast)
  unfolding ฯ†Procedure_def INTERP_SPEC subset_iff
  apply (clarsimp simp add: less_eq_BI_iff times_set_def split_comp_All INTERP_SPEC_def)
  by fastforce

lemmas ฯ†Procedure_I = ฯ†Procedure_alt[THEN iffD2]

lemma ฯ†Procedure_protect_body:
  โ€น T โ‰ก T'
โŸน U โ‰ก U'
โŸน E โ‰ก E'
โŸน ฯ†Procedure f T U E โ‰ก ฯ†Procedure f T' U' E'โ€บ
  by simp

subsubsection โ€นSyntaxโ€บ

ML_file โ€นlibrary/syntax/procedure.MLโ€บ

section โ€นView Shiftโ€บ

definition View_Shift
    :: "assn โ‡’ assn โ‡’ bool โ‡’ bool" ("(2_/ ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ _/ ๐—๐—‚๐—๐— _)" [13,13,13] 12)
  where "View_Shift T U P โŸท (โˆ€x R. x โŠจ INTERP_SPEC (T * R) โŸถ x โŠจ INTERP_SPEC (U * R) โˆง P)"

abbreviation Simple_View_Shift
    :: "assn โ‡’ assn โ‡’ bool" ("(2_/ ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ _)"  [13,13] 12)
  where โ€นSimple_View_Shift T U โ‰ก View_Shift T U Trueโ€บ

declare [[ฯ†reason_default_pattern
    โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?Y ๐—๐—‚๐—๐— _โ€บ โ‡’ โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?Y ๐—๐—‚๐—๐— _โ€บ (10)
and โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ _ โฆ‚ ?U ๐—๐—‚๐—๐— _โ€บ โ‡’ โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?var_y โฆ‚ ?U ๐—๐—‚๐—๐— _โ€บ (20)
and โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?Y ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ _ ๐—๐—‚๐—๐— _โ€บ โ‡’ โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?Y ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ _ ๐—๐—‚๐—๐— _โ€บ (20)
and โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ _ โฆ‚ ?U ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ _ ๐—๐—‚๐—๐— _โ€บ โ‡’ โ€น?X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?var_y โฆ‚ ?U ๐—‹๐–พ๐—†๐–บ๐—‚๐—‡๐—Œ _ ๐—๐—‚๐—๐— _โ€บ (30)
]]

subsection โ€นBasic Rulesโ€บ

lemma View_Shift_imply_P:
  โ€น X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P1
โŸน (P1 โŸถ P2)
โŸน X ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P2โ€บ
  unfolding View_Shift_def
  by blast

lemma view_shift_by_implication[intro?, ฯ†reason 10]:
  โ€น A ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ B ๐—๐—‚๐—๐— P
โŸน A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B ๐—๐—‚๐—๐— Pโ€บ
  unfolding Transformation_def View_Shift_def INTERP_SPEC_def
  by clarsimp blast

lemma view_shift_0[simp]:
  โ€น 0 ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ X ๐—๐—‚๐—๐— any โ€บ
  by (blast intro: view_shift_by_implication zero_implies_any)

lemma [ฯ†reason 2000 for โ€น0 ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?X ๐—๐—‚๐—๐— ?Pโ€บ]:
  โ€น 0 ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ X ๐—๐—‚๐—๐— False โ€บ
  by simp

lemma view_shift_refl[ฯ†reason 2000 for โ€น?A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?B ๐—๐—‚๐—๐— ?Pโ€บ]:
  "A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ A"
  by (blast intro: view_shift_by_implication transformation_refl)

lemma [ฯ†reason 800 for โ€น?x โฆ‚ ?T ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ ?y โฆ‚ ?T' ๐—๐—‚๐—๐— ?Pโ€บ]:
  " Object_Equiv T eq
โŸน ๐—‰๐—‹๐–พ๐—†๐—‚๐—Œ๐–พ eq x y
โŸน x โฆ‚ T ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ y โฆ‚ T"
  unfolding Object_Equiv_def Premise_def
  by (insert view_shift_by_implication, presburger)

lemma view_shift_union[ฯ†reason 800]:
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ X ๐—๐—‚๐—๐— P
โŸน A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ X + Y ๐—๐—‚๐—๐— Pโ€บ
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P
โŸน A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ X + Y ๐—๐—‚๐—๐— Pโ€บ
  by (simp add: View_Shift_def distrib_right)+

lemma ฯ†view_shift_trans:
  "A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B ๐—๐—‚๐—๐— P
    โŸน (P โŸน B ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ C ๐—๐—‚๐—๐— Q)
    โŸน A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ C ๐—๐—‚๐—๐— P โˆง Q"
  unfolding View_Shift_def by blast

lemma ฯ†frame_view:
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B ๐—๐—‚๐—๐— P
โŸน A * R ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B * R ๐—๐—‚๐—๐— Pโ€บ
  unfolding View_Shift_def
  by (metis (no_types, lifting) mult.assoc)

lemma ฯ†view_shift_intro_frame:
  "U' ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ U ๐—๐—‚๐—๐— P โŸน U' * R ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ U * R ๐—๐—‚๐—๐— P "
  by (simp add: ฯ†frame_view)

lemma ฯ†view_shift_intro_frame_R:
  "U' ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ U ๐—๐—‚๐—๐— P โŸน R * U' ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ R * U ๐—๐—‚๐—๐— P "
  by (metis ฯ†frame_view mult.commute)


section โ€นHoare Rules \& SL Rulesโ€บ

subsection โ€นFundamental Rulesโ€บ

lemma ฯ†SKIP[simp,intro!]: "๐—‰๐—‹๐—ˆ๐–ผ det_lift (Success v) โฆƒ T v โŸผ T โฆ„"
  unfolding ฯ†Procedure_def det_lift_def less_eq_BI_iff by clarsimp

lemma ฯ†SEQ:
   "๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A โŸผ B โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน (โ‹€vs. ๐—‰๐—‹๐—ˆ๐–ผ g vs โฆƒ B vs โŸผ C โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E)
โŸน ๐—‰๐—‹๐—ˆ๐–ผ (f โคœ (ฮปv. g v)) โฆƒ A โŸผ C โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E"
  unfolding ฯ†Procedure_def bind_def apply (clarsimp simp add: less_eq_BI_iff)
  subgoal for comp R x s
    apply (cases s; clarsimp; cases x; clarsimp; blast) . .

lemma ฯ†frame:
  " ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A โŸผ B โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A * R โŸผ ฮปret. B ret * R โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ (ฮปex. E ex * R) "
  unfolding ฯ†Procedure_def less_eq_BI_iff
  apply clarify subgoal premises prems for comp R' s
    using prems(1)[THEN spec[where x=comp], THEN spec[where x=โ€นR * R'โ€บ],
          simplified mult.assoc[symmetric], THEN mp, OF prems(2)] prems(3)
    by blast .

lemma ฯ†Satisfiable:
  โ€น(Satisfiable X โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ X โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E)
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ X โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ Eโ€บ
  unfolding ฯ†Procedure_def Satisfiable_def
  by (meson INTERP_SPEC sep_conj_expn)


subsubsection โ€นView Shiftโ€บ

lemma ฯ†frame_view_right:
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B ๐—๐—‚๐—๐— P
โŸน A * R ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B * R ๐—๐—‚๐—๐— Pโ€บ
  unfolding View_Shift_def
  by (metis (no_types, lifting) mult.assoc mult.commute)

lemma ฯ†view_trans:
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B ๐—๐—‚๐—๐— P1
โŸน (P1 โŸน B ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ C ๐—๐—‚๐—๐— P2)
โŸน A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ C ๐—๐—‚๐—๐— P1 โˆง P2โ€บ
  unfolding View_Shift_def by blast

lemma ฯ†CONSEQ:
   "๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A  โŸผ B  โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน A' ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ A ๐—๐—‚๐—๐— Any1
โŸน (โ‹€ret. B ret ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ B' ret ๐—๐—‚๐—๐— Any2)
โŸน (โ‹€ex.  E ex ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ E' ex ๐—๐—‚๐—๐— Any3)
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A' โŸผ B' โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E' "
  unfolding ฯ†Procedure_def View_Shift_def less_eq_BI_iff
  by (clarsimp, smt (verit, del_insts) LooseState_expn')

subsection โ€นHelper Rulesโ€บ

lemma ฯ†frame0:
  "๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A โŸผ B โฆ„ โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A * R โŸผ ฮปret. B ret * R โฆ„"
  using ฯ†frame[where E=0, simplified, folded zero_fun_def] .

lemma ฯ†CONSEQ'E:
   "(โ‹€v. E v ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ E' v ๐—๐—‚๐—๐— P3)
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A  โŸผ B  โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A โŸผ B โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E' "
  using ฯ†CONSEQ view_shift_refl by blast

lemmas ฯ†CONSEQ'E0 = ฯ†CONSEQ'E[OF view_shift_0, unfolded zero_fun_eta]

subsubsection โ€นCase Analysisโ€บ

lemma ฯ†CASE:
  โ€น ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A โŸผ C โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ B โŸผ C โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E
โŸน ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ A + B โŸผ C โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ Eโ€บ
  unfolding ฯ†Procedure_def
  by(simp add: distrib_right)

lemma ฯ†CASE_VS:
  โ€น A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P1
โŸน B ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P2
โŸน B + A ๐—Œ๐—๐—‚๐–ฟ๐—๐—Œ Y ๐—๐—‚๐—๐— P2 โˆจ P1โ€บ
  unfolding View_Shift_def
  by (simp add: distrib_right)

lemma ฯ†CASE_IMP:
  โ€น A ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ Y ๐—๐—‚๐—๐— P1
โŸน B ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ Y ๐—๐—‚๐—๐— P2
โŸน B + A ๐—๐—‹๐–บ๐—‡๐—Œ๐–ฟ๐—ˆ๐—‹๐—†๐—Œ Y ๐—๐—‚๐—๐— P2 โˆจ P1โ€บ
  unfolding Transformation_def
  by (simp add: distrib_left)


subsubsection โ€นNormalization in Preconditionโ€บ

lemma norm_precond_conj:
  "(๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ T ๐—Œ๐—Ž๐–ป๐—ƒ P โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E) = (P โŸถ ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ T โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E )"
  unfolding ฯ†Procedure_def
  by (simp add: INTERP_SPEC_subj) blast

lemmas norm_precond_conj_metaeq[unfolded atomize_eq[symmetric]] = norm_precond_conj

lemma norm_precond_ex:
  "(๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ ExBI X โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E) = (โˆ€x. ๐—‰๐—‹๐—ˆ๐–ผ f โฆƒ X x โŸผ Y โฆ„ ๐—๐—๐—‹๐—ˆ๐—๐—Œ E)"
  unfolding ฯ†Procedure_def
  by (simp add: INTERP_SPEC_ex) blast


ML_file โ€นlibrary/syntax/syntax0.MLโ€บ


section โ€นReasoning Configurationโ€บ

subsection โ€นNormalization of Assertionsโ€บ

consts semantic_mode :: mode
       ABNORMAL :: mode

ML โ€น
structure Assertion_SS_Abnormal = Simpset (
  val initial_ss = Simpset_Configure.Empty_SS
  val binding = SOME bindingโ€นassertion_simps_abnormalโ€บ
  val comment = "Simp rules normalizing particularly the abnormal spec of a triple."
  val attribute = NONE
  val post_merging = I
)
โ€บ

ฯ†reasoner_ML assertion_simp_abnormal 1300
  (โ€นSimplify (assertion_simps ABNORMAL) ?X' ?Xโ€บ)
  = โ€นPhi_Reasoners.wrap (PLPR_Simplifier.simplifier_by_ss' (K Seq.empty) (fn ctxt =>
      Raw_Simplifier.merge_ss (Assertion_SS.get' ctxt, Assertion_SS_Abnormal.get' ctxt)) {fix_vars=false}) o sndโ€บ

ฯ†reasoner_ML semantic_simps 1200
  (โ€นPremise semantic_mode _โ€บ | โ€นSimplify semantic_mode ?X' ?Xโ€บ
     )
  = โ€นPhi_Reasoners.wrap (PLPR_Simplifier.simplifier (K Seq.empty) (fn ctxt =>
        Simplifier.clear_simpset ctxt addsimps @{thms ฯ†V_simps ฯ†arg.sel ฯ†arg.collapse}) {fix_vars=false}) o sndโ€บ

lemmas [assertion_simps] =
  ฯ†V_simps

end