src/HOL/BNF/Tools/bnf_util.ML
author blanchet
Fri Jun 07 09:30:13 2013 +0200 (2013-06-07)
changeset 52334 705bc4f5fc70
parent 52280 c3f837d92537
child 52545 d2ad6eae514f
permissions -rw-r--r--
tuning
     1 (*  Title:      HOL/BNF/Tools/bnf_util.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Copyright   2012
     4 
     5 Library for bounded natural functors.
     6 *)
     7 
     8 signature BNF_UTIL =
     9 sig
    10   val map3: ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
    11   val map4: ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list
    12   val map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f) ->
    13     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list
    14   val map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) ->
    15     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list
    16   val map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) ->
    17     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list
    18   val map8: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) ->
    19     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> 'i list
    20   val map9: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j) ->
    21     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    22     'i list -> 'j list
    23   val map10: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k) ->
    24     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    25     'i list -> 'j list -> 'k list
    26   val map11: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l) ->
    27     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    28     'i list -> 'j list -> 'k list -> 'l list
    29   val map12: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm) ->
    30     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    31     'i list -> 'j list -> 'k list -> 'l list -> 'm list
    32   val fold_map2: ('a -> 'b -> 'c -> 'd * 'c) -> 'a list -> 'b list -> 'c -> 'd list * 'c
    33   val fold_map3: ('a -> 'b -> 'c -> 'd -> 'e * 'd) ->
    34     'a list -> 'b list -> 'c list -> 'd -> 'e list * 'd
    35   val fold_map4: ('a -> 'b -> 'c -> 'd -> 'e -> 'f * 'e) ->
    36     'a list -> 'b list -> 'c list -> 'd list -> 'e -> 'f list * 'e
    37   val fold_map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g * 'f) ->
    38     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f -> 'g list * 'f
    39   val fold_map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h * 'g) ->
    40     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g -> 'h list * 'g
    41   val fold_map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i * 'h) ->
    42     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h -> 'i list * 'h
    43   val fold_map8: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j * 'i) ->
    44     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> 'i ->
    45     'j list * 'i
    46   val fold_map9: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k * 'j) ->
    47     'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list ->
    48     'i list -> 'j -> 'k list * 'j
    49   val split_list4: ('a * 'b * 'c * 'd) list -> 'a list * 'b list * 'c list * 'd list
    50   val splice: 'a list -> 'a list -> 'a list
    51   val transpose: 'a list list -> 'a list list
    52   val sort_like: ('a * 'b -> bool) -> 'b list -> 'c list -> 'a list -> 'c list
    53   val seq_conds: (bool -> 'a -> 'b) -> int -> int -> 'a list -> 'b list
    54   val pad_list: 'a -> int -> 'a list -> 'a list
    55 
    56   val mk_fresh_names: Proof.context -> int -> string -> string list * Proof.context
    57   val mk_TFrees: int -> Proof.context -> typ list * Proof.context
    58   val mk_TFreess: int list -> Proof.context -> typ list list * Proof.context
    59   val mk_TFrees': sort list -> Proof.context -> typ list * Proof.context
    60   val mk_Frees: string -> typ list -> Proof.context -> term list * Proof.context
    61   val mk_Freess: string -> typ list list -> Proof.context -> term list list * Proof.context
    62   val mk_Freesss: string -> typ list list list -> Proof.context ->
    63     term list list list * Proof.context
    64   val mk_Freessss: string -> typ list list list list -> Proof.context ->
    65     term list list list list * Proof.context
    66   val mk_Frees': string -> typ list -> Proof.context ->
    67     (term list * (string * typ) list) * Proof.context
    68   val mk_Freess': string -> typ list list -> Proof.context ->
    69     (term list list * (string * typ) list list) * Proof.context
    70   val retype_free: typ -> term -> term
    71   val nonzero_string_of_int: int -> string
    72 
    73   val num_binder_types: typ -> int
    74   val strip_typeN: int -> typ -> typ list * typ
    75 
    76   val mk_predT: typ list -> typ
    77   val mk_pred1T: typ -> typ
    78   val mk_pred2T: typ -> typ -> typ
    79   val mk_optionT: typ -> typ
    80   val mk_relT: typ * typ -> typ
    81   val dest_relT: typ -> typ * typ
    82   val dest_pred2T: typ -> typ * typ
    83   val mk_sumT: typ * typ -> typ
    84 
    85   val ctwo: term
    86   val fst_const: typ -> term
    87   val snd_const: typ -> term
    88   val Id_const: typ -> term
    89 
    90   val mk_Ball: term -> term -> term
    91   val mk_Bex: term -> term -> term
    92   val mk_Card_order: term -> term
    93   val mk_Field: term -> term
    94   val mk_Gr: term -> term -> term
    95   val mk_Grp: term -> term -> term
    96   val mk_IfN: typ -> term list -> term list -> term
    97   val mk_Trueprop_eq: term * term -> term
    98   val mk_UNION: term -> term -> term
    99   val mk_Union: typ -> term
   100   val mk_card_binop: string -> (typ * typ -> typ) -> term -> term -> term
   101   val mk_card_of: term -> term
   102   val mk_card_order: term -> term
   103   val mk_ccexp: term -> term -> term
   104   val mk_cexp: term -> term -> term
   105   val mk_cinfinite: term -> term
   106   val mk_collect: term list -> typ -> term
   107   val mk_converse: term -> term
   108   val mk_conversep: term -> term
   109   val mk_cprod: term -> term -> term
   110   val mk_csum: term -> term -> term
   111   val mk_dir_image: term -> term -> term
   112   val mk_image: term -> term
   113   val mk_in: term list -> term list -> typ -> term
   114   val mk_leq: term -> term -> term
   115   val mk_ordLeq: term -> term -> term
   116   val mk_rel_comp: term * term -> term
   117   val mk_rel_compp: term * term -> term
   118   val mk_wpull: term -> term -> term -> term -> term -> (term * term) option -> term -> term -> term
   119 
   120   val rapp: term -> term -> term
   121 
   122   val list_all_free: term list -> term -> term
   123   val list_exists_free: term list -> term -> term
   124 
   125   (*parameterized terms*)
   126   val mk_nthN: int -> term -> int -> term
   127 
   128   (*parameterized thms*)
   129   val mk_Un_upper: int -> int -> thm
   130   val mk_conjIN: int -> thm
   131   val mk_conjunctN: int -> int -> thm
   132   val conj_dests: int -> thm -> thm list
   133   val mk_disjIN: int -> int -> thm
   134   val mk_nthI: int -> int -> thm
   135   val mk_nth_conv: int -> int -> thm
   136   val mk_ordLeq_csum: int -> int -> thm -> thm
   137   val mk_UnIN: int -> int -> thm
   138 
   139   val Pair_eqD: thm
   140   val Pair_eqI: thm
   141   val ctrans: thm
   142   val id_apply: thm
   143   val meta_mp: thm
   144   val meta_spec: thm
   145   val o_apply: thm
   146   val set_mp: thm
   147   val set_rev_mp: thm
   148   val subset_UNIV: thm
   149   val mk_sym: thm -> thm
   150   val mk_trans: thm -> thm -> thm
   151   val mk_unabs_def: int -> thm -> thm
   152 
   153   val is_triv_implies: thm -> bool
   154   val is_refl: thm -> bool
   155   val is_concl_refl: thm -> bool
   156   val no_refl: thm list -> thm list
   157   val no_reflexive: thm list -> thm list
   158 
   159   val cterm_instantiate_pos: cterm option list -> thm -> thm
   160   val fold_thms: Proof.context -> thm list -> thm -> thm
   161   val unfold_thms: Proof.context -> thm list -> thm -> thm
   162 
   163   val mk_permute: ''a list -> ''a list -> 'b list -> 'b list
   164   val find_indices: ''a list -> ''a list -> int list
   165 
   166   val certifyT: Proof.context -> typ -> ctyp
   167   val certify: Proof.context -> term -> cterm
   168 
   169   val standard_binding: binding
   170   val equal_binding: binding
   171   val parse_binding: binding parser
   172   val parse_binding_colon: binding parser
   173   val parse_opt_binding_colon: binding parser
   174 
   175   val typedef: binding * (string * sort) list * mixfix -> term ->
   176     (binding * binding) option -> tactic -> local_theory -> (string * Typedef.info) * local_theory
   177 
   178   val WRAP: ('a -> tactic) -> ('a -> tactic) -> 'a list -> tactic -> tactic
   179   val WRAP': ('a -> int -> tactic) -> ('a -> int -> tactic) -> 'a list -> (int -> tactic) -> int ->
   180     tactic
   181   val CONJ_WRAP_GEN: tactic -> ('a -> tactic) -> 'a list -> tactic
   182   val CONJ_WRAP_GEN': (int -> tactic) -> ('a -> int -> tactic) -> 'a list -> int -> tactic
   183   val CONJ_WRAP: ('a -> tactic) -> 'a list -> tactic
   184   val CONJ_WRAP': ('a -> int -> tactic) -> 'a list -> int -> tactic
   185 end;
   186 
   187 structure BNF_Util : BNF_UTIL =
   188 struct
   189 
   190 (* Library proper *)
   191 
   192 fun map3 _ [] [] [] = []
   193   | map3 f (x1::x1s) (x2::x2s) (x3::x3s) = f x1 x2 x3 :: map3 f x1s x2s x3s
   194   | map3 _ _ _ _ = raise ListPair.UnequalLengths;
   195 
   196 fun map4 _ [] [] [] [] = []
   197   | map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) = f x1 x2 x3 x4 :: map4 f x1s x2s x3s x4s
   198   | map4 _ _ _ _ _ = raise ListPair.UnequalLengths;
   199 
   200 fun map5 _ [] [] [] [] [] = []
   201   | map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) =
   202     f x1 x2 x3 x4 x5 :: map5 f x1s x2s x3s x4s x5s
   203   | map5 _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   204 
   205 fun map6 _ [] [] [] [] [] [] = []
   206   | map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) =
   207     f x1 x2 x3 x4 x5 x6 :: map6 f x1s x2s x3s x4s x5s x6s
   208   | map6 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   209 
   210 fun map7 _ [] [] [] [] [] [] [] = []
   211   | map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) =
   212     f x1 x2 x3 x4 x5 x6 x7 :: map7 f x1s x2s x3s x4s x5s x6s x7s
   213   | map7 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   214 
   215 fun map8 _ [] [] [] [] [] [] [] [] = []
   216   | map8 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) =
   217     f x1 x2 x3 x4 x5 x6 x7 x8 :: map8 f x1s x2s x3s x4s x5s x6s x7s x8s
   218   | map8 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   219 
   220 fun map9 _ [] [] [] [] [] [] [] [] [] = []
   221   | map9 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
   222       (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) =
   223     f x1 x2 x3 x4 x5 x6 x7 x8 x9 :: map9 f x1s x2s x3s x4s x5s x6s x7s x8s x9s
   224   | map9 _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   225 
   226 fun map10 _ [] [] [] [] [] [] [] [] [] [] = []
   227   | map10 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
   228       (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) =
   229     f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 :: map10 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s
   230   | map10 _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   231 
   232 fun map11 _ [] [] [] [] [] [] [] [] [] [] [] = []
   233   | map11 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
   234       (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) (x11::x11s) =
   235     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
   236   | map11 _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   237 
   238 fun map12 _ [] [] [] [] [] [] [] [] [] [] [] [] = []
   239   | map12 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s)
   240       (x6::x6s) (x7::x7s) (x8::x8s) (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) =
   241     f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ::
   242       map12 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s
   243   | map12 _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   244 
   245 fun fold_map2 _ [] [] acc = ([], acc)
   246   | fold_map2 f (x1::x1s) (x2::x2s) acc =
   247     let
   248       val (x, acc') = f x1 x2 acc;
   249       val (xs, acc'') = fold_map2 f x1s x2s acc';
   250     in (x :: xs, acc'') end
   251   | fold_map2 _ _ _ _ = raise ListPair.UnequalLengths;
   252 
   253 fun fold_map3 _ [] [] [] acc = ([], acc)
   254   | fold_map3 f (x1::x1s) (x2::x2s) (x3::x3s) acc =
   255     let
   256       val (x, acc') = f x1 x2 x3 acc;
   257       val (xs, acc'') = fold_map3 f x1s x2s x3s acc';
   258     in (x :: xs, acc'') end
   259   | fold_map3 _ _ _ _ _ = raise ListPair.UnequalLengths;
   260 
   261 fun fold_map4 _ [] [] [] [] acc = ([], acc)
   262   | fold_map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) acc =
   263     let
   264       val (x, acc') = f x1 x2 x3 x4 acc;
   265       val (xs, acc'') = fold_map4 f x1s x2s x3s x4s acc';
   266     in (x :: xs, acc'') end
   267   | fold_map4 _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   268 
   269 fun fold_map5 _ [] [] [] [] [] acc = ([], acc)
   270   | fold_map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) acc =
   271     let
   272       val (x, acc') = f x1 x2 x3 x4 x5 acc;
   273       val (xs, acc'') = fold_map5 f x1s x2s x3s x4s x5s acc';
   274     in (x :: xs, acc'') end
   275   | fold_map5 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   276 
   277 fun fold_map6 _ [] [] [] [] [] [] acc = ([], acc)
   278   | fold_map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) acc =
   279     let
   280       val (x, acc') = f x1 x2 x3 x4 x5 x6 acc;
   281       val (xs, acc'') = fold_map6 f x1s x2s x3s x4s x5s x6s acc';
   282     in (x :: xs, acc'') end
   283   | fold_map6 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   284 
   285 fun fold_map7 _ [] [] [] [] [] [] [] acc = ([], acc)
   286   | fold_map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) acc =
   287     let
   288       val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 acc;
   289       val (xs, acc'') = fold_map7 f x1s x2s x3s x4s x5s x6s x7s acc';
   290     in (x :: xs, acc'') end
   291   | fold_map7 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   292 
   293 fun fold_map8 _ [] [] [] [] [] [] [] [] acc = ([], acc)
   294   | fold_map8 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s)
   295       acc =
   296     let
   297       val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 x8 acc;
   298       val (xs, acc'') = fold_map8 f x1s x2s x3s x4s x5s x6s x7s x8s acc';
   299     in (x :: xs, acc'') end
   300   | fold_map8 _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   301 
   302 fun fold_map9 _ [] [] [] [] [] [] [] [] [] acc = ([], acc)
   303   | fold_map9 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s)
   304       (x9::x9s) acc =
   305     let
   306       val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 x8 x9 acc;
   307       val (xs, acc'') = fold_map9 f x1s x2s x3s x4s x5s x6s x7s x8s x9s acc';
   308     in (x :: xs, acc'') end
   309   | fold_map9 _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths;
   310 
   311 fun split_list4 [] = ([], [], [], [])
   312   | split_list4 ((x1, x2, x3, x4) :: xs) =
   313     let val (xs1, xs2, xs3, xs4) = split_list4 xs;
   314     in (x1 :: xs1, x2 :: xs2, x3 :: xs3, x4 :: xs4) end;
   315 
   316 (*stolen from ~~/src/HOL/Tools/SMT/smt_utils.ML*)
   317 fun certify ctxt = Thm.cterm_of (Proof_Context.theory_of ctxt);
   318 fun certifyT ctxt = Thm.ctyp_of (Proof_Context.theory_of ctxt);
   319 
   320 (* The standard binding stands for a name generated following the canonical convention (e.g.
   321    "is_Nil" from "Nil"). The smart binding is either the standard binding or no binding at all,
   322    depending on the context. *)
   323 val standard_binding = @{binding _};
   324 val equal_binding = @{binding "="};
   325 
   326 val parse_binding = Parse.binding || @{keyword "="} >> K equal_binding;
   327 val parse_binding_colon = parse_binding --| @{keyword ":"};
   328 val parse_opt_binding_colon = Scan.optional parse_binding_colon Binding.empty;
   329 
   330 (*TODO: is this really different from Typedef.add_typedef_global?*)
   331 fun typedef typ set opt_morphs tac lthy =
   332   let
   333     val ((name, info), (lthy, lthy_old)) =
   334       lthy
   335       |> Typedef.add_typedef typ set opt_morphs tac
   336       ||> `Local_Theory.restore;
   337     val phi = Proof_Context.export_morphism lthy_old lthy;
   338   in
   339     ((name, Typedef.transform_info phi info), lthy)
   340   end;
   341 
   342 (*Tactical WRAP surrounds a static given tactic (core) with two deterministic chains of tactics*)
   343 fun WRAP gen_before gen_after xs core_tac =
   344   fold_rev (fn x => fn tac => gen_before x THEN tac THEN gen_after x) xs core_tac;
   345 
   346 fun WRAP' gen_before gen_after xs core_tac =
   347   fold_rev (fn x => fn tac => gen_before x THEN' tac THEN' gen_after x) xs core_tac;
   348 
   349 fun CONJ_WRAP_GEN conj_tac gen_tac xs =
   350   let val (butlast, last) = split_last xs;
   351   in WRAP (fn thm => conj_tac THEN gen_tac thm) (K all_tac) butlast (gen_tac last) end;
   352 
   353 fun CONJ_WRAP_GEN' conj_tac gen_tac xs =
   354   let val (butlast, last) = split_last xs;
   355   in WRAP' (fn thm => conj_tac THEN' gen_tac thm) (K (K all_tac)) butlast (gen_tac last) end;
   356 
   357 (*not eta-converted because of monotype restriction*)
   358 fun CONJ_WRAP gen_tac = CONJ_WRAP_GEN (rtac conjI 1) gen_tac;
   359 fun CONJ_WRAP' gen_tac = CONJ_WRAP_GEN' (rtac conjI) gen_tac;
   360 
   361 
   362 
   363 (* Term construction *)
   364 
   365 (** Fresh variables **)
   366 
   367 fun nonzero_string_of_int 0 = ""
   368   | nonzero_string_of_int n = string_of_int n;
   369 
   370 val mk_TFrees' = apfst (map TFree) oo Variable.invent_types;
   371 
   372 fun mk_TFrees n = mk_TFrees' (replicate n HOLogic.typeS);
   373 val mk_TFreess = fold_map mk_TFrees;
   374 
   375 fun mk_names n x = if n = 1 then [x] else map (fn i => x ^ string_of_int i) (1 upto n);
   376 
   377 fun mk_fresh_names ctxt = (fn xs => Variable.variant_fixes xs ctxt) oo mk_names;
   378 fun mk_Frees x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => map2 (curry Free) xs Ts);
   379 fun mk_Freess x Tss = fold_map2 mk_Frees (mk_names (length Tss) x) Tss;
   380 fun mk_Freesss x Tsss = fold_map2 mk_Freess (mk_names (length Tsss) x) Tsss;
   381 fun mk_Freessss x Tssss = fold_map2 mk_Freesss (mk_names (length Tssss) x) Tssss;
   382 fun mk_Frees' x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => `(map Free) (xs ~~ Ts));
   383 fun mk_Freess' x Tss = fold_map2 mk_Frees' (mk_names (length Tss) x) Tss #>> split_list;
   384 
   385 fun retype_free T (Free (s, _)) = Free (s, T)
   386   | retype_free _ t = raise TERM ("retype_free", [t]);
   387 
   388 
   389 (** Types **)
   390 
   391 (*stolen from ~~/src/HOL/Tools/Nitpick/nitpick_hol.ML*)
   392 fun num_binder_types (Type (@{type_name fun}, [_, T2])) =
   393     1 + num_binder_types T2
   394   | num_binder_types _ = 0
   395 
   396 fun strip_typeN 0 T = ([], T)
   397   | strip_typeN n (Type (@{type_name fun}, [T, T'])) = strip_typeN (n - 1) T' |>> cons T
   398   | strip_typeN _ T = raise TYPE ("strip_typeN", [T], []);
   399 
   400 fun mk_predT Ts = Ts ---> HOLogic.boolT;
   401 fun mk_pred1T T = mk_predT [T];
   402 fun mk_pred2T T U = mk_predT [T, U];
   403 fun mk_optionT T = Type (@{type_name option}, [T]);
   404 val mk_relT = HOLogic.mk_setT o HOLogic.mk_prodT;
   405 val dest_relT = HOLogic.dest_prodT o HOLogic.dest_setT;
   406 val dest_pred2T = apsnd Term.domain_type o Term.dest_funT;
   407 fun mk_sumT (LT, RT) = Type (@{type_name Sum_Type.sum}, [LT, RT]);
   408 fun mk_partial_funT (ranT, domT) = domT --> mk_optionT ranT;
   409 
   410 
   411 (** Constants **)
   412 
   413 fun fst_const T = Const (@{const_name fst}, T --> fst (HOLogic.dest_prodT T));
   414 fun snd_const T = Const (@{const_name snd}, T --> snd (HOLogic.dest_prodT T));
   415 fun Id_const T = Const (@{const_name Id}, mk_relT (T, T));
   416 
   417 
   418 (** Operators **)
   419 
   420 val mk_Trueprop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
   421 
   422 fun mk_IfN _ _ [t] = t
   423   | mk_IfN T (c :: cs) (t :: ts) =
   424     Const (@{const_name If}, HOLogic.boolT --> T --> T --> T) $ c $ t $ mk_IfN T cs ts;
   425 
   426 fun mk_converse R =
   427   let
   428     val RT = dest_relT (fastype_of R);
   429     val RST = mk_relT (snd RT, fst RT);
   430   in Const (@{const_name converse}, fastype_of R --> RST) $ R end;
   431 
   432 fun mk_rel_comp (R, S) =
   433   let
   434     val RT = fastype_of R;
   435     val ST = fastype_of S;
   436     val RST = mk_relT (fst (dest_relT RT), snd (dest_relT ST));
   437   in Const (@{const_name relcomp}, RT --> ST --> RST) $ R $ S end;
   438 
   439 fun mk_Gr A f =
   440   let val ((AT, BT), FT) = `dest_funT (fastype_of f);
   441   in Const (@{const_name Gr}, HOLogic.mk_setT AT --> FT --> mk_relT (AT, BT)) $ A $ f end;
   442 
   443 fun mk_conversep R =
   444   let
   445     val RT = dest_pred2T (fastype_of R);
   446     val RST = mk_pred2T (snd RT) (fst RT);
   447   in Const (@{const_name conversep}, fastype_of R --> RST) $ R end;
   448 
   449 fun mk_rel_compp (R, S) =
   450   let
   451     val RT = fastype_of R;
   452     val ST = fastype_of S;
   453     val RST = mk_pred2T (fst (dest_pred2T RT)) (snd (dest_pred2T ST));
   454   in Const (@{const_name relcompp}, RT --> ST --> RST) $ R $ S end;
   455 
   456 fun mk_Grp A f =
   457   let val ((AT, BT), FT) = `dest_funT (fastype_of f);
   458   in Const (@{const_name Grp}, HOLogic.mk_setT AT --> FT --> mk_pred2T AT BT) $ A $ f end;
   459 
   460 fun mk_image f =
   461   let val (T, U) = dest_funT (fastype_of f);
   462   in Const (@{const_name image},
   463     (T --> U) --> (HOLogic.mk_setT T) --> (HOLogic.mk_setT U)) $ f end;
   464 
   465 fun mk_Ball X f =
   466   Const (@{const_name Ball}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f;
   467 
   468 fun mk_Bex X f =
   469   Const (@{const_name Bex}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f;
   470 
   471 fun mk_UNION X f =
   472   let val (T, U) = dest_funT (fastype_of f);
   473   in Const (@{const_name SUPR}, fastype_of X --> (T --> U) --> U) $ X $ f end;
   474 
   475 fun mk_Union T =
   476   Const (@{const_name Sup}, HOLogic.mk_setT (HOLogic.mk_setT T) --> HOLogic.mk_setT T);
   477 
   478 fun mk_Field r =
   479   let val T = fst (dest_relT (fastype_of r));
   480   in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
   481 
   482 fun mk_card_order bd =
   483   let
   484     val T = fastype_of bd;
   485     val AT = fst (dest_relT T);
   486   in
   487     Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $
   488       (HOLogic.mk_UNIV AT) $ bd
   489   end;
   490 
   491 fun mk_Card_order bd =
   492   let
   493     val T = fastype_of bd;
   494     val AT = fst (dest_relT T);
   495   in
   496     Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $
   497       mk_Field bd $ bd
   498   end;
   499 
   500 fun mk_cinfinite bd =
   501   Const (@{const_name cinfinite}, fastype_of bd --> HOLogic.boolT) $ bd;
   502 
   503 fun mk_ordLeq t1 t2 =
   504   HOLogic.mk_mem (HOLogic.mk_prod (t1, t2),
   505     Const (@{const_name ordLeq}, mk_relT (fastype_of t1, fastype_of t2)));
   506 
   507 fun mk_card_of A =
   508   let
   509     val AT = fastype_of A;
   510     val T = HOLogic.dest_setT AT;
   511   in
   512     Const (@{const_name card_of}, AT --> mk_relT (T, T)) $ A
   513   end;
   514 
   515 fun mk_dir_image r f =
   516   let val (T, U) = dest_funT (fastype_of f);
   517   in Const (@{const_name dir_image}, mk_relT (T, T) --> (T --> U) --> mk_relT (U, U)) $ r $ f end;
   518 
   519 (*FIXME: "x"?*)
   520 (*(nth sets i) must be of type "T --> 'ai set"*)
   521 fun mk_in As sets T =
   522   let
   523     fun in_single set A =
   524       let val AT = fastype_of A;
   525       in Const (@{const_name less_eq},
   526         AT --> AT --> HOLogic.boolT) $ (set $ Free ("x", T)) $ A end;
   527   in
   528     if length sets > 0
   529     then HOLogic.mk_Collect ("x", T, foldr1 (HOLogic.mk_conj) (map2 in_single sets As))
   530     else HOLogic.mk_UNIV T
   531   end;
   532 
   533 fun mk_wpull A B1 B2 f1 f2 pseudo p1 p2 =
   534   let
   535     val AT = fastype_of A;
   536     val BT1 = fastype_of B1;
   537     val BT2 = fastype_of B2;
   538     val FT1 = fastype_of f1;
   539     val FT2 = fastype_of f2;
   540     val PT1 = fastype_of p1;
   541     val PT2 = fastype_of p2;
   542     val T1 = HOLogic.dest_setT BT1;
   543     val T2 = HOLogic.dest_setT BT2;
   544     val domP = domain_type PT1;
   545     val ranF = range_type FT1;
   546     val _ = if is_some pseudo orelse
   547                (HOLogic.dest_setT AT = domP andalso
   548                domain_type FT1 = T1 andalso
   549                domain_type FT2 = T2 andalso
   550                domain_type PT2 = domP andalso
   551                range_type PT1 = T1 andalso
   552                range_type PT2 = T2 andalso
   553                range_type FT2 = ranF)
   554       then () else raise TYPE ("mk_wpull", [BT1, BT2, FT1, FT2, PT1, PT2], []);
   555   in
   556     (case pseudo of
   557       NONE => Const (@{const_name wpull},
   558         AT --> BT1 --> BT2 --> FT1 --> FT2 --> PT1 --> PT2 --> HOLogic.boolT) $
   559         A $ B1 $ B2 $ f1 $ f2 $ p1 $ p2
   560     | SOME (e1, e2) => Const (@{const_name wppull},
   561         AT --> BT1 --> BT2 --> FT1 --> FT2 --> fastype_of e1 --> fastype_of e2 -->
   562           PT1 --> PT2 --> HOLogic.boolT) $
   563         A $ B1 $ B2 $ f1 $ f2 $ e1 $ e2 $ p1 $ p2)
   564   end;
   565 
   566 fun mk_leq t1 t2 =
   567   Const (@{const_name less_eq}, (fastype_of t1) --> (fastype_of t2) --> HOLogic.boolT) $ t1 $ t2;
   568 
   569 fun mk_card_binop binop typop t1 t2 =
   570   let
   571     val (T1, relT1) = `(fst o dest_relT) (fastype_of t1);
   572     val (T2, relT2) = `(fst o dest_relT) (fastype_of t2);
   573   in
   574     Const (binop, relT1 --> relT2 --> mk_relT (typop (T1, T2), typop (T1, T2))) $ t1 $ t2
   575   end;
   576 
   577 val mk_csum = mk_card_binop @{const_name csum} mk_sumT;
   578 val mk_cprod = mk_card_binop @{const_name cprod} HOLogic.mk_prodT;
   579 val mk_cexp = mk_card_binop @{const_name cexp} mk_partial_funT;
   580 val mk_ccexp = mk_card_binop @{const_name ccexp} mk_partial_funT;
   581 val ctwo = @{term ctwo};
   582 
   583 fun mk_collect xs defT =
   584   let val T = (case xs of [] => defT | (x::_) => fastype_of x);
   585   in Const (@{const_name collect}, HOLogic.mk_setT T --> T) $ (HOLogic.mk_set T xs) end;
   586 
   587 fun mk_permute src dest xs = map (nth xs o (fn x => find_index ((curry op =) x) src)) dest;
   588 
   589 fun rapp u t = betapply (t, u);
   590 
   591 val list_all_free =
   592   fold_rev (fn free => fn P =>
   593     let val (x, T) = Term.dest_Free free;
   594     in HOLogic.all_const T $ Term.absfree (x, T) P end);
   595 
   596 val list_exists_free =
   597   fold_rev (fn free => fn P =>
   598     let val (x, T) = Term.dest_Free free;
   599     in HOLogic.exists_const T $ Term.absfree (x, T) P end);
   600 
   601 fun find_indices xs ys = map_filter I
   602   (map_index (fn (i, y) => if member (op =) xs y then SOME i else NONE) ys);
   603 
   604 fun mk_trans thm1 thm2 = trans OF [thm1, thm2];
   605 fun mk_sym thm = sym OF [thm];
   606 
   607 (*TODO: antiquote heavily used theorems once*)
   608 val Pair_eqD = @{thm iffD1[OF Pair_eq]};
   609 val Pair_eqI = @{thm iffD2[OF Pair_eq]};
   610 val ctrans = @{thm ordLeq_transitive};
   611 val id_apply = @{thm id_apply};
   612 val meta_mp = @{thm meta_mp};
   613 val meta_spec = @{thm meta_spec};
   614 val o_apply = @{thm o_apply};
   615 val set_mp = @{thm set_mp};
   616 val set_rev_mp = @{thm set_rev_mp};
   617 val subset_UNIV = @{thm subset_UNIV};
   618 
   619 fun mk_nthN 1 t 1 = t
   620   | mk_nthN _ t 1 = HOLogic.mk_fst t
   621   | mk_nthN 2 t 2 = HOLogic.mk_snd t
   622   | mk_nthN n t m = mk_nthN (n - 1) (HOLogic.mk_snd t) (m - 1);
   623 
   624 fun mk_nth_conv n m =
   625   let
   626     fun thm b = if b then @{thm fst_snd} else @{thm snd_snd}
   627     fun mk_nth_conv _ 1 1 = refl
   628       | mk_nth_conv _ _ 1 = @{thm fst_conv}
   629       | mk_nth_conv _ 2 2 = @{thm snd_conv}
   630       | mk_nth_conv b _ 2 = @{thm snd_conv} RS thm b
   631       | mk_nth_conv b n m = mk_nth_conv false (n - 1) (m - 1) RS thm b;
   632   in mk_nth_conv (not (m = n)) n m end;
   633 
   634 fun mk_nthI 1 1 = @{thm TrueE[OF TrueI]}
   635   | mk_nthI n m = fold (curry op RS) (replicate (m - 1) @{thm sndI})
   636     (if m = n then @{thm TrueE[OF TrueI]} else @{thm fstI});
   637 
   638 fun mk_conjunctN 1 1 = @{thm TrueE[OF TrueI]}
   639   | mk_conjunctN _ 1 = conjunct1
   640   | mk_conjunctN 2 2 = conjunct2
   641   | mk_conjunctN n m = conjunct2 RS (mk_conjunctN (n - 1) (m - 1));
   642 
   643 fun conj_dests n thm = map (fn k => thm RS mk_conjunctN n k) (1 upto n);
   644 
   645 fun mk_conjIN 1 = @{thm TrueE[OF TrueI]}
   646   | mk_conjIN n = mk_conjIN (n - 1) RSN (2, conjI);
   647 
   648 fun mk_disjIN 1 1 = @{thm TrueE[OF TrueI]}
   649   | mk_disjIN _ 1 = disjI1
   650   | mk_disjIN 2 2 = disjI2
   651   | mk_disjIN n m = (mk_disjIN (n - 1) (m - 1)) RS disjI2;
   652 
   653 fun mk_ordLeq_csum 1 1 thm = thm
   654   | mk_ordLeq_csum _ 1 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum1}]
   655   | mk_ordLeq_csum 2 2 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum2}]
   656   | mk_ordLeq_csum n m thm = @{thm ordLeq_transitive} OF
   657     [mk_ordLeq_csum (n - 1) (m - 1) thm, @{thm ordLeq_csum2[OF Card_order_csum]}];
   658 
   659 local
   660   fun mk_Un_upper' 0 = subset_refl
   661     | mk_Un_upper' 1 = @{thm Un_upper1}
   662     | mk_Un_upper' k = Library.foldr (op RS o swap)
   663       (replicate (k - 1) @{thm subset_trans[OF Un_upper1]}, @{thm Un_upper1});
   664 in
   665   fun mk_Un_upper 1 1 = subset_refl
   666     | mk_Un_upper n 1 = mk_Un_upper' (n - 2) RS @{thm subset_trans[OF Un_upper1]}
   667     | mk_Un_upper n m = mk_Un_upper' (n - m) RS @{thm subset_trans[OF Un_upper2]};
   668 end;
   669 
   670 local
   671   fun mk_UnIN' 0 = @{thm UnI2}
   672     | mk_UnIN' m = mk_UnIN' (m - 1) RS @{thm UnI1};
   673 in
   674   fun mk_UnIN 1 1 = @{thm TrueE[OF TrueI]}
   675     | mk_UnIN n 1 = Library.foldr1 (op RS o swap) (replicate (n - 1) @{thm UnI1})
   676     | mk_UnIN n m = mk_UnIN' (n - m)
   677 end;
   678 
   679 fun splice xs ys = flat (map2 (fn x => fn y => [x, y]) xs ys);
   680 
   681 fun transpose [] = []
   682   | transpose ([] :: xss) = transpose xss
   683   | transpose xss = map hd xss :: transpose (map tl xss);
   684 
   685 fun sort_like eq xs ys = map (fn x => nth ys (find_index (curry eq x) xs));
   686 
   687 fun seq_conds f n k xs =
   688   if k = n then
   689     map (f false) (take (k - 1) xs)
   690   else
   691     let val (negs, pos) = split_last (take k xs) in
   692       map (f false) negs @ [f true pos]
   693     end;
   694 
   695 fun pad_list x n xs = xs @ replicate (n - length xs) x;
   696 
   697 fun mk_unabs_def n = funpow n (fn thm => thm RS fun_cong);
   698 
   699 fun is_triv_implies thm =
   700   op aconv (Logic.dest_implies (Thm.prop_of thm))
   701   handle TERM _ => false;
   702 
   703 fun is_refl_prop t =
   704   op aconv (HOLogic.dest_eq (HOLogic.dest_Trueprop t))
   705   handle TERM _ => false;
   706 
   707 val is_refl = is_refl_prop o Thm.prop_of;
   708 val is_concl_refl = is_refl_prop o Logic.strip_imp_concl o Thm.prop_of;
   709 
   710 val no_refl = filter_out is_refl;
   711 val no_reflexive = filter_out Thm.is_reflexive;
   712 
   713 fun cterm_instantiate_pos cts thm =
   714   let
   715     val cert = Thm.cterm_of (Thm.theory_of_thm thm);
   716     val vars = Term.add_vars (prop_of thm) [];
   717     val vars' = rev (drop (length vars - length cts) vars);
   718     val ps = map_filter (fn (_, NONE) => NONE
   719       | (var, SOME ct) => SOME (cert (Var var), ct)) (vars' ~~ cts);
   720   in
   721     Drule.cterm_instantiate ps thm
   722   end;
   723 
   724 fun fold_thms ctxt thms = Local_Defs.fold ctxt (distinct Thm.eq_thm_prop thms);
   725 fun unfold_thms ctxt thms = Local_Defs.unfold ctxt (distinct Thm.eq_thm_prop thms);
   726 
   727 end;