Theory TypeRel
section ‹Relations between Java Types›
theory TypeRel
imports Decl
begin
inductive_set
subcls1 :: "'c prog => (cname × cname) set"
and subcls1' :: "'c prog => cname ⇒ cname => bool" ("_ ⊢ _ ≺C1 _" [71,71,71] 70)
for G :: "'c prog"
where
"G ⊢ C ≺C1 D ≡ (C, D) ∈ subcls1 G"
| subcls1I: "⟦class G C = Some (D,rest); C ≠ Object⟧ ⟹ G ⊢ C ≺C1 D"
abbreviation
subcls :: "'c prog => cname ⇒ cname => bool" ("_ ⊢ _ ≼C _" [71,71,71] 70)
where "G ⊢ C ≼C D ≡ (C, D) ∈ (subcls1 G)⇧*"
lemma subcls1D:
"G⊢C≺C1D ⟹ C ≠ Object ∧ (∃fs ms. class G C = Some (D,fs,ms))"
apply (erule subcls1.cases)
apply auto
done
lemma subcls1_def2:
"subcls1 P =
(SIGMA C:{C. is_class P C}. {D. C≠Object ∧ fst (the (class P C))=D})"
by (auto simp add: is_class_def dest: subcls1D intro: subcls1I)
lemma finite_subcls1: "finite (subcls1 G)"
apply(simp add: subcls1_def2 del: mem_Sigma_iff)
apply(rule finite_SigmaI [OF finite_is_class])
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
apply auto
done
lemma subcls_is_class: "(C, D) ∈ (subcls1 G)⇧+ ⟹ is_class G C"
apply (unfold is_class_def)
apply(erule trancl_trans_induct)
apply (auto dest!: subcls1D)
done
lemma subcls_is_class2 [rule_format (no_asm)]:
"G⊢C≼C D ⟹ is_class G D ⟶ is_class G C"
apply (unfold is_class_def)
apply (erule rtrancl_induct)
apply (drule_tac [2] subcls1D)
apply auto
done
definition class_rec :: "'c prog ⇒ cname ⇒ 'a ⇒
(cname ⇒ fdecl list ⇒ 'c mdecl list ⇒ 'a ⇒ 'a) ⇒ 'a" where
"class_rec G == wfrec ((subcls1 G)¯)
(λr C t f. case class G C of
None ⇒ undefined
| Some (D,fs,ms) ⇒
f C fs ms (if C = Object then t else r D t f))"
lemma class_rec_lemma:
assumes wf: "wf ((subcls1 G)¯)"
and cls: "class G C = Some (D, fs, ms)"
shows "class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
by (subst wfrec_def_adm[OF class_rec_def])
(auto simp: assms adm_wf_def fun_eq_iff subcls1I split: option.split)
definition
"wf_class G = wf ((subcls1 G)¯)"
text ‹Code generator setup›
code_pred
(modes: i ⇒ i ⇒ o ⇒ bool, i ⇒ i ⇒ i ⇒ bool)
subcls1p
.
declare subcls1_def [code_pred_def]
code_pred
(modes: i ⇒ i × o ⇒ bool, i ⇒ i × i ⇒ bool)
[inductify]
subcls1
.
definition subcls' where "subcls' G = (subcls1p G)⇧*⇧*"
code_pred
(modes: i ⇒ i ⇒ i ⇒ bool, i ⇒ i ⇒ o ⇒ bool)
[inductify]
subcls'
.
lemma subcls_conv_subcls' [code_unfold]:
"(subcls1 G)⇧* = {(C, D). subcls' G C D}"
by(simp add: subcls'_def subcls1_def rtrancl_def)
lemma class_rec_code [code]:
"class_rec G C t f =
(if wf_class G then
(case class G C of
None ⇒ class_rec G C t f
| Some (D, fs, ms) ⇒
if C = Object then f Object fs ms t else f C fs ms (class_rec G D t f))
else class_rec G C t f)"
apply(cases "wf_class G")
apply(unfold class_rec_def wf_class_def)
apply(subst wfrec, assumption)
apply(cases "class G C")
apply(simp add: wfrec)
apply clarsimp
apply(rename_tac D fs ms)
apply(rule_tac f="f C fs ms" in arg_cong)
apply(clarsimp simp add: cut_def)
apply(blast intro: subcls1I)
apply simp
done
lemma wf_class_code [code]:
"wf_class G ⟷ (∀(C, rest) ∈ set G. C ≠ Object ⟶ ¬ G ⊢ fst (the (class G C)) ≼C C)"
proof
assume "wf_class G"
hence wf: "wf (((subcls1 G)⇧+)¯)" unfolding wf_class_def by(rule wf_converse_trancl)
hence acyc: "acyclic ((subcls1 G)⇧+)" by(auto dest: wf_acyclic)
show "∀(C, rest) ∈ set G. C ≠ Object ⟶ ¬ G ⊢ fst (the (class G C)) ≼C C"
proof(safe)
fix C D fs ms
assume "(C, D, fs, ms) ∈ set G"
and "C ≠ Object"
and subcls: "G ⊢ fst (the (class G C)) ≼C C"
from ‹(C, D, fs, ms) ∈ set G› obtain D' fs' ms'
where "class": "class G C = Some (D', fs', ms')"
unfolding class_def by(auto dest!: weak_map_of_SomeI)
hence "G ⊢ C ≺C1 D'" using ‹C ≠ Object› ..
hence *: "(C, D') ∈ (subcls1 G)⇧+" ..
also from * acyc have "C ≠ D'" by(auto simp add: acyclic_def)
with subcls "class" have "(D', C) ∈ (subcls1 G)⇧+" by(auto dest: rtranclD)
finally show False using acyc by(auto simp add: acyclic_def)
qed
next
assume rhs[rule_format]: "∀(C, rest) ∈ set G. C ≠ Object ⟶ ¬ G ⊢ fst (the (class G C)) ≼C C"
have "acyclic (subcls1 G)"
proof(intro acyclicI strip notI)
fix C
assume "(C, C) ∈ (subcls1 G)⇧+"
thus False
proof(cases)
case base
then obtain rest where "class G C = Some (C, rest)"
and "C ≠ Object" by cases
from ‹class G C = Some (C, rest)› have "(C, C, rest) ∈ set G"
unfolding class_def by(rule map_of_SomeD)
with ‹C ≠ Object› ‹class G C = Some (C, rest)›
have "¬ G ⊢ C ≼C C" by(auto dest: rhs)
thus False by simp
next
case (step D)
from ‹G ⊢ D ≺C1 C› obtain rest where "class G D = Some (C, rest)"
and "D ≠ Object" by cases
from ‹class G D = Some (C, rest)› have "(D, C, rest) ∈ set G"
unfolding class_def by(rule map_of_SomeD)
with ‹D ≠ Object› ‹class G D = Some (C, rest)›
have "¬ G ⊢ C ≼C D" by(auto dest: rhs)
moreover from ‹(C, D) ∈ (subcls1 G)⇧+›
have "G ⊢ C ≼C D" by(rule trancl_into_rtrancl)
ultimately show False by contradiction
qed
qed
thus "wf_class G" unfolding wf_class_def
by(rule finite_acyclic_wf_converse[OF finite_subcls1])
qed
definition "method" :: "'c prog × cname => (sig ⇀ cname × ty × 'c)"
where [code]: "method ≡ λ(G,C). class_rec G C Map.empty (λC fs ms ts.
ts ++ map_of (map (λ(s,m). (s,(C,m))) ms))"
definition fields :: "'c prog × cname => ((vname × cname) × ty) list"
where [code]: "fields ≡ λ(G,C). class_rec G C [] (λC fs ms ts.
map (λ(fn,ft). ((fn,C),ft)) fs @ ts)"
definition field :: "'c prog × cname => (vname ⇀ cname × ty)"
where [code]: "field == map_of o (map (λ((fn,fd),ft). (fn,(fd,ft)))) o fields"
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)¯)|] ==>
method (G,C) = (if C = Object then Map.empty else method (G,D)) ++
map_of (map (λ(s,m). (s,(C,m))) ms)"
apply (unfold method_def)
apply (simp split del: if_split)
apply (erule (1) class_rec_lemma [THEN trans])
apply auto
done
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)¯)|] ==>
fields (G,C) =
map (λ(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
apply (unfold fields_def)
apply (simp split del: if_split)
apply (erule (1) class_rec_lemma [THEN trans])
apply auto
done
lemma field_fields:
"field (G,C) fn = Some (fd, fT) ⟹ map_of (fields (G,C)) (fn, fd) = Some fT"
apply (unfold field_def)
apply (rule table_of_remap_SomeD)
apply simp
done
inductive
widen :: "'c prog => [ty , ty ] => bool" ("_ ⊢ _ ≼ _" [71,71,71] 70)
for G :: "'c prog"
where
refl [intro!, simp]: "G⊢ T ≼ T"
| subcls : "G⊢C≼C D ==> G⊢Class C ≼ Class D"
| null [intro!]: "G⊢ NT ≼ RefT R"
code_pred widen .
lemmas refl = HOL.refl
inductive
cast :: "'c prog => [ty , ty ] => bool" ("_ ⊢ _ ≼? _" [71,71,71] 70)
for G :: "'c prog"
where
widen: "G⊢ C≼ D ==> G⊢C ≼? D"
| subcls: "G⊢ D≼C C ==> G⊢Class C ≼? Class D"
lemma widen_PrimT_RefT [iff]: "(G⊢PrimT pT≼RefT rT) = False"
apply (rule iffI)
apply (erule widen.cases)
apply auto
done
lemma widen_RefT: "G⊢RefT R≼T ==> ∃t. T=RefT t"
apply (ind_cases "G⊢RefT R≼T")
apply auto
done
lemma widen_RefT2: "G⊢S≼RefT R ==> ∃t. S=RefT t"
apply (ind_cases "G⊢S≼RefT R")
apply auto
done
lemma widen_Class: "G⊢Class C≼T ==> ∃D. T=Class D"
apply (ind_cases "G⊢Class C≼T")
apply auto
done
lemma widen_Class_NullT [iff]: "(G⊢Class C≼NT) = False"
apply (rule iffI)
apply (ind_cases "G⊢Class C≼NT")
apply auto
done
lemma widen_Class_Class [iff]: "(G⊢Class C≼ Class D) = (G⊢C≼C D)"
apply (rule iffI)
apply (ind_cases "G⊢Class C ≼ Class D")
apply (auto elim: widen.subcls)
done
lemma widen_NT_Class [simp]: "G ⊢ T ≼ NT ⟹ G ⊢ T ≼ Class D"
by (ind_cases "G ⊢ T ≼ NT", auto)
lemma cast_PrimT_RefT [iff]: "(G⊢PrimT pT≼? RefT rT) = False"
apply (rule iffI)
apply (erule cast.cases)
apply auto
done
lemma cast_RefT: "G ⊢ C ≼? Class D ⟹ ∃ rT. C = RefT rT"
apply (erule cast.cases)
apply simp apply (erule widen.cases)
apply auto
done
theorem widen_trans[trans]: "⟦G⊢S≼U; G⊢U≼T⟧ ⟹ G⊢S≼T"
proof -
assume "G⊢S≼U" thus "⋀T. G⊢U≼T ⟹ G⊢S≼T"
proof induct
case (refl T T') thus "G⊢T≼T'" .
next
case (subcls C D T)
then obtain E where "T = Class E" by (blast dest: widen_Class)
with subcls show "G⊢Class C≼T" by auto
next
case (null R RT)
then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
thus "G⊢NT≼RT" by auto
qed
qed
end