author | bulwahn |
Tue, 04 Aug 2009 08:34:56 +0200 | |
changeset 32307 | 55166cd57a6d |
parent 32306 | 19f55947d4d5 |
child 32308 | c2b74affab85 |
permissions | -rw-r--r-- |
31217 | 1 |
(* Author: Lukas Bulwahn, TU Muenchen |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
2 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
3 |
(Prototype of) A compiler from predicates specified by intro/elim rules |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
4 |
to equations. |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
5 |
*) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
6 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
7 |
signature PREDICATE_COMPILE = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
8 |
sig |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
9 |
type mode = int list option list * int list |
31514 | 10 |
val add_equations_of: string list -> theory -> theory |
11 |
val register_predicate : (thm list * thm * int) -> theory -> theory |
|
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
12 |
val is_registered : theory -> string -> bool |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
13 |
val fetch_pred_data : theory -> string -> (thm list * thm * int) |
31514 | 14 |
val predfun_intro_of: theory -> string -> mode -> thm |
15 |
val predfun_elim_of: theory -> string -> mode -> thm |
|
16 |
val strip_intro_concl: int -> term -> term * (term list * term list) |
|
17 |
val predfun_name_of: theory -> string -> mode -> string |
|
18 |
val all_preds_of : theory -> string list |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
19 |
val modes_of: theory -> string -> mode list |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
20 |
val string_of_mode : mode -> string |
31514 | 21 |
val intros_of: theory -> string -> thm list |
22 |
val nparams_of: theory -> string -> int |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
23 |
val add_intro: thm -> theory -> theory |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
24 |
val set_elim: thm -> theory -> theory |
31124 | 25 |
val setup: theory -> theory |
26 |
val code_pred: string -> Proof.context -> Proof.state |
|
27 |
val code_pred_cmd: string -> Proof.context -> Proof.state |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
28 |
val print_stored_rules: theory -> unit |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
29 |
val print_all_modes: theory -> unit |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
30 |
val do_proofs: bool ref |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
31 |
val mk_casesrule : Proof.context -> int -> thm list -> term |
31217 | 32 |
val analyze_compr: theory -> term -> term |
33 |
val eval_ref: (unit -> term Predicate.pred) option ref |
|
31514 | 34 |
val add_equations : string -> theory -> theory |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
35 |
val code_pred_intros_attrib : attribute |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
36 |
(* used by Quickcheck_Generator *) |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
37 |
val funT_of : mode -> typ -> typ |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
38 |
val mk_if_pred : term -> term |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
39 |
val mk_Eval : term * term -> term |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
40 |
val mk_tupleT : typ list -> typ |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
41 |
val mk_predT : typ -> typ |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
42 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
43 |
|
31124 | 44 |
structure Predicate_Compile : PREDICATE_COMPILE = |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
45 |
struct |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
46 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
47 |
(** auxiliary **) |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
48 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
49 |
(* debug stuff *) |
31320
72eeb1b4e006
provide local dummy version of makestring -- NB: makestring is fragile and not portable, it should not occur in repository sources;
wenzelm
parents:
31284
diff
changeset
|
50 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
51 |
fun tracing s = (if ! Toplevel.debug then Output.tracing s else ()); |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
52 |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
53 |
fun print_tac s = Seq.single; (* (if ! Toplevel.debug then Tactical.print_tac s else Seq.single); *) |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
54 |
fun debug_tac msg = Seq.single; (* (fn st => (tracing msg; Seq.single st)); *) |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
55 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
56 |
val do_proofs = ref true; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
57 |
|
31514 | 58 |
fun mycheat_tac thy i st = |
59 |
(Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) i) st |
|
60 |
||
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
61 |
fun remove_last_goal thy st = |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
62 |
(Tactic.rtac (SkipProof.make_thm thy (Var (("A", 0), propT))) (nprems_of st)) st |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
63 |
|
31514 | 64 |
(* reference to preprocessing of InductiveSet package *) |
65 |
||
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31580
diff
changeset
|
66 |
val ind_set_codegen_preproc = Inductive_Set.codegen_preproc; |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
67 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
68 |
(** fundamentals **) |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
69 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
70 |
(* syntactic operations *) |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
71 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
72 |
fun mk_eq (x, xs) = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
73 |
let fun mk_eqs _ [] = [] |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
74 |
| mk_eqs a (b::cs) = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
75 |
HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
76 |
in mk_eqs x xs end; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
77 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
78 |
fun mk_tupleT [] = HOLogic.unitT |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
79 |
| mk_tupleT Ts = foldr1 HOLogic.mk_prodT Ts; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
80 |
|
31514 | 81 |
fun dest_tupleT (Type (@{type_name Product_Type.unit}, [])) = [] |
82 |
| dest_tupleT (Type (@{type_name "*"}, [T1, T2])) = T1 :: (dest_tupleT T2) |
|
83 |
| dest_tupleT t = [t] |
|
84 |
||
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
85 |
fun mk_tuple [] = HOLogic.unit |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
86 |
| mk_tuple ts = foldr1 HOLogic.mk_prod ts; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
87 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
88 |
fun dest_tuple (Const (@{const_name Product_Type.Unity}, _)) = [] |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
89 |
| dest_tuple (Const (@{const_name Pair}, _) $ t1 $ t2) = t1 :: (dest_tuple t2) |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
90 |
| dest_tuple t = [t] |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
91 |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
92 |
fun mk_predT T = Type (@{type_name "Predicate.pred"}, [T]) |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
93 |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
94 |
fun dest_predT (Type (@{type_name "Predicate.pred"}, [T])) = T |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
95 |
| dest_predT T = raise TYPE ("dest_predT", [T], []); |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
96 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
97 |
fun mk_Enum f = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
98 |
let val T as Type ("fun", [T', _]) = fastype_of f |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
99 |
in |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
100 |
Const (@{const_name Predicate.Pred}, T --> mk_predT T') $ f |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
101 |
end; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
102 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
103 |
fun mk_Eval (f, x) = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
104 |
let val T = fastype_of x |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
105 |
in |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
106 |
Const (@{const_name Predicate.eval}, mk_predT T --> T --> HOLogic.boolT) $ f $ x |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
107 |
end; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
108 |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
109 |
fun mk_bot T = Const (@{const_name Orderings.bot}, mk_predT T); |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
110 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
111 |
fun mk_single t = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
112 |
let val T = fastype_of t |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
113 |
in Const(@{const_name Predicate.single}, T --> mk_predT T) $ t end; |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
114 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
115 |
fun mk_bind (x, f) = |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
116 |
let val T as Type ("fun", [_, U]) = fastype_of f |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
117 |
in |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
118 |
Const (@{const_name Predicate.bind}, fastype_of x --> T --> U) $ x $ f |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
119 |
end; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
120 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
121 |
val mk_sup = HOLogic.mk_binop @{const_name sup}; |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
122 |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
123 |
fun mk_if_pred cond = Const (@{const_name Predicate.if_pred}, |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
124 |
HOLogic.boolT --> mk_predT HOLogic.unitT) $ cond; |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
125 |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
126 |
fun mk_not_pred t = let val T = mk_predT HOLogic.unitT |
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
127 |
in Const (@{const_name Predicate.not_pred}, T --> T) $ t end |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
128 |
|
31514 | 129 |
(* destruction of intro rules *) |
130 |
||
131 |
(* FIXME: look for other place where this functionality was used before *) |
|
132 |
fun strip_intro_concl nparams intro = let |
|
133 |
val _ $ u = Logic.strip_imp_concl intro |
|
134 |
val (pred, all_args) = strip_comb u |
|
135 |
val (params, args) = chop nparams all_args |
|
136 |
in (pred, (params, args)) end |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
137 |
|
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
138 |
(* data structures *) |
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
139 |
|
31514 | 140 |
type mode = int list option list * int list; (*pmode FIMXE*) |
141 |
||
142 |
fun string_of_mode (iss, is) = space_implode " -> " (map |
|
143 |
(fn NONE => "X" |
|
144 |
| SOME js => enclose "[" "]" (commas (map string_of_int js))) |
|
145 |
(iss @ [SOME is])); |
|
146 |
||
147 |
fun print_modes modes = Output.tracing ("Inferred modes:\n" ^ |
|
148 |
cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map |
|
149 |
string_of_mode ms)) modes)); |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
150 |
|
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
151 |
|
31514 | 152 |
datatype predfun_data = PredfunData of { |
153 |
name : string, |
|
154 |
definition : thm, |
|
155 |
intro : thm, |
|
156 |
elim : thm |
|
157 |
}; |
|
158 |
||
159 |
fun rep_predfun_data (PredfunData data) = data; |
|
160 |
fun mk_predfun_data (name, definition, intro, elim) = |
|
161 |
PredfunData {name = name, definition = definition, intro = intro, elim = elim} |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
162 |
|
31514 | 163 |
datatype pred_data = PredData of { |
164 |
intros : thm list, |
|
165 |
elim : thm option, |
|
166 |
nparams : int, |
|
167 |
functions : (mode * predfun_data) list |
|
168 |
}; |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
169 |
|
31514 | 170 |
fun rep_pred_data (PredData data) = data; |
171 |
fun mk_pred_data ((intros, elim, nparams), functions) = |
|
172 |
PredData {intros = intros, elim = elim, nparams = nparams, functions = functions} |
|
173 |
fun map_pred_data f (PredData {intros, elim, nparams, functions}) = |
|
174 |
mk_pred_data (f ((intros, elim, nparams), functions)) |
|
175 |
||
176 |
fun eq_option eq (NONE, NONE) = true |
|
177 |
| eq_option eq (SOME x, SOME y) = eq (x, y) |
|
178 |
| eq_option eq _ = false |
|
179 |
||
180 |
fun eq_pred_data (PredData d1, PredData d2) = |
|
181 |
eq_list (Thm.eq_thm) (#intros d1, #intros d2) andalso |
|
182 |
eq_option (Thm.eq_thm) (#elim d1, #elim d2) andalso |
|
183 |
#nparams d1 = #nparams d2 |
|
184 |
||
185 |
structure PredData = TheoryDataFun |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
186 |
( |
31514 | 187 |
type T = pred_data Graph.T; |
188 |
val empty = Graph.empty; |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
189 |
val copy = I; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
190 |
val extend = I; |
31514 | 191 |
fun merge _ = Graph.merge eq_pred_data; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
192 |
); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
193 |
|
31514 | 194 |
(* queries *) |
195 |
||
31549 | 196 |
fun lookup_pred_data thy name = |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
197 |
Option.map rep_pred_data (try (Graph.get_node (PredData.get thy)) name) |
31514 | 198 |
|
199 |
fun the_pred_data thy name = case lookup_pred_data thy name |
|
200 |
of NONE => error ("No such predicate " ^ quote name) |
|
201 |
| SOME data => data; |
|
202 |
||
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
203 |
val is_registered = is_some oo lookup_pred_data |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
204 |
|
31514 | 205 |
val all_preds_of = Graph.keys o PredData.get |
206 |
||
207 |
val intros_of = #intros oo the_pred_data |
|
208 |
||
209 |
fun the_elim_of thy name = case #elim (the_pred_data thy name) |
|
210 |
of NONE => error ("No elimination rule for predicate " ^ quote name) |
|
211 |
| SOME thm => thm |
|
212 |
||
213 |
val has_elim = is_some o #elim oo the_pred_data; |
|
214 |
||
215 |
val nparams_of = #nparams oo the_pred_data |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
216 |
|
31514 | 217 |
val modes_of = (map fst) o #functions oo the_pred_data |
218 |
||
219 |
fun all_modes_of thy = map (fn name => (name, modes_of thy name)) (all_preds_of thy) |
|
220 |
||
221 |
val is_compiled = not o null o #functions oo the_pred_data |
|
222 |
||
223 |
fun lookup_predfun_data thy name mode = |
|
224 |
Option.map rep_predfun_data (AList.lookup (op =) |
|
225 |
(#functions (the_pred_data thy name)) mode) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
226 |
|
31514 | 227 |
fun the_predfun_data thy name mode = case lookup_predfun_data thy name mode |
228 |
of NONE => error ("No such mode" ^ string_of_mode mode) |
|
229 |
| SOME data => data; |
|
230 |
||
231 |
val predfun_name_of = #name ooo the_predfun_data |
|
232 |
||
233 |
val predfun_definition_of = #definition ooo the_predfun_data |
|
234 |
||
235 |
val predfun_intro_of = #intro ooo the_predfun_data |
|
236 |
||
237 |
val predfun_elim_of = #elim ooo the_predfun_data |
|
238 |
||
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
239 |
fun print_stored_rules thy = |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
240 |
let |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
241 |
val preds = (Graph.keys o PredData.get) thy |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
242 |
fun print pred () = let |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
243 |
val _ = writeln ("predicate: " ^ pred) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
244 |
val _ = writeln ("number of parameters: " ^ string_of_int (nparams_of thy pred)) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
245 |
val _ = writeln ("introrules: ") |
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31986
diff
changeset
|
246 |
val _ = fold (fn thm => fn u => writeln (Display.string_of_thm_global thy thm)) |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
247 |
(rev (intros_of thy pred)) () |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
248 |
in |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
249 |
if (has_elim thy pred) then |
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31986
diff
changeset
|
250 |
writeln ("elimrule: " ^ Display.string_of_thm_global thy (the_elim_of thy pred)) |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
251 |
else |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
252 |
writeln ("no elimrule defined") |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
253 |
end |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
254 |
in |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
255 |
fold print preds () |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
256 |
end; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
257 |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
258 |
fun print_all_modes thy = |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
259 |
let |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
260 |
val _ = writeln ("Inferred modes:") |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
261 |
fun print (pred, modes) u = |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
262 |
let |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
263 |
val _ = writeln ("predicate: " ^ pred) |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
264 |
val _ = writeln ("modes: " ^ (commas (map string_of_mode modes))) |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
265 |
in u end |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
266 |
in |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
267 |
fold print (all_modes_of thy) () |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
268 |
end |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
269 |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
270 |
(** preprocessing rules **) |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
271 |
|
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
272 |
fun imp_prems_conv cv ct = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
273 |
case Thm.term_of ct of |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
274 |
Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
275 |
| _ => Conv.all_conv ct |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
276 |
|
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
277 |
fun Trueprop_conv cv ct = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
278 |
case Thm.term_of ct of |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
279 |
Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
280 |
| _ => error "Trueprop_conv" |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
281 |
|
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
282 |
fun preprocess_intro thy rule = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
283 |
Conv.fconv_rule |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
284 |
(imp_prems_conv |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
285 |
(Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq}))))) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
286 |
(Thm.transfer thy rule) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
287 |
|
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
288 |
fun preprocess_elim thy nargs elimrule = let |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
289 |
fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
290 |
HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
291 |
| replace_eqs t = t |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
292 |
fun preprocess_case t = let |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
293 |
val params = Logic.strip_params t |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
294 |
val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
295 |
val assums_hyp' = assums1 @ (map replace_eqs assums2) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
296 |
in list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t)) end |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
297 |
val prems = Thm.prems_of elimrule |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
298 |
val cases' = map preprocess_case (tl prems) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
299 |
val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
300 |
in |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
301 |
Thm.equal_elim |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
302 |
(Thm.symmetric (Conv.implies_concl_conv (MetaSimplifier.rewrite true [@{thm eq_is_eq}]) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
303 |
(cterm_of thy elimrule'))) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
304 |
elimrule |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
305 |
end; |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
306 |
|
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
307 |
fun fetch_pred_data thy name = |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31580
diff
changeset
|
308 |
case try (Inductive.the_inductive (ProofContext.init thy)) name of |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
309 |
SOME (info as (_, result)) => |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
310 |
let |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
311 |
fun is_intro_of intro = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
312 |
let |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
313 |
val (const, _) = strip_comb (HOLogic.dest_Trueprop (concl_of intro)) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
314 |
in (fst (dest_Const const) = name) end; |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
315 |
val intros = map (preprocess_intro thy) (filter is_intro_of (#intrs result)) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
316 |
val elim = nth (#elims result) (find_index (fn s => s = name) (#names (fst info))) |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31580
diff
changeset
|
317 |
val nparams = length (Inductive.params_of (#raw_induct result)) |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
318 |
in (intros, elim, nparams) end |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
319 |
| NONE => error ("No such predicate: " ^ quote name) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
320 |
|
31514 | 321 |
(* updaters *) |
322 |
||
323 |
fun add_predfun name mode data = let |
|
324 |
val add = apsnd (cons (mode, mk_predfun_data data)) |
|
325 |
in PredData.map (Graph.map_node name (map_pred_data add)) end |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
326 |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
327 |
fun is_inductive_predicate thy name = |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
328 |
is_some (try (Inductive.the_inductive (ProofContext.init thy)) name) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
329 |
|
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
330 |
fun depending_preds_of thy intros = fold Term.add_consts (map Thm.prop_of intros) [] |> map fst |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
331 |
|> filter (fn c => is_inductive_predicate thy c orelse is_registered thy c) |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
332 |
|
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
333 |
(* code dependency graph *) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
334 |
fun dependencies_of thy name = |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
335 |
let |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
336 |
val (intros, elim, nparams) = fetch_pred_data thy name |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
337 |
val data = mk_pred_data ((intros, SOME elim, nparams), []) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
338 |
val keys = depending_preds_of thy intros |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
339 |
in |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
340 |
(data, keys) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
341 |
end; |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
342 |
|
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
343 |
(* TODO: add_edges - by analysing dependencies *) |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
344 |
fun add_intro thm thy = let |
31514 | 345 |
val (name, _) = dest_Const (fst (strip_intro_concl 0 (prop_of thm))) |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
346 |
fun cons_intro gr = |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
347 |
case try (Graph.get_node gr) name of |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
348 |
SOME pred_data => Graph.map_node name (map_pred_data |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
349 |
(apfst (fn (intro, elim, nparams) => (thm::intro, elim, nparams)))) gr |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
350 |
| NONE => |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
351 |
let |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
352 |
val nparams = the_default 0 (try (#3 o fetch_pred_data thy) name) |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
353 |
in Graph.new_node (name, mk_pred_data (([thm], NONE, nparams), [])) gr end; |
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
354 |
in PredData.map cons_intro thy end |
31514 | 355 |
|
356 |
fun set_elim thm = let |
|
357 |
val (name, _) = dest_Const (fst |
|
358 |
(strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm))))) |
|
359 |
fun set (intros, _, nparams) = (intros, SOME thm, nparams) |
|
360 |
in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
361 |
|
31514 | 362 |
fun set_nparams name nparams = let |
363 |
fun set (intros, elim, _ ) = (intros, elim, nparams) |
|
364 |
in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
365 |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
366 |
fun register_predicate (intros, elim, nparams) thy = let |
31514 | 367 |
val (name, _) = dest_Const (fst (strip_intro_concl nparams (prop_of (hd intros)))) |
368 |
fun set _ = (intros, SOME elim, nparams) |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
369 |
in |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
370 |
PredData.map (Graph.new_node (name, mk_pred_data ((intros, SOME elim, nparams), [])) |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
371 |
#> fold Graph.add_edge (map (pair name) (depending_preds_of thy intros))) thy |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
372 |
end |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
373 |
|
31514 | 374 |
|
375 |
(* Mode analysis *) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
376 |
|
31514 | 377 |
(*** check if a term contains only constructor functions ***) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
378 |
fun is_constrt thy = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
379 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
380 |
val cnstrs = flat (maps |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
381 |
(map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd) |
31784 | 382 |
(Symtab.dest (Datatype.get_all thy))); |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
383 |
fun check t = (case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
384 |
(Free _, []) => true |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
385 |
| (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
386 |
(SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
387 |
| _ => false) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
388 |
| _ => false) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
389 |
in check end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
390 |
|
31514 | 391 |
(*** check if a type is an equality type (i.e. doesn't contain fun) |
392 |
FIXME this is only an approximation ***) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
393 |
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
394 |
| is_eqT _ = true; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
395 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
396 |
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm []; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
397 |
val terms_vs = distinct (op =) o maps term_vs; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
398 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
399 |
(** collect all Frees in a term (with duplicates!) **) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
400 |
fun term_vTs tm = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
401 |
fold_aterms (fn Free xT => cons xT | _ => I) tm []; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
402 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
403 |
fun get_args is ts = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
404 |
fun get_args' _ _ [] = ([], []) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
405 |
| get_args' is i (t::ts) = (if i mem is then apfst else apsnd) (cons t) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
406 |
(get_args' is (i+1) ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
407 |
in get_args' is 1 ts end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
408 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
409 |
(*FIXME this function should not be named merge... make it local instead*) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
410 |
fun merge xs [] = xs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
411 |
| merge [] ys = ys |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
412 |
| merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
413 |
else y::merge (x::xs) ys; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
414 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
415 |
fun subsets i j = if i <= j then |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
416 |
let val is = subsets (i+1) j |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
417 |
in merge (map (fn ks => i::ks) is) is end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
418 |
else [[]]; |
31514 | 419 |
|
420 |
(* FIXME: should be in library - map_prod *) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
421 |
fun cprod ([], ys) = [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
422 |
| cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
423 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
424 |
fun cprods xss = foldr (map op :: o cprod) [[]] xss; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
425 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
426 |
datatype hmode = Mode of mode * int list * hmode option list; (*FIXME don't understand |
31514 | 427 |
why there is another mode type tmode !?*) |
428 |
||
31515 | 429 |
|
31514 | 430 |
(*TODO: cleanup function and put together with modes_of_term *) |
431 |
fun modes_of_param default modes t = let |
|
432 |
val (vs, t') = strip_abs t |
|
433 |
val b = length vs |
|
434 |
fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) => |
|
435 |
let |
|
436 |
val (args1, args2) = |
|
437 |
if length args < length iss then |
|
438 |
error ("Too few arguments for inductive predicate " ^ name) |
|
439 |
else chop (length iss) args; |
|
440 |
val k = length args2; |
|
31986 | 441 |
val perm = map (fn i => (find_index (fn t => t = Bound (b - i)) args2) + 1) |
442 |
(1 upto b) |
|
31514 | 443 |
val partial_mode = (1 upto k) \\ perm |
444 |
in |
|
445 |
if not (partial_mode subset is) then [] else |
|
446 |
let |
|
447 |
val is' = |
|
448 |
(fold_index (fn (i, j) => if j mem is then cons (i + 1) else I) perm []) |
|
449 |
|> fold (fn i => if i > k then cons (i - k + b) else I) is |
|
450 |
||
451 |
val res = map (fn x => Mode (m, is', x)) (cprods (map |
|
452 |
(fn (NONE, _) => [NONE] |
|
453 |
| (SOME js, arg) => map SOME (filter |
|
454 |
(fn Mode (_, js', _) => js=js') (modes_of_term modes arg))) |
|
455 |
(iss ~~ args1))) |
|
456 |
in res end |
|
457 |
end)) (AList.lookup op = modes name) |
|
458 |
in case strip_comb t' of |
|
459 |
(Const (name, _), args) => the_default default (mk_modes name args) |
|
460 |
| (Var ((name, _), _), args) => the (mk_modes name args) |
|
461 |
| (Free (name, _), args) => the (mk_modes name args) |
|
462 |
| _ => default end |
|
463 |
||
464 |
and modes_of_term modes t = |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
465 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
466 |
val ks = 1 upto length (binder_types (fastype_of t)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
467 |
val default = [Mode (([], ks), ks, [])]; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
468 |
fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
469 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
470 |
val (args1, args2) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
471 |
if length args < length iss then |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
472 |
error ("Too few arguments for inductive predicate " ^ name) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
473 |
else chop (length iss) args; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
474 |
val k = length args2; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
475 |
val prfx = 1 upto k |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
476 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
477 |
if not (is_prefix op = prfx is) then [] else |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
478 |
let val is' = map (fn i => i - k) (List.drop (is, k)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
479 |
in map (fn x => Mode (m, is', x)) (cprods (map |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
480 |
(fn (NONE, _) => [NONE] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
481 |
| (SOME js, arg) => map SOME (filter |
31170 | 482 |
(fn Mode (_, js', _) => js=js') (modes_of_term modes arg))) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
483 |
(iss ~~ args1))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
484 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
485 |
end)) (AList.lookup op = modes name) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
486 |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
487 |
in |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
488 |
case strip_comb (Envir.eta_contract t) of |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
489 |
(Const (name, _), args) => the_default default (mk_modes name args) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
490 |
| (Var ((name, _), _), args) => the (mk_modes name args) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
491 |
| (Free (name, _), args) => the (mk_modes name args) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
492 |
| (Abs _, []) => error "Abs at param position" (* modes_of_param default modes t *) |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
493 |
| _ => default |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
494 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
495 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
496 |
datatype indprem = Prem of term list * term | Negprem of term list * term | Sidecond of term; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
497 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
498 |
fun select_mode_prem thy modes vs ps = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
499 |
find_first (is_some o snd) (ps ~~ map |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
500 |
(fn Prem (us, t) => find_first (fn Mode (_, is, _) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
501 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
502 |
val (in_ts, out_ts) = get_args is us; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
503 |
val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
504 |
val vTs = maps term_vTs out_ts'; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
505 |
val dupTs = map snd (duplicates (op =) vTs) @ |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
506 |
List.mapPartial (AList.lookup (op =) vTs) vs; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
507 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
508 |
terms_vs (in_ts @ in_ts') subset vs andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
509 |
forall (is_eqT o fastype_of) in_ts' andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
510 |
term_vs t subset vs andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
511 |
forall is_eqT dupTs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
512 |
end) |
31170 | 513 |
(modes_of_term modes t handle Option => |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
514 |
error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
515 |
| Negprem (us, t) => find_first (fn Mode (_, is, _) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
516 |
length us = length is andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
517 |
terms_vs us subset vs andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
518 |
term_vs t subset vs) |
31170 | 519 |
(modes_of_term modes t handle Option => |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
520 |
error ("Bad predicate: " ^ Syntax.string_of_term_global thy t)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
521 |
| Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], [])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
522 |
else NONE |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
523 |
) ps); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
524 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
525 |
fun check_mode_clause thy param_vs modes (iss, is) (ts, ps) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
526 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
527 |
val modes' = modes @ List.mapPartial |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
528 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
529 |
(param_vs ~~ iss); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
530 |
fun check_mode_prems vs [] = SOME vs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
531 |
| check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
532 |
NONE => NONE |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
533 |
| SOME (x, _) => check_mode_prems |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
534 |
(case x of Prem (us, _) => vs union terms_vs us | _ => vs) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
535 |
(filter_out (equal x) ps)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
536 |
val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is ts)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
537 |
val in_vs = terms_vs in_ts; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
538 |
val concl_vs = terms_vs ts |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
539 |
val _ = Output.tracing ("ts :" ^ (commas (map (Syntax.string_of_term_global thy) ts))) |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
540 |
val _ = () |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
541 |
val ret = forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
542 |
forall (is_eqT o fastype_of) in_ts' andalso |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
543 |
(case check_mode_prems (param_vs union in_vs) ps of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
544 |
NONE => false |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
545 |
| SOME vs => concl_vs subset vs) |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
546 |
|
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
547 |
in |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
548 |
ret |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
549 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
550 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
551 |
fun check_modes_pred thy param_vs preds modes (p, ms) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
552 |
let val SOME rs = AList.lookup (op =) preds p |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
553 |
in (p, List.filter (fn m => case find_index |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
554 |
(not o check_mode_clause thy param_vs modes m) rs of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
555 |
~1 => true |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
556 |
| i => (Output.tracing ("Clause " ^ string_of_int (i+1) ^ " of " ^ |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
557 |
p ^ " violates mode " ^ string_of_mode m); false)) ms) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
558 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
559 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
560 |
fun fixp f (x : (string * mode list) list) = |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
561 |
let val y = f x |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
562 |
in if x = y then x else fixp f y end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
563 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
564 |
fun infer_modes thy extra_modes arities param_vs preds = fixp (fn modes => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
565 |
map (check_modes_pred thy param_vs preds (modes @ extra_modes)) modes) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
566 |
(map (fn (s, (ks, k)) => (s, cprod (cprods (map |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
567 |
(fn NONE => [NONE] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
568 |
| SOME k' => map SOME (subsets 1 k')) ks), |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
569 |
subsets 1 k))) arities); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
570 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
571 |
|
31514 | 572 |
(* term construction *) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
573 |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
574 |
(* Remark: types of param_funT_of and funT_of are swapped - which is the more |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
575 |
canonical order? *) |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
576 |
fun param_funT_of T NONE = T |
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
577 |
| param_funT_of T (SOME mode) = let |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
578 |
val Ts = binder_types T; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
579 |
val (Us1, Us2) = get_args mode Ts |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
580 |
in Us1 ---> (mk_predT (mk_tupleT Us2)) end; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
581 |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
582 |
fun funT_of (iss, is) T = let |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
583 |
val Ts = binder_types T |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
584 |
val (paramTs, argTs) = chop (length iss) Ts |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
585 |
val paramTs' = map2 (fn SOME is => funT_of ([], is) | NONE => I) iss paramTs |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
586 |
val (inargTs, outargTs) = get_args is argTs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
587 |
in |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
588 |
(paramTs' @ inargTs) ---> (mk_predT (mk_tupleT outargTs)) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
589 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
590 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
591 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
592 |
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of |
31514 | 593 |
NONE => (Free (s, T), (names, (s, [])::vs)) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
594 |
| SOME xs => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
595 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
596 |
val s' = Name.variant names s; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
597 |
val v = Free (s', T) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
598 |
in |
31514 | 599 |
(v, (s'::names, AList.update (op =) (s, v::xs) vs)) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
600 |
end); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
601 |
|
31514 | 602 |
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T |
603 |
| distinct_v (t $ u) nvs = |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
604 |
let |
31514 | 605 |
val (t', nvs') = distinct_v t nvs; |
606 |
val (u', nvs'') = distinct_v u nvs'; |
|
607 |
in (t' $ u', nvs'') end |
|
608 |
| distinct_v x nvs = (x, nvs); |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
609 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
610 |
fun compile_match thy eqs eqs' out_ts success_t = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
611 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
612 |
val eqs'' = maps mk_eq eqs @ eqs' |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
613 |
val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) []; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
614 |
val name = Name.variant names "x"; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
615 |
val name' = Name.variant (name :: names) "y"; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
616 |
val T = mk_tupleT (map fastype_of out_ts); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
617 |
val U = fastype_of success_t; |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
618 |
val U' = dest_predT U; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
619 |
val v = Free (name, T); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
620 |
val v' = Free (name', T); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
621 |
in |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31580
diff
changeset
|
622 |
lambda v (fst (Datatype.make_case |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
623 |
(ProofContext.init thy) false [] v |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
624 |
[(mk_tuple out_ts, |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
625 |
if null eqs'' then success_t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
626 |
else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $ |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
627 |
foldr1 HOLogic.mk_conj eqs'' $ success_t $ |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
628 |
mk_bot U'), |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
629 |
(v', mk_bot U')])) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
630 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
631 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
632 |
(*FIXME function can be removed*) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
633 |
fun mk_funcomp f t = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
634 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
635 |
val names = Term.add_free_names t []; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
636 |
val Ts = binder_types (fastype_of t); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
637 |
val vs = map Free |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
638 |
(Name.variant_list names (replicate (length Ts) "x") ~~ Ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
639 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
640 |
fold_rev lambda vs (f (list_comb (t, vs))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
641 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
642 |
|
31514 | 643 |
|
644 |
||
645 |
fun compile_param_ext thy modes (NONE, t) = t |
|
646 |
| compile_param_ext thy modes (m as SOME (Mode ((iss, is'), is, ms)), t) = |
|
647 |
let |
|
648 |
val (vs, u) = strip_abs t |
|
31515 | 649 |
val (ivs, ovs) = get_args is vs |
31514 | 650 |
val (f, args) = strip_comb u |
651 |
val (params, args') = chop (length ms) args |
|
652 |
val (inargs, outargs) = get_args is' args' |
|
653 |
val b = length vs |
|
31986 | 654 |
val perm = map (fn i => find_index (fn t => t = Bound (b - i)) args' + 1) (1 upto b) |
31514 | 655 |
val outp_perm = |
656 |
snd (get_args is perm) |
|
657 |
|> map (fn i => i - length (filter (fn x => x < i) is')) |
|
658 |
val names = [] (* TODO *) |
|
659 |
val out_names = Name.variant_list names (replicate (length outargs) "x") |
|
660 |
val f' = case f of |
|
661 |
Const (name, T) => |
|
662 |
if AList.defined op = modes name then |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
663 |
Const (predfun_name_of thy name (iss, is'), funT_of (iss, is') T) |
31514 | 664 |
else error "compile param: Not an inductive predicate with correct mode" |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
665 |
| Free (name, T) => Free (name, param_funT_of T (SOME is')) |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
666 |
val outTs = dest_tupleT (dest_predT (body_type (fastype_of f'))) |
31514 | 667 |
val out_vs = map Free (out_names ~~ outTs) |
668 |
val params' = map (compile_param thy modes) (ms ~~ params) |
|
669 |
val f_app = list_comb (f', params' @ inargs) |
|
670 |
val single_t = (mk_single (mk_tuple (map (fn i => nth out_vs (i - 1)) outp_perm))) |
|
671 |
val match_t = compile_match thy [] [] out_vs single_t |
|
672 |
in list_abs (ivs, |
|
673 |
mk_bind (f_app, match_t)) |
|
674 |
end |
|
675 |
| compile_param_ext _ _ _ = error "compile params" |
|
676 |
||
677 |
and compile_param thy modes (NONE, t) = t |
|
678 |
| compile_param thy modes (m as SOME (Mode ((iss, is'), is, ms)), t) = |
|
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
679 |
(* (case t of |
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
680 |
Abs _ => error "compile_param: Invalid term" *) (* compile_param_ext thy modes (m, t) *) |
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
681 |
(* | _ => let *) |
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
682 |
let |
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
683 |
val (f, args) = strip_comb (Envir.eta_contract t) |
31514 | 684 |
val (params, args') = chop (length ms) args |
685 |
val params' = map (compile_param thy modes) (ms ~~ params) |
|
686 |
val f' = case f of |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
687 |
Const (name, T) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
688 |
if AList.defined op = modes name then |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
689 |
Const (predfun_name_of thy name (iss, is'), funT_of (iss, is') T) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
690 |
else error "compile param: Not an inductive predicate with correct mode" |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
691 |
| Free (name, T) => Free (name, param_funT_of T (SOME is')) |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
692 |
in list_comb (f', params' @ args') end |
31514 | 693 |
| compile_param _ _ _ = error "compile params" |
694 |
||
695 |
||
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
696 |
fun compile_expr thy modes (SOME (Mode (mode, is, ms)), t) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
697 |
(case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
698 |
(Const (name, T), params) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
699 |
if AList.defined op = modes name then |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
700 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
701 |
val (Ts, Us) = get_args is |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
702 |
(curry Library.drop (length ms) (fst (strip_type T))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
703 |
val params' = map (compile_param thy modes) (ms ~~ params) |
31514 | 704 |
in list_comb (Const (predfun_name_of thy name mode, ((map fastype_of params') @ Ts) ---> |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
705 |
mk_predT (mk_tupleT Us)), params') |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
706 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
707 |
else error "not a valid inductive expression" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
708 |
| (Free (name, T), args) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
709 |
(*if name mem param_vs then *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
710 |
(* Higher order mode call *) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
711 |
let val r = Free (name, param_funT_of T (SOME is)) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
712 |
in list_comb (r, args) end) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
713 |
| compile_expr _ _ _ = error "not a valid inductive expression" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
714 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
715 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
716 |
fun compile_clause thy all_vs param_vs modes (iss, is) (ts, ps) inp = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
717 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
718 |
val modes' = modes @ List.mapPartial |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
719 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
720 |
(param_vs ~~ iss); |
31514 | 721 |
fun check_constrt t (names, eqs) = |
722 |
if is_constrt thy t then (t, (names, eqs)) else |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
723 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
724 |
val s = Name.variant names "x"; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
725 |
val v = Free (s, fastype_of t) |
31514 | 726 |
in (v, (s::names, HOLogic.mk_eq (v, t)::eqs)) end; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
727 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
728 |
val (in_ts, out_ts) = get_args is ts; |
31514 | 729 |
val (in_ts', (all_vs', eqs)) = |
730 |
fold_map check_constrt in_ts (all_vs, []); |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
731 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
732 |
fun compile_prems out_ts' vs names [] = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
733 |
let |
31514 | 734 |
val (out_ts'', (names', eqs')) = |
735 |
fold_map check_constrt out_ts' (names, []); |
|
736 |
val (out_ts''', (names'', constr_vs)) = fold_map distinct_v |
|
737 |
out_ts'' (names', map (rpair []) vs); |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
738 |
in |
31514 | 739 |
compile_match thy constr_vs (eqs @ eqs') out_ts''' |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
740 |
(mk_single (mk_tuple out_ts)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
741 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
742 |
| compile_prems out_ts vs names ps = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
743 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
744 |
val vs' = distinct (op =) (flat (vs :: map term_vs out_ts)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
745 |
val SOME (p, mode as SOME (Mode (_, js, _))) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
746 |
select_mode_prem thy modes' vs' ps |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
747 |
val ps' = filter_out (equal p) ps |
31514 | 748 |
val (out_ts', (names', eqs)) = |
749 |
fold_map check_constrt out_ts (names, []) |
|
750 |
val (out_ts'', (names'', constr_vs')) = fold_map distinct_v |
|
751 |
out_ts' ((names', map (rpair []) vs)) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
752 |
val (compiled_clause, rest) = case p of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
753 |
Prem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
754 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
755 |
val (in_ts, out_ts''') = get_args js us; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
756 |
val u = list_comb (compile_expr thy modes (mode, t), in_ts) |
31514 | 757 |
val rest = compile_prems out_ts''' vs' names'' ps' |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
758 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
759 |
(u, rest) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
760 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
761 |
| Negprem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
762 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
763 |
val (in_ts, out_ts''') = get_args js us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
764 |
val u = list_comb (compile_expr thy modes (mode, t), in_ts) |
31514 | 765 |
val rest = compile_prems out_ts''' vs' names'' ps' |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
766 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
767 |
(mk_not_pred u, rest) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
768 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
769 |
| Sidecond t => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
770 |
let |
31514 | 771 |
val rest = compile_prems [] vs' names'' ps'; |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
772 |
in |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
773 |
(mk_if_pred t, rest) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
774 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
775 |
in |
31514 | 776 |
compile_match thy constr_vs' eqs out_ts'' |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
777 |
(mk_bind (compiled_clause, rest)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
778 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
779 |
val prem_t = compile_prems in_ts' param_vs all_vs' ps; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
780 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
781 |
mk_bind (mk_single inp, prem_t) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
782 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
783 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
784 |
fun compile_pred thy all_vs param_vs modes s T cls mode = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
785 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
786 |
val Ts = binder_types T; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
787 |
val (Ts1, Ts2) = chop (length param_vs) Ts; |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
788 |
val Ts1' = map2 param_funT_of Ts1 (fst mode) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
789 |
val (Us1, Us2) = get_args (snd mode) Ts2; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
790 |
val xnames = Name.variant_list param_vs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
791 |
(map (fn i => "x" ^ string_of_int i) (snd mode)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
792 |
val xs = map2 (fn s => fn T => Free (s, T)) xnames Us1; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
793 |
val cl_ts = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
794 |
map (fn cl => compile_clause thy |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
795 |
all_vs param_vs modes mode cl (mk_tuple xs)) cls; |
31514 | 796 |
val mode_id = predfun_name_of thy s mode |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
797 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
798 |
HOLogic.mk_Trueprop (HOLogic.mk_eq |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
799 |
(list_comb (Const (mode_id, (Ts1' @ Us1) ---> |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
800 |
mk_predT (mk_tupleT Us2)), |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
801 |
map2 (fn s => fn T => Free (s, T)) param_vs Ts1' @ xs), |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
802 |
foldr1 mk_sup cl_ts)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
803 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
804 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
805 |
fun compile_preds thy all_vs param_vs modes preds = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
806 |
map (fn (s, (T, cls)) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
807 |
map (compile_pred thy all_vs param_vs modes s T cls) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
808 |
((the o AList.lookup (op =) modes) s)) preds; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
809 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
810 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
811 |
(* special setup for simpset *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
812 |
val HOL_basic_ss' = HOL_basic_ss setSolver |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
813 |
(mk_solver "all_tac_solver" (fn _ => fn _ => all_tac)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
814 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
815 |
|
31514 | 816 |
(* Definition of executable functions and their intro and elim rules *) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
817 |
|
30972
5b65835ccc92
some experiements towards user interface for predicate compiler
haftmann
parents:
30528
diff
changeset
|
818 |
fun print_arities arities = tracing ("Arities:\n" ^ |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
819 |
cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^ |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
820 |
space_implode " -> " (map |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
821 |
(fn NONE => "X" | SOME k' => string_of_int k') |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
822 |
(ks @ [SOME k]))) arities)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
823 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
824 |
fun mk_Eval_of ((x, T), NONE) names = (x, names) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
825 |
| mk_Eval_of ((x, T), SOME mode) names = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
826 |
val Ts = binder_types T |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
827 |
val argnames = Name.variant_list names |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
828 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts))); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
829 |
val args = map Free (argnames ~~ Ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
830 |
val (inargs, outargs) = get_args mode args |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
831 |
val r = mk_Eval (list_comb (x, inargs), mk_tuple outargs) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
832 |
val t = fold_rev lambda args r |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
833 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
834 |
(t, argnames @ names) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
835 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
836 |
|
31514 | 837 |
fun create_intro_elim_rule nparams mode defthm mode_id funT pred thy = |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
838 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
839 |
val Ts = binder_types (fastype_of pred) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
840 |
val funtrm = Const (mode_id, funT) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
841 |
val argnames = Name.variant_list [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
842 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts))); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
843 |
val (Ts1, Ts2) = chop nparams Ts; |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
844 |
val Ts1' = map2 param_funT_of Ts1 (fst mode) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
845 |
val args = map Free (argnames ~~ (Ts1' @ Ts2)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
846 |
val (params, io_args) = chop nparams args |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
847 |
val (inargs, outargs) = get_args (snd mode) io_args |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
848 |
val param_names = Name.variant_list argnames |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
849 |
(map (fn i => "p" ^ string_of_int i) (1 upto nparams)) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
850 |
val param_vs = map Free (param_names ~~ Ts1) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
851 |
val (params', names) = fold_map mk_Eval_of ((params ~~ Ts1) ~~ (fst mode)) [] |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
852 |
val predpropI = HOLogic.mk_Trueprop (list_comb (pred, param_vs @ io_args)) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
853 |
val predpropE = HOLogic.mk_Trueprop (list_comb (pred, params' @ io_args)) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
854 |
val param_eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (param_vs ~~ params') |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
855 |
val funargs = params @ inargs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
856 |
val funpropE = HOLogic.mk_Trueprop (mk_Eval (list_comb (funtrm, funargs), |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
857 |
if null outargs then Free("y", HOLogic.unitT) else mk_tuple outargs)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
858 |
val funpropI = HOLogic.mk_Trueprop (mk_Eval (list_comb (funtrm, funargs), |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
859 |
mk_tuple outargs)) |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
860 |
val introtrm = Logic.list_implies (predpropI :: param_eqs, funpropI) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
861 |
val _ = tracing (Syntax.string_of_term_global thy introtrm) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
862 |
val simprules = [defthm, @{thm eval_pred}, |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
863 |
@{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
864 |
val unfolddef_tac = (Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1) |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
865 |
val introthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ ["y"]) [] introtrm (fn {...} => unfolddef_tac) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
866 |
val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)); |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
867 |
val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
868 |
val elimthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ ["y", "P"]) [] elimtrm (fn {...} => unfolddef_tac) |
31514 | 869 |
in |
870 |
(introthm, elimthm) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
871 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
872 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
873 |
fun create_definitions preds nparams (name, modes) thy = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
874 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
875 |
val _ = tracing "create definitions" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
876 |
val T = AList.lookup (op =) preds name |> the |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
877 |
fun create_definition mode thy = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
878 |
fun string_of_mode mode = if null mode then "0" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
879 |
else space_implode "_" (map string_of_int mode) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
880 |
val HOmode = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
881 |
fun string_of_HOmode m s = case m of NONE => s | SOME mode => s ^ "__" ^ (string_of_mode mode) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
882 |
in (fold string_of_HOmode (fst mode) "") end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
883 |
val mode_id = name ^ (if HOmode = "" then "_" else HOmode ^ "___") |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
884 |
^ (string_of_mode (snd mode)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
885 |
val Ts = binder_types T; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
886 |
val (Ts1, Ts2) = chop nparams Ts; |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
887 |
val Ts1' = map2 param_funT_of Ts1 (fst mode) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
888 |
val (Us1, Us2) = get_args (snd mode) Ts2; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
889 |
val names = Name.variant_list [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
890 |
(map (fn i => "x" ^ string_of_int i) (1 upto (length Ts))); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
891 |
val xs = map Free (names ~~ (Ts1' @ Ts2)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
892 |
val (xparams, xargs) = chop nparams xs; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
893 |
val (xparams', names') = fold_map mk_Eval_of ((xparams ~~ Ts1) ~~ (fst mode)) names |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
894 |
val (xins, xouts) = get_args (snd mode) xargs; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
895 |
fun mk_split_lambda [] t = lambda (Free (Name.variant names' "x", HOLogic.unitT)) t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
896 |
| mk_split_lambda [x] t = lambda x t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
897 |
| mk_split_lambda xs t = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
898 |
fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
899 |
| mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
900 |
in mk_split_lambda' xs t end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
901 |
val predterm = mk_Enum (mk_split_lambda xouts (list_comb (Const (name, T), xparams' @ xargs))) |
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
902 |
val funT = (Ts1' @ Us1) ---> (mk_predT (mk_tupleT Us2)) |
30387 | 903 |
val mode_id = Sign.full_bname thy (Long_Name.base_name mode_id) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
904 |
val lhs = list_comb (Const (mode_id, funT), xparams @ xins) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
905 |
val def = Logic.mk_equals (lhs, predterm) |
31514 | 906 |
val ([definition], thy') = thy |> |
30387 | 907 |
Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_id), funT, NoSyn)] |> |
908 |
PureThy.add_defs false [((Binding.name (Long_Name.base_name mode_id ^ "_def"), def), [])] |
|
31514 | 909 |
val (intro, elim) = create_intro_elim_rule nparams mode definition mode_id funT (Const (name, T)) thy' |
910 |
in thy' |> add_predfun name mode (mode_id, definition, intro, elim) |
|
911 |
|> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "I"), intro) |> snd |
|
912 |
|> PureThy.store_thm (Binding.name (Long_Name.base_name mode_id ^ "E"), elim) |> snd |
|
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
913 |
|> Theory.checkpoint |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
914 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
915 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
916 |
fold create_definition modes thy |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
917 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
918 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
919 |
(**************************************************************************************) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
920 |
(* Proving equivalence of term *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
921 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
922 |
fun is_Type (Type _) = true |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
923 |
| is_Type _ = false |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
924 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
925 |
(* returns true if t is an application of an datatype constructor *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
926 |
(* which then consequently would be splitted *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
927 |
(* else false *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
928 |
fun is_constructor thy t = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
929 |
if (is_Type (fastype_of t)) then |
31784 | 930 |
(case Datatype.get_info thy ((fst o dest_Type o fastype_of) t) of |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
931 |
NONE => false |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
932 |
| SOME info => (let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
933 |
val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
934 |
val (c, _) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
935 |
in (case c of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
936 |
Const (name, _) => name mem_string constr_consts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
937 |
| _ => false) end)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
938 |
else false |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
939 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
940 |
(* MAJOR FIXME: prove_params should be simple |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
941 |
- different form of introrule for parameters ? *) |
31549 | 942 |
fun prove_param thy modes (NONE, t) = |
943 |
all_tac |
|
944 |
| prove_param thy modes (m as SOME (Mode (mode, is, ms)), t) = |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
945 |
REPEAT_DETERM (etac @{thm thin_rl} 1) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
946 |
THEN REPEAT_DETERM (rtac @{thm ext} 1) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
947 |
THEN (rtac @{thm iffI} 1) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
948 |
THEN print_tac "prove_param" |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
949 |
(* proof in one direction *) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
950 |
THEN (atac 1) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
951 |
(* proof in the other direction *) |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
952 |
THEN (atac 1) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
953 |
THEN print_tac "after prove_param" |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
954 |
(* let |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
955 |
val (f, args) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
956 |
val (params, _) = chop (length ms) args |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
957 |
val f_tac = case f of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
958 |
Const (name, T) => simp_tac (HOL_basic_ss addsimps |
31514 | 959 |
(@{thm eval_pred}::(predfun_definition_of thy name mode):: |
960 |
@{thm "Product_Type.split_conv"}::[])) 1 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
961 |
| Free _ => all_tac |
31514 | 962 |
| Abs _ => error "TODO: implement here" |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
963 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
964 |
print_tac "before simplification in prove_args:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
965 |
THEN f_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
966 |
THEN print_tac "after simplification in prove_args" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
967 |
THEN (EVERY (map (prove_param thy modes) (ms ~~ params))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
968 |
THEN (REPEAT_DETERM (atac 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
969 |
end |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
970 |
*) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
971 |
fun prove_expr thy modes (SOME (Mode (mode, is, ms)), t, us) (premposition : int) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
972 |
(case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
973 |
(Const (name, T), args) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
974 |
if AList.defined op = modes name then (let |
31514 | 975 |
val introrule = predfun_intro_of thy name mode |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
976 |
(*val (in_args, out_args) = get_args is us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
977 |
val (pred, rargs) = strip_comb (HOLogic.dest_Trueprop |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
978 |
(hd (Logic.strip_imp_prems (prop_of introrule)))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
979 |
val nparams = length ms (* get_nparams thy (fst (dest_Const pred)) *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
980 |
val (_, args) = chop nparams rargs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
981 |
val subst = map (pairself (cterm_of thy)) (args ~~ us) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
982 |
val inst_introrule = Drule.cterm_instantiate subst introrule*) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
983 |
(* the next line is old and probably wrong *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
984 |
val (args1, args2) = chop (length ms) args |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
985 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
986 |
rtac @{thm bindI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
987 |
THEN print_tac "before intro rule:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
988 |
(* for the right assumption in first position *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
989 |
THEN rotate_tac premposition 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
990 |
THEN rtac introrule 1 |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
991 |
THEN print_tac "after intro rule" |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
992 |
(* work with parameter arguments *) |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
993 |
THEN (atac 1) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
994 |
THEN (print_tac "parameter goal") |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
995 |
THEN (EVERY (map (prove_param thy modes) (ms ~~ args1))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
996 |
THEN (REPEAT_DETERM (atac 1)) end) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
997 |
else error "Prove expr if case not implemented" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
998 |
| _ => rtac @{thm bindI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
999 |
THEN atac 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1000 |
| prove_expr _ _ _ _ = error "Prove expr not implemented" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1001 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1002 |
fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1003 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1004 |
fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1005 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1006 |
fun prove_match thy (out_ts : term list) = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1007 |
fun get_case_rewrite t = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1008 |
if (is_constructor thy t) then let |
31784 | 1009 |
val case_rewrites = (#case_rewrites (Datatype.the_info thy |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1010 |
((fst o dest_Type o fastype_of) t))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1011 |
in case_rewrites @ (flat (map get_case_rewrite (snd (strip_comb t)))) end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1012 |
else [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1013 |
val simprules = @{thm "unit.cases"} :: @{thm "prod.cases"} :: (flat (map get_case_rewrite out_ts)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1014 |
(* replace TRY by determining if it necessary - are there equations when calling compile match? *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1015 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1016 |
(* make this simpset better! *) |
31515 | 1017 |
asm_simp_tac (HOL_basic_ss' addsimps simprules) 1 |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1018 |
THEN print_tac "after prove_match:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1019 |
THEN (DETERM (TRY (EqSubst.eqsubst_tac (ProofContext.init thy) [0] [@{thm "HOL.if_P"}] 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1020 |
THEN (REPEAT_DETERM (rtac @{thm conjI} 1 THEN (SOLVED (asm_simp_tac HOL_basic_ss 1)))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1021 |
THEN (SOLVED (asm_simp_tac HOL_basic_ss 1))))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1022 |
THEN print_tac "after if simplification" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1023 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1024 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1025 |
(* corresponds to compile_fun -- maybe call that also compile_sidecond? *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1026 |
|
31515 | 1027 |
fun prove_sidecond thy modes t = |
1028 |
let |
|
1029 |
fun preds_of t nameTs = case strip_comb t of |
|
1030 |
(f as Const (name, T), args) => |
|
1031 |
if AList.defined (op =) modes name then (name, T) :: nameTs |
|
1032 |
else fold preds_of args nameTs |
|
1033 |
| _ => nameTs |
|
1034 |
val preds = preds_of t [] |
|
1035 |
val defs = map |
|
1036 |
(fn (pred, T) => predfun_definition_of thy pred ([], (1 upto (length (binder_types T))))) |
|
1037 |
preds |
|
1038 |
in |
|
1039 |
(* remove not_False_eq_True when simpset in prove_match is better *) |
|
1040 |
simp_tac (HOL_basic_ss addsimps @{thm not_False_eq_True} :: @{thm eval_pred} :: defs) 1 |
|
1041 |
(* need better control here! *) |
|
1042 |
end |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1043 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1044 |
fun prove_clause thy nargs all_vs param_vs modes (iss, is) (ts, ps) = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1045 |
val modes' = modes @ List.mapPartial |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1046 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1047 |
(param_vs ~~ iss); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1048 |
fun check_constrt ((names, eqs), t) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1049 |
if is_constrt thy t then ((names, eqs), t) else |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1050 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1051 |
val s = Name.variant names "x"; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1052 |
val v = Free (s, fastype_of t) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1053 |
in ((s::names, HOLogic.mk_eq (v, t)::eqs), v) end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1054 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1055 |
val (in_ts, clause_out_ts) = get_args is ts; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1056 |
val ((all_vs', eqs), in_ts') = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1057 |
(*FIXME*) Library.foldl_map check_constrt ((all_vs, []), in_ts); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1058 |
fun prove_prems out_ts vs [] = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1059 |
(prove_match thy out_ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1060 |
THEN asm_simp_tac HOL_basic_ss' 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1061 |
THEN print_tac "before the last rule of singleI:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1062 |
THEN (rtac (if null clause_out_ts then @{thm singleI_unit} else @{thm singleI}) 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1063 |
| prove_prems out_ts vs rps = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1064 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1065 |
val vs' = distinct (op =) (flat (vs :: map term_vs out_ts)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1066 |
val SOME (p, mode as SOME (Mode ((iss, js), _, param_modes))) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1067 |
select_mode_prem thy modes' vs' rps; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1068 |
val premposition = (find_index (equal p) ps) + nargs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1069 |
val rps' = filter_out (equal p) rps; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1070 |
val rest_tac = (case p of Prem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1071 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1072 |
val (in_ts, out_ts''') = get_args js us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1073 |
val rec_tac = prove_prems out_ts''' vs' rps' |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1074 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1075 |
print_tac "before clause:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1076 |
THEN asm_simp_tac HOL_basic_ss 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1077 |
THEN print_tac "before prove_expr:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1078 |
THEN prove_expr thy modes (mode, t, us) premposition |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1079 |
THEN print_tac "after prove_expr:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1080 |
THEN rec_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1081 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1082 |
| Negprem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1083 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1084 |
val (in_ts, out_ts''') = get_args js us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1085 |
val rec_tac = prove_prems out_ts''' vs' rps' |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1086 |
val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1087 |
val (_, params) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1088 |
in |
31515 | 1089 |
rtac @{thm bindI} 1 |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1090 |
THEN (if (is_some name) then |
31514 | 1091 |
simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, js)]) 1 |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1092 |
THEN rtac @{thm not_predI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1093 |
(* FIXME: work with parameter arguments *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1094 |
THEN (EVERY (map (prove_param thy modes) (param_modes ~~ params))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1095 |
else |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1096 |
rtac @{thm not_predI'} 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1097 |
THEN (REPEAT_DETERM (atac 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1098 |
THEN rec_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1099 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1100 |
| Sidecond t => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1101 |
rtac @{thm bindI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1102 |
THEN rtac @{thm if_predI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1103 |
THEN print_tac "before sidecond:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1104 |
THEN prove_sidecond thy modes t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1105 |
THEN print_tac "after sidecond:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1106 |
THEN prove_prems [] vs' rps') |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1107 |
in (prove_match thy out_ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1108 |
THEN rest_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1109 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1110 |
val prems_tac = prove_prems in_ts' param_vs ps |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1111 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1112 |
rtac @{thm bindI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1113 |
THEN rtac @{thm singleI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1114 |
THEN prems_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1115 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1116 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1117 |
fun select_sup 1 1 = [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1118 |
| select_sup _ 1 = [rtac @{thm supI1}] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1119 |
| select_sup n i = (rtac @{thm supI2})::(select_sup (n - 1) (i - 1)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1120 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1121 |
fun prove_one_direction thy all_vs param_vs modes clauses ((pred, T), mode) = let |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31580
diff
changeset
|
1122 |
(* val ind_result = Inductive.the_inductive (ProofContext.init thy) pred |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1123 |
val index = find_index (fn s => s = pred) (#names (fst ind_result)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1124 |
val (_, T) = dest_Const (nth (#preds (snd ind_result)) index) *) |
31514 | 1125 |
val nargs = length (binder_types T) - nparams_of thy pred |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1126 |
val pred_case_rule = singleton (ind_set_codegen_preproc thy) |
31514 | 1127 |
(preprocess_elim thy nargs (the_elim_of thy pred)) |
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1128 |
(* FIXME preprocessor |> Simplifier.full_simplify (HOL_basic_ss addsimps [@{thm Predicate.memb_code}])*) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1129 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1130 |
REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})) |
31514 | 1131 |
THEN etac (predfun_elim_of thy pred mode) 1 |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1132 |
THEN etac pred_case_rule 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1133 |
THEN (EVERY (map |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1134 |
(fn i => EVERY' (select_sup (length clauses) i) i) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1135 |
(1 upto (length clauses)))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1136 |
THEN (EVERY (map (prove_clause thy nargs all_vs param_vs modes mode) clauses)) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1137 |
THEN print_tac "proved one direction" |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1138 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1139 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1140 |
(*******************************************************************************************************) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1141 |
(* Proof in the other direction ************************************************************************) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1142 |
(*******************************************************************************************************) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1143 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1144 |
fun prove_match2 thy out_ts = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1145 |
fun split_term_tac (Free _) = all_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1146 |
| split_term_tac t = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1147 |
if (is_constructor thy t) then let |
31784 | 1148 |
val info = Datatype.the_info thy ((fst o dest_Type o fastype_of) t) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1149 |
val num_of_constrs = length (#case_rewrites info) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1150 |
(* special treatment of pairs -- because of fishing *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1151 |
val split_rules = case (fst o dest_Type o fastype_of) t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1152 |
"*" => [@{thm prod.split_asm}] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1153 |
| _ => PureThy.get_thms thy (((fst o dest_Type o fastype_of) t) ^ ".split_asm") |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1154 |
val (_, ts) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1155 |
in |
31515 | 1156 |
(Splitter.split_asm_tac split_rules 1) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1157 |
(* THEN (Simplifier.asm_full_simp_tac HOL_basic_ss 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1158 |
THEN (DETERM (TRY (etac @{thm Pair_inject} 1))) *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1159 |
THEN (REPEAT_DETERM_N (num_of_constrs - 1) (etac @{thm botE} 1 ORELSE etac @{thm botE} 2)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1160 |
THEN (EVERY (map split_term_tac ts)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1161 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1162 |
else all_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1163 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1164 |
split_term_tac (mk_tuple out_ts) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1165 |
THEN (DETERM (TRY ((Splitter.split_asm_tac [@{thm "split_if_asm"}] 1) THEN (etac @{thm botE} 2)))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1166 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1167 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1168 |
(* VERY LARGE SIMILIRATIY to function prove_param |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1169 |
-- join both functions |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1170 |
*) |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1171 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1172 |
fun prove_param2 thy modes (NONE, t) = all_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1173 |
| prove_param2 thy modes (m as SOME (Mode (mode, is, ms)), t) = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1174 |
val (f, args) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1175 |
val (params, _) = chop (length ms) args |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1176 |
val f_tac = case f of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1177 |
Const (name, T) => full_simp_tac (HOL_basic_ss addsimps |
31514 | 1178 |
(@{thm eval_pred}::(predfun_definition_of thy name mode) |
1179 |
:: @{thm "Product_Type.split_conv"}::[])) 1 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1180 |
| Free _ => all_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1181 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1182 |
print_tac "before simplification in prove_args:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1183 |
THEN f_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1184 |
THEN print_tac "after simplification in prove_args" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1185 |
THEN (EVERY (map (prove_param2 thy modes) (ms ~~ params))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1186 |
end |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1187 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1188 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1189 |
fun prove_expr2 thy modes (SOME (Mode (mode, is, ms)), t) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1190 |
(case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1191 |
(Const (name, T), args) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1192 |
if AList.defined op = modes name then |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1193 |
etac @{thm bindE} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1194 |
THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1195 |
THEN print_tac "prove_expr2-before" |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1196 |
THEN (debug_tac (Syntax.string_of_term_global thy |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1197 |
(prop_of (predfun_elim_of thy name mode)))) |
31514 | 1198 |
THEN (etac (predfun_elim_of thy name mode) 1) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1199 |
THEN print_tac "prove_expr2" |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1200 |
(* TODO -- FIXME: replace remove_last_goal*) |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1201 |
(* THEN (EVERY (replicate (length args) (remove_last_goal thy))) *) |
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1202 |
THEN (EVERY (map (prove_param thy modes) (ms ~~ args))) |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1203 |
THEN print_tac "finished prove_expr2" |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1204 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1205 |
else error "Prove expr2 if case not implemented" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1206 |
| _ => etac @{thm bindE} 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1207 |
| prove_expr2 _ _ _ = error "Prove expr2 not implemented" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1208 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1209 |
fun prove_sidecond2 thy modes t = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1210 |
fun preds_of t nameTs = case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1211 |
(f as Const (name, T), args) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1212 |
if AList.defined (op =) modes name then (name, T) :: nameTs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1213 |
else fold preds_of args nameTs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1214 |
| _ => nameTs |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1215 |
val preds = preds_of t [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1216 |
val defs = map |
31514 | 1217 |
(fn (pred, T) => predfun_definition_of thy pred ([], (1 upto (length (binder_types T))))) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1218 |
preds |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1219 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1220 |
(* only simplify the one assumption *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1221 |
full_simp_tac (HOL_basic_ss' addsimps @{thm eval_pred} :: defs) 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1222 |
(* need better control here! *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1223 |
THEN print_tac "after sidecond2 simplification" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1224 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1225 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1226 |
fun prove_clause2 thy all_vs param_vs modes (iss, is) (ts, ps) pred i = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1227 |
val modes' = modes @ List.mapPartial |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1228 |
(fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1229 |
(param_vs ~~ iss); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1230 |
fun check_constrt ((names, eqs), t) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1231 |
if is_constrt thy t then ((names, eqs), t) else |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1232 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1233 |
val s = Name.variant names "x"; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1234 |
val v = Free (s, fastype_of t) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1235 |
in ((s::names, HOLogic.mk_eq (v, t)::eqs), v) end; |
31514 | 1236 |
val pred_intro_rule = nth (intros_of thy pred) (i - 1) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1237 |
|> preprocess_intro thy |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1238 |
|> (fn thm => hd (ind_set_codegen_preproc thy [thm])) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1239 |
(* FIXME preprocess |> Simplifier.full_simplify (HOL_basic_ss addsimps [@ {thm Predicate.memb_code}]) *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1240 |
val (in_ts, clause_out_ts) = get_args is ts; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1241 |
val ((all_vs', eqs), in_ts') = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1242 |
(*FIXME*) Library.foldl_map check_constrt ((all_vs, []), in_ts); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1243 |
fun prove_prems2 out_ts vs [] = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1244 |
print_tac "before prove_match2 - last call:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1245 |
THEN prove_match2 thy out_ts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1246 |
THEN print_tac "after prove_match2 - last call:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1247 |
THEN (etac @{thm singleE} 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1248 |
THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1249 |
THEN (asm_full_simp_tac HOL_basic_ss' 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1250 |
THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1251 |
THEN (asm_full_simp_tac HOL_basic_ss' 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1252 |
THEN SOLVED (print_tac "state before applying intro rule:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1253 |
THEN (rtac pred_intro_rule 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1254 |
(* How to handle equality correctly? *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1255 |
THEN (print_tac "state before assumption matching") |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1256 |
THEN (REPEAT (atac 1 ORELSE |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1257 |
(CHANGED (asm_full_simp_tac HOL_basic_ss' 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1258 |
THEN print_tac "state after simp_tac:")))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1259 |
| prove_prems2 out_ts vs ps = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1260 |
val vs' = distinct (op =) (flat (vs :: map term_vs out_ts)); |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1261 |
val SOME (p, mode as SOME (Mode ((iss, js), _, param_modes))) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1262 |
select_mode_prem thy modes' vs' ps; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1263 |
val ps' = filter_out (equal p) ps; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1264 |
val rest_tac = (case p of Prem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1265 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1266 |
val (in_ts, out_ts''') = get_args js us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1267 |
val rec_tac = prove_prems2 out_ts''' vs' ps' |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1268 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1269 |
(prove_expr2 thy modes (mode, t)) THEN rec_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1270 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1271 |
| Negprem (us, t) => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1272 |
let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1273 |
val (in_ts, out_ts''') = get_args js us |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1274 |
val rec_tac = prove_prems2 out_ts''' vs' ps' |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1275 |
val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1276 |
val (_, params) = strip_comb t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1277 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1278 |
print_tac "before neg prem 2" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1279 |
THEN etac @{thm bindE} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1280 |
THEN (if is_some name then |
31514 | 1281 |
full_simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, js)]) 1 |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1282 |
THEN etac @{thm not_predE} 1 |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1283 |
THEN (EVERY (map (prove_param thy modes) (param_modes ~~ params))) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1284 |
else |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1285 |
etac @{thm not_predE'} 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1286 |
THEN rec_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1287 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1288 |
| Sidecond t => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1289 |
etac @{thm bindE} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1290 |
THEN etac @{thm if_predE} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1291 |
THEN prove_sidecond2 thy modes t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1292 |
THEN prove_prems2 [] vs' ps') |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1293 |
in print_tac "before prove_match2:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1294 |
THEN prove_match2 thy out_ts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1295 |
THEN print_tac "after prove_match2:" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1296 |
THEN rest_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1297 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1298 |
val prems_tac = prove_prems2 in_ts' param_vs ps |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1299 |
in |
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1300 |
print_tac "starting prove_clause2" |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1301 |
THEN etac @{thm bindE} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1302 |
THEN (etac @{thm singleE'} 1) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1303 |
THEN (TRY (etac @{thm Pair_inject} 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1304 |
THEN print_tac "after singleE':" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1305 |
THEN prems_tac |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1306 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1307 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1308 |
fun prove_other_direction thy all_vs param_vs modes clauses (pred, mode) = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1309 |
fun prove_clause (clause, i) = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1310 |
(if i < length clauses then etac @{thm supE} 1 else all_tac) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1311 |
THEN (prove_clause2 thy all_vs param_vs modes mode clause pred i) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1312 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1313 |
(DETERM (TRY (rtac @{thm unit.induct} 1))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1314 |
THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all}))) |
31514 | 1315 |
THEN (rtac (predfun_intro_of thy pred mode) 1) |
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1316 |
THEN (REPEAT_DETERM (rtac @{thm refl} 2)) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1317 |
THEN (EVERY (map prove_clause (clauses ~~ (1 upto (length clauses))))) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1318 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1319 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1320 |
fun prove_pred thy all_vs param_vs modes clauses (((pred, T), mode), t) = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1321 |
val ctxt = ProofContext.init thy |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1322 |
val clauses' = the (AList.lookup (op =) clauses pred) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1323 |
in |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1324 |
Goal.prove ctxt (Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) t []) [] t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1325 |
(if !do_proofs then |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1326 |
(fn _ => |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1327 |
rtac @{thm pred_iffI} 1 |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1328 |
THEN prove_one_direction thy all_vs param_vs modes clauses' ((pred, T), mode) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1329 |
THEN print_tac "proved one direction" |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1330 |
THEN prove_other_direction thy all_vs param_vs modes clauses' (pred, mode) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1331 |
THEN print_tac "proved other direction") |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1332 |
else (fn _ => mycheat_tac thy 1)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1333 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1334 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1335 |
fun prove_preds thy all_vs param_vs modes clauses pmts = |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1336 |
map (prove_pred thy all_vs param_vs modes clauses) pmts |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1337 |
|
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1338 |
(* special case: inductive predicate with no clauses *) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1339 |
fun noclause (predname, T) thy = let |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1340 |
val Ts = binder_types T |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1341 |
val names = Name.variant_list [] |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1342 |
(map (fn i => "x" ^ (string_of_int i)) (1 upto (length Ts))) |
31124 | 1343 |
val vs = map2 (curry Free) names Ts |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1344 |
val clausehd = HOLogic.mk_Trueprop (list_comb(Const (predname, T), vs)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1345 |
val intro_t = Logic.mk_implies (@{prop False}, clausehd) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1346 |
val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1347 |
val elim_t = Logic.list_implies ([clausehd, Logic.mk_implies (@{prop False}, P)], P) |
31514 | 1348 |
val intro = Goal.prove (ProofContext.init thy) names [] intro_t |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1349 |
(fn {...} => etac @{thm FalseE} 1) |
31514 | 1350 |
val elim = Goal.prove (ProofContext.init thy) ("P" :: names) [] elim_t |
1351 |
(fn {...} => etac (the_elim_of thy predname) 1) |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1352 |
in |
31514 | 1353 |
add_intro intro thy |
1354 |
|> set_elim elim |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1355 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1356 |
|
31514 | 1357 |
fun prepare_intrs thy prednames = |
1358 |
let |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1359 |
(* FIXME: preprocessing moved to fetch_pred_data *) |
31514 | 1360 |
val intrs = map (preprocess_intro thy) (maps (intros_of thy) prednames) |
1361 |
|> ind_set_codegen_preproc thy (*FIXME preprocessor |
|
1362 |
|> map (Simplifier.full_simplify (HOL_basic_ss addsimps [@ {thm Predicate.memb_code}]))*) |
|
1363 |
|> map (Logic.unvarify o prop_of) |
|
1364 |
val nparams = nparams_of thy (hd prednames) |
|
1365 |
val preds = distinct (op =) (map (dest_Const o fst o (strip_intro_concl nparams)) intrs) |
|
1366 |
val extra_modes = all_modes_of thy |> filter_out (fn (name, _) => member (op =) prednames name) |
|
32307
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
1367 |
val _ = Output.tracing ("extra_modes are: " ^ |
55166cd57a6d
exported functions for quickcheck generator; renamed type constructing functions to fit with the type name in the Predicate theory
bulwahn
parents:
32306
diff
changeset
|
1368 |
cat_lines (map (fn (name, modes) => name ^ " has modes:" ^ (commas (map string_of_mode modes))) extra_modes)) |
31514 | 1369 |
val _ $ u = Logic.strip_imp_concl (hd intrs); |
1370 |
val params = List.take (snd (strip_comb u), nparams); |
|
1371 |
val param_vs = maps term_vs params |
|
1372 |
val all_vs = terms_vs intrs |
|
1373 |
fun dest_prem t = |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1374 |
(case strip_comb t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1375 |
(v as Free _, ts) => if v mem params then Prem (ts, v) else Sidecond t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1376 |
| (c as Const (@{const_name Not}, _), [t]) => (case dest_prem t of |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1377 |
Prem (ts, t) => Negprem (ts, t) |
31515 | 1378 |
| Negprem _ => error ("Double negation not allowed in premise: " ^ (Syntax.string_of_term_global thy (c $ t))) |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1379 |
| Sidecond t => Sidecond (c $ t)) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1380 |
| (c as Const (s, _), ts) => |
31877
e3de75d3b898
exported is_registered; added debug messages; removed extended parameter compilation in predicate compiler
bulwahn
parents:
31876
diff
changeset
|
1381 |
if is_registered thy s then |
31514 | 1382 |
let val (ts1, ts2) = chop (nparams_of thy s) ts |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1383 |
in Prem (ts2, list_comb (c, ts1)) end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1384 |
else Sidecond t |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1385 |
| _ => Sidecond t) |
31514 | 1386 |
fun add_clause intr (clauses, arities) = |
1387 |
let |
|
1388 |
val _ $ t = Logic.strip_imp_concl intr; |
|
1389 |
val (Const (name, T), ts) = strip_comb t; |
|
1390 |
val (ts1, ts2) = chop nparams ts; |
|
1391 |
val prems = map (dest_prem o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr); |
|
1392 |
val (Ts, Us) = chop nparams (binder_types T) |
|
1393 |
in |
|
1394 |
(AList.update op = (name, these (AList.lookup op = clauses name) @ |
|
1395 |
[(ts2, prems)]) clauses, |
|
1396 |
AList.update op = (name, (map (fn U => (case strip_type U of |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1397 |
(Rs as _ :: _, Type ("bool", [])) => SOME (length Rs) |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1398 |
| _ => NONE)) Ts, |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1399 |
length Us)) arities) |
31514 | 1400 |
end; |
1401 |
val (clauses, arities) = fold add_clause intrs ([], []); |
|
1402 |
in (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) end; |
|
1403 |
||
1404 |
fun arrange kvs = |
|
1405 |
let |
|
1406 |
fun add (key, value) table = |
|
1407 |
AList.update op = (key, these (AList.lookup op = table key) @ [value]) table |
|
1408 |
in fold add kvs [] end; |
|
1409 |
||
1410 |
(* main function *) |
|
1411 |
||
1412 |
fun add_equations_of prednames thy = |
|
1413 |
let |
|
1414 |
val _ = tracing ("starting add_equations with " ^ commas prednames ^ "...") |
|
1415 |
(* null clause handling *) |
|
1416 |
(* |
|
1417 |
val thy' = fold (fn pred as (predname, T) => fn thy => |
|
1418 |
if null (intros_of thy predname) then noclause pred thy else thy) preds thy |
|
1419 |
*) |
|
1420 |
val (preds, nparams, all_vs, param_vs, extra_modes, clauses, arities) = |
|
1421 |
prepare_intrs thy prednames |
|
1422 |
val _ = tracing "Infering modes..." |
|
1423 |
val modes = infer_modes thy extra_modes arities param_vs clauses |
|
31549 | 1424 |
val _ = print_modes modes |
31514 | 1425 |
val _ = tracing "Defining executable functions..." |
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1426 |
val thy' = fold (create_definitions preds nparams) modes thy |> Theory.checkpoint |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1427 |
val clauses' = map (fn (s, cls) => (s, (the (AList.lookup (op =) preds s), cls))) clauses |
31514 | 1428 |
val _ = tracing "Compiling equations..." |
1429 |
val ts = compile_preds thy' all_vs param_vs (extra_modes @ modes) clauses' |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1430 |
(* val _ = map (tracing o (Syntax.string_of_term_global thy')) (flat ts) *) |
31514 | 1431 |
val pred_mode = |
1432 |
maps (fn (s, (T, _)) => map (pair (s, T)) ((the o AList.lookup (op =) modes) s)) clauses' |
|
32306
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents:
32287
diff
changeset
|
1433 |
val _ = tracing "Proving equations..." |
31514 | 1434 |
val result_thms = |
1435 |
prove_preds thy' all_vs param_vs (extra_modes @ modes) clauses (pred_mode ~~ (flat ts)) |
|
1436 |
val thy'' = fold (fn (name, result_thms) => fn thy => snd (PureThy.add_thmss |
|
1437 |
[((Binding.qualify true (Long_Name.base_name name) (Binding.name "equation"), result_thms), |
|
1438 |
[Attrib.attribute_i thy Code.add_default_eqn_attrib])] thy)) |
|
1439 |
(arrange ((map (fn ((name, _), _) => name) pred_mode) ~~ result_thms)) thy' |
|
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1440 |
|> Theory.checkpoint |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1441 |
in |
31514 | 1442 |
thy'' |
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1443 |
end |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1444 |
|
31106 | 1445 |
(* generation of case rules from user-given introduction rules *) |
1446 |
||
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1447 |
fun mk_casesrule ctxt nparams introrules = |
31124 | 1448 |
let |
31573
0047df9eb347
improved infrastructure of predicate compiler for adding manual introduction rules
bulwahn
parents:
31551
diff
changeset
|
1449 |
val intros = map (Logic.unvarify o prop_of) introrules |
31514 | 1450 |
val (pred, (params, args)) = strip_intro_concl nparams (hd intros) |
31106 | 1451 |
val ([propname], ctxt1) = Variable.variant_fixes ["thesis"] ctxt |
1452 |
val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT)) |
|
1453 |
val (argnames, ctxt2) = Variable.variant_fixes |
|
1454 |
(map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt1 |
|
31514 | 1455 |
val argvs = map2 (curry Free) argnames (map fastype_of args) |
31106 | 1456 |
fun mk_case intro = let |
31514 | 1457 |
val (_, (_, args)) = strip_intro_concl nparams intro |
31106 | 1458 |
val prems = Logic.strip_imp_prems intro |
1459 |
val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args) |
|
1460 |
val frees = (fold o fold_aterms) |
|
1461 |
(fn t as Free _ => |
|
1462 |
if member (op aconv) params t then I else insert (op aconv) t |
|
1463 |
| _ => I) (args @ prems) [] |
|
1464 |
in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end |
|
1465 |
val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs)) |
|
1466 |
val cases = map mk_case intros |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1467 |
in Logic.list_implies (assm :: cases, prop) end; |
31106 | 1468 |
|
31514 | 1469 |
fun add_equations name thy = |
1470 |
let |
|
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1471 |
val thy' = PredData.map (Graph.extend (dependencies_of thy) name) thy |> Theory.checkpoint; |
31514 | 1472 |
(*val preds = Graph.all_preds (PredData.get thy') [name] |> filter_out (has_elim thy') *) |
1473 |
fun strong_conn_of gr keys = |
|
1474 |
Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr) |
|
1475 |
val scc = strong_conn_of (PredData.get thy') [name] |
|
31524
8abf99ab669c
avoiding duplicate definitions of executable functions
bulwahn
parents:
31517
diff
changeset
|
1476 |
val thy'' = fold_rev |
8abf99ab669c
avoiding duplicate definitions of executable functions
bulwahn
parents:
31517
diff
changeset
|
1477 |
(fn preds => fn thy => |
8abf99ab669c
avoiding duplicate definitions of executable functions
bulwahn
parents:
31517
diff
changeset
|
1478 |
if forall (null o modes_of thy) preds then add_equations_of preds thy else thy) |
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1479 |
scc thy' |> Theory.checkpoint |
31514 | 1480 |
in thy'' end |
31124 | 1481 |
|
31876
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1482 |
|
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1483 |
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1484 |
|
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1485 |
val code_pred_intros_attrib = attrib add_intro; |
9ab571673059
added diagnostic printing; changed proof for parameters; moved code
bulwahn
parents:
31784
diff
changeset
|
1486 |
|
31124 | 1487 |
(** user interface **) |
1488 |
||
1489 |
local |
|
1490 |
||
31514 | 1491 |
(* TODO: make TheoryDataFun to GenericDataFun & remove duplication of local theory and theory *) |
1492 |
(* TODO: must create state to prove multiple cases *) |
|
31124 | 1493 |
fun generic_code_pred prep_const raw_const lthy = |
1494 |
let |
|
31551
995d6b90e9d6
making isatest happy; but misunderstanding remains
bulwahn
parents:
31550
diff
changeset
|
1495 |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1496 |
val thy = ProofContext.theory_of lthy |
31124 | 1497 |
val const = prep_const thy raw_const |
31551
995d6b90e9d6
making isatest happy; but misunderstanding remains
bulwahn
parents:
31550
diff
changeset
|
1498 |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1499 |
val lthy' = LocalTheory.theory (PredData.map (Graph.extend (dependencies_of thy) const)) lthy |
31554
a6c6bf2751a0
added sporadic (Local)Theory.checkpoint, to enable parallel proof checking;
wenzelm
parents:
31550
diff
changeset
|
1500 |
|> LocalTheory.checkpoint |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1501 |
val thy' = ProofContext.theory_of lthy' |
31514 | 1502 |
val preds = Graph.all_preds (PredData.get thy') [const] |> filter_out (has_elim thy') |
31551
995d6b90e9d6
making isatest happy; but misunderstanding remains
bulwahn
parents:
31550
diff
changeset
|
1503 |
|
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1504 |
fun mk_cases const = |
31514 | 1505 |
let |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1506 |
val nparams = nparams_of thy' const |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1507 |
val intros = intros_of thy' const |
31580 | 1508 |
in mk_casesrule lthy' nparams intros end |
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1509 |
val cases_rules = map mk_cases preds |
31580 | 1510 |
val cases = |
1511 |
map (fn case_rule => RuleCases.Case {fixes = [], |
|
1512 |
assumes = [("", Logic.strip_imp_prems case_rule)], |
|
1513 |
binds = [], cases = []}) cases_rules |
|
1514 |
val case_env = map2 (fn p => fn c => (Long_Name.base_name p, SOME c)) preds cases |
|
1515 |
val lthy'' = ProofContext.add_cases true case_env lthy' |
|
1516 |
||
31550
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1517 |
fun after_qed thms = |
b63d253ed9e2
code_pred command now also requires proofs for dependent predicates; changed handling of parameters in introrules of executable function
bulwahn
parents:
31549
diff
changeset
|
1518 |
LocalTheory.theory (fold set_elim (map the_single thms) #> add_equations const) |
31124 | 1519 |
in |
31580 | 1520 |
Proof.theorem_i NONE after_qed (map (single o (rpair [])) cases_rules) lthy'' |
31124 | 1521 |
end; |
1522 |
||
1523 |
structure P = OuterParse |
|
31106 | 1524 |
|
31124 | 1525 |
in |
1526 |
||
1527 |
val code_pred = generic_code_pred (K I); |
|
31156 | 1528 |
val code_pred_cmd = generic_code_pred Code.read_const |
31124 | 1529 |
|
31514 | 1530 |
val setup = PredData.put (Graph.empty) #> |
1531 |
Attrib.setup @{binding code_pred_intros} (Scan.succeed (attrib add_intro)) |
|
1532 |
"adding alternative introduction rules for code generation of inductive predicates" |
|
1533 |
(* Attrib.setup @{binding code_ind_cases} (Scan.succeed add_elim_attrib) |
|
31124 | 1534 |
"adding alternative elimination rules for code generation of inductive predicates"; |
31514 | 1535 |
*) |
31124 | 1536 |
(*FIXME name discrepancy in attribs and ML code*) |
1537 |
(*FIXME intros should be better named intro*) |
|
31514 | 1538 |
(*FIXME why distinguished attribute for cases?*) |
31124 | 1539 |
|
1540 |
val _ = OuterSyntax.local_theory_to_proof "code_pred" |
|
1541 |
"prove equations for predicate specified by intro/elim rules" |
|
1542 |
OuterKeyword.thy_goal (P.term_group >> code_pred_cmd) |
|
1543 |
||
1544 |
end |
|
1545 |
||
1546 |
(*FIXME |
|
1547 |
- Naming of auxiliary rules necessary? |
|
31217 | 1548 |
- add default code equations P x y z = P_i_i_i x y z |
31124 | 1549 |
*) |
31106 | 1550 |
|
31169
f4c61cbea49d
added predicate transformation function for code generation
bulwahn
parents:
31107
diff
changeset
|
1551 |
(* transformation for code generation *) |
f4c61cbea49d
added predicate transformation function for code generation
bulwahn
parents:
31107
diff
changeset
|
1552 |
|
31217 | 1553 |
val eval_ref = ref (NONE : (unit -> term Predicate.pred) option); |
1554 |
||
1555 |
fun analyze_compr thy t_compr = |
|
1556 |
let |
|
1557 |
val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t |
|
1558 |
| _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr); |
|
32287
65d5c5b30747
cleaned up abstract tuple operations and named them consistently
haftmann
parents:
32091
diff
changeset
|
1559 |
val (body, Ts, fp) = HOLogic.strip_splits split; |
31217 | 1560 |
(*FIXME former order of tuple positions must be restored*) |
1561 |
val (pred as Const (name, T), all_args) = strip_comb body |
|
31514 | 1562 |
val (params, args) = chop (nparams_of thy name) all_args |
31217 | 1563 |
val user_mode = map_filter I (map_index |
1564 |
(fn (i, t) => case t of Bound j => if j < length Ts then NONE |
|
1565 |
else SOME (i+1) | _ => SOME (i+1)) args) (*FIXME dangling bounds should not occur*) |
|
1566 |
val (inargs, _) = get_args user_mode args; |
|
1567 |
val modes = filter (fn Mode (_, is, _) => is = user_mode) |
|
31514 | 1568 |
(modes_of_term (all_modes_of thy) (list_comb (pred, params))); |
31217 | 1569 |
val m = case modes |
1570 |
of [] => error ("No mode possible for comprehension " |
|
1571 |
^ Syntax.string_of_term_global thy t_compr) |
|
1572 |
| [m] => m |
|
1573 |
| m :: _ :: _ => (warning ("Multiple modes possible for comprehension " |
|
1574 |
^ Syntax.string_of_term_global thy t_compr); m); |
|
31514 | 1575 |
val t_eval = list_comb (compile_expr thy (all_modes_of thy) (SOME m, list_comb (pred, params)), |
31217 | 1576 |
inargs) |
1577 |
in t_eval end; |
|
31169
f4c61cbea49d
added predicate transformation function for code generation
bulwahn
parents:
31107
diff
changeset
|
1578 |
|
30374
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1579 |
end; |
7311a1546d85
added predicate compiler, as formally checked prototype, not as user package
haftmann
parents:
diff
changeset
|
1580 |