Theory Calculus_of_Programming
chapter βΉCalculus of ProgrammingβΊ
theory Calculus_of_Programming
imports Spec_Framework IDE_CP_Reasoning1
abbrevs "<state>" = "πππΊππΎ"
and "<results>" = "ππΎπππ
ππ"
and "<in>" = "ππ"
and "<is>" = "ππ"
begin
section βΉImplementing CoP SequentβΊ
text βΉCoP sequent βΉP | S |- QβΊ for βΉS = (Cβ©1,vβ©1); β― ; (Cβ©n,vβ©n)βΊ is implemented as
\begin{align*}
& βΉπΌππππΎππ sβ©0 [R] ππΎπππ
ππ ππ PβΊ, \\
& βΉCode sβ©0 sβ©1 Cβ©1 vβ©1,βΊ \\
& \qquad βΉβ―βΊ \\
& βΉCode sβ©iβ©-β©1 sβ©i Cβ©i vβ©i,βΊ \\
& \qquad βΉβ―βΊ \\
& βΉCode sβ©nβ©-β©1 sβ©n Cβ©n vβ©nβΊ \\
βΉβ’βΊ \;&\; βΉπΌππππΎππ sβ©n [R] ππΎπππ
ππ ππ QβΊ
\end{align*}
where βΉsβ©0βΊ denotes the initial state before execution and βΉsβ©i, vβ©iβΊ denote
respectively the intermediate state after executing procedure βΉCβ©iβΊ and
the return value of βΉCβ©iβΊ.
Sequence βΉ{sβ©i}β©nβΊ therefore links execution of each procedure.
βΉRβΊ is the frame variable.
[C]-modality βΉ[C]{Q}{E}βΊ is implemented by βΉππΎππ½πππ C ππ sβ©n [R] ππΎπππ
ππ ππ Q ππππππ EβΊ.
βΊ
text βΉ
In addition, besides programming of procedures,
the system is extended to deduce view shift and transformation of abstraction by programming.
The programming deduction of view shift is also realized using similar structures
(βΉCurrentConstructionβΊ). Thus we reuse the infrastructures and
give two modes βΉprogramming_modeβΊ and βΉview_shift_modeβΊ to differentiate the two modes.
βΊ
consts programming_mode :: mode
view_shift_mode :: mode
definition CurrentConstruction :: " mode β resource β assn β assn β bool "
where "CurrentConstruction mode s R S β· s β¨ INTERP_SPEC (S * R)"
abbreviation Programming_CurrentConstruction ("(2πΌππππΎππ _ [_] ππΎπππ
ππ ππ/ _)" [1000,1000,11] 10)
where βΉProgramming_CurrentConstruction β‘ CurrentConstruction programming_modeβΊ
abbreviation View_Shift_CurrentConstruction ("(2πππΎπ _ [_] ππ/ _)" [1000,1000,11] 10)
where βΉView_Shift_CurrentConstruction β‘ CurrentConstruction view_shift_modeβΊ
consts Programming_CurrentConstruction_syntax :: βΉassn β boolβΊ ("(2πΌππππΎππ πππΊππΎ:/ (βΉconsistent=trueβΊ_))" [11] 10)
consts View_Shift_CurrentConstruction_syntax :: βΉassn β boolβΊ ("(2πΌππππΎππ πππΎπ:/ _)" [11] 10)
definition PendingConstruction :: " 'ret proc
β resource
β assn
β ('ret Οarg β assn)
β (ABNM β assn)
β bool "
("ππΎππ½πππ _ ππ _ [_]/ ππΎπππ
ππ ππ _/ ππππππ _" [1000,1000,1000,11,11] 10)
where "PendingConstruction f s R S E β·
BI_lift (f s) β€ LooseState (Ξ»ret. INTERP_SPEC (S ret * R)) (Ξ»ex. INTERP_SPEC (E ex * R))"
consts PendingConstruction_syntax :: βΉ'ret proc β ('ret Οarg β assn) β (ABNM β assn) β boolβΊ
("ππΎππ½πππ ππππΌ _/ ππΎπππ
ππ ππ _/ ππππππ _" [1000,11,11] 10)
translations
"πΌππππΎππ πππΊππΎ: S" <= "CONST Programming_CurrentConstruction s R S"
"πΌππππΎππ πππΎπ: S" <= "CONST View_Shift_CurrentConstruction s R S"
"ππΎππ½πππ ππππΌ f ππΎπππ
ππ ππ S ππππππ E" <= "CONST PendingConstruction f s R S E"
definition βΉCode s s' f ret β· Success ret s' β f sβΊ
lemma CurrentConstruction_D: "CurrentConstruction mode s H T βΉ Satisfiable T"
unfolding CurrentConstruction_def Satisfiable_def
by (clarsimp simp add: INTERP_SPEC set_mult_expn, blast)
definition ToA_Construction :: βΉ'a β 'a BI β boolβΊ ("πΊπ»ππππΊπΌππππ'(_') ππ/ _" [11,11] 10)
where βΉToA_Construction = (β¨)βΊ
subsection βΉReasoning ConfigurationβΊ
subsubsection βΉSimplificationβΊ
Οreasoner_ML Οprogramming_simps (βΉπππππ
ππΏπ[programming_mode] _ : _βΊ) =
βΉfn (_, (ctxt,sequent)) => Seq.make (fn () =>
let val lev = Config.get ctxt Phi_Reasoner.auto_level
in if lev <= 0
then SOME ((ctxt, @{thm' Simplify_I} RS sequent), Seq.empty)
else sequent
|> PLPR_Simplifier.simplifier (K Seq.empty) (equip_Phi_Programming_Simp lev) {fix_vars=false} ctxt
|> Seq.map (pair ctxt)
|> Seq.pull
end)βΊ
section βΉRules for Constructing ProgramsβΊ
subsection βΉConstruct ProcedureβΊ
lemma Οapply_proc:
"(πΌππππΎππ blk [R] ππΎπππ
ππ ππ S)
βΉ ππππΌ f β¦ S βΌ T β¦ ππππππ E
βΉ(ππΎππ½πππ f ππ blk [R] ππΎπππ
ππ ππ T ππππππ E)"
unfolding ΟProcedure_def CurrentConstruction_def PendingConstruction_def bind_def Satisfaction_def
by (simp add: mult.commute)
lemma
βΉ (βs' x. Code s s' f x β§ Code s' s'' (g x) y)
β· Code s s'' (f β€ g) yβΊ
unfolding Code_def bind_def
apply (rule; clarsimp)
apply blast
by (case_tac x; clarsimp; blast)
lemma Οassemble_proc:
βΉ ππΎππ½πππ f ππ s [R] ππΎπππ
ππ ππ T ππππππ E1
βΉ (βs' ret. Code s s' f ret βΉ ππΎππ½πππ (g ret) ππ s' [R] ππΎπππ
ππ ππ U ππππππ E2)
βΉ ππΎππ½πππ (f β€ g) ππ s [R] ππΎπππ
ππ ππ U ππππππ E1 + E2βΊ
unfolding CurrentConstruction_def PendingConstruction_def bind_def less_eq_BI_iff Code_def
apply clarsimp subgoal for s s'
by (cases s; simp; cases s'; simp add: split_comp_All ring_distribs plus_fun) .
lemma Οaccept_proc:
βΉ ππΎππ½πππ f ππ s [R] ππΎπππ
ππ ππ T ππππππ E
βΉ Code s s' f ret
βΉ πΌππππΎππ s' [R] ππΎπππ
ππ ππ T retβΊ
unfolding PendingConstruction_def bind_def less_eq_BI_iff CurrentConstruction_def Code_def
by blast
lemma Οaccept_proc_optimize_return_v:
βΉ ππΎππ½πππ (Return v) ππ s [R] ππΎπππ
ππ ππ T ππππππ E
βΉ πΌππππΎππ s [R] ππΎπππ
ππ ππ T vβΊ
unfolding PendingConstruction_def bind_def less_eq_BI_iff CurrentConstruction_def Return_def det_lift_def
by simp
lemma Οreturn_additional_unit:
βΉ ππΎππ½πππ f ππ s [R] ππΎπππ
ππ ππ T ππππππ E
βΉ ππΎππ½πππ (f β€ (Ξ»v. Return (ΟV_pair v ΟV_none))) ππ s [R]
ππΎπππ
ππ ππ (Ξ»ret. T (ΟV_fst ret)) ππππππ EβΊ
unfolding CurrentConstruction_def PendingConstruction_def bind_def Return_def ΟV_pair_def
ΟV_fst_def ΟV_snd_def det_lift_def less_eq_BI_iff
apply clarsimp subgoal for s' s'' by (cases s'; simp; cases s''; simp add: ring_distribs; blast) .
lemma Οreturn:
" πΌππππΎππ s [R] ππΎπππ
ππ ππ T'
βΉ T' = T ret
βΉ ππΎππ½πππ (Return ret) ππ s [R] ππΎπππ
ππ ππ T ππππππ 0"
unfolding CurrentConstruction_def PendingConstruction_def bind_def Return_def det_lift_def less_eq_BI_iff
by simp+
lemma Οreassemble_proc_final:
"(βs H. πΌππππΎππ s [H] ππΎπππ
ππ ππ S βΉ ππΎππ½πππ g ππ s [H] ππΎπππ
ππ ππ T ππππππ E)
βΉ ππππΌ g β¦ S βΌ T β¦ ππππππ E"
unfolding CurrentConstruction_def PendingConstruction_def ΟProcedure_def bind_def split_paired_all
by (simp add: mult.commute)
lemma "Ο__Return_rule__":
βΉ X ππππΏππ Y ππππ Any
βΉ ππππΌ Return ΟV_none β¦ X βΌ Ξ»_::unit Οarg. Y β¦βΊ
unfolding ΟProcedure_def Return_def View_Shift_def less_eq_BI_iff det_lift_def
by clarsimp
subsection βΉConstruct View ShiftβΊ
lemma Οmake_view_shift:
βΉ (βs R. πππΎπ s [R] ππ S βΉ (πππΎπ s [R] ππ S' πππ»π P))
βΉ S ππππΏππ S' ππππ PβΊ
unfolding CurrentConstruction_def View_Shift_def
by (simp add: INTERP_SPEC_subj)
subsection βΉConstruct ImplicationβΊ
lemma "Οmake_implication":
βΉ(βx. πΊπ»ππππΊπΌππππ(x) ππ S βΉ πΊπ»ππππΊπΌππππ(x) ππ T πππ»π P) βΉ S πππΊπππΏππππ T ππππ PβΊ
unfolding Transformation_def ToA_Construction_def
by simp
subsection βΉCastβΊ
lemma Οapply_view_shift:
" CurrentConstruction mode blk R S
βΉ S ππππΏππ S' ππππ P
βΉ (CurrentConstruction mode blk R S') β§ P"
unfolding CurrentConstruction_def View_Shift_def
by (simp_all add: split_paired_all)
lemmas Οapply_implication = Οapply_view_shift[OF _ view_shift_by_implication]
lemma Οapply_view_shift_pending:
" PendingConstruction f blk H T E
βΉ (βx. T x ππππΏππ T' x ππππ P)
βΉ PendingConstruction f blk H T' E"
unfolding PendingConstruction_def View_Shift_def
by (clarsimp simp add: LooseState_expn' less_eq_BI_iff split_comp_All)
lemma Οapply_view_shift_pending_E:
" PendingConstruction f blk H T E
βΉ (βx. E x ππππΏππ E' x ππππ P)
βΉ PendingConstruction f blk H T E'"
unfolding PendingConstruction_def View_Shift_def
by (clarsimp simp add: LooseState_expn' less_eq_BI_iff split_comp_All)
lemmas Οapply_implication_pending =
Οapply_view_shift_pending[OF _ view_shift_by_implication]
lemmas Οapply_implication_pending_E =
Οapply_view_shift_pending_E[OF _ view_shift_by_implication]
lemma Οex_quantify_E:
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ (E ret)
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ (Ξ»e. ExBI (Ξ»x. E x e))βΊ
using Οapply_implication_pending_E[OF _ ExBI_transformation_I[OF transformation_refl]] .
lemma Οapply_implication_impl:
βΉ πΊπ»ππππΊπΌππππ(s) ππ S
βΉ S πππΊπππΏππππ S' ππππ P
βΉ(πΊπ»ππππΊπΌππππ(s) ππ S') β§ PβΊ
unfolding ToA_Construction_def Transformation_def by blast
lemma "_Οcast_internal_rule_":
" CurrentConstruction mode blk H T
βΉ T πππΊπππΏππππ T' ππππ Any
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ CurrentConstruction mode blk H T'"
unfolding Action_Tag_def
using Οapply_implication by blast
lemma "_Οcast_internal_rule_'":
" ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ E
βΉ (βv. T v πππΊπππΏππππ T' v ππππ Any)
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T' ππππππ E"
unfolding Action_Tag_def
using Οapply_implication_pending by blast
lemma "_Οcast_exception_":
" ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ E
βΉ (βv. E v πππΊπππΏππππ E' v)
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ E'"
unfolding Action_Tag_def
using Οapply_implication_pending_E by blast
lemma "_Οcast_exception_rule_":
" ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ E
βΉ (βv. E v πππΊπππΏππππ E' v)
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ T ππππππ E'"
using "_Οcast_exception_" .
lemma "_Οcast_implication_":
βΉ πΊπ»ππππΊπΌππππ(x) ππ S
βΉ S πππΊπππΏππππ T ππππ Any
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ πΊπ»ππππΊπΌππππ(x) ππ TβΊ
unfolding ToA_Construction_def Action_Tag_def Transformation_def by blast
lemma "_Οcast_proc_return_internal_rule_":
" ππππΌ f β¦ X βΌ Y β¦ ππππππ E
βΉ (βv. Y v πππΊπππΏππππ Y' v ππππ Any)
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ ππππΌ f β¦ X βΌ Y' β¦ ππππππ E"
unfolding Action_Tag_def
using ΟCONSEQ view_shift_by_implication view_shift_refl by blast
lemma "_Οcast_proc_exception_internal_rule_":
" ππππΌ f β¦ X βΌ Y β¦ ππππππ E
βΉ (βe. E e πππΊπππΏππππ E' e ππππ Any)
βΉ πSuccess
βΉ ππ»π
πππΊππππ True
βΉ ππππΌ f β¦ X βΌ Y β¦ ππππππ E'"
unfolding Action_Tag_def
using ΟCONSEQ view_shift_by_implication view_shift_refl by blast
subsection βΉFinalization RewritesβΊ
text βΉRules showing the obtained procedure is identical to the desired goal
in the end of the construction.βΊ
ML βΉstructure Proc_Monad_SS = Simpset(
val initial_ss = Simpset_Configure.Minimal_SS
val binding = SOME \<^binding>βΉprocedure_simpsβΊ
val comment = "declare the rules for simplifying procedure monad."
val attribute = NONE
val post_merging = I
)βΊ
setup βΉContext.theory_map (Proc_Monad_SS.map (
Simplifier.add_cong @{thm' mk_symbol_cong}
))βΊ
consts procedure_ss :: mode
lemmas [procedure_simps] =
proc_bind_SKIP proc_bind_SKIP'
proc_bind_assoc proc_bind_return_none ΟV_simps
Οreasoner_ML procedure_equivalence 1200 (βΉPremise procedure_ss ?PβΊ)
= βΉPhi_Reasoners.wrap (PLPR_Simplifier.simplifier_by_ss' (K Seq.empty) Proc_Monad_SS.get' {fix_vars=false}) o sndβΊ
Οreasoner_ML procedure_ss 1000 (βΉSimplify procedure_ss ?x ?yβΊ)
= βΉPhi_Reasoners.wrap (PLPR_Simplifier.simplifier_by_ss' (K Seq.empty) Proc_Monad_SS.get' {fix_vars=true}) o sndβΊ
subsection βΉMiscβΊ
paragraph βΉInhabitanceβΊ
lemma ToA_Construction_Satisfiable_rule:
βΉπΊπ»ππππΊπΌππππ(x) ππ S βΉ Satisfiable SβΊ
unfolding ToA_Construction_def Satisfiable_def by blast
lemma CurrentConstruction_Satisfiable_rule:
"CurrentConstruction mode s H T βΉ Satisfiable T"
using CurrentConstruction_D by blast
paragraph βΉFact StoreβΊ
lemma [Οprogramming_simps]:
"CurrentConstruction mode s H (T πππ»π P) β· (CurrentConstruction mode s H T) β§ P"
unfolding CurrentConstruction_def
by (simp_all add: INTERP_SPEC_subj split_paired_all)
lemma [Οprogramming_simps]:
"(CurrentConstruction mode s H T β§ B) β§ C β· (CurrentConstruction mode s H T) β§ (B β§ C)"
by simp
lemma [Οprogramming_simps]:
βΉ(πΊπ»ππππΊπΌππππ(x) ππ T πππ»π P) β· (πΊπ»ππππΊπΌππππ(x) ππ T) β§ PβΊ
unfolding ToA_Construction_def by simp
lemma [Οprogramming_simps]:
βΉ((πΊπ»ππππΊπΌππππ(x) ππ T) β§ B) β§ C β· (πΊπ»ππππΊπΌππππ(x) ππ T) β§ (B β§ C)βΊ
by simp
paragraph βΉFixing Existentially Quantified VariableβΊ
lemma ΟExTyp_strip:
"(CurrentConstruction mode p H (β*c. T c)) β‘ (βc. CurrentConstruction mode p H (T c))"
unfolding CurrentConstruction_def atomize_eq
by (simp_all add: INTERP_SPEC_ex split_paired_all)
lemma ΟExTyp_strip_imp:
βΉToA_Construction s (β*c. T c) β‘ (βc. ToA_Construction s (T c))βΊ
unfolding ToA_Construction_def by simp
paragraph βΉIntroducing Existential QuantificationβΊ
lemma introduce_Ex:
βΉCurrentConstruction mode blk H (S x) βΉ CurrentConstruction mode blk H (ExBI S)βΊ
using Οapply_implication[OF _ ExBI_transformation_I[OF transformation_refl], THEN conjunct1] .
lemma introduce_Ex_subj:
βΉCurrentConstruction mode blk H (S x πππ»π Q) βΉ CurrentConstruction mode blk H (ExBI S πππ»π Q)βΊ
by (metis Subjection_True Subjection_cong introduce_Ex)
lemma introduce_Ex_pending:
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ (Ξ»v. Q x v) ππππππ E
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ (Ξ»v. β*x. Q x v) ππππππ EβΊ
using Οapply_implication_pending[OF _ ExBI_transformation_I[OF transformation_refl]] .
lemma introduce_Ex_pending_E:
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ Q ππππππ (Ξ»v. E x v)
βΉ ππΎππ½πππ f ππ blk [H] ππΎπππ
ππ ππ Q ππππππ (Ξ»v. β*x. E x v)βΊ
using Οapply_implication_pending_E[OF _ ExBI_transformation_I[OF transformation_refl]] .
lemma introduce_Ex_ToA:
βΉ ToA_Construction s (S x)
βΉ ToA_Construction s (ExBI S) βΊ
using ΟExTyp_strip_imp by fastforce
lemma introduce_Ex_ToA_subj:
βΉ ToA_Construction s (S x πππ»π Q)
βΉ ToA_Construction s (ExBI S πππ»π Q) βΊ
by (metis (full_types) Subjection_Flase Subjection_True introduce_Ex_ToA)
lemma introduce_Ex_ToA_subj_P:
βΉ ToA_Construction s (X πππ»π S x)
βΉ ToA_Construction s (X πππ»π Ex S) βΊ
by (metis Subjection_expn ToA_Construction_def)
paragraph βΉReturnβΊ
lemma ΟM_Success[intro!]:
βΉ v β¨ (y β¦ T)
βΉ ππππΌ Return (Οarg v) β¦ X βΌ Ξ»u. y β¦ Val u Tβ X β¦ ππππππ Any βΊ
unfolding ΟProcedure_def det_lift_def Return_def
by (clarsimp simp add: Val_def ΟType_def less_eq_BI_iff)
lemma ΟM_Success_P:
βΉ v β¨ (y β¦ T)
βΉ P (Οarg v)
βΉ ππππΌ Return (Οarg v) β¦ X βΌ Ξ»u. y β¦ Val u Tβ X πππ»π P u β¦ ππππππ Any βΊ
unfolding ΟProcedure_def det_lift_def Return_def
by (clarsimp simp add: Val_def ΟType_def INTERP_SPEC_subj less_eq_BI_iff)
declare ΟM_Success[where X=1, simplified, intro!]
lemma ΟM_Success'[intro!]:
βΉ ππππΌ Return vs β¦ X vs βΌ X β¦ ππππππ Any βΊ
unfolding Return_def ΟProcedure_def det_lift_def less_eq_BI_iff by clarsimp
hide_const (open) Code
end