src/HOL/Tools/Nitpick/nitpick_util.ML
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43870 3e060b1c844b
parent 43567 9bc5dc48f1a5
child 43926 0a2f5b86bdd7
permissions -rw-r--r--
use helpers and tweak Quickcheck's priority to it comes second (to give Solve Direct slightly more time before another prover runs)
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_util.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34969
     3
    Copyright   2008, 2009, 2010
blanchet@33192
     4
blanchet@33192
     5
General-purpose functions used by the Nitpick modules.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33705
     8
signature NITPICK_UTIL =
blanchet@33192
     9
sig
blanchet@33192
    10
  type styp = string * typ
blanchet@33192
    11
  datatype polarity = Pos | Neg | Neut
blanchet@33192
    12
blanchet@33192
    13
  exception ARG of string * string
blanchet@33192
    14
  exception BAD of string * string
blanchet@34121
    15
  exception TOO_SMALL of string * string
blanchet@34121
    16
  exception TOO_LARGE of string * string
blanchet@33192
    17
  exception NOT_SUPPORTED of string
blanchet@33192
    18
  exception SAME of unit
blanchet@33192
    19
blanchet@33192
    20
  val nitpick_prefix : string
blanchet@33192
    21
  val curry3 : ('a * 'b * 'c -> 'd) -> 'a -> 'b -> 'c -> 'd
blanchet@33705
    22
  val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
blanchet@35385
    23
  val pair_from_fun : (bool -> 'a) -> 'a * 'a
blanchet@35385
    24
  val fun_from_pair : 'a * 'a -> bool -> 'a
blanchet@35385
    25
  val int_from_bool : bool -> int
blanchet@33705
    26
  val nat_minus : int -> int -> int
blanchet@33192
    27
  val reasonable_power : int -> int -> int
blanchet@33192
    28
  val exact_log : int -> int -> int
blanchet@33192
    29
  val exact_root : int -> int -> int
blanchet@33192
    30
  val offset_list : int list -> int list
blanchet@33192
    31
  val index_seq : int -> int -> int list
blanchet@33192
    32
  val filter_indices : int list -> 'a list -> 'a list
blanchet@33192
    33
  val filter_out_indices : int list -> 'a list -> 'a list
blanchet@33192
    34
  val fold1 : ('a -> 'a -> 'a) -> 'a list -> 'a
blanchet@33192
    35
  val replicate_list : int -> 'a list -> 'a list
blanchet@33192
    36
  val n_fold_cartesian_product : 'a list list -> 'a list list
blanchet@33192
    37
  val all_distinct_unordered_pairs_of : ''a list -> (''a * ''a) list
blanchet@33192
    38
  val nth_combination : (int * int) list -> int -> int list
blanchet@33192
    39
  val all_combinations : (int * int) list -> int list list
blanchet@33192
    40
  val all_permutations : 'a list -> 'a list list
blanchet@33192
    41
  val batch_list : int -> 'a list -> 'a list list
blanchet@33192
    42
  val chunk_list_unevenly : int list -> 'a list -> 'a list list
blanchet@33192
    43
  val map3 : ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
blanchet@33192
    44
  val double_lookup :
blanchet@33192
    45
    ('a * 'a -> bool) -> ('a option * 'b) list -> 'a -> 'b option
blanchet@33192
    46
  val triple_lookup :
blanchet@33192
    47
    (''a * ''a -> bool) -> (''a option * 'b) list -> ''a -> 'b option
blanchet@33192
    48
  val is_substring_of : string -> string -> bool
blanchet@33192
    49
  val plural_s : int -> string
blanchet@33192
    50
  val plural_s_for_list : 'a list -> string
blanchet@36380
    51
  val serial_commas : string -> string list -> string list
blanchet@38415
    52
  val pretty_serial_commas : string -> Pretty.T list -> Pretty.T list
blanchet@36380
    53
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@36380
    54
  val parse_time_option : string -> string -> Time.time option
blanchet@40582
    55
  val string_from_time : Time.time -> string
blanchet@38890
    56
  val nat_subscript : int -> string
blanchet@33192
    57
  val flip_polarity : polarity -> polarity
blanchet@33192
    58
  val prop_T : typ
blanchet@33192
    59
  val bool_T : typ
blanchet@33192
    60
  val nat_T : typ
blanchet@33192
    61
  val int_T : typ
blanchet@37259
    62
  val simple_string_of_typ : typ -> string
blanchet@37259
    63
  val is_real_constr : theory -> string * typ -> bool
blanchet@37259
    64
  val typ_of_dtyp :
blanchet@37259
    65
    Datatype_Aux.descr -> (Datatype_Aux.dtyp * typ) list -> Datatype_Aux.dtyp
blanchet@37259
    66
    -> typ
blanchet@43567
    67
  val varify_type : Proof.context -> typ -> typ
blanchet@43567
    68
  val instantiate_type : theory -> typ -> typ -> typ -> typ
blanchet@43567
    69
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
blanchet@37259
    70
  val is_of_class_const : theory -> string * typ -> bool
blanchet@37259
    71
  val get_class_def : theory -> string -> (string * term) option
blanchet@36553
    72
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@37259
    73
  val specialize_type : theory -> string * typ -> term -> term
blanchet@38890
    74
  val eta_expand : typ list -> term -> int -> term
blanchet@33192
    75
  val time_limit : Time.time option -> ('a -> 'b) -> 'a -> 'b
blanchet@33192
    76
  val DETERM_TIMEOUT : Time.time option -> tactic -> tactic
wenzelm@39366
    77
  val set_show_all_types : Proof.context -> Proof.context
blanchet@33192
    78
  val indent_size : int
blanchet@33192
    79
  val pstrs : string -> Pretty.T list
blanchet@34969
    80
  val unyxml : string -> string
blanchet@38415
    81
  val pretty_maybe_quote : Pretty.T -> Pretty.T
blanchet@36380
    82
  val hashw : word * word -> word
blanchet@36380
    83
  val hashw_string : string * word -> word
blanchet@35866
    84
end;
blanchet@33192
    85
blanchet@33224
    86
structure Nitpick_Util : NITPICK_UTIL =
blanchet@33192
    87
struct
blanchet@33192
    88
blanchet@33192
    89
type styp = string * typ
blanchet@33192
    90
blanchet@33192
    91
datatype polarity = Pos | Neg | Neut
blanchet@33192
    92
blanchet@33192
    93
exception ARG of string * string
blanchet@33192
    94
exception BAD of string * string
blanchet@34121
    95
exception TOO_SMALL of string * string
blanchet@34121
    96
exception TOO_LARGE of string * string
blanchet@33192
    97
exception NOT_SUPPORTED of string
blanchet@33192
    98
exception SAME of unit
blanchet@33192
    99
blanchet@33192
   100
val nitpick_prefix = "Nitpick."
blanchet@33192
   101
blanchet@33192
   102
fun curry3 f = fn x => fn y => fn z => f (x, y, z)
blanchet@33192
   103
blanchet@33705
   104
fun pairf f g x = (f x, g x)
blanchet@33192
   105
blanchet@35385
   106
fun pair_from_fun f = (f false, f true)
blanchet@35385
   107
fun fun_from_pair (f, t) b = if b then t else f
blanchet@35385
   108
blanchet@35385
   109
fun int_from_bool b = if b then 1 else 0
blanchet@33705
   110
fun nat_minus i j = if i > j then i - j else 0
blanchet@33192
   111
blanchet@33192
   112
val max_exponent = 16384
blanchet@33192
   113
blanchet@35280
   114
fun reasonable_power _ 0 = 1
blanchet@33192
   115
  | reasonable_power a 1 = a
blanchet@33192
   116
  | reasonable_power 0 _ = 0
blanchet@33192
   117
  | reasonable_power 1 _ = 1
blanchet@33192
   118
  | reasonable_power a b =
blanchet@34121
   119
    if b < 0 then
blanchet@34121
   120
      raise ARG ("Nitpick_Util.reasonable_power",
blanchet@34121
   121
                 "negative exponent (" ^ signed_string_of_int b ^ ")")
blanchet@34121
   122
    else if b > max_exponent then
blanchet@34121
   123
      raise TOO_LARGE ("Nitpick_Util.reasonable_power",
blanchet@34121
   124
                       "too large exponent (" ^ signed_string_of_int b ^ ")")
blanchet@33192
   125
    else
blanchet@34121
   126
      let val c = reasonable_power a (b div 2) in
blanchet@34121
   127
        c * c * reasonable_power a (b mod 2)
blanchet@34121
   128
      end
blanchet@33192
   129
blanchet@33192
   130
fun exact_log m n =
blanchet@33192
   131
  let
blanchet@33192
   132
    val r = Math.ln (Real.fromInt n) / Math.ln (Real.fromInt m) |> Real.round
blanchet@33192
   133
  in
blanchet@33192
   134
    if reasonable_power m r = n then
blanchet@33192
   135
      r
blanchet@33192
   136
    else
blanchet@33224
   137
      raise ARG ("Nitpick_Util.exact_log",
blanchet@33192
   138
                 commas (map signed_string_of_int [m, n]))
blanchet@33192
   139
  end
blanchet@33192
   140
blanchet@33192
   141
fun exact_root m n =
blanchet@33192
   142
  let val r = Math.pow (Real.fromInt n, 1.0 / (Real.fromInt m)) |> Real.round in
blanchet@33192
   143
    if reasonable_power r m = n then
blanchet@33192
   144
      r
blanchet@33192
   145
    else
blanchet@33224
   146
      raise ARG ("Nitpick_Util.exact_root",
blanchet@33192
   147
                 commas (map signed_string_of_int [m, n]))
blanchet@33192
   148
  end
blanchet@33192
   149
blanchet@33192
   150
fun fold1 f = foldl1 (uncurry f)
blanchet@33192
   151
blanchet@33192
   152
fun replicate_list 0 _ = []
blanchet@33192
   153
  | replicate_list n xs = xs @ replicate_list (n - 1) xs
blanchet@33192
   154
blanchet@33192
   155
fun offset_list ns = rev (tl (fold (fn x => fn xs => (x + hd xs) :: xs) ns [0]))
blanchet@33192
   156
fun index_seq j0 n = if j0 < 0 then j0 downto j0 - n + 1 else j0 upto j0 + n - 1
blanchet@33192
   157
blanchet@33192
   158
fun filter_indices js xs =
blanchet@33192
   159
  let
blanchet@33192
   160
    fun aux _ [] _ = []
blanchet@33192
   161
      | aux i (j :: js) (x :: xs) =
blanchet@33192
   162
        if i = j then x :: aux (i + 1) js xs else aux (i + 1) (j :: js) xs
blanchet@33224
   163
      | aux _ _ _ = raise ARG ("Nitpick_Util.filter_indices",
blanchet@33192
   164
                               "indices unordered or out of range")
blanchet@33192
   165
  in aux 0 js xs end
blanchet@33192
   166
fun filter_out_indices js xs =
blanchet@33192
   167
  let
blanchet@33192
   168
    fun aux _ [] xs = xs
blanchet@33192
   169
      | aux i (j :: js) (x :: xs) =
blanchet@33192
   170
        if i = j then aux (i + 1) js xs else x :: aux (i + 1) (j :: js) xs
blanchet@33224
   171
      | aux _ _ _ = raise ARG ("Nitpick_Util.filter_out_indices",
blanchet@33192
   172
                               "indices unordered or out of range")
blanchet@33192
   173
  in aux 0 js xs end
blanchet@33192
   174
blanchet@33192
   175
fun cartesian_product [] _ = []
blanchet@33192
   176
  | cartesian_product (x :: xs) yss =
blanchet@33192
   177
    map (cons x) yss @ cartesian_product xs yss
blanchet@33192
   178
fun n_fold_cartesian_product xss = fold_rev cartesian_product xss [[]]
blanchet@33192
   179
fun all_distinct_unordered_pairs_of [] = []
blanchet@33192
   180
  | all_distinct_unordered_pairs_of (x :: xs) =
blanchet@33192
   181
    map (pair x) xs @ all_distinct_unordered_pairs_of xs
blanchet@33192
   182
blanchet@33192
   183
val nth_combination =
blanchet@33192
   184
  let
blanchet@33192
   185
    fun aux [] n = ([], n)
blanchet@33192
   186
      | aux ((k, j0) :: xs) n =
blanchet@33192
   187
        let val (js, n) = aux xs n in ((n mod k) + j0 :: js, n div k) end
blanchet@33192
   188
  in fst oo aux end
blanchet@33192
   189
blanchet@33192
   190
val all_combinations = n_fold_cartesian_product o map (uncurry index_seq o swap)
blanchet@33192
   191
blanchet@33192
   192
fun all_permutations [] = [[]]
blanchet@33192
   193
  | all_permutations xs =
blanchet@33192
   194
    maps (fn j => map (cons (nth xs j)) (all_permutations (nth_drop j xs)))
blanchet@33192
   195
         (index_seq 0 (length xs))
blanchet@33192
   196
blanchet@33192
   197
fun batch_list _ [] = []
blanchet@33192
   198
  | batch_list k xs =
blanchet@33192
   199
    if length xs <= k then [xs]
blanchet@33192
   200
    else List.take (xs, k) :: batch_list k (List.drop (xs, k))
blanchet@33192
   201
blanchet@33192
   202
fun chunk_list_unevenly _ [] = []
blanchet@33192
   203
  | chunk_list_unevenly [] ys = map single ys
blanchet@33192
   204
  | chunk_list_unevenly (k :: ks) ys =
blanchet@33192
   205
    let val (ys1, ys2) = chop k ys in ys1 :: chunk_list_unevenly ks ys2 end
blanchet@33192
   206
blanchet@33192
   207
fun map3 _ [] [] [] = []
blanchet@33192
   208
  | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
wenzelm@40978
   209
  | map3 _ _ _ _ = raise ListPair.UnequalLengths
blanchet@33192
   210
blanchet@33192
   211
fun double_lookup eq ps key =
blanchet@33192
   212
  case AList.lookup (fn (SOME x, SOME y) => eq (x, y) | _ => false) ps
blanchet@33192
   213
                    (SOME key) of
blanchet@33192
   214
    SOME z => SOME z
blanchet@33192
   215
  | NONE => ps |> find_first (is_none o fst) |> Option.map snd
blanchet@35220
   216
fun triple_lookup _ [(NONE, z)] _ = SOME z
blanchet@35220
   217
  | triple_lookup eq ps key =
blanchet@35220
   218
    case AList.lookup (op =) ps (SOME key) of
blanchet@35220
   219
      SOME z => SOME z
blanchet@35220
   220
    | NONE => double_lookup eq ps key
blanchet@33192
   221
blanchet@33192
   222
fun is_substring_of needle stack =
blanchet@33192
   223
  not (Substring.isEmpty (snd (Substring.position needle
blanchet@33192
   224
                                                  (Substring.full stack))))
blanchet@33192
   225
blanchet@36380
   226
val plural_s = Sledgehammer_Util.plural_s
blanchet@33192
   227
fun plural_s_for_list xs = plural_s (length xs)
blanchet@33192
   228
blanchet@43870
   229
val serial_commas = Try.serial_commas
blanchet@36380
   230
blanchet@38415
   231
fun pretty_serial_commas _ [] = []
blanchet@38415
   232
  | pretty_serial_commas _ [p] = [p]
blanchet@38415
   233
  | pretty_serial_commas conj [p1, p2] =
blanchet@38415
   234
    [p1, Pretty.brk 1, Pretty.str conj, Pretty.brk 1, p2]
blanchet@38415
   235
  | pretty_serial_commas conj [p1, p2, p3] =
blanchet@38415
   236
    [p1, Pretty.str ",", Pretty.brk 1, p2, Pretty.str ",", Pretty.brk 1,
blanchet@38415
   237
     Pretty.str conj, Pretty.brk 1, p3]
blanchet@38415
   238
  | pretty_serial_commas conj (p :: ps) =
blanchet@38415
   239
    p :: Pretty.str "," :: Pretty.brk 1 :: pretty_serial_commas conj ps
blanchet@38415
   240
blanchet@36380
   241
val parse_bool_option = Sledgehammer_Util.parse_bool_option
blanchet@36380
   242
val parse_time_option = Sledgehammer_Util.parse_time_option
blanchet@40582
   243
val string_from_time = Sledgehammer_Util.string_from_time
blanchet@36380
   244
wenzelm@40875
   245
val i_subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
blanchet@38890
   246
fun be_subscript s = "\<^bsub>" ^ s ^ "\<^esub>"
blanchet@38890
   247
fun nat_subscript n =
blanchet@38890
   248
  let val s = signed_string_of_int n in
blanchet@38890
   249
    if print_mode_active Symbol.xsymbolsN then
blanchet@38890
   250
      (* cheap trick to ensure proper alphanumeric ordering for one- and
blanchet@38890
   251
         two-digit numbers *)
blanchet@38890
   252
      (if n <= 9 then be_subscript else i_subscript) s
blanchet@38890
   253
    else
blanchet@38890
   254
      s
blanchet@38890
   255
  end
blanchet@38890
   256
blanchet@33192
   257
fun flip_polarity Pos = Neg
blanchet@33192
   258
  | flip_polarity Neg = Pos
blanchet@33192
   259
  | flip_polarity Neut = Neut
blanchet@33192
   260
blanchet@33192
   261
val prop_T = @{typ prop}
blanchet@33192
   262
val bool_T = @{typ bool}
blanchet@33192
   263
val nat_T = @{typ nat}
blanchet@33192
   264
val int_T = @{typ int}
blanchet@33192
   265
blanchet@37259
   266
val simple_string_of_typ = Refute.string_of_typ
blanchet@37259
   267
val is_real_constr = Refute.is_IDT_constructor
blanchet@43550
   268
val typ_of_dtyp = Sledgehammer_Util.typ_of_dtyp
blanchet@43567
   269
val varify_type = Sledgehammer_Util.varify_type
blanchet@43567
   270
val instantiate_type = Sledgehammer_Util.instantiate_type
blanchet@43567
   271
val varify_and_instantiate_type = Sledgehammer_Util.varify_and_instantiate_type
blanchet@37259
   272
val is_of_class_const = Refute.is_const_of_class
blanchet@37259
   273
val get_class_def = Refute.get_classdef
blanchet@36553
   274
val monomorphic_term = Sledgehammer_Util.monomorphic_term
blanchet@36553
   275
val specialize_type = Sledgehammer_Util.specialize_type
blanchet@38890
   276
val eta_expand = Sledgehammer_Util.eta_expand
blanchet@33192
   277
blanchet@34039
   278
fun time_limit NONE = I
blanchet@34039
   279
  | time_limit (SOME delay) = TimeLimit.timeLimit delay
blanchet@33192
   280
blanchet@33192
   281
fun DETERM_TIMEOUT delay tac st =
blanchet@33192
   282
  Seq.of_list (the_list (time_limit delay (fn () => SINGLE tac st) ()))
blanchet@33192
   283
wenzelm@39366
   284
fun set_show_all_types ctxt =
wenzelm@39366
   285
  Config.put show_all_types
wenzelm@39393
   286
    (Config.get ctxt show_types orelse Config.get ctxt show_sorts
wenzelm@39393
   287
      orelse Config.get ctxt show_all_types) ctxt
blanchet@33192
   288
blanchet@33192
   289
val indent_size = 2
blanchet@33192
   290
blanchet@33192
   291
val pstrs = Pretty.breaks o map Pretty.str o space_explode " "
blanchet@33192
   292
blanchet@36479
   293
val unyxml = Sledgehammer_Util.unyxml
blanchet@38415
   294
blanchet@36479
   295
val maybe_quote = Sledgehammer_Util.maybe_quote
blanchet@38415
   296
fun pretty_maybe_quote pretty =
blanchet@38415
   297
  let val s = Pretty.str_of pretty in
blanchet@38415
   298
    if maybe_quote s = s then pretty else Pretty.enum "" "\"" "\"" [pretty]
blanchet@38415
   299
  end
blanchet@33192
   300
blanchet@43438
   301
val hashw = ATP_Problem.hashw
blanchet@43438
   302
val hashw_string = ATP_Problem.hashw_string
blanchet@36380
   303
blanchet@33192
   304
end;