Nominal/Nominal2.thy
changeset 2957 01ff621599bc
parent 2950 0911cb7bf696
child 2959 9bd97ed202f7
equal deleted inserted replaced
2955:4049a2651dd9 2957:01ff621599bc
   143 
   143 
   144 
   144 
   145 ML {*
   145 ML {*
   146 (* definition of the raw datatype *)
   146 (* definition of the raw datatype *)
   147 
   147 
   148 fun define_raw_dts dts bn_funs bn_eqs bclauses lthy =
   148 fun define_raw_dts dts cnstr_names cnstr_tys bn_funs bn_eqs bclauses lthy =
   149 let
   149 let
   150   val thy = Local_Theory.exit_global lthy
   150   val thy = Local_Theory.exit_global lthy
   151   val thy_name = Context.theory_name thy
   151   val thy_name = Context.theory_name thy
   152 
   152 
   153   val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
   153   val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
   154   val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
   154   val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
   155   val dt_full_names' = add_raws dt_full_names
   155   val dt_full_names' = add_raws dt_full_names
   156   val dts_env = dt_full_names ~~ dt_full_names'
   156   val dts_env = dt_full_names ~~ dt_full_names'
   157 
   157 
   158   val cnstr_names = get_cnstr_strs dts
       
   159   val cnstr_tys = get_typed_cnstrs dts
       
   160   val cnstr_full_names = map (Long_Name.qualify thy_name) cnstr_names
   158   val cnstr_full_names = map (Long_Name.qualify thy_name) cnstr_names
   161   val cnstr_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
   159   val cnstr_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
   162     (Long_Name.qualify (add_raw x) (add_raw y))) cnstr_tys
   160     (Long_Name.qualify (add_raw x) (add_raw y))) cnstr_tys
   163   val cnstrs_env = cnstr_full_names ~~ cnstr_full_names'
   161   val cnstrs_env = cnstr_full_names ~~ cnstr_full_names'
   164 
   162 
   166   val bn_fun_strs' = add_raws bn_fun_strs
   164   val bn_fun_strs' = add_raws bn_fun_strs
   167   val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
   165   val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
   168   val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
   166   val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
   169     (bn_fun_strs ~~ bn_fun_strs')
   167     (bn_fun_strs ~~ bn_fun_strs')
   170   
   168   
   171   val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
   169   val (raw_full_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
   172   val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
   170   val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
   173   val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env bclauses 
   171   val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env bclauses 
   174 
   172 
   175   val (raw_dt_full_names, thy1) = 
   173   val (raw_full_dt_names', thy1) = 
   176     Datatype.add_datatype Datatype.default_config raw_dt_names raw_dts thy
   174     Datatype.add_datatype Datatype.default_config raw_full_dt_names raw_dts thy
   177 
   175 
   178   val lthy1 = Named_Target.theory_init thy1
   176   val lthy1 = Named_Target.theory_init thy1
   179 in
   177 
   180   (raw_dt_full_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, cnstr_names, lthy1)
   178   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) raw_full_dt_names' 
   181 end
       
   182 *}
       
   183 
       
   184 
       
   185 ML {*
       
   186 fun nominal_datatype2 opt_thms_name dts bn_funs bn_eqs bclauses lthy =
       
   187 let
       
   188   val _ = trace_msg (K "Defining raw datatypes...")
       
   189   val (raw_dt_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, cnstr_names, lthy0) =
       
   190     define_raw_dts dts bn_funs bn_eqs bclauses lthy   
       
   191 
       
   192   val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) raw_dt_names 
       
   193   val {descr, sorts, ...} = hd dtinfos
   179   val {descr, sorts, ...} = hd dtinfos
   194 
   180 
   195   val raw_tys = Datatype_Aux.get_rec_types descr sorts
   181   val raw_tys = Datatype_Aux.get_rec_types descr sorts
   196   val tvs = hd raw_tys
   182   val raw_ty_args = hd raw_tys
   197     |> snd o dest_Type
   183     |> snd o dest_Type
   198     |> map dest_TFree  
   184     |> map dest_TFree 
   199 
   185 
   200   val raw_cns_info = all_dtyp_constrs_types descr sorts
   186   val raw_cns_info = all_dtyp_constrs_types descr sorts
   201   val raw_constrs = (map o map) (fn (c, _, _, _) => c) raw_cns_info
   187   val raw_all_cns = (map o map) (fn (c, _, _, _) => c) raw_cns_info
   202 
   188 
   203   val raw_inject_thms = flat (map #inject dtinfos)
   189   val raw_inject_thms = flat (map #inject dtinfos)
   204   val raw_distinct_thms = flat (map #distinct dtinfos)
   190   val raw_distinct_thms = flat (map #distinct dtinfos)
   205   val raw_induct_thm = #induct (hd dtinfos)
   191   val raw_induct_thm = #induct (hd dtinfos)
   206   val raw_induct_thms = #inducts (hd dtinfos)
   192   val raw_induct_thms = #inducts (hd dtinfos)
   207   val raw_exhaust_thms = map #exhaust dtinfos
   193   val raw_exhaust_thms = map #exhaust dtinfos
   208   val raw_size_trms = map HOLogic.size_const raw_tys
   194   val raw_size_trms = map HOLogic.size_const raw_tys
   209   val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy0) (hd raw_dt_names)
   195   val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy1) (hd raw_full_dt_names')
   210     |> `(fn thms => (length thms) div 2)
   196     |> `(fn thms => (length thms) div 2)
   211     |> uncurry drop
   197     |> uncurry drop
       
   198 
       
   199   val raw_result = RawDtInfo 
       
   200     {raw_dt_names = raw_full_dt_names',
       
   201      raw_dts = raw_dts,
       
   202      raw_tys = raw_tys,
       
   203      raw_ty_args = raw_ty_args,
       
   204      raw_cns_info = raw_cns_info,
       
   205      raw_all_cns = raw_all_cns,
       
   206      raw_inject_thms = raw_inject_thms,
       
   207      raw_distinct_thms = raw_distinct_thms,
       
   208      raw_induct_thm = raw_induct_thm,
       
   209      raw_induct_thms = raw_induct_thms,
       
   210      raw_exhaust_thms = raw_exhaust_thms,
       
   211      raw_size_trms = raw_size_trms,
       
   212      raw_size_thms = raw_size_thms}
       
   213 in
       
   214   (raw_bclauses, raw_bn_funs, raw_bn_eqs, raw_result, lthy1)
       
   215 end
       
   216 *}
       
   217 
       
   218 
       
   219 ML {*
       
   220 fun nominal_datatype2 opt_thms_name dts bn_funs bn_eqs bclauses lthy =
       
   221 let
       
   222   val cnstr_names = get_cnstr_strs dts
       
   223   val cnstr_tys = get_typed_cnstrs dts
       
   224 
       
   225   val _ = trace_msg (K "Defining raw datatypes...")
       
   226   val (raw_bclauses, raw_bn_funs, raw_bn_eqs, raw_dt_info, lthy0) =
       
   227     define_raw_dts dts cnstr_names cnstr_tys bn_funs bn_eqs bclauses lthy   
       
   228 
       
   229   val RawDtInfo 
       
   230     {raw_dt_names,
       
   231      raw_tys,
       
   232      raw_ty_args,
       
   233      raw_all_cns,
       
   234      raw_inject_thms,
       
   235      raw_distinct_thms,
       
   236      raw_induct_thm,
       
   237      raw_induct_thms,
       
   238      raw_exhaust_thms,
       
   239      raw_size_trms,
       
   240      raw_size_thms, ...} = raw_dt_info
   212   
   241   
   213   val _ = trace_msg (K "Defining raw permutations...")
   242   val _ = trace_msg (K "Defining raw permutations...")
   214   val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2a) =
   243   val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2a) = define_raw_perms raw_dt_info lthy0
   215     define_raw_perms raw_dt_names raw_tys tvs (flat raw_constrs) raw_induct_thm lthy0
       
   216  
   244  
   217   (* noting the raw permutations as eqvt theorems *)
   245   (* noting the raw permutations as eqvt theorems *)
   218   val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_perm_simps) lthy2a
   246   val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_perm_simps) lthy2a
   219 
   247 
   220   val _ = trace_msg (K "Defining raw fv- and bn-functions...")
   248   val _ = trace_msg (K "Defining raw fv- and bn-functions...")
   221   val (raw_bns, raw_bn_defs, raw_bn_info, raw_bn_inducts, lthy3a) =
   249   val (raw_bns, raw_bn_defs, raw_bn_info, raw_bn_inducts, lthy3a) =
   222     define_raw_bns raw_dt_names raw_dts raw_bn_funs raw_bn_eqs 
   250     define_raw_bns raw_dt_info raw_bn_funs raw_bn_eqs lthy3
   223       (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3
       
   224     
   251     
   225   (* defining the permute_bn functions *)
   252   (* defining the permute_bn functions *)
   226   val (raw_perm_bns, raw_perm_bn_simps, lthy3b) = 
   253   val (raw_perm_bns, raw_perm_bn_simps, lthy3b) = 
   227     define_raw_bn_perms raw_tys raw_bn_info raw_cns_info 
   254     define_raw_bn_perms raw_dt_info raw_bn_info lthy3a
   228       (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3a
       
   229     
   255     
   230   val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3c) = 
   256   val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3c) = 
   231     define_raw_fvs raw_dt_names raw_tys raw_cns_info raw_bn_info raw_bclauses 
   257     define_raw_fvs raw_dt_info raw_bn_info raw_bclauses lthy3b
   232       (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3b
       
   233     
   258     
   234   val _ = trace_msg (K "Defining alpha relations...")
   259   val _ = trace_msg (K "Defining alpha relations...")
   235   val (alpha_result, lthy4) =
   260   val (alpha_result, lthy4) =
   236     define_raw_alpha raw_dt_names raw_tys raw_cns_info raw_bn_info raw_bclauses raw_fvs lthy3c
   261     define_raw_alpha raw_dt_info raw_bn_info raw_bclauses raw_fvs lthy3c
   237     
   262     
   238   val _ = trace_msg (K "Proving distinct theorems...")
   263   val _ = trace_msg (K "Proving distinct theorems...")
   239   val alpha_distincts = 
   264   val alpha_distincts = raw_prove_alpha_distincts lthy4 alpha_result raw_dt_info
   240     raw_prove_alpha_distincts lthy4 alpha_result raw_distinct_thms raw_dt_names
       
   241 
   265 
   242   val _ = trace_msg (K "Proving eq-iff theorems...")
   266   val _ = trace_msg (K "Proving eq-iff theorems...")
   243   val alpha_eq_iff = raw_prove_alpha_eq_iff lthy4 alpha_result raw_distinct_thms raw_inject_thms
   267   val alpha_eq_iff = raw_prove_alpha_eq_iff lthy4 alpha_result raw_dt_info
   244     
   268     
   245   val _ = trace_msg (K "Proving equivariance of bns, fvs, size and alpha...")
   269   val _ = trace_msg (K "Proving equivariance of bns, fvs, size and alpha...")
   246   val raw_bn_eqvt = 
   270   val raw_bn_eqvt = 
   247     raw_prove_eqvt raw_bns raw_bn_inducts (raw_bn_defs @ raw_perm_simps) lthy4
   271     raw_prove_eqvt raw_bns raw_bn_inducts (raw_bn_defs @ raw_perm_simps) lthy4
   248     
   272     
   251 
   275 
   252   val raw_fv_eqvt = 
   276   val raw_fv_eqvt = 
   253     raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps) 
   277     raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps) 
   254       (Local_Theory.restore lthy_tmp)
   278       (Local_Theory.restore lthy_tmp)
   255     
   279     
   256   val raw_size_eqvt = 
   280   val raw_size_eqvt =
   257     raw_prove_eqvt raw_size_trms raw_induct_thms (raw_size_thms @ raw_perm_simps) 
   281     let
   258       (Local_Theory.restore lthy_tmp)
   282       val RawDtInfo {raw_size_trms, raw_size_thms, raw_induct_thms, ...} = raw_dt_info
   259       |> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
   283     in
   260       |> map (fn thm => thm RS @{thm sym})
   284       raw_prove_eqvt raw_size_trms raw_induct_thms (raw_size_thms @ raw_perm_simps) 
       
   285         (Local_Theory.restore lthy_tmp)
       
   286         |> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
       
   287         |> map (fn thm => thm RS @{thm sym})
       
   288     end 
   261      
   289      
   262   val lthy5 = snd (Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_fv_eqvt) lthy_tmp)
   290   val lthy5 = snd (Local_Theory.note ((Binding.empty, [eqvt_attr]), raw_fv_eqvt) lthy_tmp)
   263 
   291 
   264   val alpha_eqvt =
   292   val alpha_eqvt =
   265     let
   293     let
   291   val raw_size_rsp = 
   319   val raw_size_rsp = 
   292     raw_size_rsp_aux lthy5 alpha_result (raw_size_thms @ raw_size_eqvt)
   320     raw_size_rsp_aux lthy5 alpha_result (raw_size_thms @ raw_size_eqvt)
   293       |> map mk_funs_rsp
   321       |> map mk_funs_rsp
   294 
   322 
   295   val raw_constrs_rsp = 
   323   val raw_constrs_rsp = 
   296     raw_constrs_rsp lthy5 alpha_result (flat raw_constrs) (alpha_bn_imp_thms @ raw_funs_rsp_aux) 
   324     raw_constrs_rsp lthy5 alpha_result (flat raw_all_cns) (alpha_bn_imp_thms @ raw_funs_rsp_aux) 
   297     
   325     
   298   val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
   326   val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
   299 
   327 
   300   val alpha_bn_rsp = 
   328   val alpha_bn_rsp = 
   301     raw_alpha_bn_rsp alpha_result alpha_bn_equivp_thms alpha_bn_imp_thms
   329     raw_alpha_bn_rsp alpha_result alpha_bn_equivp_thms alpha_bn_imp_thms
   322   val qty_full_names = map (fst o dest_Type) qtys
   350   val qty_full_names = map (fst o dest_Type) qtys
   323   val qty_names = map Long_Name.base_name qty_full_names             
   351   val qty_names = map Long_Name.base_name qty_full_names             
   324 
   352 
   325   val _ = trace_msg (K "Defining the quotient constants...")
   353   val _ = trace_msg (K "Defining the quotient constants...")
   326   val qconstrs_descrs =
   354   val qconstrs_descrs =
   327     (map2 o map2) (fn (b, _, mx) => fn t => (Variable.check_name b, t, mx)) (get_cnstrs dts) raw_constrs
   355     (map2 o map2) (fn (b, _, mx) => fn t => (Variable.check_name b, t, mx)) (get_cnstrs dts) raw_all_cns
   328 
   356 
   329   val qbns_descr =
   357   val qbns_descr =
   330     map2 (fn (b, _, mx) => fn t => (Variable.check_name b, t, mx)) bn_funs raw_bns
   358     map2 (fn (b, _, mx) => fn t => (Variable.check_name b, t, mx)) bn_funs raw_bns
   331 
   359 
   332   val qfvs_descr = 
   360   val qfvs_descr = 
   360       ||>> define_qconsts qtys qfv_bns_descr
   388       ||>> define_qconsts qtys qfv_bns_descr
   361       ||>> define_qconsts qtys qalpha_bns_descr
   389       ||>> define_qconsts qtys qalpha_bns_descr
   362       ||>> define_qconsts qtys qperm_bn_descr
   390       ||>> define_qconsts qtys qperm_bn_descr
   363 
   391 
   364   val lthy9 = 
   392   val lthy9 = 
   365     define_qperms qtys qty_full_names tvs qperm_descr raw_perm_laws lthy8 
   393     define_qperms qtys qty_full_names raw_ty_args qperm_descr raw_perm_laws lthy8 
   366   
   394   
   367   val lthy9a = 
   395   val lthy9a = 
   368     define_qsizes qtys qty_full_names tvs qsize_descr lthy9
   396     define_qsizes qtys qty_full_names raw_ty_args qsize_descr lthy9
   369 
   397 
   370   val qtrms = (map o map) #qconst qconstrs_infos
   398   val qtrms = (map o map) #qconst qconstrs_infos
   371   val qbns = map #qconst qbns_info
   399   val qbns = map #qconst qbns_info
   372   val qfvs = map #qconst qfvs_info
   400   val qfvs = map #qconst qfvs_info
   373   val qfv_bns = map #qconst qfv_bns_info
   401   val qfv_bns = map #qconst qfv_bns_info
   406 
   434 
   407   (* finite supp lemmas *)
   435   (* finite supp lemmas *)
   408   val qfsupp_thms = prove_fsupp lthyB qtys qinduct qsupports_thms
   436   val qfsupp_thms = prove_fsupp lthyB qtys qinduct qsupports_thms
   409 
   437 
   410   (* fs instances *)
   438   (* fs instances *)
   411   val lthyC = fs_instance qtys qty_full_names tvs qfsupp_thms lthyB
   439   val lthyC = fs_instance qtys qty_full_names raw_ty_args qfsupp_thms lthyB
   412 
   440 
   413   val _ = trace_msg (K "Proving equality between fv and supp...")
   441   val _ = trace_msg (K "Proving equality between fv and supp...")
   414   val qfv_supp_thms = 
   442   val qfv_supp_thms = 
   415     prove_fv_supp qtys (flat qtrms) qfvs qfv_bns qalpha_bns qfv_defs qeq_iffs 
   443     prove_fv_supp qtys (flat qtrms) qfvs qfv_bns qalpha_bns qfv_defs qeq_iffs 
   416       qperm_simps qfv_qbn_eqvts qinduct (flat raw_bclauses) lthyC
   444       qperm_simps qfv_qbn_eqvts qinduct (flat raw_bclauses) lthyC