src/HOL/Codatatype/Tools/bnf_fp_util.ML
author blanchet
Tue Sep 04 15:51:32 2012 +0200 (2012-09-04)
changeset 49125 5fc5211cf104
parent 49121 9e0acaa470ab
child 49128 1a86ef0a0210
permissions -rw-r--r--
implemented "mk_exhaust_tac"
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@48975
    21
  val coiter_uniqueN: string
blanchet@48975
    22
  val corecN: string
blanchet@49020
    23
  val exhaustN: string
blanchet@48975
    24
  val fldN: string
blanchet@48975
    25
  val fld_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@48975
    30
  val fld_unfN: string
blanchet@48975
    31
  val hsetN: string
blanchet@48975
    32
  val hset_recN: string
blanchet@48975
    33
  val inductN: string
blanchet@49019
    34
  val injectN: string
blanchet@48975
    35
  val isNodeN: string
blanchet@48975
    36
  val iterN: string
blanchet@48975
    37
  val iter_uniqueN: string
blanchet@48975
    38
  val lsbisN: string
blanchet@48975
    39
  val map_simpsN: string
blanchet@48975
    40
  val map_uniqueN: string
blanchet@48975
    41
  val min_algN: string
blanchet@48975
    42
  val morN: string
blanchet@49020
    43
  val nchotomyN: string
blanchet@48975
    44
  val pred_coinductN: string
blanchet@48975
    45
  val pred_coinduct_uptoN: string
blanchet@48975
    46
  val recN: string
blanchet@48975
    47
  val rel_coinductN: string
blanchet@48975
    48
  val rel_coinduct_uptoN: string
blanchet@48975
    49
  val rvN: string
blanchet@48975
    50
  val set_inclN: string
blanchet@48975
    51
  val set_set_inclN: string
blanchet@48975
    52
  val strTN: string
blanchet@48975
    53
  val str_initN: string
blanchet@48975
    54
  val sum_bdN: string
blanchet@48975
    55
  val sum_bdTN: string
blanchet@48975
    56
  val unfN: string
blanchet@48975
    57
  val unf_coinductN: string
blanchet@48975
    58
  val unf_coinduct_uptoN: string
blanchet@48975
    59
  val unf_exhaustN: string
blanchet@48975
    60
  val unf_fldN: string
blanchet@48975
    61
  val unf_injectN: string
blanchet@48975
    62
  val uniqueN: string
blanchet@48975
    63
  val uptoN: string
blanchet@48975
    64
blanchet@48975
    65
  val mk_exhaustN: string -> string
blanchet@48975
    66
  val mk_injectN: string -> string
blanchet@48975
    67
  val mk_nchotomyN: string -> string
blanchet@48975
    68
  val mk_set_simpsN: int -> string
blanchet@48975
    69
  val mk_set_minimalN: int -> string
blanchet@48975
    70
  val mk_set_inductN: int -> string
blanchet@48975
    71
blanchet@48975
    72
  val typedef: bool -> binding option -> binding * (string * sort) list * mixfix -> term ->
blanchet@48975
    73
    (binding * binding) option -> tactic -> local_theory -> (string * Typedef.info) * local_theory
blanchet@48975
    74
blanchet@48975
    75
  val split_conj_thm: thm -> thm list
blanchet@48975
    76
  val split_conj_prems: int -> thm -> thm
blanchet@48975
    77
blanchet@49121
    78
  val Inl_const: typ -> typ -> term
blanchet@49121
    79
  val Inr_const: typ -> typ -> term
blanchet@49121
    80
blanchet@49121
    81
  val mk_Inl: term -> typ -> term
blanchet@49121
    82
  val mk_Inr: term -> typ -> term
blanchet@49121
    83
  val mk_InN: typ list -> term -> int -> term
blanchet@49121
    84
blanchet@48975
    85
  val mk_Field: term -> term
blanchet@48975
    86
  val mk_union: term * term -> term
blanchet@48975
    87
blanchet@49125
    88
  val mk_sumEN: int -> thm
blanchet@49125
    89
blanchet@48975
    90
  val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
blanchet@48975
    91
blanchet@48975
    92
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@48975
    93
blanchet@49121
    94
  val fp_bnf: (binding list -> typ list list -> BNF_Def.BNF list -> Proof.context -> 'a) ->
blanchet@49121
    95
    binding list -> ((string * sort) * typ) list -> Proof.context -> 'a
blanchet@49121
    96
  val fp_bnf_cmd: (binding list -> typ list list -> BNF_Def.BNF list -> Proof.context -> 'a) ->
blanchet@49121
    97
    binding list * (string list * string list) -> Proof.context -> 'a
blanchet@48975
    98
end;
blanchet@48975
    99
blanchet@48975
   100
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   101
struct
blanchet@48975
   102
blanchet@48975
   103
open BNF_Comp
blanchet@48975
   104
open BNF_Def
blanchet@48975
   105
open BNF_Util
blanchet@48975
   106
blanchet@48975
   107
val timing = true;
blanchet@48975
   108
fun time timer msg = (if timing
blanchet@48975
   109
  then warning (msg ^ ": " ^ ATP_Util.string_from_time (Timer.checkRealTimer timer))
blanchet@48975
   110
  else (); Timer.startRealTimer ());
blanchet@48975
   111
blanchet@48975
   112
(*TODO: is this really different from Typedef.add_typedef_global?*)
blanchet@48975
   113
fun typedef def opt_name typ set opt_morphs tac lthy =
blanchet@48975
   114
  let
blanchet@48975
   115
    val ((name, info), (lthy, lthy_old)) =
blanchet@48975
   116
      lthy
blanchet@48975
   117
      |> Typedef.add_typedef def opt_name typ set opt_morphs tac
blanchet@48975
   118
      ||> `Local_Theory.restore;
blanchet@48975
   119
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   120
  in
blanchet@48975
   121
    ((name, Typedef.transform_info phi info), lthy)
blanchet@48975
   122
  end;
blanchet@48975
   123
blanchet@48975
   124
val coN = "co"
blanchet@48975
   125
val algN = "alg"
blanchet@48975
   126
val IITN = "IITN"
blanchet@48975
   127
val iterN = "iter"
blanchet@48975
   128
val coiterN = coN ^ iterN
blanchet@48975
   129
val uniqueN = "_unique"
blanchet@48975
   130
val iter_uniqueN = iterN ^ uniqueN
blanchet@48975
   131
val coiter_uniqueN = coiterN ^ uniqueN
blanchet@48975
   132
val fldN = "fld"
blanchet@48975
   133
val unfN = "unf"
blanchet@48975
   134
val fld_coiterN = fldN ^ "_" ^ coiterN
blanchet@48975
   135
val map_simpsN = mapN ^ "_simps"
blanchet@48975
   136
val map_uniqueN = mapN ^ uniqueN
blanchet@48975
   137
val min_algN = "min_alg"
blanchet@48975
   138
val morN = "mor"
blanchet@48975
   139
val bisN = "bis"
blanchet@48975
   140
val lsbisN = "lsbis"
blanchet@48975
   141
val sum_bdTN = "sbdT"
blanchet@48975
   142
val sum_bdN = "sbd"
blanchet@48975
   143
val carTN = "carT"
blanchet@48975
   144
val strTN = "strT"
blanchet@48975
   145
val isNodeN = "isNode"
blanchet@48975
   146
val LevN = "Lev"
blanchet@48975
   147
val rvN = "recover"
blanchet@48975
   148
val behN = "beh"
blanchet@48975
   149
fun mk_set_simpsN i = mk_setN i ^ "_simps"
blanchet@48975
   150
fun mk_set_minimalN i = mk_setN i ^ "_minimal"
blanchet@48975
   151
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@48975
   152
blanchet@48975
   153
val str_initN = "str_init"
blanchet@48975
   154
val recN = "rec"
blanchet@48975
   155
val corecN = coN ^ recN
blanchet@48975
   156
blanchet@48975
   157
val fld_unfN = fldN ^ "_" ^ unfN
blanchet@48975
   158
val unf_fldN = unfN ^ "_" ^ fldN
blanchet@49020
   159
val nchotomyN = "nchotomy"
blanchet@49020
   160
fun mk_nchotomyN s = s ^ "_" ^ nchotomyN
blanchet@49019
   161
val injectN = "inject"
blanchet@49019
   162
fun mk_injectN s = s ^ "_" ^ injectN
blanchet@49020
   163
val exhaustN = "exhaust"
blanchet@49020
   164
fun mk_exhaustN s = s ^ "_" ^ exhaustN
blanchet@48975
   165
val fld_injectN = mk_injectN fldN
blanchet@48975
   166
val fld_exhaustN = mk_exhaustN fldN
blanchet@48975
   167
val unf_injectN = mk_injectN unfN
blanchet@48975
   168
val unf_exhaustN = mk_exhaustN unfN
blanchet@48975
   169
val inductN = "induct"
blanchet@48975
   170
val coinductN = coN ^ inductN
blanchet@48975
   171
val fld_inductN = fldN ^ "_" ^ inductN
blanchet@48975
   172
val fld_induct2N = fld_inductN ^ "2"
blanchet@48975
   173
val unf_coinductN = unfN ^ "_" ^ coinductN
blanchet@48975
   174
val rel_coinductN = relN ^ "_" ^ coinductN
blanchet@48975
   175
val pred_coinductN = predN ^ "_" ^ coinductN
blanchet@48975
   176
val uptoN = "upto"
blanchet@48975
   177
val unf_coinduct_uptoN = unf_coinductN ^ "_" ^ uptoN
blanchet@48975
   178
val rel_coinduct_uptoN = rel_coinductN ^ "_" ^ uptoN
blanchet@48975
   179
val pred_coinduct_uptoN = pred_coinductN ^ "_" ^ uptoN
blanchet@48975
   180
val hsetN = "Hset"
blanchet@48975
   181
val hset_recN = hsetN ^ "_rec"
blanchet@48975
   182
val set_inclN = "set_incl"
blanchet@48975
   183
val set_set_inclN = "set_set_incl"
blanchet@48975
   184
blanchet@49121
   185
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49121
   186
fun mk_Inl t RT = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   187
blanchet@49121
   188
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49121
   189
fun mk_Inr t LT = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   190
blanchet@49121
   191
fun mk_InN [_] t 1 = t
blanchet@49121
   192
  | mk_InN (_ :: Ts) t 1 = mk_Inl t (mk_sumTN Ts)
blanchet@49121
   193
  | mk_InN (LT :: Ts) t m = mk_Inr (mk_InN Ts t (m - 1)) LT
blanchet@49121
   194
  | mk_InN Ts t _ = raise (TYPE ("mk_InN", Ts, [t]));
blanchet@49121
   195
blanchet@48975
   196
fun mk_Field r =
blanchet@48975
   197
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   198
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   199
blanchet@48975
   200
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   201
blanchet@48975
   202
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   203
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   204
blanchet@48975
   205
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   206
fun split_conj_thm th =
blanchet@49119
   207
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   208
blanchet@48975
   209
fun split_conj_prems limit th =
blanchet@48975
   210
  let
blanchet@48975
   211
    fun split n i th =
blanchet@48975
   212
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   213
  in split limit 1 th end;
blanchet@48975
   214
blanchet@49125
   215
local
blanchet@49125
   216
  fun mk_sumEN' 1 = @{thm obj_sum_step}
blanchet@49125
   217
    | mk_sumEN' n = mk_sumEN' (n - 1) RSN (2, @{thm obj_sum_step});
blanchet@49125
   218
in
blanchet@49125
   219
  fun mk_sumEN 1 = @{thm obj_sum_base}
blanchet@49125
   220
    | mk_sumEN 2 = @{thm sumE}
blanchet@49125
   221
    | mk_sumEN n = (mk_sumEN' (n - 2) RSN (2, @{thm obj_sumE})) OF replicate n (impI RS allI);
blanchet@49125
   222
end;
blanchet@49125
   223
blanchet@48975
   224
fun mk_tactics mid mcomp mcong snat bdco bdinf sbd inbd wpull =
blanchet@48975
   225
  [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
blanchet@48975
   226
blanchet@48975
   227
fun fp_sort lhss Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
blanchet@48975
   228
  (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss;
blanchet@48975
   229
blanchet@48975
   230
fun mk_fp_bnf timer construct bs sort bnfs deads lives unfold lthy =
blanchet@48975
   231
  let
blanchet@48975
   232
    (* TODO: assert that none of the deads is a lhs *)
blanchet@48975
   233
blanchet@48975
   234
    val name = fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "";
blanchet@48975
   235
    fun qualify i bind =
blanchet@48975
   236
      let val namei = if i > 0 then name ^ string_of_int i else name;
blanchet@48975
   237
      in
blanchet@48975
   238
        if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
blanchet@48975
   239
        else Binding.prefix_name namei bind
blanchet@48975
   240
      end;
blanchet@48975
   241
blanchet@48975
   242
    val Ass = map (map dest_TFree) lives;
blanchet@48975
   243
    val Ds = fold (fold Term.add_tfreesT) deads [];
blanchet@48975
   244
blanchet@48975
   245
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   246
blanchet@48975
   247
    val ((kill_poss, _), (bnfs', (unfold', lthy'))) =
blanchet@48975
   248
      normalize_bnfs qualify Ass Ds sort bnfs unfold lthy;
blanchet@48975
   249
blanchet@48975
   250
    val Dss = map3 (append oo map o nth) lives kill_poss deads;
blanchet@48975
   251
blanchet@48975
   252
    val (bnfs'', lthy'') =
blanchet@48975
   253
      fold_map3 (seal_bnf unfold') (map (Binding.suffix_name "BNF") bs) Dss bnfs' lthy';
blanchet@48975
   254
blanchet@48975
   255
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   256
blanchet@48975
   257
    val res = construct bs Dss bnfs'' lthy'';
blanchet@48975
   258
blanchet@48975
   259
    val timer = time (timer "FP construction in total");
blanchet@48975
   260
  in
blanchet@48975
   261
    res
blanchet@48975
   262
  end;
blanchet@48975
   263
blanchet@48975
   264
fun fp_bnf construct bs eqs lthy =
blanchet@48975
   265
  let
blanchet@48975
   266
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   267
    val (lhss, rhss) = split_list eqs;
blanchet@48975
   268
    val sort = fp_sort lhss;
blanchet@48975
   269
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@48975
   270
      (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort) bs rhss
blanchet@48975
   271
        (empty_unfold, lthy));
blanchet@48975
   272
  in
blanchet@48975
   273
    mk_fp_bnf timer construct bs sort bnfs Dss Ass unfold lthy'
blanchet@48975
   274
  end;
blanchet@48975
   275
blanchet@48975
   276
fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
blanchet@48975
   277
  let
blanchet@48975
   278
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   279
    val lhss = map (dest_TFree o Syntax.read_typ lthy) raw_lhss;
blanchet@48975
   280
    val sort = fp_sort lhss;
blanchet@48975
   281
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@48975
   282
      (fold_map2 (fn b => fn rawT =>
blanchet@48975
   283
        (bnf_of_typ Smart_Inline (Binding.suffix_name "RAW" b) I sort (Syntax.read_typ lthy rawT)))
blanchet@48975
   284
        bs raw_bnfs (empty_unfold, lthy));
blanchet@48975
   285
  in
blanchet@48975
   286
    mk_fp_bnf timer construct bs sort bnfs Dss Ass unfold lthy'
blanchet@48975
   287
  end;
blanchet@48975
   288
blanchet@48975
   289
end;