1 (* Title: HOL/Tools/Predicate_Compile/predicate_compile.ML |
1 (* Title: HOL/Tools/Predicate_Compile/predicate_compile.ML |
2 Author: Lukas Bulwahn, TU Muenchen |
2 Author: Lukas Bulwahn, TU Muenchen |
3 |
3 |
4 FIXME. |
4 Entry point for the predicate compiler; definition of Toplevel commands code_pred and values |
5 *) |
5 *) |
6 |
6 |
7 signature PREDICATE_COMPILE = |
7 signature PREDICATE_COMPILE = |
8 sig |
8 sig |
9 val setup : theory -> theory |
9 val setup : theory -> theory |
103 val gr = Graph.subgraph (member (op =) (Graph.all_succs gr [const])) gr |
103 val gr = Graph.subgraph (member (op =) (Graph.all_succs gr [const])) gr |
104 in fold_rev (preprocess_strong_conn_constnames options gr) |
104 in fold_rev (preprocess_strong_conn_constnames options gr) |
105 (Graph.strong_conn gr) thy |
105 (Graph.strong_conn gr) thy |
106 end |
106 end |
107 |
107 |
108 fun extract_options ((modes, raw_options), const) = |
108 fun extract_options (((expected_modes, proposed_modes), raw_options), const) = |
109 let |
109 let |
110 fun chk s = member (op =) raw_options s |
110 fun chk s = member (op =) raw_options s |
111 in |
111 in |
112 Options { |
112 Options { |
113 expected_modes = Option.map ((pair const) o (map fst)) modes, |
113 expected_modes = Option.map (pair const) expected_modes, |
114 user_proposals = |
114 proposed_modes = if not (null proposed_modes) then [(const, map fst proposed_modes)] else [], |
115 the_default [] (Option.map (map_filter |
115 proposed_names = |
116 (fn (m, NONE) => NONE | (m, SOME name) => SOME ((const, m), name))) modes), |
116 map_filter |
|
117 (fn (m, NONE) => NONE | (m, SOME name) => SOME ((const, m), name)) proposed_modes, |
117 show_steps = chk "show_steps", |
118 show_steps = chk "show_steps", |
118 show_intermediate_results = chk "show_intermediate_results", |
119 show_intermediate_results = chk "show_intermediate_results", |
119 show_proof_trace = chk "show_proof_trace", |
120 show_proof_trace = chk "show_proof_trace", |
120 show_modes = chk "show_modes", |
121 show_modes = chk "show_modes", |
121 show_mode_inference = chk "show_mode_inference", |
122 show_mode_inference = chk "show_mode_inference", |
126 depth_limited = chk "depth_limited", |
127 depth_limited = chk "depth_limited", |
127 annotated = chk "annotated" |
128 annotated = chk "annotated" |
128 } |
129 } |
129 end |
130 end |
130 |
131 |
131 fun code_pred_cmd ((modes, raw_options), raw_const) lthy = |
132 fun code_pred_cmd (((expected_modes, proposed_modes), raw_options), raw_const) lthy = |
132 let |
133 let |
133 val thy = ProofContext.theory_of lthy |
134 val thy = ProofContext.theory_of lthy |
134 val const = Code.read_const thy raw_const |
135 val const = Code.read_const thy raw_const |
135 val options = extract_options ((modes, raw_options), const) |
136 val options = extract_options (((expected_modes, proposed_modes), raw_options), const) |
136 in |
137 in |
137 if (is_inductify options) then |
138 if (is_inductify options) then |
138 let |
139 let |
139 val lthy' = LocalTheory.theory (preprocess options const) lthy |
140 val lthy' = LocalTheory.theory (preprocess options const) lthy |
140 |> LocalTheory.checkpoint |
141 |> LocalTheory.checkpoint |
141 val const = case Predicate_Compile_Fun.pred_of_function (ProofContext.theory_of lthy') const of |
142 val const = |
|
143 case Predicate_Compile_Fun.pred_of_function (ProofContext.theory_of lthy') const of |
142 SOME c => c |
144 SOME c => c |
143 | NONE => const |
145 | NONE => const |
144 val _ = print_step options "Starting Predicate Compile Core..." |
146 val _ = print_step options "Starting Predicate Compile Core..." |
145 in |
147 in |
146 Predicate_Compile_Core.code_pred options const lthy' |
148 Predicate_Compile_Core.code_pred options const lthy' |
157 |
159 |
158 local structure P = OuterParse |
160 local structure P = OuterParse |
159 in |
161 in |
160 |
162 |
161 (* Parser for mode annotations *) |
163 (* Parser for mode annotations *) |
162 |
164 (* FIXME: remove old parser *) |
163 (*val parse_argmode' = P.nat >> rpair NONE || P.$$$ "(" |-- P.enum1 "," --| P.$$$ ")"*) |
165 (*val parse_argmode' = P.nat >> rpair NONE || P.$$$ "(" |-- P.enum1 "," --| P.$$$ ")"*) |
164 datatype raw_argmode = Argmode of string | Argmode_Tuple of string list |
166 datatype raw_argmode = Argmode of string | Argmode_Tuple of string list |
165 |
167 |
166 val parse_argmode' = |
168 val parse_argmode' = |
167 ((Args.$$$ "i" || Args.$$$ "o") >> Argmode) || |
169 ((Args.$$$ "i" || Args.$$$ "o") >> Argmode) || |
203 |
205 |
204 val mode_and_opt_proposal = new_parse_mode3 -- |
206 val mode_and_opt_proposal = new_parse_mode3 -- |
205 Scan.optional (Args.$$$ "as" |-- P.xname >> SOME) NONE |
207 Scan.optional (Args.$$$ "as" |-- P.xname >> SOME) NONE |
206 |
208 |
207 val opt_modes = |
209 val opt_modes = |
208 Scan.optional (P.$$$ "(" |-- Args.$$$ "mode" |-- P.$$$ ":" |-- |
210 Scan.optional (P.$$$ "(" |-- Args.$$$ "modes" |-- P.$$$ ":" |-- |
209 P.enum1 "," mode_and_opt_proposal --| P.$$$ ")" >> SOME) NONE |
211 P.enum1 "," mode_and_opt_proposal --| P.$$$ ")") [] |
|
212 |
|
213 val opt_expected_modes = |
|
214 Scan.optional (P.$$$ "(" |-- Args.$$$ "expected_modes" |-- P.$$$ ":" |-- |
|
215 P.enum "," new_parse_mode3 --| P.$$$ ")" >> SOME) NONE |
210 |
216 |
211 (* Parser for options *) |
217 (* Parser for options *) |
212 |
218 |
213 val scan_options = |
219 val scan_options = |
214 let |
220 let |
217 Scan.optional (P.$$$ "[" |-- P.enum1 "," scan_bool_option --| P.$$$ "]") [] |
223 Scan.optional (P.$$$ "[" |-- P.enum1 "," scan_bool_option --| P.$$$ "]") [] |
218 end |
224 end |
219 |
225 |
220 val opt_print_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) []; |
226 val opt_print_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) []; |
221 |
227 |
222 val opt_smode = (P.$$$ "_" >> K NONE) || (parse_smode >> SOME) |
228 val opt_mode = (P.$$$ "_" >> K NONE) || (new_parse_mode3 >> SOME) |
223 |
229 |
224 val opt_param_modes = Scan.optional (P.$$$ "[" |-- Args.$$$ "mode" |-- P.$$$ ":" |-- |
230 val opt_param_modes = Scan.optional (P.$$$ "[" |-- Args.$$$ "mode" |-- P.$$$ ":" |-- |
225 P.enum ", " opt_smode --| P.$$$ "]" >> SOME) NONE |
231 P.enum ", " opt_mode --| P.$$$ "]" >> SOME) NONE |
226 |
232 |
227 val value_options = |
233 val value_options = |
228 let |
234 let |
229 val depth_limit = Scan.optional (Args.$$$ "depth_limit" |-- P.$$$ "=" |-- P.nat >> SOME) NONE |
235 val depth_limit = Scan.optional (Args.$$$ "depth_limit" |-- P.$$$ "=" |-- P.nat >> SOME) NONE |
230 val random = Scan.optional (Args.$$$ "random" >> K true) false |
236 val random = Scan.optional (Args.$$$ "random" >> K true) false |
236 |
242 |
237 (* code_pred command and values command *) |
243 (* code_pred command and values command *) |
238 |
244 |
239 val _ = OuterSyntax.local_theory_to_proof "code_pred" |
245 val _ = OuterSyntax.local_theory_to_proof "code_pred" |
240 "prove equations for predicate specified by intro/elim rules" |
246 "prove equations for predicate specified by intro/elim rules" |
241 OuterKeyword.thy_goal (opt_modes -- scan_options -- P.term_group >> code_pred_cmd) |
247 OuterKeyword.thy_goal |
|
248 (opt_expected_modes -- opt_modes -- scan_options -- P.term_group >> code_pred_cmd) |
242 |
249 |
243 val _ = OuterSyntax.improper_command "values" "enumerate and print comprehensions" OuterKeyword.diag |
250 val _ = OuterSyntax.improper_command "values" "enumerate and print comprehensions" OuterKeyword.diag |
244 (opt_print_modes -- opt_param_modes -- value_options -- Scan.optional P.nat ~1 -- P.term |
251 (opt_print_modes -- opt_param_modes -- value_options -- Scan.optional P.nat ~1 -- P.term |
245 >> (fn ((((print_modes, param_modes), options), k), t) => Toplevel.no_timing o Toplevel.keep |
252 >> (fn ((((print_modes, param_modes), options), k), t) => Toplevel.no_timing o Toplevel.keep |
246 (Predicate_Compile_Core.values_cmd print_modes param_modes options k t))); |
253 (Predicate_Compile_Core.values_cmd print_modes param_modes options k t))); |