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