author | bulwahn |
Sat, 24 Oct 2009 16:55:42 +0200 | |
changeset 33120 | ca77d8c34ce2 |
parent 33114 | 4785ef554dcc |
child 33121 | 9b10dc5da0e0 |
permissions | -rw-r--r-- |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
1 |
(* Author: Lukas Bulwahn, TU Muenchen |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
2 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
3 |
*) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
4 |
signature PREDICATE_COMPILE = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
5 |
sig |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
6 |
val setup : theory -> theory |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
7 |
val preprocess : string -> theory -> theory |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
8 |
end; |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
9 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
10 |
structure Predicate_Compile : PREDICATE_COMPILE = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
11 |
struct |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
12 |
|
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33107
diff
changeset
|
13 |
(* options *) |
33111 | 14 |
val fail_safe_mode = true |
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33107
diff
changeset
|
15 |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
16 |
open Predicate_Compile_Aux; |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
17 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
18 |
val priority = tracing; |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
19 |
|
33113 | 20 |
(* tuple processing *) |
21 |
||
22 |
fun expand_tuples thy intro = |
|
23 |
let |
|
24 |
fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt) |
|
25 |
| rewrite_args (arg::args) (pats, intro_t, ctxt) = |
|
26 |
(case HOLogic.strip_tupleT (fastype_of arg) of |
|
27 |
(Ts as _ :: _ :: _) => |
|
28 |
let |
|
29 |
fun rewrite_arg' (Const ("Pair", _) $ _ $ t2, Type ("*", [_, T2])) |
|
30 |
(args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt)) |
|
31 |
| rewrite_arg' (t, Type ("*", [T1, T2])) (args, (pats, intro_t, ctxt)) = |
|
32 |
let |
|
33 |
val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt |
|
34 |
val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2))) |
|
35 |
(*val _ = tracing ("Rewriting term " ^ |
|
36 |
(Syntax.string_of_term_global thy (fst pat)) ^ " to " ^ |
|
37 |
(Syntax.string_of_term_global thy (snd pat)) ^ " in " ^ |
|
38 |
(Syntax.string_of_term_global thy intro_t))*) |
|
39 |
val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t |
|
40 |
val args' = map (Pattern.rewrite_term thy [pat] []) args |
|
41 |
in |
|
42 |
rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt')) |
|
43 |
end |
|
44 |
| rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt)) |
|
45 |
val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg) |
|
46 |
(args, (pats, intro_t, ctxt)) |
|
47 |
in |
|
48 |
rewrite_args args' (pats, intro_t', ctxt') |
|
49 |
end |
|
50 |
| _ => rewrite_args args (pats, intro_t, ctxt)) |
|
51 |
fun rewrite_prem atom = |
|
52 |
let |
|
53 |
val (_, args) = strip_comb atom |
|
54 |
in rewrite_args args end |
|
55 |
val ctxt = ProofContext.init thy |
|
56 |
val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt |
|
57 |
val intro_t = prop_of intro' |
|
58 |
val concl = Logic.strip_imp_concl intro_t |
|
59 |
val (p, args) = strip_comb (HOLogic.dest_Trueprop concl) |
|
60 |
val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1) |
|
61 |
val (pats', intro_t', ctxt3) = |
|
62 |
fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2) |
|
63 |
(*val _ = Output.tracing ("intro_t': " ^ (Syntax.string_of_term_global thy intro_t')) |
|
64 |
val _ = Output.tracing ("pats : " ^ (commas (map |
|
65 |
(fn (t1, t2) => (Syntax.string_of_term_global thy t1) ^ " -> " ^ |
|
66 |
Syntax.string_of_term_global thy t2) pats'))*) |
|
67 |
fun rewrite_pat (ct1, ct2) = |
|
68 |
(ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2))) |
|
69 |
val t_insts' = map rewrite_pat t_insts |
|
70 |
(*val _ = Output.tracing ("t_insts':" ^ (commas (map |
|
71 |
(fn (ct1, ct2) => (Syntax.string_of_term_global thy (term_of ct1) ^ " -> " ^ |
|
72 |
Syntax.string_of_term_global thy (term_of ct2))) t_insts')))*) |
|
73 |
val intro'' = Thm.instantiate (T_insts, t_insts') intro |
|
74 |
(*val _ = Output.tracing ("intro'':" ^ (Display.string_of_thm_global thy intro''))*) |
|
75 |
val [intro'''] = Variable.export ctxt3 ctxt [intro''] |
|
76 |
(*val _ = Output.tracing ("intro''':" ^ (Display.string_of_thm_global thy intro'''))*) |
|
77 |
in |
|
78 |
intro''' |
|
79 |
end |
|
80 |
||
81 |
(* eliminating fst/snd functions *) |
|
82 |
val simplify_fst_snd = Simplifier.full_simplify |
|
83 |
(HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}]) |
|
84 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
85 |
(* Some last processing *) |
33113 | 86 |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
87 |
fun remove_pointless_clauses intro = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
88 |
if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
89 |
[] |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
90 |
else [intro] |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
91 |
|
33120 | 92 |
|
93 |
fun print_intross thy msg intross = |
|
94 |
tracing (msg ^ |
|
95 |
(space_implode "; " (map |
|
96 |
(fn intros => commas (map (Display.string_of_thm_global thy) intros)) intross))) |
|
97 |
||
98 |
||
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
99 |
fun preprocess_strong_conn_constnames gr constnames thy = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
100 |
let |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
101 |
val get_specs = map (fn k => (k, Graph.get_node gr k)) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
102 |
val _ = priority ("Preprocessing scc of " ^ commas constnames) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
103 |
val (prednames, funnames) = List.partition (is_pred thy) constnames |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
104 |
(* untangle recursion by defining predicates for all functions *) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
105 |
val _ = priority "Compiling functions to predicates..." |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
106 |
val _ = Output.tracing ("funnames: " ^ commas funnames) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
107 |
val thy' = |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
108 |
thy |> not (null funnames) ? Predicate_Compile_Fun.define_predicates |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
109 |
(get_specs funnames) |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
110 |
val _ = priority "Compiling predicates to flat introrules..." |
33113 | 111 |
val (intross1, thy'') = apfst flat (fold_map Predicate_Compile_Pred.preprocess |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
112 |
(get_specs prednames) thy') |
33120 | 113 |
val _ = print_intross thy'' "Flattened introduction rules: " intross1 |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
114 |
val _ = priority "Replacing functions in introrules..." |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
115 |
(* val _ = burrow (maps (Predicate_Compile_Fun.rewrite_intro thy'')) intross *) |
33113 | 116 |
val intross2 = |
33108
9d9afd478016
added test for higher-order function inductification; added debug messages
bulwahn
parents:
33107
diff
changeset
|
117 |
if fail_safe_mode then |
33113 | 118 |
case try (burrow (maps (Predicate_Compile_Fun.rewrite_intro thy''))) intross1 of |
119 |
SOME intross => intross |
|
120 |
| NONE => let val _ = warning "Function replacement failed!" in intross1 end |
|
121 |
else burrow (maps (Predicate_Compile_Fun.rewrite_intro thy'')) intross1 |
|
33120 | 122 |
val _ = print_intross thy'' "Introduction rules with replaced functions: " intross2 |
123 |
val intross3 = map (maps remove_pointless_clauses) intross2 |
|
124 |
val _ = print_intross thy'' "After removing pointless clauses: " intross3 |
|
33113 | 125 |
val intross4 = burrow (map (AxClass.overload thy'')) intross3 |
126 |
val intross5 = burrow (map (simplify_fst_snd o expand_tuples thy'')) intross4 |
|
33120 | 127 |
val _ = print_intross thy'' "introduction rules before registering: " intross5 |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
128 |
val _ = priority "Registering intro rules..." |
33113 | 129 |
val thy''' = fold Predicate_Compile_Core.register_intros intross5 thy'' |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
130 |
in |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
131 |
thy''' |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
132 |
end; |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
133 |
|
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
134 |
fun preprocess const thy = |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
135 |
let |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
136 |
val _ = Output.tracing ("Fetching definitions from theory...") |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
137 |
val table = Pred_Compile_Data.make_const_spec_table thy |
33107
6aa76ce59ef2
added filtering of case constants in the definition retrieval of the predicate compiler
bulwahn
parents:
32672
diff
changeset
|
138 |
val gr = Pred_Compile_Data.obtain_specification_graph thy table const |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
139 |
val _ = Output.tracing (commas (Graph.all_succs gr [const])) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
140 |
val gr = Graph.subgraph (member (op =) (Graph.all_succs gr [const])) gr |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
141 |
in fold_rev (preprocess_strong_conn_constnames gr) |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
142 |
(Graph.strong_conn gr) thy |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
143 |
end |
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
144 |
|
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
145 |
fun code_pred_cmd (((modes, inductify_all), rpred), raw_const) lthy = |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
146 |
if inductify_all then |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
147 |
let |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
148 |
val thy = ProofContext.theory_of lthy |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
149 |
val const = Code.read_const thy raw_const |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
150 |
val lthy' = LocalTheory.theory (preprocess const) lthy |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
151 |
|> LocalTheory.checkpoint |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
152 |
|
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
153 |
val const = case Predicate_Compile_Fun.pred_of_function (ProofContext.theory_of lthy') const of |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
154 |
SOME c => c |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
155 |
| NONE => const |
32672
90f3ce5d27ae
added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents:
32669
diff
changeset
|
156 |
val _ = tracing "Starting Predicate Compile Core..." |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
157 |
in Predicate_Compile_Core.code_pred modes rpred const lthy' end |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
158 |
else |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
159 |
Predicate_Compile_Core.code_pred_cmd modes rpred raw_const lthy |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
160 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
161 |
val setup = Predicate_Compile_Fun.setup_oracle #> Predicate_Compile_Core.setup |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
162 |
|
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
163 |
val _ = List.app OuterKeyword.keyword ["inductify_all", "rpred", "mode"] |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
164 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
165 |
local structure P = OuterParse |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
166 |
in |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
167 |
|
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
168 |
val opt_modes = |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
169 |
Scan.optional (P.$$$ "(" |-- P.$$$ "mode" |-- P.$$$ ":" |-- |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
170 |
P.enum1 "," (P.$$$ "[" |-- P.enum "," P.nat --| P.$$$ "]") |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
171 |
--| P.$$$ ")" >> SOME) NONE |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
172 |
|
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
173 |
val _ = OuterSyntax.local_theory_to_proof "code_pred" |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
174 |
"prove equations for predicate specified by intro/elim rules" |
33114
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
175 |
OuterKeyword.thy_goal (opt_modes -- |
4785ef554dcc
added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents:
33113
diff
changeset
|
176 |
P.opt_keyword "inductify_all" -- P.opt_keyword "rpred" -- P.term_group >> code_pred_cmd) |
32668
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
177 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
178 |
end |
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
179 |
|
b2de45007537
added first prototype of the extended predicate compiler
bulwahn
parents:
diff
changeset
|
180 |
end |