3191
|
1 |
signature TFL =
|
|
2 |
sig
|
2112
|
3 |
structure Prim : TFL_sig
|
|
4 |
|
3191
|
5 |
val tgoalw : theory -> thm list -> thm list -> thm list
|
|
6 |
val tgoal: theory -> thm list -> thm list
|
2112
|
7 |
|
|
8 |
val WF_TAC : thm list -> tactic
|
|
9 |
|
|
10 |
val simplifier : thm -> thm
|
|
11 |
val std_postprocessor : theory
|
|
12 |
-> {induction:thm, rules:thm, TCs:term list list}
|
|
13 |
-> {induction:thm, rules:thm, nested_tcs:thm list}
|
|
14 |
|
3191
|
15 |
val define_i : theory -> term -> term -> theory * (thm * Prim.pattern list)
|
|
16 |
val define : theory -> string -> string list -> theory * Prim.pattern list
|
2112
|
17 |
|
3191
|
18 |
val simplify_defn : theory * (string * Prim.pattern list)
|
|
19 |
-> {rules:thm list, induct:thm, tcs:term list}
|
2112
|
20 |
|
3191
|
21 |
(*-------------------------------------------------------------------------
|
|
22 |
val function : theory -> term -> {theory:theory, eq_ind : thm}
|
|
23 |
val lazyR_def: theory -> term -> {theory:theory, eqns : thm}
|
|
24 |
*-------------------------------------------------------------------------*)
|
2112
|
25 |
|
|
26 |
val tflcongs : theory -> thm list
|
|
27 |
|
3191
|
28 |
end;
|
|
29 |
|
|
30 |
|
|
31 |
structure Tfl: TFL =
|
2112
|
32 |
struct
|
|
33 |
structure Prim = Prim
|
3191
|
34 |
structure S = Prim.USyntax
|
2112
|
35 |
|
3191
|
36 |
(*---------------------------------------------------------------------------
|
|
37 |
* Extract termination goals so that they can be put it into a goalstack, or
|
|
38 |
* have a tactic directly applied to them.
|
|
39 |
*--------------------------------------------------------------------------*)
|
|
40 |
fun termination_goals rules =
|
|
41 |
map (Logic.freeze_vars o S.drop_Trueprop)
|
|
42 |
(foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
|
|
43 |
|
|
44 |
(*---------------------------------------------------------------------------
|
|
45 |
* Finds the termination conditions in (highly massaged) definition and
|
|
46 |
* puts them into a goalstack.
|
|
47 |
*--------------------------------------------------------------------------*)
|
|
48 |
fun tgoalw thy defs rules =
|
|
49 |
let val L = termination_goals rules
|
2112
|
50 |
open USyntax
|
|
51 |
val g = cterm_of (sign_of thy) (mk_prop(list_mk_conj L))
|
|
52 |
in goalw_cterm defs g
|
|
53 |
end;
|
|
54 |
|
|
55 |
val tgoal = Utils.C tgoalw [];
|
|
56 |
|
3191
|
57 |
(*---------------------------------------------------------------------------
|
|
58 |
* Simple wellfoundedness prover.
|
|
59 |
*--------------------------------------------------------------------------*)
|
2112
|
60 |
fun WF_TAC thms = REPEAT(FIRST1(map rtac thms))
|
3208
|
61 |
val WFtac = WF_TAC[wf_measure, wf_inv_image, wf_lex_prod, wf_less_than,
|
2112
|
62 |
wf_pred_nat, wf_pred_list, wf_trancl];
|
|
63 |
|
3208
|
64 |
val terminator = simp_tac(!simpset addsimps [less_than_def, pred_nat_def,
|
|
65 |
pred_list_def]) 1
|
|
66 |
THEN TRY(best_tac
|
|
67 |
(!claset addSDs [not0_implies_Suc]
|
|
68 |
addIs [r_into_trancl,
|
|
69 |
trans_trancl RS transD]
|
|
70 |
addss (!simpset)) 1);
|
3191
|
71 |
|
2112
|
72 |
val simpls = [less_eq RS eq_reflection,
|
3208
|
73 |
lex_prod_def, rprod_def, measure_def, inv_image_def];
|
2112
|
74 |
|
3191
|
75 |
(*---------------------------------------------------------------------------
|
|
76 |
* Does some standard things with the termination conditions of a definition:
|
|
77 |
* attempts to prove wellfoundedness of the given relation; simplifies the
|
|
78 |
* non-proven termination conditions; and finally attempts to prove the
|
|
79 |
* simplified termination conditions.
|
|
80 |
*--------------------------------------------------------------------------*)
|
2112
|
81 |
val std_postprocessor = Prim.postprocess{WFtac = WFtac,
|
|
82 |
terminator = terminator,
|
|
83 |
simplifier = Prim.Rules.simpl_conv simpls};
|
|
84 |
|
3208
|
85 |
val simplifier = rewrite_rule (simpls @ #simps(rep_ss (!simpset)) @
|
|
86 |
[less_than_def, pred_nat_def, pred_list_def]);
|
3191
|
87 |
|
2112
|
88 |
fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy));
|
|
89 |
|
|
90 |
|
|
91 |
val concl = #2 o Prim.Rules.dest_thm;
|
|
92 |
|
|
93 |
(*---------------------------------------------------------------------------
|
3191
|
94 |
* Defining a function with an associated termination relation.
|
|
95 |
*---------------------------------------------------------------------------*)
|
|
96 |
fun define_i thy R eqs =
|
|
97 |
let val dummy = require_thy thy "WF_Rel" "recursive function definitions";
|
|
98 |
|
|
99 |
val {functional,pats} = Prim.mk_functional thy eqs
|
|
100 |
val (thm,thry) = Prim.wfrec_definition0 thy R functional
|
|
101 |
in (thry,(thm,pats))
|
|
102 |
end;
|
|
103 |
|
|
104 |
(*lcp's version: takes strings; doesn't return "thm"
|
|
105 |
(whose signature is a draft and therefore useless) *)
|
|
106 |
fun define thy R eqs =
|
|
107 |
let fun read thy = readtm (sign_of thy) (TVar(("DUMMY",0),[]))
|
|
108 |
val (thy',(_,pats)) =
|
|
109 |
define_i thy (read thy R)
|
|
110 |
(fold_bal (app Ind_Syntax.conj) (map (read thy) eqs))
|
|
111 |
in (thy',pats) end
|
|
112 |
handle Utils.ERR {mesg,...} => error mesg;
|
|
113 |
|
|
114 |
(*---------------------------------------------------------------------------
|
|
115 |
* Postprocess a definition made by "define". This is a separate stage of
|
|
116 |
* processing from the definition stage.
|
2112
|
117 |
*---------------------------------------------------------------------------*)
|
|
118 |
local
|
|
119 |
structure R = Prim.Rules
|
|
120 |
structure U = Utils
|
|
121 |
|
3191
|
122 |
(* The rest of these local definitions are for the tricky nested case *)
|
2112
|
123 |
val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl
|
|
124 |
|
|
125 |
fun id_thm th =
|
|
126 |
let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th))))
|
|
127 |
in S.aconv lhs rhs
|
|
128 |
end handle _ => false
|
|
129 |
|
|
130 |
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
|
|
131 |
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
|
|
132 |
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
|
|
133 |
fun mk_meta_eq r = case concl_of r of
|
|
134 |
Const("==",_)$_$_ => r
|
|
135 |
| _$(Const("op =",_)$_$_) => r RS eq_reflection
|
|
136 |
| _ => r RS P_imp_P_eq_True
|
|
137 |
fun rewrite L = rewrite_rule (map mk_meta_eq (Utils.filter(not o id_thm) L))
|
3191
|
138 |
fun reducer thl = rewrite (map standard thl @ #simps(rep_ss HOL_ss))
|
2112
|
139 |
|
|
140 |
fun join_assums th =
|
|
141 |
let val {sign,...} = rep_thm th
|
|
142 |
val tych = cterm_of sign
|
|
143 |
val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
|
|
144 |
val cntxtl = (#1 o S.strip_imp) lhs (* cntxtl should = cntxtr *)
|
|
145 |
val cntxtr = (#1 o S.strip_imp) rhs (* but union is solider *)
|
|
146 |
val cntxt = U.union S.aconv cntxtl cntxtr
|
|
147 |
in
|
3191
|
148 |
R.GEN_ALL
|
|
149 |
(R.DISCH_ALL
|
|
150 |
(rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
|
2112
|
151 |
end
|
|
152 |
val gen_all = S.gen_all
|
|
153 |
in
|
3191
|
154 |
(*---------------------------------------------------------------------------
|
|
155 |
* The "reducer" argument is
|
|
156 |
* (fn thl => rewrite (map standard thl @ #simps(rep_ss HOL_ss)));
|
|
157 |
*---------------------------------------------------------------------------*)
|
|
158 |
fun proof_stage theory reducer {f, R, rules, full_pats_TCs, TCs} =
|
|
159 |
let val dummy = output(std_out, "Proving induction theorem.. ")
|
|
160 |
val ind = Prim.mk_induction theory f R full_pats_TCs
|
|
161 |
val dummy = output(std_out, "Proved induction theorem.\n")
|
|
162 |
val pp = std_postprocessor theory
|
|
163 |
val dummy = output(std_out, "Postprocessing.. ")
|
|
164 |
val {rules,induction,nested_tcs} = pp{rules=rules,induction=ind,TCs=TCs}
|
|
165 |
in
|
|
166 |
case nested_tcs
|
|
167 |
of [] => (output(std_out, "Postprocessing done.\n");
|
|
168 |
{induction=induction, rules=rules,tcs=[]})
|
|
169 |
| L => let val dummy = output(std_out, "Simplifying nested TCs.. ")
|
2112
|
170 |
val (solved,simplified,stubborn) =
|
|
171 |
U.itlist (fn th => fn (So,Si,St) =>
|
|
172 |
if (id_thm th) then (So, Si, th::St) else
|
|
173 |
if (solved th) then (th::So, Si, St)
|
|
174 |
else (So, th::Si, St)) nested_tcs ([],[],[])
|
|
175 |
val simplified' = map join_assums simplified
|
|
176 |
val induction' = reducer (solved@simplified') induction
|
|
177 |
val rules' = reducer (solved@simplified') rules
|
3191
|
178 |
val dummy = output(std_out, "Postprocessing done.\n")
|
2112
|
179 |
in
|
|
180 |
{induction = induction',
|
|
181 |
rules = rules',
|
3191
|
182 |
tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
|
|
183 |
(simplified@stubborn)}
|
2112
|
184 |
end
|
3191
|
185 |
end handle (e as Utils.ERR _) => Utils.Raise e
|
|
186 |
| e => print_exn e;
|
|
187 |
|
|
188 |
|
|
189 |
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
|
|
190 |
val meta_outer =
|
|
191 |
standard o rule_by_tactic (REPEAT_FIRST (resolve_tac [allI, impI, conjI]));
|
|
192 |
|
|
193 |
|
|
194 |
(*Strip off the outer !P*)
|
|
195 |
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
|
2112
|
196 |
|
|
197 |
|
3191
|
198 |
fun simplify_defn (thy,(id,pats)) =
|
3208
|
199 |
let val dummy = deny (id mem map ! (stamps_of_thy thy))
|
|
200 |
("Recursive definition " ^ id ^
|
|
201 |
" would clash with the theory of the same name!")
|
|
202 |
val def = freezeT(get_def thy id RS meta_eq_to_obj_eq)
|
3191
|
203 |
val {theory,rules,TCs,full_pats_TCs,patterns} =
|
|
204 |
Prim.post_definition (thy,(def,pats))
|
|
205 |
val {lhs=f,rhs} = S.dest_eq(concl def)
|
|
206 |
val (_,[R,_]) = S.strip_comb rhs
|
|
207 |
val {induction, rules, tcs} =
|
|
208 |
proof_stage theory reducer
|
|
209 |
{f = f, R = R, rules = rules,
|
|
210 |
full_pats_TCs = full_pats_TCs,
|
|
211 |
TCs = TCs}
|
|
212 |
val rules' = map (standard o normalize_thm [RSmp]) (R.CONJUNCTS rules)
|
|
213 |
in {induct = meta_outer
|
|
214 |
(normalize_thm [RSspec,RSmp] (induction RS spec')),
|
|
215 |
rules = rules',
|
|
216 |
tcs = (termination_goals rules') @ tcs}
|
|
217 |
end
|
|
218 |
handle Utils.ERR {mesg,...} => error mesg
|
2112
|
219 |
end;
|
|
220 |
|
3191
|
221 |
(*---------------------------------------------------------------------------
|
|
222 |
*
|
|
223 |
* Definitions with synthesized termination relation temporarily
|
|
224 |
* deleted -- it's not clear how to integrate this facility with
|
|
225 |
* the Isabelle theory file scheme, which restricts
|
|
226 |
* inference at theory-construction time.
|
|
227 |
*
|
2112
|
228 |
|
3208
|
229 |
local structure R = Prim.Rules
|
2112
|
230 |
in
|
|
231 |
fun function theory eqs =
|
3208
|
232 |
let val dummy = prs "Making definition.. "
|
2112
|
233 |
val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs
|
|
234 |
val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules))
|
3208
|
235 |
val dummy = prs "Definition made.\n"
|
|
236 |
val dummy = prs "Proving induction theorem.. "
|
2112
|
237 |
val induction = Prim.mk_induction theory f R full_pats_TCs
|
3208
|
238 |
val dummy = prs "Induction theorem proved.\n"
|
2112
|
239 |
in {theory = theory,
|
|
240 |
eq_ind = standard (induction RS (rules RS conjI))}
|
|
241 |
end
|
|
242 |
handle (e as Utils.ERR _) => Utils.Raise e
|
|
243 |
| e => print_exn e
|
|
244 |
end;
|
|
245 |
|
|
246 |
|
|
247 |
fun lazyR_def theory eqs =
|
|
248 |
let val {rules,theory, ...} = Prim.lazyR_def theory eqs
|
|
249 |
in {eqns=rules, theory=theory}
|
|
250 |
end
|
|
251 |
handle (e as Utils.ERR _) => Utils.Raise e
|
|
252 |
| e => print_exn e;
|
3191
|
253 |
*
|
|
254 |
*
|
|
255 |
*---------------------------------------------------------------------------*)
|
|
256 |
|
|
257 |
|
2112
|
258 |
|
|
259 |
|
3191
|
260 |
(*---------------------------------------------------------------------------
|
|
261 |
* Install the basic context notions. Others (for nat and list and prod)
|
|
262 |
* have already been added in thry.sml
|
|
263 |
*---------------------------------------------------------------------------*)
|
|
264 |
val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG];
|
2112
|
265 |
|
|
266 |
end;
|