src/HOL/Tools/BNF/bnf_fp_def_sugar.ML
changeset 63787 ad2036bb81c6
parent 63786 b87d9d2ca67b
child 63796 45c8762353dd
equal deleted inserted replaced
63786:b87d9d2ca67b 63787:ad2036bb81c6
    80   val fp_sugars_interpretation: string -> (fp_sugar list -> local_theory -> local_theory) ->
    80   val fp_sugars_interpretation: string -> (fp_sugar list -> local_theory -> local_theory) ->
    81     theory -> theory
    81     theory -> theory
    82   val interpret_fp_sugars: (string -> bool) -> fp_sugar list -> local_theory -> local_theory
    82   val interpret_fp_sugars: (string -> bool) -> fp_sugar list -> local_theory -> local_theory
    83   val register_fp_sugars_raw: fp_sugar list -> local_theory -> local_theory
    83   val register_fp_sugars_raw: fp_sugar list -> local_theory -> local_theory
    84   val register_fp_sugars: (string -> bool) -> fp_sugar list -> local_theory -> local_theory
    84   val register_fp_sugars: (string -> bool) -> fp_sugar list -> local_theory -> local_theory
       
    85 
       
    86   val merge_type_args: BNF_Util.fp_kind -> ''a list * ''a list -> ''a list
       
    87   val type_args_named_constrained_of_spec: (((('a * 'b) * 'c) * 'd) * 'e) * 'f -> 'a
       
    88   val type_binding_of_spec: (((('a * 'b) * 'c) * 'd) * 'e) * 'f -> 'b
       
    89   val mixfix_of_spec: ((('a * 'b) * 'c) * 'd) * 'e -> 'b
       
    90   val mixfixed_ctr_specs_of_spec: (('a * 'b) * 'c) * 'd -> 'b
       
    91   val map_binding_of_spec: ('a * ('b * 'c * 'd)) * 'e -> 'b
       
    92   val rel_binding_of_spec: ('a * ('b * 'c * 'd)) * 'e -> 'c
       
    93   val pred_binding_of_spec: ('a * ('b * 'c * 'd)) * 'e -> 'd
       
    94   val sel_default_eqs_of_spec: 'a * 'b -> 'b
    85 
    95 
    86   val mk_parametricity_goal: Proof.context -> term list -> term -> term -> term
    96   val mk_parametricity_goal: Proof.context -> term list -> term -> term -> term
    87 
    97 
    88   val flat_corec_preds_predsss_gettersss: 'a list -> 'a list list list -> 'a list list list ->
    98   val flat_corec_preds_predsss_gettersss: 'a list -> 'a list list list -> 'a list list list ->
    89     'a list
    99     'a list
   466 fun unfla xs = fold_map (fn _ => fn (c :: cs) => (c, cs)) xs;
   476 fun unfla xs = fold_map (fn _ => fn (c :: cs) => (c, cs)) xs;
   467 fun unflat xss = fold_map unfla xss;
   477 fun unflat xss = fold_map unfla xss;
   468 fun unflatt xsss = fold_map unflat xsss;
   478 fun unflatt xsss = fold_map unflat xsss;
   469 fun unflattt xssss = fold_map unflatt xssss;
   479 fun unflattt xssss = fold_map unflatt xssss;
   470 
   480 
       
   481 fun cannot_merge_types fp =
       
   482   error ("Mutually " ^ co_prefix fp ^ "recursive types must have the same type parameters");
       
   483 
       
   484 fun merge_type_arg fp T T' = if T = T' then T else cannot_merge_types fp;
       
   485 
       
   486 fun merge_type_args fp (As, As') =
       
   487   if length As = length As' then map2 (merge_type_arg fp) As As' else cannot_merge_types fp;
       
   488 
       
   489 fun type_args_named_constrained_of_spec (((((ncAs, _), _), _), _), _) = ncAs;
       
   490 fun type_binding_of_spec (((((_, b), _), _), _), _) = b;
       
   491 fun mixfix_of_spec ((((_, mx), _), _), _) = mx;
       
   492 fun mixfixed_ctr_specs_of_spec (((_, mx_ctr_specs), _), _) = mx_ctr_specs;
       
   493 fun map_binding_of_spec ((_, (b, _, _)), _) = b;
       
   494 fun rel_binding_of_spec ((_, (_, b, _)), _) = b;
       
   495 fun pred_binding_of_spec ((_, (_, _, b)), _) = b;
       
   496 fun sel_default_eqs_of_spec (_, ts) = ts;
       
   497 
   471 fun ctr_sugar_kind_of_fp_kind Least_FP = Datatype
   498 fun ctr_sugar_kind_of_fp_kind Least_FP = Datatype
   472   | ctr_sugar_kind_of_fp_kind Greatest_FP = Codatatype;
   499   | ctr_sugar_kind_of_fp_kind Greatest_FP = Codatatype;
   473 
   500 
   474 fun uncurry_thm 0 thm = thm
   501 fun uncurry_thm 0 thm = thm
   475   | uncurry_thm 1 thm = thm
   502   | uncurry_thm 1 thm = thm
   539 
   566 
   540 fun liveness_of_fp_bnf n bnf =
   567 fun liveness_of_fp_bnf n bnf =
   541   (case T_of_bnf bnf of
   568   (case T_of_bnf bnf of
   542     Type (_, Ts) => map (not o member (op =) (deads_of_bnf bnf)) Ts
   569     Type (_, Ts) => map (not o member (op =) (deads_of_bnf bnf)) Ts
   543   | _ => replicate n false);
   570   | _ => replicate n false);
   544 
       
   545 fun cannot_merge_types fp =
       
   546   error ("Mutually " ^ co_prefix fp ^ "recursive types must have the same type parameters");
       
   547 
       
   548 fun merge_type_arg fp T T' = if T = T' then T else cannot_merge_types fp;
       
   549 
       
   550 fun merge_type_args fp (As, As') =
       
   551   if length As = length As' then map2 (merge_type_arg fp) As As' else cannot_merge_types fp;
       
   552 
       
   553 fun type_args_named_constrained_of_spec (((((ncAs, _), _), _), _), _) = ncAs;
       
   554 fun type_binding_of_spec (((((_, b), _), _), _), _) = b;
       
   555 fun mixfix_of_spec ((((_, mx), _), _), _) = mx;
       
   556 fun mixfixed_ctr_specs_of_spec (((_, mx_ctr_specs), _), _) = mx_ctr_specs;
       
   557 fun map_binding_of_spec ((_, (b, _, _)), _) = b;
       
   558 fun rel_binding_of_spec ((_, (_, b, _)), _) = b;
       
   559 fun pred_binding_of_spec ((_, (_, _, b)), _) = b;
       
   560 fun sel_default_eqs_of_spec (_, ts) = ts;
       
   561 
   571 
   562 fun add_nesting_bnf_names Us =
   572 fun add_nesting_bnf_names Us =
   563   let
   573   let
   564     fun add (Type (s, Ts)) ss =
   574     fun add (Type (s, Ts)) ss =
   565         let val (needs, ss') = fold_map add Ts ss in
   575         let val (needs, ss') = fold_map add Ts ss in
  2030   end;
  2040   end;
  2031 
  2041 
  2032 fun define_co_datatypes prepare_plugins prepare_constraint prepare_typ prepare_term fp construct_fp
  2042 fun define_co_datatypes prepare_plugins prepare_constraint prepare_typ prepare_term fp construct_fp
  2033     ((raw_plugins, discs_sels0), specs) no_defs_lthy =
  2043     ((raw_plugins, discs_sels0), specs) no_defs_lthy =
  2034   let
  2044   let
  2035     (* TODO: sanity checks on arguments *)
       
  2036 
       
  2037     val plugins = prepare_plugins no_defs_lthy raw_plugins;
  2045     val plugins = prepare_plugins no_defs_lthy raw_plugins;
  2038     val discs_sels = discs_sels0 orelse fp = Greatest_FP;
  2046     val discs_sels = discs_sels0 orelse fp = Greatest_FP;
  2039 
  2047 
  2040     val nn = length specs;
  2048     val nn = length specs;
  2041     val fp_bs = map type_binding_of_spec specs;
  2049     val fp_bs = map type_binding_of_spec specs;