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