src/ZF/Tools/datatype_package.ML
changeset 74319 54b2e5f771da
parent 74296 abc878973216
child 74342 5d411d85da8c
equal deleted inserted replaced
74318:3360ea6b659d 74319:54b2e5f771da
   113 
   113 
   114   (*** Define the case operator ***)
   114   (*** Define the case operator ***)
   115 
   115 
   116   (*Combine split terms using case; yields the case operator for one part*)
   116   (*Combine split terms using case; yields the case operator for one part*)
   117   fun call_case case_list =
   117   fun call_case case_list =
   118     let fun call_f (free,[]) = Abs("null", \<^typ>\<open>i\<close>, free)
   118     let fun call_f (free,[]) = Abs("null", \<^Type>\<open>i\<close>, free)
   119           | call_f (free,args) =
   119           | call_f (free,args) =
   120                 CP.ap_split (foldr1 CP.mk_prod (map (#2 o dest_Free) args))
   120                 CP.ap_split (foldr1 CP.mk_prod (map (#2 o dest_Free) args))
   121                             \<^typ>\<open>i\<close>
   121                             \<^Type>\<open>i\<close>
   122                             free
   122                             free
   123     in  Balanced_Tree.make (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_f case_list)  end;
   123     in  Balanced_Tree.make (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_f case_list)  end;
   124 
   124 
   125   (** Generating function variables for the case definition
   125   (** Generating function variables for the case definition
   126       Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
   126       Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
   160 
   160 
   161   (** Generating function variables for the recursor definition
   161   (** Generating function variables for the recursor definition
   162       Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
   162       Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
   163 
   163 
   164   (*a recursive call for x is the application rec`x  *)
   164   (*a recursive call for x is the application rec`x  *)
   165   val rec_call = \<^Const>\<open>apply\<close> $ Free ("rec", \<^typ>\<open>i\<close>);
   165   val rec_call = \<^Const>\<open>apply\<close> $ Free ("rec", \<^Type>\<open>i\<close>);
   166 
   166 
   167   (*look back down the "case args" (which have been reversed) to
   167   (*look back down the "case args" (which have been reversed) to
   168     determine the de Bruijn index*)
   168     determine the de Bruijn index*)
   169   fun make_rec_call ([], _) arg = error
   169   fun make_rec_call ([], _) arg = error
   170           "Internal error in datatype (variable name mismatch)"
   170           "Internal error in datatype (variable name mismatch)"
   197     | rec_args (_::prems) = rec_args prems;
   197     | rec_args (_::prems) = rec_args prems;
   198 
   198 
   199   (*Add an argument position for each occurrence of a recursive set.
   199   (*Add an argument position for each occurrence of a recursive set.
   200     Strictly speaking, the recursive arguments are the LAST of the function
   200     Strictly speaking, the recursive arguments are the LAST of the function
   201     variable, but they all have type "i" anyway*)
   201     variable, but they all have type "i" anyway*)
   202   fun add_rec_args args' T = (map (fn _ => \<^typ>\<open>i\<close>) args') ---> T
   202   fun add_rec_args args' T = (map (fn _ => \<^Type>\<open>i\<close>) args') ---> T
   203 
   203 
   204   (*Plug in the function variable type needed for the recursor
   204   (*Plug in the function variable type needed for the recursor
   205     as well as the new arguments (recursive calls)*)
   205     as well as the new arguments (recursive calls)*)
   206   fun rec_ty_elem ((id, T, syn), name, args, prems) =
   206   fun rec_ty_elem ((id, T, syn), name, args, prems) =
   207       let val args' = rec_args prems
   207       let val args' = rec_args prems
   230 
   230 
   231   val recursor_def =
   231   val recursor_def =
   232       Misc_Legacy.mk_defpair
   232       Misc_Legacy.mk_defpair
   233         (recursor_tm,
   233         (recursor_tm,
   234          \<^Const>\<open>Univ.Vrecursor\<close> $
   234          \<^Const>\<open>Univ.Vrecursor\<close> $
   235            absfree ("rec", \<^typ>\<open>i\<close>) (list_comb (case_const, recursor_cases)));
   235            absfree ("rec", \<^Type>\<open>i\<close>) (list_comb (case_const, recursor_cases)));
   236 
   236 
   237   (* Build the new theory *)
   237   (* Build the new theory *)
   238 
   238 
   239   val need_recursor = (not coind andalso recursor_typ <> case_typ);
   239   val need_recursor = (not coind andalso recursor_typ <> case_typ);
   240 
   240 
   410 
   410 
   411 fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy =
   411 fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy =
   412   let
   412   let
   413     val ctxt = Proof_Context.init_global thy;
   413     val ctxt = Proof_Context.init_global thy;
   414     fun read_is strs =
   414     fun read_is strs =
   415       map (Syntax.parse_term ctxt #> Type.constraint \<^typ>\<open>i\<close>) strs
   415       map (Syntax.parse_term ctxt #> Type.constraint \<^Type>\<open>i\<close>) strs
   416       |> Syntax.check_terms ctxt;
   416       |> Syntax.check_terms ctxt;
   417 
   417 
   418     val rec_tms = read_is srec_tms;
   418     val rec_tms = read_is srec_tms;
   419     val con_ty_lists = Ind_Syntax.read_constructs ctxt scon_ty_lists;
   419     val con_ty_lists = Ind_Syntax.read_constructs ctxt scon_ty_lists;
   420     val dom_sum =
   420     val dom_sum =