TFL/post.ML
author paulson
Wed Jul 25 13:13:01 2001 +0200 (2001-07-25)
changeset 11451 8abfb4f7bd02
parent 11038 932d66879fe7
child 11632 6fc8de600f58
permissions -rw-r--r--
partial restructuring to reduce dependence on Axiom of Choice
wenzelm@10769
     1
(*  Title:      TFL/post.ML
wenzelm@10769
     2
    ID:         $Id$
wenzelm@10769
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
wenzelm@10769
     4
    Copyright   1997  University of Cambridge
wenzelm@10769
     5
wenzelm@10769
     6
Second part of main module (postprocessing of TFL definitions).
wenzelm@10769
     7
*)
wenzelm@10769
     8
wenzelm@10769
     9
signature TFL =
wenzelm@10769
    10
sig
wenzelm@10769
    11
  val trace: bool ref
wenzelm@10769
    12
  val quiet_mode: bool ref
wenzelm@10769
    13
  val message: string -> unit
wenzelm@10769
    14
  val tgoalw: theory -> thm list -> thm list -> thm list
wenzelm@10769
    15
  val tgoal: theory -> thm list -> thm list
wenzelm@10769
    16
  val std_postprocessor: claset -> simpset -> thm list -> theory ->
wenzelm@10769
    17
    {induction: thm, rules: thm, TCs: term list list} ->
wenzelm@10769
    18
    {induction: thm, rules: thm, nested_tcs: thm list}
wenzelm@10769
    19
  val define_i: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
wenzelm@10769
    20
    term -> term list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
wenzelm@10769
    21
  val define: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
wenzelm@10769
    22
    string -> string list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
wenzelm@10769
    23
  val defer_i: theory -> thm list -> xstring -> term list -> theory * thm
wenzelm@10769
    24
  val defer: theory -> thm list -> xstring -> string list -> theory * thm
wenzelm@10769
    25
end;
wenzelm@10769
    26
wenzelm@10769
    27
structure Tfl: TFL =
wenzelm@10769
    28
struct
wenzelm@10769
    29
wenzelm@10769
    30
structure S = USyntax
wenzelm@10769
    31
wenzelm@10769
    32
wenzelm@10769
    33
(* messages *)
wenzelm@10769
    34
wenzelm@10769
    35
val trace = Prim.trace
wenzelm@10769
    36
wenzelm@10769
    37
val quiet_mode = ref false;
wenzelm@10769
    38
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@10769
    39
wenzelm@10769
    40
wenzelm@10769
    41
(* misc *)
wenzelm@10769
    42
wenzelm@10769
    43
fun read_term thy = Sign.simple_read_term (Theory.sign_of thy) HOLogic.termT;
wenzelm@10769
    44
wenzelm@10769
    45
wenzelm@10769
    46
(*---------------------------------------------------------------------------
wenzelm@10769
    47
 * Extract termination goals so that they can be put it into a goalstack, or
wenzelm@10769
    48
 * have a tactic directly applied to them.
wenzelm@10769
    49
 *--------------------------------------------------------------------------*)
wenzelm@10769
    50
fun termination_goals rules =
wenzelm@10769
    51
    map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)
wenzelm@10769
    52
      (foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
wenzelm@10769
    53
wenzelm@10769
    54
(*---------------------------------------------------------------------------
wenzelm@10769
    55
 * Finds the termination conditions in (highly massaged) definition and
wenzelm@10769
    56
 * puts them into a goalstack.
wenzelm@10769
    57
 *--------------------------------------------------------------------------*)
wenzelm@10769
    58
fun tgoalw thy defs rules =
wenzelm@10769
    59
  case termination_goals rules of
wenzelm@10769
    60
      [] => error "tgoalw: no termination conditions to prove"
wenzelm@10769
    61
    | L  => goalw_cterm defs
wenzelm@10769
    62
              (Thm.cterm_of (Theory.sign_of thy)
wenzelm@10769
    63
                        (HOLogic.mk_Trueprop(USyntax.list_mk_conj L)));
wenzelm@10769
    64
wenzelm@10769
    65
fun tgoal thy = tgoalw thy [];
wenzelm@10769
    66
wenzelm@10769
    67
(*---------------------------------------------------------------------------
wenzelm@10769
    68
 * Three postprocessors are applied to the definition.  It
wenzelm@10769
    69
 * attempts to prove wellfoundedness of the given relation, simplifies the
wenzelm@10769
    70
 * non-proved termination conditions, and finally attempts to prove the
wenzelm@10769
    71
 * simplified termination conditions.
wenzelm@10769
    72
 *--------------------------------------------------------------------------*)
wenzelm@10769
    73
fun std_postprocessor cs ss wfs =
wenzelm@10769
    74
  Prim.postprocess
wenzelm@10769
    75
   {wf_tac     = REPEAT (ares_tac wfs 1),
wenzelm@10769
    76
    terminator = asm_simp_tac ss 1
wenzelm@10769
    77
                 THEN TRY (fast_tac (cs addSDs [not0_implies_Suc] addss ss) 1),
wenzelm@10769
    78
    simplifier = Rules.simpl_conv ss []};
wenzelm@10769
    79
wenzelm@10769
    80
wenzelm@10769
    81
wenzelm@10769
    82
val concl = #2 o Rules.dest_thm;
wenzelm@10769
    83
wenzelm@10769
    84
(*---------------------------------------------------------------------------
wenzelm@10769
    85
 * Postprocess a definition made by "define". This is a separate stage of
wenzelm@10769
    86
 * processing from the definition stage.
wenzelm@10769
    87
 *---------------------------------------------------------------------------*)
wenzelm@10769
    88
local
wenzelm@10769
    89
structure R = Rules
wenzelm@10769
    90
structure U = Utils
wenzelm@10769
    91
wenzelm@10769
    92
(* The rest of these local definitions are for the tricky nested case *)
wenzelm@10769
    93
val solved = not o can S.dest_eq o #2 o S.strip_forall o concl
wenzelm@10769
    94
wenzelm@10769
    95
fun id_thm th =
wenzelm@10769
    96
   let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
wenzelm@10769
    97
   in lhs aconv rhs end
wenzelm@10769
    98
   handle U.ERR _ => false;
wenzelm@10769
    99
   
wenzelm@10769
   100
wenzelm@10769
   101
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
wenzelm@10769
   102
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
wenzelm@10769
   103
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
wenzelm@10769
   104
fun mk_meta_eq r = case concl_of r of
wenzelm@10769
   105
     Const("==",_)$_$_ => r
wenzelm@10769
   106
  |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
wenzelm@10769
   107
  |   _ => r RS P_imp_P_eq_True
wenzelm@10769
   108
wenzelm@10769
   109
(*Is this the best way to invoke the simplifier??*)
wenzelm@10769
   110
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
wenzelm@10769
   111
wenzelm@10769
   112
fun join_assums th =
wenzelm@10769
   113
  let val {sign,...} = rep_thm th
wenzelm@10769
   114
      val tych = cterm_of sign
wenzelm@10769
   115
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
wenzelm@10769
   116
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
wenzelm@10769
   117
      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
wenzelm@10769
   118
      val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
wenzelm@10769
   119
  in
wenzelm@10769
   120
    R.GEN_ALL
wenzelm@10769
   121
      (R.DISCH_ALL
wenzelm@10769
   122
         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
wenzelm@10769
   123
  end
wenzelm@10769
   124
  val gen_all = S.gen_all
wenzelm@10769
   125
in
wenzelm@10769
   126
fun proof_stage cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
wenzelm@10769
   127
  let
wenzelm@10769
   128
    val _ = message "Proving induction theorem ..."
wenzelm@10769
   129
    val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
wenzelm@10769
   130
    val _ = message "Postprocessing ...";
wenzelm@10769
   131
    val {rules, induction, nested_tcs} =
wenzelm@10769
   132
      std_postprocessor cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
wenzelm@10769
   133
  in
wenzelm@10769
   134
  case nested_tcs
wenzelm@10769
   135
  of [] => {induction=induction, rules=rules,tcs=[]}
wenzelm@10769
   136
  | L  => let val dummy = message "Simplifying nested TCs ..."
wenzelm@10769
   137
              val (solved,simplified,stubborn) =
wenzelm@10769
   138
               U.itlist (fn th => fn (So,Si,St) =>
wenzelm@10769
   139
                     if (id_thm th) then (So, Si, th::St) else
wenzelm@10769
   140
                     if (solved th) then (th::So, Si, St)
wenzelm@10769
   141
                     else (So, th::Si, St)) nested_tcs ([],[],[])
wenzelm@10769
   142
              val simplified' = map join_assums simplified
wenzelm@10769
   143
              val rewr = full_simplify (ss addsimps (solved @ simplified'));
wenzelm@10769
   144
              val induction' = rewr induction
wenzelm@10769
   145
              and rules'     = rewr rules
wenzelm@10769
   146
          in
wenzelm@10769
   147
          {induction = induction',
wenzelm@10769
   148
               rules = rules',
wenzelm@10769
   149
                 tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
wenzelm@10769
   150
                           (simplified@stubborn)}
wenzelm@10769
   151
          end
wenzelm@10769
   152
  end;
wenzelm@10769
   153
wenzelm@10769
   154
wenzelm@10769
   155
(*lcp: curry the predicate of the induction rule*)
wenzelm@11038
   156
fun curry_rule rl =
wenzelm@11038
   157
  SplitRule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl)), rl);
wenzelm@10769
   158
wenzelm@10769
   159
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
wenzelm@10769
   160
val meta_outer =
wenzelm@11038
   161
  curry_rule o standard o
wenzelm@11038
   162
  rule_by_tactic (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));
wenzelm@10769
   163
wenzelm@10769
   164
(*Strip off the outer !P*)
wenzelm@10769
   165
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
wenzelm@10769
   166
wenzelm@10769
   167
fun simplify_defn thy cs ss congs wfs id pats def0 =
wenzelm@10769
   168
   let val def = freezeT def0 RS meta_eq_to_obj_eq
wenzelm@10769
   169
       val {theory,rules,rows,TCs,full_pats_TCs} = Prim.post_definition congs (thy, (def,pats))
wenzelm@10769
   170
       val {lhs=f,rhs} = S.dest_eq (concl def)
wenzelm@10769
   171
       val (_,[R,_]) = S.strip_comb rhs
wenzelm@10769
   172
       val {induction, rules, tcs} =
wenzelm@10769
   173
             proof_stage cs ss wfs theory
wenzelm@10769
   174
               {f = f, R = R, rules = rules,
wenzelm@10769
   175
                full_pats_TCs = full_pats_TCs,
wenzelm@10769
   176
                TCs = TCs}
wenzelm@10769
   177
       val rules' = map (standard o Rulify.rulify_no_asm) (R.CONJUNCTS rules)
wenzelm@10769
   178
   in  {induct = meta_outer (Rulify.rulify_no_asm (induction RS spec')),
wenzelm@10769
   179
        rules = ListPair.zip(rules', rows),
wenzelm@10769
   180
        tcs = (termination_goals rules') @ tcs}
wenzelm@10769
   181
   end
wenzelm@10769
   182
  handle U.ERR {mesg,func,module} =>
wenzelm@10769
   183
               error (mesg ^
wenzelm@10769
   184
                      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
wenzelm@10769
   185
wenzelm@10769
   186
(*---------------------------------------------------------------------------
wenzelm@10769
   187
 * Defining a function with an associated termination relation.
wenzelm@10769
   188
 *---------------------------------------------------------------------------*)
wenzelm@10769
   189
fun define_i thy cs ss congs wfs fid R eqs =
wenzelm@10769
   190
  let val {functional,pats} = Prim.mk_functional thy eqs
wenzelm@10769
   191
      val (thy, def) = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional
wenzelm@10769
   192
  in (thy, simplify_defn thy cs ss congs wfs fid pats def) end;
wenzelm@10769
   193
wenzelm@10769
   194
fun define thy cs ss congs wfs fid R seqs =
wenzelm@10769
   195
  define_i thy cs ss congs wfs fid (read_term thy R) (map (read_term thy) seqs)
wenzelm@10769
   196
    handle U.ERR {mesg,...} => error mesg;
wenzelm@10769
   197
wenzelm@10769
   198
wenzelm@10769
   199
(*---------------------------------------------------------------------------
wenzelm@10769
   200
 *
wenzelm@10769
   201
 *     Definitions with synthesized termination relation
wenzelm@10769
   202
 *
wenzelm@10769
   203
 *---------------------------------------------------------------------------*)
wenzelm@10769
   204
wenzelm@10769
   205
fun func_of_cond_eqn tm =
wenzelm@10769
   206
  #1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));
wenzelm@10769
   207
wenzelm@10769
   208
fun defer_i thy congs fid eqs =
wenzelm@10769
   209
 let val {rules,R,theory,full_pats_TCs,SV,...} =
wenzelm@10769
   210
             Prim.lazyR_def thy (Sign.base_name fid) congs eqs
wenzelm@10769
   211
     val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));
wenzelm@10769
   212
     val dummy = message "Proving induction theorem ...";
wenzelm@10769
   213
     val induction = Prim.mk_induction theory
wenzelm@10769
   214
                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
wenzelm@10769
   215
 in (theory,
wenzelm@10769
   216
     (*return the conjoined induction rule and recursion equations,
wenzelm@10769
   217
       with assumptions remaining to discharge*)
wenzelm@10769
   218
     standard (induction RS (rules RS conjI)))
wenzelm@10769
   219
 end
wenzelm@10769
   220
wenzelm@10769
   221
fun defer thy congs fid seqs =
wenzelm@10769
   222
  defer_i thy congs fid (map (read_term thy) seqs)
wenzelm@10769
   223
    handle U.ERR {mesg,...} => error mesg;
wenzelm@10769
   224
end;
wenzelm@10769
   225
wenzelm@10769
   226
end;