Theory ResourcedAdequacy
theory ResourcedAdequacy
imports ResourcedDenotational Launchbury "AList-Utils" CorrectnessResourced
begin
lemma demand_not_0: "demand (π©β¦eβ§βΟβ) β β₯"
proof
assume "demand (π©β¦eβ§βΟβ) = β₯"
with demand_suffices'[where n = 0, simplified, OF this]
have "(π©β¦eβ§βΟβ)β
β₯ β β₯" by simp
thus False by simp
qed
text βΉ
The semantics of an expression, given only @{term r} resources, will only use values from the
environment with less resources.
βΊ
lemma restr_can_restrict_env: "(π©β¦ e β§βΟβ)|βCβ
rβ = (π©β¦ e β§βΟ|β§ββrββ)|βCβ
rβ"
proof(induction e arbitrary: Ο r rule: exp_induct)
case (Var x)
show ?case
proof (rule C_restr_C_cong)
fix r'
assume "r' β r"
have "(π©β¦ Var x β§βΟβ)β
(Cβ
r') = Ο xβ
r'" by simp
also have "β¦ = ((Ο x)|βrβ)β
r'" using βΉr' β rβΊ by simp
also have "β¦ = (π©β¦ Var x β§βΟ|β§ββrββ)β
(Cβ
r')" by simp
finally show "(π©β¦ Var x β§βΟβ)β
(Cβ
r') = (π©β¦ Var x β§βΟ|β§ββrββ)β
(Cβ
r')".
qed simp
next
case (Lam x e)
show ?case
proof(rule C_restr_C_cong)
fix r'
assume "r' β r"
hence "r' β Cβ
r" by (metis below_C below_trans)
{
fix v
have "Ο(x := v)|β§ββrβ = (Ο|β§ββrβ)(x := v)|β§ββrβ"
by simp
hence "(π©β¦ e β§βΟ(x := v)β)|βr'β = (π©β¦ e β§β(Ο|β§ββrβ)(x := v)β)|βr'β"
by (subst (1 2) C_restr_eq_lower[OF Lam βΉr' β Cβ
rβΊ ]) simp
}
thus "(π©β¦ Lam [x]. e β§βΟβ)β
(Cβ
r') = (π©β¦ Lam [x]. e β§βΟ|β§ββrββ)β
(Cβ
r')"
by simp
qed simp
next
case (App e x)
show ?case
proof (rule C_restr_C_cong)
fix r'
assume "r' β r"
hence "r' β Cβ
r" by (metis below_C below_trans)
hence "(π©β¦ e β§βΟβ)β
r' = (π©β¦ e β§βΟ|β§ββrββ)β
r'"
by (rule C_restr_eqD[OF App])
thus "(π©β¦ App e x β§βΟβ)β
(Cβ
r') = (π©β¦ App e x β§βΟ|β§ββrββ)β
(Cβ
r')"
using βΉr' β rβΊ by simp
qed simp
next
case (Bool b)
show ?case by simp
next
case (IfThenElse scrut eβ©1 eβ©2)
show ?case
proof (rule C_restr_C_cong)
fix r'
assume "r' β r"
hence "r' β Cβ
r" by (metis below_C below_trans)
have "(π©β¦ scrut β§βΟβ)β
r' = (π©β¦ scrut β§βΟ|β§ββrββ)β
r'"
using βΉr' β Cβ
rβΊ by (rule C_restr_eqD[OF IfThenElse(1)])
moreover
have "(π©β¦ eβ©1 β§βΟβ)β
r' = (π©β¦ eβ©1 β§βΟ|β§ββrββ)β
r'"
using βΉr' β Cβ
rβΊ by (rule C_restr_eqD[OF IfThenElse(2)])
moreover
have "(π©β¦ eβ©2 β§βΟβ)β
r' = (π©β¦ eβ©2 β§βΟ|β§ββrββ)β
r'"
using βΉr' β Cβ
rβΊ by (rule C_restr_eqD[OF IfThenElse(3)])
ultimately
show "(π©β¦ (scrut ? eβ©1 : eβ©2) β§βΟβ)β
(Cβ
r') = (π©β¦ (scrut ? eβ©1 : eβ©2) β§βΟ|β§ββrββ)β
(Cβ
r')"
using βΉr' β rβΊ by simp
qed simp
next
case (Let Ξ e)
txt βΉThe lemma, lifted to heapsβΊ
have restr_can_restrict_env_heap : "β r. (π©β¦Ξβ¦Ο)|β§ββrβ = (π©β¦Ξβ¦Ο|β§ββrβ)|β§ββrβ"
proof(rule has_ESem.parallel_HSem_ind)
fix Οβ©1 Οβ©2 :: CEnv and r :: C
assume "Οβ©1|β§ββrβ = Οβ©2|β§ββrβ"
show " (Ο ++βdomA Ξβ βπ©β¦ Ξ ββ§βΟβ©1β)|β§ββrβ = (Ο|β§ββrβ ++βdomA Ξβ βπ©β¦ Ξ ββ§βΟβ©2β)|β§ββrβ"
proof(rule env_C_restr_cong)
fix x and r'
assume "r' β r"
hence "r' β Cβ
r" by (metis below_C below_trans)
show "(Ο ++βdomA Ξβ βπ©β¦ Ξ ββ§βΟβ©1β) xβ
r' = (Ο|β§ββrβ ++βdomA Ξβ βπ©β¦ Ξ ββ§βΟβ©2β) xβ
r'"
proof(cases "x β domA Ξ")
case True
have "(π©β¦ the (map_of Ξ x) β§βΟβ©1β)β
r' = (π©β¦ the (map_of Ξ x) β§βΟβ©1|β§ββrββ)β
r'"
by (rule C_restr_eqD[OF Let(1)[OF True] βΉr' β Cβ
rβΊ])
also have "β¦ = (π©β¦ the (map_of Ξ x) β§βΟβ©2|β§ββrββ)β
r'"
unfolding βΉΟβ©1|β§ββrβ = Οβ©2|β§ββrββΊ..
also have "β¦ = (π©β¦ the (map_of Ξ x) β§βΟβ©2β)β
r'"
by (rule C_restr_eqD[OF Let(1)[OF True] βΉr' β Cβ
rβΊ, symmetric])
finally
show ?thesis using True by (simp add: lookupEvalHeap)
next
case False
with βΉr' β rβΊ
show ?thesis by simp
qed
qed
qed simp_all
show ?case
proof (rule C_restr_C_cong)
fix r'
assume "r' β r"
hence "r' β Cβ
r" by (metis below_C below_trans)
have "(π©β¦Ξβ¦Ο)|β§ββrβ = (π©β¦Ξβ¦(Ο|β§ββrβ))|β§ββrβ"
by (rule restr_can_restrict_env_heap)
hence "(π©β¦ e β§βπ©β¦Ξβ¦Οβ)β
r' = (π©β¦ e β§βπ©β¦Ξβ¦Ο|β§ββrββ)β
r'"
by (subst (1 2) C_restr_eqD[OF Let(2) βΉr' β Cβ
rβΊ]) simp
thus "(π©β¦ Let Ξ e β§βΟβ)β
(Cβ
r') = (π©β¦ Let Ξ e β§βΟ|β§ββrββ)β
(Cβ
r')"
using βΉr' β rβΊ by simp
qed simp
qed
lemma can_restrict_env:
"(π©β¦eβ§βΟβ)β
(Cβ
r) = (π©β¦ e β§βΟ|β§ββrββ)β
(Cβ
r)"
by (rule C_restr_eqD[OF restr_can_restrict_env below_refl])
text βΉ
When an expression @{term e} terminates, then we can remove such an expression from the heap and it
still terminates. This is the crucial trick to handle black-holing in the resourced semantics.
βΊ
lemma add_BH:
assumes "map_of Ξ x = Some e"
assumes "(π©β¦eβ§βπ©β¦Ξβ¦β)β
r' β β₯"
shows "(π©β¦eβ§βπ©β¦delete x Ξβ¦β)β
r' β β₯"
proof-
obtain r where r: "Cβ
r = demand (π©β¦eβ§βπ©β¦Ξβ¦β)"
using demand_not_0 by (cases "demand (π©β¦ e β§βπ©β¦Ξβ¦β)") auto
from assms(2)
have "Cβ
r β r'" unfolding r not_bot_demand by simp
from assms(1)
have [simp]: "the (map_of Ξ x) = e" by (metis option.sel)
from assms(1)
have [simp]: "x β domA Ξ" by (metis domIff dom_map_of_conv_domA not_Some_eq)
define ub where "ub = π©β¦Ξβ¦"
have heaps: "(π©β¦Ξβ¦)|β§ββrβ β π©β¦delete x Ξβ¦" and "π©β¦Ξβ¦ β ub"
proof (induction rule: HSem_bot_ind)
fix Ο
assume "Ο|β§ββrβ β π©β¦delete x Ξβ¦"
assume "Ο β ub"
show "(βπ©β¦ Ξ ββ§βΟβ)|β§ββrβ β π©β¦delete x Ξβ¦"
proof (rule fun_belowI)
fix y
show "((βπ©β¦ Ξ ββ§βΟβ)|β§ββrβ) y β (π©β¦delete x Ξβ¦) y"
proof (cases "y = x")
case True
have "((βπ©β¦ Ξ ββ§βΟβ)|β§ββrβ) x = (π©β¦ e β§βΟβ)|βrβ"
by (simp add: lookupEvalHeap)
also have "β¦ β (π©β¦ e β§βubβ)|βrβ"
using βΉΟ β ubβΊ by (intro monofun_cfun_arg)
also have "β¦ = (π©β¦ e β§βπ©β¦Ξβ¦β)|βrβ"
unfolding ub_def..
also have "β¦ = β₯"
using r by (rule C_restr_bot_demand[OF eq_imp_below])
also have "β¦ = (π©β¦delete x Ξβ¦) x"
by (simp add: lookup_HSem_other)
finally
show ?thesis unfolding True.
next
case False
show ?thesis
proof (cases "y β domA Ξ")
case True
have "(π©β¦ the (map_of Ξ y) β§βΟβ)|βrβ = (π©β¦ the (map_of Ξ y) β§βΟ|β§ββrββ)|βrβ"
by (rule C_restr_eq_lower[OF restr_can_restrict_env below_C])
also have "β¦ β π©β¦ the (map_of Ξ y) β§βΟ|β§ββrββ"
by (rule C_restr_below)
also note βΉΟ|β§ββrβ β π©β¦delete x Ξβ¦βΊ
finally
show ?thesis
using βΉy β domA ΞβΊ βΉy β xβΊ
by (simp add: lookupEvalHeap lookup_HSem_heap)
next
case False
thus ?thesis by simp
qed
qed
qed
from βΉΟ β ubβΊ
have "(βπ©β¦ Ξ ββ§βΟβ) β (βπ©β¦ Ξ ββ§βubβ)"
by (rule cont2monofunE[rotated]) simp
also have "β¦ = ub"
unfolding ub_def HSem_bot_eq[symmetric]..
finally
show "(βπ©β¦ Ξ ββ§βΟβ) β ub".
qed simp_all
from assms(2)
have "(π©β¦eβ§βπ©β¦Ξβ¦β)β
(Cβ
r) β β₯"
unfolding r
by (rule demand_suffices[OF infinite_resources_suffice])
also
have "(π©β¦eβ§βπ©β¦Ξβ¦β)β
(Cβ
r) = (π©β¦eβ§β(π©β¦Ξβ¦)|β§ββrββ)β
(Cβ
r)"
by (rule can_restrict_env)
also
have "β¦ β (π©β¦eβ§βπ©β¦delete x Ξβ¦β)β
(Cβ
r)"
by (intro monofun_cfun_arg monofun_cfun_fun heaps )
also
have "β¦ β (π©β¦eβ§βπ©β¦delete x Ξβ¦β)β
r'"
using βΉCβ
r β r'βΊ by (rule monofun_cfun_arg)
finally
show ?thesis by this (intro cont2cont)+
qed
text βΉ
The semantics is continuous, so we can apply induction here:
βΊ
lemma resourced_adequacy:
assumes "(π©β¦eβ§βπ©β¦Ξβ¦β)β
r β β₯"
shows "β Ξ v. Ξ : e ββSβ Ξ : v"
using assms
proof(induction r arbitrary: Ξ e S rule: C.induct[case_names adm bot step])
case adm show ?case by simp
next
case bot
hence False by auto
thus ?case..
next
case (step r)
show ?case
proof(cases e rule:exp_strong_exhaust(1)[where c = "(Ξ,S)", case_names Var App Let Lam Bool IfThenElse])
case (Var x)
let ?e = "the (map_of Ξ x)"
from step.prems[unfolded Var]
have "x β domA Ξ"
by (auto intro: ccontr simp add: lookup_HSem_other)
hence "map_of Ξ x = Some ?e" by (rule domA_map_of_Some_the)
moreover
from step.prems[unfolded Var] βΉmap_of Ξ x = Some ?eβΊ βΉx β domA ΞβΊ
have "(π©β¦?eβ§βπ©β¦Ξβ¦β)β
r β β₯" by (auto simp add: lookup_HSem_heap simp del: app_strict)
hence "(π©β¦?eβ§βπ©β¦delete x Ξβ¦β)β
r β β₯" by (rule add_BH[OF βΉmap_of Ξ x = Some ?eβΊ])
from step.IH[OF this]
obtain Ξ v where "delete x Ξ : ?e ββx # Sβ Ξ : v" by blast
ultimately
have "Ξ : (Var x) ββSβ (x,v) # Ξ : v" by (rule Variable)
thus ?thesis using Var by auto
next
case (App e' x)
have "finite (set S βͺ fv (Ξ, e'))" by simp
from finite_list[OF this]
obtain S' where S': "set S' = set S βͺ fv (Ξ, e')"..
from step.prems[unfolded App]
have prem: "((π©β¦ e' β§βπ©β¦Ξβ¦β)β
r βCFn (π©β¦Ξβ¦) x|βrβ)β
r β β₯" by (auto simp del: app_strict)
hence "(π©β¦e'β§βπ©β¦Ξβ¦β)β
r β β₯" by auto
from step.IH[OF this]
obtain Ξ v where lhs': "Ξ : e' ββS'β Ξ : v" by blast
have "fv (Ξ, e') β set S'" using S' by auto
from correctness_empty_env[OF lhs' this]
have correct1: "π©β¦e'β§βπ©β¦Ξβ¦β β π©β¦vβ§βπ©β¦Ξβ¦β" and "π©β¦Ξβ¦ β π©β¦Ξβ¦" by auto
from prem
have "((π©β¦ v β§βπ©β¦Ξβ¦β)β
r βCFn (π©β¦Ξβ¦) x|βrβ)β
r β β₯"
by (rule not_bot_below_trans)(intro correct1 monofun_cfun_fun monofun_cfun_arg)
with result_evaluated[OF lhs']
have "isLam v" by (cases r, auto, cases v rule: isVal.cases, auto)
then obtain y e'' where n': "v = (Lam [y]. e'')" by (rule isLam_obtain_fresh)
with lhs'
have lhs: "Ξ : e' ββS'β Ξ : Lam [y]. e''" by simp
have "((π©β¦ v β§βπ©β¦Ξβ¦β)β
r βCFn (π©β¦Ξβ¦) x|βrβ)β
r β β₯" by fact
also have "(π©β¦Ξβ¦) x|βrβ β (π©β¦Ξβ¦) x" by (rule C_restr_below)
also note βΉv = _βΊ
also note βΉ(π©β¦Ξβ¦) β (π©β¦Ξβ¦)βΊ
also have "(π©β¦ Lam [y]. e'' β§βπ©β¦Ξβ¦β)β
r β CFnβ
(Ξ v. π©β¦e''β§β(π©β¦Ξβ¦)(y := v)β)"
by (rule CELam_no_restr)
also have "(β¦ βCFn (π©β¦Ξβ¦) x)β
r = (π©β¦e''β§β(π©β¦Ξβ¦)(y := ((π©β¦Ξβ¦) x))β)β
r" by simp
also have "β¦ = (π©β¦e''[y::=x]β§βπ©β¦Ξβ¦β)β
r"
unfolding ESem_subst..
finally
have "β¦ β β₯" by this (intro cont2cont cont_fun)+
then
obtain Ξ v' where rhs: "Ξ : e''[y::=x] ββS'β Ξ : v'" using step.IH by blast
have "Ξ : App e' x ββS'β Ξ : v'"
by (rule reds_ApplicationI[OF lhs rhs])
hence "Ξ : App e' x ββSβ Ξ : v'"
apply (rule reds_smaller_L) using S' by auto
thus ?thesis using App by auto
next
case (Lam v e')
have "Ξ : Lam [v]. e' ββSβ Ξ : Lam [v]. e'" ..
thus ?thesis using Lam by blast
next
case (Bool b)
have "Ξ : Bool b ββSβ Ξ : Bool b" by rule
thus ?thesis using Bool by blast
next
case (IfThenElse scrut eβ©1 eβ©2)
from step.prems[unfolded IfThenElse]
have prem: "CB_projectβ
((π©β¦ scrut β§βπ©β¦Ξβ¦β)β
r)β
((π©β¦ eβ©1 β§βπ©β¦Ξβ¦β)β
r)β
((π©β¦ eβ©2 β§βπ©β¦Ξβ¦β)β
r) β β₯" by (auto simp del: app_strict)
then obtain b where
is_CB: "(π©β¦ scrut β§βπ©β¦Ξβ¦β)β
r = CBβ
(Discr b)"
and not_bot2: "((π©β¦ (if b then eβ©1 else eβ©2) β§βπ©β¦Ξβ¦β)β
r) β β₯"
unfolding CB_project_not_bot by (auto split: if_splits)
have "finite (set S βͺ fv (Ξ, scrut))" by simp
from finite_list[OF this]
obtain S' where S': "set S' = set S βͺ fv (Ξ, scrut)"..
from is_CB have "(π©β¦ scrut β§βπ©β¦Ξβ¦β)β
r β β₯" by simp
from step.IH[OF this]
obtain Ξ v where lhs': "Ξ : scrut ββS'β Ξ : v" by blast
then have "isVal v" by (rule result_evaluated)
have "fv (Ξ, scrut) β set S'" using S' by simp
from correctness_empty_env[OF lhs' this]
have correct1: "π©β¦scrutβ§βπ©β¦Ξβ¦β β π©β¦vβ§βπ©β¦Ξβ¦β" and correct2: "π©β¦Ξβ¦ β π©β¦Ξβ¦" by auto
from correct1
have "(π©β¦ scrut β§βπ©β¦Ξβ¦β)β
r β (π©β¦ v β§βπ©β¦Ξβ¦β)β
r" by (rule monofun_cfun_fun)
with is_CB
have "(π©β¦ v β§βπ©β¦Ξβ¦β)β
r = CBβ
(Discr b)" by simp
with βΉisVal vβΊ
have "v = Bool b" by (cases v rule: isVal.cases) (case_tac r, auto)+
from not_bot2 βΉπ©β¦Ξβ¦ β π©β¦Ξβ¦βΊ
have "(π©β¦ (if b then eβ©1 else eβ©2) β§βπ©β¦Ξβ¦β)β
r β β₯"
by (rule not_bot_below_trans[OF _ monofun_cfun_fun[OF monofun_cfun_arg]])
from step.IH[OF this]
obtain Ξ v' where rhs: "Ξ : (if b then eβ©1 else eβ©2) ββS'β Ξ : v'" by blast
from lhs'[unfolded βΉv = _βΊ] rhs
have "Ξ : (scrut ? eβ©1 : eβ©2) ββS'β Ξ : v'" by rule
hence "Ξ : (scrut ? eβ©1 : eβ©2) ββSβ Ξ : v'"
apply (rule reds_smaller_L) using S' by auto
thus ?thesis unfolding IfThenElse by blast
next
case (Let Ξ e')
from step.prems[unfolded Let(2)]
have prem: "(π©β¦e'β§βπ©β¦Ξβ¦π©β¦Ξβ¦β)β
r β β₯"
by (simp del: app_strict)
also
have "atom ` domA Ξ β―* Ξ" using Let(1) by (simp add: fresh_star_Pair)
hence "π©β¦Ξβ¦π©β¦Ξβ¦ = π©β¦Ξ @ Ξβ¦" by (rule HSem_merge)
finally
have "(π©β¦e'β§βπ©β¦Ξ @ Ξβ¦β)β
r β β₯".
then
obtain Ξ v where "Ξ @ Ξ : e' ββSβ Ξ : v" using step.IH by blast
hence "Ξ : Let Ξ e' ββSβ Ξ : v"
by (rule reds.Let[OF Let(1)])
thus ?thesis using Let by auto
qed
qed
end