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