TFL/post.ML
author nipkow
Wed May 26 14:57:06 2004 +0200 (2004-05-26)
changeset 14804 8de39d3e8eb6
parent 14240 d3843feb9de7
child 15150 c7af682b9ee5
permissions -rw-r--r--
Corrected printer bug for bounded quantifiers Q x<=y. P
     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
    16   val define_i: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->
    17     term -> term list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
    18   val define: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->
    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 val read_term = Thm.term_of oo (HOLogic.read_cterm o Theory.sign_of);
    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  * non-proved termination conditions, and finally attempts to prove the
    68  * simplified termination conditions.
    69  *--------------------------------------------------------------------------*)
    70 fun std_postprocessor strict cs ss wfs =
    71   Prim.postprocess strict
    72    {wf_tac     = REPEAT (ares_tac wfs 1),
    73     terminator = asm_simp_tac ss 1
    74                  THEN TRY (silent_arith_tac 1 ORELSE
    75                            fast_tac (cs addSDs [not0_implies_Suc] addss ss) 1),
    76     simplifier = Rules.simpl_conv ss []};
    77 
    78 
    79 
    80 val concl = #2 o Rules.dest_thm;
    81 
    82 (*---------------------------------------------------------------------------
    83  * Postprocess a definition made by "define". This is a separate stage of
    84  * processing from the definition stage.
    85  *---------------------------------------------------------------------------*)
    86 local
    87 structure R = Rules
    88 structure U = Utils
    89 
    90 (* The rest of these local definitions are for the tricky nested case *)
    91 val solved = not o can S.dest_eq o #2 o S.strip_forall o concl
    92 
    93 fun id_thm th =
    94    let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
    95    in lhs aconv rhs end
    96    handle U.ERR _ => false;
    97    
    98 
    99 fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
   100 val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
   101 val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
   102 fun mk_meta_eq r = case concl_of r of
   103      Const("==",_)$_$_ => r
   104   |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
   105   |   _ => r RS P_imp_P_eq_True
   106 
   107 (*Is this the best way to invoke the simplifier??*)
   108 fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
   109 
   110 fun join_assums th =
   111   let val {sign,...} = rep_thm th
   112       val tych = cterm_of sign
   113       val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
   114       val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
   115       val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
   116       val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
   117   in
   118     R.GEN_ALL
   119       (R.DISCH_ALL
   120          (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
   121   end
   122   val gen_all = S.gen_all
   123 in
   124 fun proof_stage strict cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
   125   let
   126     val _ = message "Proving induction theorem ..."
   127     val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
   128     val _ = message "Postprocessing ...";
   129     val {rules, induction, nested_tcs} =
   130       std_postprocessor strict cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
   131   in
   132   case nested_tcs
   133   of [] => {induction=induction, rules=rules,tcs=[]}
   134   | L  => let val dummy = message "Simplifying nested TCs ..."
   135               val (solved,simplified,stubborn) =
   136                U.itlist (fn th => fn (So,Si,St) =>
   137                      if (id_thm th) then (So, Si, th::St) else
   138                      if (solved th) then (th::So, Si, St)
   139                      else (So, th::Si, St)) nested_tcs ([],[],[])
   140               val simplified' = map join_assums simplified
   141               val dummy = (Prim.trace_thms "solved =" solved;
   142                            Prim.trace_thms "simplified' =" simplified')
   143               val rewr = full_simplify (ss addsimps (solved @ simplified'));
   144               val dummy = Prim.trace_thms "Simplifying the induction rule..."
   145                                           [induction]
   146               val induction' = rewr induction
   147               val dummy = Prim.trace_thms "Simplifying the recursion rules..."
   148                                           [rules]
   149               val rules'     = rewr rules
   150               val _ = message "... Postprocessing finished";
   151           in
   152           {induction = induction',
   153                rules = rules',
   154                  tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
   155                            (simplified@stubborn)}
   156           end
   157   end;
   158 
   159 
   160 (*lcp: curry the predicate of the induction rule*)
   161 fun curry_rule rl =
   162   SplitRule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl)), rl);
   163 
   164 (*lcp: put a theorem into Isabelle form, using meta-level connectives*)
   165 val meta_outer =
   166   curry_rule o standard o
   167   rule_by_tactic (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));
   168 
   169 (*Strip off the outer !P*)
   170 val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
   171 
   172 fun tracing true _ = ()
   173   | tracing false msg = writeln msg;
   174 
   175 fun simplify_defn strict thy cs ss congs wfs id pats def0 =
   176    let val def = freezeT def0 RS meta_eq_to_obj_eq
   177        val {theory,rules,rows,TCs,full_pats_TCs} =
   178            Prim.post_definition congs (thy, (def,pats))
   179        val {lhs=f,rhs} = S.dest_eq (concl def)
   180        val (_,[R,_]) = S.strip_comb rhs
   181        val dummy = Prim.trace_thms "congs =" congs
   182        (*the next step has caused simplifier looping in some cases*)
   183        val {induction, rules, tcs} =
   184              proof_stage strict cs ss wfs theory
   185                {f = f, R = R, rules = rules,
   186                 full_pats_TCs = full_pats_TCs,
   187                 TCs = TCs}
   188        val rules' = map (standard o ObjectLogic.rulify_no_asm)
   189                         (R.CONJUNCTS rules)
   190          in  {induct = meta_outer (ObjectLogic.rulify_no_asm (induction RS spec')),
   191         rules = ListPair.zip(rules', rows),
   192         tcs = (termination_goals rules') @ tcs}
   193    end
   194   handle U.ERR {mesg,func,module} =>
   195                error (mesg ^
   196                       "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
   197 
   198 (*---------------------------------------------------------------------------
   199  * Defining a function with an associated termination relation.
   200  *---------------------------------------------------------------------------*)
   201 fun define_i strict thy cs ss congs wfs fid R eqs =
   202   let val {functional,pats} = Prim.mk_functional thy eqs
   203       val (thy, def) = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional
   204   in (thy, simplify_defn strict thy cs ss congs wfs fid pats def) end;
   205 
   206 fun define strict thy cs ss congs wfs fid R seqs =
   207   define_i strict thy cs ss congs wfs fid (read_term thy R) (map (read_term thy) seqs)
   208     handle U.ERR {mesg,...} => error mesg;
   209 
   210 
   211 (*---------------------------------------------------------------------------
   212  *
   213  *     Definitions with synthesized termination relation
   214  *
   215  *---------------------------------------------------------------------------*)
   216 
   217 fun func_of_cond_eqn tm =
   218   #1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));
   219 
   220 fun defer_i thy congs fid eqs =
   221  let val {rules,R,theory,full_pats_TCs,SV,...} =
   222              Prim.lazyR_def thy (Sign.base_name fid) congs eqs
   223      val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));
   224      val dummy = message "Proving induction theorem ...";
   225      val induction = Prim.mk_induction theory
   226                         {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
   227  in (theory,
   228      (*return the conjoined induction rule and recursion equations,
   229        with assumptions remaining to discharge*)
   230      standard (induction RS (rules RS conjI)))
   231  end
   232 
   233 fun defer thy congs fid seqs =
   234   defer_i thy congs fid (map (read_term thy) seqs)
   235     handle U.ERR {mesg,...} => error mesg;
   236 end;
   237 
   238 end;