10769

1 
(* Title: TFL/post.ML


2 
ID: $Id$


3 
Author: Konrad Slind, Cambridge University Computer Laboratory


4 
Copyright 1997 University of Cambridge


5 


6 
Second part of main module (postprocessing of TFL definitions).


7 
*)


8 


9 
signature TFL =


10 
sig


11 
val trace: bool ref


12 
val quiet_mode: bool ref


13 
val message: string > unit


14 
val tgoalw: theory > thm list > thm list > thm list


15 
val tgoal: theory > thm list > thm list

11632

16 
val define_i: bool > theory > claset > simpset > thm list > thm list > xstring >

10769

17 
term > term list > theory * {rules: (thm * int) list, induct: thm, tcs: term list}

11632

18 
val define: bool > theory > claset > simpset > thm list > thm list > xstring >

10769

19 
string > string list > theory * {rules: (thm * int) list, induct: thm, tcs: term list}


20 
val defer_i: theory > thm list > xstring > term list > theory * thm


21 
val defer: theory > thm list > xstring > string list > theory * thm


22 
end;


23 


24 
structure Tfl: TFL =


25 
struct


26 


27 
structure S = USyntax


28 


29 


30 
(* messages *)


31 


32 
val trace = Prim.trace


33 


34 
val quiet_mode = ref false;


35 
fun message s = if ! quiet_mode then () else writeln s;


36 


37 


38 
(* misc *)


39 


40 
fun read_term thy = Sign.simple_read_term (Theory.sign_of thy) HOLogic.termT;


41 


42 


43 
(*


44 
* Extract termination goals so that they can be put it into a goalstack, or


45 
* have a tactic directly applied to them.


46 
**)


47 
fun termination_goals rules =


48 
map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)


49 
(foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));


50 


51 
(*


52 
* Finds the termination conditions in (highly massaged) definition and


53 
* puts them into a goalstack.


54 
**)


55 
fun tgoalw thy defs rules =


56 
case termination_goals rules of


57 
[] => error "tgoalw: no termination conditions to prove"


58 
 L => goalw_cterm defs


59 
(Thm.cterm_of (Theory.sign_of thy)


60 
(HOLogic.mk_Trueprop(USyntax.list_mk_conj L)));


61 


62 
fun tgoal thy = tgoalw thy [];


63 


64 
(*


65 
* Three postprocessors are applied to the definition. It


66 
* attempts to prove wellfoundedness of the given relation, simplifies the


67 
* nonproved termination conditions, and finally attempts to prove the


68 
* simplified termination conditions.


69 
**)

11632

70 
fun std_postprocessor strict cs ss wfs =


71 
Prim.postprocess strict

10769

72 
{wf_tac = REPEAT (ares_tac wfs 1),


73 
terminator = asm_simp_tac ss 1


74 
THEN TRY (fast_tac (cs addSDs [not0_implies_Suc] addss ss) 1),


75 
simplifier = Rules.simpl_conv ss []};


76 


77 


78 


79 
val concl = #2 o Rules.dest_thm;


80 


81 
(*


82 
* Postprocess a definition made by "define". This is a separate stage of


83 
* processing from the definition stage.


84 
**)


85 
local


86 
structure R = Rules


87 
structure U = Utils


88 


89 
(* The rest of these local definitions are for the tricky nested case *)


90 
val solved = not o can S.dest_eq o #2 o S.strip_forall o concl


91 


92 
fun id_thm th =


93 
let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));


94 
in lhs aconv rhs end


95 
handle U.ERR _ => false;


96 


97 


98 
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);


99 
val P_imp_P_iff_True = prover "P > (P= True)" RS mp;


100 
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;


101 
fun mk_meta_eq r = case concl_of r of


102 
Const("==",_)$_$_ => r


103 
 _ $(Const("op =",_)$_$_) => r RS eq_reflection


104 
 _ => r RS P_imp_P_eq_True


105 


106 
(*Is this the best way to invoke the simplifier??*)


107 
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))


108 


109 
fun join_assums th =


110 
let val {sign,...} = rep_thm th


111 
val tych = cterm_of sign


112 
val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))


113 
val cntxtl = (#1 o S.strip_imp) lhs (* cntxtl should = cntxtr *)


114 
val cntxtr = (#1 o S.strip_imp) rhs (* but union is solider *)


115 
val cntxt = gen_union (op aconv) (cntxtl, cntxtr)


116 
in


117 
R.GEN_ALL


118 
(R.DISCH_ALL


119 
(rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))


120 
end


121 
val gen_all = S.gen_all


122 
in

11632

123 
fun proof_stage strict cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =

10769

124 
let


125 
val _ = message "Proving induction theorem ..."


126 
val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}


127 
val _ = message "Postprocessing ...";


128 
val {rules, induction, nested_tcs} =

11632

129 
std_postprocessor strict cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}

10769

130 
in


131 
case nested_tcs


132 
of [] => {induction=induction, rules=rules,tcs=[]}


133 
 L => let val dummy = message "Simplifying nested TCs ..."


134 
val (solved,simplified,stubborn) =


135 
U.itlist (fn th => fn (So,Si,St) =>


136 
if (id_thm th) then (So, Si, th::St) else


137 
if (solved th) then (th::So, Si, St)


138 
else (So, th::Si, St)) nested_tcs ([],[],[])


139 
val simplified' = map join_assums simplified


140 
val rewr = full_simplify (ss addsimps (solved @ simplified'));


141 
val induction' = rewr induction


142 
and rules' = rewr rules


143 
in


144 
{induction = induction',


145 
rules = rules',


146 
tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)


147 
(simplified@stubborn)}


148 
end


149 
end;


150 


151 


152 
(*lcp: curry the predicate of the induction rule*)

11038

153 
fun curry_rule rl =


154 
SplitRule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl)), rl);

10769

155 


156 
(*lcp: put a theorem into Isabelle form, using metalevel connectives*)


157 
val meta_outer =

11038

158 
curry_rule o standard o


159 
rule_by_tactic (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));

10769

160 


161 
(*Strip off the outer !P*)


162 
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;


163 

11632

164 
fun simplify_defn strict thy cs ss congs wfs id pats def0 =

10769

165 
let val def = freezeT def0 RS meta_eq_to_obj_eq


166 
val {theory,rules,rows,TCs,full_pats_TCs} = Prim.post_definition congs (thy, (def,pats))


167 
val {lhs=f,rhs} = S.dest_eq (concl def)


168 
val (_,[R,_]) = S.strip_comb rhs


169 
val {induction, rules, tcs} =

11632

170 
proof_stage strict cs ss wfs theory

10769

171 
{f = f, R = R, rules = rules,


172 
full_pats_TCs = full_pats_TCs,


173 
TCs = TCs}

11771

174 
val rules' = map (standard o ObjectLogic.rulify_no_asm) (R.CONJUNCTS rules)


175 
in {induct = meta_outer (ObjectLogic.rulify_no_asm (induction RS spec')),

10769

176 
rules = ListPair.zip(rules', rows),


177 
tcs = (termination_goals rules') @ tcs}


178 
end


179 
handle U.ERR {mesg,func,module} =>


180 
error (mesg ^


181 
"\n (In TFL function " ^ module ^ "." ^ func ^ ")");


182 


183 
(*


184 
* Defining a function with an associated termination relation.


185 
**)

11632

186 
fun define_i strict thy cs ss congs wfs fid R eqs =

10769

187 
let val {functional,pats} = Prim.mk_functional thy eqs


188 
val (thy, def) = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional

11632

189 
in (thy, simplify_defn strict thy cs ss congs wfs fid pats def) end;

10769

190 

11632

191 
fun define strict thy cs ss congs wfs fid R seqs =


192 
define_i strict thy cs ss congs wfs fid (read_term thy R) (map (read_term thy) seqs)

10769

193 
handle U.ERR {mesg,...} => error mesg;


194 


195 


196 
(*


197 
*


198 
* Definitions with synthesized termination relation


199 
*


200 
**)


201 


202 
fun func_of_cond_eqn tm =


203 
#1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));


204 


205 
fun defer_i thy congs fid eqs =


206 
let val {rules,R,theory,full_pats_TCs,SV,...} =


207 
Prim.lazyR_def thy (Sign.base_name fid) congs eqs


208 
val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));


209 
val dummy = message "Proving induction theorem ...";


210 
val induction = Prim.mk_induction theory


211 
{fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}


212 
in (theory,


213 
(*return the conjoined induction rule and recursion equations,


214 
with assumptions remaining to discharge*)


215 
standard (induction RS (rules RS conjI)))


216 
end


217 


218 
fun defer thy congs fid seqs =


219 
defer_i thy congs fid (map (read_term thy) seqs)


220 
handle U.ERR {mesg,...} => error mesg;


221 
end;


222 


223 
end;
