TFL/post.sml
author paulson
Thu Jun 05 13:27:28 1997 +0200 (1997-06-05)
changeset 3405 2cccd0e3e9ea
parent 3391 5e45dd3b64e9
child 3459 112cbb8301dc
permissions -rw-r--r--
Numerous simplifications and removal of HOL-isms
Addition of the "simpset" feature (replacing references to \!simpset)
paulson@3302
     1
(*  Title:      TFL/post
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
paulson@3302
     6
Postprocessing of TFL definitions
paulson@3302
     7
*)
paulson@3302
     8
paulson@3191
     9
signature TFL = 
paulson@3191
    10
  sig
paulson@2112
    11
   structure Prim : TFL_sig
paulson@2112
    12
paulson@3191
    13
   val tgoalw : theory -> thm list -> thm list -> thm list
paulson@3191
    14
   val tgoal: theory -> thm list -> thm list
paulson@2112
    15
paulson@3405
    16
   val std_postprocessor : simpset -> theory 
paulson@2112
    17
                           -> {induction:thm, rules:thm, TCs:term list list} 
paulson@2112
    18
                           -> {induction:thm, rules:thm, nested_tcs:thm list}
paulson@2112
    19
paulson@3391
    20
   val define_i : theory -> string -> term -> term list
paulson@3405
    21
                  -> theory * Prim.pattern list
paulson@3331
    22
paulson@3331
    23
   val define   : theory -> string -> string -> string list 
paulson@3331
    24
                  -> theory * Prim.pattern list
paulson@2112
    25
paulson@3405
    26
   val simplify_defn : simpset -> theory * (string * Prim.pattern list)
paulson@3191
    27
                        -> {rules:thm list, induct:thm, tcs:term list}
paulson@2112
    28
paulson@3191
    29
  (*-------------------------------------------------------------------------
paulson@3191
    30
       val function : theory -> term -> {theory:theory, eq_ind : thm}
paulson@3191
    31
       val lazyR_def: theory -> term -> {theory:theory, eqns : thm}
paulson@3191
    32
   *-------------------------------------------------------------------------*)
paulson@2112
    33
paulson@2112
    34
   val tflcongs : theory -> thm list
paulson@2112
    35
paulson@3191
    36
  end;
paulson@3191
    37
paulson@3191
    38
paulson@3191
    39
structure Tfl: TFL =
paulson@2112
    40
struct
paulson@2112
    41
 structure Prim = Prim
paulson@3391
    42
 structure S = USyntax
paulson@2112
    43
paulson@3191
    44
(*---------------------------------------------------------------------------
paulson@3191
    45
 * Extract termination goals so that they can be put it into a goalstack, or 
paulson@3191
    46
 * have a tactic directly applied to them.
paulson@3191
    47
 *--------------------------------------------------------------------------*)
paulson@3191
    48
fun termination_goals rules = 
paulson@3405
    49
    map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)
paulson@3191
    50
      (foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
paulson@3191
    51
paulson@3405
    52
(*---------------------------------------------------------------------------
paulson@3405
    53
 * Finds the termination conditions in (highly massaged) definition and 
paulson@3405
    54
 * puts them into a goalstack.
paulson@3405
    55
 *--------------------------------------------------------------------------*)
paulson@3405
    56
fun tgoalw thy defs rules = 
paulson@3405
    57
   let val L = termination_goals rules
paulson@3405
    58
       open USyntax
paulson@3405
    59
       val g = cterm_of (sign_of thy) (HOLogic.mk_Trueprop(list_mk_conj L))
paulson@3405
    60
   in goalw_cterm defs g
paulson@3405
    61
   end;
paulson@2112
    62
paulson@3405
    63
fun tgoal thy = tgoalw thy [];
paulson@2112
    64
paulson@3405
    65
(*---------------------------------------------------------------------------
paulson@3405
    66
* Three postprocessors are applied to the definition.  It
paulson@3405
    67
* attempts to prove wellfoundedness of the given relation, simplifies the
paulson@3405
    68
* non-proved termination conditions, and finally attempts to prove the 
paulson@3405
    69
* simplified termination conditions.
paulson@3405
    70
*--------------------------------------------------------------------------*)
paulson@3405
    71
fun std_postprocessor ss =
paulson@3405
    72
  Prim.postprocess
paulson@3405
    73
   {WFtac      = REPEAT (ares_tac [wf_measure, wf_inv_image, wf_lex_prod, 
paulson@3405
    74
				   wf_less_than, wf_trancl] 1),
paulson@3405
    75
    terminator = asm_simp_tac ss 1
paulson@3405
    76
		 THEN TRY(best_tac
paulson@3405
    77
			  (!claset addSDs [not0_implies_Suc] addss ss) 1),
paulson@3405
    78
    simplifier = Rules.simpl_conv ss []};
paulson@2112
    79
paulson@3405
    80
fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy));
paulson@2112
    81
paulson@2112
    82
paulson@3391
    83
val concl = #2 o Rules.dest_thm;
paulson@2112
    84
paulson@2112
    85
(*---------------------------------------------------------------------------
paulson@3191
    86
 * Defining a function with an associated termination relation. 
paulson@3191
    87
 *---------------------------------------------------------------------------*)
paulson@3331
    88
fun define_i thy fid R eqs = 
paulson@3331
    89
  let val dummy = require_thy thy "WF_Rel" "recursive function definitions"
paulson@3191
    90
      val {functional,pats} = Prim.mk_functional thy eqs
paulson@3405
    91
  in (Prim.wfrec_definition0 thy fid R functional, pats)
paulson@3191
    92
  end;
paulson@3191
    93
paulson@3191
    94
(*lcp's version: takes strings; doesn't return "thm" 
paulson@3245
    95
        (whose signature is a draft and therefore useless) *)
paulson@3331
    96
fun define thy fid R eqs = 
paulson@3191
    97
  let fun read thy = readtm (sign_of thy) (TVar(("DUMMY",0),[])) 
paulson@3405
    98
  in  define_i thy fid (read thy R) (map (read thy) eqs)  end
paulson@3191
    99
  handle Utils.ERR {mesg,...} => error mesg;
paulson@3191
   100
paulson@3191
   101
(*---------------------------------------------------------------------------
paulson@3191
   102
 * Postprocess a definition made by "define". This is a separate stage of 
paulson@3191
   103
 * processing from the definition stage.
paulson@2112
   104
 *---------------------------------------------------------------------------*)
paulson@2112
   105
local 
paulson@3391
   106
structure R = Rules
paulson@2112
   107
structure U = Utils
paulson@2112
   108
paulson@3191
   109
(* The rest of these local definitions are for the tricky nested case *)
paulson@2112
   110
val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl
paulson@2112
   111
paulson@2112
   112
fun id_thm th = 
paulson@2112
   113
   let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th))))
paulson@3391
   114
   in lhs aconv rhs
paulson@2112
   115
   end handle _ => false
paulson@2112
   116
paulson@2112
   117
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
paulson@2112
   118
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
paulson@2112
   119
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
paulson@2112
   120
fun mk_meta_eq r = case concl_of r of
paulson@2112
   121
     Const("==",_)$_$_ => r
paulson@2112
   122
  |   _$(Const("op =",_)$_$_) => r RS eq_reflection
paulson@2112
   123
  |   _ => r RS P_imp_P_eq_True
paulson@3405
   124
paulson@3405
   125
(*Is this the best way to invoke the simplifier??*)
paulson@3245
   126
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
paulson@2112
   127
paulson@2112
   128
fun join_assums th = 
paulson@2112
   129
  let val {sign,...} = rep_thm th
paulson@2112
   130
      val tych = cterm_of sign
paulson@2112
   131
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
paulson@2112
   132
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
paulson@2112
   133
      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
paulson@3245
   134
      val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
paulson@2112
   135
  in 
paulson@3191
   136
    R.GEN_ALL 
paulson@3191
   137
      (R.DISCH_ALL 
paulson@3191
   138
         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
paulson@2112
   139
  end
paulson@2112
   140
  val gen_all = S.gen_all
paulson@2112
   141
in
paulson@3405
   142
fun proof_stage ss theory {f, R, rules, full_pats_TCs, TCs} =
paulson@3271
   143
  let val dummy = prs "Proving induction theorem..  "
paulson@3191
   144
      val ind = Prim.mk_induction theory f R full_pats_TCs
paulson@3405
   145
      val dummy = prs "Proved induction theorem.\nPostprocessing..  "
paulson@3405
   146
      val {rules, induction, nested_tcs} = 
paulson@3405
   147
	  std_postprocessor ss theory {rules=rules, induction=ind, TCs=TCs}
paulson@3191
   148
  in
paulson@3191
   149
  case nested_tcs
paulson@3271
   150
  of [] => (writeln "Postprocessing done.";
paulson@3191
   151
            {induction=induction, rules=rules,tcs=[]})
paulson@3271
   152
  | L  => let val dummy = prs "Simplifying nested TCs..  "
paulson@2112
   153
              val (solved,simplified,stubborn) =
paulson@2112
   154
               U.itlist (fn th => fn (So,Si,St) =>
paulson@2112
   155
                     if (id_thm th) then (So, Si, th::St) else
paulson@2112
   156
                     if (solved th) then (th::So, Si, St) 
paulson@2112
   157
                     else (So, th::Si, St)) nested_tcs ([],[],[])
paulson@2112
   158
              val simplified' = map join_assums simplified
paulson@3405
   159
              val rewr = rewrite (solved @ simplified' @ #simps(rep_ss ss))
paulson@3405
   160
              val induction' = rewr induction
paulson@3405
   161
              and rules'     = rewr rules
paulson@3271
   162
              val dummy = writeln "Postprocessing done."
paulson@2112
   163
          in
paulson@2112
   164
          {induction = induction',
paulson@2112
   165
               rules = rules',
paulson@3191
   166
                 tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
paulson@3191
   167
                           (simplified@stubborn)}
paulson@2112
   168
          end
paulson@3391
   169
  end;
paulson@3191
   170
paulson@3191
   171
paulson@3302
   172
(*lcp: curry the predicate of the induction rule*)
paulson@3302
   173
fun curry_rule rl = Prod_Syntax.split_rule_var
paulson@3271
   174
                        (head_of (HOLogic.dest_Trueprop (concl_of rl)), 
paulson@3271
   175
			 rl);
paulson@3271
   176
paulson@3191
   177
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
paulson@3191
   178
val meta_outer = 
paulson@3302
   179
    curry_rule o standard o 
paulson@3271
   180
    rule_by_tactic (REPEAT_FIRST (resolve_tac [allI, impI, conjI]
paulson@3271
   181
				  ORELSE' etac conjE));
paulson@3191
   182
paulson@3191
   183
(*Strip off the outer !P*)
paulson@3191
   184
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
paulson@2112
   185
paulson@3405
   186
val wf_rel_defs = [lex_prod_def, measure_def, inv_image_def];
paulson@2112
   187
paulson@3405
   188
fun simplify_defn ss (thy,(id,pats)) =
paulson@3208
   189
   let val dummy = deny (id  mem  map ! (stamps_of_thy thy))
paulson@3208
   190
                        ("Recursive definition " ^ id ^ 
paulson@3245
   191
                         " would clash with the theory of the same name!")
paulson@3405
   192
       val def =  freezeT(get_def thy id)   RS   meta_eq_to_obj_eq
paulson@3405
   193
       val ss' = ss addsimps ((less_Suc_eq RS iffD2) :: wf_rel_defs)
paulson@3191
   194
       val {theory,rules,TCs,full_pats_TCs,patterns} = 
paulson@3405
   195
                Prim.post_definition ss' (thy,(def,pats))
paulson@3191
   196
       val {lhs=f,rhs} = S.dest_eq(concl def)
paulson@3191
   197
       val (_,[R,_]) = S.strip_comb rhs
paulson@3191
   198
       val {induction, rules, tcs} = 
paulson@3405
   199
             proof_stage ss' theory 
paulson@3245
   200
               {f = f, R = R, rules = rules,
paulson@3245
   201
                full_pats_TCs = full_pats_TCs,
paulson@3245
   202
                TCs = TCs}
paulson@3191
   203
       val rules' = map (standard o normalize_thm [RSmp]) (R.CONJUNCTS rules)
paulson@3191
   204
   in  {induct = meta_outer
paulson@3245
   205
                  (normalize_thm [RSspec,RSmp] (induction RS spec')), 
paulson@3245
   206
        rules = rules', 
paulson@3245
   207
        tcs = (termination_goals rules') @ tcs}
paulson@3191
   208
   end
paulson@3391
   209
  handle Utils.ERR {mesg,func,module} => 
paulson@3391
   210
               error (mesg ^ 
paulson@3405
   211
		      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
paulson@2112
   212
end;
paulson@2112
   213
paulson@3191
   214
(*---------------------------------------------------------------------------
paulson@3191
   215
 *
paulson@3191
   216
 *     Definitions with synthesized termination relation temporarily
paulson@3191
   217
 *     deleted -- it's not clear how to integrate this facility with
paulson@3191
   218
 *     the Isabelle theory file scheme, which restricts
paulson@3191
   219
 *     inference at theory-construction time.
paulson@3191
   220
 *
paulson@2112
   221
paulson@3391
   222
local structure R = Rules
paulson@2112
   223
in
paulson@2112
   224
fun function theory eqs = 
paulson@3208
   225
 let val dummy = prs "Making definition..   "
paulson@2112
   226
     val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs
paulson@2112
   227
     val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules))
paulson@3208
   228
     val dummy = prs "Definition made.\n"
paulson@3208
   229
     val dummy = prs "Proving induction theorem..  "
paulson@2112
   230
     val induction = Prim.mk_induction theory f R full_pats_TCs
paulson@3208
   231
     val dummy = prs "Induction theorem proved.\n"
paulson@2112
   232
 in {theory = theory, 
paulson@2112
   233
     eq_ind = standard (induction RS (rules RS conjI))}
paulson@2112
   234
 end
paulson@2112
   235
end;
paulson@2112
   236
paulson@2112
   237
paulson@2112
   238
fun lazyR_def theory eqs = 
paulson@2112
   239
   let val {rules,theory, ...} = Prim.lazyR_def theory eqs
paulson@2112
   240
   in {eqns=rules, theory=theory}
paulson@2112
   241
   end
paulson@3391
   242
   handle    e              => print_exn e;
paulson@3191
   243
 *
paulson@3191
   244
 *
paulson@3191
   245
 *---------------------------------------------------------------------------*)
paulson@3191
   246
paulson@3191
   247
paulson@2112
   248
paulson@2112
   249
paulson@3191
   250
(*---------------------------------------------------------------------------
paulson@3191
   251
 * Install the basic context notions. Others (for nat and list and prod) 
paulson@3191
   252
 * have already been added in thry.sml
paulson@3191
   253
 *---------------------------------------------------------------------------*)
paulson@3191
   254
val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG];
paulson@2112
   255
paulson@2112
   256
end;