Theory Phi_Semantics_Framework
section ‹Modular Formalization of Program Semantics›
text ‹Using the Virtual Datatype, Resource Space, Fiction Space, now in this section it is
feasible to formalize the semantics of programs modularly and extensibly.
The section first presents an empty formalization (framework) of computation states
consisting of empty types, empty values, empty resources, and empty fictions.
It serves for future formalization of any specific program semantics.
Then on this empty formalization of computation states.
The framework formalizes computations using state-error-exception monad,
supporting most of control flows and therefore most of (imperative) languages.
›
theory Phi_Semantics_Framework
imports Resource_Space Virtual_Datatype.Virtual_Datatype Debt_Axiom.Debt_Axiom
"HOL-Library.Monad_Syntax"
keywords "resource_space" :: thy_goal
abbrevs "<throws>" = "𝗍𝗁𝗋𝗈𝗐𝗌"
and "<proc>" = "𝗉𝗋𝗈𝖼"
begin
text ‹The section provides the initial empty semantics of computation states
serving as the base for any further substantial formalization.›
subsection ‹Type›
unspecified_type TY
debt_axiomatization 𝗉𝗈𝗂𝗌𝗈𝗇 :: TY
subsection ‹Value›
unspecified_type VAL
instance VAL :: sep_magma ..
text ‹The semantic value is a separation magma. It is nothing related to the semantic
or the specification framework themselves but just to be helpful in some situation for
formalization of some semantics such as that in aggregate the separation can represent
concatenation of fields.›
subsubsection ‹Deep Representation of Aggregated Values›
class VAL =
fixes to_val :: ‹'a ⇒ VAL›
and from_val :: ‹VAL ⇒ 'a›
assumes from_to_val[simp]: ‹from_val (to_val x) = x›
class VALs =
fixes to_vals :: ‹'a ⇒ VAL list›
and from_vals :: ‹VAL list ⇒ 'a›
assumes from_to_vals[simp]: ‹from_vals (to_vals x) = x›
class FIX_ARITY_VALs = VALs +
fixes vals_arity :: ‹'a itself ⇒ nat›
assumes length_to_vals[simp]: ‹length (to_vals x) = vals_arity TYPE('a)›
instantiation VAL :: VAL begin
definition to_val_VAL :: ‹VAL ⇒ VAL› where ‹to_val_VAL = id›
definition from_val_VAL :: ‹VAL ⇒ VAL› where ‹from_val_VAL = id›
instance by standard (simp add: to_val_VAL_def from_val_VAL_def)
end
instantiation unit :: FIX_ARITY_VALs begin
definition to_vals_unit :: ‹unit ⇒ VAL list› where ‹to_vals_unit v = []›
definition from_vals_unit :: ‹VAL list ⇒ unit› where ‹from_vals_unit _ = ()›
definition vals_arity_unit :: ‹unit itself ⇒ nat› where ‹vals_arity_unit _ = 0›
instance by standard (simp_all add: vals_arity_unit_def to_vals_unit_def)
end
instantiation VAL :: FIX_ARITY_VALs begin
definition to_vals_VAL :: ‹VAL ⇒ VAL list› where ‹to_vals_VAL v = [v]›
definition from_vals_VAL :: ‹VAL list ⇒ VAL› where ‹from_vals_VAL = hd›
definition vals_arity_VAL :: ‹VAL itself ⇒ nat› where ‹vals_arity_VAL _ = 1›
instance by standard (simp_all add: to_vals_VAL_def from_vals_VAL_def vals_arity_VAL_def)
end
instantiation prod :: (FIX_ARITY_VALs, FIX_ARITY_VALs) FIX_ARITY_VALs begin
definition to_vals_prod :: ‹'a × 'b ⇒ VAL list›
where ‹to_vals_prod v = (case v of (v1,v2) ⇒ to_vals v1 @ to_vals v2)›
definition from_vals_prod :: ‹VAL list ⇒ 'a × 'b›
where ‹from_vals_prod vs = (@v. to_vals v = vs)›
definition vals_arity_prod :: ‹('a × 'b) itself ⇒ nat›
where ‹vals_arity_prod _ = vals_arity TYPE('a) + vals_arity TYPE('b)›
instance apply standard
apply (clarsimp simp add: to_vals_prod_def from_vals_prod_def)
apply (smt (verit) Eps_case_prod_eq Eps_cong append_eq_append_conv from_to_vals length_to_vals split_def)
by (clarsimp simp add: to_vals_prod_def vals_arity_prod_def)
end
instantiation list :: (VAL) VALs begin
definition to_vals_list :: ‹'a list ⇒ VAL list› where ‹to_vals_list = map to_val›
definition from_vals_list :: ‹VAL list ⇒ 'a list› where ‹from_vals_list = map from_val›
instance by standard (induct_tac x; simp add: to_vals_list_def from_vals_list_def)
end
subsection ‹Resource›
unspecified_type RES
unspecified_type RES_N
type_synonym resource = ‹RES_N ⇒ RES›
type_synonym 'T resource_entry = "(RES_N, RES, 'T) Resource_Space.kind"
setup ‹Sign.mandatory_path "RES"›
consts DOMAIN :: ‹RES_N ⇒ RES sep_homo_set›
debt_axiomatization sort: ‹OFCLASS(RES, sep_algebra_class)›
setup ‹Sign.parent_path›
instance RES :: sep_algebra using RES.sort .
setup ‹Sign.mandatory_path "RES"›
debt_axiomatization ex_RES_not_1: ‹∃r::RES. r ≠ 1›
lemma ex_two_distinct:
‹∃r1 r2 :: resource. r1 ≠ r2›
unfolding fun_eq_iff apply simp
by (meson RES.ex_RES_not_1)
setup ‹Sign.parent_path›
instantiation RES :: sep_carrier_1 begin
definition mul_carrier_RES :: ‹RES ⇒ bool› where ‹mul_carrier_RES = (λ_. True)›
instance by (standard; simp add: mul_carrier_RES_def)
end
interpretation RES: "resource_space" RES.DOMAIN .
ML_file ‹resource_space_more.ML›
ML ‹Resource_Space.define_command \<^command_keyword>‹resource_space› "extend resource semantics"›
subsection ‹Abnormal›
unspecified_type ABNM
subsection ‹All-in-One Semantics›
debt_axiomatization Well_Type :: ‹TY ⇒ VAL set›
where Well_Type_disjoint: ‹ta ≠ tb ⟹ Well_Type ta ∩ Well_Type tb = {}›
and Well_Type_poison[simp]: ‹Well_Type 𝗉𝗈𝗂𝗌𝗈𝗇 = {}›
debt_axiomatization Can_EqCompare :: ‹resource ⇒ VAL ⇒ VAL ⇒ bool›
where can_eqcmp_sym: "Can_EqCompare res A B ⟷ Can_EqCompare res B A"
consts EqCompare :: ‹VAL ⇒ VAL ⇒ bool›
debt_axiomatization Zero :: ‹TY ⇒ VAL option›
where zero_well_typ: "pred_option (λv. v ∈ Well_Type T) (Zero T)"
definition has_Zero :: ‹TY ⇒ bool›
where ‹has_Zero T ⟷ Zero T ≠ None›
lemma Zero_𝗉𝗈𝗂𝗌𝗈𝗇[simp]:
‹ Zero 𝗉𝗈𝗂𝗌𝗈𝗇 = None ›
by (metis Well_Type_poison empty_iff not_None_eq option.pred_inject(2) zero_well_typ)
lemma has_Zero_𝗉𝗈𝗂𝗌𝗈𝗇[simp]:
‹ ¬ has_Zero 𝗉𝗈𝗂𝗌𝗈𝗇 ›
unfolding has_Zero_def
by simp
lemma Well_Type_unique:
‹v ∈ Well_Type ta ⟹ v ∈ Well_Type tb ⟹ ta = tb›
using Well_Type_disjoint by blast
subsection ‹Formalization of Computation›
subsubsection ‹Explicit Annotation of Semantic Arguments and Returns›
text ‹Arguments and Returns are wrapped by φarg type.
It helps the programming framework and syntax parser to recognize which one is an argument
or a return, among values that may be used for other purposes of specification.›