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