Theory FJDefs

(*  Title:       A theory of Featherweight Java in Isabelle/HOL
    Author:      Nate Foster <jnfoster at cis.upenn.edu>, 
                 Dimitrios Vytiniotis <dimitriv at cis.upenn.edu>, 2006
    Maintainer:  Nate Foster <jnfoster at cis.upenn.edu>,
                 Dimitrios Vytiniotis <dimitriv at cis.upenn.edu>
    License:     LGPL
*)

section ‹{\tt FJDefs}: Basic Definitions›

theory FJDefs
imports Main
begin

subsection ‹Syntax›

text ‹We use a named representation for terms: variables, method
names, and class names, are all represented as {\tt nat}s. We use the
finite maps defined in {\tt Map.thy} to represent typing contexts and
the static class table. This section defines the representations of
each syntactic category (expressions, methods, constructors, classes,
class tables) and defines several constants ({\tt Object} and {\tt this}).
›

subsubsection‹Type definitions›

type_synonym varName = nat
type_synonym methodName = nat
type_synonym className = nat
record varDef     = 
  vdName :: "varName"
  vdType :: "className"
type_synonym varCtx = "varName  className"

subsubsection‹Constants›

definition
  Object :: "className" where
  "Object = 0"

definition
  this :: "varName" where
  "this == 0"

subsubsection ‹Expressions›

datatype exp = 
    Var "varName"
  | FieldProj "exp" "varName"              
  | MethodInvk "exp" "methodName" "exp list"
  | New "className" "exp list"
  | Cast "className" "exp"

subsubsection ‹Methods›

record methodDef = 
  mReturn :: "className"
  mName :: "methodName"
  mParams :: "varDef list"
  mBody :: "exp"


subsubsection ‹Constructors›

record constructorDef = 
  kName :: "className"
  kParams :: "varDef list"
  kSuper :: "varName list"
  kInits :: "varName list"

subsubsection ‹Classes›

record classDef = 
  cName :: "className"
  cSuper :: "className"
  cFields :: "varDef list"
  cConstructor :: "constructorDef"
  cMethods :: "methodDef list"

subsubsection ‹Class Tables›

type_synonym classTable = "className  classDef"

subsection ‹Sub-expression Relation›

text ‹The sub-expression relation, written $t \in
\mathit{subexprs}(s)$, is defined as the reflexive and transitive
closure of the immediate subexpression relation.
›

inductive_set
  isubexprs :: "(exp * exp) set" 
  and isubexprs' :: "[exp,exp]  bool"  (‹_  isubexprs'(_') [80,80] 80)
where
  "e'  isubexprs(e)  (e',e)  isubexprs"
| se_field    : "e  isubexprs(FieldProj e fi)"
| se_invkrecv : "e  isubexprs(MethodInvk e m es)"
| se_invkarg  : " ei  set es   ei  isubexprs(MethodInvk e m es)"
| se_newarg   : " ei  set es   ei  isubexprs(New C es)"
| se_cast     : "e  isubexprs(Cast C e)"

abbreviation
  subexprs :: "[exp,exp]  bool"  (‹_  subexprs'(_') [80,80] 80)  where
  "e'  subexprs(e)  (e',e)  isubexprs^*"

subsection ‹Values›

text‹A {\em value} is an expression of the form $\mathtt{new}\
\mathtt{C}(\mathit{overline{vs}})$, where $\mathit{\overline{vs}}$ is a list
of values.›

inductive
  vals :: "[exp list]  bool" (vals'(_') [80] 80)
  and val :: "[exp]  bool" (val'(_') [80] 80)
where
   vals_nil : "vals([])"
 | vals_cons : " val(vh); vals(vt)   vals((vh # vt))"
 | val : " vals(vs)   val(New C vs)"

subsection ‹Substitution›

text ‹The substitutions of a list of expressions $\mathit{ds}$ for a
list of variables $\mathit{xs}$ in another expression $e$ or a list of
expressions $\mathit{es}$ are defined in the obvious way, and written
$(\mathit{ds}/\mathit{xs})e$ and $[\mathit{ds}/\mathit{xs}]es$
respecitvely. 
›

primrec substs :: "(varName  exp)  exp  exp"
  and subst_list1 :: "(varName  exp)  exp list  exp list"
  and subst_list2 :: "(varName  exp)  exp list  exp list" where
  "substs σ (Var x) =             (case (σ(x)) of None  (Var x) | Some p  p)"
| "substs σ (FieldProj e f) =     FieldProj (substs σ e) f"
| "substs σ (MethodInvk e m es) = MethodInvk (substs σ e) m (subst_list1 σ es)"
| "substs σ (New C es) =          New C (subst_list2 σ es)"
| "substs σ (Cast C e) =          Cast C (substs σ e)"
| "subst_list1 σ [] = []"
| "subst_list1 σ (h # t) = (substs σ h) # (subst_list1 σ t)"
| "subst_list2 σ [] = []"
| "subst_list2 σ (h # t) = (substs σ h) # (subst_list2 σ t)"

abbreviation
  substs_syn :: "[exp list]  [varName list]  [exp]  exp"
    ('(_'/_')_› [80,80,80] 80) where
  "(ds/xs)e  substs (map_upds Map.empty xs ds) e"

abbreviation
  subst_list_syn :: "[exp list]  [varName list]  [exp list]  exp list"
    ('[_'/_']_› [80,80,80] 80) where
  "[ds/xs]es  map (substs (map_upds Map.empty xs ds)) es"


subsection ‹Lookup›

text ‹The fuction $\mathit{lookup}\ f\ l$ function returns an option
containing the first element of $l$ satisfying $f$, or $\mathtt{None}$
if no such element exists 
›

primrec lookup :: "'a list  ('a  bool)  'a option"
where
  "lookup [] P = None"
| "lookup (h#t) P = (if P h then Some h else lookup t P)"

primrec lookup2 :: "'a list  'b list  ('a  bool)  'b option"
where
  "lookup2 [] l2 P = None"
| "lookup2 (h1#t1) l2 P = (if P h1 then Some(hd l2) else lookup2 t1 (tl l2) P)"

subsection ‹Variable Definition Accessors›

text‹This section contains several helper functions for reading off
the names and types of variable definitions (e.g., in field
 and method parameter declarations).›

definition
  varDefs_names :: "varDef list  varName list" where
  "varDefs_names = map vdName"

definition
  varDefs_types :: "varDef list  className list" where
  "varDefs_types = map vdType"

subsection ‹Subtyping Relation›

text ‹The subtyping relation, written $\mathit{CT} \vdash C
\mathtt{\lt:} D$ is just the reflexive and transitive closure of the
immediate subclass relation. (For the sake of simplicity, we define
subtyping directly instead of using the reflexive and transitive
closure operator.) The subtyping relation is extended to lists of
classes, written $\mathit{CT} \vdash\mathtt{+} \mathit{Cs} \mathtt{\lt:}
\mathit{Ds}$.›

inductive
  subtyping :: "[classTable, className, className]  bool"  (‹_  _ <: _› [80,80,80] 80)
where
  s_refl  :  "CT  C <: C"
| s_trans : " CT  C <: D; CT  D <: E   CT  C <: E"
| s_super : " CT(C) = Some(CDef); cSuper CDef = D   CT  C <: D"

abbreviation
  neg_subtyping :: "[classTable, className, className]  bool"  (‹_  _ ¬<: _› [80,80,80] 80)
  where "CT  S ¬<: T  ¬ CT  S <: T"

inductive
  subtypings :: "[classTable, className list, className list]  bool"  (‹_ ⊢+ _ <: _› [80,80,80] 80)
where
  ss_nil  : "CT ⊢+ [] <: []"
| ss_cons : " CT  C0 <: D0; CT ⊢+ Cs <: Ds   CT ⊢+ (C0 # Cs) <: (D0 # Ds)"

subsection ‹{\tt fields} Relation›

text‹The {\tt fields} relation, written
$\mathtt{fields}(\mathit{CT},C) = \mathit{Cf}$, relates $\mathit{Cf}$
to $C$ when $\mathit{Cf}$ is the list of fields declared directly or
indirectly (i.e., by a superclass) in $C$.›

inductive
  fields :: "[classTable, className, varDef list]  bool" (fields'(_,_') = _› [80,80,80] 80)
where
  f_obj: 
  "fields(CT,Object) = []"
| f_class: 
  " CT(C) = Some(CDef); cSuper CDef = D; cFields CDef = Cf; fields(CT,D) = Dg; DgCf = Dg @ Cf  
   fields(CT,C) = DgCf"

subsection ‹{\tt mtype } Relation›

text‹The {\tt mtype} relation, written
$\mathtt{mtype}(\mathit{CT},m,C) = \mathit{Cs} \rightarrow C_0$ relates
a class $C$, method name $m$, and the arrow type $\mathit{Cs}
\rightarrow C_0$. It either returns the type of the declaration of $m$
in $C$, if any such declaration exists, and otherwise returning the
type of $m$ from $C$'s superclass.
›

inductive
  mtype :: "[classTable, methodName, className, className list, className]  bool" (mtype'(_,_,_') = _  _› [80,80,80,80] 80)
where
  mt_class: 
  " CT(C) = Some(CDef);
    lookup (cMethods CDef) (λmd.(mName md = m)) = Some(mDef);
    varDefs_types (mParams mDef) = Bs;
    mReturn mDef = B 
   mtype(CT,m,C) = Bs  B"

| mt_super: 
  " CT(C) = Some (CDef);
    lookup (cMethods CDef) (λmd.(mName md = m)) = None;
    cSuper CDef = D;
    mtype(CT,m,D) = Bs  B 
   mtype(CT,m,C) = Bs  B"

subsection ‹{\tt mbody} Relation›

text‹The {\tt mtype} relation, written
$\mathtt{mbody}(\mathit{CT},m,C) = \mathit{xs} . e_0$ relates a class
$C$, method name $m$, and the names of the parameters $\mathit{xs}$
and the body of the method $e_0$. It either returns the parameter
names and body of the declaration of $m$ in $C$, if any such
declaration exists, and otherwise the parameter names and body of $m$
from $C$'s superclass.  
›

inductive
  mbody :: "[classTable, methodName, className, varName list, exp]  bool" (mbody'(_,_,_') = _ . _› [80,80,80,80] 80)
where
  mb_class: 
  " CT(C) = Some(CDef);
     lookup (cMethods CDef) (λmd.(mName md = m)) = Some(mDef);
     varDefs_names (mParams mDef) = xs;
     mBody mDef = e 
   mbody(CT,m,C) = xs . e"

| mb_super:
  " CT(C) = Some(CDef);
     lookup (cMethods CDef) (λmd.(mName md = m)) = None;
     cSuper CDef = D;
     mbody(CT,m,D) = xs . e 
   mbody(CT,m,C) = xs . e"


subsection ‹Typing Relation›

text ‹
The typing relation, written $\mathit{CT};\Gamma \vdash e : C$
relates an expression $e$ to its type $C$, under the typing context
$\Gamma$. The multi-typing relation, written $\mathit{CT};\Gamma
\vdash\mathtt{+} \mathit{es}:\mathit{Cs}$ relates lists of expressions
to lists of types. 
›

inductive
  typings :: "[classTable, varCtx, exp list, className list]  bool" (‹_;_ ⊢+ _ : _› [80,80,80,80] 80)
  and typing :: "[classTable, varCtx, exp, className]  bool" (‹_;_  _ : _› [80,80,80,80] 80)
where
  ts_nil : "CT;Γ ⊢+ [] : []"

| ts_cons  : 
  " CT;Γ  e0 : C0; CT;Γ ⊢+ es : Cs  
   CT;Γ ⊢+ (e0 # es) : (C0 # Cs)"

| t_var : 
  " Γ(x) = Some C   CT;Γ  (Var x) : C"

| t_field : 
  " CT;Γ  e0 : C0;
     fields(CT,C0) = Cf;
     lookup Cf (λfd.(vdName fd = fi)) = Some(fDef);
     vdType fDef = Ci 
   CT;Γ  FieldProj e0 fi : Ci"

| t_invk : 
  " CT;Γ  e0 : C0;
     mtype(CT,m,C0) = Ds  C;
     CT;Γ ⊢+ es : Cs;
     CT ⊢+ Cs <: Ds;
     length es = length Ds 
   CT;Γ  MethodInvk e0 m es : C"

| t_new : 
  " fields(CT,C) = Df;
     length es = length Df;
     varDefs_types Df = Ds;
     CT;Γ ⊢+ es : Cs;
     CT ⊢+ Cs <: Ds 
   CT;Γ  New C es : C"

| t_ucast : 
  " CT;Γ  e0 : D; 
     CT  D <: C 
   CT;Γ  Cast C e0 : C"

| t_dcast : 
  " CT;Γ  e0 : D; 
     CT  C <: D; C  D 
   CT;Γ  Cast C e0 : C"

| t_scast : 
  " CT;Γ  e0 : D;
     CT  C ¬<: D;
     CT  D ¬<: C 
   CT;Γ  Cast C e0 : C"

text ‹We occasionally find the following induction principle, which
only mentions the typing of a single expression, more useful than the
mutual induction principle generated by Isabelle, which mentions the
typings of single expressions and of lists of expressions. 
›

lemma typing_induct:
  assumes "CT;Γ  e : C" (is ?T)
  and "C CT Γ x. Γ x = Some C  P CT Γ (Var x) C" 
  and "C0 CT Cf Ci Γ e0 fDef fi. CT;Γ  e0 : C0; P CT Γ e0 C0; fields(CT,C0) = Cf; lookup Cf (λfd. vdName fd = fi) = Some fDef; vdType fDef = Ci  P CT Γ (FieldProj e0 fi) Ci" 
  and "C C0 CT Cs Ds Γ e0 es m. CT;Γ  e0 : C0; P CT Γ e0 C0; mtype(CT,m,C0) = Ds  C; CT;Γ ⊢+ es : Cs; i .  i < length es    P CT Γ (es!i) (Cs!i); CT ⊢+ Cs <: Ds; length es = length Ds  P CT Γ (MethodInvk e0 m es) C"
  and "C CT Cs Df Ds Γ es. fields(CT,C) = Df; length es = length Df; varDefs_types Df = Ds; CT;Γ ⊢+ es : Cs; i.  i < length es   P CT Γ (es!i) (Cs!i); CT ⊢+ Cs <: Ds  P CT Γ (New C es) C"
  and "C CT D Γ e0. CT;Γ  e0 : D; P CT Γ e0 D; CT  D <: C  P CT Γ (Cast C e0) C" 
  and "C CT D Γ e0. CT;Γ  e0 : D; P CT Γ e0 D; CT  C <: D; C  D  P CT Γ (Cast C e0) C"
  and "C CT D Γ e0. CT;Γ  e0 : D; P CT Γ e0 D; CT  C ¬<: D; CT  D ¬<: C  P CT Γ (Cast C e0) C" 
 shows "P CT Γ e C" (is ?P)
proof -
  fix es Cs
  let ?IH="CT;Γ ⊢+ es : Cs  (i < length es.  P CT Γ (es!i) (Cs!i))"
  have "?IH  (?T  ?P)"
  proof(induct rule:typings_typing.induct)
    case (ts_nil CT Γ) show ?case by auto
  next
    case (ts_cons CT Γ e0 C0 es Cs) 
    show ?case proof
      fix i
      show "i < length (e0#es)  P CT Γ ((e0#es)!i) ((C0#Cs)!i)" using ts_cons by(cases i, auto)
    qed
  next
    case t_var then show ?case using assms by auto
  next
    case t_field then show ?case using assms by auto
  next
    case t_invk then show ?case using assms by auto
  next
    case t_new then show ?case using assms by auto
  next
    case t_ucast then show ?case using assms by auto
  next
    case t_dcast then show ?case using assms by auto
  next
    case t_scast then show ?case using assms by auto
  qed
  thus ?thesis using assms by auto
qed

subsection ‹Method Typing Relation›

text ‹A method definition $\mathit{md}$, declared in a class $C$, is
well-typed, written $\mathit{CT} \vdash \mathit{md} \texttt{OK IN}\ C$
if its body is well-typed and it has the same type (i.e., overrides)
any method with the same name declared in the superclass of $C$.›

inductive
  method_typing :: "[classTable, methodDef, className]  bool" (‹_  _ OK IN _› [80,80,80] 80)
where
m_typing:
  " CT(C) = Some(CDef);
     cName CDef = C;
     cSuper CDef = D;
     mName mDef = m;
     lookup (cMethods CDef) (λmd.(mName md = m)) = Some(mDef);
     mReturn mDef = C0; mParams mDef = Cxs; mBody mDef = e0;
     varDefs_types Cxs = Cs;
     varDefs_names Cxs = xs;
     Γ = (map_upds Map.empty xs Cs)(this  C); 
     CT;Γ  e0 : E0;
     CT  E0 <: C0;
     Ds D0. (mtype(CT,m,D) = Ds  D0)  (Cs=Ds  C0=D0) 
   CT  mDef OK IN C"

inductive
  method_typings :: "[classTable, methodDef list, className]  bool" (‹_ ⊢+ _ OK IN _› [80,80,80] 80)
where
  ms_nil : 
  "CT ⊢+ [] OK IN C"

| ms_cons : 
  " CT  m OK IN C; 
     CT ⊢+ ms OK IN C 
   CT ⊢+ (m # ms) OK IN C"


subsection ‹Class Typing Relation›

text ‹A class definition $\mathit{cd}$ is well-typed, written
$\mathit{CT}\vdash \mathit{cd} \texttt{OK}$ if its constructor
initializes each field, and all of its methods are well-typed.›

inductive
  class_typing :: "[classTable, classDef]  bool" (‹_  _ OK [80,80] 80)
where
t_class: " cName CDef = C;            
            cSuper CDef = D;
            cConstructor CDef = KDef;
            cMethods CDef = M;
            kName KDef = C;
            kParams KDef = (Dg@Cf);
            kSuper KDef = varDefs_names Dg;
            kInits KDef = varDefs_names Cf;
            fields(CT,D) = Dg;
            CT ⊢+ M OK IN C 
   CT  CDef OK"

subsection ‹Class Table Typing Relation›

text ‹A class table is well-typed, written $\mathit{CT}\
\texttt{OK}$ if for every class name $C$, the class definition mapped
to by $\mathit{CT}$ is is well-typed and has name $C$.›

inductive
  ct_typing :: "classTable  bool" (‹_ OK 80)
where
ct_all_ok: 
  " Object  dom(CT); 
     C CDef. CT(C) = Some(CDef)  (CT  CDef OK)  (cName CDef = C) 
   CT OK"

subsection ‹Evaluation Relation›

text ‹The single-step and multi-step evaluation relations are
written $\mathit{CT} \vdash e \rightarrow e'$ and $\mathit{CT} \vdash
e \rightarrow^* e'$ respectively.›

inductive
  reduction :: "[classTable, exp, exp]  bool" (‹_  _  _› [80,80,80] 80)
where

  r_field: 
  " fields(CT,C) = Cf;                   
     lookup2 Cf es (λfd.(vdName fd = fi)) = Some(ei) 
   CT  FieldProj (New C es) fi  ei"

| r_invk: 
  " mbody(CT,m,C) = xs . e0;
     substs ((map_upds Map.empty xs ds)(this  (New C es))) e0 = e0' 
   CT  MethodInvk (New C es) m ds  e0'"

| r_cast: 
  " CT  C <: D  
   CT  Cast D (New C es)  New C es"

| rc_field: 
  " CT  e0  e0'  
   CT  FieldProj e0 f  FieldProj e0' f"

| rc_invk_recv: 
  " CT  e0  e0'  
   CT  MethodInvk e0 m es  MethodInvk e0' m es"

| rc_invk_arg: 
  " CT  ei  ei' 
   CT  MethodInvk e0 m (el@ei#er)  MethodInvk e0 m (el@ei'#er)"

| rc_new_arg: 
  " CT  ei  ei'  
   CT  New C (el@ei#er)  New C (el@ei'#er)"

| rc_cast: 
  " CT  e0  e0'  
   CT  Cast C e0  Cast C e0'"

inductive
  reductions :: "[classTable, exp, exp]  bool" (‹_  _ →* _› [80,80,80] 80)
where
  rs_refl: "CT  e →* e" 
| rs_trans: " CT  e  e'; CT  e' →* e''    CT  e →* e''"

end