src/HOL/MiniML/MiniML.ML
changeset 14422 b8da5f258b04
parent 14421 ee97b6463cb4
child 14423 35da60cbbb58
equal deleted inserted replaced
14421:ee97b6463cb4 14422:b8da5f258b04
     1 (* Title:     HOL/MiniML/MiniML.ML
       
     2    ID:        $Id$
       
     3    Author:    Dieter Nazareth and Tobias Nipkow
       
     4    Copyright  1995 TU Muenchen
       
     5 *)
       
     6 
       
     7 Addsimps has_type.intrs;
       
     8 Addsimps [Un_upper1,Un_upper2];
       
     9 
       
    10 Addsimps [is_bound_typ_instance_closed_subst];
       
    11 
       
    12 
       
    13 Goal "!!t::typ. $(%n. if n : (free_tv A) Un (free_tv t) then (S n) else (TVar n)) t = $S t";
       
    14 by (rtac typ_substitutions_only_on_free_variables 1);
       
    15 by (asm_full_simp_tac (simpset() addsimps [Ball_def]) 1);
       
    16 qed "s'_t_equals_s_t";
       
    17 
       
    18 Addsimps [s'_t_equals_s_t];
       
    19 
       
    20 Goal "!!A::type_scheme list. $(%n. if n : (free_tv A) Un (free_tv t) then (S n) else (TVar n)) A = $S A";
       
    21 by (rtac scheme_list_substitutions_only_on_free_variables 1);
       
    22 by (asm_full_simp_tac (simpset() addsimps [Ball_def]) 1);
       
    23 qed "s'_a_equals_s_a";
       
    24 
       
    25 Addsimps [s'_a_equals_s_a];
       
    26 
       
    27 Goal
       
    28  "$(%n. if n : (free_tv A) Un (free_tv t) then S n else TVar n) A |- \
       
    29 \    e :: $(%n. if n : (free_tv A) Un (free_tv t) then S n else TVar n) t \
       
    30 \ ==> $S A |- e :: $S t";
       
    31 
       
    32 by (res_inst_tac [("P","%A. A |- e :: $S t")] ssubst 1);
       
    33 by (rtac (s'_a_equals_s_a RS sym) 1);
       
    34 by (res_inst_tac [("P","%t. $ (%n. if n : free_tv A Un free_tv (?t1 S) then S n else TVar n) A |- e :: t")] ssubst 1);
       
    35 by (rtac (s'_t_equals_s_t RS sym) 1);
       
    36 by (Asm_full_simp_tac 1);
       
    37 qed "replace_s_by_s'";
       
    38 
       
    39 Goal "!!A::type_scheme list. $ (%x. TVar (if x : free_tv A then x else n + x)) A = $ id_subst A";
       
    40 by (rtac scheme_list_substitutions_only_on_free_variables 1);
       
    41 by (asm_full_simp_tac (simpset() addsimps [id_subst_def]) 1);
       
    42 qed "alpha_A'";
       
    43 
       
    44 Goal "!!A::type_scheme list. $ (%x. TVar (if x : free_tv A then x else n + x)) A = A";
       
    45 by (rtac (alpha_A' RS ssubst) 1);
       
    46 by (Asm_full_simp_tac 1);
       
    47 qed "alpha_A";
       
    48 
       
    49 Goal "$ (S o alpha) (t::typ) = $ S ($ (%x. TVar (alpha x)) t)";
       
    50 by (induct_tac "t" 1);
       
    51 by (ALLGOALS (Asm_full_simp_tac));
       
    52 qed "S_o_alpha_typ";
       
    53 
       
    54 Goal "$ (%u. (S o alpha) u) (t::typ) = $ S ($ (%x. TVar (alpha x)) t)";
       
    55 by (induct_tac "t" 1);
       
    56 by (ALLGOALS (Asm_full_simp_tac));
       
    57 val S_o_alpha_typ' = result ();
       
    58 
       
    59 Goal "$ (S o alpha) (sch::type_scheme) = $ S ($ (%x. TVar (alpha x)) sch)";
       
    60 by (induct_tac "sch" 1);
       
    61 by (ALLGOALS (Asm_full_simp_tac));
       
    62 qed "S_o_alpha_type_scheme";
       
    63 
       
    64 Goal "$ (S o alpha) (A::type_scheme list) = $ S ($ (%x. TVar (alpha x)) A)";
       
    65 by (induct_tac "A" 1);
       
    66 by (ALLGOALS (Asm_full_simp_tac));
       
    67 by (rtac (rewrite_rule [o_def] S_o_alpha_type_scheme) 1);
       
    68 qed "S_o_alpha_type_scheme_list";
       
    69 
       
    70 Goal "!!A::type_scheme list. \
       
    71 \     $ (%n. if n : free_tv A Un free_tv t then S n else TVar n) A = \
       
    72 \     $ ((%x. if x : free_tv A Un free_tv t then S x else TVar x) o \
       
    73 \        (%x. if x : free_tv A then x else n + x)) A";
       
    74 by (stac S_o_alpha_type_scheme_list 1);
       
    75 by (stac alpha_A 1);
       
    76 by (rtac refl 1);
       
    77 qed "S'_A_eq_S'_alpha_A";
       
    78 
       
    79 Goalw [free_tv_subst,dom_def]
       
    80  "dom (%n. if n : free_tv A Un free_tv t then S n else TVar n) <= \
       
    81 \ free_tv A Un free_tv t";
       
    82 by (Simp_tac 1);
       
    83 by (Fast_tac 1);
       
    84 qed "dom_S'";
       
    85 
       
    86 Goalw [free_tv_subst,cod_def,subset_def]
       
    87   "!!(A::type_scheme list) (t::typ). \ 
       
    88 \  cod (%n. if n : free_tv A Un free_tv t then S n else TVar n) <= \
       
    89 \  free_tv ($ S A) Un free_tv ($ S t)";
       
    90 by (rtac ballI 1);
       
    91 by (etac UN_E 1);
       
    92 by (dtac (dom_S' RS subsetD) 1);
       
    93 by (Asm_full_simp_tac 1);
       
    94 by (fast_tac (claset() addDs [free_tv_of_substitutions_extend_to_scheme_lists] 
       
    95                       addIs [free_tv_of_substitutions_extend_to_types RS subsetD]) 1);
       
    96 qed "cod_S'";
       
    97 
       
    98 Goalw [free_tv_subst]
       
    99  "!!(A::type_scheme list) (t::typ). \
       
   100 \ free_tv (%n. if n : free_tv A Un free_tv t then S n else TVar n) <= \
       
   101 \ free_tv A Un free_tv ($ S A) Un free_tv t Un free_tv ($ S t)";
       
   102 by (fast_tac (claset() addDs [dom_S' RS subsetD,cod_S' RS subsetD]) 1);
       
   103 qed "free_tv_S'";
       
   104 
       
   105 Goal "!!t1::typ. \
       
   106 \     (free_tv ($ (%x. TVar (if x : free_tv A then x else n + x)) t1) - free_tv A) <= \
       
   107 \         {x. ? y. x = n + y}";
       
   108 by (induct_tac "t1" 1);
       
   109 by (Simp_tac 1);
       
   110 by (Fast_tac 1);
       
   111 by (Simp_tac 1);
       
   112 by (Fast_tac 1);
       
   113 qed "free_tv_alpha";
       
   114 
       
   115 Goal "!!(k::nat). n <= n + k";
       
   116 by (induct_thm_tac nat_induct "k" 1);
       
   117 by (Simp_tac 1);
       
   118 by (Asm_simp_tac 1);
       
   119 val aux_plus = result();
       
   120 
       
   121 Addsimps [aux_plus];
       
   122 
       
   123 Goal "!!t::typ. new_tv n t ==> {x. ? y. x = n + y} Int free_tv t = {}";
       
   124 by Safe_tac;
       
   125 by (cut_facts_tac [aux_plus] 1);
       
   126 by (dtac new_tv_le 1);
       
   127 by (assume_tac 1);
       
   128 by (rotate_tac 1 1);
       
   129 by (dtac new_tv_not_free_tv 1);
       
   130 by (Fast_tac 1);
       
   131 val new_tv_Int_free_tv_empty_type = result ();
       
   132 
       
   133 Goal "!!sch::type_scheme. new_tv n sch ==> {x. ? y. x = n + y} Int free_tv sch = {}";
       
   134 by Safe_tac;
       
   135 by (cut_facts_tac [aux_plus] 1);
       
   136 by (dtac new_tv_le 1);
       
   137 by (assume_tac 1);
       
   138 by (rotate_tac 1 1);
       
   139 by (dtac new_tv_not_free_tv 1);
       
   140 by (Fast_tac 1);
       
   141 val new_tv_Int_free_tv_empty_scheme = result ();
       
   142 
       
   143 Goal "!A::type_scheme list. new_tv n A --> {x. ? y. x = n + y} Int free_tv A = {}";
       
   144 by (rtac allI 1);
       
   145 by (induct_tac "A" 1);
       
   146 by (Simp_tac 1);
       
   147 by (Simp_tac 1);
       
   148 by (fast_tac (claset() addDs [new_tv_Int_free_tv_empty_scheme ]) 1);
       
   149 val new_tv_Int_free_tv_empty_scheme_list = result ();
       
   150 
       
   151 Goalw [le_type_scheme_def,is_bound_typ_instance] 
       
   152    "new_tv n A --> gen A t <= gen A ($ (%x. TVar (if x : free_tv A then x else n + x)) t)";
       
   153 by (strip_tac 1);
       
   154 by (etac exE 1);
       
   155 by (hyp_subst_tac 1);
       
   156 by (res_inst_tac [("x","(%x. S (if n <= x then x - n else x))")] exI 1);
       
   157 by (induct_tac "t" 1);
       
   158 by (Simp_tac 1);
       
   159 by (case_tac "nat : free_tv A" 1);
       
   160 by (Asm_simp_tac 1);
       
   161 by (subgoal_tac "n <= n + nat" 1);
       
   162 by (dtac new_tv_le 1);
       
   163 by (assume_tac 1);
       
   164 by (dtac new_tv_not_free_tv 1);
       
   165 by (dtac new_tv_not_free_tv 1);
       
   166 by (asm_simp_tac (simpset() addsimps [diff_add_inverse ]) 1);
       
   167 by (Simp_tac 1);
       
   168 by (Asm_simp_tac 1);
       
   169 qed_spec_mp "gen_t_le_gen_alpha_t";
       
   170 
       
   171 AddSIs has_type.intrs;
       
   172 
       
   173 Goal "A |- e::t ==> !B. A <= B -->  B |- e::t";
       
   174 by (etac has_type.induct 1);
       
   175    by (simp_tac (simpset() addsimps [le_env_def]) 1);
       
   176    by (fast_tac (claset() addEs [bound_typ_instance_trans] addss simpset()) 1);
       
   177   by (Asm_full_simp_tac 1);
       
   178  by (Fast_tac 1);
       
   179 by (slow_tac (claset() addEs [le_env_free_tv RS free_tv_subset_gen_le]) 1);
       
   180 qed_spec_mp "has_type_le_env";
       
   181 
       
   182 (* has_type is closed w.r.t. substitution *)
       
   183 Goal "A |- e :: t ==> !S. $S A |- e :: $S t";
       
   184 by (etac has_type.induct 1);
       
   185 (* case VarI *)
       
   186    by (rtac allI 1);
       
   187    by (rtac has_type.VarI 1);
       
   188     by (asm_full_simp_tac (simpset() addsimps [app_subst_list]) 1);
       
   189    by (asm_simp_tac (simpset() addsimps [app_subst_list]) 1);
       
   190   (* case AbsI *)
       
   191   by (rtac allI 1);
       
   192   by (Simp_tac 1);  
       
   193   by (rtac has_type.AbsI 1);
       
   194   by (Asm_full_simp_tac 1);
       
   195  (* case AppI *)
       
   196  by (rtac allI 1);
       
   197  by (rtac has_type.AppI 1);
       
   198   by (Asm_full_simp_tac 1);
       
   199   by (etac spec 1);
       
   200  by (etac spec 1);
       
   201 (* case LetI *)
       
   202 by (rtac allI 1);
       
   203 by (rtac replace_s_by_s' 1);
       
   204 by (cut_inst_tac [("A","$ S A"), 
       
   205                   ("A'","A"),
       
   206                   ("t","t"),
       
   207                   ("t'","$ S t")] 
       
   208                  ex_fresh_variable 1);
       
   209 by (etac exE 1);
       
   210 by (REPEAT (etac conjE 1));
       
   211 by (res_inst_tac [("t1.0","$((%x. if x : free_tv A Un free_tv t then S x else TVar x) o \
       
   212 \                            (%x. if x : free_tv A then x else n + x)) t1")] 
       
   213                  has_type.LETI 1);
       
   214  by (dres_inst_tac [("x","(%x. if x : free_tv A Un free_tv t then S x else TVar x) o \
       
   215 \                         (%x. if x : free_tv A then x else n + x)")] spec 1);
       
   216  by (stac (S'_A_eq_S'_alpha_A) 1);
       
   217  by (assume_tac 1);
       
   218 by (stac S_o_alpha_typ 1);
       
   219 by (stac gen_subst_commutes 1);
       
   220  by (rtac subset_antisym 1);
       
   221   by (rtac subsetI 1);
       
   222   by (etac IntE 1);
       
   223   by (dtac (free_tv_S' RS subsetD) 1);
       
   224   by (dtac (free_tv_alpha RS subsetD) 1);
       
   225   by (asm_full_simp_tac (simpset() delsimps [full_SetCompr_eq])  1);
       
   226   by (etac exE 1);
       
   227   by (hyp_subst_tac 1);
       
   228   by (subgoal_tac "new_tv (n + y) ($ S A)" 1);
       
   229    by (subgoal_tac "new_tv (n + y) ($ S t)" 1);
       
   230     by (subgoal_tac "new_tv (n + y) A" 1);
       
   231      by (subgoal_tac "new_tv (n + y) t" 1);
       
   232       by (REPEAT (dtac new_tv_not_free_tv 1));
       
   233       by (Fast_tac 1);
       
   234      by (REPEAT ((rtac new_tv_le 1) THEN (assume_tac 2) THEN (Simp_tac 1)));
       
   235  by (Fast_tac 1);
       
   236 by (rtac has_type_le_env 1);
       
   237  by (dtac spec 1);
       
   238  by (dtac spec 1);
       
   239  by (assume_tac 1);
       
   240 by (rtac (app_subst_Cons RS subst) 1);
       
   241 by (rtac S_compatible_le_scheme_lists 1);
       
   242 by (Asm_simp_tac 1);
       
   243 qed "has_type_cl_sub";