src/HOL/Tools/Nitpick/nitpick_util.ML
author blanchet
Thu, 18 Feb 2010 18:48:07 +0100
changeset 35220 2bcdae5f4fdb
parent 34969 7b8c366e34a2
child 35280 54ab4921f826
permissions -rw-r--r--
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
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@33192
    23
  val int_for_bool : bool -> int
blanchet@33705
    24
  val nat_minus : int -> int -> int
blanchet@33192
    25
  val reasonable_power : int -> int -> int
blanchet@33192
    26
  val exact_log : int -> int -> int
blanchet@33192
    27
  val exact_root : int -> int -> int
blanchet@33192
    28
  val offset_list : int list -> int list
blanchet@33192
    29
  val index_seq : int -> int -> int list
blanchet@33192
    30
  val filter_indices : int list -> 'a list -> 'a list
blanchet@33192
    31
  val filter_out_indices : int list -> 'a list -> 'a list
blanchet@33192
    32
  val fold1 : ('a -> 'a -> 'a) -> 'a list -> 'a
blanchet@33192
    33
  val replicate_list : int -> 'a list -> 'a list
blanchet@33192
    34
  val n_fold_cartesian_product : 'a list list -> 'a list list
blanchet@33192
    35
  val all_distinct_unordered_pairs_of : ''a list -> (''a * ''a) list
blanchet@33192
    36
  val nth_combination : (int * int) list -> int -> int list
blanchet@33192
    37
  val all_combinations : (int * int) list -> int list list
blanchet@33192
    38
  val all_permutations : 'a list -> 'a list list
blanchet@33192
    39
  val batch_list : int -> 'a list -> 'a list list
blanchet@33192
    40
  val chunk_list_unevenly : int list -> 'a list -> 'a list list
blanchet@33192
    41
  val map3 : ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
blanchet@33192
    42
  val double_lookup :
blanchet@33192
    43
    ('a * 'a -> bool) -> ('a option * 'b) list -> 'a -> 'b option
blanchet@33192
    44
  val triple_lookup :
blanchet@33192
    45
    (''a * ''a -> bool) -> (''a option * 'b) list -> ''a -> 'b option
blanchet@33192
    46
  val is_substring_of : string -> string -> bool
blanchet@33192
    47
  val serial_commas : string -> string list -> string list
blanchet@33192
    48
  val plural_s : int -> string
blanchet@33192
    49
  val plural_s_for_list : 'a list -> string
blanchet@33192
    50
  val flip_polarity : polarity -> polarity
blanchet@33192
    51
  val prop_T : typ
blanchet@33192
    52
  val bool_T : typ
blanchet@33192
    53
  val nat_T : typ
blanchet@33192
    54
  val int_T : typ
blanchet@33192
    55
  val nat_subscript : int -> string
blanchet@33192
    56
  val time_limit : Time.time option -> ('a -> 'b) -> 'a -> 'b
blanchet@33192
    57
  val DETERM_TIMEOUT : Time.time option -> tactic -> tactic
blanchet@33192
    58
  val setmp_show_all_types : ('a -> 'b) -> 'a -> 'b
blanchet@33192
    59
  val indent_size : int
blanchet@33192
    60
  val pstrs : string -> Pretty.T list
blanchet@34969
    61
  val unyxml : string -> string
blanchet@33192
    62
  val maybe_quote : string -> string
blanchet@33192
    63
end
blanchet@33192
    64
blanchet@33224
    65
structure Nitpick_Util : NITPICK_UTIL =
blanchet@33192
    66
struct
blanchet@33192
    67
blanchet@33192
    68
type styp = string * typ
blanchet@33192
    69
blanchet@33192
    70
datatype polarity = Pos | Neg | Neut
blanchet@33192
    71
blanchet@33192
    72
exception ARG of string * string
blanchet@33192
    73
exception BAD of string * string
blanchet@34121
    74
exception TOO_SMALL of string * string
blanchet@34121
    75
exception TOO_LARGE of string * string
blanchet@33192
    76
exception NOT_SUPPORTED of string
blanchet@33192
    77
exception SAME of unit
blanchet@33192
    78
blanchet@33192
    79
val nitpick_prefix = "Nitpick."
blanchet@33192
    80
blanchet@33192
    81
(* ('a * 'b * 'c -> 'd) -> 'a -> 'b -> 'c -> 'd *)
blanchet@33192
    82
fun curry3 f = fn x => fn y => fn z => f (x, y, z)
blanchet@33192
    83
blanchet@33705
    84
(* ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c *)
blanchet@33705
    85
fun pairf f g x = (f x, g x)
blanchet@33192
    86
blanchet@33192
    87
(* bool -> int *)
blanchet@33192
    88
fun int_for_bool b = if b then 1 else 0
blanchet@33705
    89
(* int -> int -> int *)
blanchet@33705
    90
fun nat_minus i j = if i > j then i - j else 0
blanchet@33192
    91
blanchet@33192
    92
val max_exponent = 16384
blanchet@33192
    93
blanchet@33192
    94
(* int -> int -> int *)
blanchet@33192
    95
fun reasonable_power a 0 = 1
blanchet@33192
    96
  | reasonable_power a 1 = a
blanchet@33192
    97
  | reasonable_power 0 _ = 0
blanchet@33192
    98
  | reasonable_power 1 _ = 1
blanchet@33192
    99
  | reasonable_power a b =
blanchet@34121
   100
    if b < 0 then
blanchet@34121
   101
      raise ARG ("Nitpick_Util.reasonable_power",
blanchet@34121
   102
                 "negative exponent (" ^ signed_string_of_int b ^ ")")
blanchet@34121
   103
    else if b > max_exponent then
blanchet@34121
   104
      raise TOO_LARGE ("Nitpick_Util.reasonable_power",
blanchet@34121
   105
                       "too large exponent (" ^ signed_string_of_int b ^ ")")
blanchet@33192
   106
    else
blanchet@34121
   107
      let val c = reasonable_power a (b div 2) in
blanchet@34121
   108
        c * c * reasonable_power a (b mod 2)
blanchet@34121
   109
      end
blanchet@33192
   110
blanchet@33192
   111
(* int -> int -> int *)
blanchet@33192
   112
fun exact_log m n =
blanchet@33192
   113
  let
blanchet@33192
   114
    val r = Math.ln (Real.fromInt n) / Math.ln (Real.fromInt m) |> Real.round
blanchet@33192
   115
  in
blanchet@33192
   116
    if reasonable_power m r = n then
blanchet@33192
   117
      r
blanchet@33192
   118
    else
blanchet@33224
   119
      raise ARG ("Nitpick_Util.exact_log",
blanchet@33192
   120
                 commas (map signed_string_of_int [m, n]))
blanchet@33192
   121
  end
blanchet@33192
   122
blanchet@33192
   123
(* int -> int -> int *)
blanchet@33192
   124
fun exact_root m n =
blanchet@33192
   125
  let val r = Math.pow (Real.fromInt n, 1.0 / (Real.fromInt m)) |> Real.round in
blanchet@33192
   126
    if reasonable_power r m = n then
blanchet@33192
   127
      r
blanchet@33192
   128
    else
blanchet@33224
   129
      raise ARG ("Nitpick_Util.exact_root",
blanchet@33192
   130
                 commas (map signed_string_of_int [m, n]))
blanchet@33192
   131
  end
blanchet@33192
   132
blanchet@33192
   133
(* ('a -> 'a -> 'a) -> 'a list -> 'a *)
blanchet@33192
   134
fun fold1 f = foldl1 (uncurry f)
blanchet@33192
   135
blanchet@33192
   136
(* int -> 'a list -> 'a list *)
blanchet@33192
   137
fun replicate_list 0 _ = []
blanchet@33192
   138
  | replicate_list n xs = xs @ replicate_list (n - 1) xs
blanchet@33192
   139
blanchet@33192
   140
(* int list -> int list *)
blanchet@33192
   141
fun offset_list ns = rev (tl (fold (fn x => fn xs => (x + hd xs) :: xs) ns [0]))
blanchet@33192
   142
(* int -> int -> int list *)
blanchet@33192
   143
fun index_seq j0 n = if j0 < 0 then j0 downto j0 - n + 1 else j0 upto j0 + n - 1
blanchet@33192
   144
blanchet@33192
   145
(* int list -> 'a list -> 'a list *)
blanchet@33192
   146
fun filter_indices js xs =
blanchet@33192
   147
  let
blanchet@33192
   148
    (* int -> int list -> 'a list -> 'a list *)
blanchet@33192
   149
    fun aux _ [] _ = []
blanchet@33192
   150
      | aux i (j :: js) (x :: xs) =
blanchet@33192
   151
        if i = j then x :: aux (i + 1) js xs else aux (i + 1) (j :: js) xs
blanchet@33224
   152
      | aux _ _ _ = raise ARG ("Nitpick_Util.filter_indices",
blanchet@33192
   153
                               "indices unordered or out of range")
blanchet@33192
   154
  in aux 0 js xs end
blanchet@33192
   155
fun filter_out_indices js xs =
blanchet@33192
   156
  let
blanchet@33192
   157
    (* int -> int list -> 'a list -> 'a list *)
blanchet@33192
   158
    fun aux _ [] xs = xs
blanchet@33192
   159
      | aux i (j :: js) (x :: xs) =
blanchet@33192
   160
        if i = j then aux (i + 1) js xs else x :: aux (i + 1) (j :: js) xs
blanchet@33224
   161
      | aux _ _ _ = raise ARG ("Nitpick_Util.filter_out_indices",
blanchet@33192
   162
                               "indices unordered or out of range")
blanchet@33192
   163
  in aux 0 js xs end
blanchet@33192
   164
blanchet@33192
   165
(* 'a list -> 'a list list -> 'a list list *)
blanchet@33192
   166
fun cartesian_product [] _ = []
blanchet@33192
   167
  | cartesian_product (x :: xs) yss =
blanchet@33192
   168
    map (cons x) yss @ cartesian_product xs yss
blanchet@33192
   169
(* 'a list list -> 'a list list *)
blanchet@33192
   170
fun n_fold_cartesian_product xss = fold_rev cartesian_product xss [[]]
blanchet@33192
   171
(* ''a list -> (''a * ''a) list *)
blanchet@33192
   172
fun all_distinct_unordered_pairs_of [] = []
blanchet@33192
   173
  | all_distinct_unordered_pairs_of (x :: xs) =
blanchet@33192
   174
    map (pair x) xs @ all_distinct_unordered_pairs_of xs
blanchet@33192
   175
blanchet@33192
   176
(* (int * int) list -> int -> int list *)
blanchet@33192
   177
val nth_combination =
blanchet@33192
   178
  let
blanchet@33192
   179
    (* (int * int) list -> int -> int list * int *)
blanchet@33192
   180
    fun aux [] n = ([], n)
blanchet@33192
   181
      | aux ((k, j0) :: xs) n =
blanchet@33192
   182
        let val (js, n) = aux xs n in ((n mod k) + j0 :: js, n div k) end
blanchet@33192
   183
  in fst oo aux end
blanchet@33192
   184
blanchet@33192
   185
(* (int * int) list -> int list list *)
blanchet@33192
   186
val all_combinations = n_fold_cartesian_product o map (uncurry index_seq o swap)
blanchet@33192
   187
blanchet@33192
   188
(* 'a list -> 'a list list *)
blanchet@33192
   189
fun all_permutations [] = [[]]
blanchet@33192
   190
  | all_permutations xs =
blanchet@33192
   191
    maps (fn j => map (cons (nth xs j)) (all_permutations (nth_drop j xs)))
blanchet@33192
   192
         (index_seq 0 (length xs))
blanchet@33192
   193
blanchet@33192
   194
(* int -> 'a list -> 'a list list *)
blanchet@33192
   195
fun batch_list _ [] = []
blanchet@33192
   196
  | batch_list k xs =
blanchet@33192
   197
    if length xs <= k then [xs]
blanchet@33192
   198
    else List.take (xs, k) :: batch_list k (List.drop (xs, k))
blanchet@33192
   199
blanchet@33192
   200
(* int list -> 'a list -> 'a list list *)
blanchet@33192
   201
fun chunk_list_unevenly _ [] = []
blanchet@33192
   202
  | chunk_list_unevenly [] ys = map single ys
blanchet@33192
   203
  | chunk_list_unevenly (k :: ks) ys =
blanchet@33192
   204
    let val (ys1, ys2) = chop k ys in ys1 :: chunk_list_unevenly ks ys2 end
blanchet@33192
   205
blanchet@33192
   206
(* ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list *)
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
blanchet@33192
   209
  | map3 _ _ _ _ = raise UnequalLengths
blanchet@33192
   210
blanchet@33192
   211
(* ('a * 'a -> bool) -> ('a option * 'b) list -> 'a -> 'b option *)
blanchet@33192
   212
fun double_lookup eq ps key =
blanchet@33192
   213
  case AList.lookup (fn (SOME x, SOME y) => eq (x, y) | _ => false) ps
blanchet@33192
   214
                    (SOME key) of
blanchet@33192
   215
    SOME z => SOME z
blanchet@33192
   216
  | NONE => ps |> find_first (is_none o fst) |> Option.map snd
blanchet@33192
   217
(* (''a * ''a -> bool) -> (''a option * 'b) list -> ''a -> 'b option *)
blanchet@35220
   218
fun triple_lookup _ [(NONE, z)] _ = SOME z
blanchet@35220
   219
  | triple_lookup eq ps key =
blanchet@35220
   220
    case AList.lookup (op =) ps (SOME key) of
blanchet@35220
   221
      SOME z => SOME z
blanchet@35220
   222
    | NONE => double_lookup eq ps key
blanchet@33192
   223
blanchet@33192
   224
(* string -> string -> bool *)
blanchet@33192
   225
fun is_substring_of needle stack =
blanchet@33192
   226
  not (Substring.isEmpty (snd (Substring.position needle
blanchet@33192
   227
                                                  (Substring.full stack))))
blanchet@33192
   228
blanchet@33192
   229
(* string -> string list -> string list *)
blanchet@33192
   230
fun serial_commas _ [] = ["??"]
blanchet@33192
   231
  | serial_commas _ [s] = [s]
blanchet@33192
   232
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@33192
   233
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@33192
   234
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@33192
   235
blanchet@33192
   236
(* int -> string *)
blanchet@33192
   237
fun plural_s n = if n = 1 then "" else "s"
blanchet@33192
   238
(* 'a list -> string *)
blanchet@33192
   239
fun plural_s_for_list xs = plural_s (length xs)
blanchet@33192
   240
blanchet@33192
   241
(* polarity -> polarity *)
blanchet@33192
   242
fun flip_polarity Pos = Neg
blanchet@33192
   243
  | flip_polarity Neg = Pos
blanchet@33192
   244
  | flip_polarity Neut = Neut
blanchet@33192
   245
blanchet@33192
   246
val prop_T = @{typ prop}
blanchet@33192
   247
val bool_T = @{typ bool}
blanchet@33192
   248
val nat_T = @{typ nat}
blanchet@33192
   249
val int_T = @{typ int}
blanchet@33192
   250
blanchet@33192
   251
(* string -> string *)
blanchet@33192
   252
val subscript = implode o map (prefix "\<^isub>") o explode
blanchet@33192
   253
(* int -> string *)
blanchet@34121
   254
fun nat_subscript n =
blanchet@34121
   255
  (* cheap trick to ensure proper alphanumeric ordering for one- and two-digit
blanchet@34121
   256
     numbers *)
blanchet@34121
   257
  if n <= 9 then "\<^bsub>" ^ signed_string_of_int n ^ "\<^esub>"
blanchet@34121
   258
  else subscript (string_of_int n)
blanchet@33192
   259
blanchet@33192
   260
(* Time.time option -> ('a -> 'b) -> 'a -> 'b *)
blanchet@34039
   261
fun time_limit NONE = I
blanchet@34039
   262
  | time_limit (SOME delay) = TimeLimit.timeLimit delay
blanchet@33192
   263
blanchet@33192
   264
(* Time.time option -> tactic -> tactic *)
blanchet@33192
   265
fun DETERM_TIMEOUT delay tac st =
blanchet@33192
   266
  Seq.of_list (the_list (time_limit delay (fn () => SINGLE tac st) ()))
blanchet@33192
   267
blanchet@33192
   268
(* ('a -> 'b) -> 'a -> 'b *)
blanchet@33192
   269
fun setmp_show_all_types f =
blanchet@33192
   270
  setmp_CRITICAL show_all_types
blanchet@33192
   271
                 (! show_types orelse ! show_sorts orelse ! show_all_types) f
blanchet@33192
   272
blanchet@33192
   273
val indent_size = 2
blanchet@33192
   274
blanchet@33192
   275
(* string -> Pretty.T list *)
blanchet@33192
   276
val pstrs = Pretty.breaks o map Pretty.str o space_explode " "
blanchet@33192
   277
blanchet@33192
   278
(* XML.tree -> string *)
blanchet@33192
   279
fun plain_string_from_xml_tree t =
blanchet@33192
   280
  Buffer.empty |> XML.add_content t |> Buffer.content
blanchet@33192
   281
(* string -> string *)
blanchet@34969
   282
val unyxml = plain_string_from_xml_tree o YXML.parse
blanchet@33192
   283
blanchet@33192
   284
(* string -> bool *)
blanchet@33192
   285
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
blanchet@33192
   286
(* string -> string *)
blanchet@33192
   287
fun maybe_quote y =
blanchet@34969
   288
  let val s = unyxml y in
blanchet@34923
   289
    y |> (not (is_long_identifier (perhaps (try (unprefix "'")) s)) orelse
blanchet@34923
   290
          OuterKeyword.is_keyword s) ? quote
blanchet@33192
   291
  end
blanchet@33192
   292
blanchet@33192
   293
end;