Theory TypeRel

Up to index of Isabelle/HOL/SafeImp

theory TypeRel
imports Decl
begin

(*  Title:      HOL/MicroJava/J/TypeRel.thy
    ID:         $Id: TypeRel.thy,v 1.33 2007/07/11 09:32:11 berghofe Exp $
    Author:     David von Oheimb
    Copyright   1999 Technische Universitaet Muenchen
*)

header {* \isaheader{Relations between Java Types} *}

theory TypeRel imports Decl begin

-- "direct subclass, cf. 8.1.3"
inductive
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
  for G :: "'c prog"
where
  subcls1I: "[|class G C = Some (D,rest); C ≠ Object|] ==> G\<turnstile>C\<prec>C1D"

abbreviation
  subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
  where "G\<turnstile>C \<preceq>C  D ≡ (subcls1 G)^** C D"
 

lemma subcls1D: 
  "G\<turnstile>C\<prec>C1D ==> C ≠ Object ∧ (∃fs ms. class G C = Some (D,fs,ms))"
apply (erule subcls1.cases)
apply auto
done

lemma subcls1_def2: 
  "subcls1 G = (λC D. (C, D) ∈
     (SIGMA C: {C. is_class G C} . {D. C≠Object ∧ fst (the (class G C))=D}))"
  by (auto simp add: is_class_def expand_fun_eq dest: subcls1D intro: subcls1I)

lemma finite_subcls1: "finite {(C, D). subcls1 G C D}"
apply(simp add: subcls1_def2 del: mem_Sigma_iff)
thm finite_SigmaI
apply(rule finite_SigmaI [OF finite_is_class])
thm finite_subset
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
apply  auto
done

lemma subcls_is_class: "(subcls1 G)^++ C D ==> is_class G C"
apply (unfold is_class_def)
apply(erule tranclp_trans_induct)
apply (auto dest!: subcls1D)
done

lemma subcls_is_class2 [rule_format (no_asm)]: 
  "G\<turnstile>C\<preceq>C D ==> is_class G D --> is_class G C"
apply (unfold is_class_def)
apply (erule rtranclp_induct)
apply  (drule_tac [2] subcls1D)
apply  auto
done


constdefs
  class_rec :: "'c prog => cname => 'a =>
    (cname => fdecl list => 'c mdecl list => 'a 
           => 'a) => 'a"
  "class_rec G == wfrec {(C, D). (subcls1 G)^--1 C D}
    (λr C t f. case class G C of
         None => arbitrary
       | Some (D,fs,ms) => 
           f C fs ms (if C = Object then t else r D t f))"

lemma class_rec_lemma: "wfP ((subcls1 G)^--1) ==> class G C = Some (D,fs,ms) ==>
 class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
  by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
    cut_apply [where r="{(C, D). subcls1 G D C}", simplified, OF subcls1I])



definition
  "wf_class G = wfP ((subcls1 G)^--1)"

lemma class_rec_func [code func]:
  "class_rec G C t f = (if wf_class G then
    (case class G C
      of None => arbitrary
       | Some (D, fs, ms) => f C fs ms (if C = Object then t else class_rec G D t f))
    else class_rec G C t f)"
proof (cases "wf_class G")
  case False then show ?thesis by auto
next
  case True
  from `wf_class G` have wf: "wfP ((subcls1 G)^--1)"
    unfolding wf_class_def .
  show ?thesis
  proof (cases "class G C")
    case None
    with wf show ?thesis
      by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
        cut_apply [where r="{(C, D).subcls1 G D C}", simplified, OF subcls1I])
  next
    case (Some x) show ?thesis
    proof (cases x)
      case (fields D fs ms)
      then have is_some: "class G C = Some (D, fs, ms)" using Some by simp
      note class_rec = class_rec_lemma [OF wf is_some]
      show ?thesis unfolding class_rec by (simp add: is_some)
    qed
  qed
qed

consts

  method  :: "'c prog × cname => ( sig   \<rightharpoonup> cname × ty × 'c)" (* ###curry *)
  method' :: "'c prog × cname => ( sig   \<rightharpoonup> cname × ty × 'c)" (* ###curry *)
  field   :: "'c prog × cname => ( vname \<rightharpoonup> cname × ty     )" (* ###curry *)
  fields  :: "'c prog × cname => ((vname × cname) × ty) list" (* ###curry *)

-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"

defs method_def: "method ≡ λ(G,C). class_rec G C empty (λC fs ms ts.
                           ts ++ map_of (map (λ(s,m). (s,(C,m))) ms))"

-- "methods of a class, with out inheritance, overriding and hiding"
defs method'_def: "method' ≡ λ(G,C). map_of (map (λ(s,m). (s,(C,m))) 
                                             (snd (snd (the (class G C)))))"


lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
  map_of (map (λ(s,m). (s,(C,m))) ms)"
apply (unfold method_def)
apply (simp split del: split_if)
apply (erule (1) class_rec_lemma [THEN trans]);
apply auto
done


-- "list of fields of a class, including inherited and hidden ones"
defs fields_def: "fields ≡ λ(G,C). class_rec G C []    (λC fs ms ts.
                           map (λ(fn,ft). ((fn,C),ft)) fs @ ts)"

lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
 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: split_if)
apply (erule (1) class_rec_lemma [THEN trans]);
apply auto
done


defs field_def: "field == map_of o (map (λ((fn,fd),ft). (fn,(fd,ft)))) o fields"

lemma table_of_remap_SomeD [rule_format (no_asm)]: 
"map_of (map (λ((k,k'),x). (k,(k',x))) t) k = Some (k',x) --> 
 map_of t (k, k') = Some x"
apply (induct_tac "t")
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


-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
inductive
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
  for G :: "'c prog"
where
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"

lemmas refl = HOL.refl

-- "casting conversion, cf. 5.5 / 5.1.5"
-- "left out casts on primitve types"
inductive
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
  for G :: "'c prog"
where
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"


lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
apply (rule iffI)
apply (erule widen.cases)
apply auto
done

lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> ∃t. T=RefT t"
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
apply auto
done

lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> ∃t. S=RefT t"
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
apply auto
done

lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> ∃D. T=Class D"
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
apply auto
done

lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
apply (rule iffI)
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
apply auto
done

lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
apply (rule iffI)
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
apply (auto elim: widen.subcls)
done

lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT ==> G \<turnstile> T \<preceq> Class D"
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)

lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
apply (rule iffI)
apply (erule cast.cases)
apply auto
done

lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D ==> ∃ rT. C = RefT rT"
apply (erule cast.cases)
apply simp apply (erule widen.cases) 
apply auto
done

theorem widen_trans[trans]: "[|G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T|] ==> G\<turnstile>S\<preceq>T"
proof -
  assume "G\<turnstile>S\<preceq>U" thus "!!T. G\<turnstile>U\<preceq>T ==> G\<turnstile>S\<preceq>T"
  proof induct
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
  next
    case (subcls C D T)
    then obtain E where "T = Class E" by (blast dest: widen_Class)
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
  next
    case (null R RT)
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
    thus "G\<turnstile>NT\<preceq>RT" by auto
  qed
qed

end

lemma subcls1D:

  G \<turnstile> C \<prec>C1 D
  ==> C  Object ∧ (∃fs ms. class G C = Some (D, fs, ms))

lemma subcls1_def2:

  subcls1 G =
  (λC D. (C, D)
         ∈ (SIGMA C:{C. is_class G C}.
               {D. C  Object ∧ fst (the (class G C)) = D}))

lemma finite_subcls1:

  finite {(C, D). G \<turnstile> C \<prec>C1 D}

lemma subcls_is_class:

  (subcls1 G)++ C D ==> is_class G C

lemma subcls_is_class2:

  [| G \<turnstile> C \<preceq>C D; is_class G D |] ==> is_class G C

lemma class_rec_lemma:

  [| wfP (subcls1 G)^--1; class G C = Some (D, fs, ms) |]
  ==> class_rec G C t f = f C fs ms (if C = Object then t else class_rec G D t f)

lemma class_rec_func:

  class_rec G C t f =
  (if wf_class G
   then case class G C of None => arbitrary
        | Some (D, fs, ms) =>
            f C fs ms (if C = Object then t else class_rec G D t f)
   else class_rec G C t f)

lemma method_rec_lemma:

  [| class G C = Some (D, fs, ms); wfP (subcls1 G)^--1 |]
  ==> method (G, C) =
      (if C = Object then empty else method (G, D)) ++
      map_of (map (λ(s, m). (s, C, m)) ms)

lemma fields_rec_lemma:

  [| class G C = Some (D, fs, ms); wfP (subcls1 G)^--1 |]
  ==> fields (G, C) =
      map (λ(fn, ft). ((fn, C), ft)) fs @
      (if C = Object then [] else fields (G, D))

lemma table_of_remap_SomeD:

  map_of (map (λ((k, k'), x). (k, k', x)) t) k = Some (k', x)
  ==> map_of t (k, k') = Some x

lemma field_fields:

  TypeRel.field (G, C) fn = Some (fd, fT)
  ==> map_of (fields (G, C)) (fn, fd) = Some fT

lemma refl:

  t = t

lemma widen_PrimT_RefT:

  G \<turnstile> PrimT pT \<preceq> RefT rT = False

lemma widen_RefT:

  G \<turnstile> RefT R \<preceq> T ==> ∃t. T = RefT t

lemma widen_RefT2:

  G \<turnstile> S \<preceq> RefT R ==> ∃t. S = RefT t

lemma widen_Class:

  G \<turnstile> Class C \<preceq> T ==> ∃D. T = Class D

lemma widen_Class_NullT:

  G \<turnstile> Class C \<preceq> NT = False

lemma widen_Class_Class:

  G \<turnstile> Class C \<preceq> Class D = G \<turnstile> C \<preceq>C D

lemma widen_NT_Class:

  G \<turnstile> T \<preceq> NT ==> G \<turnstile> T \<preceq> Class D

lemma cast_PrimT_RefT:

  G \<turnstile> PrimT pT \<preceq>? RefT rT = False

lemma cast_RefT:

  G \<turnstile> C \<preceq>? Class D ==> ∃rT. C = RefT rT

theorem widen_trans:

  [| G \<turnstile> S \<preceq> U; G \<turnstile> U \<preceq> T |]
  ==> G \<turnstile> S \<preceq> T