1 (* Title: HOL/Tools/Nitpick/nitpick_util.ML
2 Author: Jasmin Blanchette, TU Muenchen
3 Copyright 2008, 2009, 2010
5 General-purpose functions used by the Nitpick modules.
8 signature NITPICK_UTIL =
10 type styp = string * typ
11 datatype polarity = Pos | Neg | Neut
13 exception ARG of string * string
14 exception BAD of string * string
15 exception TOO_SMALL of string * string
16 exception TOO_LARGE of string * string
17 exception NOT_SUPPORTED of string
18 exception SAME of unit
20 val nitpick_prefix : string
21 val curry3 : ('a * 'b * 'c -> 'd) -> 'a -> 'b -> 'c -> 'd
22 val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
23 val pair_from_fun : (bool -> 'a) -> 'a * 'a
24 val fun_from_pair : 'a * 'a -> bool -> 'a
25 val int_from_bool : bool -> int
26 val nat_minus : int -> int -> int
27 val reasonable_power : int -> int -> int
28 val exact_log : int -> int -> int
29 val exact_root : int -> int -> int
30 val offset_list : int list -> int list
31 val index_seq : int -> int -> int list
32 val filter_indices : int list -> 'a list -> 'a list
33 val filter_out_indices : int list -> 'a list -> 'a list
34 val fold1 : ('a -> 'a -> 'a) -> 'a list -> 'a
35 val replicate_list : int -> 'a list -> 'a list
36 val n_fold_cartesian_product : 'a list list -> 'a list list
37 val all_distinct_unordered_pairs_of : ''a list -> (''a * ''a) list
38 val nth_combination : (int * int) list -> int -> int list
39 val all_combinations : (int * int) list -> int list list
40 val all_permutations : 'a list -> 'a list list
41 val batch_list : int -> 'a list -> 'a list list
42 val chunk_list_unevenly : int list -> 'a list -> 'a list list
43 val map3 : ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
45 ('a * 'a -> bool) -> ('a option * 'b) list -> 'a -> 'b option
47 (''a * ''a -> bool) -> (''a option * 'b) list -> ''a -> 'b option
48 val is_substring_of : string -> string -> bool
49 val plural_s : int -> string
50 val plural_s_for_list : 'a list -> string
51 val serial_commas : string -> string list -> string list
52 val parse_bool_option : bool -> string -> string -> bool option
53 val parse_time_option : string -> string -> Time.time option
54 val flip_polarity : polarity -> polarity
59 val simple_string_of_typ : typ -> string
60 val is_real_constr : theory -> string * typ -> bool
62 Datatype_Aux.descr -> (Datatype_Aux.dtyp * typ) list -> Datatype_Aux.dtyp
64 val is_of_class_const : theory -> string * typ -> bool
65 val get_class_def : theory -> string -> (string * term) option
66 val monomorphic_term : Type.tyenv -> term -> term
67 val specialize_type : theory -> string * typ -> term -> term
68 val nat_subscript : int -> string
69 val time_limit : Time.time option -> ('a -> 'b) -> 'a -> 'b
70 val DETERM_TIMEOUT : Time.time option -> tactic -> tactic
71 val setmp_show_all_types : ('a -> 'b) -> 'a -> 'b
73 val pstrs : string -> Pretty.T list
74 val unyxml : string -> string
75 val maybe_quote : string -> string
76 val hashw : word * word -> word
77 val hashw_string : string * word -> word
80 structure Nitpick_Util : NITPICK_UTIL =
83 type styp = string * typ
85 datatype polarity = Pos | Neg | Neut
87 exception ARG of string * string
88 exception BAD of string * string
89 exception TOO_SMALL of string * string
90 exception TOO_LARGE of string * string
91 exception NOT_SUPPORTED of string
92 exception SAME of unit
94 val nitpick_prefix = "Nitpick."
96 fun curry3 f = fn x => fn y => fn z => f (x, y, z)
98 fun pairf f g x = (f x, g x)
100 fun pair_from_fun f = (f false, f true)
101 fun fun_from_pair (f, t) b = if b then t else f
103 fun int_from_bool b = if b then 1 else 0
104 fun nat_minus i j = if i > j then i - j else 0
106 val max_exponent = 16384
108 fun reasonable_power _ 0 = 1
109 | reasonable_power a 1 = a
110 | reasonable_power 0 _ = 0
111 | reasonable_power 1 _ = 1
112 | reasonable_power a b =
114 raise ARG ("Nitpick_Util.reasonable_power",
115 "negative exponent (" ^ signed_string_of_int b ^ ")")
116 else if b > max_exponent then
117 raise TOO_LARGE ("Nitpick_Util.reasonable_power",
118 "too large exponent (" ^ signed_string_of_int b ^ ")")
120 let val c = reasonable_power a (b div 2) in
121 c * c * reasonable_power a (b mod 2)
126 val r = Math.ln (Real.fromInt n) / Math.ln (Real.fromInt m) |> Real.round
128 if reasonable_power m r = n then
131 raise ARG ("Nitpick_Util.exact_log",
132 commas (map signed_string_of_int [m, n]))
136 let val r = Math.pow (Real.fromInt n, 1.0 / (Real.fromInt m)) |> Real.round in
137 if reasonable_power r m = n then
140 raise ARG ("Nitpick_Util.exact_root",
141 commas (map signed_string_of_int [m, n]))
144 fun fold1 f = foldl1 (uncurry f)
146 fun replicate_list 0 _ = []
147 | replicate_list n xs = xs @ replicate_list (n - 1) xs
149 fun offset_list ns = rev (tl (fold (fn x => fn xs => (x + hd xs) :: xs) ns [0]))
150 fun index_seq j0 n = if j0 < 0 then j0 downto j0 - n + 1 else j0 upto j0 + n - 1
152 fun filter_indices js xs =
155 | aux i (j :: js) (x :: xs) =
156 if i = j then x :: aux (i + 1) js xs else aux (i + 1) (j :: js) xs
157 | aux _ _ _ = raise ARG ("Nitpick_Util.filter_indices",
158 "indices unordered or out of range")
160 fun filter_out_indices js xs =
163 | aux i (j :: js) (x :: xs) =
164 if i = j then aux (i + 1) js xs else x :: aux (i + 1) (j :: js) xs
165 | aux _ _ _ = raise ARG ("Nitpick_Util.filter_out_indices",
166 "indices unordered or out of range")
169 fun cartesian_product [] _ = []
170 | cartesian_product (x :: xs) yss =
171 map (cons x) yss @ cartesian_product xs yss
172 fun n_fold_cartesian_product xss = fold_rev cartesian_product xss [[]]
173 fun all_distinct_unordered_pairs_of [] = []
174 | all_distinct_unordered_pairs_of (x :: xs) =
175 map (pair x) xs @ all_distinct_unordered_pairs_of xs
177 val nth_combination =
179 fun aux [] n = ([], n)
180 | aux ((k, j0) :: xs) n =
181 let val (js, n) = aux xs n in ((n mod k) + j0 :: js, n div k) end
184 val all_combinations = n_fold_cartesian_product o map (uncurry index_seq o swap)
186 fun all_permutations [] = [[]]
187 | all_permutations xs =
188 maps (fn j => map (cons (nth xs j)) (all_permutations (nth_drop j xs)))
189 (index_seq 0 (length xs))
191 fun batch_list _ [] = []
193 if length xs <= k then [xs]
194 else List.take (xs, k) :: batch_list k (List.drop (xs, k))
196 fun chunk_list_unevenly _ [] = []
197 | chunk_list_unevenly [] ys = map single ys
198 | chunk_list_unevenly (k :: ks) ys =
199 let val (ys1, ys2) = chop k ys in ys1 :: chunk_list_unevenly ks ys2 end
201 fun map3 _ [] [] [] = []
202 | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
203 | map3 _ _ _ _ = raise UnequalLengths
205 fun double_lookup eq ps key =
206 case AList.lookup (fn (SOME x, SOME y) => eq (x, y) | _ => false) ps
209 | NONE => ps |> find_first (is_none o fst) |> Option.map snd
210 fun triple_lookup _ [(NONE, z)] _ = SOME z
211 | triple_lookup eq ps key =
212 case AList.lookup (op =) ps (SOME key) of
214 | NONE => double_lookup eq ps key
216 fun is_substring_of needle stack =
217 not (Substring.isEmpty (snd (Substring.position needle
218 (Substring.full stack))))
220 val plural_s = Sledgehammer_Util.plural_s
221 fun plural_s_for_list xs = plural_s (length xs)
223 val serial_commas = Sledgehammer_Util.serial_commas
225 val parse_bool_option = Sledgehammer_Util.parse_bool_option
226 val parse_time_option = Sledgehammer_Util.parse_time_option
228 fun flip_polarity Pos = Neg
229 | flip_polarity Neg = Pos
230 | flip_polarity Neut = Neut
232 val prop_T = @{typ prop}
233 val bool_T = @{typ bool}
234 val nat_T = @{typ nat}
235 val int_T = @{typ int}
237 val simple_string_of_typ = Refute.string_of_typ
238 val is_real_constr = Refute.is_IDT_constructor
239 val typ_of_dtyp = Refute.typ_of_dtyp
240 val is_of_class_const = Refute.is_const_of_class
241 val get_class_def = Refute.get_classdef
242 val monomorphic_term = Sledgehammer_Util.monomorphic_term
243 val specialize_type = Sledgehammer_Util.specialize_type
245 val i_subscript = implode o map (prefix "\<^isub>") o explode
246 fun be_subscript s = "\<^bsub>" ^ s ^ "\<^esub>"
247 fun nat_subscript n =
248 let val s = signed_string_of_int n in
249 if print_mode_active Symbol.xsymbolsN then
250 (* cheap trick to ensure proper alphanumeric ordering for one- and
252 (if n <= 9 then be_subscript else i_subscript) s
257 fun time_limit NONE = I
258 | time_limit (SOME delay) = TimeLimit.timeLimit delay
260 fun DETERM_TIMEOUT delay tac st =
261 Seq.of_list (the_list (time_limit delay (fn () => SINGLE tac st) ()))
263 fun setmp_show_all_types f =
264 setmp_CRITICAL show_all_types
265 (! show_types orelse ! show_sorts orelse ! show_all_types) f
269 val pstrs = Pretty.breaks o map Pretty.str o space_explode " "
271 val unyxml = Sledgehammer_Util.unyxml
272 val maybe_quote = Sledgehammer_Util.maybe_quote
274 (* This hash function is recommended in Compilers: Principles, Techniques, and
275 Tools, by Aho, Sethi, and Ullman. The "hashpjw" function, which they
276 particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
277 fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
278 fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
279 fun hashw_string (s:string, w) = CharVector.foldl hashw_char w s