src/HOL/Codatatype/Tools/bnf_util.ML
author traytel
Mon, 17 Sep 2012 16:57:22 +0200
changeset 50440 f27f83f71e94
parent 50410 323414474c1f
child 50449 433dc7e028c8
permissions -rw-r--r--
cleaned up internal naming scheme for bnfs
blanchet@49990
     1
(*  Title:      HOL/Codatatype/Tools/bnf_util.ML
blanchet@49990
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@49990
     3
    Copyright   2012
blanchet@49990
     4
blanchet@50297
     5
Library for bounded natural functors.
blanchet@49990
     6
*)
blanchet@49990
     7
blanchet@49990
     8
signature BNF_UTIL =
blanchet@49990
     9
sig
blanchet@49990
    10
  val map3: ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
blanchet@49990
    11
  val map4: ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list
blanchet@49990
    12
  val map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f) ->
blanchet@49990
    13
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list
blanchet@49990
    14
  val map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) ->
blanchet@49990
    15
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list
blanchet@49990
    16
  val map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) ->
blanchet@49990
    17
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list
blanchet@49990
    18
  val map8: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) ->
blanchet@49990
    19
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> 'i list
blanchet@49990
    20
  val map9: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j) ->
blanchet@49990
    21
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
blanchet@49990
    22
    'i list -> 'j list
blanchet@49990
    23
  val map10: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k) ->
blanchet@49990
    24
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
blanchet@49990
    25
    'i list -> 'j list -> 'k list
blanchet@49990
    26
  val map11: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l) ->
blanchet@49990
    27
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
blanchet@49990
    28
    'i list -> 'j list -> 'k list -> 'l list
blanchet@49990
    29
  val map12: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm) ->
blanchet@49990
    30
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
blanchet@49990
    31
    'i list -> 'j list -> 'k list -> 'l list -> 'm list
blanchet@49990
    32
  val fold_map2: ('a -> 'b -> 'c -> 'd * 'c) -> 'a list -> 'b list -> 'c -> 'd list * 'c
blanchet@49990
    33
  val fold_map3: ('a -> 'b -> 'c -> 'd -> 'e * 'd) ->
blanchet@49990
    34
    'a list -> 'b list -> 'c list -> 'd -> 'e list * 'd
blanchet@49990
    35
  val fold_map4: ('a -> 'b -> 'c -> 'd -> 'e -> 'f * 'e) ->
blanchet@49990
    36
    'a list -> 'b list -> 'c list -> 'd list -> 'e -> 'f list * 'e
blanchet@49990
    37
  val fold_map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g * 'f) ->
blanchet@49990
    38
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f -> 'g list * 'f
blanchet@49990
    39
  val fold_map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h * 'g) ->
blanchet@49990
    40
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g -> 'h list * 'g
blanchet@49990
    41
  val fold_map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i * 'h) ->
blanchet@49990
    42
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h -> 'i list * 'h
blanchet@50090
    43
  val interleave: 'a list -> 'a list -> 'a list
blanchet@49990
    44
  val transpose: 'a list list -> 'a list list
blanchet@50227
    45
  val seq_conds: (bool -> 'a -> 'b) -> int -> int -> 'a list -> 'b list
blanchet@49990
    46
blanchet@49990
    47
  val mk_fresh_names: Proof.context -> int -> string -> string list * Proof.context
blanchet@49990
    48
  val mk_TFrees: int -> Proof.context -> typ list * Proof.context
blanchet@49990
    49
  val mk_TFreess: int list -> Proof.context -> typ list list * Proof.context
blanchet@50313
    50
  val mk_TFrees': sort list -> Proof.context -> typ list * Proof.context
blanchet@49990
    51
  val mk_Frees: string -> typ list -> Proof.context -> term list * Proof.context
blanchet@49990
    52
  val mk_Freess: string -> typ list list -> Proof.context -> term list list * Proof.context
blanchet@50192
    53
  val mk_Freesss: string -> typ list list list -> Proof.context ->
blanchet@50192
    54
    term list list list * Proof.context
blanchet@50215
    55
  val mk_Freessss: string -> typ list list list list -> Proof.context ->
blanchet@50215
    56
    term list list list list * Proof.context
blanchet@49990
    57
  val mk_Frees': string -> typ list -> Proof.context ->
blanchet@49990
    58
    (term list * (string * typ) list) * Proof.context
blanchet@49990
    59
  val mk_Freess': string -> typ list list -> Proof.context ->
blanchet@49990
    60
    (term list list * (string * typ) list list) * Proof.context
traytel@50440
    61
  val nonzero_string_of_int: int -> string
blanchet@49990
    62
traytel@50410
    63
  val strip_typeN: int -> typ -> typ list * typ
traytel@50410
    64
blanchet@49990
    65
  val mk_optionT: typ -> typ
blanchet@49990
    66
  val mk_relT: typ * typ -> typ
blanchet@49990
    67
  val dest_relT: typ -> typ * typ
blanchet@49990
    68
  val mk_sumT: typ * typ -> typ
blanchet@49990
    69
blanchet@49990
    70
  val ctwo: term
blanchet@49990
    71
  val fst_const: typ -> term
blanchet@49990
    72
  val snd_const: typ -> term
blanchet@49990
    73
  val Id_const: typ -> term
blanchet@49990
    74
blanchet@49990
    75
  val mk_Ball: term -> term -> term
blanchet@49990
    76
  val mk_Bex: term -> term -> term
blanchet@49990
    77
  val mk_Card_order: term -> term
blanchet@49990
    78
  val mk_Field: term -> term
blanchet@49990
    79
  val mk_Gr: term -> term -> term
blanchet@50225
    80
  val mk_IfN: typ -> term list -> term list -> term
blanchet@50138
    81
  val mk_Trueprop_eq: term * term -> term
blanchet@49990
    82
  val mk_UNION: term -> term -> term
blanchet@49990
    83
  val mk_Union: typ -> term
blanchet@49990
    84
  val mk_card_binop: string -> (typ * typ -> typ) -> term -> term -> term
blanchet@49990
    85
  val mk_card_of: term -> term
blanchet@49990
    86
  val mk_card_order: term -> term
blanchet@49990
    87
  val mk_ccexp: term -> term -> term
blanchet@49990
    88
  val mk_cexp: term -> term -> term
blanchet@49990
    89
  val mk_cinfinite: term -> term
blanchet@49990
    90
  val mk_collect: term list -> typ -> term
blanchet@49990
    91
  val mk_converse: term -> term
blanchet@49990
    92
  val mk_cprod: term -> term -> term
blanchet@49990
    93
  val mk_csum: term -> term -> term
blanchet@49990
    94
  val mk_dir_image: term -> term -> term
blanchet@49990
    95
  val mk_image: term -> term
blanchet@49990
    96
  val mk_in: term list -> term list -> typ -> term
blanchet@49990
    97
  val mk_ordLeq: term -> term -> term
blanchet@49990
    98
  val mk_rel_comp: term * term -> term
blanchet@49990
    99
  val mk_subset: term -> term -> term
blanchet@49990
   100
  val mk_wpull: term -> term -> term -> term -> term -> (term * term) option -> term -> term -> term
blanchet@49990
   101
blanchet@50090
   102
  val list_all_free: term list -> term -> term
blanchet@50090
   103
  val list_exists_free: term list -> term -> term
blanchet@50090
   104
blanchet@49990
   105
  (*parameterized terms*)
blanchet@49990
   106
  val mk_nthN: int -> term -> int -> term
blanchet@49990
   107
blanchet@49990
   108
  (*parameterized thms*)
blanchet@49990
   109
  val mk_Un_upper: int -> int -> thm
blanchet@49990
   110
  val mk_conjIN: int -> thm
blanchet@49990
   111
  val mk_conjunctN: int -> int -> thm
blanchet@50352
   112
  val conj_dests: int -> thm -> thm list
blanchet@50090
   113
  val mk_disjIN: int -> int -> thm
blanchet@49990
   114
  val mk_nthI: int -> int -> thm
blanchet@49990
   115
  val mk_nth_conv: int -> int -> thm
blanchet@49990
   116
  val mk_ordLeq_csum: int -> int -> thm -> thm
blanchet@50381
   117
  val mk_UnIN: int -> int -> thm
blanchet@49990
   118
blanchet@49990
   119
  val ctrans: thm
blanchet@49990
   120
  val o_apply: thm
blanchet@49990
   121
  val mk_sym: thm -> thm
blanchet@49990
   122
  val mk_trans: thm -> thm -> thm
blanchet@49990
   123
  val mk_unabs_def: int -> thm -> thm
blanchet@49990
   124
blanchet@49990
   125
  val mk_permute: ''a list -> ''a list -> 'b list -> 'b list
blanchet@49990
   126
  val find_indices: ''a list -> ''a list -> int list
blanchet@49990
   127
blanchet@50090
   128
  val certifyT: Proof.context -> typ -> ctyp
blanchet@50090
   129
  val certify: Proof.context -> term -> cterm
blanchet@50090
   130
traytel@50243
   131
  val typedef: bool -> binding option -> binding * (string * sort) list * mixfix -> term ->
traytel@50243
   132
    (binding * binding) option -> tactic -> local_theory -> (string * Typedef.info) * local_theory
traytel@50243
   133
blanchet@49990
   134
  val WRAP: ('a -> tactic) -> ('a -> tactic) -> 'a list -> tactic -> tactic
blanchet@49990
   135
  val WRAP': ('a -> int -> tactic) -> ('a -> int -> tactic) -> 'a list -> (int -> tactic) -> int ->
blanchet@49990
   136
    tactic
blanchet@49990
   137
  val CONJ_WRAP_GEN: tactic -> ('a -> tactic) -> 'a list -> tactic
blanchet@49990
   138
  val CONJ_WRAP_GEN': (int -> tactic) -> ('a -> int -> tactic) -> 'a list -> int -> tactic
blanchet@49990
   139
  val CONJ_WRAP: ('a -> tactic) -> 'a list -> tactic
blanchet@49990
   140
  val CONJ_WRAP': ('a -> int -> tactic) -> 'a list -> int -> tactic
blanchet@49990
   141
end;
blanchet@49990
   142
blanchet@49990
   143
structure BNF_Util : BNF_UTIL =
blanchet@49990
   144
struct
blanchet@49990
   145
blanchet@49990
   146
(* Library proper *)
blanchet@49990
   147
blanchet@49990
   148
fun map3 _ [] [] [] = []
blanchet@49990
   149
  | map3 f (x1::x1s) (x2::x2s) (x3::x3s) = f x1 x2 x3 :: map3 f x1s x2s x3s
blanchet@49990
   150
  | map3 _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   151
blanchet@49990
   152
fun map4 _ [] [] [] [] = []
blanchet@49990
   153
  | map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) = f x1 x2 x3 x4 :: map4 f x1s x2s x3s x4s
blanchet@49990
   154
  | map4 _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   155
blanchet@49990
   156
fun map5 _ [] [] [] [] [] = []
blanchet@49990
   157
  | map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) =
blanchet@49990
   158
    f x1 x2 x3 x4 x5 :: map5 f x1s x2s x3s x4s x5s
blanchet@49990
   159
  | map5 _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   160
blanchet@49990
   161
fun map6 _ [] [] [] [] [] [] = []
blanchet@49990
   162
  | map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) =
blanchet@49990
   163
    f x1 x2 x3 x4 x5 x6 :: map6 f x1s x2s x3s x4s x5s x6s
blanchet@49990
   164
  | map6 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   165
blanchet@49990
   166
fun map7 _ [] [] [] [] [] [] [] = []
blanchet@49990
   167
  | map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) =
blanchet@49990
   168
    f x1 x2 x3 x4 x5 x6 x7 :: map7 f x1s x2s x3s x4s x5s x6s x7s
blanchet@49990
   169
  | map7 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   170
blanchet@49990
   171
fun map8 _ [] [] [] [] [] [] [] [] = []
blanchet@49990
   172
  | map8 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) =
blanchet@49990
   173
    f x1 x2 x3 x4 x5 x6 x7 x8 :: map8 f x1s x2s x3s x4s x5s x6s x7s x8s
blanchet@49990
   174
  | map8 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   175
blanchet@49990
   176
fun map9 _ [] [] [] [] [] [] [] [] [] = []
blanchet@49990
   177
  | map9 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
blanchet@49990
   178
      (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) =
blanchet@49990
   179
    f x1 x2 x3 x4 x5 x6 x7 x8 x9 :: map9 f x1s x2s x3s x4s x5s x6s x7s x8s x9s
blanchet@49990
   180
  | map9 _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   181
blanchet@49990
   182
fun map10 _ [] [] [] [] [] [] [] [] [] [] = []
blanchet@49990
   183
  | map10 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
blanchet@49990
   184
      (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) =
blanchet@49990
   185
    f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 :: map10 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s
blanchet@49990
   186
  | map10 _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   187
blanchet@49990
   188
fun map11 _ [] [] [] [] [] [] [] [] [] [] [] = []
blanchet@49990
   189
  | map11 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
blanchet@49990
   190
      (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) (x11::x11s) =
blanchet@49990
   191
    f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 :: map11 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s
blanchet@49990
   192
  | map11 _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   193
blanchet@49990
   194
fun map12 _ [] [] [] [] [] [] [] [] [] [] [] [] = []
blanchet@49990
   195
  | map12 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
blanchet@49990
   196
      (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) =
blanchet@49990
   197
    f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ::
blanchet@49990
   198
      map12 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s
blanchet@49990
   199
  | map12 _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   200
blanchet@49990
   201
fun fold_map2 _ [] [] acc = ([], acc)
blanchet@49990
   202
  | fold_map2 f (x1::x1s) (x2::x2s) acc =
blanchet@49990
   203
    let
blanchet@49990
   204
      val (x, acc') = f x1 x2 acc;
blanchet@49990
   205
      val (xs, acc'') = fold_map2 f x1s x2s acc';
blanchet@49990
   206
    in (x :: xs, acc'') end
blanchet@49990
   207
  | fold_map2 _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   208
blanchet@49990
   209
fun fold_map3 _ [] [] [] acc = ([], acc)
blanchet@49990
   210
  | fold_map3 f (x1::x1s) (x2::x2s) (x3::x3s) acc =
blanchet@49990
   211
    let
blanchet@49990
   212
      val (x, acc') = f x1 x2 x3 acc;
blanchet@49990
   213
      val (xs, acc'') = fold_map3 f x1s x2s x3s acc';
blanchet@49990
   214
    in (x :: xs, acc'') end
blanchet@49990
   215
  | fold_map3 _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   216
blanchet@49990
   217
fun fold_map4 _ [] [] [] [] acc = ([], acc)
blanchet@49990
   218
  | fold_map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) acc =
blanchet@49990
   219
    let
blanchet@49990
   220
      val (x, acc') = f x1 x2 x3 x4 acc;
blanchet@49990
   221
      val (xs, acc'') = fold_map4 f x1s x2s x3s x4s acc';
blanchet@49990
   222
    in (x :: xs, acc'') end
blanchet@49990
   223
  | fold_map4 _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   224
blanchet@49990
   225
fun fold_map5 _ [] [] [] [] [] acc = ([], acc)
blanchet@49990
   226
  | fold_map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) acc =
blanchet@49990
   227
    let
blanchet@49990
   228
      val (x, acc') = f x1 x2 x3 x4 x5 acc;
blanchet@49990
   229
      val (xs, acc'') = fold_map5 f x1s x2s x3s x4s x5s acc';
blanchet@49990
   230
    in (x :: xs, acc'') end
blanchet@49990
   231
  | fold_map5 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   232
blanchet@49990
   233
fun fold_map6 _ [] [] [] [] [] [] acc = ([], acc)
blanchet@49990
   234
  | fold_map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) acc =
blanchet@49990
   235
    let
blanchet@49990
   236
      val (x, acc') = f x1 x2 x3 x4 x5 x6 acc;
blanchet@49990
   237
      val (xs, acc'') = fold_map6 f x1s x2s x3s x4s x5s x6s acc';
blanchet@49990
   238
    in (x :: xs, acc'') end
blanchet@49990
   239
  | fold_map6 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   240
blanchet@49990
   241
fun fold_map7 _ [] [] [] [] [] [] [] acc = ([], acc)
blanchet@49990
   242
  | fold_map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) acc =
blanchet@49990
   243
    let
blanchet@49990
   244
      val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 acc;
blanchet@49990
   245
      val (xs, acc'') = fold_map7 f x1s x2s x3s x4s x5s x6s x7s acc';
blanchet@49990
   246
    in (x :: xs, acc'') end
blanchet@49990
   247
  | fold_map7 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
blanchet@49990
   248
blanchet@50090
   249
(*stolen from ~~/src/HOL/Tools/SMT/smt_utils.ML*)
blanchet@50090
   250
fun certify ctxt = Thm.cterm_of (Proof_Context.theory_of ctxt);
blanchet@50090
   251
fun certifyT ctxt = Thm.ctyp_of (Proof_Context.theory_of ctxt);
blanchet@50090
   252
traytel@50243
   253
(*TODO: is this really different from Typedef.add_typedef_global?*)
traytel@50243
   254
fun typedef def opt_name typ set opt_morphs tac lthy =
traytel@50243
   255
  let
traytel@50243
   256
    val ((name, info), (lthy, lthy_old)) =
traytel@50243
   257
      lthy
traytel@50243
   258
      |> Typedef.add_typedef def opt_name typ set opt_morphs tac
traytel@50243
   259
      ||> `Local_Theory.restore;
traytel@50243
   260
    val phi = Proof_Context.export_morphism lthy_old lthy;
traytel@50243
   261
  in
traytel@50243
   262
    ((name, Typedef.transform_info phi info), lthy)
traytel@50243
   263
  end;
traytel@50243
   264
blanchet@49990
   265
(*Tactical WRAP surrounds a static given tactic (core) with two deterministic chains of tactics*)
blanchet@49990
   266
fun WRAP gen_before gen_after xs core_tac =
blanchet@49990
   267
  fold_rev (fn x => fn tac => gen_before x THEN tac THEN gen_after x) xs core_tac;
blanchet@49990
   268
blanchet@49990
   269
fun WRAP' gen_before gen_after xs core_tac =
blanchet@49990
   270
  fold_rev (fn x => fn tac => gen_before x THEN' tac THEN' gen_after x) xs core_tac;
blanchet@49990
   271
blanchet@49990
   272
fun CONJ_WRAP_GEN conj_tac gen_tac xs =
blanchet@49990
   273
  let val (butlast, last) = split_last xs;
blanchet@49990
   274
  in WRAP (fn thm => conj_tac THEN gen_tac thm) (K all_tac) butlast (gen_tac last) end;
blanchet@49990
   275
blanchet@49990
   276
fun CONJ_WRAP_GEN' conj_tac gen_tac xs =
blanchet@49990
   277
  let val (butlast, last) = split_last xs;
blanchet@49990
   278
  in WRAP' (fn thm => conj_tac THEN' gen_tac thm) (K (K all_tac)) butlast (gen_tac last) end;
blanchet@49990
   279
blanchet@49990
   280
(*not eta-converted because of monotype restriction*)
blanchet@49990
   281
fun CONJ_WRAP gen_tac = CONJ_WRAP_GEN (rtac conjI 1) gen_tac;
blanchet@49990
   282
fun CONJ_WRAP' gen_tac = CONJ_WRAP_GEN' (rtac conjI) gen_tac;
blanchet@49990
   283
blanchet@49990
   284
blanchet@49990
   285
blanchet@49990
   286
(* Term construction *)
blanchet@49990
   287
blanchet@49990
   288
(** Fresh variables **)
blanchet@49990
   289
traytel@50440
   290
fun nonzero_string_of_int 0 = ""
traytel@50440
   291
  | nonzero_string_of_int n = string_of_int n;
traytel@50440
   292
blanchet@50313
   293
val mk_TFrees' = apfst (map TFree) oo Variable.invent_types;
blanchet@50313
   294
blanchet@50313
   295
fun mk_TFrees n = mk_TFrees' (replicate n HOLogic.typeS);
blanchet@50313
   296
val mk_TFreess = fold_map mk_TFrees;
blanchet@49990
   297
blanchet@50193
   298
fun mk_names n x = if n = 1 then [x] else map (fn i => x ^ string_of_int i) (1 upto n);
blanchet@49990
   299
blanchet@50193
   300
fun mk_fresh_names ctxt = (fn xs => Variable.variant_fixes xs ctxt) oo mk_names;
blanchet@50193
   301
fun mk_Frees x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => map2 (curry Free) xs Ts);
blanchet@50192
   302
fun mk_Freess x Tss = fold_map2 mk_Frees (mk_names (length Tss) x) Tss;
blanchet@50192
   303
fun mk_Freesss x Tsss = fold_map2 mk_Freess (mk_names (length Tsss) x) Tsss;
blanchet@50215
   304
fun mk_Freessss x Tssss = fold_map2 mk_Freesss (mk_names (length Tssss) x) Tssss;
blanchet@50193
   305
fun mk_Frees' x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => `(map Free) (xs ~~ Ts));
blanchet@50192
   306
fun mk_Freess' x Tss = fold_map2 mk_Frees' (mk_names (length Tss) x) Tss #>> split_list;
blanchet@49990
   307
blanchet@49990
   308
blanchet@49990
   309
(** Types **)
blanchet@49990
   310
traytel@50410
   311
fun strip_typeN 0 T = ([], T)
traytel@50410
   312
  | strip_typeN n (Type (@{type_name fun}, [T, T'])) = strip_typeN (n - 1) T' |>> cons T
traytel@50410
   313
  | strip_typeN n T = raise TYPE ("strip_typeN", [T], []);
traytel@50410
   314
blanchet@49990
   315
fun mk_optionT T = Type (@{type_name option}, [T]);
blanchet@49990
   316
val mk_relT = HOLogic.mk_setT o HOLogic.mk_prodT;
blanchet@49990
   317
val dest_relT = HOLogic.dest_prodT o HOLogic.dest_setT;
blanchet@49990
   318
fun mk_sumT (LT, RT) = Type (@{type_name Sum_Type.sum}, [LT, RT]);
blanchet@49990
   319
fun mk_partial_funT (ranT, domT) = domT --> mk_optionT ranT;
blanchet@49990
   320
blanchet@49990
   321
blanchet@49990
   322
(** Constants **)
blanchet@49990
   323
blanchet@49990
   324
fun fst_const T = Const (@{const_name fst}, T --> fst (HOLogic.dest_prodT T));
blanchet@49990
   325
fun snd_const T = Const (@{const_name snd}, T --> snd (HOLogic.dest_prodT T));
blanchet@49990
   326
fun Id_const T = Const (@{const_name Id}, mk_relT (T, T));
blanchet@49990
   327
blanchet@49990
   328
blanchet@49990
   329
(** Operators **)
blanchet@49990
   330
blanchet@50138
   331
val mk_Trueprop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
blanchet@50138
   332
blanchet@50225
   333
fun mk_IfN _ _ [t] = t
blanchet@50225
   334
  | mk_IfN T (c :: cs) (t :: ts) =
blanchet@50225
   335
    Const (@{const_name If}, HOLogic.boolT --> T --> T --> T) $ c $ t $ mk_IfN T cs ts;
blanchet@50225
   336
blanchet@49990
   337
fun mk_converse R =
blanchet@49990
   338
  let
blanchet@49990
   339
    val RT = dest_relT (fastype_of R);
blanchet@49990
   340
    val RST = mk_relT (snd RT, fst RT);
blanchet@49990
   341
  in Const (@{const_name converse}, fastype_of R --> RST) $ R end;
blanchet@49990
   342
blanchet@49990
   343
fun mk_rel_comp (R, S) =
blanchet@49990
   344
  let
blanchet@49990
   345
    val RT = fastype_of R;
blanchet@49990
   346
    val ST = fastype_of S;
blanchet@49990
   347
    val RST = mk_relT (fst (dest_relT RT), snd (dest_relT ST));
blanchet@49990
   348
  in Const (@{const_name relcomp}, RT --> ST --> RST) $ R $ S end;
blanchet@49990
   349
blanchet@49990
   350
fun mk_Gr A f =
blanchet@49990
   351
  let val ((AT, BT), FT) = `dest_funT (fastype_of f);
blanchet@49990
   352
  in Const (@{const_name Gr}, HOLogic.mk_setT AT --> FT --> mk_relT (AT, BT)) $ A $ f end;
blanchet@49990
   353
blanchet@49990
   354
fun mk_image f =
blanchet@49990
   355
  let val (T, U) = dest_funT (fastype_of f);
blanchet@49990
   356
  in Const (@{const_name image},
blanchet@49990
   357
    (T --> U) --> (HOLogic.mk_setT T) --> (HOLogic.mk_setT U)) $ f end;
blanchet@49990
   358
blanchet@49990
   359
fun mk_Ball X f =
blanchet@49990
   360
  Const (@{const_name Ball}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f;
blanchet@49990
   361
blanchet@49990
   362
fun mk_Bex X f =
blanchet@49990
   363
  Const (@{const_name Bex}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f;
blanchet@49990
   364
blanchet@49990
   365
fun mk_UNION X f =
blanchet@49990
   366
  let val (T, U) = dest_funT (fastype_of f);
blanchet@49990
   367
  in Const (@{const_name SUPR}, fastype_of X --> (T --> U) --> U) $ X $ f end;
blanchet@49990
   368
blanchet@49990
   369
fun mk_Union T =
blanchet@49990
   370
  Const (@{const_name Sup}, HOLogic.mk_setT (HOLogic.mk_setT T) --> HOLogic.mk_setT T);
blanchet@49990
   371
blanchet@49990
   372
fun mk_Field r =
blanchet@49990
   373
  let val T = fst (dest_relT (fastype_of r));
blanchet@49990
   374
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@49990
   375
blanchet@49990
   376
fun mk_card_order bd =
blanchet@49990
   377
  let
blanchet@49990
   378
    val T = fastype_of bd;
blanchet@49990
   379
    val AT = fst (dest_relT T);
blanchet@49990
   380
  in
blanchet@49990
   381
    Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $
blanchet@49990
   382
      (HOLogic.mk_UNIV AT) $ bd
blanchet@49990
   383
  end;
blanchet@49990
   384
blanchet@49990
   385
fun mk_Card_order bd =
blanchet@49990
   386
  let
blanchet@49990
   387
    val T = fastype_of bd;
blanchet@49990
   388
    val AT = fst (dest_relT T);
blanchet@49990
   389
  in
blanchet@49990
   390
    Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $
blanchet@49990
   391
      mk_Field bd $ bd
blanchet@49990
   392
  end;
blanchet@49990
   393
blanchet@49990
   394
fun mk_cinfinite bd =
blanchet@49990
   395
  Const (@{const_name cinfinite}, fastype_of bd --> HOLogic.boolT) $ bd;
blanchet@49990
   396
blanchet@49990
   397
fun mk_ordLeq t1 t2 =
blanchet@49990
   398
  HOLogic.mk_mem (HOLogic.mk_prod (t1, t2),
blanchet@49990
   399
    Const (@{const_name ordLeq}, mk_relT (fastype_of t1, fastype_of t2)));
blanchet@49990
   400
blanchet@49990
   401
fun mk_card_of A =
blanchet@49990
   402
  let
blanchet@49990
   403
    val AT = fastype_of A;
blanchet@49990
   404
    val T = HOLogic.dest_setT AT;
blanchet@49990
   405
  in
blanchet@49990
   406
    Const (@{const_name card_of}, AT --> mk_relT (T, T)) $ A
blanchet@49990
   407
  end;
blanchet@49990
   408
blanchet@49990
   409
fun mk_dir_image r f =
blanchet@49990
   410
  let val (T, U) = dest_funT (fastype_of f);
blanchet@49990
   411
  in Const (@{const_name dir_image}, mk_relT (T, T) --> (T --> U) --> mk_relT (U, U)) $ r $ f end;
blanchet@49990
   412
blanchet@49990
   413
(*FIXME: "x"?*)
blanchet@49990
   414
(*(nth sets i) must be of type "T --> 'ai set"*)
blanchet@49990
   415
fun mk_in As sets T =
blanchet@49990
   416
  let
blanchet@49990
   417
    fun in_single set A =
blanchet@49990
   418
      let val AT = fastype_of A;
blanchet@49990
   419
      in Const (@{const_name less_eq},
blanchet@49990
   420
        AT --> AT --> HOLogic.boolT) $ (set $ Free ("x", T)) $ A end;
blanchet@49990
   421
  in
blanchet@49990
   422
    if length sets > 0
blanchet@49990
   423
    then HOLogic.mk_Collect ("x", T, foldr1 (HOLogic.mk_conj) (map2 in_single sets As))
blanchet@49990
   424
    else HOLogic.mk_UNIV T
blanchet@49990
   425
  end;
blanchet@49990
   426
blanchet@49990
   427
fun mk_wpull A B1 B2 f1 f2 pseudo p1 p2 =
blanchet@49990
   428
  let
blanchet@49990
   429
    val AT = fastype_of A;
blanchet@49990
   430
    val BT1 = fastype_of B1;
blanchet@49990
   431
    val BT2 = fastype_of B2;
blanchet@49990
   432
    val FT1 = fastype_of f1;
blanchet@49990
   433
    val FT2 = fastype_of f2;
blanchet@49990
   434
    val PT1 = fastype_of p1;
blanchet@49990
   435
    val PT2 = fastype_of p2;
blanchet@49990
   436
    val T1 = HOLogic.dest_setT BT1;
blanchet@49990
   437
    val T2 = HOLogic.dest_setT BT2;
blanchet@49990
   438
    val domP = domain_type PT1;
blanchet@49990
   439
    val ranF = range_type FT1;
blanchet@49990
   440
    val _ = if is_some pseudo orelse
blanchet@49990
   441
               (HOLogic.dest_setT AT = domP andalso
blanchet@49990
   442
               domain_type FT1 = T1 andalso
blanchet@49990
   443
               domain_type FT2 = T2 andalso
blanchet@49990
   444
               domain_type PT2 = domP andalso
blanchet@49990
   445
               range_type PT1 = T1 andalso
blanchet@49990
   446
               range_type PT2 = T2 andalso
blanchet@49990
   447
               range_type FT2 = ranF)
blanchet@49990
   448
      then () else raise TYPE ("mk_wpull", [BT1, BT2, FT1, FT2, PT1, PT2], []);
blanchet@49990
   449
  in
blanchet@49990
   450
    (case pseudo of
blanchet@49990
   451
      NONE => Const (@{const_name wpull},
blanchet@49990
   452
        AT --> BT1 --> BT2 --> FT1 --> FT2 --> PT1 --> PT2 --> HOLogic.boolT) $
blanchet@49990
   453
        A $ B1 $ B2 $ f1 $ f2 $ p1 $ p2
blanchet@49990
   454
    | SOME (e1, e2) => Const (@{const_name wppull},
blanchet@49990
   455
        AT --> BT1 --> BT2 --> FT1 --> FT2 --> fastype_of e1 --> fastype_of e2 -->
blanchet@49990
   456
          PT1 --> PT2 --> HOLogic.boolT) $
blanchet@49990
   457
        A $ B1 $ B2 $ f1 $ f2 $ e1 $ e2 $ p1 $ p2)
blanchet@49990
   458
  end;
blanchet@49990
   459
blanchet@49990
   460
fun mk_subset t1 t2 =
blanchet@49990
   461
  Const (@{const_name less_eq}, (fastype_of t1) --> (fastype_of t2) --> HOLogic.boolT) $ t1 $ t2;
blanchet@49990
   462
blanchet@49990
   463
fun mk_card_binop binop typop t1 t2 =
blanchet@49990
   464
  let
blanchet@49990
   465
    val (T1, relT1) = `(fst o dest_relT) (fastype_of t1);
blanchet@49990
   466
    val (T2, relT2) = `(fst o dest_relT) (fastype_of t2);
blanchet@49990
   467
  in
blanchet@49990
   468
    Const (binop, relT1 --> relT2 --> mk_relT (typop (T1, T2), typop (T1, T2))) $ t1 $ t2
blanchet@49990
   469
  end;
blanchet@49990
   470
blanchet@49990
   471
val mk_csum = mk_card_binop @{const_name csum} mk_sumT;
blanchet@49990
   472
val mk_cprod = mk_card_binop @{const_name cprod} HOLogic.mk_prodT;
blanchet@49990
   473
val mk_cexp = mk_card_binop @{const_name cexp} mk_partial_funT;
blanchet@49990
   474
val mk_ccexp = mk_card_binop @{const_name ccexp} mk_partial_funT;
blanchet@49990
   475
val ctwo = @{term ctwo};
blanchet@49990
   476
blanchet@49990
   477
fun mk_collect xs defT =
blanchet@49990
   478
  let val T = (case xs of [] => defT | (x::_) => fastype_of x);
blanchet@49990
   479
  in Const (@{const_name collect}, HOLogic.mk_setT T --> T) $ (HOLogic.mk_set T xs) end;
blanchet@49990
   480
blanchet@49990
   481
fun mk_permute src dest xs = map (nth xs o (fn x => find_index ((curry op =) x) src)) dest;
blanchet@49990
   482
blanchet@50090
   483
val list_all_free =
blanchet@50090
   484
  fold_rev (fn free => fn P =>
blanchet@50090
   485
    let val (x, T) = Term.dest_Free free;
blanchet@50090
   486
    in HOLogic.all_const T $ Term.absfree (x, T) P end);
blanchet@50090
   487
blanchet@50090
   488
val list_exists_free =
blanchet@50090
   489
  fold_rev (fn free => fn P =>
blanchet@50090
   490
    let val (x, T) = Term.dest_Free free;
blanchet@50090
   491
    in HOLogic.exists_const T $ Term.absfree (x, T) P end);
blanchet@50090
   492
blanchet@49990
   493
fun find_indices xs ys = map_filter I
blanchet@49990
   494
  (map_index (fn (i, y) => if member (op =) xs y then SOME i else NONE) ys);
blanchet@49990
   495
blanchet@49990
   496
fun mk_trans thm1 thm2 = trans OF [thm1, thm2];
blanchet@49990
   497
fun mk_sym thm = sym OF [thm];
blanchet@49990
   498
blanchet@49990
   499
(*TODO: antiquote heavily used theorems once*)
blanchet@49990
   500
val ctrans = @{thm ordLeq_transitive};
blanchet@49990
   501
val o_apply = @{thm o_apply};
blanchet@49990
   502
blanchet@49990
   503
fun mk_nthN 1 t 1 = t
blanchet@49990
   504
  | mk_nthN _ t 1 = HOLogic.mk_fst t
blanchet@49990
   505
  | mk_nthN 2 t 2 = HOLogic.mk_snd t
blanchet@49990
   506
  | mk_nthN n t m = mk_nthN (n - 1) (HOLogic.mk_snd t) (m - 1);
blanchet@49990
   507
blanchet@49990
   508
fun mk_nth_conv n m =
blanchet@49990
   509
  let
blanchet@49990
   510
    fun thm b = if b then @{thm fst_snd} else @{thm snd_snd}
blanchet@49990
   511
    fun mk_nth_conv _ 1 1 = refl
blanchet@49990
   512
      | mk_nth_conv _ _ 1 = @{thm fst_conv}
blanchet@49990
   513
      | mk_nth_conv _ 2 2 = @{thm snd_conv}
blanchet@49990
   514
      | mk_nth_conv b _ 2 = @{thm snd_conv} RS thm b
blanchet@49990
   515
      | mk_nth_conv b n m = mk_nth_conv false (n - 1) (m - 1) RS thm b;
blanchet@49990
   516
  in mk_nth_conv (not (m = n)) n m end;
blanchet@49990
   517
blanchet@49990
   518
fun mk_nthI 1 1 = @{thm TrueE[OF TrueI]}
blanchet@49990
   519
  | mk_nthI n m = fold (curry op RS) (replicate (m - 1) @{thm sndI})
blanchet@49990
   520
    (if m = n then @{thm TrueE[OF TrueI]} else @{thm fstI});
blanchet@49990
   521
blanchet@49990
   522
fun mk_conjunctN 1 1 = @{thm TrueE[OF TrueI]}
blanchet@49990
   523
  | mk_conjunctN _ 1 = conjunct1
blanchet@49990
   524
  | mk_conjunctN 2 2 = conjunct2
blanchet@49990
   525
  | mk_conjunctN n m = conjunct2 RS (mk_conjunctN (n - 1) (m - 1));
blanchet@49990
   526
blanchet@50352
   527
fun conj_dests n thm = map (fn k => thm RS mk_conjunctN n k) (1 upto n);
blanchet@50352
   528
blanchet@49990
   529
fun mk_conjIN 1 = @{thm TrueE[OF TrueI]}
blanchet@49990
   530
  | mk_conjIN n = mk_conjIN (n - 1) RSN (2, conjI);
blanchet@49990
   531
blanchet@50090
   532
fun mk_disjIN 1 1 = @{thm TrueE[OF TrueI]}
blanchet@50090
   533
  | mk_disjIN _ 1 = disjI1
blanchet@50090
   534
  | mk_disjIN 2 2 = disjI2
blanchet@50090
   535
  | mk_disjIN n m = (mk_disjIN (n - 1) (m - 1)) RS disjI2;
blanchet@50090
   536
blanchet@49990
   537
fun mk_ordLeq_csum 1 1 thm = thm
blanchet@49990
   538
  | mk_ordLeq_csum _ 1 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum1}]
blanchet@49990
   539
  | mk_ordLeq_csum 2 2 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum2}]
blanchet@49990
   540
  | mk_ordLeq_csum n m thm = @{thm ordLeq_transitive} OF
blanchet@49990
   541
    [mk_ordLeq_csum (n - 1) (m - 1) thm, @{thm ordLeq_csum2[OF Card_order_csum]}];
blanchet@49990
   542
blanchet@49990
   543
local
blanchet@49990
   544
  fun mk_Un_upper' 0 = subset_refl
blanchet@49990
   545
    | mk_Un_upper' 1 = @{thm Un_upper1}
blanchet@49990
   546
    | mk_Un_upper' k = Library.foldr (op RS o swap)
blanchet@49990
   547
      (replicate (k - 1) @{thm subset_trans[OF Un_upper1]}, @{thm Un_upper1});
blanchet@49990
   548
in
blanchet@49990
   549
  fun mk_Un_upper 1 1 = subset_refl
blanchet@49990
   550
    | mk_Un_upper n 1 = mk_Un_upper' (n - 2) RS @{thm subset_trans[OF Un_upper1]}
blanchet@49990
   551
    | mk_Un_upper n m = mk_Un_upper' (n - m) RS @{thm subset_trans[OF Un_upper2]};
blanchet@49990
   552
end;
blanchet@49990
   553
blanchet@49990
   554
local
blanchet@50381
   555
  fun mk_UnIN' 0 = @{thm UnI2}
blanchet@50381
   556
    | mk_UnIN' m = mk_UnIN' (m - 1) RS @{thm UnI1};
blanchet@49990
   557
in
blanchet@50381
   558
  fun mk_UnIN 1 1 = @{thm TrueE[OF TrueI]}
blanchet@50381
   559
    | mk_UnIN n 1 = Library.foldr1 (op RS o swap) (replicate (n - 1) @{thm UnI1})
blanchet@50381
   560
    | mk_UnIN n m = mk_UnIN' (n - m)
blanchet@49990
   561
end;
blanchet@49990
   562
blanchet@50090
   563
fun interleave xs ys = flat (map2 (fn x => fn y => [x, y]) xs ys);
blanchet@50090
   564
blanchet@49990
   565
fun transpose [] = []
blanchet@49990
   566
  | transpose ([] :: xss) = transpose xss
blanchet@49990
   567
  | transpose xss = map hd xss :: transpose (map tl xss);
blanchet@49990
   568
blanchet@50227
   569
fun seq_conds f n k xs =
blanchet@50227
   570
  if k = n then
blanchet@50227
   571
    map (f false) (take (k - 1) xs)
blanchet@50227
   572
  else
blanchet@50227
   573
    let val (negs, pos) = split_last (take k xs) in
blanchet@50227
   574
      map (f false) negs @ [f true pos]
blanchet@50227
   575
    end;
blanchet@50227
   576
blanchet@49990
   577
fun mk_unabs_def 0 thm = thm
blanchet@49990
   578
  | mk_unabs_def n thm = mk_unabs_def (n - 1) thm RS @{thm spec[OF iffD1[OF fun_eq_iff]]};
blanchet@49990
   579
blanchet@49990
   580
end;