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