author | blanchet |
Tue, 27 Apr 2010 16:00:20 +0200 | |
changeset 36478 | 1aba777a367f |
parent 36402 | 1b20805974c7 |
child 36486 | c2d7e2dff59e |
permissions | -rw-r--r-- |
36062 | 1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_util.ML |
35963 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
||
4 |
General-purpose functions used by the Sledgehammer modules. |
|
5 |
*) |
|
6 |
||
7 |
signature SLEDGEHAMMER_UTIL = |
|
8 |
sig |
|
36378 | 9 |
val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c |
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36062
diff
changeset
|
10 |
val plural_s : int -> string |
35963 | 11 |
val serial_commas : string -> string list -> string list |
36169
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
12 |
val replace_all : string -> string -> string -> string |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
13 |
val remove_all : string -> string -> string |
36170
0cdb76723c88
added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents:
36169
diff
changeset
|
14 |
val timestamp : unit -> string |
35963 | 15 |
val parse_bool_option : bool -> string -> string -> bool option |
16 |
val parse_time_option : string -> string -> Time.time option |
|
36478
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
17 |
val unyxml : string -> string |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
18 |
val maybe_quote : string -> string |
35963 | 19 |
end; |
36170
0cdb76723c88
added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents:
36169
diff
changeset
|
20 |
|
35963 | 21 |
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL = |
22 |
struct |
|
23 |
||
36378 | 24 |
fun pairf f g x = (f x, g x) |
25 |
||
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36062
diff
changeset
|
26 |
fun plural_s n = if n = 1 then "" else "s" |
36062 | 27 |
|
35963 | 28 |
fun serial_commas _ [] = ["??"] |
29 |
| serial_commas _ [s] = [s] |
|
30 |
| serial_commas conj [s1, s2] = [s1, conj, s2] |
|
31 |
| serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3] |
|
32 |
| serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss |
|
33 |
||
36169
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
34 |
fun replace_all bef aft = |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
35 |
let |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
36 |
fun aux seen "" = String.implode (rev seen) |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
37 |
| aux seen s = |
36183
f723348b2231
Sledgehammer: the empty set of fact () should mean nothing, not unchanged
blanchet
parents:
36170
diff
changeset
|
38 |
if String.isPrefix bef s then |
36169
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
39 |
aux seen "" ^ aft ^ aux [] (unprefix bef s) |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
40 |
else |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
41 |
aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE)) |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
42 |
in aux [] end |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
43 |
fun remove_all bef = replace_all bef "" |
27b1cc58715e
store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents:
36142
diff
changeset
|
44 |
|
36170
0cdb76723c88
added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents:
36169
diff
changeset
|
45 |
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now |
0cdb76723c88
added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents:
36169
diff
changeset
|
46 |
|
35963 | 47 |
fun parse_bool_option option name s = |
48 |
(case s of |
|
49 |
"smart" => if option then NONE else raise Option |
|
50 |
| "false" => SOME false |
|
51 |
| "true" => SOME true |
|
52 |
| "" => SOME true |
|
53 |
| _ => raise Option) |
|
54 |
handle Option.Option => |
|
55 |
let val ss = map quote ((option ? cons "smart") ["true", "false"]) in |
|
56 |
error ("Parameter " ^ quote name ^ " must be assigned " ^ |
|
57 |
space_implode " " (serial_commas "or" ss) ^ ".") |
|
58 |
end |
|
59 |
||
60 |
fun parse_time_option _ "none" = NONE |
|
61 |
| parse_time_option name s = |
|
62 |
let |
|
63 |
val msecs = |
|
64 |
case space_explode " " s of |
|
65 |
[s1, "min"] => 60000 * the (Int.fromString s1) |
|
66 |
| [s1, "s"] => 1000 * the (Int.fromString s1) |
|
67 |
| [s1, "ms"] => the (Int.fromString s1) |
|
68 |
| _ => 0 |
|
69 |
in |
|
70 |
if msecs <= 0 then |
|
71 |
error ("Parameter " ^ quote name ^ " must be assigned a positive time \ |
|
72 |
\value (e.g., \"60 s\", \"200 ms\") or \"none\".") |
|
73 |
else |
|
74 |
SOME (Time.fromMilliseconds msecs) |
|
75 |
end |
|
76 |
||
36478
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
77 |
fun plain_string_from_xml_tree t = |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
78 |
Buffer.empty |> XML.add_content t |> Buffer.content |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
79 |
val unyxml = plain_string_from_xml_tree o YXML.parse |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
80 |
|
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
81 |
val is_long_identifier = forall Syntax.is_identifier o space_explode "." |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
82 |
fun maybe_quote y = |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
83 |
let val s = unyxml y in |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
84 |
y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
85 |
not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
86 |
OuterKeyword.is_keyword s) ? quote |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
87 |
end |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
88 |
|
35963 | 89 |
end; |