author | wenzelm |
Sat, 20 Nov 2010 00:53:26 +0100 | |
changeset 40627 | becf5d5187cc |
parent 40341 | 03156257040f |
child 41199 | 4698d12dd860 |
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 |
|
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36062
diff
changeset
|
9 |
val plural_s : int -> string |
35963 | 10 |
val serial_commas : string -> string list -> string list |
38738
0ce517c1970f
make sure that "undo_ascii_of" is the inverse of "ascii_of", also for non-printable characters -- and avoid those in ``-style facts
blanchet
parents:
38698
diff
changeset
|
11 |
val simplify_spaces : string -> string |
35963 | 12 |
val parse_bool_option : bool -> string -> string -> bool option |
13 |
val parse_time_option : string -> string -> Time.time option |
|
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
14 |
val string_from_time : Time.time -> string |
36486
c2d7e2dff59e
support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents:
36478
diff
changeset
|
15 |
val nat_subscript : int -> string |
36478
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
16 |
val unyxml : string -> string |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
17 |
val maybe_quote : string -> string |
36555
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
18 |
val monomorphic_term : Type.tyenv -> term -> term |
38652
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
19 |
val eta_expand : typ list -> term -> int -> term |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
20 |
val transform_elim_term : term -> term |
36555
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
21 |
val specialize_type : theory -> (string * typ) -> term -> term |
38044 | 22 |
val subgoal_count : Proof.state -> int |
37995
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
23 |
val strip_subgoal : thm -> int -> (string * typ) list * term list * term |
38696
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38652
diff
changeset
|
24 |
val reserved_isar_keyword_table : unit -> unit Symtab.table |
35963 | 25 |
end; |
39318 | 26 |
|
35963 | 27 |
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL = |
28 |
struct |
|
29 |
||
36142
f5e15e9aae10
make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents:
36062
diff
changeset
|
30 |
fun plural_s n = if n = 1 then "" else "s" |
36062 | 31 |
|
35963 | 32 |
fun serial_commas _ [] = ["??"] |
33 |
| serial_commas _ [s] = [s] |
|
34 |
| serial_commas conj [s1, s2] = [s1, conj, s2] |
|
35 |
| serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3] |
|
36 |
| serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss |
|
37 |
||
39457 | 38 |
val simplify_spaces = ATP_Proof.strip_spaces (K true) |
37962
d7dbe01f48d7
keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents:
37575
diff
changeset
|
39 |
|
35963 | 40 |
fun parse_bool_option option name s = |
41 |
(case s of |
|
42 |
"smart" => if option then NONE else raise Option |
|
43 |
| "false" => SOME false |
|
44 |
| "true" => SOME true |
|
45 |
| "" => SOME true |
|
46 |
| _ => raise Option) |
|
47 |
handle Option.Option => |
|
48 |
let val ss = map quote ((option ? cons "smart") ["true", "false"]) in |
|
49 |
error ("Parameter " ^ quote name ^ " must be assigned " ^ |
|
50 |
space_implode " " (serial_commas "or" ss) ^ ".") |
|
51 |
end |
|
52 |
||
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
53 |
val has_junk = |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40341
diff
changeset
|
54 |
exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *) |
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
55 |
|
35963 | 56 |
fun parse_time_option _ "none" = NONE |
57 |
| parse_time_option name s = |
|
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
58 |
let val secs = if has_junk s then NONE else Real.fromString s in |
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
59 |
if is_none secs orelse Real.<= (the secs, 0.0) then |
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
60 |
error ("Parameter " ^ quote name ^ " must be assigned a positive \ |
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
61 |
\number of seconds (e.g., \"60\", \"0.5\") or \"none\".") |
35963 | 62 |
else |
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
63 |
SOME (seconds (the secs)) |
35963 | 64 |
end |
65 |
||
40341
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
66 |
fun string_from_time t = |
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
67 |
string_of_real (0.01 * Real.fromInt (Time.toMilliseconds t div 10)) ^ " s" |
03156257040f
standardize on seconds for Nitpick and Sledgehammer timeouts
blanchet
parents:
39890
diff
changeset
|
68 |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40341
diff
changeset
|
69 |
val subscript = implode o map (prefix "\<^isub>") o raw_explode (* FIXME Symbol.explode (?) *) |
37321 | 70 |
fun nat_subscript n = |
71 |
n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript |
|
36486
c2d7e2dff59e
support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents:
36478
diff
changeset
|
72 |
|
39555
ccb223a4d49c
added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents:
39500
diff
changeset
|
73 |
val unyxml = XML.content_of o YXML.parse_body |
36478
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
74 |
|
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
75 |
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
|
76 |
fun maybe_quote y = |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
77 |
let val s = unyxml y in |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
78 |
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
|
79 |
not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36555
diff
changeset
|
80 |
Keyword.is_keyword s) ? quote |
36478
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
81 |
end |
1aba777a367f
fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents:
36402
diff
changeset
|
82 |
|
36555
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
83 |
fun monomorphic_term subst t = |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
84 |
map_types (map_type_tvar (fn v => |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
85 |
case Type.lookup subst v of |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
86 |
SOME typ => typ |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
87 |
| NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \ |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
88 |
\variable", [t]))) t |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
89 |
|
38652
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
90 |
fun eta_expand _ t 0 = t |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
91 |
| eta_expand Ts (Abs (s, T, t')) n = |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
92 |
Abs (s, T, eta_expand (T :: Ts) t' (n - 1)) |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
93 |
| eta_expand Ts t n = |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
94 |
fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t')) |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
95 |
(List.take (binder_types (fastype_of1 (Ts, t)), n)) |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
96 |
(list_comb (incr_boundvars n t, map Bound (n - 1 downto 0))) |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
97 |
|
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
98 |
(* Converts an elim-rule into an equivalent theorem that does not have the |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
99 |
predicate variable. Leaves other theorems unchanged. We simply instantiate |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
100 |
the conclusion variable to False. (Cf. "transform_elim_theorem" in |
39890 | 101 |
"Meson_Clausify".) *) |
38652
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
102 |
fun transform_elim_term t = |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
103 |
case Logic.strip_imp_concl t of |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
104 |
@{const Trueprop} $ Var (z, @{typ bool}) => |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
105 |
subst_Vars [(z, @{const False})] t |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
106 |
| Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
107 |
| _ => t |
e063be321438
perform eta-expansion of quantifier bodies in Sledgehammer translation when needed + transform elim rules later;
blanchet
parents:
38608
diff
changeset
|
108 |
|
36555
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
109 |
fun specialize_type thy (s, T) t = |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
110 |
let |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
111 |
fun subst_for (Const (s', T')) = |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
112 |
if s = s' then |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
113 |
SOME (Sign.typ_match thy (T', T) Vartab.empty) |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
114 |
handle Type.TYPE_MATCH => NONE |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
115 |
else |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
116 |
NONE |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
117 |
| subst_for (t1 $ t2) = |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
118 |
(case subst_for t1 of SOME x => SOME x | NONE => subst_for t2) |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
119 |
| subst_for (Abs (_, _, t')) = subst_for t' |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
120 |
| subst_for _ = NONE |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
121 |
in |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
122 |
case subst_for t of |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
123 |
SOME subst => monomorphic_term subst t |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
124 |
| NONE => raise Type.TYPE_MATCH |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
125 |
end |
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
126 |
|
38044 | 127 |
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal |
128 |
||
37995
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
129 |
fun strip_subgoal goal i = |
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
130 |
let |
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
131 |
val (t, frees) = Logic.goal_params (prop_of goal) i |
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
132 |
val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees) |
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
133 |
val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees |
06f02b15ef8a
generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents:
37962
diff
changeset
|
134 |
in (rev (map dest_Free frees), hyp_ts, concl_t) end |
36555
8ff45c2076da
expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents:
36496
diff
changeset
|
135 |
|
38696
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38652
diff
changeset
|
136 |
fun reserved_isar_keyword_table () = |
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38652
diff
changeset
|
137 |
union (op =) (Keyword.dest_keywords ()) (Keyword.dest_commands ()) |
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38652
diff
changeset
|
138 |
|> map (rpair ()) |> Symtab.make |
4c6b65d6a135
quote facts whose names collide with a keyword or command name (cf. "subclass" in "Jinja/J/TypeSafe.thy")
blanchet
parents:
38652
diff
changeset
|
139 |
|
35963 | 140 |
end; |