TFL/post.ML
changeset 10769 70b9b0cfe05f
child 11038 932d66879fe7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TFL/post.ML	Wed Jan 03 21:20:40 2001 +0100
     1.3 @@ -0,0 +1,229 @@
     1.4 +(*  Title:      TFL/post.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Konrad Slind, Cambridge University Computer Laboratory
     1.7 +    Copyright   1997  University of Cambridge
     1.8 +
     1.9 +Second part of main module (postprocessing of TFL definitions).
    1.10 +*)
    1.11 +
    1.12 +signature TFL =
    1.13 +sig
    1.14 +  val trace: bool ref
    1.15 +  val quiet_mode: bool ref
    1.16 +  val message: string -> unit
    1.17 +  val tgoalw: theory -> thm list -> thm list -> thm list
    1.18 +  val tgoal: theory -> thm list -> thm list
    1.19 +  val std_postprocessor: claset -> simpset -> thm list -> theory ->
    1.20 +    {induction: thm, rules: thm, TCs: term list list} ->
    1.21 +    {induction: thm, rules: thm, nested_tcs: thm list}
    1.22 +  val define_i: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
    1.23 +    term -> term list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
    1.24 +  val define: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
    1.25 +    string -> string list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
    1.26 +  val defer_i: theory -> thm list -> xstring -> term list -> theory * thm
    1.27 +  val defer: theory -> thm list -> xstring -> string list -> theory * thm
    1.28 +end;
    1.29 +
    1.30 +structure Tfl: TFL =
    1.31 +struct
    1.32 +
    1.33 +structure S = USyntax
    1.34 +
    1.35 +
    1.36 +(* messages *)
    1.37 +
    1.38 +val trace = Prim.trace
    1.39 +
    1.40 +val quiet_mode = ref false;
    1.41 +fun message s = if ! quiet_mode then () else writeln s;
    1.42 +
    1.43 +
    1.44 +(* misc *)
    1.45 +
    1.46 +fun read_term thy = Sign.simple_read_term (Theory.sign_of thy) HOLogic.termT;
    1.47 +
    1.48 +
    1.49 +(*---------------------------------------------------------------------------
    1.50 + * Extract termination goals so that they can be put it into a goalstack, or
    1.51 + * have a tactic directly applied to them.
    1.52 + *--------------------------------------------------------------------------*)
    1.53 +fun termination_goals rules =
    1.54 +    map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)
    1.55 +      (foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
    1.56 +
    1.57 +(*---------------------------------------------------------------------------
    1.58 + * Finds the termination conditions in (highly massaged) definition and
    1.59 + * puts them into a goalstack.
    1.60 + *--------------------------------------------------------------------------*)
    1.61 +fun tgoalw thy defs rules =
    1.62 +  case termination_goals rules of
    1.63 +      [] => error "tgoalw: no termination conditions to prove"
    1.64 +    | L  => goalw_cterm defs
    1.65 +              (Thm.cterm_of (Theory.sign_of thy)
    1.66 +                        (HOLogic.mk_Trueprop(USyntax.list_mk_conj L)));
    1.67 +
    1.68 +fun tgoal thy = tgoalw thy [];
    1.69 +
    1.70 +(*---------------------------------------------------------------------------
    1.71 + * Three postprocessors are applied to the definition.  It
    1.72 + * attempts to prove wellfoundedness of the given relation, simplifies the
    1.73 + * non-proved termination conditions, and finally attempts to prove the
    1.74 + * simplified termination conditions.
    1.75 + *--------------------------------------------------------------------------*)
    1.76 +fun std_postprocessor cs ss wfs =
    1.77 +  Prim.postprocess
    1.78 +   {wf_tac     = REPEAT (ares_tac wfs 1),
    1.79 +    terminator = asm_simp_tac ss 1
    1.80 +                 THEN TRY (fast_tac (cs addSDs [not0_implies_Suc] addss ss) 1),
    1.81 +    simplifier = Rules.simpl_conv ss []};
    1.82 +
    1.83 +
    1.84 +
    1.85 +val concl = #2 o Rules.dest_thm;
    1.86 +
    1.87 +(*---------------------------------------------------------------------------
    1.88 + * Postprocess a definition made by "define". This is a separate stage of
    1.89 + * processing from the definition stage.
    1.90 + *---------------------------------------------------------------------------*)
    1.91 +local
    1.92 +structure R = Rules
    1.93 +structure U = Utils
    1.94 +
    1.95 +(* The rest of these local definitions are for the tricky nested case *)
    1.96 +val solved = not o can S.dest_eq o #2 o S.strip_forall o concl
    1.97 +
    1.98 +fun id_thm th =
    1.99 +   let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
   1.100 +   in lhs aconv rhs end
   1.101 +   handle U.ERR _ => false;
   1.102 +   
   1.103 +
   1.104 +fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
   1.105 +val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
   1.106 +val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
   1.107 +fun mk_meta_eq r = case concl_of r of
   1.108 +     Const("==",_)$_$_ => r
   1.109 +  |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
   1.110 +  |   _ => r RS P_imp_P_eq_True
   1.111 +
   1.112 +(*Is this the best way to invoke the simplifier??*)
   1.113 +fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
   1.114 +
   1.115 +fun join_assums th =
   1.116 +  let val {sign,...} = rep_thm th
   1.117 +      val tych = cterm_of sign
   1.118 +      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
   1.119 +      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
   1.120 +      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
   1.121 +      val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
   1.122 +  in
   1.123 +    R.GEN_ALL
   1.124 +      (R.DISCH_ALL
   1.125 +         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
   1.126 +  end
   1.127 +  val gen_all = S.gen_all
   1.128 +in
   1.129 +fun proof_stage cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
   1.130 +  let
   1.131 +    val _ = message "Proving induction theorem ..."
   1.132 +    val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
   1.133 +    val _ = message "Postprocessing ...";
   1.134 +    val {rules, induction, nested_tcs} =
   1.135 +      std_postprocessor cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
   1.136 +  in
   1.137 +  case nested_tcs
   1.138 +  of [] => {induction=induction, rules=rules,tcs=[]}
   1.139 +  | L  => let val dummy = message "Simplifying nested TCs ..."
   1.140 +              val (solved,simplified,stubborn) =
   1.141 +               U.itlist (fn th => fn (So,Si,St) =>
   1.142 +                     if (id_thm th) then (So, Si, th::St) else
   1.143 +                     if (solved th) then (th::So, Si, St)
   1.144 +                     else (So, th::Si, St)) nested_tcs ([],[],[])
   1.145 +              val simplified' = map join_assums simplified
   1.146 +              val rewr = full_simplify (ss addsimps (solved @ simplified'));
   1.147 +              val induction' = rewr induction
   1.148 +              and rules'     = rewr rules
   1.149 +          in
   1.150 +          {induction = induction',
   1.151 +               rules = rules',
   1.152 +                 tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
   1.153 +                           (simplified@stubborn)}
   1.154 +          end
   1.155 +  end;
   1.156 +
   1.157 +
   1.158 +(*lcp: curry the predicate of the induction rule*)
   1.159 +fun curry_rule rl = split_rule_var
   1.160 +                        (head_of (HOLogic.dest_Trueprop (concl_of rl)),
   1.161 +                         rl);
   1.162 +
   1.163 +(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
   1.164 +val meta_outer =
   1.165 +    curry_rule o standard o
   1.166 +    rule_by_tactic (REPEAT
   1.167 +                    (FIRSTGOAL (resolve_tac [allI, impI, conjI]
   1.168 +                                ORELSE' etac conjE)));
   1.169 +
   1.170 +(*Strip off the outer !P*)
   1.171 +val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
   1.172 +
   1.173 +fun simplify_defn thy cs ss congs wfs id pats def0 =
   1.174 +   let val def = freezeT def0 RS meta_eq_to_obj_eq
   1.175 +       val {theory,rules,rows,TCs,full_pats_TCs} = Prim.post_definition congs (thy, (def,pats))
   1.176 +       val {lhs=f,rhs} = S.dest_eq (concl def)
   1.177 +       val (_,[R,_]) = S.strip_comb rhs
   1.178 +       val {induction, rules, tcs} =
   1.179 +             proof_stage cs ss wfs theory
   1.180 +               {f = f, R = R, rules = rules,
   1.181 +                full_pats_TCs = full_pats_TCs,
   1.182 +                TCs = TCs}
   1.183 +       val rules' = map (standard o Rulify.rulify_no_asm) (R.CONJUNCTS rules)
   1.184 +   in  {induct = meta_outer (Rulify.rulify_no_asm (induction RS spec')),
   1.185 +        rules = ListPair.zip(rules', rows),
   1.186 +        tcs = (termination_goals rules') @ tcs}
   1.187 +   end
   1.188 +  handle U.ERR {mesg,func,module} =>
   1.189 +               error (mesg ^
   1.190 +                      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
   1.191 +
   1.192 +(*---------------------------------------------------------------------------
   1.193 + * Defining a function with an associated termination relation.
   1.194 + *---------------------------------------------------------------------------*)
   1.195 +fun define_i thy cs ss congs wfs fid R eqs =
   1.196 +  let val {functional,pats} = Prim.mk_functional thy eqs
   1.197 +      val (thy, def) = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional
   1.198 +  in (thy, simplify_defn thy cs ss congs wfs fid pats def) end;
   1.199 +
   1.200 +fun define thy cs ss congs wfs fid R seqs =
   1.201 +  define_i thy cs ss congs wfs fid (read_term thy R) (map (read_term thy) seqs)
   1.202 +    handle U.ERR {mesg,...} => error mesg;
   1.203 +
   1.204 +
   1.205 +(*---------------------------------------------------------------------------
   1.206 + *
   1.207 + *     Definitions with synthesized termination relation
   1.208 + *
   1.209 + *---------------------------------------------------------------------------*)
   1.210 +
   1.211 +fun func_of_cond_eqn tm =
   1.212 +  #1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));
   1.213 +
   1.214 +fun defer_i thy congs fid eqs =
   1.215 + let val {rules,R,theory,full_pats_TCs,SV,...} =
   1.216 +             Prim.lazyR_def thy (Sign.base_name fid) congs eqs
   1.217 +     val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));
   1.218 +     val dummy = message "Proving induction theorem ...";
   1.219 +     val induction = Prim.mk_induction theory
   1.220 +                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
   1.221 + in (theory,
   1.222 +     (*return the conjoined induction rule and recursion equations,
   1.223 +       with assumptions remaining to discharge*)
   1.224 +     standard (induction RS (rules RS conjI)))
   1.225 + end
   1.226 +
   1.227 +fun defer thy congs fid seqs =
   1.228 +  defer_i thy congs fid (map (read_term thy) seqs)
   1.229 +    handle U.ERR {mesg,...} => error mesg;
   1.230 +end;
   1.231 +
   1.232 +end;