| 2525 |      1 | (* Title:     HOL/MiniML/Instance.ML
 | 
|  |      2 |    ID:        $Id$
 | 
|  |      3 |    Author:    Wolfgang Naraschewski and Tobias Nipkow
 | 
|  |      4 |    Copyright  1996 TU Muenchen
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
|  |      7 | (* lemmatas for instatiation *)
 | 
|  |      8 | 
 | 
|  |      9 | 
 | 
|  |     10 | (* lemmatas for bound_typ_inst *)
 | 
|  |     11 | 
 | 
| 5069 |     12 | Goal "bound_typ_inst S (mk_scheme t) = t";
 | 
| 5184 |     13 | by (induct_tac "t" 1);
 | 
| 2525 |     14 | by (ALLGOALS Asm_simp_tac);
 | 
|  |     15 | qed "bound_typ_inst_mk_scheme";
 | 
|  |     16 | 
 | 
|  |     17 | Addsimps [bound_typ_inst_mk_scheme];
 | 
| 2625 |     18 | 
 | 
| 5118 |     19 | Goal "bound_typ_inst ($S o R) ($S sch) = $S (bound_typ_inst R sch)";
 | 
| 5184 |     20 | by (induct_tac "sch" 1);
 | 
| 2525 |     21 | by (ALLGOALS Asm_full_simp_tac);
 | 
|  |     22 | qed "bound_typ_inst_composed_subst";
 | 
|  |     23 | 
 | 
|  |     24 | Addsimps [bound_typ_inst_composed_subst];
 | 
|  |     25 | 
 | 
| 5118 |     26 | Goal "S = S' ==> sch = sch' ==> bound_typ_inst S sch = bound_typ_inst S' sch'";
 | 
| 2525 |     27 | by (Asm_full_simp_tac 1);
 | 
|  |     28 | qed "bound_typ_inst_eq";
 | 
|  |     29 | 
 | 
|  |     30 | 
 | 
| 2625 |     31 | 
 | 
| 2525 |     32 | (* lemmatas for bound_scheme_inst *)
 | 
|  |     33 | 
 | 
| 5118 |     34 | Goal "bound_scheme_inst B (mk_scheme t) = mk_scheme t";
 | 
| 5184 |     35 | by (induct_tac "t" 1);
 | 
| 2525 |     36 | by (Simp_tac 1);
 | 
|  |     37 | by (Asm_simp_tac 1);
 | 
|  |     38 | qed "bound_scheme_inst_mk_scheme";
 | 
|  |     39 | 
 | 
|  |     40 | Addsimps [bound_scheme_inst_mk_scheme];
 | 
|  |     41 | 
 | 
| 5118 |     42 | Goal "$S (bound_scheme_inst B sch) = (bound_scheme_inst ($S o B) ($ S sch))";
 | 
| 5184 |     43 | by (induct_tac "sch" 1);
 | 
| 2525 |     44 | by (Simp_tac 1);
 | 
|  |     45 | by (Simp_tac 1);
 | 
|  |     46 | by (Asm_simp_tac 1);
 | 
|  |     47 | qed "substitution_lemma";
 | 
|  |     48 | 
 | 
| 5069 |     49 | Goal "!t. mk_scheme t = bound_scheme_inst B sch --> \
 | 
| 2525 |     50 | \         (? S. !x:bound_tv sch. B x = mk_scheme (S x))";
 | 
| 5184 |     51 | by (induct_tac "sch" 1);
 | 
| 2525 |     52 | by (Simp_tac 1);
 | 
| 4153 |     53 | by Safe_tac;
 | 
| 2525 |     54 | by (rtac exI 1);
 | 
|  |     55 | by (rtac ballI 1);
 | 
|  |     56 | by (rtac sym 1);
 | 
|  |     57 | by (Asm_full_simp_tac 1);
 | 
|  |     58 | by (Asm_full_simp_tac 1);
 | 
|  |     59 | by (dtac mk_scheme_Fun 1);
 | 
|  |     60 | by (REPEAT (etac exE 1));
 | 
|  |     61 | by (etac conjE 1);
 | 
|  |     62 | by (dtac sym 1);
 | 
|  |     63 | by (dtac sym 1);
 | 
|  |     64 | by (REPEAT ((dtac mp 1) THEN (Fast_tac 1)));
 | 
| 4153 |     65 | by Safe_tac;
 | 
| 2525 |     66 | by (rename_tac "S1 S2" 1);
 | 
|  |     67 | by (res_inst_tac [("x","%x. if x:bound_tv type_scheme1 then (S1 x) else (S2 x)")] exI 1);
 | 
| 5521 |     68 | by (Auto_tac);
 | 
| 2525 |     69 | qed_spec_mp "bound_scheme_inst_type";
 | 
|  |     70 | 
 | 
|  |     71 | 
 | 
| 5118 |     72 | (* lemmas for subst_to_scheme *)
 | 
| 2525 |     73 | 
 | 
| 5118 |     74 | Goal "new_tv n sch --> subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k) \
 | 
| 2525 |     75 | \                                                 (bound_typ_inst (%k. TVar (k + n)) sch) = sch";
 | 
| 5184 |     76 | by (induct_tac "sch" 1);
 | 
| 5350 |     77 | by (simp_tac (simpset() addsimps [le_def]) 1);
 | 
| 4686 |     78 | by (simp_tac (simpset() addsimps [le_add2,diff_add_inverse2]) 1);
 | 
| 2525 |     79 | by (Asm_simp_tac 1);
 | 
|  |     80 | qed_spec_mp "subst_to_scheme_inverse";
 | 
|  |     81 | 
 | 
| 5118 |     82 | Goal "t = t' ==> \
 | 
|  |     83 | \     subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k) t = \
 | 
|  |     84 | \     subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k) t'";
 | 
| 2525 |     85 | by (Fast_tac 1);
 | 
|  |     86 | val aux = result ();
 | 
|  |     87 | 
 | 
| 5069 |     88 | Goal "new_tv n sch --> \
 | 
| 5118 |     89 | \     subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k) (bound_typ_inst S sch) = \
 | 
|  |     90 | \      bound_scheme_inst ((subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k)) o S) sch";
 | 
| 5184 |     91 | by (induct_tac "sch" 1);
 | 
| 5350 |     92 | by Auto_tac;
 | 
| 2525 |     93 | val aux2 = result () RS mp;
 | 
|  |     94 | 
 | 
|  |     95 | 
 | 
|  |     96 | (* lemmata for <= *)
 | 
|  |     97 | 
 | 
| 5069 |     98 | Goalw [le_type_scheme_def,is_bound_typ_instance]
 | 
| 5118 |     99 |   "!!(sch::type_scheme) sch'. \
 | 
|  |    100 | \  (sch' <= sch) = (? B. sch' = bound_scheme_inst B sch)";
 | 
| 2525 |    101 | by (rtac iffI 1);
 | 
|  |    102 | by (cut_inst_tac [("sch","sch")] fresh_variable_type_schemes 1); 
 | 
|  |    103 | by (cut_inst_tac [("sch","sch'")] fresh_variable_type_schemes 1);
 | 
|  |    104 | by (dtac make_one_new_out_of_two 1);
 | 
| 3018 |    105 | by (assume_tac 1);
 | 
| 2525 |    106 | by (thin_tac "? n. new_tv n sch'" 1); 
 | 
|  |    107 | by (etac exE 1);
 | 
|  |    108 | by (etac allE 1);
 | 
|  |    109 | by (dtac mp 1);
 | 
|  |    110 | by (res_inst_tac [("x","(%k. TVar (k + n))")] exI 1);
 | 
|  |    111 | by (rtac refl 1);
 | 
|  |    112 | by (etac exE 1);
 | 
|  |    113 | by (REPEAT (etac conjE 1));
 | 
|  |    114 | by (dres_inst_tac [("n","n")] aux 1);
 | 
| 4089 |    115 | by (asm_full_simp_tac (simpset() addsimps [subst_to_scheme_inverse]) 1);
 | 
| 2525 |    116 | by (res_inst_tac [("x","(subst_to_scheme (%k. if n <= k then BVar (k - n) else FVar k)) o S")] exI 1);
 | 
| 4089 |    117 | by (asm_simp_tac (simpset() addsimps [aux2]) 1);
 | 
| 4153 |    118 | by Safe_tac;
 | 
| 2525 |    119 | by (res_inst_tac [("x","%n. bound_typ_inst S (B n)")] exI 1);
 | 
| 5184 |    120 | by (induct_tac "sch" 1);
 | 
| 2525 |    121 | by (Simp_tac 1);
 | 
|  |    122 | by (Simp_tac 1);
 | 
|  |    123 | by (Asm_simp_tac 1);
 | 
|  |    124 | qed "le_type_scheme_def2";
 | 
|  |    125 | 
 | 
| 5069 |    126 | Goalw [is_bound_typ_instance] "(mk_scheme t) <= sch = t <| sch";
 | 
| 4089 |    127 | by (simp_tac (simpset() addsimps [le_type_scheme_def2]) 1); 
 | 
| 2525 |    128 | by (rtac iffI 1); 
 | 
|  |    129 | by (etac exE 1); 
 | 
|  |    130 | by (forward_tac [bound_scheme_inst_type] 1);
 | 
|  |    131 | by (etac exE 1);
 | 
|  |    132 | by (rtac exI 1);
 | 
|  |    133 | by (rtac mk_scheme_injective 1); 
 | 
|  |    134 | by (Asm_full_simp_tac 1);
 | 
|  |    135 | by (rotate_tac 1 1);
 | 
|  |    136 | by (rtac mp 1);
 | 
| 3018 |    137 | by (assume_tac 2);
 | 
| 5184 |    138 | by (induct_tac "sch" 1);
 | 
| 2525 |    139 | by (Simp_tac 1);
 | 
|  |    140 | by (Asm_full_simp_tac 1);
 | 
|  |    141 | by (Fast_tac 1);
 | 
|  |    142 | by (strip_tac 1);
 | 
|  |    143 | by (Asm_full_simp_tac 1);
 | 
|  |    144 | by (etac exE 1);
 | 
|  |    145 | by (Asm_full_simp_tac 1);
 | 
|  |    146 | by (rtac exI 1);
 | 
| 5184 |    147 | by (induct_tac "sch" 1);
 | 
| 2525 |    148 | by (Simp_tac 1);
 | 
|  |    149 | by (Simp_tac 1);
 | 
|  |    150 | by (Asm_full_simp_tac 1);
 | 
|  |    151 | qed_spec_mp "le_type_eq_is_bound_typ_instance";
 | 
|  |    152 | 
 | 
| 5069 |    153 | Goalw [le_env_def]
 | 
| 2525 |    154 |   "(sch # A <= sch' # B) = (sch <= (sch'::type_scheme) & A <= B)";
 | 
| 3018 |    155 | by (Simp_tac 1);
 | 
|  |    156 | by (rtac iffI 1);
 | 
| 4153 |    157 |  by (SELECT_GOAL Safe_tac 1);
 | 
| 3018 |    158 |   by (eres_inst_tac [("x","0")] allE 1);
 | 
|  |    159 |   by (Asm_full_simp_tac 1);
 | 
|  |    160 |  by (eres_inst_tac [("x","Suc i")] allE 1);
 | 
|  |    161 |  by (Asm_full_simp_tac 1);
 | 
|  |    162 | by (rtac conjI 1);
 | 
|  |    163 |  by (Fast_tac 1);
 | 
|  |    164 | by (rtac allI 1);
 | 
| 5184 |    165 | by (induct_tac "i" 1);
 | 
| 3018 |    166 | by (ALLGOALS Asm_simp_tac);
 | 
| 2525 |    167 | qed "le_env_Cons";
 | 
|  |    168 | AddIffs [le_env_Cons];
 | 
|  |    169 | 
 | 
| 5118 |    170 | Goalw [is_bound_typ_instance]"t <| sch ==> $S t <| $S sch";
 | 
| 2525 |    171 | by (etac exE 1);
 | 
|  |    172 | by (rename_tac "SA" 1);
 | 
|  |    173 | by (hyp_subst_tac 1);
 | 
|  |    174 | by (res_inst_tac [("x","$S o SA")] exI 1);
 | 
|  |    175 | by (Simp_tac 1);
 | 
|  |    176 | qed "is_bound_typ_instance_closed_subst";
 | 
|  |    177 | 
 | 
| 5069 |    178 | Goal "!!(sch::type_scheme) sch'. sch' <= sch ==> $S sch' <= $ S sch";
 | 
| 4089 |    179 | by (asm_full_simp_tac (simpset() addsimps [le_type_scheme_def2]) 1);
 | 
| 2525 |    180 | by (etac exE 1);
 | 
| 4089 |    181 | by (asm_full_simp_tac (simpset() addsimps [substitution_lemma]) 1);
 | 
| 2525 |    182 | by (Fast_tac 1);
 | 
|  |    183 | qed "S_compatible_le_scheme";
 | 
|  |    184 | 
 | 
| 5118 |    185 | Goalw [le_env_def,app_subst_list]
 | 
|  |    186 |  "!!(A::type_scheme list) A'. A' <= A ==> $S A' <= $ S A";
 | 
| 4089 |    187 | by (simp_tac (simpset() addcongs [conj_cong]) 1);
 | 
|  |    188 | by (fast_tac (claset() addSIs [S_compatible_le_scheme]) 1);
 | 
| 2525 |    189 | qed "S_compatible_le_scheme_lists";
 | 
|  |    190 | 
 | 
| 5118 |    191 | Goalw [le_type_scheme_def] "[| t <| sch; sch <= sch' |] ==> t <| sch'";
 | 
| 3018 |    192 | by (Fast_tac 1);
 | 
| 2525 |    193 | qed "bound_typ_instance_trans";
 | 
|  |    194 | 
 | 
| 5069 |    195 | Goalw [le_type_scheme_def] "sch <= (sch::type_scheme)";
 | 
| 3018 |    196 | by (Fast_tac 1);
 | 
| 2525 |    197 | qed "le_type_scheme_refl";
 | 
|  |    198 | AddIffs [le_type_scheme_refl];
 | 
|  |    199 | 
 | 
| 5069 |    200 | Goalw [le_env_def] "A <= (A::type_scheme list)";
 | 
| 3018 |    201 | by (Fast_tac 1);
 | 
| 2525 |    202 | qed "le_env_refl";
 | 
|  |    203 | AddIffs [le_env_refl];
 | 
|  |    204 | 
 | 
| 5069 |    205 | Goalw [le_type_scheme_def,is_bound_typ_instance] "sch <= BVar n";
 | 
| 3018 |    206 | by (strip_tac 1);
 | 
| 3842 |    207 | by (res_inst_tac [("x","%a. t")]exI 1);
 | 
| 3018 |    208 | by (Simp_tac 1);
 | 
| 2525 |    209 | qed "bound_typ_instance_BVar";
 | 
|  |    210 | AddIffs [bound_typ_instance_BVar];
 | 
|  |    211 | 
 | 
| 5118 |    212 | Goalw [le_type_scheme_def,is_bound_typ_instance]
 | 
|  |    213 |  "(sch <= FVar n) = (sch = FVar n)";
 | 
| 5184 |    214 | by (induct_tac "sch" 1);
 | 
| 3018 |    215 |   by (Simp_tac 1);
 | 
|  |    216 |  by (Simp_tac 1);
 | 
|  |    217 |  by (Fast_tac 1);
 | 
|  |    218 | by (Asm_full_simp_tac 1);
 | 
|  |    219 | by (Fast_tac 1);
 | 
| 2525 |    220 | qed "le_FVar";
 | 
|  |    221 | Addsimps [le_FVar];
 | 
|  |    222 | 
 | 
| 5069 |    223 | Goalw [le_type_scheme_def,is_bound_typ_instance] "~(FVar n <= sch1 =-> sch2)";
 | 
| 3018 |    224 | by (Simp_tac 1);
 | 
| 2525 |    225 | qed "not_FVar_le_Fun";
 | 
|  |    226 | AddIffs [not_FVar_le_Fun];
 | 
|  |    227 | 
 | 
| 5069 |    228 | Goalw [le_type_scheme_def,is_bound_typ_instance] "~(BVar n <= sch1 =-> sch2)";
 | 
| 3018 |    229 | by (Simp_tac 1);
 | 
|  |    230 | by (res_inst_tac [("x","TVar n")] exI 1);
 | 
|  |    231 | by (Simp_tac 1);
 | 
|  |    232 | by (Fast_tac 1);
 | 
| 2525 |    233 | qed "not_BVar_le_Fun";
 | 
|  |    234 | AddIffs [not_BVar_le_Fun];
 | 
|  |    235 | 
 | 
| 5069 |    236 | Goalw [le_type_scheme_def,is_bound_typ_instance]
 | 
| 5118 |    237 |   "(sch1 =-> sch2 <= sch1' =-> sch2') ==> sch1 <= sch1' & sch2 <= sch2'";
 | 
| 4089 |    238 | by (fast_tac (claset() addss simpset()) 1);
 | 
| 2525 |    239 | qed "Fun_le_FunD";
 | 
|  |    240 | 
 | 
| 5069 |    241 | Goal "(sch' <= sch1 =-> sch2) --> (? sch'1 sch'2. sch' = sch'1 =-> sch'2)";
 | 
| 5184 |    242 | by (induct_tac "sch'" 1);
 | 
| 2525 |    243 | by (Asm_simp_tac 1);
 | 
|  |    244 | by (Asm_simp_tac 1);
 | 
|  |    245 | by (Fast_tac 1);
 | 
|  |    246 | qed_spec_mp "scheme_le_Fun";
 | 
|  |    247 | 
 | 
| 5069 |    248 | Goal "!sch'::type_scheme. sch <= sch' --> free_tv sch' <= free_tv sch";
 | 
| 5184 |    249 | by (induct_tac "sch" 1);
 | 
| 3018 |    250 |   by (rtac allI 1);
 | 
| 5184 |    251 |   by (induct_tac "sch'" 1);
 | 
| 3018 |    252 |     by (Simp_tac 1);
 | 
|  |    253 |    by (Simp_tac 1);
 | 
|  |    254 |   by (Simp_tac 1);
 | 
|  |    255 |  by (rtac allI 1);
 | 
| 5184 |    256 |  by (induct_tac "sch'" 1);
 | 
| 3018 |    257 |    by (Simp_tac 1);
 | 
|  |    258 |   by (Simp_tac 1);
 | 
|  |    259 |  by (Simp_tac 1);
 | 
|  |    260 | by (rtac allI 1);
 | 
| 5184 |    261 | by (induct_tac "sch'" 1);
 | 
| 3018 |    262 |   by (Simp_tac 1);
 | 
|  |    263 |  by (Simp_tac 1);
 | 
|  |    264 | by (Asm_full_simp_tac 1);
 | 
|  |    265 | by (strip_tac 1);
 | 
|  |    266 | by (dtac Fun_le_FunD 1);
 | 
|  |    267 | by (Fast_tac 1);
 | 
| 2525 |    268 | qed_spec_mp "le_type_scheme_free_tv";
 | 
|  |    269 | 
 | 
| 5069 |    270 | Goal "!A::type_scheme list. A <= B --> free_tv B <= free_tv A";
 | 
| 5184 |    271 | by (induct_tac "B" 1);
 | 
| 3018 |    272 |  by (Simp_tac 1);
 | 
|  |    273 | by (rtac allI 1);
 | 
| 5184 |    274 | by (induct_tac "A" 1);
 | 
| 4089 |    275 |  by (simp_tac (simpset() addsimps [le_env_def]) 1);
 | 
| 3018 |    276 | by (Simp_tac 1);
 | 
| 4089 |    277 | by (fast_tac (claset() addDs [le_type_scheme_free_tv]) 1);
 | 
| 2525 |    278 | qed_spec_mp "le_env_free_tv";
 |