src/HOL/BNF/Tools/bnf_fp_def_sugar.ML
changeset 53592 5a7bf8c859f6
parent 53591 b6e2993fd0d3
child 53645 44f15d386aae
equal deleted inserted replaced
53591:b6e2993fd0d3 53592:5a7bf8c859f6
    51         * term list list list list) list option
    51         * term list list list list) list option
    52      * (string * term list * term list list
    52      * (string * term list * term list list
    53         * ((term list list * term list list list) * (typ list * typ list list)) list) option)
    53         * ((term list list * term list list list) * (typ list * typ list list)) list) option)
    54     * Proof.context
    54     * Proof.context
    55 
    55 
    56   val mk_iter_fun_arg_types: typ list -> int list -> int list list -> term ->
    56   val mk_iter_fun_arg_types: typ list list list -> int list -> int list list -> term ->
    57     typ list list list list
    57     typ list list list list
    58   val mk_coiter_fun_arg_types: typ list list list -> typ list -> int list -> term ->
    58   val mk_coiter_fun_arg_types: typ list list list -> typ list -> int list -> term ->
    59     typ list list
    59     typ list list
    60     * (typ list list list list * typ list list list * typ list list list list * typ list)
    60     * (typ list list list list * typ list list list * typ list list list list * typ list)
    61   val define_iters: string list ->
    61   val define_iters: string list ->
   266     map (Term.subst_TVars rho) ts0
   266     map (Term.subst_TVars rho) ts0
   267   end;
   267   end;
   268 
   268 
   269 val mk_fp_iter_fun_types = binder_fun_types o fastype_of;
   269 val mk_fp_iter_fun_types = binder_fun_types o fastype_of;
   270 
   270 
   271 (* ### FIXME? *)
   271 fun unzip_recT (Type (@{type_name prod}, _)) T = [T]
   272 fun unzip_recT Cs (T as Type (@{type_name prod}, Ts as [_, U])) =
   272   | unzip_recT _ (T as Type (@{type_name prod}, Ts)) = Ts
   273     if member (op =) Cs U then Ts else [T]
       
   274   | unzip_recT _ T = [T];
   273   | unzip_recT _ T = [T];
   275 
   274 
   276 fun unzip_corecT (Type (@{type_name sum}, _)) T = [T]
   275 fun unzip_corecT (Type (@{type_name sum}, _)) T = [T]
   277   | unzip_corecT _ (T as Type (@{type_name sum}, Ts)) = Ts
   276   | unzip_corecT _ (T as Type (@{type_name sum}, Ts)) = Ts
   278   | unzip_corecT _ T = [T];
   277   | unzip_corecT _ T = [T];
   397 
   396 
   398 fun indexify proj xs f p = f (find_index (curry op = (proj p)) xs) p;
   397 fun indexify proj xs f p = f (find_index (curry op = (proj p)) xs) p;
   399 
   398 
   400 fun mk_iter_fun_arg_types0 n ms = map2 dest_tupleT ms o dest_sumTN_balanced n o domain_type;
   399 fun mk_iter_fun_arg_types0 n ms = map2 dest_tupleT ms o dest_sumTN_balanced n o domain_type;
   401 
   400 
   402 fun mk_iter_fun_arg_types Cs ns mss =
   401 fun mk_iter_fun_arg_types ctr_Tsss ns mss =
   403   mk_fp_iter_fun_types
   402   mk_fp_iter_fun_types
   404   #> map3 mk_iter_fun_arg_types0 ns mss
   403   #> map3 mk_iter_fun_arg_types0 ns mss
   405   #> map (map (map (unzip_recT Cs)));
   404   #> map2 (map2 (map2 unzip_recT)) ctr_Tsss;
   406 
   405 
   407 fun mk_iters_args_types Cs ns mss ctor_iter_fun_Tss lthy =
   406 fun mk_iters_args_types ctr_Tsss Cs ns mss ctor_iter_fun_Tss lthy =
   408   let
   407   let
   409     val Css = map2 replicate ns Cs;
   408     val Css = map2 replicate ns Cs;
   410     val y_Tsss = map3 mk_iter_fun_arg_types0 ns mss (map un_fold_of ctor_iter_fun_Tss);
   409     val y_Tsss = map3 mk_iter_fun_arg_types0 ns mss (map un_fold_of ctor_iter_fun_Tss);
   411     val g_Tss = map2 (fn C => map (fn y_Ts => y_Ts ---> C)) Cs y_Tsss;
   410     val g_Tss = map2 (fn C => map (fn y_Ts => y_Ts ---> C)) Cs y_Tsss;
   412 
   411 
   417 
   416 
   418     val y_Tssss = map (map (map single)) y_Tsss;
   417     val y_Tssss = map (map (map single)) y_Tsss;
   419     val yssss = map (map (map single)) ysss;
   418     val yssss = map (map (map single)) ysss;
   420 
   419 
   421     val z_Tssss =
   420     val z_Tssss =
   422       map3 (fn n => fn ms => map2 (map (unzip_recT Cs) oo dest_tupleT) ms o
   421       map4 (fn n => fn ms => fn ctr_Tss => fn ctor_iter_fun_Ts =>
   423         dest_sumTN_balanced n o domain_type o co_rec_of) ns mss ctor_iter_fun_Tss;
   422           map3 (fn m => fn ctr_Ts => fn ctor_iter_fun_T =>
       
   423               map2 unzip_recT ctr_Ts (dest_tupleT m ctor_iter_fun_T))
       
   424             ms ctr_Tss (dest_sumTN_balanced n (domain_type (co_rec_of ctor_iter_fun_Ts))))
       
   425         ns mss ctr_Tsss ctor_iter_fun_Tss;
   424 
   426 
   425     val z_Tsss' = map (map flat_rec_arg_args) z_Tssss;
   427     val z_Tsss' = map (map flat_rec_arg_args) z_Tssss;
   426     val h_Tss = map2 (map2 (curry op --->)) z_Tsss' Css;
   428     val h_Tss = map2 (map2 (curry op --->)) z_Tsss' Css;
   427 
   429 
   428     val hss = map2 (map2 retype_free) h_Tss gss;
   430     val hss = map2 (map2 retype_free) h_Tss gss;
   520       map (mk_co_iters thy fp fpTs Cs #> `(mk_fp_iter_fun_types o hd)) (transpose xtor_co_iterss0)
   522       map (mk_co_iters thy fp fpTs Cs #> `(mk_fp_iter_fun_types o hd)) (transpose xtor_co_iterss0)
   521       |> apsnd transpose o apfst transpose o split_list;
   523       |> apsnd transpose o apfst transpose o split_list;
   522 
   524 
   523     val ((iters_args_types, coiters_args_types), lthy') =
   525     val ((iters_args_types, coiters_args_types), lthy') =
   524       if fp = Least_FP then
   526       if fp = Least_FP then
   525         mk_iters_args_types Cs ns mss xtor_co_iter_fun_Tss lthy |>> (rpair NONE o SOME)
   527         mk_iters_args_types ctr_Tsss Cs ns mss xtor_co_iter_fun_Tss lthy |>> (rpair NONE o SOME)
   526       else
   528       else
   527         mk_coiters_args_types ctr_Tsss Cs ns mss xtor_co_iter_fun_Tss lthy |>> (pair NONE o SOME)
   529         mk_coiters_args_types ctr_Tsss Cs ns mss xtor_co_iter_fun_Tss lthy |>> (pair NONE o SOME)
   528   in
   530   in
   529     ((xtor_co_iterss, iters_args_types, coiters_args_types), lthy')
   531     ((xtor_co_iterss, iters_args_types, coiters_args_types), lthy')
   530   end;
   532   end;