src/HOL/Codatatype/Tools/bnf_fp_util.ML
author blanchet
Sat Sep 08 21:04:26 2012 +0200 (2012-09-08)
changeset 49218 d01a5c918298
parent 49207 4634c217b77b
child 49222 cbe8c859817c
permissions -rw-r--r--
renamed xxxBNF to pre_xxx
blanchet@48975
     1
(*  Title:      HOL/Codatatype/Tools/bnf_fp_util.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Copyright   2012
blanchet@48975
     4
blanchet@48975
     5
Shared library for the datatype and the codatatype construction.
blanchet@48975
     6
*)
blanchet@48975
     7
blanchet@48975
     8
signature BNF_FP_UTIL =
blanchet@48975
     9
sig
blanchet@48975
    10
  val time: Timer.real_timer -> string -> Timer.real_timer
blanchet@48975
    11
blanchet@48975
    12
  val IITN: string
blanchet@48975
    13
  val LevN: string
blanchet@48975
    14
  val algN: string
blanchet@48975
    15
  val behN: string
blanchet@48975
    16
  val bisN: string
blanchet@48975
    17
  val carTN: string
blanchet@48975
    18
  val coN: string
blanchet@48975
    19
  val coinductN: string
blanchet@48975
    20
  val coiterN: string
blanchet@49128
    21
  val unf_coiter_uniqueN: string
blanchet@48975
    22
  val corecN: string
blanchet@49020
    23
  val exhaustN: string
blanchet@48975
    24
  val fldN: string
blanchet@49128
    25
  val fld_unf_coiterN: string
blanchet@48975
    26
  val fld_exhaustN: string
blanchet@48975
    27
  val fld_induct2N: string
blanchet@48975
    28
  val fld_inductN: string
blanchet@48975
    29
  val fld_injectN: string
blanchet@49128
    30
  val fld_iterN: string
blanchet@49128
    31
  val fld_recN: string
blanchet@48975
    32
  val fld_unfN: string
blanchet@48975
    33
  val hsetN: string
blanchet@48975
    34
  val hset_recN: string
blanchet@48975
    35
  val inductN: string
blanchet@49019
    36
  val injectN: string
blanchet@48975
    37
  val isNodeN: string
blanchet@48975
    38
  val iterN: string
blanchet@49128
    39
  val fld_iter_uniqueN: string
blanchet@48975
    40
  val lsbisN: string
blanchet@48975
    41
  val map_simpsN: string
blanchet@48975
    42
  val map_uniqueN: string
blanchet@48975
    43
  val min_algN: string
blanchet@48975
    44
  val morN: string
blanchet@49020
    45
  val nchotomyN: string
blanchet@48975
    46
  val pred_coinductN: string
blanchet@48975
    47
  val pred_coinduct_uptoN: string
blanchet@48975
    48
  val recN: string
blanchet@48975
    49
  val rel_coinductN: string
blanchet@48975
    50
  val rel_coinduct_uptoN: string
blanchet@48975
    51
  val rvN: string
blanchet@48975
    52
  val set_inclN: string
blanchet@48975
    53
  val set_set_inclN: string
blanchet@48975
    54
  val strTN: string
blanchet@48975
    55
  val str_initN: string
blanchet@48975
    56
  val sum_bdN: string
blanchet@48975
    57
  val sum_bdTN: string
blanchet@48975
    58
  val unfN: string
blanchet@48975
    59
  val unf_coinductN: string
blanchet@48975
    60
  val unf_coinduct_uptoN: string
blanchet@49128
    61
  val unf_coiterN: string
blanchet@49128
    62
  val unf_corecN: string
blanchet@48975
    63
  val unf_exhaustN: string
blanchet@48975
    64
  val unf_fldN: string
blanchet@48975
    65
  val unf_injectN: string
blanchet@48975
    66
  val uniqueN: string
blanchet@48975
    67
  val uptoN: string
blanchet@48975
    68
blanchet@48975
    69
  val mk_exhaustN: string -> string
blanchet@48975
    70
  val mk_injectN: string -> string
blanchet@48975
    71
  val mk_nchotomyN: string -> string
blanchet@48975
    72
  val mk_set_simpsN: int -> string
blanchet@48975
    73
  val mk_set_minimalN: int -> string
blanchet@48975
    74
  val mk_set_inductN: int -> string
blanchet@48975
    75
blanchet@48975
    76
  val typedef: bool -> binding option -> binding * (string * sort) list * mixfix -> term ->
blanchet@48975
    77
    (binding * binding) option -> tactic -> local_theory -> (string * Typedef.info) * local_theory
blanchet@48975
    78
blanchet@48975
    79
  val split_conj_thm: thm -> thm list
blanchet@48975
    80
  val split_conj_prems: int -> thm -> thm
blanchet@48975
    81
blanchet@49121
    82
  val Inl_const: typ -> typ -> term
blanchet@49121
    83
  val Inr_const: typ -> typ -> term
blanchet@49121
    84
blanchet@49121
    85
  val mk_Inl: term -> typ -> term
blanchet@49121
    86
  val mk_Inr: term -> typ -> term
blanchet@49121
    87
  val mk_InN: typ list -> term -> int -> term
blanchet@49129
    88
  val mk_sum_case: term -> term -> term
blanchet@49129
    89
  val mk_sum_caseN: term list -> term
blanchet@49121
    90
blanchet@49176
    91
  val dest_sumTN: int -> typ -> typ list
blanchet@49176
    92
  val dest_tupleT: int -> typ -> typ list
blanchet@49176
    93
blanchet@48975
    94
  val mk_Field: term -> term
blanchet@48975
    95
  val mk_union: term * term -> term
blanchet@48975
    96
blanchet@49125
    97
  val mk_sumEN: int -> thm
blanchet@49130
    98
  val mk_sum_casesN: int -> int -> thm
blanchet@49125
    99
blanchet@48975
   100
  val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
blanchet@48975
   101
blanchet@48975
   102
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@48975
   103
traytel@49185
   104
  val fp_bnf: (mixfix list -> (string * sort) list option -> binding list ->
traytel@49185
   105
    typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
blanchet@49169
   106
    binding list -> mixfix list -> (string * sort) list -> ((string * sort) * typ) list ->
blanchet@49207
   107
    local_theory -> thm list * 'a
traytel@49185
   108
  val fp_bnf_cmd: (mixfix list -> (string * sort) list option -> binding list ->
traytel@49185
   109
    typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
blanchet@49134
   110
    binding list * (string list * string list) -> local_theory -> 'a
blanchet@48975
   111
end;
blanchet@48975
   112
blanchet@48975
   113
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   114
struct
blanchet@48975
   115
blanchet@48975
   116
open BNF_Comp
blanchet@48975
   117
open BNF_Def
blanchet@48975
   118
open BNF_Util
blanchet@48975
   119
blanchet@48975
   120
val timing = true;
blanchet@48975
   121
fun time timer msg = (if timing
blanchet@48975
   122
  then warning (msg ^ ": " ^ ATP_Util.string_from_time (Timer.checkRealTimer timer))
blanchet@48975
   123
  else (); Timer.startRealTimer ());
blanchet@48975
   124
blanchet@48975
   125
(*TODO: is this really different from Typedef.add_typedef_global?*)
blanchet@48975
   126
fun typedef def opt_name typ set opt_morphs tac lthy =
blanchet@48975
   127
  let
blanchet@48975
   128
    val ((name, info), (lthy, lthy_old)) =
blanchet@48975
   129
      lthy
blanchet@48975
   130
      |> Typedef.add_typedef def opt_name typ set opt_morphs tac
blanchet@48975
   131
      ||> `Local_Theory.restore;
blanchet@48975
   132
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   133
  in
blanchet@48975
   134
    ((name, Typedef.transform_info phi info), lthy)
blanchet@48975
   135
  end;
blanchet@48975
   136
blanchet@49218
   137
val pre_N = "pre_"
blanchet@49218
   138
val raw_N = "raw_"
blanchet@49218
   139
blanchet@48975
   140
val coN = "co"
blanchet@48975
   141
val algN = "alg"
blanchet@48975
   142
val IITN = "IITN"
blanchet@48975
   143
val iterN = "iter"
blanchet@48975
   144
val coiterN = coN ^ iterN
blanchet@48975
   145
val uniqueN = "_unique"
blanchet@48975
   146
val fldN = "fld"
blanchet@48975
   147
val unfN = "unf"
blanchet@49128
   148
val fld_iterN = fldN ^ "_" ^ iterN
blanchet@49128
   149
val unf_coiterN = unfN ^ "_" ^ coiterN
blanchet@49128
   150
val fld_iter_uniqueN = fld_iterN ^ uniqueN
blanchet@49128
   151
val unf_coiter_uniqueN = unf_coiterN ^ uniqueN
blanchet@49128
   152
val fld_unf_coiterN = fldN ^ "_" ^ unf_coiterN
blanchet@48975
   153
val map_simpsN = mapN ^ "_simps"
blanchet@48975
   154
val map_uniqueN = mapN ^ uniqueN
blanchet@48975
   155
val min_algN = "min_alg"
blanchet@48975
   156
val morN = "mor"
blanchet@48975
   157
val bisN = "bis"
blanchet@48975
   158
val lsbisN = "lsbis"
blanchet@48975
   159
val sum_bdTN = "sbdT"
blanchet@48975
   160
val sum_bdN = "sbd"
blanchet@48975
   161
val carTN = "carT"
blanchet@48975
   162
val strTN = "strT"
blanchet@48975
   163
val isNodeN = "isNode"
blanchet@48975
   164
val LevN = "Lev"
blanchet@48975
   165
val rvN = "recover"
blanchet@48975
   166
val behN = "beh"
blanchet@48975
   167
fun mk_set_simpsN i = mk_setN i ^ "_simps"
blanchet@48975
   168
fun mk_set_minimalN i = mk_setN i ^ "_minimal"
blanchet@48975
   169
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@48975
   170
blanchet@48975
   171
val str_initN = "str_init"
blanchet@48975
   172
val recN = "rec"
blanchet@48975
   173
val corecN = coN ^ recN
blanchet@49128
   174
val fld_recN = fldN ^ "_" ^ recN
blanchet@49128
   175
val unf_corecN = unfN ^ "_" ^ corecN
blanchet@48975
   176
blanchet@48975
   177
val fld_unfN = fldN ^ "_" ^ unfN
blanchet@48975
   178
val unf_fldN = unfN ^ "_" ^ fldN
blanchet@49020
   179
val nchotomyN = "nchotomy"
blanchet@49020
   180
fun mk_nchotomyN s = s ^ "_" ^ nchotomyN
blanchet@49019
   181
val injectN = "inject"
blanchet@49019
   182
fun mk_injectN s = s ^ "_" ^ injectN
blanchet@49020
   183
val exhaustN = "exhaust"
blanchet@49020
   184
fun mk_exhaustN s = s ^ "_" ^ exhaustN
blanchet@48975
   185
val fld_injectN = mk_injectN fldN
blanchet@48975
   186
val fld_exhaustN = mk_exhaustN fldN
blanchet@48975
   187
val unf_injectN = mk_injectN unfN
blanchet@48975
   188
val unf_exhaustN = mk_exhaustN unfN
blanchet@48975
   189
val inductN = "induct"
blanchet@48975
   190
val coinductN = coN ^ inductN
blanchet@48975
   191
val fld_inductN = fldN ^ "_" ^ inductN
blanchet@48975
   192
val fld_induct2N = fld_inductN ^ "2"
blanchet@48975
   193
val unf_coinductN = unfN ^ "_" ^ coinductN
blanchet@48975
   194
val rel_coinductN = relN ^ "_" ^ coinductN
blanchet@48975
   195
val pred_coinductN = predN ^ "_" ^ coinductN
blanchet@48975
   196
val uptoN = "upto"
blanchet@48975
   197
val unf_coinduct_uptoN = unf_coinductN ^ "_" ^ uptoN
blanchet@48975
   198
val rel_coinduct_uptoN = rel_coinductN ^ "_" ^ uptoN
blanchet@48975
   199
val pred_coinduct_uptoN = pred_coinductN ^ "_" ^ uptoN
blanchet@48975
   200
val hsetN = "Hset"
blanchet@48975
   201
val hset_recN = hsetN ^ "_rec"
blanchet@48975
   202
val set_inclN = "set_incl"
blanchet@48975
   203
val set_set_inclN = "set_set_incl"
blanchet@48975
   204
blanchet@49121
   205
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49121
   206
fun mk_Inl t RT = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   207
blanchet@49121
   208
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49121
   209
fun mk_Inr t LT = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   210
blanchet@49121
   211
fun mk_InN [_] t 1 = t
blanchet@49121
   212
  | mk_InN (_ :: Ts) t 1 = mk_Inl t (mk_sumTN Ts)
blanchet@49121
   213
  | mk_InN (LT :: Ts) t m = mk_Inr (mk_InN Ts t (m - 1)) LT
blanchet@49121
   214
  | mk_InN Ts t _ = raise (TYPE ("mk_InN", Ts, [t]));
blanchet@49121
   215
blanchet@49129
   216
fun mk_sum_case f g =
blanchet@49129
   217
  let
blanchet@49129
   218
    val fT = fastype_of f;
blanchet@49129
   219
    val gT = fastype_of g;
blanchet@49129
   220
  in
blanchet@49129
   221
    Const (@{const_name sum_case},
blanchet@49129
   222
      fT --> gT --> mk_sumT (domain_type fT, domain_type gT) --> range_type fT) $ f $ g
blanchet@49129
   223
  end;
blanchet@49129
   224
blanchet@49129
   225
fun mk_sum_caseN [f] = f
blanchet@49129
   226
  | mk_sum_caseN (f :: fs) = mk_sum_case f (mk_sum_caseN fs);
blanchet@49129
   227
blanchet@49176
   228
fun dest_sumTN 1 T = [T]
blanchet@49176
   229
  | dest_sumTN n (Type (@{type_name sum}, [T, T'])) = T :: dest_sumTN (n - 1) T';
blanchet@49176
   230
blanchet@49176
   231
(* TODO: move something like this to "HOLogic"? *)
blanchet@49176
   232
fun dest_tupleT 0 @{typ unit} = []
blanchet@49176
   233
  | dest_tupleT 1 T = [T]
blanchet@49176
   234
  | dest_tupleT n (Type (@{type_name prod}, [T, T'])) = T :: dest_tupleT (n - 1) T';
blanchet@49176
   235
blanchet@48975
   236
fun mk_Field r =
blanchet@48975
   237
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   238
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   239
blanchet@48975
   240
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   241
blanchet@48975
   242
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   243
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   244
blanchet@48975
   245
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   246
fun split_conj_thm th =
blanchet@49119
   247
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   248
blanchet@48975
   249
fun split_conj_prems limit th =
blanchet@48975
   250
  let
blanchet@48975
   251
    fun split n i th =
blanchet@48975
   252
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   253
  in split limit 1 th end;
blanchet@48975
   254
blanchet@49125
   255
local
blanchet@49125
   256
  fun mk_sumEN' 1 = @{thm obj_sum_step}
blanchet@49125
   257
    | mk_sumEN' n = mk_sumEN' (n - 1) RSN (2, @{thm obj_sum_step});
blanchet@49125
   258
in
blanchet@49125
   259
  fun mk_sumEN 1 = @{thm obj_sum_base}
blanchet@49125
   260
    | mk_sumEN 2 = @{thm sumE}
blanchet@49125
   261
    | mk_sumEN n = (mk_sumEN' (n - 2) RSN (2, @{thm obj_sumE})) OF replicate n (impI RS allI);
blanchet@49125
   262
end;
blanchet@49125
   263
blanchet@49130
   264
fun mk_sum_casesN 1 1 = @{thm refl}
blanchet@49130
   265
  | mk_sum_casesN _ 1 = @{thm sum.cases(1)}
blanchet@49130
   266
  | mk_sum_casesN 2 2 = @{thm sum.cases(2)}
blanchet@49130
   267
  | mk_sum_casesN n m = trans OF [@{thm sum_case_step(2)}, mk_sum_casesN (n - 1) (m - 1)];
blanchet@49130
   268
blanchet@48975
   269
fun mk_tactics mid mcomp mcong snat bdco bdinf sbd inbd wpull =
blanchet@48975
   270
  [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
blanchet@48975
   271
blanchet@49141
   272
(* FIXME: because of "@ lhss", the output could contain type variables that are not in the input;
blanchet@49141
   273
   also, "fp_sort" should put the "resBs" first and in the order in which they appear *)
traytel@49185
   274
fun fp_sort lhss NONE Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
traytel@49185
   275
    (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss
traytel@49185
   276
  | fp_sort lhss (SOME resBs) Ass =
traytel@49185
   277
    (subtract (op =) lhss (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs)) @ lhss;
blanchet@48975
   278
traytel@49185
   279
fun mk_fp_bnf timer construct resBs bs sort lhss bnfs deadss livess unfold lthy =
blanchet@48975
   280
  let
blanchet@48975
   281
    val name = fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "";
blanchet@48975
   282
    fun qualify i bind =
blanchet@48975
   283
      let val namei = if i > 0 then name ^ string_of_int i else name;
blanchet@48975
   284
      in
blanchet@48975
   285
        if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
blanchet@48975
   286
        else Binding.prefix_name namei bind
blanchet@48975
   287
      end;
blanchet@48975
   288
blanchet@49132
   289
    val Ass = map (map dest_TFree) livess;
traytel@49185
   290
    val resDs = (case resBs of NONE => [] | SOME Ts => fold (subtract (op =)) Ass Ts);
traytel@49185
   291
    val Ds = fold (fold Term.add_tfreesT) deadss [];
blanchet@49132
   292
traytel@49156
   293
    val _ = (case Library.inter (op =) Ds lhss of [] => ()
blanchet@49132
   294
      | A :: _ => error ("Nonadmissible type recursion (cannot take fixed point of dead type \
blanchet@49132
   295
        \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
blanchet@48975
   296
blanchet@48975
   297
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   298
blanchet@48975
   299
    val ((kill_poss, _), (bnfs', (unfold', lthy'))) =
blanchet@48975
   300
      normalize_bnfs qualify Ass Ds sort bnfs unfold lthy;
blanchet@48975
   301
blanchet@49132
   302
    val Dss = map3 (append oo map o nth) livess kill_poss deadss;
blanchet@48975
   303
traytel@49185
   304
    val ((bnfs'', deadss), lthy'') =
blanchet@49218
   305
      fold_map3 (seal_bnf unfold') (map (Binding.prefix_name pre_N) bs) Dss bnfs' lthy'
traytel@49185
   306
      |>> split_list;
blanchet@48975
   307
blanchet@49207
   308
    val pre_map_defs = map map_def_of_bnf bnfs'';
blanchet@49207
   309
blanchet@48975
   310
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   311
traytel@49185
   312
    val res = construct resBs bs (map TFree resDs, deadss) bnfs'' lthy'';
blanchet@48975
   313
blanchet@48975
   314
    val timer = time (timer "FP construction in total");
blanchet@48975
   315
  in
blanchet@49207
   316
    (pre_map_defs, res)
blanchet@48975
   317
  end;
blanchet@48975
   318
blanchet@49169
   319
fun fp_bnf construct bs mixfixes resBs eqs lthy =
blanchet@48975
   320
  let
blanchet@48975
   321
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   322
    val (lhss, rhss) = split_list eqs;
traytel@49185
   323
    val sort = fp_sort lhss (SOME resBs);
blanchet@48975
   324
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@49218
   325
      (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.prefix_name raw_N b) I sort) bs rhss
blanchet@48975
   326
        (empty_unfold, lthy));
blanchet@48975
   327
  in
traytel@49185
   328
    mk_fp_bnf timer (construct mixfixes) (SOME resBs) bs sort lhss bnfs Dss Ass unfold lthy'
blanchet@48975
   329
  end;
blanchet@48975
   330
blanchet@48975
   331
fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
blanchet@48975
   332
  let
blanchet@48975
   333
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   334
    val lhss = map (dest_TFree o Syntax.read_typ lthy) raw_lhss;
traytel@49185
   335
    val sort = fp_sort lhss NONE;
blanchet@48975
   336
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@48975
   337
      (fold_map2 (fn b => fn rawT =>
blanchet@49218
   338
        (bnf_of_typ Smart_Inline (Binding.prefix_name raw_N b) I sort (Syntax.read_typ lthy rawT)))
traytel@49185
   339
      bs raw_bnfs (empty_unfold, lthy));
blanchet@48975
   340
  in
blanchet@49207
   341
    snd (mk_fp_bnf timer (construct (map (K NoSyn) bs)) NONE bs sort lhss bnfs Dss Ass unfold lthy')
blanchet@48975
   342
  end;
blanchet@48975
   343
blanchet@48975
   344
end;