src/HOL/MiniML/Type.ML
changeset 3018 e65b60b28341
parent 3008 0a887d5b6718
child 3385 f59e64fe4058
equal deleted inserted replaced
3017:84c2178db936 3018:e65b60b28341
    16 by (Fast_tac 1);
    16 by (Fast_tac 1);
    17 qed_spec_mp "mk_scheme_Fun";
    17 qed_spec_mp "mk_scheme_Fun";
    18 
    18 
    19 goal thy "!t'.mk_scheme t = mk_scheme t' --> t=t'";
    19 goal thy "!t'.mk_scheme t = mk_scheme t' --> t=t'";
    20 by (typ.induct_tac "t" 1);
    20 by (typ.induct_tac "t" 1);
    21  br allI 1;
    21  by (rtac allI 1);
    22  by (typ.induct_tac "t'" 1);
    22  by (typ.induct_tac "t'" 1);
    23   by(Simp_tac 1);
    23   by (Simp_tac 1);
    24  by(Asm_full_simp_tac 1);
    24  by (Asm_full_simp_tac 1);
    25 br allI 1;
    25 by (rtac allI 1);
    26 by (typ.induct_tac "t'" 1);
    26 by (typ.induct_tac "t'" 1);
    27  by(Simp_tac 1);
    27  by (Simp_tac 1);
    28 by(Asm_full_simp_tac 1);
    28 by (Asm_full_simp_tac 1);
    29 by(Fast_tac 1);
    29 by (Fast_tac 1);
    30 qed_spec_mp "mk_scheme_injective";
    30 qed_spec_mp "mk_scheme_injective";
    31 
    31 
    32 goal thy "!!t. free_tv (mk_scheme t) = free_tv t";
    32 goal thy "!!t. free_tv (mk_scheme t) = free_tv t";
    33 by (typ.induct_tac "t" 1);
    33 by (typ.induct_tac "t" 1);
    34 by (ALLGOALS Asm_simp_tac);
    34 by (ALLGOALS Asm_simp_tac);
   103 
   103 
   104 Addsimps [new_tv_TVar,new_tv_FVar,new_tv_BVar,new_tv_Fun,new_tv_Fun2,new_tv_Nil,new_tv_Cons,new_tv_TVar_subst];
   104 Addsimps [new_tv_TVar,new_tv_FVar,new_tv_BVar,new_tv_Fun,new_tv_Fun2,new_tv_Nil,new_tv_Cons,new_tv_TVar_subst];
   105 
   105 
   106 goalw thy [id_subst_def,new_tv_def,free_tv_subst,dom_def,cod_def]
   106 goalw thy [id_subst_def,new_tv_def,free_tv_subst,dom_def,cod_def]
   107   "new_tv n id_subst";
   107   "new_tv n id_subst";
   108 by(Simp_tac 1);
   108 by (Simp_tac 1);
   109 qed "new_tv_id_subst";
   109 qed "new_tv_id_subst";
   110 Addsimps[new_tv_id_subst];
   110 Addsimps[new_tv_id_subst];
   111 
   111 
   112 goal thy "new_tv n (sch::type_scheme) --> \
   112 goal thy "new_tv n (sch::type_scheme) --> \
   113 \              $(%k.if k<n then S k else S' k) sch = $S sch";
   113 \              $(%k.if k<n then S k else S' k) sch = $S sch";
   114 by (type_scheme.induct_tac "sch" 1);
   114 by (type_scheme.induct_tac "sch" 1);
   115 by(ALLGOALS Asm_simp_tac);
   115 by (ALLGOALS Asm_simp_tac);
   116 qed "new_if_subst_type_scheme";
   116 qed "new_if_subst_type_scheme";
   117 Addsimps [new_if_subst_type_scheme];
   117 Addsimps [new_if_subst_type_scheme];
   118 
   118 
   119 goal thy "new_tv n (A::type_scheme list) --> \
   119 goal thy "new_tv n (A::type_scheme list) --> \
   120 \              $(%k.if k<n then S k else S' k) A = $S A";
   120 \              $(%k.if k<n then S k else S' k) A = $S A";
   121 by (list.induct_tac "A" 1);
   121 by (list.induct_tac "A" 1);
   122 by(ALLGOALS Asm_simp_tac);
   122 by (ALLGOALS Asm_simp_tac);
   123 qed "new_if_subst_type_scheme_list";
   123 qed "new_if_subst_type_scheme_list";
   124 Addsimps [new_if_subst_type_scheme_list];
   124 Addsimps [new_if_subst_type_scheme_list];
   125 
   125 
   126 
   126 
   127 (* constructor laws for dom and cod *)
   127 (* constructor laws for dom and cod *)
   251 by (fast_tac (HOL_cs addIs [eq_subst_type_scheme_eq_free] addss (!simpset)) 1);
   251 by (fast_tac (HOL_cs addIs [eq_subst_type_scheme_eq_free] addss (!simpset)) 1);
   252 qed_spec_mp "eq_subst_scheme_list_eq_free";
   252 qed_spec_mp "eq_subst_scheme_list_eq_free";
   253 
   253 
   254 goalw thy [free_tv_subst] 
   254 goalw thy [free_tv_subst] 
   255       "!!v. v : cod S ==> v : free_tv S";
   255       "!!v. v : cod S ==> v : free_tv S";
   256 by( fast_tac set_cs 1);
   256 by ( fast_tac set_cs 1);
   257 qed "codD";
   257 qed "codD";
   258  
   258  
   259 goalw thy [free_tv_subst,dom_def] 
   259 goalw thy [free_tv_subst,dom_def] 
   260       "!! x. x ~: free_tv S ==> S x = TVar x";
   260       "!! x. x ~: free_tv S ==> S x = TVar x";
   261 by( fast_tac set_cs 1);
   261 by ( fast_tac set_cs 1);
   262 qed "not_free_impl_id";
   262 qed "not_free_impl_id";
   263 
   263 
   264 goalw thy [new_tv_def] 
   264 goalw thy [new_tv_def] 
   265       "!! n. [| new_tv n t; m:free_tv t |] ==> m<n";
   265       "!! n. [| new_tv n t; m:free_tv t |] ==> m<n";
   266 by( fast_tac HOL_cs 1 );
   266 by ( fast_tac HOL_cs 1 );
   267 qed "free_tv_le_new_tv";
   267 qed "free_tv_le_new_tv";
   268 
   268 
   269 goalw thy [dom_def,cod_def,UNION_def,Bex_def]
   269 goalw thy [dom_def,cod_def,UNION_def,Bex_def]
   270   "!!v. [| v : free_tv(S n); v~=n |] ==> v : cod S";
   270   "!!v. [| v : free_tv(S n); v~=n |] ==> v : cod S";
   271 by (Simp_tac 1);
   271 by (Simp_tac 1);
   276 qed "cod_app_subst";
   276 qed "cod_app_subst";
   277 Addsimps [cod_app_subst];
   277 Addsimps [cod_app_subst];
   278 
   278 
   279 goal thy 
   279 goal thy 
   280      "free_tv (S (v::nat)) <= cod S Un {v}";
   280      "free_tv (S (v::nat)) <= cod S Un {v}";
   281 by( cut_inst_tac [("P","v:dom S")] excluded_middle 1);
   281 by ( cut_inst_tac [("P","v:dom S")] excluded_middle 1);
   282 by( safe_tac (HOL_cs addSIs [subsetI]) );
   282 by ( safe_tac (HOL_cs addSIs [subsetI]) );
   283 by (fast_tac (set_cs addss (!simpset addsimps [dom_def])) 1);
   283 by (fast_tac (set_cs addss (!simpset addsimps [dom_def])) 1);
   284 by (fast_tac (set_cs addss (!simpset addsimps [cod_def])) 1);
   284 by (fast_tac (set_cs addss (!simpset addsimps [cod_def])) 1);
   285 qed "free_tv_subst_var";
   285 qed "free_tv_subst_var";
   286 
   286 
   287 goal thy 
   287 goal thy 
   288      "free_tv ($ S (t::typ)) <= cod S Un free_tv t";
   288      "free_tv ($ S (t::typ)) <= cod S Un free_tv t";
   289 by( typ.induct_tac "t" 1);
   289 by ( typ.induct_tac "t" 1);
   290 (* case TVar n *)
   290 (* case TVar n *)
   291 by( simp_tac (!simpset addsimps [free_tv_subst_var]) 1);
   291 by ( simp_tac (!simpset addsimps [free_tv_subst_var]) 1);
   292 (* case Fun t1 t2 *)
   292 (* case Fun t1 t2 *)
   293 by( fast_tac (set_cs addss !simpset) 1);
   293 by ( fast_tac (set_cs addss !simpset) 1);
   294 qed "free_tv_app_subst_te";     
   294 qed "free_tv_app_subst_te";     
   295 
   295 
   296 goal thy 
   296 goal thy 
   297      "free_tv ($ S (sch::type_scheme)) <= cod S Un free_tv sch";
   297      "free_tv ($ S (sch::type_scheme)) <= cod S Un free_tv sch";
   298 by( type_scheme.induct_tac "sch" 1);
   298 by ( type_scheme.induct_tac "sch" 1);
   299 (* case FVar n *)
   299 (* case FVar n *)
   300 by( simp_tac (!simpset addsimps [free_tv_subst_var]) 1);
   300 by ( simp_tac (!simpset addsimps [free_tv_subst_var]) 1);
   301 (* case BVar n *)
   301 (* case BVar n *)
   302 by (Simp_tac 1);
   302 by (Simp_tac 1);
   303 (* case Fun t1 t2 *)
   303 (* case Fun t1 t2 *)
   304 by (fast_tac (set_cs addss !simpset) 1);
   304 by (fast_tac (set_cs addss !simpset) 1);
   305 qed "free_tv_app_subst_type_scheme";  
   305 qed "free_tv_app_subst_type_scheme";  
   306 
   306 
   307 goal thy 
   307 goal thy 
   308      "free_tv ($ S (A::type_scheme list)) <= cod S Un free_tv A";
   308      "free_tv ($ S (A::type_scheme list)) <= cod S Un free_tv A";
   309 by( list.induct_tac "A" 1);
   309 by ( list.induct_tac "A" 1);
   310 (* case [] *)
   310 (* case [] *)
   311 by (Simp_tac 1);
   311 by (Simp_tac 1);
   312 (* case a#al *)
   312 (* case a#al *)
   313 by( cut_facts_tac [free_tv_app_subst_type_scheme] 1);
   313 by ( cut_facts_tac [free_tv_app_subst_type_scheme] 1);
   314 by( fast_tac (set_cs addss !simpset) 1);
   314 by ( fast_tac (set_cs addss !simpset) 1);
   315 qed "free_tv_app_subst_scheme_list";
   315 qed "free_tv_app_subst_scheme_list";
   316 
   316 
   317 goalw thy [free_tv_subst,dom_def]
   317 goalw thy [free_tv_subst,dom_def]
   318      "free_tv (%u::nat. $ s1 (s2 u) :: typ) <=   \
   318      "free_tv (%u::nat. $ s1 (s2 u) :: typ) <=   \
   319 \     free_tv s1 Un free_tv s2";
   319 \     free_tv s1 Un free_tv s2";
   394 (* lemmata for new_tv *)
   394 (* lemmata for new_tv *)
   395 
   395 
   396 goalw thy [new_tv_def]
   396 goalw thy [new_tv_def]
   397   "new_tv n S = ((!m. n <= m --> (S m = TVar m)) & \
   397   "new_tv n S = ((!m. n <= m --> (S m = TVar m)) & \
   398 \                (! l. l < n --> new_tv n (S l) ))";
   398 \                (! l. l < n --> new_tv n (S l) ))";
   399 by( safe_tac HOL_cs );
   399 by ( safe_tac HOL_cs );
   400 (* ==> *)
   400 (* ==> *)
   401 by( fast_tac (HOL_cs addDs [leD] addss (!simpset addsimps [free_tv_subst,dom_def])) 1);
   401 by ( fast_tac (HOL_cs addDs [leD] addss (!simpset addsimps [free_tv_subst,dom_def])) 1);
   402 by( subgoal_tac "m:cod S | S l = TVar l" 1);
   402 by ( subgoal_tac "m:cod S | S l = TVar l" 1);
   403 by( safe_tac HOL_cs );
   403 by ( safe_tac HOL_cs );
   404 by(fast_tac (HOL_cs addDs [UnI2] addss (!simpset addsimps [free_tv_subst])) 1);
   404 by (fast_tac (HOL_cs addDs [UnI2] addss (!simpset addsimps [free_tv_subst])) 1);
   405 by(dres_inst_tac [("P","%x. m:free_tv x")] subst 1 THEN atac 1);
   405 by (dres_inst_tac [("P","%x. m:free_tv x")] subst 1 THEN atac 1);
   406 by(Asm_full_simp_tac 1);
   406 by (Asm_full_simp_tac 1);
   407 by(fast_tac (set_cs addss (!simpset addsimps [free_tv_subst,cod_def,dom_def])) 1);
   407 by (fast_tac (set_cs addss (!simpset addsimps [free_tv_subst,cod_def,dom_def])) 1);
   408 (* <== *)
   408 (* <== *)
   409 by( rewrite_goals_tac [free_tv_subst,cod_def,dom_def] );
   409 by ( rewrite_goals_tac [free_tv_subst,cod_def,dom_def] );
   410 by( safe_tac set_cs );
   410 by ( safe_tac set_cs );
   411 by( cut_inst_tac [("m","m"),("n","n")] less_linear 1);
   411 by ( cut_inst_tac [("m","m"),("n","n")] less_linear 1);
   412 by( fast_tac (HOL_cs addSIs [less_or_eq_imp_le]) 1);
   412 by ( fast_tac (HOL_cs addSIs [less_or_eq_imp_le]) 1);
   413 by( cut_inst_tac [("m","ma"),("n","n")] less_linear 1);
   413 by ( cut_inst_tac [("m","ma"),("n","n")] less_linear 1);
   414 by( fast_tac (HOL_cs addSIs [less_or_eq_imp_le]) 1);
   414 by ( fast_tac (HOL_cs addSIs [less_or_eq_imp_le]) 1);
   415 qed "new_tv_subst";
   415 qed "new_tv_subst";
   416 
   416 
   417 goal thy 
   417 goal thy 
   418   "new_tv n  = list_all (new_tv n)";
   418   "new_tv n  = list_all (new_tv n)";
   419 by (rtac ext 1);
   419 by (rtac ext 1);
   420 by(list.induct_tac "x" 1);
   420 by (list.induct_tac "x" 1);
   421 by(ALLGOALS Asm_simp_tac);
   421 by (ALLGOALS Asm_simp_tac);
   422 qed "new_tv_list";
   422 qed "new_tv_list";
   423 
   423 
   424 (* substitution affects only variables occurring freely *)
   424 (* substitution affects only variables occurring freely *)
   425 goal thy
   425 goal thy
   426   "new_tv n (t::typ) --> $(%x. if x=n then t' else S x) t = $S t";
   426   "new_tv n (t::typ) --> $(%x. if x=n then t' else S x) t = $S t";
   499 qed_spec_mp "new_tv_subst_scheme_list";
   499 qed_spec_mp "new_tv_subst_scheme_list";
   500 Addsimps [new_tv_subst_scheme_list];
   500 Addsimps [new_tv_subst_scheme_list];
   501 
   501 
   502 goal thy
   502 goal thy
   503   "new_tv n A --> new_tv (Suc n) ((TVar n)#A)";
   503   "new_tv n A --> new_tv (Suc n) ((TVar n)#A)";
   504 by( simp_tac (!simpset addsimps [new_tv_list]) 1);
   504 by ( simp_tac (!simpset addsimps [new_tv_list]) 1);
   505 by (list.induct_tac "A" 1);
   505 by (list.induct_tac "A" 1);
   506 by (ALLGOALS Asm_full_simp_tac);
   506 by (ALLGOALS Asm_full_simp_tac);
   507 qed "new_tv_Suc_list";
   507 qed "new_tv_Suc_list";
   508 
   508 
   509 goalw thy [new_tv_def] "!!sch::type_scheme. (free_tv sch) = (free_tv sch') --> (new_tv n sch --> new_tv n sch')";
   509 goalw thy [new_tv_def] "!!sch::type_scheme. (free_tv sch) = (free_tv sch') --> (new_tv n sch --> new_tv n sch')";
   625 goal thy "!!(A::type_scheme list) (A'::type_scheme list) (t::typ) (t'::typ). \
   625 goal thy "!!(A::type_scheme list) (A'::type_scheme list) (t::typ) (t'::typ). \
   626 \         ? n. (new_tv n A) & (new_tv n A') & (new_tv n t) & (new_tv n t')" ;
   626 \         ? n. (new_tv n A) & (new_tv n A') & (new_tv n t) & (new_tv n t')" ;
   627 by (cut_inst_tac [("t","t")] fresh_variable_types 1);
   627 by (cut_inst_tac [("t","t")] fresh_variable_types 1);
   628 by (cut_inst_tac [("t","t'")] fresh_variable_types 1);
   628 by (cut_inst_tac [("t","t'")] fresh_variable_types 1);
   629 by (dtac make_one_new_out_of_two 1);
   629 by (dtac make_one_new_out_of_two 1);
   630 ba 1;
   630 by (assume_tac 1);
   631 by (thin_tac "? n. new_tv n t'" 1);
   631 by (thin_tac "? n. new_tv n t'" 1);
   632 by (cut_inst_tac [("A","A")] fresh_variable_type_scheme_lists 1);
   632 by (cut_inst_tac [("A","A")] fresh_variable_type_scheme_lists 1);
   633 by (cut_inst_tac [("A","A'")] fresh_variable_type_scheme_lists 1);
   633 by (cut_inst_tac [("A","A'")] fresh_variable_type_scheme_lists 1);
   634 by (rotate_tac 1 1);
   634 by (rotate_tac 1 1);
   635 by (dtac make_one_new_out_of_two 1);
   635 by (dtac make_one_new_out_of_two 1);
   636 ba 1;
   636 by (assume_tac 1);
   637 by (thin_tac "? n. new_tv n A'" 1);
   637 by (thin_tac "? n. new_tv n A'" 1);
   638 by (REPEAT (etac exE 1));
   638 by (REPEAT (etac exE 1));
   639 by (rename_tac "n2 n1" 1);
   639 by (rename_tac "n2 n1" 1);
   640 by (res_inst_tac [("x","(max n1 n2)")] exI 1);
   640 by (res_inst_tac [("x","(max n1 n2)")] exI 1);
   641 by (rewtac new_tv_def);
   641 by (rewtac new_tv_def);
   644 
   644 
   645 (* mgu does not introduce new type variables *)
   645 (* mgu does not introduce new type variables *)
   646 goalw thy [new_tv_def] 
   646 goalw thy [new_tv_def] 
   647       "!! n. [|mgu t1 t2 = Some u; new_tv n t1; new_tv n t2|] ==> \
   647       "!! n. [|mgu t1 t2 = Some u; new_tv n t1; new_tv n t2|] ==> \
   648 \            new_tv n u";
   648 \            new_tv n u";
   649 by( fast_tac (set_cs addDs [mgu_free]) 1);
   649 by ( fast_tac (set_cs addDs [mgu_free]) 1);
   650 qed "mgu_new";
   650 qed "mgu_new";
   651 
   651 
   652 
   652 
   653 (* lemmata for substitutions *)
   653 (* lemmata for substitutions *)
   654 
   654 
   655 goalw Type.thy [app_subst_list] "length ($ S A) = length A";
   655 goalw Type.thy [app_subst_list] "length ($ S A) = length A";
   656 by(Simp_tac 1);
   656 by (Simp_tac 1);
   657 qed "length_app_subst_list";
   657 qed "length_app_subst_list";
   658 Addsimps [length_app_subst_list];
   658 Addsimps [length_app_subst_list];
   659 
   659 
   660 goal thy "!!sch::type_scheme. $ TVar sch = sch";
   660 goal thy "!!sch::type_scheme. $ TVar sch = sch";
   661 by (type_scheme.induct_tac "sch" 1);
   661 by (type_scheme.induct_tac "sch" 1);
   712 
   712 
   713 Addsimps [id_subst_A];
   713 Addsimps [id_subst_A];
   714 
   714 
   715 (* composition of substitutions *)
   715 (* composition of substitutions *)
   716 goalw Type.thy [id_subst_def,o_def] "$S o id_subst = S";
   716 goalw Type.thy [id_subst_def,o_def] "$S o id_subst = S";
   717 br ext 1;
   717 by (rtac ext 1);
   718 by(Simp_tac 1);
   718 by (Simp_tac 1);
   719 qed "o_id_subst";
   719 qed "o_id_subst";
   720 Addsimps[o_id_subst];
   720 Addsimps[o_id_subst];
   721 
   721 
   722 goal thy
   722 goal thy
   723   "$ R ($ S t::typ) = $ (%x. $ R (S x) ) t";
   723   "$ R ($ S t::typ) = $ (%x. $ R (S x) ) t";
   745 by (asm_full_simp_tac (!simpset addsimps [id_subst_def]) 1);
   745 by (asm_full_simp_tac (!simpset addsimps [id_subst_def]) 1);
   746 qed "subst_id_on_type_scheme_list'";
   746 qed "subst_id_on_type_scheme_list'";
   747 
   747 
   748 goal thy "!!A::type_scheme list. !x : free_tv A. S x = (TVar x) ==> $ S A = A";
   748 goal thy "!!A::type_scheme list. !x : free_tv A. S x = (TVar x) ==> $ S A = A";
   749 by (stac subst_id_on_type_scheme_list' 1);
   749 by (stac subst_id_on_type_scheme_list' 1);
   750 ba 1;
   750 by (assume_tac 1);
   751 by (Simp_tac 1);
   751 by (Simp_tac 1);
   752 qed "subst_id_on_type_scheme_list";
   752 qed "subst_id_on_type_scheme_list";
   753 
   753 
   754 goal thy "!!n. !n. n < length A --> nth n ($ S A) = $S (nth n A)";
   754 goal thy "!!n. !n. n < length A --> nth n ($ S A) = $S (nth n A)";
   755 by (list.induct_tac "A" 1);
   755 by (list.induct_tac "A" 1);