author | bulwahn |
Sat, 24 Oct 2009 16:55:42 +0200 | |
changeset 33143 | 730a2e8a6ec6 |
parent 33141 | 89b23fad5e02 |
child 33144 | 1831516747d4 |
permissions | -rw-r--r-- |
32667 | 1 |
(* Author: Lukas Bulwahn, TU Muenchen |
2 |
||
3 |
(Prototype of) A compiler from predicates specified by intro/elim rules |
|
4 |
to equations. |
|
5 |
*) |
|
6 |
||
7 |
signature PREDICATE_COMPILE_CORE = |
|
8 |
sig |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
9 |
val setup: theory -> theory |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
10 |
val code_pred: Predicate_Compile_Aux.options -> string -> Proof.context -> Proof.state |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
11 |
val code_pred_cmd: Predicate_Compile_Aux.options -> string -> Proof.context -> Proof.state |
32667 | 12 |
type smode = (int * int list option) list |
13 |
type mode = smode option list * smode |
|
14 |
datatype tmode = Mode of mode * smode * tmode option list; |
|
15 |
val register_predicate : (thm list * thm * int) -> theory -> theory |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
16 |
val register_intros : thm list -> theory -> theory |
32667 | 17 |
val is_registered : theory -> string -> bool |
18 |
val predfun_intro_of: theory -> string -> mode -> thm |
|
19 |
val predfun_elim_of: theory -> string -> mode -> thm |
|
20 |
val predfun_name_of: theory -> string -> mode -> string |
|
21 |
val all_preds_of : theory -> string list |
|
22 |
val modes_of: theory -> string -> mode list |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
23 |
val depth_limited_modes_of: theory -> string -> mode list |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
24 |
val depth_limited_function_name_of : theory -> string -> mode -> string |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
25 |
val generator_modes_of: theory -> string -> mode list |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
26 |
val generator_name_of : theory -> string -> mode -> string |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
27 |
val all_modes_of : theory -> (string * mode list) list |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
28 |
val all_generator_modes_of : theory -> (string * mode list) list |
32667 | 29 |
val string_of_mode : mode -> string |
30 |
val intros_of: theory -> string -> thm list |
|
31 |
val nparams_of: theory -> string -> int |
|
32 |
val add_intro: thm -> theory -> theory |
|
33 |
val set_elim: thm -> theory -> theory |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
34 |
val set_nparams : string -> int -> theory -> theory |
32667 | 35 |
val print_stored_rules: theory -> unit |
36 |
val print_all_modes: theory -> unit |
|
37 |
val mk_casesrule : Proof.context -> int -> thm list -> term |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
38 |
val eval_ref : (unit -> term Predicate.pred) option Unsynchronized.ref |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
39 |
val random_eval_ref : (unit -> int * int -> term Predicate.pred * (int * int)) option Unsynchronized.ref |
32667 | 40 |
val code_pred_intros_attrib : attribute |
41 |
(* used by Quickcheck_Generator *) |
|
42 |
(* temporary for testing of the compilation *) |
|
43 |
datatype compilation_funs = CompilationFuns of { |
|
44 |
mk_predT : typ -> typ, |
|
45 |
dest_predT : typ -> typ, |
|
46 |
mk_bot : typ -> term, |
|
47 |
mk_single : term -> term, |
|
48 |
mk_bind : term * term -> term, |
|
49 |
mk_sup : term * term -> term, |
|
50 |
mk_if : term -> term, |
|
51 |
mk_not : term -> term, |
|
52 |
mk_map : typ -> typ -> term -> term -> term, |
|
53 |
lift_pred : term -> term |
|
33140
83951822bfd0
cleaning the signature of the predicate compiler core; renaming signature and structures to uniform long names
bulwahn
parents:
33139
diff
changeset
|
54 |
}; |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
55 |
val pred_compfuns : compilation_funs |
32667 | 56 |
val rpred_compfuns : compilation_funs |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
57 |
(* val add_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
58 |
val add_quickcheck_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
59 |
val add_depth_limited_equations : Predicate_Compile_Aux.options -> string list -> theory -> theory |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
60 |
*) |
32667 | 61 |
end; |
62 |
||
63 |
structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE = |
|
64 |
struct |
|
65 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
66 |
open Predicate_Compile_Aux; |
32667 | 67 |
(** auxiliary **) |
68 |
||
69 |
(* debug stuff *) |
|
70 |
||
71 |
fun tracing s = (if ! Toplevel.debug then Output.tracing s else ()); |
|
72 |
||
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
73 |
fun print_tac s = Seq.single; |
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
74 |
|
33127 | 75 |
fun print_tac' options s = |
76 |
if show_proof_trace options then Tactical.print_tac s else Seq.single; |
|
77 |
||
32667 | 78 |
fun debug_tac msg = Seq.single; (* (fn st => (Output.tracing msg; Seq.single st)); *) |
79 |
||
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
80 |
datatype assertion = Max_number_of_subgoals of int |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
81 |
fun assert_tac (Max_number_of_subgoals i) st = |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
82 |
if (nprems_of st <= i) then Seq.single st |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
83 |
else error ("assert_tac: Numbers of subgoals mismatch at goal state :" |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
84 |
^ "\n" ^ Pretty.string_of (Pretty.chunks |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
85 |
(Goal_Display.pretty_goals_without_context (! Goal_Display.goals_limit) st))); |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
86 |
|
32667 | 87 |
(* reference to preprocessing of InductiveSet package *) |
88 |
||
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
89 |
val ind_set_codegen_preproc = (fn thy => I) (*Inductive_Set.codegen_preproc;*) |
32667 | 90 |
|
91 |
(** fundamentals **) |
|
92 |
||
93 |
(* syntactic operations *) |
|
94 |
||
95 |
fun mk_eq (x, xs) = |
|
96 |
let fun mk_eqs _ [] = [] |
|
97 |
| mk_eqs a (b::cs) = |
|
98 |
HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs |
|
99 |
in mk_eqs x xs end; |
|
100 |
||
101 |
fun mk_tupleT [] = HOLogic.unitT |
|
102 |
| mk_tupleT Ts = foldr1 HOLogic.mk_prodT Ts; |
|
103 |
||
104 |
fun dest_tupleT (Type (@{type_name Product_Type.unit}, [])) = [] |
|
105 |
| dest_tupleT (Type (@{type_name "*"}, [T1, T2])) = T1 :: (dest_tupleT T2) |
|
106 |
| dest_tupleT t = [t] |
|
107 |
||
108 |
fun mk_tuple [] = HOLogic.unit |
|
109 |
| mk_tuple ts = foldr1 HOLogic.mk_prod ts; |
|
110 |
||
111 |
fun dest_tuple (Const (@{const_name Product_Type.Unity}, _)) = [] |
|
112 |
| dest_tuple (Const (@{const_name Pair}, _) $ t1 $ t2) = t1 :: (dest_tuple t2) |
|
113 |
| dest_tuple t = [t] |
|
114 |
||
115 |
fun mk_scomp (t, u) = |
|
116 |
let |
|
117 |
val T = fastype_of t |
|
118 |
val U = fastype_of u |
|
119 |
val [A] = binder_types T |
|
120 |
val D = body_type U |
|
121 |
in |
|
122 |
Const (@{const_name "scomp"}, T --> U --> A --> D) $ t $ u |
|
123 |
end; |
|
124 |
||
125 |
fun dest_funT (Type ("fun",[S, T])) = (S, T) |
|
126 |
| dest_funT T = raise TYPE ("dest_funT", [T], []) |
|
127 |
||
128 |
fun mk_fun_comp (t, u) = |
|
129 |
let |
|
130 |
val (_, B) = dest_funT (fastype_of t) |
|
131 |
val (C, A) = dest_funT (fastype_of u) |
|
132 |
in |
|
133 |
Const(@{const_name "Fun.comp"}, (A --> B) --> (C --> A) --> C --> B) $ t $ u |
|
134 |
end; |
|
135 |
||
136 |
fun dest_randomT (Type ("fun", [@{typ Random.seed}, |
|
32674
b629fbcc5313
merged; adopted to changes from Code_Evaluation in the predicate compiler
bulwahn
parents:
32673
diff
changeset
|
137 |
Type ("*", [Type ("*", [T, @{typ "unit => Code_Evaluation.term"}]) ,@{typ Random.seed}])])) = T |
32667 | 138 |
| dest_randomT T = raise TYPE ("dest_randomT", [T], []) |
139 |
||
140 |
(* destruction of intro rules *) |
|
141 |
||
142 |
(* FIXME: look for other place where this functionality was used before *) |
|
143 |
fun strip_intro_concl nparams intro = let |
|
144 |
val _ $ u = Logic.strip_imp_concl intro |
|
145 |
val (pred, all_args) = strip_comb u |
|
146 |
val (params, args) = chop nparams all_args |
|
147 |
in (pred, (params, args)) end |
|
148 |
||
149 |
(** data structures **) |
|
150 |
||
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
151 |
(* new datatype for modes: *) |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
152 |
(* |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
153 |
datatype instantiation = Input | Output |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
154 |
type arg_mode = Tuple of instantiation list | Atom of instantiation | HigherOrderMode of mode |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
155 |
type mode = arg_mode list |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
156 |
type tmode = Mode of mode * |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
157 |
*) |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
158 |
type smode = (int * int list option) list |
32667 | 159 |
type mode = smode option list * smode; |
160 |
datatype tmode = Mode of mode * smode * tmode option list; |
|
161 |
||
162 |
fun gen_split_smode (mk_tuple, strip_tuple) smode ts = |
|
163 |
let |
|
164 |
fun split_tuple' _ _ [] = ([], []) |
|
165 |
| split_tuple' is i (t::ts) = |
|
166 |
(if i mem is then apfst else apsnd) (cons t) |
|
167 |
(split_tuple' is (i+1) ts) |
|
168 |
fun split_tuple is t = split_tuple' is 1 (strip_tuple t) |
|
169 |
fun split_smode' _ _ [] = ([], []) |
|
170 |
| split_smode' smode i (t::ts) = |
|
171 |
(if i mem (map fst smode) then |
|
172 |
case (the (AList.lookup (op =) smode i)) of |
|
173 |
NONE => apfst (cons t) |
|
174 |
| SOME is => |
|
175 |
let |
|
176 |
val (ts1, ts2) = split_tuple is t |
|
177 |
fun cons_tuple ts = if null ts then I else cons (mk_tuple ts) |
|
178 |
in (apfst (cons_tuple ts1)) o (apsnd (cons_tuple ts2)) end |
|
179 |
else apsnd (cons t)) |
|
180 |
(split_smode' smode (i+1) ts) |
|
181 |
in split_smode' smode 1 ts end |
|
182 |
||
183 |
val split_smode = gen_split_smode (HOLogic.mk_tuple, HOLogic.strip_tuple) |
|
184 |
val split_smodeT = gen_split_smode (HOLogic.mk_tupleT, HOLogic.strip_tupleT) |
|
185 |
||
186 |
fun gen_split_mode split_smode (iss, is) ts = |
|
187 |
let |
|
188 |
val (t1, t2) = chop (length iss) ts |
|
189 |
in (t1, split_smode is t2) end |
|
190 |
||
191 |
val split_mode = gen_split_mode split_smode |
|
192 |
val split_modeT = gen_split_mode split_smodeT |
|
193 |
||
194 |
fun string_of_smode js = |
|
195 |
commas (map |
|
196 |
(fn (i, is) => |
|
197 |
string_of_int i ^ (case is of NONE => "" |
|
198 |
| SOME is => "p" ^ enclose "[" "]" (commas (map string_of_int is)))) js) |
|
199 |
||
200 |
fun string_of_mode (iss, is) = space_implode " -> " (map |
|
201 |
(fn NONE => "X" |
|
202 |
| SOME js => enclose "[" "]" (string_of_smode js)) |
|
203 |
(iss @ [SOME is])); |
|
204 |
||
205 |
fun string_of_tmode (Mode (predmode, termmode, param_modes)) = |
|
206 |
"predmode: " ^ (string_of_mode predmode) ^ |
|
207 |
(if null param_modes then "" else |
|
208 |
"; " ^ "params: " ^ commas (map (the_default "NONE" o Option.map string_of_tmode) param_modes)) |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
209 |
|
32667 | 210 |
datatype indprem = Prem of term list * term | Negprem of term list * term | Sidecond of term | |
211 |
GeneratorPrem of term list * term | Generator of (string * typ); |
|
212 |
||
213 |
type moded_clause = term list * (indprem * tmode) list |
|
214 |
type 'a pred_mode_table = (string * (mode * 'a) list) list |
|
215 |
||
216 |
datatype predfun_data = PredfunData of { |
|
217 |
name : string, |
|
218 |
definition : thm, |
|
219 |
intro : thm, |
|
220 |
elim : thm |
|
221 |
}; |
|
222 |
||
223 |
fun rep_predfun_data (PredfunData data) = data; |
|
224 |
fun mk_predfun_data (name, definition, intro, elim) = |
|
225 |
PredfunData {name = name, definition = definition, intro = intro, elim = elim} |
|
226 |
||
227 |
datatype function_data = FunctionData of { |
|
228 |
name : string, |
|
229 |
equation : thm option (* is not used at all? *) |
|
230 |
}; |
|
231 |
||
232 |
fun rep_function_data (FunctionData data) = data; |
|
233 |
fun mk_function_data (name, equation) = |
|
234 |
FunctionData {name = name, equation = equation} |
|
235 |
||
236 |
datatype pred_data = PredData of { |
|
237 |
intros : thm list, |
|
238 |
elim : thm option, |
|
239 |
nparams : int, |
|
240 |
functions : (mode * predfun_data) list, |
|
241 |
generators : (mode * function_data) list, |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
242 |
depth_limited_functions : (mode * function_data) list |
32667 | 243 |
}; |
244 |
||
245 |
fun rep_pred_data (PredData data) = data; |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
246 |
fun mk_pred_data ((intros, elim, nparams), (functions, generators, depth_limited_functions)) = |
32667 | 247 |
PredData {intros = intros, elim = elim, nparams = nparams, |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
248 |
functions = functions, generators = generators, depth_limited_functions = depth_limited_functions} |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
249 |
fun map_pred_data f (PredData {intros, elim, nparams, functions, generators, depth_limited_functions}) = |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
250 |
mk_pred_data (f ((intros, elim, nparams), (functions, generators, depth_limited_functions))) |
32667 | 251 |
|
252 |
fun eq_option eq (NONE, NONE) = true |
|
253 |
| eq_option eq (SOME x, SOME y) = eq (x, y) |
|
254 |
| eq_option eq _ = false |
|
255 |
||
256 |
fun eq_pred_data (PredData d1, PredData d2) = |
|
257 |
eq_list (Thm.eq_thm) (#intros d1, #intros d2) andalso |
|
258 |
eq_option (Thm.eq_thm) (#elim d1, #elim d2) andalso |
|
259 |
#nparams d1 = #nparams d2 |
|
260 |
||
261 |
structure PredData = TheoryDataFun |
|
262 |
( |
|
263 |
type T = pred_data Graph.T; |
|
264 |
val empty = Graph.empty; |
|
265 |
val copy = I; |
|
266 |
val extend = I; |
|
267 |
fun merge _ = Graph.merge eq_pred_data; |
|
268 |
); |
|
269 |
||
270 |
(* queries *) |
|
271 |
||
272 |
fun lookup_pred_data thy name = |
|
273 |
Option.map rep_pred_data (try (Graph.get_node (PredData.get thy)) name) |
|
274 |
||
275 |
fun the_pred_data thy name = case lookup_pred_data thy name |
|
276 |
of NONE => error ("No such predicate " ^ quote name) |
|
277 |
| SOME data => data; |
|
278 |
||
279 |
val is_registered = is_some oo lookup_pred_data |
|
280 |
||
281 |
val all_preds_of = Graph.keys o PredData.get |
|
282 |
||
283 |
fun intros_of thy = map (Thm.transfer thy) o #intros o the_pred_data thy |
|
284 |
||
285 |
fun the_elim_of thy name = case #elim (the_pred_data thy name) |
|
286 |
of NONE => error ("No elimination rule for predicate " ^ quote name) |
|
287 |
| SOME thm => Thm.transfer thy thm |
|
288 |
||
289 |
val has_elim = is_some o #elim oo the_pred_data; |
|
290 |
||
291 |
val nparams_of = #nparams oo the_pred_data |
|
292 |
||
293 |
val modes_of = (map fst) o #functions oo the_pred_data |
|
294 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
295 |
val depth_limited_modes_of = (map fst) o #depth_limited_functions oo the_pred_data |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
296 |
|
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
297 |
val rpred_modes_of = (map fst) o #generators oo the_pred_data |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
298 |
|
32667 | 299 |
fun all_modes_of thy = map (fn name => (name, modes_of thy name)) (all_preds_of thy) |
300 |
||
301 |
val is_compiled = not o null o #functions oo the_pred_data |
|
302 |
||
303 |
fun lookup_predfun_data thy name mode = |
|
304 |
Option.map rep_predfun_data (AList.lookup (op =) |
|
305 |
(#functions (the_pred_data thy name)) mode) |
|
306 |
||
307 |
fun the_predfun_data thy name mode = case lookup_predfun_data thy name mode |
|
308 |
of NONE => error ("No function defined for mode " ^ string_of_mode mode ^ " of predicate " ^ name) |
|
309 |
| SOME data => data; |
|
310 |
||
311 |
val predfun_name_of = #name ooo the_predfun_data |
|
312 |
||
313 |
val predfun_definition_of = #definition ooo the_predfun_data |
|
314 |
||
315 |
val predfun_intro_of = #intro ooo the_predfun_data |
|
316 |
||
317 |
val predfun_elim_of = #elim ooo the_predfun_data |
|
318 |
||
319 |
fun lookup_generator_data thy name mode = |
|
320 |
Option.map rep_function_data (AList.lookup (op =) |
|
321 |
(#generators (the_pred_data thy name)) mode) |
|
322 |
||
323 |
fun the_generator_data thy name mode = case lookup_generator_data thy name mode |
|
324 |
of NONE => error ("No generator defined for mode " ^ string_of_mode mode ^ " of predicate " ^ name) |
|
325 |
| SOME data => data |
|
326 |
||
327 |
val generator_name_of = #name ooo the_generator_data |
|
328 |
||
329 |
val generator_modes_of = (map fst) o #generators oo the_pred_data |
|
330 |
||
331 |
fun all_generator_modes_of thy = |
|
332 |
map (fn name => (name, generator_modes_of thy name)) (all_preds_of thy) |
|
333 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
334 |
fun lookup_depth_limited_function_data thy name mode = |
32667 | 335 |
Option.map rep_function_data (AList.lookup (op =) |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
336 |
(#depth_limited_functions (the_pred_data thy name)) mode) |
32667 | 337 |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
338 |
fun the_depth_limited_function_data thy name mode = case lookup_depth_limited_function_data thy name mode |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
339 |
of NONE => error ("No depth-limited function defined for mode " ^ string_of_mode mode |
32667 | 340 |
^ " of predicate " ^ name) |
341 |
| SOME data => data |
|
342 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
343 |
val depth_limited_function_name_of = #name ooo the_depth_limited_function_data |
32667 | 344 |
|
345 |
(*val generator_modes_of = (map fst) o #generators oo the_pred_data*) |
|
346 |
||
347 |
(* diagnostic display functions *) |
|
348 |
||
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
349 |
fun print_modes modes = |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
350 |
Output.tracing ("Inferred modes:\n" ^ |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
351 |
cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
352 |
string_of_mode ms)) modes)); |
32667 | 353 |
|
354 |
fun print_pred_mode_table string_of_entry thy pred_mode_table = |
|
355 |
let |
|
356 |
fun print_mode pred (mode, entry) = "mode : " ^ (string_of_mode mode) |
|
357 |
^ (string_of_entry pred mode entry) |
|
358 |
fun print_pred (pred, modes) = |
|
359 |
"predicate " ^ pred ^ ": " ^ cat_lines (map (print_mode pred) modes) |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
360 |
val _ = Output.tracing (cat_lines (map print_pred pred_mode_table)) |
32667 | 361 |
in () end; |
362 |
||
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
363 |
fun string_of_prem thy (Prem (ts, p)) = |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
364 |
(Syntax.string_of_term_global thy (list_comb (p, ts))) ^ "(premise)" |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
365 |
| string_of_prem thy (Negprem (ts, p)) = |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
366 |
(Syntax.string_of_term_global thy (HOLogic.mk_not (list_comb (p, ts)))) ^ "(negative premise)" |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
367 |
| string_of_prem thy (Sidecond t) = |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
368 |
(Syntax.string_of_term_global thy t) ^ "(sidecondition)" |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
369 |
| string_of_prem thy _ = error "string_of_prem: unexpected input" |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
370 |
|
32667 | 371 |
fun string_of_moded_prem thy (Prem (ts, p), tmode) = |
372 |
(Syntax.string_of_term_global thy (list_comb (p, ts))) ^ |
|
373 |
"(" ^ (string_of_tmode tmode) ^ ")" |
|
374 |
| string_of_moded_prem thy (GeneratorPrem (ts, p), Mode (predmode, is, _)) = |
|
375 |
(Syntax.string_of_term_global thy (list_comb (p, ts))) ^ |
|
376 |
"(generator_mode: " ^ (string_of_mode predmode) ^ ")" |
|
377 |
| string_of_moded_prem thy (Generator (v, T), _) = |
|
378 |
"Generator for " ^ v ^ " of Type " ^ (Syntax.string_of_typ_global thy T) |
|
379 |
| string_of_moded_prem thy (Negprem (ts, p), Mode (_, is, _)) = |
|
380 |
(Syntax.string_of_term_global thy (list_comb (p, ts))) ^ |
|
381 |
"(negative mode: " ^ string_of_smode is ^ ")" |
|
382 |
| string_of_moded_prem thy (Sidecond t, Mode (_, is, _)) = |
|
383 |
(Syntax.string_of_term_global thy t) ^ |
|
384 |
"(sidecond mode: " ^ string_of_smode is ^ ")" |
|
385 |
| string_of_moded_prem _ _ = error "string_of_moded_prem: unimplemented" |
|
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
386 |
|
32667 | 387 |
fun print_moded_clauses thy = |
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
388 |
let |
32667 | 389 |
fun string_of_clause pred mode clauses = |
390 |
cat_lines (map (fn (ts, prems) => (space_implode " --> " |
|
391 |
(map (string_of_moded_prem thy) prems)) ^ " --> " ^ pred ^ " " |
|
392 |
^ (space_implode " " (map (Syntax.string_of_term_global thy) ts))) clauses) |
|
393 |
in print_pred_mode_table string_of_clause thy end; |
|
394 |
||
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
395 |
fun string_of_clause thy pred (ts, prems) = |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
396 |
(space_implode " --> " |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
397 |
(map (string_of_prem thy) prems)) ^ " --> " ^ pred ^ " " |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
398 |
^ (space_implode " " (map (Syntax.string_of_term_global thy) ts)) |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
399 |
|
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
400 |
fun print_compiled_terms options thy = |
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
401 |
if show_compilation options then |
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
402 |
print_pred_mode_table (fn _ => fn _ => Syntax.string_of_term_global thy) thy |
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
403 |
else K () |
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
404 |
|
32667 | 405 |
fun print_stored_rules thy = |
406 |
let |
|
407 |
val preds = (Graph.keys o PredData.get) thy |
|
408 |
fun print pred () = let |
|
409 |
val _ = writeln ("predicate: " ^ pred) |
|
410 |
val _ = writeln ("number of parameters: " ^ string_of_int (nparams_of thy pred)) |
|
411 |
val _ = writeln ("introrules: ") |
|
412 |
val _ = fold (fn thm => fn u => writeln (Display.string_of_thm_global thy thm)) |
|
413 |
(rev (intros_of thy pred)) () |
|
414 |
in |
|
415 |
if (has_elim thy pred) then |
|
416 |
writeln ("elimrule: " ^ Display.string_of_thm_global thy (the_elim_of thy pred)) |
|
417 |
else |
|
418 |
writeln ("no elimrule defined") |
|
419 |
end |
|
420 |
in |
|
421 |
fold print preds () |
|
422 |
end; |
|
423 |
||
424 |
fun print_all_modes thy = |
|
425 |
let |
|
426 |
val _ = writeln ("Inferred modes:") |
|
427 |
fun print (pred, modes) u = |
|
428 |
let |
|
429 |
val _ = writeln ("predicate: " ^ pred) |
|
430 |
val _ = writeln ("modes: " ^ (commas (map string_of_mode modes))) |
|
431 |
in u end |
|
432 |
in |
|
433 |
fold print (all_modes_of thy) () |
|
434 |
end |
|
33129
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
435 |
|
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
436 |
(* validity checks *) |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
437 |
|
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
438 |
fun check_expected_modes (options : Predicate_Compile_Aux.options) modes = |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
439 |
case expected_modes options of |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
440 |
SOME (s, ms) => (case AList.lookup (op =) modes s of |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
441 |
SOME modes => |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
442 |
if not (eq_set (map (map (rpair NONE)) ms, map snd modes)) then |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
443 |
error ("expected modes were not inferred:" |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
444 |
^ "infered modes for " ^ s ^ ": " ^ commas (map (string_of_smode o snd) modes)) |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
445 |
else () |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
446 |
| NONE => ()) |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
447 |
| NONE => () |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
448 |
|
33129
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
449 |
(* importing introduction rules *) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
450 |
|
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
451 |
fun unify_consts thy cs intr_ts = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
452 |
(let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
453 |
val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I); |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
454 |
fun varify (t, (i, ts)) = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
455 |
let val t' = map_types (Logic.incr_tvar (i + 1)) (#2 (Type.varify [] t)) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
456 |
in (maxidx_of_term t', t'::ts) end; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
457 |
val (i, cs') = foldr varify (~1, []) cs; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
458 |
val (i', intr_ts') = foldr varify (i, []) intr_ts; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
459 |
val rec_consts = fold add_term_consts_2 cs' []; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
460 |
val intr_consts = fold add_term_consts_2 intr_ts' []; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
461 |
fun unify (cname, cT) = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
462 |
let val consts = map snd (List.filter (fn c => fst c = cname) intr_consts) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
463 |
in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
464 |
val (env, _) = fold unify rec_consts (Vartab.empty, i'); |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
465 |
val subst = map_types (Envir.norm_type env) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
466 |
in (map subst cs', map subst intr_ts') |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
467 |
end) handle Type.TUNIFY => |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
468 |
(warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts)); |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
469 |
|
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
470 |
fun import_intros _ [] ctxt = ([], ctxt) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
471 |
| import_intros nparams (th :: ths) ctxt = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
472 |
let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
473 |
val ((_, [th']), ctxt') = Variable.import false [th] ctxt |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
474 |
val thy = ProofContext.theory_of ctxt' |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
475 |
val (pred, (params, args)) = strip_intro_concl nparams (prop_of th') |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
476 |
val ho_args = filter (is_predT o fastype_of) args |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
477 |
fun instantiate_typ th = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
478 |
let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
479 |
val (pred', _) = strip_intro_concl 0 (prop_of th) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
480 |
val _ = if not (fst (dest_Const pred) = fst (dest_Const pred')) then |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
481 |
error "Trying to instantiate another predicate" else () |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
482 |
val subst = Sign.typ_match thy |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
483 |
(fastype_of pred', fastype_of pred) Vartab.empty |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
484 |
val subst' = map (fn (indexname, (s, T)) => ((indexname, s), T)) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
485 |
(Vartab.dest subst) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
486 |
in Thm.certify_instantiate (subst', []) th end; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
487 |
fun instantiate_ho_args th = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
488 |
let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
489 |
val (_, (params', args')) = strip_intro_concl nparams (prop_of th) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
490 |
val ho_args' = map dest_Var (filter (is_predT o fastype_of) args') |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
491 |
in Thm.certify_instantiate ([], map dest_Var params' ~~ params) th end |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
492 |
val ((_, ths'), ctxt1) = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
493 |
Variable.import false (map (instantiate_typ #> instantiate_ho_args) ths) ctxt' |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
494 |
in |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
495 |
(th' :: ths', ctxt1) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
496 |
end |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
497 |
|
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
498 |
(* generation of case rules from user-given introduction rules *) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
499 |
|
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
500 |
fun mk_casesrule ctxt nparams introrules = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
501 |
let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
502 |
val (intros_th, ctxt1) = import_intros nparams introrules ctxt |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
503 |
val intros = map prop_of intros_th |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
504 |
val (pred, (params, args)) = strip_intro_concl nparams (hd intros) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
505 |
val ([propname], ctxt2) = Variable.variant_fixes ["thesis"] ctxt1 |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
506 |
val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT)) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
507 |
val (argnames, ctxt3) = Variable.variant_fixes |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
508 |
(map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt2 |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
509 |
val argvs = map2 (curry Free) argnames (map fastype_of args) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
510 |
fun mk_case intro = |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
511 |
let |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
512 |
val (_, (_, args)) = strip_intro_concl nparams intro |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
513 |
val prems = Logic.strip_imp_prems intro |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
514 |
val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
515 |
val frees = (fold o fold_aterms) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
516 |
(fn t as Free _ => |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
517 |
if member (op aconv) params t then I else insert (op aconv) t |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
518 |
| _ => I) (args @ prems) [] |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
519 |
in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
520 |
val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs)) |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
521 |
val cases = map mk_case intros |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
522 |
in Logic.list_implies (assm :: cases, prop) end; |
3085da75ed54
changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents:
33128
diff
changeset
|
523 |
|
32667 | 524 |
(** preprocessing rules **) |
525 |
||
526 |
fun imp_prems_conv cv ct = |
|
527 |
case Thm.term_of ct of |
|
528 |
Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct |
|
529 |
| _ => Conv.all_conv ct |
|
530 |
||
531 |
fun Trueprop_conv cv ct = |
|
532 |
case Thm.term_of ct of |
|
533 |
Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct |
|
534 |
| _ => error "Trueprop_conv" |
|
535 |
||
536 |
fun preprocess_intro thy rule = |
|
537 |
Conv.fconv_rule |
|
538 |
(imp_prems_conv |
|
539 |
(Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq}))))) |
|
540 |
(Thm.transfer thy rule) |
|
541 |
||
542 |
fun preprocess_elim thy nparams elimrule = |
|
543 |
let |
|
544 |
fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) = |
|
545 |
HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs) |
|
546 |
| replace_eqs t = t |
|
33128 | 547 |
val ctxt = ProofContext.init thy |
548 |
val ((_, [elimrule]), ctxt') = Variable.import false [elimrule] ctxt |
|
549 |
val prems = Thm.prems_of elimrule |
|
32667 | 550 |
val nargs = length (snd (strip_comb (HOLogic.dest_Trueprop (hd prems)))) - nparams |
551 |
fun preprocess_case t = |
|
33128 | 552 |
let |
32667 | 553 |
val params = Logic.strip_params t |
554 |
val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t) |
|
555 |
val assums_hyp' = assums1 @ (map replace_eqs assums2) |
|
33128 | 556 |
in |
32667 | 557 |
list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t)) |
33128 | 558 |
end |
32667 | 559 |
val cases' = map preprocess_case (tl prems) |
560 |
val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule) |
|
561 |
val bigeq = (Thm.symmetric (Conv.implies_concl_conv |
|
562 |
(MetaSimplifier.rewrite true [@{thm Predicate.eq_is_eq}]) |
|
563 |
(cterm_of thy elimrule'))) |
|
33113 | 564 |
val tac = (fn _ => setmp quick_and_dirty true (SkipProof.cheat_tac thy)) |
33109
7025bc7a5054
changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents:
33108
diff
changeset
|
565 |
val eq = Goal.prove ctxt' [] [] (Logic.mk_equals ((Thm.prop_of elimrule), elimrule')) tac |
32667 | 566 |
in |
33109
7025bc7a5054
changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents:
33108
diff
changeset
|
567 |
Thm.equal_elim eq elimrule |> singleton (Variable.export ctxt' ctxt) |
32667 | 568 |
end; |
569 |
||
570 |
(* special case: predicate with no introduction rule *) |
|
571 |
fun noclause thy predname elim = let |
|
572 |
val T = (Logic.unvarifyT o Sign.the_const_type thy) predname |
|
573 |
val Ts = binder_types T |
|
574 |
val names = Name.variant_list [] |
|
575 |
(map (fn i => "x" ^ (string_of_int i)) (1 upto (length Ts))) |
|
576 |
val vs = map2 (curry Free) names Ts |
|
577 |
val clausehd = HOLogic.mk_Trueprop (list_comb (Const (predname, T), vs)) |
|
578 |
val intro_t = Logic.mk_implies (@{prop False}, clausehd) |
|
579 |
val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)) |
|
580 |
val elim_t = Logic.list_implies ([clausehd, Logic.mk_implies (@{prop False}, P)], P) |
|
581 |
val intro = Goal.prove (ProofContext.init thy) names [] intro_t |
|
582 |
(fn {...} => etac @{thm FalseE} 1) |
|
583 |
val elim = Goal.prove (ProofContext.init thy) ("P" :: names) [] elim_t |
|
584 |
(fn {...} => etac elim 1) |
|
585 |
in |
|
586 |
([intro], elim) |
|
587 |
end |
|
588 |
||
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
589 |
fun expand_tuples_elim th = th |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
590 |
|
32667 | 591 |
fun fetch_pred_data thy name = |
592 |
case try (Inductive.the_inductive (ProofContext.init thy)) name of |
|
593 |
SOME (info as (_, result)) => |
|
594 |
let |
|
595 |
fun is_intro_of intro = |
|
596 |
let |
|
597 |
val (const, _) = strip_comb (HOLogic.dest_Trueprop (concl_of intro)) |
|
598 |
in (fst (dest_Const const) = name) end; |
|
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
599 |
val intros = ind_set_codegen_preproc thy |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
600 |
(map (expand_tuples thy #> preprocess_intro thy) (filter is_intro_of (#intrs result))) |
32667 | 601 |
val pre_elim = nth (#elims result) (find_index (fn s => s = name) (#names (fst info))) |
602 |
val nparams = length (Inductive.params_of (#raw_induct result)) |
|
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
603 |
(*val elim = singleton (ind_set_codegen_preproc thy) (preprocess_elim thy nparams |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
604 |
(expand_tuples_elim pre_elim))*) |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
605 |
val elim = |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
606 |
(Drule.standard o (setmp quick_and_dirty true (SkipProof.make_thm thy))) |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
607 |
(mk_casesrule (ProofContext.init thy) nparams intros) |
32667 | 608 |
val (intros, elim) = if null intros then noclause thy name elim else (intros, elim) |
609 |
in |
|
610 |
mk_pred_data ((intros, SOME elim, nparams), ([], [], [])) |
|
611 |
end |
|
612 |
| NONE => error ("No such predicate: " ^ quote name) |
|
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
613 |
|
32667 | 614 |
(* updaters *) |
615 |
||
616 |
fun apfst3 f (x, y, z) = (f x, y, z) |
|
617 |
fun apsnd3 f (x, y, z) = (x, f y, z) |
|
618 |
fun aptrd3 f (x, y, z) = (x, y, f z) |
|
619 |
||
620 |
fun add_predfun name mode data = |
|
621 |
let |
|
622 |
val add = (apsnd o apfst3 o cons) (mode, mk_predfun_data data) |
|
623 |
in PredData.map (Graph.map_node name (map_pred_data add)) end |
|
624 |
||
625 |
fun is_inductive_predicate thy name = |
|
626 |
is_some (try (Inductive.the_inductive (ProofContext.init thy)) name) |
|
627 |
||
628 |
fun depending_preds_of thy (key, value) = |
|
629 |
let |
|
630 |
val intros = (#intros o rep_pred_data) value |
|
631 |
in |
|
632 |
fold Term.add_const_names (map Thm.prop_of intros) [] |
|
633 |
|> filter (fn c => (not (c = key)) andalso (is_inductive_predicate thy c orelse is_registered thy c)) |
|
634 |
end; |
|
635 |
||
636 |
||
637 |
(* code dependency graph *) |
|
638 |
(* |
|
639 |
fun dependencies_of thy name = |
|
640 |
let |
|
641 |
val (intros, elim, nparams) = fetch_pred_data thy name |
|
642 |
val data = mk_pred_data ((intros, SOME elim, nparams), ([], [], [])) |
|
643 |
val keys = depending_preds_of thy intros |
|
644 |
in |
|
645 |
(data, keys) |
|
646 |
end; |
|
647 |
*) |
|
648 |
(* guessing number of parameters *) |
|
649 |
fun find_indexes pred xs = |
|
650 |
let |
|
651 |
fun find is n [] = is |
|
652 |
| find is n (x :: xs) = find (if pred x then (n :: is) else is) (n + 1) xs; |
|
653 |
in rev (find [] 0 xs) end; |
|
654 |
||
655 |
fun guess_nparams T = |
|
656 |
let |
|
657 |
val argTs = binder_types T |
|
658 |
val nparams = fold (curry Int.max) |
|
659 |
(map (fn x => x + 1) (find_indexes is_predT argTs)) 0 |
|
660 |
in nparams end; |
|
661 |
||
662 |
fun add_intro thm thy = let |
|
663 |
val (name, T) = dest_Const (fst (strip_intro_concl 0 (prop_of thm))) |
|
664 |
fun cons_intro gr = |
|
665 |
case try (Graph.get_node gr) name of |
|
666 |
SOME pred_data => Graph.map_node name (map_pred_data |
|
33116
b379ee2cddb1
changed importing introduction rules to fix the same type variables in all introduction rules; improved mode analysis for partially applied relations; added test case; tuned
bulwahn
parents:
33115
diff
changeset
|
667 |
(apfst (fn (intros, elim, nparams) => (thm::intros, elim, nparams)))) gr |
32667 | 668 |
| NONE => |
669 |
let |
|
670 |
val nparams = the_default (guess_nparams T) (try (#nparams o rep_pred_data o (fetch_pred_data thy)) name) |
|
671 |
in Graph.new_node (name, mk_pred_data (([thm], NONE, nparams), ([], [], []))) gr end; |
|
672 |
in PredData.map cons_intro thy end |
|
673 |
||
674 |
fun set_elim thm = let |
|
675 |
val (name, _) = dest_Const (fst |
|
676 |
(strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm))))) |
|
677 |
fun set (intros, _, nparams) = (intros, SOME thm, nparams) |
|
678 |
in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end |
|
679 |
||
680 |
fun set_nparams name nparams = let |
|
681 |
fun set (intros, elim, _ ) = (intros, elim, nparams) |
|
682 |
in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end |
|
683 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
684 |
fun register_predicate (pre_intros, pre_elim, nparams) thy = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
685 |
let |
32667 | 686 |
val (name, _) = dest_Const (fst (strip_intro_concl nparams (prop_of (hd pre_intros)))) |
687 |
(* preprocessing *) |
|
688 |
val intros = ind_set_codegen_preproc thy (map (preprocess_intro thy) pre_intros) |
|
689 |
val elim = singleton (ind_set_codegen_preproc thy) (preprocess_elim thy nparams pre_elim) |
|
690 |
in |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
691 |
if not (member (op =) (Graph.keys (PredData.get thy)) name) then |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
692 |
PredData.map |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
693 |
(Graph.new_node (name, mk_pred_data ((intros, SOME elim, nparams), ([], [], [])))) thy |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
694 |
else thy |
32667 | 695 |
end |
696 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
697 |
fun register_intros pre_intros thy = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
698 |
let |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
699 |
val (c, T) = dest_Const (fst (strip_intro_concl 0 (prop_of (hd pre_intros)))) |
33120 | 700 |
fun constname_of_intro intr = fst (dest_Const (fst (strip_intro_concl 0 (prop_of intr)))) |
701 |
val _ = if not (forall (fn intr => constname_of_intro intr = c) pre_intros) then |
|
702 |
error "register_intros: Introduction rules of different constants are used" else () |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
703 |
val nparams = guess_nparams T |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
704 |
val pre_elim = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
705 |
(Drule.standard o (setmp quick_and_dirty true (SkipProof.make_thm thy))) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
706 |
(mk_casesrule (ProofContext.init thy) nparams pre_intros) |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
707 |
in register_predicate (pre_intros, pre_elim, nparams) thy end |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
708 |
|
32667 | 709 |
fun set_generator_name pred mode name = |
710 |
let |
|
711 |
val set = (apsnd o apsnd3 o cons) (mode, mk_function_data (name, NONE)) |
|
712 |
in |
|
713 |
PredData.map (Graph.map_node pred (map_pred_data set)) |
|
714 |
end |
|
715 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
716 |
fun set_depth_limited_function_name pred mode name = |
32667 | 717 |
let |
718 |
val set = (apsnd o aptrd3 o cons) (mode, mk_function_data (name, NONE)) |
|
719 |
in |
|
720 |
PredData.map (Graph.map_node pred (map_pred_data set)) |
|
721 |
end |
|
722 |
||
723 |
(** data structures for generic compilation for different monads **) |
|
724 |
||
725 |
(* maybe rename functions more generic: |
|
726 |
mk_predT -> mk_monadT; dest_predT -> dest_monadT |
|
727 |
mk_single -> mk_return (?) |
|
728 |
*) |
|
729 |
datatype compilation_funs = CompilationFuns of { |
|
730 |
mk_predT : typ -> typ, |
|
731 |
dest_predT : typ -> typ, |
|
732 |
mk_bot : typ -> term, |
|
733 |
mk_single : term -> term, |
|
734 |
mk_bind : term * term -> term, |
|
735 |
mk_sup : term * term -> term, |
|
736 |
mk_if : term -> term, |
|
737 |
mk_not : term -> term, |
|
738 |
mk_map : typ -> typ -> term -> term -> term, |
|
739 |
lift_pred : term -> term |
|
740 |
}; |
|
741 |
||
742 |
fun mk_predT (CompilationFuns funs) = #mk_predT funs |
|
743 |
fun dest_predT (CompilationFuns funs) = #dest_predT funs |
|
744 |
fun mk_bot (CompilationFuns funs) = #mk_bot funs |
|
745 |
fun mk_single (CompilationFuns funs) = #mk_single funs |
|
746 |
fun mk_bind (CompilationFuns funs) = #mk_bind funs |
|
747 |
fun mk_sup (CompilationFuns funs) = #mk_sup funs |
|
748 |
fun mk_if (CompilationFuns funs) = #mk_if funs |
|
749 |
fun mk_not (CompilationFuns funs) = #mk_not funs |
|
750 |
fun mk_map (CompilationFuns funs) = #mk_map funs |
|
751 |
fun lift_pred (CompilationFuns funs) = #lift_pred funs |
|
752 |
||
753 |
fun funT_of compfuns (iss, is) T = |
|
754 |
let |
|
755 |
val Ts = binder_types T |
|
756 |
val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts |
|
757 |
val paramTs' = map2 (fn NONE => I | SOME is => funT_of compfuns ([], is)) iss paramTs |
|
758 |
in |
|
759 |
(paramTs' @ inargTs) ---> (mk_predT compfuns (mk_tupleT outargTs)) |
|
760 |
end; |
|
761 |
||
762 |
fun mk_fun_of compfuns thy (name, T) mode = |
|
763 |
Const (predfun_name_of thy name mode, funT_of compfuns mode T) |
|
764 |
||
765 |
||
766 |
structure PredicateCompFuns = |
|
767 |
struct |
|
768 |
||
769 |
fun mk_predT T = Type (@{type_name "Predicate.pred"}, [T]) |
|
770 |
||
771 |
fun dest_predT (Type (@{type_name "Predicate.pred"}, [T])) = T |
|
772 |
| dest_predT T = raise TYPE ("dest_predT", [T], []); |
|
773 |
||
774 |
fun mk_bot T = Const (@{const_name Orderings.bot}, mk_predT T); |
|
775 |
||
776 |
fun mk_single t = |
|
777 |
let val T = fastype_of t |
|
778 |
in Const(@{const_name Predicate.single}, T --> mk_predT T) $ t end; |
|
779 |
||
780 |
fun mk_bind (x, f) = |
|
781 |
let val T as Type ("fun", [_, U]) = fastype_of f |
|
782 |
in |
|
783 |
Const (@{const_name Predicate.bind}, fastype_of x --> T --> U) $ x $ f |
|
784 |
end; |
|
785 |
||
786 |
val mk_sup = HOLogic.mk_binop @{const_name sup}; |
|
787 |
||
788 |
fun mk_if cond = Const (@{const_name Predicate.if_pred}, |
|
789 |
HOLogic.boolT --> mk_predT HOLogic.unitT) $ cond; |
|
790 |
||
791 |
fun mk_not t = let val T = mk_predT HOLogic.unitT |
|
792 |
in Const (@{const_name Predicate.not_pred}, T --> T) $ t end |
|
793 |
||
794 |
fun mk_Enum f = |
|
795 |
let val T as Type ("fun", [T', _]) = fastype_of f |
|
796 |
in |
|
797 |
Const (@{const_name Predicate.Pred}, T --> mk_predT T') $ f |
|
798 |
end; |
|
799 |
||
800 |
fun mk_Eval (f, x) = |
|
801 |
let |
|
802 |
val T = fastype_of x |
|
803 |
in |
|
804 |
Const (@{const_name Predicate.eval}, mk_predT T --> T --> HOLogic.boolT) $ f $ x |
|
805 |
end; |
|
806 |
||
807 |
fun mk_map T1 T2 tf tp = Const (@{const_name Predicate.map}, |
|
808 |
(T1 --> T2) --> mk_predT T1 --> mk_predT T2) $ tf $ tp; |
|
809 |
||
810 |
val lift_pred = I |
|
811 |
||
812 |
val compfuns = CompilationFuns {mk_predT = mk_predT, dest_predT = dest_predT, mk_bot = mk_bot, |
|
813 |
mk_single = mk_single, mk_bind = mk_bind, mk_sup = mk_sup, mk_if = mk_if, mk_not = mk_not, |
|
814 |
mk_map = mk_map, lift_pred = lift_pred}; |
|
815 |
||
816 |
end; |
|
817 |
||
818 |
structure RPredCompFuns = |
|
819 |
struct |
|
820 |
||
821 |
fun mk_rpredT T = |
|
822 |
@{typ "Random.seed"} --> HOLogic.mk_prodT (PredicateCompFuns.mk_predT T, @{typ "Random.seed"}) |
|
823 |
||
824 |
fun dest_rpredT (Type ("fun", [_, |
|
825 |
Type (@{type_name "*"}, [Type (@{type_name "Predicate.pred"}, [T]), _])])) = T |
|
826 |
| dest_rpredT T = raise TYPE ("dest_rpredT", [T], []); |
|
827 |
||
828 |
fun mk_bot T = Const(@{const_name RPred.bot}, mk_rpredT T) |
|
829 |
||
830 |
fun mk_single t = |
|
831 |
let |
|
832 |
val T = fastype_of t |
|
833 |
in |
|
834 |
Const (@{const_name RPred.return}, T --> mk_rpredT T) $ t |
|
835 |
end; |
|
836 |
||
837 |
fun mk_bind (x, f) = |
|
838 |
let |
|
839 |
val T as (Type ("fun", [_, U])) = fastype_of f |
|
840 |
in |
|
841 |
Const (@{const_name RPred.bind}, fastype_of x --> T --> U) $ x $ f |
|
842 |
end |
|
843 |
||
844 |
val mk_sup = HOLogic.mk_binop @{const_name RPred.supp} |
|
845 |
||
846 |
fun mk_if cond = Const (@{const_name RPred.if_rpred}, |
|
847 |
HOLogic.boolT --> mk_rpredT HOLogic.unitT) $ cond; |
|
848 |
||
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
849 |
fun mk_not t = let val T = mk_rpredT HOLogic.unitT |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
850 |
in Const (@{const_name RPred.not_rpred}, T --> T) $ t end |
32667 | 851 |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
852 |
fun mk_map T1 T2 tf tp = Const (@{const_name RPred.map}, |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
853 |
(T1 --> T2) --> mk_rpredT T1 --> mk_rpredT T2) $ tf $ tp |
32667 | 854 |
|
855 |
fun lift_pred t = |
|
856 |
let |
|
857 |
val T = PredicateCompFuns.dest_predT (fastype_of t) |
|
858 |
val lift_predT = PredicateCompFuns.mk_predT T --> mk_rpredT T |
|
859 |
in |
|
860 |
Const (@{const_name "RPred.lift_pred"}, lift_predT) $ t |
|
861 |
end; |
|
862 |
||
863 |
val compfuns = CompilationFuns {mk_predT = mk_rpredT, dest_predT = dest_rpredT, mk_bot = mk_bot, |
|
864 |
mk_single = mk_single, mk_bind = mk_bind, mk_sup = mk_sup, mk_if = mk_if, mk_not = mk_not, |
|
865 |
mk_map = mk_map, lift_pred = lift_pred}; |
|
866 |
||
867 |
end; |
|
868 |
(* for external use with interactive mode *) |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
869 |
val pred_compfuns = PredicateCompFuns.compfuns |
32667 | 870 |
val rpred_compfuns = RPredCompFuns.compfuns; |
871 |
||
872 |
fun lift_random random = |
|
873 |
let |
|
874 |
val T = dest_randomT (fastype_of random) |
|
875 |
in |
|
876 |
Const (@{const_name lift_random}, (@{typ Random.seed} --> |
|
877 |
HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed})) --> |
|
878 |
RPredCompFuns.mk_rpredT T) $ random |
|
879 |
end; |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
880 |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
881 |
fun depth_limited_funT_of compfuns (iss, is) T = |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
882 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
883 |
val Ts = binder_types T |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
884 |
val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
885 |
val paramTs' = map2 (fn SOME is => depth_limited_funT_of compfuns ([], is) | NONE => I) iss paramTs |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
886 |
in |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
887 |
(paramTs' @ inargTs @ [@{typ bool}, @{typ "code_numeral"}]) ---> (mk_predT compfuns (mk_tupleT outargTs)) |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
888 |
end; |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
889 |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
890 |
fun mk_depth_limited_fun_of compfuns thy (name, T) mode = |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
891 |
Const (depth_limited_function_name_of thy name mode, depth_limited_funT_of compfuns mode T) |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
892 |
|
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
893 |
fun mk_generator_of compfuns thy (name, T) mode = |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
894 |
Const (generator_name_of thy name mode, depth_limited_funT_of compfuns mode T) |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
895 |
|
32667 | 896 |
(* Mode analysis *) |
897 |
||
898 |
(*** check if a term contains only constructor functions ***) |
|
899 |
fun is_constrt thy = |
|
900 |
let |
|
901 |
val cnstrs = flat (maps |
|
902 |
(map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd) |
|
903 |
(Symtab.dest (Datatype.get_all thy))); |
|
904 |
fun check t = (case strip_comb t of |
|
905 |
(Free _, []) => true |
|
906 |
| (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of |
|
907 |
(SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts |
|
908 |
| _ => false) |
|
909 |
| _ => false) |
|
910 |
in check end; |
|
911 |
||
912 |
(*** check if a type is an equality type (i.e. doesn't contain fun) |
|
913 |
FIXME this is only an approximation ***) |
|
914 |
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts |
|
915 |
| is_eqT _ = true; |
|
916 |
||
917 |
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm []; |
|
918 |
val terms_vs = distinct (op =) o maps term_vs; |
|
919 |
||
920 |
(** collect all Frees in a term (with duplicates!) **) |
|
921 |
fun term_vTs tm = |
|
922 |
fold_aterms (fn Free xT => cons xT | _ => I) tm []; |
|
923 |
||
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
924 |
fun subsets i j = |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
925 |
if i <= j then |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
926 |
let |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
927 |
fun merge xs [] = xs |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
928 |
| merge [] ys = ys |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
929 |
| merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys) |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
930 |
else y::merge (x::xs) ys; |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
931 |
val is = subsets (i+1) j |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
932 |
in merge (map (fn ks => i::ks) is) is end |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
933 |
else [[]]; |
32667 | 934 |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
935 |
(* FIXME: should be in library - cprod = map_prod I *) |
32667 | 936 |
fun cprod ([], ys) = [] |
937 |
| cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys); |
|
938 |
||
939 |
fun cprods xss = foldr (map op :: o cprod) [[]] xss; |
|
940 |
||
941 |
fun cprods_subset [] = [[]] |
|
942 |
| cprods_subset (xs :: xss) = |
|
943 |
let |
|
944 |
val yss = (cprods_subset xss) |
|
945 |
in maps (fn ys => map (fn x => cons x ys) xs) yss @ yss end |
|
946 |
||
947 |
(*TODO: cleanup function and put together with modes_of_term *) |
|
948 |
(* |
|
949 |
fun modes_of_param default modes t = let |
|
950 |
val (vs, t') = strip_abs t |
|
951 |
val b = length vs |
|
952 |
fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) => |
|
953 |
let |
|
954 |
val (args1, args2) = |
|
955 |
if length args < length iss then |
|
956 |
error ("Too few arguments for inductive predicate " ^ name) |
|
957 |
else chop (length iss) args; |
|
958 |
val k = length args2; |
|
959 |
val perm = map (fn i => (find_index_eq (Bound (b - i)) args2) + 1) |
|
960 |
(1 upto b) |
|
961 |
val partial_mode = (1 upto k) \\ perm |
|
962 |
in |
|
963 |
if not (partial_mode subset is) then [] else |
|
964 |
let |
|
965 |
val is' = |
|
966 |
(fold_index (fn (i, j) => if j mem is then cons (i + 1) else I) perm []) |
|
967 |
|> fold (fn i => if i > k then cons (i - k + b) else I) is |
|
968 |
||
969 |
val res = map (fn x => Mode (m, is', x)) (cprods (map |
|
970 |
(fn (NONE, _) => [NONE] |
|
971 |
| (SOME js, arg) => map SOME (filter |
|
972 |
(fn Mode (_, js', _) => js=js') (modes_of_term modes arg))) |
|
973 |
(iss ~~ args1))) |
|
974 |
in res end |
|
975 |
end)) (AList.lookup op = modes name) |
|
976 |
in case strip_comb t' of |
|
977 |
(Const (name, _), args) => the_default default (mk_modes name args) |
|
978 |
| (Var ((name, _), _), args) => the (mk_modes name args) |
|
979 |
| (Free (name, _), args) => the (mk_modes name args) |
|
980 |
| _ => default end |
|
981 |
||
982 |
and |
|
983 |
*) |
|
984 |
fun modes_of_term modes t = |
|
985 |
let |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
986 |
val ks = map_index (fn (i, T) => (i + 1, NONE)) (binder_types (fastype_of t)); |
32667 | 987 |
val default = [Mode (([], ks), ks, [])]; |
988 |
fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) => |
|
989 |
let |
|
990 |
val (args1, args2) = |
|
991 |
if length args < length iss then |
|
992 |
error ("Too few arguments for inductive predicate " ^ name) |
|
993 |
else chop (length iss) args; |
|
994 |
val k = length args2; |
|
995 |
val prfx = map (rpair NONE) (1 upto k) |
|
996 |
in |
|
997 |
if not (is_prefix op = prfx is) then [] else |
|
33116
b379ee2cddb1
changed importing introduction rules to fix the same type variables in all introduction rules; improved mode analysis for partially applied relations; added test case; tuned
bulwahn
parents:
33115
diff
changeset
|
998 |
let val is' = map (fn (i, t) => (i - k, t)) (List.drop (is, k)) |
32667 | 999 |
in map (fn x => Mode (m, is', x)) (cprods (map |
1000 |
(fn (NONE, _) => [NONE] |
|
1001 |
| (SOME js, arg) => map SOME (filter |
|
1002 |
(fn Mode (_, js', _) => js=js') (modes_of_term modes arg))) |
|
1003 |
(iss ~~ args1))) |
|
1004 |
end |
|
1005 |
end)) (AList.lookup op = modes name) |
|
1006 |
||
1007 |
in |
|
1008 |
case strip_comb (Envir.eta_contract t) of |
|
1009 |
(Const (name, _), args) => the_default default (mk_modes name args) |
|
1010 |
| (Var ((name, _), _), args) => the (mk_modes name args) |
|
1011 |
| (Free (name, _), args) => the (mk_modes name args) |
|
1012 |
| (Abs _, []) => error "Abs at param position" (* modes_of_param default modes t *) |
|
1013 |
| _ => default |
|
1014 |
end |
|
1015 |
||
1016 |
fun select_mode_prem thy modes vs ps = |
|
1017 |
find_first (is_some o snd) (ps ~~ map |
|
1018 |
(fn Prem (us, t) => find_first (fn Mode (_, is, _) => |
|
1019 |
let |
|
1020 |
val (in_ts, out_ts) = split_smode is us; |
|
1021 |
val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts; |
|
1022 |
val vTs = maps term_vTs out_ts'; |
|
1023 |
val dupTs = map snd (duplicates (op =) vTs) @ |
|
1024 |
List.mapPartial (AList.lookup (op =) vTs) vs; |
|
1025 |
in |
|
1026 |
terms_vs (in_ts @ in_ts') subset vs andalso |
|
1027 |
forall (is_eqT o fastype_of) in_ts' andalso |
|
1028 |
term_vs t subset vs andalso |
|
1029 |
forall is_eqT dupTs |
|
1030 |
end) |
|
1031 |
(modes_of_term modes t handle Option => |
|
1032 |
error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)) |
|
1033 |
| Negprem (us, t) => find_first (fn Mode (_, is, _) => |
|
33112
6672184a736b
added a few tricky examples with tuples; added alternative introduction rules for some constants; corrected mode analysis with negation; improved fetching of definitions
bulwahn
parents:
33109
diff
changeset
|
1034 |
is = map (rpair NONE) (1 upto length us) andalso |
32667 | 1035 |
terms_vs us subset vs andalso |
1036 |
term_vs t subset vs) |
|
1037 |
(modes_of_term modes t handle Option => |
|
1038 |
error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)) |
|
1039 |
| Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], [])) |
|
1040 |
else NONE |
|
1041 |
) ps); |
|
1042 |
||
1043 |
fun fold_prem f (Prem (args, _)) = fold f args |
|
1044 |
| fold_prem f (Negprem (args, _)) = fold f args |
|
1045 |
| fold_prem f (Sidecond t) = f t |
|
1046 |
||
1047 |
fun all_subsets [] = [[]] |
|
1048 |
| all_subsets (x::xs) = let val xss' = all_subsets xs in xss' @ (map (cons x) xss') end |
|
1049 |
||
1050 |
fun generator vTs v = |
|
1051 |
let |
|
1052 |
val T = the (AList.lookup (op =) vTs v) |
|
1053 |
in |
|
1054 |
(Generator (v, T), Mode (([], []), [], [])) |
|
1055 |
end; |
|
1056 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
1057 |
fun gen_prem (Prem (us, t)) = GeneratorPrem (us, t) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
1058 |
| gen_prem (Negprem (us, t)) = error "it is a negated prem" |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
1059 |
| gen_prem (Sidecond t) = error "it is a sidecond" |
32667 | 1060 |
| gen_prem _ = error "gen_prem : invalid input for gen_prem" |
1061 |
||
1062 |
fun param_gen_prem param_vs (p as Prem (us, t as Free (v, _))) = |
|
1063 |
if member (op =) param_vs v then |
|
1064 |
GeneratorPrem (us, t) |
|
1065 |
else p |
|
1066 |
| param_gen_prem param_vs p = p |
|
1067 |
||
1068 |
fun check_mode_clause with_generator thy param_vs modes gen_modes (iss, is) (ts, ps) = |
|
1069 |
let |
|
1070 |
val modes' = modes @ List.mapPartial |
|
1071 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
|
1072 |
(param_vs ~~ iss); |
|
1073 |
val gen_modes' = gen_modes @ List.mapPartial |
|
1074 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
|
1075 |
(param_vs ~~ iss); |
|
1076 |
val vTs = distinct (op =) ((fold o fold_prem) Term.add_frees ps (fold Term.add_frees ts [])) |
|
1077 |
val prem_vs = distinct (op =) ((fold o fold_prem) Term.add_free_names ps []) |
|
1078 |
fun check_mode_prems acc_ps vs [] = SOME (acc_ps, vs) |
|
1079 |
| check_mode_prems acc_ps vs ps = (case select_mode_prem thy modes' vs ps of |
|
1080 |
NONE => |
|
1081 |
(if with_generator then |
|
1082 |
(case select_mode_prem thy gen_modes' vs ps of |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
1083 |
SOME (p as Prem _, SOME mode) => check_mode_prems ((gen_prem p, mode) :: acc_ps) |
32667 | 1084 |
(case p of Prem (us, _) => vs union terms_vs us | _ => vs) |
1085 |
(filter_out (equal p) ps) |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1086 |
| _ => |
32667 | 1087 |
let |
1088 |
val all_generator_vs = all_subsets (prem_vs \\ vs) |> sort (int_ord o (pairself length)) |
|
1089 |
in |
|
1090 |
case (find_first (fn generator_vs => is_some |
|
1091 |
(select_mode_prem thy modes' (vs union generator_vs) ps)) all_generator_vs) of |
|
1092 |
SOME generator_vs => check_mode_prems ((map (generator vTs) generator_vs) @ acc_ps) |
|
1093 |
(vs union generator_vs) ps |
|
33128 | 1094 |
| NONE => NONE |
32667 | 1095 |
end) |
1096 |
else |
|
1097 |
NONE) |
|
1098 |
| SOME (p, SOME mode) => check_mode_prems ((if with_generator then param_gen_prem param_vs p else p, mode) :: acc_ps) |
|
1099 |
(case p of Prem (us, _) => vs union terms_vs us | _ => vs) |
|
1100 |
(filter_out (equal p) ps)) |
|
1101 |
val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (split_smode is ts)); |
|
1102 |
val in_vs = terms_vs in_ts; |
|
1103 |
val concl_vs = terms_vs ts |
|
1104 |
in |
|
1105 |
if forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso |
|
1106 |
forall (is_eqT o fastype_of) in_ts' then |
|
1107 |
case check_mode_prems [] (param_vs union in_vs) ps of |
|
1108 |
NONE => NONE |
|
1109 |
| SOME (acc_ps, vs) => |
|
1110 |
if with_generator then |
|
1111 |
SOME (ts, (rev acc_ps) @ (map (generator vTs) (concl_vs \\ vs))) |
|
1112 |
else |
|
1113 |
if concl_vs subset vs then SOME (ts, rev acc_ps) else NONE |
|
1114 |
else NONE |
|
1115 |
end; |
|
1116 |
||
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1117 |
fun print_failed_mode options thy modes p m rs i = |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1118 |
if show_mode_inference options then |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1119 |
let |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1120 |
val _ = Output.tracing ("Clause " ^ string_of_int (i + 1) ^ " of " ^ |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1121 |
p ^ " violates mode " ^ string_of_mode m) |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1122 |
val _ = Output.tracing (string_of_clause thy p (nth rs i)) |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1123 |
in () end |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1124 |
else () |
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1125 |
|
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1126 |
fun check_modes_pred options with_generator thy param_vs clauses modes gen_modes (p, ms) = |
32667 | 1127 |
let val SOME rs = AList.lookup (op =) clauses p |
1128 |
in (p, List.filter (fn m => case find_index |
|
1129 |
(is_none o check_mode_clause with_generator thy param_vs modes gen_modes m) rs of |
|
1130 |
~1 => true |
|
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1131 |
| i => (print_failed_mode options thy modes p m rs i; false)) ms) |
32667 | 1132 |
end; |
1133 |
||
1134 |
fun get_modes_pred with_generator thy param_vs clauses modes gen_modes (p, ms) = |
|
1135 |
let |
|
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1136 |
val SOME rs = AList.lookup (op =) clauses p |
32667 | 1137 |
in |
1138 |
(p, map (fn m => |
|
1139 |
(m, map (the o check_mode_clause with_generator thy param_vs modes gen_modes m) rs)) ms) |
|
1140 |
end; |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
1141 |
|
32667 | 1142 |
fun fixp f (x : (string * mode list) list) = |
1143 |
let val y = f x |
|
1144 |
in if x = y then x else fixp f y end; |
|
1145 |
||
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1146 |
fun infer_modes options thy extra_modes all_modes param_vs clauses = |
32667 | 1147 |
let |
1148 |
val modes = |
|
1149 |
fixp (fn modes => |
|
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1150 |
map (check_modes_pred options false thy param_vs clauses (modes @ extra_modes) []) modes) |
32667 | 1151 |
all_modes |
1152 |
in |
|
1153 |
map (get_modes_pred false thy param_vs clauses (modes @ extra_modes) []) modes |
|
1154 |
end; |
|
1155 |
||
1156 |
fun remove_from rem [] = [] |
|
1157 |
| remove_from rem ((k, vs) :: xs) = |
|
1158 |
(case AList.lookup (op =) rem k of |
|
1159 |
NONE => (k, vs) |
|
1160 |
| SOME vs' => (k, vs \\ vs')) |
|
1161 |
:: remove_from rem xs |
|
1162 |
||
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
1163 |
fun infer_modes_with_generator options thy extra_modes all_modes param_vs clauses = |
32667 | 1164 |
let |
1165 |
val prednames = map fst clauses |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1166 |
val extra_modes' = all_modes_of thy |
32667 | 1167 |
val gen_modes = all_generator_modes_of thy |
1168 |
|> filter_out (fn (name, _) => member (op =) prednames name) |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1169 |
val starting_modes = remove_from extra_modes' all_modes |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1170 |
fun eq_mode (m1, m2) = (m1 = m2) |
32667 | 1171 |
val modes = |
1172 |
fixp (fn modes => |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1173 |
map (check_modes_pred options true thy param_vs clauses extra_modes' (gen_modes @ modes)) modes) |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1174 |
starting_modes |
32667 | 1175 |
in |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1176 |
AList.join (op =) |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1177 |
(fn _ => fn ((mps1, mps2)) => |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1178 |
merge (fn ((m1, _), (m2, _)) => eq_mode (m1, m2)) (mps1, mps2)) |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1179 |
(infer_modes options thy extra_modes all_modes param_vs clauses, |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1180 |
map (get_modes_pred true thy param_vs clauses extra_modes (gen_modes @ modes)) modes) |
32667 | 1181 |
end; |
1182 |
||
1183 |
(* term construction *) |
|
1184 |
||
1185 |
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of |
|
1186 |
NONE => (Free (s, T), (names, (s, [])::vs)) |
|
1187 |
| SOME xs => |
|
1188 |
let |
|
1189 |
val s' = Name.variant names s; |
|
1190 |
val v = Free (s', T) |
|
1191 |
in |
|
1192 |
(v, (s'::names, AList.update (op =) (s, v::xs) vs)) |
|
1193 |
end); |
|
1194 |
||
1195 |
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T |
|
1196 |
| distinct_v (t $ u) nvs = |
|
1197 |
let |
|
1198 |
val (t', nvs') = distinct_v t nvs; |
|
1199 |
val (u', nvs'') = distinct_v u nvs'; |
|
1200 |
in (t' $ u', nvs'') end |
|
1201 |
| distinct_v x nvs = (x, nvs); |
|
1202 |
||
1203 |
fun compile_match thy compfuns eqs eqs' out_ts success_t = |
|
1204 |
let |
|
1205 |
val eqs'' = maps mk_eq eqs @ eqs' |
|
1206 |
val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) []; |
|
1207 |
val name = Name.variant names "x"; |
|
1208 |
val name' = Name.variant (name :: names) "y"; |
|
1209 |
val T = mk_tupleT (map fastype_of out_ts); |
|
1210 |
val U = fastype_of success_t; |
|
1211 |
val U' = dest_predT compfuns U; |
|
1212 |
val v = Free (name, T); |
|
1213 |
val v' = Free (name', T); |
|
1214 |
in |
|
1215 |
lambda v (fst (Datatype.make_case |
|
32671 | 1216 |
(ProofContext.init thy) DatatypeCase.Quiet [] v |
32667 | 1217 |
[(mk_tuple out_ts, |
1218 |
if null eqs'' then success_t |
|
1219 |
else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $ |
|
1220 |
foldr1 HOLogic.mk_conj eqs'' $ success_t $ |
|
1221 |
mk_bot compfuns U'), |
|
1222 |
(v', mk_bot compfuns U')])) |
|
1223 |
end; |
|
1224 |
||
1225 |
(*FIXME function can be removed*) |
|
1226 |
fun mk_funcomp f t = |
|
1227 |
let |
|
1228 |
val names = Term.add_free_names t []; |
|
1229 |
val Ts = binder_types (fastype_of t); |
|
1230 |
val vs = map Free |
|
1231 |
(Name.variant_list names (replicate (length Ts) "x") ~~ Ts) |
|
1232 |
in |
|
1233 |
fold_rev lambda vs (f (list_comb (t, vs))) |
|
1234 |
end; |
|
1235 |
||
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1236 |
fun compile_param compilation_modifiers compfuns thy (NONE, t) = t |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1237 |
| compile_param compilation_modifiers compfuns thy (m as SOME (Mode (mode, _, ms)), t) = |
32667 | 1238 |
let |
1239 |
val (f, args) = strip_comb (Envir.eta_contract t) |
|
1240 |
val (params, args') = chop (length ms) args |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1241 |
val params' = map (compile_param compilation_modifiers compfuns thy) (ms ~~ params) |
32667 | 1242 |
val f' = |
1243 |
case f of |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1244 |
Const (name, T) => Const (#const_name_of compilation_modifiers thy name mode, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1245 |
#funT_of compilation_modifiers compfuns mode T) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1246 |
| Free (name, T) => Free (name, #funT_of compilation_modifiers compfuns mode T) |
32667 | 1247 |
| _ => error ("PredicateCompiler: illegal parameter term") |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1248 |
in |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1249 |
list_comb (f', params' @ args') |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1250 |
end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1251 |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1252 |
fun compile_expr compilation_modifiers compfuns thy ((Mode (mode, _, ms)), t) inargs additional_arguments = |
32667 | 1253 |
case strip_comb t of |
1254 |
(Const (name, T), params) => |
|
1255 |
let |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1256 |
val params' = map (compile_param compilation_modifiers compfuns thy) (ms ~~ params) |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1257 |
(*val mk_fun_of = if depth_limited then mk_depth_limited_fun_of else mk_fun_of*) |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1258 |
val name' = #const_name_of compilation_modifiers thy name mode |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1259 |
val T' = #funT_of compilation_modifiers compfuns mode T |
32667 | 1260 |
in |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1261 |
(list_comb (Const (name', T'), params' @ inargs @ additional_arguments)) |
32667 | 1262 |
end |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
1263 |
| (Free (name, T), params) => |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1264 |
list_comb (Free (name, #funT_of compilation_modifiers compfuns mode T), params @ inargs @ additional_arguments) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1265 |
(* |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1266 |
fun compile_gen_expr depth_limited thy compfuns mk_fun_of ((Mode (mode, is, ms)), t) inargs = |
32667 | 1267 |
case strip_comb t of |
1268 |
(Const (name, T), params) => |
|
1269 |
let |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1270 |
val params' = map (compile_param depth_limited thy RPredCompFuns.compfuns mk_fun_of) (ms ~~ params) |
32667 | 1271 |
in |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
1272 |
list_comb (mk_generator_of RPredCompFuns.compfuns thy (name, T) mode, params' @ inargs) |
32667 | 1273 |
end |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
1274 |
| (Free (name, T), params) => |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1275 |
(*lift_pred RPredCompFuns.compfuns*) |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1276 |
(list_comb (Free (name, depth_limited_funT_of RPredCompFuns.compfuns ([], is) T), params @ inargs)) |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1277 |
*) |
32667 | 1278 |
(** specific rpred functions -- move them to the correct place in this file *) |
1279 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1280 |
fun mk_Eval_of depth ((x, T), NONE) names = (x, names) |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1281 |
| mk_Eval_of depth ((x, T), SOME mode) names = |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1282 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1283 |
val Ts = binder_types T |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1284 |
(*val argnames = Name.variant_list names |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1285 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts))); |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1286 |
val args = map Free (argnames ~~ Ts) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1287 |
val (inargs, outargs) = split_smode mode args*) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1288 |
fun mk_split_lambda [] t = lambda (Free (Name.variant names "x", HOLogic.unitT)) t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1289 |
| mk_split_lambda [x] t = lambda x t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1290 |
| mk_split_lambda xs t = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1291 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1292 |
fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t)) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1293 |
| mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t)) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1294 |
in |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1295 |
mk_split_lambda' xs t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1296 |
end; |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1297 |
fun mk_arg (i, T) = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1298 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1299 |
val vname = Name.variant names ("x" ^ string_of_int i) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1300 |
val default = Free (vname, T) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1301 |
in |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1302 |
case AList.lookup (op =) mode i of |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1303 |
NONE => (([], [default]), [default]) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1304 |
| SOME NONE => (([default], []), [default]) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1305 |
| SOME (SOME pis) => |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1306 |
case HOLogic.strip_tupleT T of |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1307 |
[] => error "pair mode but unit tuple" (*(([default], []), [default])*) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1308 |
| [_] => error "pair mode but not a tuple" (*(([default], []), [default])*) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1309 |
| Ts => |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1310 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1311 |
val vnames = Name.variant_list names |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1312 |
(map (fn j => "x" ^ string_of_int i ^ "p" ^ string_of_int j) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1313 |
(1 upto length Ts)) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1314 |
val args = map Free (vnames ~~ Ts) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1315 |
fun split_args (i, arg) (ins, outs) = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1316 |
if member (op =) pis i then |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1317 |
(arg::ins, outs) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1318 |
else |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1319 |
(ins, arg::outs) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1320 |
val (inargs, outargs) = fold_rev split_args ((1 upto length Ts) ~~ args) ([], []) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1321 |
fun tuple args = if null args then [] else [HOLogic.mk_tuple args] |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1322 |
in ((tuple inargs, tuple outargs), args) end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1323 |
end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1324 |
val (inoutargs, args) = split_list (map mk_arg (1 upto (length Ts) ~~ Ts)) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1325 |
val (inargs, outargs) = pairself flat (split_list inoutargs) |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1326 |
val depth_t = case depth of NONE => [] | SOME (polarity, depth_t) => [polarity, depth_t] |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1327 |
val r = PredicateCompFuns.mk_Eval (list_comb (x, inargs @ depth_t), mk_tuple outargs) |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1328 |
val t = fold_rev mk_split_lambda args r |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1329 |
in |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1330 |
(t, names) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1331 |
end; |
32667 | 1332 |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1333 |
fun compile_arg depth thy param_vs iss arg = |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1334 |
let |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1335 |
val funT_of = case depth of NONE => funT_of | SOME _ => depth_limited_funT_of |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1336 |
fun map_params (t as Free (f, T)) = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1337 |
if member (op =) param_vs f then |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1338 |
case (the (AList.lookup (op =) (param_vs ~~ iss) f)) of |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1339 |
SOME is => let val T' = funT_of PredicateCompFuns.compfuns ([], is) T |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1340 |
in fst (mk_Eval_of depth ((Free (f, T'), T), SOME is) []) end |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1341 |
| NONE => t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1342 |
else t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1343 |
| map_params t = t |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1344 |
in map_aterms map_params arg end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1345 |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1346 |
fun compile_clause compilation_modifiers compfuns thy all_vs param_vs additional_arguments (iss, is) inp (ts, moded_ps) = |
32667 | 1347 |
let |
1348 |
fun check_constrt t (names, eqs) = |
|
1349 |
if is_constrt thy t then (t, (names, eqs)) else |
|
1350 |
let |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1351 |
val s = Name.variant names "x" |
32667 | 1352 |
val v = Free (s, fastype_of t) |
1353 |
in (v, (s::names, HOLogic.mk_eq (v, t)::eqs)) end; |
|
1354 |
||
1355 |
val (in_ts, out_ts) = split_smode is ts; |
|
1356 |
val (in_ts', (all_vs', eqs)) = |
|
1357 |
fold_map check_constrt in_ts (all_vs, []); |
|
1358 |
||
1359 |
fun compile_prems out_ts' vs names [] = |
|
1360 |
let |
|
1361 |
val (out_ts'', (names', eqs')) = |
|
1362 |
fold_map check_constrt out_ts' (names, []); |
|
1363 |
val (out_ts''', (names'', constr_vs)) = fold_map distinct_v |
|
1364 |
out_ts'' (names', map (rpair []) vs); |
|
1365 |
in |
|
1366 |
compile_match thy compfuns constr_vs (eqs @ eqs') out_ts''' |
|
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1367 |
(mk_single compfuns (mk_tuple out_ts)) |
32667 | 1368 |
end |
1369 |
| compile_prems out_ts vs names ((p, mode as Mode ((_, is), _, _)) :: ps) = |
|
1370 |
let |
|
1371 |
val vs' = distinct (op =) (flat (vs :: map term_vs out_ts)); |
|
1372 |
val (out_ts', (names', eqs)) = |
|
1373 |
fold_map check_constrt out_ts (names, []) |
|
1374 |
val (out_ts'', (names'', constr_vs')) = fold_map distinct_v |
|
1375 |
out_ts' ((names', map (rpair []) vs)) |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1376 |
val additional_arguments' = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1377 |
#transform_additional_arguments compilation_modifiers p additional_arguments |
32667 | 1378 |
val (compiled_clause, rest) = case p of |
1379 |
Prem (us, t) => |
|
1380 |
let |
|
1381 |
val (in_ts, out_ts''') = split_smode is us; |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1382 |
(* add test case for compile_arg *) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1383 |
(*val in_ts = map (compile_arg depth thy param_vs iss) in_ts*) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1384 |
(* additional_arguments |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1385 |
val args = case depth of |
32667 | 1386 |
NONE => in_ts |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1387 |
| SOME (polarity, depth_t) => in_ts @ [polarity, depth_t] |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1388 |
*) |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1389 |
val u = |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1390 |
compile_expr compilation_modifiers compfuns thy (mode, t) in_ts additional_arguments' |
32667 | 1391 |
val rest = compile_prems out_ts''' vs' names'' ps |
1392 |
in |
|
1393 |
(u, rest) |
|
1394 |
end |
|
1395 |
| Negprem (us, t) => |
|
1396 |
let |
|
1397 |
val (in_ts, out_ts''') = split_smode is us |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1398 |
(* additional_arguments |
33135
422cac7d6e31
added option to execute depth-limited computations for the values command in the predicate compiler
bulwahn
parents:
33134
diff
changeset
|
1399 |
val args = case depth of |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1400 |
NONE => in_ts |
33135
422cac7d6e31
added option to execute depth-limited computations for the values command in the predicate compiler
bulwahn
parents:
33134
diff
changeset
|
1401 |
| SOME (polarity, depth_t) => in_ts @ [HOLogic.mk_not polarity, depth_t] |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1402 |
*) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1403 |
val u = mk_not compfuns |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1404 |
(compile_expr compilation_modifiers compfuns thy (mode, t) in_ts additional_arguments') |
32667 | 1405 |
val rest = compile_prems out_ts''' vs' names'' ps |
1406 |
in |
|
1407 |
(u, rest) |
|
1408 |
end |
|
1409 |
| Sidecond t => |
|
1410 |
let |
|
1411 |
val rest = compile_prems [] vs' names'' ps; |
|
1412 |
in |
|
1413 |
(mk_if compfuns t, rest) |
|
1414 |
end |
|
1415 |
| GeneratorPrem (us, t) => |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1416 |
(* TODO: remove GeneratorPrem -- is not used anymore *) |
32667 | 1417 |
let |
1418 |
val (in_ts, out_ts''') = split_smode is us; |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1419 |
val u = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1420 |
compile_expr compilation_modifiers compfuns thy (mode, t) in_ts additional_arguments' |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1421 |
val rest = compile_prems out_ts''' vs' names'' ps |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1422 |
in |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1423 |
(u, rest) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1424 |
end |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1425 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1426 |
(* let |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1427 |
val (in_ts, out_ts''') = split_smode is us; |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1428 |
val args = case depth of |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1429 |
NONE => in_ts |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1430 |
| SOME (polarity, depth_t) => in_ts @ [polarity, depth_t] |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1431 |
val u = compile_gen_expr (is_some depth) thy compfuns mk_fun_of (mode, t) args |
32667 | 1432 |
val rest = compile_prems out_ts''' vs' names'' ps |
1433 |
in |
|
1434 |
(u, rest) |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1435 |
end*) |
32667 | 1436 |
| Generator (v, T) => |
1437 |
let |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1438 |
val [size] = additional_arguments |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1439 |
val u = lift_random (HOLogic.mk_random T size) |
32667 | 1440 |
val rest = compile_prems [Free (v, T)] vs' names'' ps; |
1441 |
in |
|
1442 |
(u, rest) |
|
1443 |
end |
|
1444 |
in |
|
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1445 |
compile_match thy compfuns constr_vs' eqs out_ts'' |
32667 | 1446 |
(mk_bind compfuns (compiled_clause, rest)) |
1447 |
end |
|
1448 |
val prem_t = compile_prems in_ts' param_vs all_vs' moded_ps; |
|
1449 |
in |
|
1450 |
mk_bind compfuns (mk_single compfuns inp, prem_t) |
|
1451 |
end |
|
1452 |
||
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1453 |
fun compile_pred compilation_modifiers compfuns thy all_vs param_vs s T mode moded_cls = |
32667 | 1454 |
let |
1455 |
val (Ts1, Ts2) = chop (length (fst mode)) (binder_types T) |
|
1456 |
val (Us1, Us2) = split_smodeT (snd mode) Ts2 |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1457 |
val Ts1' = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1458 |
map2 (fn NONE => I | SOME is => #funT_of compilation_modifiers compfuns ([], is)) (fst mode) Ts1 |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1459 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1460 |
fun mk_input_term (i, NONE) = |
32667 | 1461 |
[Free (Name.variant (all_vs @ param_vs) ("x" ^ string_of_int i), nth Ts2 (i - 1))] |
1462 |
| mk_input_term (i, SOME pis) = case HOLogic.strip_tupleT (nth Ts2 (i - 1)) of |
|
1463 |
[] => error "strange unit input" |
|
1464 |
| [T] => [Free (Name.variant (all_vs @ param_vs) ("x" ^ string_of_int i), nth Ts2 (i - 1))] |
|
1465 |
| Ts => let |
|
1466 |
val vnames = Name.variant_list (all_vs @ param_vs) |
|
1467 |
(map (fn j => "x" ^ string_of_int i ^ "p" ^ string_of_int j) |
|
1468 |
pis) |
|
1469 |
in if null pis then [] |
|
1470 |
else [HOLogic.mk_tuple (map Free (vnames ~~ map (fn j => nth Ts (j - 1)) pis))] end |
|
1471 |
val in_ts = maps mk_input_term (snd mode) |
|
1472 |
val params = map2 (fn s => fn T => Free (s, T)) param_vs Ts1' |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1473 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1474 |
(* additional arguments *) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1475 |
(* |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1476 |
val [depth_name, polarity_name] = Name.variant_list (all_vs @ param_vs) ["depth", "polarity"] |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1477 |
val depth = Free (depth_name, @{typ "code_numeral"}) |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1478 |
val polarity = Free (polarity_name, @{typ "bool"}) |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1479 |
*) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1480 |
val additional_arguments = #additional_arguments compilation_modifiers (all_vs @ param_vs) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1481 |
(* additional_argument_transformer *) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1482 |
(* |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1483 |
val decr_depth = |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1484 |
if depth_limited then |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1485 |
SOME (polarity, Const ("HOL.minus_class.minus", @{typ "code_numeral => code_numeral => code_numeral"}) |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1486 |
$ depth $ Const ("HOL.one_class.one", @{typ "Code_Numeral.code_numeral"})) |
32667 | 1487 |
else |
1488 |
NONE |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1489 |
*) |
32667 | 1490 |
val cl_ts = |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1491 |
map (compile_clause compilation_modifiers compfuns |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1492 |
thy all_vs param_vs additional_arguments mode (mk_tuple in_ts)) moded_cls; |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1493 |
(* wrap_compilation *) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1494 |
val compilation = #wrap_compilation compilation_modifiers compfuns s T mode additional_arguments |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1495 |
(foldr1 (mk_sup compfuns) cl_ts) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1496 |
(* val T' = mk_predT compfuns (mk_tupleT Us2) |
33133
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1497 |
val if_const = Const (@{const_name "If"}, @{typ bool} --> T' --> T' --> T') |
2eb7dfcf3bc3
simplified and improved compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33132
diff
changeset
|
1498 |
val full_mode = null Us2 |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1499 |
|
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
1500 |
val depth_compilation = |
33141
89b23fad5e02
modifying the depth-limited compilation to be sound, but now throws an error undefined in case of hitting the depth limit in an negative context; cleaning up the examples
bulwahn
parents:
33140
diff
changeset
|
1501 |
if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"}) |
89b23fad5e02
modifying the depth-limited compilation to be sound, but now throws an error undefined in case of hitting the depth limit in an negative context; cleaning up the examples
bulwahn
parents:
33140
diff
changeset
|
1502 |
$ (if_const $ polarity $ mk_bot compfuns (dest_predT compfuns T') |
89b23fad5e02
modifying the depth-limited compilation to be sound, but now throws an error undefined in case of hitting the depth limit in an negative context; cleaning up the examples
bulwahn
parents:
33140
diff
changeset
|
1503 |
$ (if full_mode then mk_single compfuns HOLogic.unit else Const (@{const_name undefined}, T'))) |
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
1504 |
$ compilation |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1505 |
*) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1506 |
val fun_const = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1507 |
Const (#const_name_of compilation_modifiers thy s mode, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1508 |
#funT_of compilation_modifiers compfuns mode T) |
32667 | 1509 |
in |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1510 |
HOLogic.mk_Trueprop |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1511 |
(HOLogic.mk_eq (list_comb (fun_const, params @ in_ts @ additional_arguments), compilation)) |
32667 | 1512 |
end; |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1513 |
|
32667 | 1514 |
(* special setup for simpset *) |
1515 |
val HOL_basic_ss' = HOL_basic_ss addsimps (@{thms "HOL.simp_thms"} @ [@{thm Pair_eq}]) |
|
1516 |
setSolver (mk_solver "all_tac_solver" (fn _ => fn _ => all_tac)) |
|
1517 |
setSolver (mk_solver "True_solver" (fn _ => rtac @{thm TrueI})) |
|
1518 |
||
1519 |
(* Definition of executable functions and their intro and elim rules *) |
|
1520 |
||
1521 |
fun print_arities arities = tracing ("Arities:\n" ^ |
|
1522 |
cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^ |
|
1523 |
space_implode " -> " (map |
|
1524 |
(fn NONE => "X" | SOME k' => string_of_int k') |
|
1525 |
(ks @ [SOME k]))) arities)); |
|
1526 |
||
1527 |
fun create_intro_elim_rule (mode as (iss, is)) defthm mode_id funT pred thy = |
|
1528 |
let |
|
1529 |
val Ts = binder_types (fastype_of pred) |
|
1530 |
val funtrm = Const (mode_id, funT) |
|
1531 |
val (Ts1, Ts2) = chop (length iss) Ts; |
|
1532 |
val Ts1' = map2 (fn NONE => I | SOME is => funT_of (PredicateCompFuns.compfuns) ([], is)) iss Ts1 |
|
1533 |
val param_names = Name.variant_list [] |
|
1534 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts1))); |
|
1535 |
val params = map Free (param_names ~~ Ts1') |
|
1536 |
fun mk_args (i, T) argnames = |
|
1537 |
let |
|
1538 |
val vname = Name.variant (param_names @ argnames) ("x" ^ string_of_int (length Ts1' + i)) |
|
1539 |
val default = (Free (vname, T), vname :: argnames) |
|
1540 |
in |
|
1541 |
case AList.lookup (op =) is i of |
|
1542 |
NONE => default |
|
1543 |
| SOME NONE => default |
|
1544 |
| SOME (SOME pis) => |
|
1545 |
case HOLogic.strip_tupleT T of |
|
1546 |
[] => default |
|
1547 |
| [_] => default |
|
1548 |
| Ts => |
|
1549 |
let |
|
1550 |
val vnames = Name.variant_list (param_names @ argnames) |
|
1551 |
(map (fn j => "x" ^ string_of_int (length Ts1' + i) ^ "p" ^ string_of_int j) |
|
1552 |
(1 upto (length Ts))) |
|
1553 |
in (HOLogic.mk_tuple (map Free (vnames ~~ Ts)), vnames @ argnames) end |
|
1554 |
end |
|
1555 |
val (args, argnames) = fold_map mk_args (1 upto (length Ts2) ~~ Ts2) [] |
|
1556 |
val (inargs, outargs) = split_smode is args |
|
1557 |
val param_names' = Name.variant_list (param_names @ argnames) |
|
1558 |
(map (fn i => "p" ^ string_of_int i) (1 upto (length iss))) |
|
1559 |
val param_vs = map Free (param_names' ~~ Ts1) |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1560 |
val (params', names) = fold_map (mk_Eval_of NONE) ((params ~~ Ts1) ~~ iss) [] |
32667 | 1561 |
val predpropI = HOLogic.mk_Trueprop (list_comb (pred, param_vs @ args)) |
1562 |
val predpropE = HOLogic.mk_Trueprop (list_comb (pred, params' @ args)) |
|
1563 |
val param_eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (param_vs ~~ params') |
|
1564 |
val funargs = params @ inargs |
|
1565 |
val funpropE = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, funargs), |
|
1566 |
if null outargs then Free("y", HOLogic.unitT) else mk_tuple outargs)) |
|
1567 |
val funpropI = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, funargs), |
|
1568 |
mk_tuple outargs)) |
|
1569 |
val introtrm = Logic.list_implies (predpropI :: param_eqs, funpropI) |
|
1570 |
val simprules = [defthm, @{thm eval_pred}, |
|
1571 |
@{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}, @{thm pair_collapse}] |
|
1572 |
val unfolddef_tac = Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1 |
|
1573 |
val introthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ param_names' @ ["y"]) [] introtrm (fn {...} => unfolddef_tac) |
|
1574 |
val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)); |
|
1575 |
val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P) |
|
1576 |
val elimthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ param_names' @ ["y", "P"]) [] elimtrm (fn {...} => unfolddef_tac) |
|
1577 |
in |
|
1578 |
(introthm, elimthm) |
|
1579 |
end; |
|
1580 |
||
1581 |
fun create_constname_of_mode thy prefix name mode = |
|
1582 |
let |
|
1583 |
fun string_of_mode mode = if null mode then "0" |
|
1584 |
else space_implode "_" (map (fn (i, NONE) => string_of_int i | (i, SOME pis) => string_of_int i ^ "p" |
|
1585 |
^ space_implode "p" (map string_of_int pis)) mode) |
|
1586 |
val HOmode = space_implode "_and_" |
|
1587 |
(fold (fn NONE => I | SOME mode => cons (string_of_mode mode)) (fst mode) []) |
|
1588 |
in |
|
1589 |
(Sign.full_bname thy (prefix ^ (Long_Name.base_name name))) ^ |
|
1590 |
(if HOmode = "" then "_" else "_for_" ^ HOmode ^ "_yields_") ^ (string_of_mode (snd mode)) |
|
1591 |
end; |
|
1592 |
||
1593 |
fun split_tupleT is T = |
|
1594 |
let |
|
1595 |
fun split_tuple' _ _ [] = ([], []) |
|
1596 |
| split_tuple' is i (T::Ts) = |
|
1597 |
(if i mem is then apfst else apsnd) (cons T) |
|
1598 |
(split_tuple' is (i+1) Ts) |
|
1599 |
in |
|
1600 |
split_tuple' is 1 (HOLogic.strip_tupleT T) |
|
1601 |
end |
|
1602 |
||
1603 |
fun mk_arg xin xout pis T = |
|
1604 |
let |
|
1605 |
val n = length (HOLogic.strip_tupleT T) |
|
1606 |
val ni = length pis |
|
1607 |
fun mk_proj i j t = |
|
1608 |
(if i = j then I else HOLogic.mk_fst) |
|
1609 |
(funpow (i - 1) HOLogic.mk_snd t) |
|
1610 |
fun mk_arg' i (si, so) = if i mem pis then |
|
1611 |
(mk_proj si ni xin, (si+1, so)) |
|
1612 |
else |
|
1613 |
(mk_proj so (n - ni) xout, (si, so+1)) |
|
1614 |
val (args, _) = fold_map mk_arg' (1 upto n) (1, 1) |
|
1615 |
in |
|
1616 |
HOLogic.mk_tuple args |
|
1617 |
end |
|
1618 |
||
1619 |
fun create_definitions preds (name, modes) thy = |
|
1620 |
let |
|
1621 |
val compfuns = PredicateCompFuns.compfuns |
|
1622 |
val T = AList.lookup (op =) preds name |> the |
|
1623 |
fun create_definition (mode as (iss, is)) thy = let |
|
1624 |
val mode_cname = create_constname_of_mode thy "" name mode |
|
1625 |
val mode_cbasename = Long_Name.base_name mode_cname |
|
1626 |
val Ts = binder_types T |
|
1627 |
val (Ts1, Ts2) = chop (length iss) Ts |
|
1628 |
val (Us1, Us2) = split_smodeT is Ts2 |
|
1629 |
val Ts1' = map2 (fn NONE => I | SOME is => funT_of compfuns ([], is)) iss Ts1 |
|
1630 |
val funT = (Ts1' @ Us1) ---> (mk_predT compfuns (mk_tupleT Us2)) |
|
1631 |
val names = Name.variant_list [] |
|
1632 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts))); |
|
1633 |
(* old *) |
|
1634 |
(* |
|
1635 |
val xs = map Free (names ~~ (Ts1' @ Ts2)) |
|
1636 |
val (xparams, xargs) = chop (length iss) xs |
|
1637 |
val (xins, xouts) = split_smode is xargs |
|
1638 |
*) |
|
1639 |
(* new *) |
|
1640 |
val param_names = Name.variant_list [] |
|
1641 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts1'))) |
|
1642 |
val xparams = map Free (param_names ~~ Ts1') |
|
1643 |
fun mk_vars (i, T) names = |
|
1644 |
let |
|
1645 |
val vname = Name.variant names ("x" ^ string_of_int (length Ts1' + i)) |
|
1646 |
in |
|
1647 |
case AList.lookup (op =) is i of |
|
1648 |
NONE => ((([], [Free (vname, T)]), Free (vname, T)), vname :: names) |
|
1649 |
| SOME NONE => ((([Free (vname, T)], []), Free (vname, T)), vname :: names) |
|
1650 |
| SOME (SOME pis) => |
|
1651 |
let |
|
1652 |
val (Tins, Touts) = split_tupleT pis T |
|
1653 |
val name_in = Name.variant names ("x" ^ string_of_int (length Ts1' + i) ^ "in") |
|
1654 |
val name_out = Name.variant names ("x" ^ string_of_int (length Ts1' + i) ^ "out") |
|
1655 |
val xin = Free (name_in, HOLogic.mk_tupleT Tins) |
|
1656 |
val xout = Free (name_out, HOLogic.mk_tupleT Touts) |
|
1657 |
val xarg = mk_arg xin xout pis T |
|
1658 |
in (((if null Tins then [] else [xin], if null Touts then [] else [xout]), xarg), name_in :: name_out :: names) end |
|
32669
462b1dd67a58
added context free grammar example; removed dead code; adapted to work without quick and dirty mode; fixed typo
bulwahn
parents:
32668
diff
changeset
|
1659 |
end |
32667 | 1660 |
val (xinoutargs, names) = fold_map mk_vars ((1 upto (length Ts2)) ~~ Ts2) param_names |
1661 |
val (xinout, xargs) = split_list xinoutargs |
|
1662 |
val (xins, xouts) = pairself flat (split_list xinout) |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1663 |
val (xparams', names') = fold_map (mk_Eval_of NONE) ((xparams ~~ Ts1) ~~ iss) names |
32667 | 1664 |
fun mk_split_lambda [] t = lambda (Free (Name.variant names' "x", HOLogic.unitT)) t |
1665 |
| mk_split_lambda [x] t = lambda x t |
|
1666 |
| mk_split_lambda xs t = |
|
1667 |
let |
|
1668 |
fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t)) |
|
1669 |
| mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t)) |
|
1670 |
in |
|
1671 |
mk_split_lambda' xs t |
|
1672 |
end; |
|
1673 |
val predterm = PredicateCompFuns.mk_Enum (mk_split_lambda xouts |
|
1674 |
(list_comb (Const (name, T), xparams' @ xargs))) |
|
1675 |
val lhs = list_comb (Const (mode_cname, funT), xparams @ xins) |
|
1676 |
val def = Logic.mk_equals (lhs, predterm) |
|
1677 |
val ([definition], thy') = thy |> |
|
1678 |
Sign.add_consts_i [(Binding.name mode_cbasename, funT, NoSyn)] |> |
|
1679 |
PureThy.add_defs false [((Binding.name (mode_cbasename ^ "_def"), def), [])] |
|
1680 |
val (intro, elim) = |
|
1681 |
create_intro_elim_rule mode definition mode_cname funT (Const (name, T)) thy' |
|
1682 |
in thy' |
|
1683 |
|> add_predfun name mode (mode_cname, definition, intro, elim) |
|
1684 |
|> PureThy.store_thm (Binding.name (mode_cbasename ^ "I"), intro) |> snd |
|
1685 |
|> PureThy.store_thm (Binding.name (mode_cbasename ^ "E"), elim) |> snd |
|
1686 |
|> Theory.checkpoint |
|
1687 |
end; |
|
1688 |
in |
|
1689 |
fold create_definition modes thy |
|
1690 |
end; |
|
1691 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1692 |
fun create_definitions_of_depth_limited_functions preds (name, modes) thy = |
32667 | 1693 |
let |
1694 |
val T = AList.lookup (op =) preds name |> the |
|
1695 |
fun create_definition mode thy = |
|
1696 |
let |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1697 |
val mode_cname = create_constname_of_mode thy "depth_limited_" name mode |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1698 |
val funT = depth_limited_funT_of PredicateCompFuns.compfuns mode T |
32667 | 1699 |
in |
1700 |
thy |> Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_cname), funT, NoSyn)] |
|
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
1701 |
|> set_depth_limited_function_name name mode mode_cname |
32667 | 1702 |
end; |
1703 |
in |
|
1704 |
fold create_definition modes thy |
|
1705 |
end; |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1706 |
|
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1707 |
fun generator_funT_of (iss, is) T = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1708 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1709 |
val Ts = binder_types T |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1710 |
val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
1711 |
val paramTs' = map2 (fn SOME is => generator_funT_of ([], is) | NONE => I) iss paramTs |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1712 |
in |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
1713 |
(paramTs' @ inargTs @ [@{typ code_numeral}]) ---> (mk_predT RPredCompFuns.compfuns (mk_tupleT outargTs)) |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1714 |
end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1715 |
|
32667 | 1716 |
fun rpred_create_definitions preds (name, modes) thy = |
1717 |
let |
|
1718 |
val T = AList.lookup (op =) preds name |> the |
|
1719 |
fun create_definition mode thy = |
|
1720 |
let |
|
1721 |
val mode_cname = create_constname_of_mode thy "gen_" name mode |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
1722 |
val funT = generator_funT_of mode T |
32667 | 1723 |
in |
1724 |
thy |> Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_cname), funT, NoSyn)] |
|
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
1725 |
|> set_generator_name name mode mode_cname |
32667 | 1726 |
end; |
1727 |
in |
|
1728 |
fold create_definition modes thy |
|
1729 |
end; |
|
1730 |
||
1731 |
(* Proving equivalence of term *) |
|
1732 |
||
1733 |
fun is_Type (Type _) = true |
|
1734 |
| is_Type _ = false |
|
1735 |
||
1736 |
(* returns true if t is an application of an datatype constructor *) |
|
1737 |
(* which then consequently would be splitted *) |
|
1738 |
(* else false *) |
|
1739 |
fun is_constructor thy t = |
|
1740 |
if (is_Type (fastype_of t)) then |
|
1741 |
(case Datatype.get_info thy ((fst o dest_Type o fastype_of) t) of |
|
1742 |
NONE => false |
|
1743 |
| SOME info => (let |
|
1744 |
val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info) |
|
1745 |
val (c, _) = strip_comb t |
|
1746 |
in (case c of |
|
1747 |
Const (name, _) => name mem_string constr_consts |
|
1748 |
| _ => false) end)) |
|
1749 |
else false |
|
1750 |
||
1751 |
(* MAJOR FIXME: prove_params should be simple |
|
1752 |
- different form of introrule for parameters ? *) |
|
1753 |
fun prove_param thy (NONE, t) = TRY (rtac @{thm refl} 1) |
|
1754 |
| prove_param thy (m as SOME (Mode (mode, is, ms)), t) = |
|
1755 |
let |
|
1756 |
val (f, args) = strip_comb (Envir.eta_contract t) |
|
1757 |
val (params, _) = chop (length ms) args |
|
1758 |
val f_tac = case f of |
|
1759 |
Const (name, T) => simp_tac (HOL_basic_ss addsimps |
|
1760 |
([@{thm eval_pred}, (predfun_definition_of thy name mode), |
|
1761 |
@{thm "split_eta"}, @{thm "split_beta"}, @{thm "fst_conv"}, |
|
1762 |
@{thm "snd_conv"}, @{thm pair_collapse}, @{thm "Product_Type.split_conv"}])) 1 |
|
1763 |
| Free _ => TRY (rtac @{thm refl} 1) |
|
1764 |
| Abs _ => error "prove_param: No valid parameter term" |
|
1765 |
in |
|
1766 |
REPEAT_DETERM (etac @{thm thin_rl} 1) |
|
1767 |
THEN REPEAT_DETERM (rtac @{thm ext} 1) |
|
1768 |
THEN print_tac "prove_param" |
|
1769 |
THEN f_tac |
|
1770 |
THEN print_tac "after simplification in prove_args" |
|
1771 |
THEN (EVERY (map (prove_param thy) (ms ~~ params))) |
|
1772 |
THEN (REPEAT_DETERM (atac 1)) |
|
1773 |
end |
|
1774 |
||
1775 |
fun prove_expr thy (Mode (mode, is, ms), t, us) (premposition : int) = |
|
1776 |
case strip_comb t of |
|
1777 |
(Const (name, T), args) => |
|
1778 |
let |
|
1779 |
val introrule = predfun_intro_of thy name mode |
|
1780 |
val (args1, args2) = chop (length ms) args |
|
1781 |
in |
|
1782 |
rtac @{thm bindI} 1 |
|
1783 |
THEN print_tac "before intro rule:" |
|
1784 |
(* for the right assumption in first position *) |
|
1785 |
THEN rotate_tac premposition 1 |
|
1786 |
THEN debug_tac (Display.string_of_thm (ProofContext.init thy) introrule) |
|
1787 |
THEN rtac introrule 1 |
|
1788 |
THEN print_tac "after intro rule" |
|
1789 |
(* work with parameter arguments *) |
|
1790 |
THEN (atac 1) |
|
1791 |
THEN (print_tac "parameter goal") |
|
1792 |
THEN (EVERY (map (prove_param thy) (ms ~~ args1))) |
|
1793 |
THEN (REPEAT_DETERM (atac 1)) |
|
1794 |
end |
|
1795 |
| _ => rtac @{thm bindI} 1 |
|
1796 |
THEN asm_full_simp_tac |
|
1797 |
(HOL_basic_ss' addsimps [@{thm "split_eta"}, @{thm "split_beta"}, @{thm "fst_conv"}, |
|
1798 |
@{thm "snd_conv"}, @{thm pair_collapse}]) 1 |
|
1799 |
THEN (atac 1) |
|
1800 |
THEN print_tac "after prove parameter call" |
|
1801 |
||
1802 |
||
1803 |
fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st; |
|
1804 |
||
1805 |
fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st |
|
1806 |
||
1807 |
fun prove_match thy (out_ts : term list) = let |
|
1808 |
fun get_case_rewrite t = |
|
1809 |
if (is_constructor thy t) then let |
|
1810 |
val case_rewrites = (#case_rewrites (Datatype.the_info thy |
|
1811 |
((fst o dest_Type o fastype_of) t))) |
|
1812 |
in case_rewrites @ (flat (map get_case_rewrite (snd (strip_comb t)))) end |
|
1813 |
else [] |
|
1814 |
val simprules = @{thm "unit.cases"} :: @{thm "prod.cases"} :: (flat (map get_case_rewrite out_ts)) |
|
1815 |
(* replace TRY by determining if it necessary - are there equations when calling compile match? *) |
|
1816 |
in |
|
1817 |
(* make this simpset better! *) |
|
1818 |
asm_full_simp_tac (HOL_basic_ss' addsimps simprules) 1 |
|
1819 |
THEN print_tac "after prove_match:" |
|
1820 |
THEN (DETERM (TRY (EqSubst.eqsubst_tac (ProofContext.init thy) [0] [@{thm "HOL.if_P"}] 1 |
|
1821 |
THEN (REPEAT_DETERM (rtac @{thm conjI} 1 THEN (SOLVED (asm_simp_tac HOL_basic_ss 1)))) |
|
1822 |
THEN (SOLVED (asm_simp_tac HOL_basic_ss 1))))) |
|
1823 |
THEN print_tac "after if simplification" |
|
1824 |
end; |
|
1825 |
||
1826 |
(* corresponds to compile_fun -- maybe call that also compile_sidecond? *) |
|
1827 |
||
1828 |
fun prove_sidecond thy modes t = |
|
1829 |
let |
|
1830 |
fun preds_of t nameTs = case strip_comb t of |
|
1831 |
(f as Const (name, T), args) => |
|
1832 |
if AList.defined (op =) modes name then (name, T) :: nameTs |
|
1833 |
else fold preds_of args nameTs |
|
1834 |
| _ => nameTs |
|
1835 |
val preds = preds_of t [] |
|
1836 |
val defs = map |
|
1837 |
(fn (pred, T) => predfun_definition_of thy pred |
|
1838 |
([], map (rpair NONE) (1 upto (length (binder_types T))))) |
|
1839 |
preds |
|
1840 |
in |
|
1841 |
(* remove not_False_eq_True when simpset in prove_match is better *) |
|
1842 |
simp_tac (HOL_basic_ss addsimps |
|
1843 |
(@{thms "HOL.simp_thms"} @ (@{thm not_False_eq_True} :: @{thm eval_pred} :: defs))) 1 |
|
1844 |
(* need better control here! *) |
|
1845 |
end |
|
1846 |
||
1847 |
fun prove_clause thy nargs modes (iss, is) (_, clauses) (ts, moded_ps) = |
|
1848 |
let |
|
1849 |
val (in_ts, clause_out_ts) = split_smode is ts; |
|
1850 |
fun prove_prems out_ts [] = |
|
1851 |
(prove_match thy out_ts) |
|
1852 |
THEN print_tac "before simplifying assumptions" |
|
1853 |
THEN asm_full_simp_tac HOL_basic_ss' 1 |
|
1854 |
THEN print_tac "before single intro rule" |
|
1855 |
THEN (rtac (if null clause_out_ts then @{thm singleI_unit} else @{thm singleI}) 1) |
|
1856 |
| prove_prems out_ts ((p, mode as Mode ((iss, is), _, param_modes)) :: ps) = |
|
1857 |
let |
|
1858 |
val premposition = (find_index (equal p) clauses) + nargs |
|
1859 |
val rest_tac = (case p of Prem (us, t) => |
|
1860 |
let |
|
1861 |
val (_, out_ts''') = split_smode is us |
|
1862 |
val rec_tac = prove_prems out_ts''' ps |
|
1863 |
in |
|
1864 |
print_tac "before clause:" |
|
1865 |
THEN asm_simp_tac HOL_basic_ss 1 |
|
1866 |
THEN print_tac "before prove_expr:" |
|
1867 |
THEN prove_expr thy (mode, t, us) premposition |
|
1868 |
THEN print_tac "after prove_expr:" |
|
1869 |
THEN rec_tac |
|
1870 |
end |
|
1871 |
| Negprem (us, t) => |
|
1872 |
let |
|
1873 |
val (_, out_ts''') = split_smode is us |
|
1874 |
val rec_tac = prove_prems out_ts''' ps |
|
1875 |
val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE) |
|
1876 |
val (_, params) = strip_comb t |
|
1877 |
in |
|
1878 |
rtac @{thm bindI} 1 |
|
1879 |
THEN (if (is_some name) then |
|
1880 |
simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, is)]) 1 |
|
1881 |
THEN rtac @{thm not_predI} 1 |
|
1882 |
THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1 |
|
1883 |
THEN (REPEAT_DETERM (atac 1)) |
|
1884 |
(* FIXME: work with parameter arguments *) |
|
1885 |
THEN (EVERY (map (prove_param thy) (param_modes ~~ params))) |
|
1886 |
else |
|
1887 |
rtac @{thm not_predI'} 1) |
|
1888 |
THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1 |
|
1889 |
THEN rec_tac |
|
1890 |
end |
|
1891 |
| Sidecond t => |
|
1892 |
rtac @{thm bindI} 1 |
|
1893 |
THEN rtac @{thm if_predI} 1 |
|
1894 |
THEN print_tac "before sidecond:" |
|
1895 |
THEN prove_sidecond thy modes t |
|
1896 |
THEN print_tac "after sidecond:" |
|
1897 |
THEN prove_prems [] ps) |
|
1898 |
in (prove_match thy out_ts) |
|
1899 |
THEN rest_tac |
|
1900 |
end; |
|
1901 |
val prems_tac = prove_prems in_ts moded_ps |
|
1902 |
in |
|
1903 |
rtac @{thm bindI} 1 |
|
1904 |
THEN rtac @{thm singleI} 1 |
|
1905 |
THEN prems_tac |
|
1906 |
end; |
|
1907 |
||
1908 |
fun select_sup 1 1 = [] |
|
1909 |
| select_sup _ 1 = [rtac @{thm supI1}] |
|
1910 |
| select_sup n i = (rtac @{thm supI2})::(select_sup (n - 1) (i - 1)); |
|
1911 |
||
33128 | 1912 |
fun prove_one_direction options thy clauses preds modes pred mode moded_clauses = |
32667 | 1913 |
let |
1914 |
val T = the (AList.lookup (op =) preds pred) |
|
1915 |
val nargs = length (binder_types T) - nparams_of thy pred |
|
1916 |
val pred_case_rule = the_elim_of thy pred |
|
1917 |
in |
|
1918 |
REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})) |
|
33128 | 1919 |
THEN print_tac' options "before applying elim rule" |
32667 | 1920 |
THEN etac (predfun_elim_of thy pred mode) 1 |
1921 |
THEN etac pred_case_rule 1 |
|
1922 |
THEN (EVERY (map |
|
1923 |
(fn i => EVERY' (select_sup (length moded_clauses) i) i) |
|
1924 |
(1 upto (length moded_clauses)))) |
|
1925 |
THEN (EVERY (map2 (prove_clause thy nargs modes mode) clauses moded_clauses)) |
|
1926 |
THEN print_tac "proved one direction" |
|
1927 |
end; |
|
1928 |
||
1929 |
(** Proof in the other direction **) |
|
1930 |
||
1931 |
fun prove_match2 thy out_ts = let |
|
1932 |
fun split_term_tac (Free _) = all_tac |
|
1933 |
| split_term_tac t = |
|
1934 |
if (is_constructor thy t) then let |
|
1935 |
val info = Datatype.the_info thy ((fst o dest_Type o fastype_of) t) |
|
1936 |
val num_of_constrs = length (#case_rewrites info) |
|
1937 |
(* special treatment of pairs -- because of fishing *) |
|
1938 |
val split_rules = case (fst o dest_Type o fastype_of) t of |
|
1939 |
"*" => [@{thm prod.split_asm}] |
|
1940 |
| _ => PureThy.get_thms thy (((fst o dest_Type o fastype_of) t) ^ ".split_asm") |
|
1941 |
val (_, ts) = strip_comb t |
|
1942 |
in |
|
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1943 |
(print_tac ("Term " ^ (Syntax.string_of_term_global thy t) ^ |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1944 |
"splitting with rules \n" ^ |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1945 |
commas (map (Display.string_of_thm_global thy) split_rules))) |
33115
f765c3234059
changed proof method to handle widen predicate in JinjaThreads
bulwahn
parents:
33114
diff
changeset
|
1946 |
THEN TRY ((Splitter.split_asm_tac split_rules 1) |
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1947 |
THEN (print_tac "after splitting with split_asm rules") |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1948 |
(* THEN (Simplifier.asm_full_simp_tac HOL_basic_ss 1) |
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1949 |
THEN (DETERM (TRY (etac @{thm Pair_inject} 1)))*) |
33115
f765c3234059
changed proof method to handle widen predicate in JinjaThreads
bulwahn
parents:
33114
diff
changeset
|
1950 |
THEN (REPEAT_DETERM_N (num_of_constrs - 1) (etac @{thm botE} 1 ORELSE etac @{thm botE} 2))) |
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33106
diff
changeset
|
1951 |
THEN (assert_tac (Max_number_of_subgoals 2)) |
32667 | 1952 |
THEN (EVERY (map split_term_tac ts)) |
1953 |
end |
|
1954 |
else all_tac |
|
1955 |
in |
|
1956 |
split_term_tac (mk_tuple out_ts) |
|
1957 |
THEN (DETERM (TRY ((Splitter.split_asm_tac [@{thm "split_if_asm"}] 1) THEN (etac @{thm botE} 2)))) |
|
1958 |
end |
|
1959 |
||
1960 |
(* VERY LARGE SIMILIRATIY to function prove_param |
|
1961 |
-- join both functions |
|
1962 |
*) |
|
1963 |
(* TODO: remove function *) |
|
1964 |
||
1965 |
fun prove_param2 thy (NONE, t) = all_tac |
|
1966 |
| prove_param2 thy (m as SOME (Mode (mode, is, ms)), t) = let |
|
1967 |
val (f, args) = strip_comb (Envir.eta_contract t) |
|
1968 |
val (params, _) = chop (length ms) args |
|
1969 |
val f_tac = case f of |
|
1970 |
Const (name, T) => full_simp_tac (HOL_basic_ss addsimps |
|
1971 |
(@{thm eval_pred}::(predfun_definition_of thy name mode) |
|
1972 |
:: @{thm "Product_Type.split_conv"}::[])) 1 |
|
1973 |
| Free _ => all_tac |
|
1974 |
| _ => error "prove_param2: illegal parameter term" |
|
1975 |
in |
|
1976 |
print_tac "before simplification in prove_args:" |
|
1977 |
THEN f_tac |
|
1978 |
THEN print_tac "after simplification in prove_args" |
|
1979 |
THEN (EVERY (map (prove_param2 thy) (ms ~~ params))) |
|
1980 |
end |
|
1981 |
||
1982 |
||
1983 |
fun prove_expr2 thy (Mode (mode, is, ms), t) = |
|
1984 |
(case strip_comb t of |
|
1985 |
(Const (name, T), args) => |
|
1986 |
etac @{thm bindE} 1 |
|
1987 |
THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))) |
|
1988 |
THEN print_tac "prove_expr2-before" |
|
1989 |
THEN (debug_tac (Syntax.string_of_term_global thy |
|
1990 |
(prop_of (predfun_elim_of thy name mode)))) |
|
1991 |
THEN (etac (predfun_elim_of thy name mode) 1) |
|
1992 |
THEN print_tac "prove_expr2" |
|
1993 |
THEN (EVERY (map (prove_param2 thy) (ms ~~ args))) |
|
1994 |
THEN print_tac "finished prove_expr2" |
|
1995 |
| _ => etac @{thm bindE} 1) |
|
1996 |
||
1997 |
(* FIXME: what is this for? *) |
|
1998 |
(* replace defined by has_mode thy pred *) |
|
1999 |
(* TODO: rewrite function *) |
|
2000 |
fun prove_sidecond2 thy modes t = let |
|
2001 |
fun preds_of t nameTs = case strip_comb t of |
|
2002 |
(f as Const (name, T), args) => |
|
2003 |
if AList.defined (op =) modes name then (name, T) :: nameTs |
|
2004 |
else fold preds_of args nameTs |
|
2005 |
| _ => nameTs |
|
2006 |
val preds = preds_of t [] |
|
2007 |
val defs = map |
|
2008 |
(fn (pred, T) => predfun_definition_of thy pred |
|
2009 |
([], map (rpair NONE) (1 upto (length (binder_types T))))) |
|
2010 |
preds |
|
2011 |
in |
|
2012 |
(* only simplify the one assumption *) |
|
2013 |
full_simp_tac (HOL_basic_ss' addsimps @{thm eval_pred} :: defs) 1 |
|
2014 |
(* need better control here! *) |
|
2015 |
THEN print_tac "after sidecond2 simplification" |
|
2016 |
end |
|
2017 |
||
2018 |
fun prove_clause2 thy modes pred (iss, is) (ts, ps) i = |
|
2019 |
let |
|
2020 |
val pred_intro_rule = nth (intros_of thy pred) (i - 1) |
|
2021 |
val (in_ts, clause_out_ts) = split_smode is ts; |
|
2022 |
fun prove_prems2 out_ts [] = |
|
2023 |
print_tac "before prove_match2 - last call:" |
|
2024 |
THEN prove_match2 thy out_ts |
|
2025 |
THEN print_tac "after prove_match2 - last call:" |
|
2026 |
THEN (etac @{thm singleE} 1) |
|
2027 |
THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1)) |
|
2028 |
THEN (asm_full_simp_tac HOL_basic_ss' 1) |
|
2029 |
THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1)) |
|
2030 |
THEN (asm_full_simp_tac HOL_basic_ss' 1) |
|
2031 |
THEN SOLVED (print_tac "state before applying intro rule:" |
|
2032 |
THEN (rtac pred_intro_rule 1) |
|
2033 |
(* How to handle equality correctly? *) |
|
2034 |
THEN (print_tac "state before assumption matching") |
|
2035 |
THEN (REPEAT (atac 1 ORELSE |
|
2036 |
(CHANGED (asm_full_simp_tac (HOL_basic_ss' addsimps |
|
2037 |
[@{thm split_eta}, @{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}, @{thm pair_collapse}]) 1) |
|
2038 |
THEN print_tac "state after simp_tac:")))) |
|
2039 |
| prove_prems2 out_ts ((p, mode as Mode ((iss, is), _, param_modes)) :: ps) = |
|
2040 |
let |
|
2041 |
val rest_tac = (case p of |
|
2042 |
Prem (us, t) => |
|
2043 |
let |
|
2044 |
val (_, out_ts''') = split_smode is us |
|
2045 |
val rec_tac = prove_prems2 out_ts''' ps |
|
2046 |
in |
|
2047 |
(prove_expr2 thy (mode, t)) THEN rec_tac |
|
2048 |
end |
|
2049 |
| Negprem (us, t) => |
|
2050 |
let |
|
2051 |
val (_, out_ts''') = split_smode is us |
|
2052 |
val rec_tac = prove_prems2 out_ts''' ps |
|
2053 |
val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE) |
|
2054 |
val (_, params) = strip_comb t |
|
2055 |
in |
|
2056 |
print_tac "before neg prem 2" |
|
2057 |
THEN etac @{thm bindE} 1 |
|
2058 |
THEN (if is_some name then |
|
2059 |
full_simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, is)]) 1 |
|
2060 |
THEN etac @{thm not_predE} 1 |
|
2061 |
THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1 |
|
2062 |
THEN (EVERY (map (prove_param2 thy) (param_modes ~~ params))) |
|
2063 |
else |
|
2064 |
etac @{thm not_predE'} 1) |
|
2065 |
THEN rec_tac |
|
2066 |
end |
|
2067 |
| Sidecond t => |
|
2068 |
etac @{thm bindE} 1 |
|
2069 |
THEN etac @{thm if_predE} 1 |
|
2070 |
THEN prove_sidecond2 thy modes t |
|
2071 |
THEN prove_prems2 [] ps) |
|
2072 |
in print_tac "before prove_match2:" |
|
2073 |
THEN prove_match2 thy out_ts |
|
2074 |
THEN print_tac "after prove_match2:" |
|
2075 |
THEN rest_tac |
|
2076 |
end; |
|
2077 |
val prems_tac = prove_prems2 in_ts ps |
|
2078 |
in |
|
2079 |
print_tac "starting prove_clause2" |
|
2080 |
THEN etac @{thm bindE} 1 |
|
2081 |
THEN (etac @{thm singleE'} 1) |
|
2082 |
THEN (TRY (etac @{thm Pair_inject} 1)) |
|
2083 |
THEN print_tac "after singleE':" |
|
2084 |
THEN prems_tac |
|
2085 |
end; |
|
2086 |
||
2087 |
fun prove_other_direction thy modes pred mode moded_clauses = |
|
2088 |
let |
|
2089 |
fun prove_clause clause i = |
|
2090 |
(if i < length moded_clauses then etac @{thm supE} 1 else all_tac) |
|
2091 |
THEN (prove_clause2 thy modes pred mode clause i) |
|
2092 |
in |
|
2093 |
(DETERM (TRY (rtac @{thm unit.induct} 1))) |
|
2094 |
THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all}))) |
|
2095 |
THEN (rtac (predfun_intro_of thy pred mode) 1) |
|
2096 |
THEN (REPEAT_DETERM (rtac @{thm refl} 2)) |
|
2097 |
THEN (EVERY (map2 prove_clause moded_clauses (1 upto (length moded_clauses)))) |
|
2098 |
end; |
|
2099 |
||
2100 |
(** proof procedure **) |
|
2101 |
||
33127 | 2102 |
fun prove_pred options thy clauses preds modes pred mode (moded_clauses, compiled_term) = |
32667 | 2103 |
let |
2104 |
val ctxt = ProofContext.init thy |
|
2105 |
val clauses = the (AList.lookup (op =) clauses pred) |
|
2106 |
in |
|
2107 |
Goal.prove ctxt (Term.add_free_names compiled_term []) [] compiled_term |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2108 |
(if not (skip_proof options) then |
32667 | 2109 |
(fn _ => |
2110 |
rtac @{thm pred_iffI} 1 |
|
33127 | 2111 |
THEN print_tac' options "after pred_iffI" |
33128 | 2112 |
THEN prove_one_direction options thy clauses preds modes pred mode moded_clauses |
33127 | 2113 |
THEN print_tac' options "proved one direction" |
32667 | 2114 |
THEN prove_other_direction thy modes pred mode moded_clauses |
33127 | 2115 |
THEN print_tac' options "proved other direction") |
32673
d5db9cf85401
replaced sorry by oops; removed old debug functions in predicate compiler
bulwahn
parents:
32672
diff
changeset
|
2116 |
else (fn _ => setmp quick_and_dirty true SkipProof.cheat_tac thy)) |
32667 | 2117 |
end; |
2118 |
||
2119 |
(* composition of mode inference, definition, compilation and proof *) |
|
2120 |
||
2121 |
(** auxillary combinators for table of preds and modes **) |
|
2122 |
||
2123 |
fun map_preds_modes f preds_modes_table = |
|
2124 |
map (fn (pred, modes) => |
|
2125 |
(pred, map (fn (mode, value) => (mode, f pred mode value)) modes)) preds_modes_table |
|
2126 |
||
2127 |
fun join_preds_modes table1 table2 = |
|
2128 |
map_preds_modes (fn pred => fn mode => fn value => |
|
2129 |
(value, the (AList.lookup (op =) (the (AList.lookup (op =) table2 pred)) mode))) table1 |
|
2130 |
||
2131 |
fun maps_modes preds_modes_table = |
|
2132 |
map (fn (pred, modes) => |
|
2133 |
(pred, map (fn (mode, value) => value) modes)) preds_modes_table |
|
2134 |
||
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2135 |
fun compile_preds comp_modifiers compfuns thy all_vs param_vs preds moded_clauses = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2136 |
map_preds_modes (fn pred => compile_pred comp_modifiers compfuns thy all_vs param_vs pred |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2137 |
(the (AList.lookup (op =) preds pred))) moded_clauses |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2138 |
|
33127 | 2139 |
fun prove options thy clauses preds modes moded_clauses compiled_terms = |
2140 |
map_preds_modes (prove_pred options thy clauses preds modes) |
|
32667 | 2141 |
(join_preds_modes moded_clauses compiled_terms) |
2142 |
||
33127 | 2143 |
fun prove_by_skip options thy _ _ _ _ compiled_terms = |
32669
462b1dd67a58
added context free grammar example; removed dead code; adapted to work without quick and dirty mode; fixed typo
bulwahn
parents:
32668
diff
changeset
|
2144 |
map_preds_modes (fn pred => fn mode => fn t => Drule.standard (setmp quick_and_dirty true (SkipProof.make_thm thy) t)) |
32667 | 2145 |
compiled_terms |
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2146 |
|
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2147 |
fun dest_prem thy params t = |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2148 |
(case strip_comb t of |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2149 |
(v as Free _, ts) => if v mem params then Prem (ts, v) else Sidecond t |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2150 |
| (c as Const (@{const_name Not}, _), [t]) => (case dest_prem thy params t of |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2151 |
Prem (ts, t) => Negprem (ts, t) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2152 |
| Negprem _ => error ("Double negation not allowed in premise: " ^ (Syntax.string_of_term_global thy (c $ t))) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2153 |
| Sidecond t => Sidecond (c $ t)) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2154 |
| (c as Const (s, _), ts) => |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2155 |
if is_registered thy s then |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2156 |
let val (ts1, ts2) = chop (nparams_of thy s) ts |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2157 |
in Prem (ts2, list_comb (c, ts1)) end |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2158 |
else Sidecond t |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2159 |
| _ => Sidecond t) |
32667 | 2160 |
|
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2161 |
fun prepare_intrs thy prednames intros = |
32667 | 2162 |
let |
33126
bb8806eb5da7
importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents:
33124
diff
changeset
|
2163 |
val intrs = map prop_of intros |
32667 | 2164 |
val nparams = nparams_of thy (hd prednames) |
33126
bb8806eb5da7
importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents:
33124
diff
changeset
|
2165 |
val preds = distinct (fn ((c1, _), (c2, _)) => c1 = c2) (map (dest_Const o fst o (strip_intro_concl nparams)) intrs) |
bb8806eb5da7
importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents:
33124
diff
changeset
|
2166 |
val (preds, intrs) = unify_consts thy (map Const preds) intrs |
bb8806eb5da7
importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents:
33124
diff
changeset
|
2167 |
val ([preds, intrs], _) = fold_burrow (Variable.import_terms false) [preds, intrs] (ProofContext.init thy) |
bb8806eb5da7
importing of polymorphic introduction rules with different schematic variable names
bulwahn
parents:
33124
diff
changeset
|
2168 |
val preds = map dest_Const preds |
32667 | 2169 |
val extra_modes = all_modes_of thy |> filter_out (fn (name, _) => member (op =) prednames name) |
2170 |
val _ $ u = Logic.strip_imp_concl (hd intrs); |
|
2171 |
val params = List.take (snd (strip_comb u), nparams); |
|
2172 |
val param_vs = maps term_vs params |
|
2173 |
val all_vs = terms_vs intrs |
|
2174 |
fun add_clause intr (clauses, arities) = |
|
2175 |
let |
|
2176 |
val _ $ t = Logic.strip_imp_concl intr; |
|
2177 |
val (Const (name, T), ts) = strip_comb t; |
|
2178 |
val (ts1, ts2) = chop nparams ts; |
|
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2179 |
val prems = map (dest_prem thy params o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr); |
32667 | 2180 |
val (Ts, Us) = chop nparams (binder_types T) |
2181 |
in |
|
2182 |
(AList.update op = (name, these (AList.lookup op = clauses name) @ |
|
2183 |
[(ts2, prems)]) clauses, |
|
2184 |
AList.update op = (name, (map (fn U => (case strip_type U of |
|
2185 |
(Rs as _ :: _, Type ("bool", [])) => SOME (length Rs) |
|
2186 |
| _ => NONE)) Ts, |
|
2187 |
length Us)) arities) |
|
2188 |
end; |
|
2189 |
val (clauses, arities) = fold add_clause intrs ([], []); |
|
2190 |
fun modes_of_arities arities = |
|
2191 |
(map (fn (s, (ks, k)) => (s, cprod (cprods (map |
|
2192 |
(fn NONE => [NONE] |
|
2193 |
| SOME k' => map SOME (map (map (rpair NONE)) (subsets 1 k'))) ks), |
|
2194 |
map (map (rpair NONE)) (subsets 1 k)))) arities) |
|
2195 |
fun modes_of_typ T = |
|
2196 |
let |
|
2197 |
val (Ts, Us) = chop nparams (binder_types T) |
|
2198 |
fun all_smodes_of_typs Ts = cprods_subset ( |
|
2199 |
map_index (fn (i, U) => |
|
2200 |
case HOLogic.strip_tupleT U of |
|
2201 |
[] => [(i + 1, NONE)] |
|
2202 |
| [U] => [(i + 1, NONE)] |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2203 |
| Us => (i + 1, NONE) :: |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2204 |
(map (pair (i + 1) o SOME) ((subsets 1 (length Us)) \\ [[], 1 upto (length Us)]))) |
32667 | 2205 |
Ts) |
2206 |
in |
|
2207 |
cprod (cprods (map (fn T => case strip_type T of |
|
2208 |
(Rs as _ :: _, Type ("bool", [])) => map SOME (all_smodes_of_typs Rs) | _ => [NONE]) Ts), |
|
2209 |
all_smodes_of_typs Us) |
|
2210 |
end |
|
2211 |
val all_modes = map (fn (s, T) => (s, modes_of_typ T)) preds |
|
2212 |
in (preds, nparams, all_vs, param_vs, extra_modes, clauses, all_modes) end; |
|
2213 |
||
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2214 |
fun check_format_of_intro_rule thy intro = |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2215 |
let |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2216 |
val concl = Logic.strip_imp_concl (prop_of intro) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2217 |
val (p, args) = strip_comb (HOLogic.dest_Trueprop concl) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2218 |
val params = List.take (args, nparams_of thy (fst (dest_Const p))) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2219 |
fun check_arg arg = case HOLogic.strip_tupleT (fastype_of arg) of |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2220 |
(Ts as _ :: _ :: _) => |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2221 |
if (length (HOLogic.strip_tuple arg) = length Ts) then true |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
2222 |
else |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
2223 |
error ("Format of introduction rule is invalid: tuples must be expanded:" |
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2224 |
^ (Syntax.string_of_term_global thy arg) ^ " in " ^ |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2225 |
(Display.string_of_thm_global thy intro)) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2226 |
| _ => true |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2227 |
val prems = Logic.strip_imp_prems (prop_of intro) |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2228 |
fun check_prem (Prem (args, _)) = forall check_arg args |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2229 |
| check_prem (Negprem (args, _)) = forall check_arg args |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2230 |
| check_prem _ = true |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2231 |
in |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2232 |
forall check_arg args andalso |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2233 |
forall (check_prem o dest_prem thy params o HOLogic.dest_Trueprop) prems |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2234 |
end |
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2235 |
|
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2236 |
(* |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2237 |
fun check_intros_elim_match thy prednames = |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2238 |
let |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2239 |
fun check predname = |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2240 |
let |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2241 |
val intros = intros_of thy predname |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2242 |
val elim = the_elim_of thy predname |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2243 |
val nparams = nparams_of thy predname |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2244 |
val elim' = |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2245 |
(Drule.standard o (setmp quick_and_dirty true (SkipProof.make_thm thy))) |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2246 |
(mk_casesrule (ProofContext.init thy) nparams intros) |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2247 |
in |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2248 |
if not (Thm.equiv_thm (elim, elim')) then |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2249 |
error "Introduction and elimination rules do not match!" |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2250 |
else true |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2251 |
end |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2252 |
in forall check prednames end |
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2253 |
*) |
33113 | 2254 |
|
33106
7a1636c3ffc9
extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents:
32740
diff
changeset
|
2255 |
|
32667 | 2256 |
(** main function of predicate compiler **) |
2257 |
||
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2258 |
fun add_equations_of steps options prednames thy = |
32667 | 2259 |
let |
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2260 |
val _ = print_step options ("Starting predicate compiler for predicates " ^ commas prednames ^ "...") |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2261 |
val _ = tracing (commas (map (Display.string_of_thm_global thy) (maps (intros_of thy) prednames))) |
33124
5378e61add1a
continued cleaning up; moved tuple expanding to core
bulwahn
parents:
33123
diff
changeset
|
2262 |
(*val _ = check_intros_elim_match thy prednames*) |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
2263 |
(*val _ = map (check_format_of_intro_rule thy) (maps (intros_of thy) prednames)*) |
32667 | 2264 |
val (preds, nparams, all_vs, param_vs, extra_modes, clauses, all_modes) = |
33113 | 2265 |
prepare_intrs thy prednames (maps (intros_of thy) prednames) |
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2266 |
val _ = print_step options "Infering modes..." |
33130
7eac458c2b22
added option show_mode_inference; added splitting of conjunctions in expand_tuples
bulwahn
parents:
33129
diff
changeset
|
2267 |
val moded_clauses = #infer_modes steps options thy extra_modes all_modes param_vs clauses |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2268 |
val modes = map (fn (p, mps) => (p, map fst mps)) moded_clauses |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2269 |
val _ = check_expected_modes options modes |
32667 | 2270 |
val _ = print_modes modes |
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
2271 |
(*val _ = print_moded_clauses thy moded_clauses*) |
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2272 |
val _ = print_step options "Defining executable functions..." |
32667 | 2273 |
val thy' = fold (#create_definitions steps preds) modes thy |
2274 |
|> Theory.checkpoint |
|
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2275 |
val _ = print_step options "Compiling equations..." |
32667 | 2276 |
val compiled_terms = |
2277 |
(#compile_preds steps) thy' all_vs param_vs preds moded_clauses |
|
33139
9c01ee6f8ee9
added skip_proof option; playing with compilation of depth-limited predicates
bulwahn
parents:
33138
diff
changeset
|
2278 |
val _ = print_compiled_terms options thy' compiled_terms |
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2279 |
val _ = print_step options "Proving equations..." |
33127 | 2280 |
val result_thms = #prove steps options thy' clauses preds (extra_modes @ modes) |
32667 | 2281 |
moded_clauses compiled_terms |
2282 |
val qname = #qname steps |
|
2283 |
val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute |
|
2284 |
(fn thm => Context.mapping (Code.add_eqn thm) I)))) |
|
2285 |
val thy'' = fold (fn (name, result_thms) => fn thy => snd (PureThy.add_thmss |
|
2286 |
[((Binding.qualify true (Long_Name.base_name name) (Binding.name qname), result_thms), |
|
2287 |
[attrib thy ])] thy)) |
|
2288 |
(maps_modes result_thms) thy' |
|
2289 |
|> Theory.checkpoint |
|
2290 |
in |
|
2291 |
thy'' |
|
2292 |
end |
|
2293 |
||
2294 |
fun extend' value_of edges_of key (G, visited) = |
|
2295 |
let |
|
2296 |
val (G', v) = case try (Graph.get_node G) key of |
|
2297 |
SOME v => (G, v) |
|
2298 |
| NONE => (Graph.new_node (key, value_of key) G, value_of key) |
|
2299 |
val (G'', visited') = fold (extend' value_of edges_of) (edges_of (key, v) \\ visited) |
|
2300 |
(G', key :: visited) |
|
2301 |
in |
|
2302 |
(fold (Graph.add_edge o (pair key)) (edges_of (key, v)) G'', visited') |
|
2303 |
end; |
|
2304 |
||
2305 |
fun extend value_of edges_of key G = fst (extend' value_of edges_of key (G, [])) |
|
2306 |
||
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2307 |
fun gen_add_equations steps options names thy = |
32667 | 2308 |
let |
2309 |
val thy' = PredData.map (fold (extend (fetch_pred_data thy) (depending_preds_of thy)) names) thy |
|
2310 |
|> Theory.checkpoint; |
|
2311 |
fun strong_conn_of gr keys = |
|
2312 |
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) |
|
2313 |
val scc = strong_conn_of (PredData.get thy') names |
|
2314 |
val thy'' = fold_rev |
|
2315 |
(fn preds => fn thy => |
|
33123
3c7c4372f9ad
cleaned up debugging messages; added options to code_pred command
bulwahn
parents:
33120
diff
changeset
|
2316 |
if #are_not_defined steps thy preds then |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2317 |
add_equations_of steps options preds thy else thy) |
32667 | 2318 |
scc thy' |> Theory.checkpoint |
2319 |
in thy'' end |
|
2320 |
||
2321 |
(* different instantiantions of the predicate compiler *) |
|
2322 |
||
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2323 |
val predicate_comp_modifiers = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2324 |
{const_name_of = predfun_name_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2325 |
funT_of = funT_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2326 |
additional_arguments = K [], |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2327 |
wrap_compilation = K (K (K (K (K I)))), |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2328 |
transform_additional_arguments = K I |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2329 |
} |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2330 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2331 |
val depth_limited_comp_modifiers = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2332 |
{const_name_of = depth_limited_function_name_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2333 |
funT_of = depth_limited_funT_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2334 |
additional_arguments = fn names => |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2335 |
let |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2336 |
val [depth_name, polarity_name] = Name.variant_list names ["depth", "polarity"] |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2337 |
in [Free (polarity_name, @{typ "bool"}), Free (depth_name, @{typ "code_numeral"})] end, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2338 |
wrap_compilation = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2339 |
fn compfuns => fn s => fn T => fn mode => fn additional_arguments => fn compilation => |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2340 |
let |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2341 |
val [polarity, depth] = additional_arguments |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2342 |
val (_, Ts2) = chop (length (fst mode)) (binder_types T) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2343 |
val (_, Us2) = split_smodeT (snd mode) Ts2 |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2344 |
val T' = mk_predT compfuns (mk_tupleT Us2) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2345 |
val if_const = Const (@{const_name "If"}, @{typ bool} --> T' --> T' --> T') |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2346 |
val full_mode = null Us2 |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2347 |
in |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2348 |
if_const $ HOLogic.mk_eq (depth, @{term "0 :: code_numeral"}) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2349 |
$ (if_const $ polarity $ mk_bot compfuns (dest_predT compfuns T') |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2350 |
$ (if full_mode then mk_single compfuns HOLogic.unit else Const (@{const_name undefined}, T'))) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2351 |
$ compilation |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2352 |
end, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2353 |
transform_additional_arguments = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2354 |
fn prem => fn additional_arguments => |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2355 |
let |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2356 |
val [polarity, depth] = additional_arguments |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2357 |
val polarity' = (case prem of Prem _ => I | Negprem _ => HOLogic.mk_not) polarity |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2358 |
val depth' = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2359 |
Const ("HOL.minus_class.minus", @{typ "code_numeral => code_numeral => code_numeral"}) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2360 |
$ depth $ Const ("HOL.one_class.one", @{typ "Code_Numeral.code_numeral"}) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2361 |
in [polarity', depth'] end |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2362 |
} |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2363 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2364 |
val rpred_comp_modifiers = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2365 |
{const_name_of = generator_name_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2366 |
funT_of = K generator_funT_of, |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2367 |
additional_arguments = fn names => [Free (Name.variant names "size", @{typ code_numeral})], |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2368 |
wrap_compilation = K (K (K (K (K I)))), |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2369 |
transform_additional_arguments = K I |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2370 |
} |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2371 |
|
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2372 |
|
32667 | 2373 |
val add_equations = gen_add_equations |
2374 |
{infer_modes = infer_modes, |
|
2375 |
create_definitions = create_definitions, |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2376 |
compile_preds = compile_preds predicate_comp_modifiers PredicateCompFuns.compfuns, |
32667 | 2377 |
prove = prove, |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2378 |
are_not_defined = fn thy => forall (null o modes_of thy), |
32667 | 2379 |
qname = "equation"} |
2380 |
||
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
2381 |
val add_depth_limited_equations = gen_add_equations |
32667 | 2382 |
{infer_modes = infer_modes, |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
2383 |
create_definitions = create_definitions_of_depth_limited_functions, |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2384 |
compile_preds = compile_preds depth_limited_comp_modifiers PredicateCompFuns.compfuns, |
32667 | 2385 |
prove = prove_by_skip, |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
2386 |
are_not_defined = fn thy => forall (null o depth_limited_modes_of thy), |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2387 |
qname = "depth_limited_equation"} |
32667 | 2388 |
|
2389 |
val add_quickcheck_equations = gen_add_equations |
|
2390 |
{infer_modes = infer_modes_with_generator, |
|
2391 |
create_definitions = rpred_create_definitions, |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2392 |
compile_preds = compile_preds rpred_comp_modifiers RPredCompFuns.compfuns, |
32667 | 2393 |
prove = prove_by_skip, |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2394 |
are_not_defined = fn thy => forall (null o rpred_modes_of thy), |
32667 | 2395 |
qname = "rpred_equation"} |
2396 |
||
2397 |
(** user interface **) |
|
2398 |
||
2399 |
(* code_pred_intro attribute *) |
|
2400 |
||
2401 |
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
2402 |
||
2403 |
val code_pred_intros_attrib = attrib add_intro; |
|
2404 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2405 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2406 |
(*FIXME |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2407 |
- Naming of auxiliary rules necessary? |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2408 |
- add default code equations P x y z = P_i_i_i x y z |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2409 |
*) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2410 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2411 |
val setup = PredData.put (Graph.empty) #> |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2412 |
Attrib.setup @{binding code_pred_intros} (Scan.succeed (attrib add_intro)) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2413 |
"adding alternative introduction rules for code generation of inductive predicates" |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2414 |
(*FIXME name discrepancy in attribs and ML code*) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2415 |
(*FIXME intros should be better named intro*) |
32667 | 2416 |
|
2417 |
(* TODO: make TheoryDataFun to GenericDataFun & remove duplication of local theory and theory *) |
|
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2418 |
fun generic_code_pred prep_const options raw_const lthy = |
32667 | 2419 |
let |
2420 |
val thy = ProofContext.theory_of lthy |
|
2421 |
val const = prep_const thy raw_const |
|
2422 |
val lthy' = LocalTheory.theory (PredData.map |
|
2423 |
(extend (fetch_pred_data thy) (depending_preds_of thy) const)) lthy |
|
2424 |
|> LocalTheory.checkpoint |
|
2425 |
val thy' = ProofContext.theory_of lthy' |
|
2426 |
val preds = Graph.all_preds (PredData.get thy') [const] |> filter_out (has_elim thy') |
|
2427 |
fun mk_cases const = |
|
2428 |
let |
|
2429 |
val nparams = nparams_of thy' const |
|
2430 |
val intros = intros_of thy' const |
|
2431 |
in mk_casesrule lthy' nparams intros end |
|
2432 |
val cases_rules = map mk_cases preds |
|
2433 |
val cases = |
|
2434 |
map (fn case_rule => RuleCases.Case {fixes = [], |
|
2435 |
assumes = [("", Logic.strip_imp_prems case_rule)], |
|
2436 |
binds = [], cases = []}) cases_rules |
|
2437 |
val case_env = map2 (fn p => fn c => (Long_Name.base_name p, SOME c)) preds cases |
|
2438 |
val lthy'' = lthy' |
|
2439 |
|> fold Variable.auto_fixes cases_rules |
|
2440 |
|> ProofContext.add_cases true case_env |
|
2441 |
fun after_qed thms goal_ctxt = |
|
2442 |
let |
|
2443 |
val global_thms = ProofContext.export goal_ctxt |
|
2444 |
(ProofContext.init (ProofContext.theory_of goal_ctxt)) (map the_single thms) |
|
2445 |
in |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2446 |
goal_ctxt |> LocalTheory.theory (fold set_elim global_thms #> |
33131
cef39362ce56
removed unnecessary argument rpred in code_pred function
bulwahn
parents:
33130
diff
changeset
|
2447 |
(if is_rpred options then |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2448 |
(add_equations options [const] #> |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2449 |
add_quickcheck_equations options [const]) |
33134
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
2450 |
else if is_depth_limited options then |
88c9c3460fe7
renamed functions from sizelim to more natural name depth_limited for compilation of depth-limited search in the predicate compiler
bulwahn
parents:
33133
diff
changeset
|
2451 |
add_depth_limited_equations options [const] |
33132
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2452 |
else |
07efd452a698
moved argument expected_modes into options; improved mode check to only check mode of the named predicate
bulwahn
parents:
33131
diff
changeset
|
2453 |
add_equations options [const])) |
32667 | 2454 |
end |
2455 |
in |
|
2456 |
Proof.theorem_i NONE after_qed (map (single o (rpair [])) cases_rules) lthy'' |
|
2457 |
end; |
|
2458 |
||
2459 |
val code_pred = generic_code_pred (K I); |
|
2460 |
val code_pred_cmd = generic_code_pred Code.read_const |
|
2461 |
||
2462 |
(* transformation for code generation *) |
|
2463 |
||
32740 | 2464 |
val eval_ref = Unsynchronized.ref (NONE : (unit -> term Predicate.pred) option); |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2465 |
val random_eval_ref = Unsynchronized.ref (NONE : (unit -> int * int -> term Predicate.pred * (int * int)) option); |
32667 | 2466 |
|
2467 |
(*FIXME turn this into an LCF-guarded preprocessor for comprehensions*) |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2468 |
fun analyze_compr thy compfuns (depth_limit, random) t_compr = |
32667 | 2469 |
let |
2470 |
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t |
|
2471 |
| _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr); |
|
2472 |
val (body, Ts, fp) = HOLogic.strip_psplits split; |
|
2473 |
val (pred as Const (name, T), all_args) = strip_comb body; |
|
2474 |
val (params, args) = chop (nparams_of thy name) all_args; |
|
2475 |
val user_mode = map_filter I (map_index |
|
2476 |
(fn (i, t) => case t of Bound j => if j < length Ts then NONE |
|
2477 |
else SOME (i+1) | _ => SOME (i+1)) args); (*FIXME dangling bounds should not occur*) |
|
2478 |
val user_mode' = map (rpair NONE) user_mode |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2479 |
val all_modes_of = if random then all_generator_modes_of else all_modes_of |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2480 |
(*val compile_expr = if random then compile_gen_expr else compile_expr*) |
32667 | 2481 |
val modes = filter (fn Mode (_, is, _) => is = user_mode') |
2482 |
(modes_of_term (all_modes_of thy) (list_comb (pred, params))); |
|
2483 |
val m = case modes |
|
2484 |
of [] => error ("No mode possible for comprehension " |
|
2485 |
^ Syntax.string_of_term_global thy t_compr) |
|
2486 |
| [m] => m |
|
2487 |
| m :: _ :: _ => (warning ("Multiple modes possible for comprehension " |
|
2488 |
^ Syntax.string_of_term_global thy t_compr); m); |
|
2489 |
val (inargs, outargs) = split_smode user_mode' args; |
|
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2490 |
val additional_arguments = |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2491 |
case depth_limit of |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2492 |
NONE => (if random then [@{term "5 :: code_numeral"}] else []) |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2493 |
| SOME d => [@{term "True"}, HOLogic.mk_number @{typ "code_numeral"} d] |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2494 |
val comp_modifiers = |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2495 |
case depth_limit of NONE => |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2496 |
(if random then rpred_comp_modifiers else predicate_comp_modifiers) | SOME _ => depth_limited_comp_modifiers |
33138
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
2497 |
val mk_fun_of = if random then mk_generator_of else |
e2e23987c59a
reinvestigating the compilation of the random computation in the predicate compiler
bulwahn
parents:
33137
diff
changeset
|
2498 |
if (is_some depth_limit) then mk_depth_limited_fun_of else mk_fun_of |
33143
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2499 |
val t_pred = compile_expr comp_modifiers compfuns thy |
730a2e8a6ec6
modularized the compilation in the predicate compiler
bulwahn
parents:
33141
diff
changeset
|
2500 |
(m, list_comb (pred, params)) inargs additional_arguments; |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2501 |
val t_eval = if null outargs then t_pred else |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
32667
diff
changeset
|
2502 |
let |
32667 | 2503 |
val outargs_bounds = map (fn Bound i => i) outargs; |
2504 |
val outargsTs = map (nth Ts) outargs_bounds; |
|
2505 |
val T_pred = HOLogic.mk_tupleT outargsTs; |
|
2506 |
val T_compr = HOLogic.mk_ptupleT fp Ts; |
|
2507 |
val arrange_bounds = map_index I outargs_bounds |
|
2508 |
|> sort (prod_ord (K EQUAL) int_ord) |
|
2509 |
|> map fst; |
|
2510 |
val arrange = funpow (length outargs_bounds - 1) HOLogic.mk_split |
|
2511 |
(Term.list_abs (map (pair "") outargsTs, |
|
2512 |
HOLogic.mk_ptuple fp T_compr (map Bound arrange_bounds))) |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2513 |
in mk_map compfuns T_pred T_compr arrange t_pred end |
32667 | 2514 |
in t_eval end; |
2515 |
||
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2516 |
fun eval thy (options as (depth_limit, random)) t_compr = |
32667 | 2517 |
let |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2518 |
val compfuns = if random then RPredCompFuns.compfuns else PredicateCompFuns.compfuns |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2519 |
val t = analyze_compr thy compfuns options t_compr; |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2520 |
val T = dest_predT compfuns (fastype_of t); |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2521 |
val t' = mk_map compfuns T HOLogic.termT (HOLogic.term_of_const T) t; |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2522 |
val eval = |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2523 |
if random then |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2524 |
Code_ML.eval NONE ("Predicate_Compile_Core.random_eval_ref", random_eval_ref) |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2525 |
(fn proc => fn g => fn s => g s |>> Predicate.map proc) thy t' [] |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2526 |
|> Random_Engine.run |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2527 |
else |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2528 |
Code_ML.eval NONE ("Predicate_Compile_Core.eval_ref", eval_ref) Predicate.map thy t' [] |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2529 |
in (T, eval) end; |
32667 | 2530 |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2531 |
fun values ctxt options k t_compr = |
32667 | 2532 |
let |
2533 |
val thy = ProofContext.theory_of ctxt; |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2534 |
val (T, ts) = eval thy options t_compr; |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2535 |
val (ts, _) = Predicate.yieldn k ts; |
32667 | 2536 |
val setT = HOLogic.mk_setT T; |
2537 |
val elemsT = HOLogic.mk_set T ts; |
|
2538 |
in if k = ~1 orelse length ts < k then elemsT |
|
2539 |
else Const (@{const_name Set.union}, setT --> setT --> setT) $ elemsT $ t_compr |
|
2540 |
end; |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2541 |
(* |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2542 |
fun random_values ctxt k t = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2543 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2544 |
val thy = ProofContext.theory_of ctxt |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2545 |
val _ = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2546 |
in |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2547 |
end; |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32671
diff
changeset
|
2548 |
*) |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2549 |
fun values_cmd modes options k raw_t state = |
32667 | 2550 |
let |
2551 |
val ctxt = Toplevel.context_of state; |
|
2552 |
val t = Syntax.read_term ctxt raw_t; |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2553 |
val t' = values ctxt options k t; |
32667 | 2554 |
val ty' = Term.type_of t'; |
2555 |
val ctxt' = Variable.auto_fixes t' ctxt; |
|
2556 |
val p = PrintMode.with_modes modes (fn () => |
|
2557 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
|
2558 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); |
|
2559 |
in Pretty.writeln p end; |
|
2560 |
||
2561 |
local structure P = OuterParse in |
|
2562 |
||
2563 |
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) []; |
|
2564 |
||
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2565 |
val _ = List.app OuterKeyword.keyword ["depth_limit", "random"] |
33135
422cac7d6e31
added option to execute depth-limited computations for the values command in the predicate compiler
bulwahn
parents:
33134
diff
changeset
|
2566 |
|
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2567 |
val options = |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2568 |
let |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2569 |
val depth_limit = Scan.optional (P.$$$ "depth_limit" |-- P.$$$ "=" |-- P.nat >> SOME) NONE |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2570 |
val random = Scan.optional (P.$$$ "random" >> K true) false |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2571 |
in |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2572 |
Scan.optional (P.$$$ "[" |-- depth_limit -- random --| P.$$$ "]") (NONE, false) |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2573 |
end |
33135
422cac7d6e31
added option to execute depth-limited computations for the values command in the predicate compiler
bulwahn
parents:
33134
diff
changeset
|
2574 |
|
32667 | 2575 |
val _ = OuterSyntax.improper_command "values" "enumerate and print comprehensions" OuterKeyword.diag |
33137
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2576 |
(opt_modes -- options -- Scan.optional P.nat ~1 -- P.term |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2577 |
>> (fn (((modes, options), k), t) => Toplevel.no_timing o Toplevel.keep |
0d16c07f8d24
added option to generate random values to values command in the predicate compiler
bulwahn
parents:
33135
diff
changeset
|
2578 |
(values_cmd modes options k t))); |
32667 | 2579 |
|
2580 |
end; |
|
2581 |
||
2582 |
end; |