src/HOL/Isar_examples/W_correct.thy
changeset 7800 8ee919e42174
parent 7761 7fab9592384f
child 7809 c3e6f27bfcb7
equal deleted inserted replaced
7799:4c69318e6a6d 7800:8ee919e42174
     4 
     4 
     5 Correctness of Milner's type inference algorithm W (let-free version).
     5 Correctness of Milner's type inference algorithm W (let-free version).
     6 Based upon HOL/W0 by Dieter Nazareth and Tobias Nipkow.
     6 Based upon HOL/W0 by Dieter Nazareth and Tobias Nipkow.
     7 *)
     7 *)
     8 
     8 
     9 header {* Correctness of Milner's type inference algorithm~W (let-free version) *};
     9 header {* Milner's type inference algorithm~W (let-free version) *};
    10 
    10 
    11 theory W_correct = Main + Type:;
    11 theory W_correct = Main + Type:;
    12 
    12 
    13 
    13 
    14 subsection "Mini ML with type inference rules";
    14 subsection "Mini ML with type inference rules";
    45     fix a n;
    45     fix a n;
    46     assume "n < length a";
    46     assume "n < length a";
    47     hence "n < length (map ($ s) a)"; by simp;
    47     hence "n < length (map ($ s) a)"; by simp;
    48     hence "map ($ s) a |- Var n :: map ($ s) a ! n";
    48     hence "map ($ s) a |- Var n :: map ($ s) a ! n";
    49       by (rule has_type.VarI);
    49       by (rule has_type.VarI);
    50     also; have "map ($ s) a ! n = $ s (a ! n)"; by (rule nth_map);
    50     also; have "map ($ s) a ! n = $ s (a ! n)";
    51     also; have "map ($ s) a = $ s a"; by (simp only: app_subst_list);   (* FIXME unfold fails!? *)
    51       by (rule nth_map);
       
    52     also; have "map ($ s) a = $ s a";
       
    53       by (simp only: app_subst_list);   (* FIXME unfold fails!? *)
    52     finally; show "?P a (Var n) (a ! n)"; .;
    54     finally; show "?P a (Var n) (a ! n)"; .;
    53   next;
    55   next;
    54     fix a e t1 t2;
    56     fix a e t1 t2;
    55     assume "?P (t1 # a) e t2";
    57     assume "?P (t1 # a) e t2";
    56     hence "$ s t1 # map ($ s) a |- e :: $ s t2";
    58     hence "$ s t1 # map ($ s) a |- e :: $ s t2";
   112     fix e1 e2; assume hyp1: "?P e1" and hyp2: "?P e2";
   114     fix e1 e2; assume hyp1: "?P e1" and hyp2: "?P e2";
   113     show "?P (App e1 e2)";
   115     show "?P (App e1 e2)";
   114     proof (intro allI impI);
   116     proof (intro allI impI);
   115       fix a s t m n; assume "Ok (s, t, m) = W (App e1 e2) a n";
   117       fix a s t m n; assume "Ok (s, t, m) = W (App e1 e2) a n";
   116       hence "EX s1 t1 n1 s2 t2 n2 u.
   118       hence "EX s1 t1 n1 s2 t2 n2 u.
   117         s = $ u o $ s2 o s1 & t = u n2 &
   119           s = $ u o $ s2 o s1 & t = u n2 &
   118         mgu ($ s2 t1) (t2 -> TVar n2) = Ok u &
   120           mgu ($ s2 t1) (t2 -> TVar n2) = Ok u &
   119            W e2 ($ s1 a) n1 = Ok (s2, t2, n2) &
   121              W e2 ($ s1 a) n1 = Ok (s2, t2, n2) &
   120            W e1 a n = Ok (s1, t1, n1)"; by (rule rev_mp) (simp, force); (* FIXME force fails !??*)
   122              W e1 a n = Ok (s1, t1, n1)";
       
   123         by (rule rev_mp) (simp, force); (* FIXME force fails !??*)
   121       thus "$ s a |- App e1 e2 :: t";
   124       thus "$ s a |- App e1 e2 :: t";
   122       proof (elim exE conjE);
   125       proof (elim exE conjE);
   123         fix s1 t1 n1 s2 t2 n2 u;
   126         fix s1 t1 n1 s2 t2 n2 u;
   124         assume s: "s = $ u o $ s2 o s1"
   127         assume s: "s = $ u o $ s2 o s1"
   125           and t: "t = u n2"
   128           and t: "t = u n2"
   130         proof (rule has_type.AppI);
   133         proof (rule has_type.AppI);
   131           from s; have s': "$ u ($ s2 ($ s1 a)) = $s a";
   134           from s; have s': "$ u ($ s2 ($ s1 a)) = $s a";
   132             by (simp add: subst_comp_tel o_def);
   135             by (simp add: subst_comp_tel o_def);
   133           show "$s a |- e1 :: $ u t2 -> t";
   136           show "$s a |- e1 :: $ u t2 -> t";
   134           proof -;
   137           proof -;
   135             from hyp1 W1_ok [RS sym]; have "$ s1 a |- e1 :: t1"; by blast;
   138             from hyp1 W1_ok [RS sym]; have "$ s1 a |- e1 :: t1";
       
   139               by blast;
   136             hence "$ u ($ s2 ($ s1 a)) |- e1 :: $ u ($ s2 t1)";
   140             hence "$ u ($ s2 ($ s1 a)) |- e1 :: $ u ($ s2 t1)";
   137               by (intro has_type_subst_closed);
   141               by (intro has_type_subst_closed);
   138             with s' t mgu_ok; show ?thesis; by simp;
   142             with s' t mgu_ok; show ?thesis; by simp;
   139           qed;
   143           qed;
   140           show "$ s a |- e2 :: $ u t2";
   144           show "$ s a |- e2 :: $ u t2";