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