src/HOL/Codatatype/Tools/bnf_fp_sugar.ML
author blanchet
Tue Sep 04 14:21:11 2012 +0200 (2012-09-04)
changeset 49124 968e1b7de057
parent 49123 263b0e330d8b
child 49125 5fc5211cf104
permissions -rw-r--r--
more work on FP sugar
blanchet@49112
     1
(*  Title:      HOL/Codatatype/Tools/bnf_fp_sugar.ML
blanchet@49112
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49112
     3
    Copyright   2012
blanchet@49112
     4
blanchet@49112
     5
Sugar for constructing LFPs and GFPs.
blanchet@49112
     6
*)
blanchet@49112
     7
blanchet@49112
     8
signature BNF_FP_SUGAR =
blanchet@49112
     9
sig
blanchet@49112
    10
end;
blanchet@49112
    11
blanchet@49112
    12
structure BNF_FP_Sugar : BNF_FP_SUGAR =
blanchet@49112
    13
struct
blanchet@49112
    14
blanchet@49119
    15
open BNF_Util
blanchet@49119
    16
open BNF_Wrap
blanchet@49119
    17
open BNF_FP_Util
blanchet@49119
    18
open BNF_LFP
blanchet@49119
    19
open BNF_GFP
blanchet@49123
    20
open BNF_FP_Sugar_Tactics
blanchet@49119
    21
blanchet@49124
    22
fun cannot_merge_types () = error "Mutually recursive types must have the same type parameters";
blanchet@49119
    23
blanchet@49119
    24
fun merge_type_arg_constrained ctxt (T, c) (T', c') =
blanchet@49119
    25
  if T = T' then
blanchet@49119
    26
    (case (c, c') of
blanchet@49119
    27
      (_, NONE) => (T, c)
blanchet@49119
    28
    | (NONE, _) => (T, c')
blanchet@49119
    29
    | _ =>
blanchet@49119
    30
      if c = c' then
blanchet@49119
    31
        (T, c)
blanchet@49119
    32
      else
blanchet@49119
    33
        error ("Inconsistent sort constraints for type variable " ^
blanchet@49119
    34
          quote (Syntax.string_of_typ ctxt T)))
blanchet@49119
    35
  else
blanchet@49119
    36
    cannot_merge_types ();
blanchet@49119
    37
blanchet@49119
    38
fun merge_type_args_constrained ctxt (cAs, cAs') =
blanchet@49119
    39
  if length cAs = length cAs' then map2 (merge_type_arg_constrained ctxt) cAs cAs'
blanchet@49119
    40
  else cannot_merge_types ();
blanchet@49119
    41
blanchet@49121
    42
fun type_args_constrained_of (((cAs, _), _), _) = cAs;
blanchet@49121
    43
val type_args_of = map fst o type_args_constrained_of;
blanchet@49121
    44
fun type_name_of (((_, b), _), _) = b;
blanchet@49121
    45
fun mixfix_of_typ ((_, mx), _) = mx;
blanchet@49121
    46
fun ctr_specs_of (_, ctr_specs) = ctr_specs;
blanchet@49119
    47
blanchet@49121
    48
fun disc_of (((disc, _), _), _) = disc;
blanchet@49121
    49
fun ctr_of (((_, ctr), _), _) = ctr;
blanchet@49121
    50
fun args_of ((_, args), _) = args;
blanchet@49121
    51
fun mixfix_of_ctr (_, mx) = mx;
blanchet@49119
    52
blanchet@49119
    53
val lfp_info = bnf_lfp;
blanchet@49119
    54
val gfp_info = bnf_gfp;
blanchet@49119
    55
blanchet@49121
    56
fun prepare_data prepare_typ construct specs fake_lthy lthy =
blanchet@49112
    57
  let
blanchet@49121
    58
    val constrained_As =
blanchet@49121
    59
      map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs
blanchet@49119
    60
      |> Library.foldr1 (merge_type_args_constrained lthy);
blanchet@49121
    61
    val As = map fst constrained_As;
blanchet@49119
    62
blanchet@49121
    63
    val _ = (case duplicates (op =) As of [] => ()
blanchet@49119
    64
      | T :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy T)));
blanchet@49119
    65
blanchet@49119
    66
    (* TODO: check that no type variables occur in the rhss that's not in the lhss *)
blanchet@49119
    67
    (* TODO: use sort constraints on type args *)
blanchet@49119
    68
blanchet@49119
    69
    val N = length specs;
blanchet@49119
    70
blanchet@49121
    71
    fun mk_T b =
blanchet@49121
    72
      Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))),
blanchet@49121
    73
        As);
blanchet@49121
    74
blanchet@49121
    75
    val bs = map type_name_of specs;
blanchet@49121
    76
    val Ts = map mk_T bs;
blanchet@49121
    77
blanchet@49121
    78
    val mixfixes = map mixfix_of_typ specs;
blanchet@49119
    79
blanchet@49119
    80
    val _ = (case duplicates Binding.eq_name bs of [] => ()
blanchet@49119
    81
      | b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b)));
blanchet@49119
    82
blanchet@49121
    83
    val ctr_specss = map ctr_specs_of specs;
blanchet@49119
    84
blanchet@49121
    85
    val disc_namess = map (map disc_of) ctr_specss;
blanchet@49121
    86
    val ctr_namess = map (map ctr_of) ctr_specss;
blanchet@49121
    87
    val ctr_argsss = map (map args_of) ctr_specss;
blanchet@49121
    88
    val ctr_mixfixess = map (map mixfix_of_ctr) ctr_specss;
blanchet@49119
    89
blanchet@49119
    90
    val sel_namesss = map (map (map fst)) ctr_argsss;
blanchet@49121
    91
    val ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss;
blanchet@49121
    92
blanchet@49121
    93
    val (Bs, C) =
blanchet@49121
    94
      lthy
blanchet@49121
    95
      |> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs
blanchet@49121
    96
      |> mk_TFrees N
blanchet@49121
    97
      ||> the_single o fst o mk_TFrees 1;
blanchet@49119
    98
blanchet@49121
    99
    fun freeze_rec (T as Type (s, Ts')) =
blanchet@49121
   100
        (case find_index (curry (op =) T) Ts of
blanchet@49121
   101
          ~1 => Type (s, map freeze_rec Ts')
blanchet@49121
   102
        | i => nth Bs i)
blanchet@49121
   103
      | freeze_rec T = T;
blanchet@49121
   104
blanchet@49121
   105
    val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss;
blanchet@49121
   106
    val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs;
blanchet@49119
   107
blanchet@49121
   108
    val eqs = map dest_TFree Bs ~~ sum_prod_TsBs;
blanchet@49121
   109
blanchet@49124
   110
    val ((raw_flds, raw_unfs, fld_unfs, unf_flds), lthy') = fp_bnf construct bs eqs lthy;
blanchet@49121
   111
blanchet@49124
   112
    fun mk_fld_or_unf get_foldedT Ts t =
blanchet@49124
   113
      let val Type (_, Ts0) = get_foldedT (fastype_of t) in
blanchet@49124
   114
        Term.subst_atomic_types (Ts0 ~~ Ts) t
blanchet@49121
   115
      end;
blanchet@49119
   116
blanchet@49124
   117
    val mk_fld = mk_fld_or_unf range_type;
blanchet@49124
   118
    val mk_unf = mk_fld_or_unf domain_type;
blanchet@49121
   119
blanchet@49124
   120
    val flds = map (mk_fld As) raw_flds;
blanchet@49124
   121
    val unfs = map (mk_unf As) raw_unfs;
blanchet@49124
   122
blanchet@49124
   123
    fun wrap_type ((((((((T, fld), unf), fld_unf), unf_fld), ctr_names), ctr_Tss), disc_names),
blanchet@49124
   124
        sel_namess) no_defs_lthy =
blanchet@49121
   125
      let
blanchet@49121
   126
        val n = length ctr_names;
blanchet@49121
   127
        val ks = 1 upto n;
blanchet@49121
   128
        val ms = map length ctr_Tss;
blanchet@49121
   129
blanchet@49124
   130
        val unf_T = domain_type (fastype_of fld);
blanchet@49124
   131
blanchet@49121
   132
        val prod_Ts = map HOLogic.mk_tupleT ctr_Tss;
blanchet@49119
   133
blanchet@49124
   134
        val (((u, v), xss), _) =
blanchet@49124
   135
          lthy
blanchet@49124
   136
          |> yield_singleton (mk_Frees "u") unf_T
blanchet@49124
   137
          ||>> yield_singleton (mk_Frees "v") T
blanchet@49124
   138
          ||>> mk_Freess "x" ctr_Tss;
blanchet@49121
   139
blanchet@49121
   140
        val rhss =
blanchet@49121
   141
          map2 (fn k => fn xs =>
blanchet@49121
   142
            fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss;
blanchet@49121
   143
blanchet@49121
   144
        val ((raw_ctrs, raw_ctr_defs), (lthy', lthy)) = no_defs_lthy
blanchet@49121
   145
          |> apfst split_list o fold_map2 (fn b => fn rhs =>
blanchet@49121
   146
               Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs)) #>> apsnd snd)
blanchet@49121
   147
             ctr_names rhss
blanchet@49121
   148
          ||> `Local_Theory.restore;
blanchet@49121
   149
blanchet@49121
   150
        val raw_caseof =
blanchet@49121
   151
          Const (@{const_name undefined}, map (fn Ts => Ts ---> C) ctr_Tss ---> T --> C);
blanchet@49121
   152
blanchet@49121
   153
        (*transforms defined frees into consts (and more)*)
blanchet@49121
   154
        val phi = Proof_Context.export_morphism lthy lthy';
blanchet@49121
   155
blanchet@49121
   156
        val ctr_defs = map (Morphism.thm phi) raw_ctr_defs;
blanchet@49123
   157
        val ctrs = map (Morphism.term phi) raw_ctrs;
blanchet@49123
   158
        val caseof = Morphism.term phi raw_caseof;
blanchet@49121
   159
blanchet@49123
   160
        val fld_iff_unf_thm =
blanchet@49123
   161
          let
blanchet@49124
   162
            val goal =
blanchet@49124
   163
              fold_rev Logic.all [u, v]
blanchet@49124
   164
                (mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u)));
blanchet@49123
   165
          in
blanchet@49123
   166
            Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} =>
blanchet@49124
   167
              mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unf_T, T]) (certify lthy fld)
blanchet@49123
   168
                (certify lthy unf) fld_unf unf_fld)
blanchet@49123
   169
          end;
blanchet@49121
   170
blanchet@49121
   171
        (* ### *)
blanchet@49121
   172
        fun cheat_tac {context = ctxt, ...} = Skip_Proof.cheat_tac (Proof_Context.theory_of ctxt);
blanchet@49121
   173
blanchet@49121
   174
        val exhaust_tac = cheat_tac;
blanchet@49121
   175
blanchet@49121
   176
        val inject_tacss = map (fn 0 => [] | _ => [cheat_tac]) ms;
blanchet@49121
   177
blanchet@49121
   178
        val half_distinct_tacss = map (map (K cheat_tac)) (mk_half_pairss ks);
blanchet@49121
   179
blanchet@49121
   180
        val case_tacs = map (K cheat_tac) ks;
blanchet@49121
   181
blanchet@49121
   182
        val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs];
blanchet@49119
   183
      in
blanchet@49121
   184
        wrap_data tacss ((ctrs, caseof), (disc_names, sel_namess)) lthy'
blanchet@49119
   185
      end;
blanchet@49112
   186
  in
blanchet@49124
   187
    lthy'
blanchet@49124
   188
    |> fold wrap_type (Ts ~~ flds ~~ unfs ~~ fld_unfs ~~ unf_flds ~~ ctr_namess ~~ ctr_Tsss ~~
blanchet@49124
   189
      disc_namess ~~ sel_namesss)
blanchet@49112
   190
  end;
blanchet@49112
   191
blanchet@49121
   192
fun data_cmd info specs lthy =
blanchet@49121
   193
  let
blanchet@49121
   194
    val fake_lthy =
blanchet@49121
   195
      Proof_Context.theory_of lthy
blanchet@49121
   196
      |> Theory.copy
blanchet@49121
   197
      |> Sign.add_types_global (map (fn spec =>
blanchet@49121
   198
        (type_name_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs)
blanchet@49121
   199
      |> Proof_Context.init_global
blanchet@49121
   200
  in
blanchet@49121
   201
    prepare_data Syntax.read_typ info specs fake_lthy lthy
blanchet@49121
   202
  end;
blanchet@49119
   203
blanchet@49119
   204
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_name
blanchet@49119
   205
blanchet@49112
   206
val parse_ctr_arg =
blanchet@49119
   207
  Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" ||
blanchet@49119
   208
  (Parse.typ >> pair no_name);
blanchet@49112
   209
blanchet@49112
   210
val parse_single_spec =
blanchet@49112
   211
  Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --
blanchet@49119
   212
  (@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding --
blanchet@49119
   213
    Scan.repeat parse_ctr_arg -- Parse.opt_mixfix));
blanchet@49112
   214
blanchet@49112
   215
val _ =
blanchet@49112
   216
  Outer_Syntax.local_theory @{command_spec "data"} "define BNF-based inductive datatypes"
blanchet@49119
   217
    (Parse.and_list1 parse_single_spec >> data_cmd lfp_info);
blanchet@49112
   218
blanchet@49112
   219
val _ =
blanchet@49112
   220
  Outer_Syntax.local_theory @{command_spec "codata"} "define BNF-based coinductive datatypes"
blanchet@49119
   221
    (Parse.and_list1 parse_single_spec >> data_cmd gfp_info);
blanchet@49112
   222
blanchet@49112
   223
end;