TFL/post.sml
author paulson
Wed Aug 18 18:44:20 1999 +0200 (1999-08-18)
changeset 7262 a05dc63ca29b
parent 6554 5be3f13193d7
child 8526 0be2c98f15a7
permissions -rw-r--r--
from Konrad: support for schematic definitions
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@7262
    11
paulson@7262
    12
   val trace : bool ref
paulson@7262
    13
paulson@2112
    14
   structure Prim : TFL_sig
wenzelm@6524
    15
   val quiet_mode : bool ref
wenzelm@6524
    16
   val message : string -> unit
paulson@2112
    17
paulson@3191
    18
   val tgoalw : theory -> thm list -> thm list -> thm list
paulson@3191
    19
   val tgoal: theory -> thm list -> thm list
paulson@2112
    20
paulson@3405
    21
   val std_postprocessor : simpset -> theory 
paulson@2112
    22
                           -> {induction:thm, rules:thm, TCs:term list list} 
paulson@2112
    23
                           -> {induction:thm, rules:thm, nested_tcs:thm list}
paulson@2112
    24
paulson@7262
    25
   val define_i : theory -> xstring -> term 
paulson@7262
    26
                  -> simpset * thm list (*allows special simplication*)
paulson@7262
    27
                  -> term list
paulson@7262
    28
                  -> theory * {rules:thm list, induct:thm, tcs:term list}
paulson@3331
    29
paulson@7262
    30
   val define   : theory -> xstring -> string 
paulson@7262
    31
                  -> simpset * thm list 
paulson@7262
    32
                  -> string list 
paulson@7262
    33
                  -> theory * {rules:thm list, induct:thm, tcs:term list}
paulson@2112
    34
paulson@7262
    35
   val defer_i : theory -> xstring
paulson@6498
    36
                  -> thm list (* congruence rules *)
paulson@6498
    37
                  -> term list -> theory * thm
paulson@6498
    38
paulson@7262
    39
   val defer : theory -> xstring
paulson@6498
    40
                  -> thm list 
paulson@6498
    41
                  -> string list -> theory * thm
paulson@6498
    42
paulson@3459
    43
   val simplify_defn : simpset * thm list 
paulson@3459
    44
                        -> theory * (string * Prim.pattern list)
paulson@3191
    45
                        -> {rules:thm list, induct:thm, tcs:term list}
paulson@2112
    46
paulson@3191
    47
  end;
paulson@3191
    48
paulson@3191
    49
paulson@3191
    50
structure Tfl: TFL =
paulson@2112
    51
struct
paulson@2112
    52
 structure Prim = Prim
paulson@3391
    53
 structure S = USyntax
paulson@2112
    54
wenzelm@6524
    55
 (* messages *)
wenzelm@6524
    56
wenzelm@6524
    57
 val quiet_mode = ref false;
wenzelm@6524
    58
 fun message s = if ! quiet_mode then () else writeln s;
wenzelm@6524
    59
paulson@6498
    60
 val trace = Prim.trace
paulson@6498
    61
paulson@6498
    62
 (*---------------------------------------------------------------------------
paulson@6498
    63
  * Extract termination goals so that they can be put it into a goalstack, or 
paulson@6498
    64
  * have a tactic directly applied to them.
paulson@6498
    65
  *--------------------------------------------------------------------------*)
paulson@6498
    66
 fun termination_goals rules = 
paulson@6498
    67
     map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)
paulson@6498
    68
       (foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
paulson@3191
    69
paulson@6498
    70
 (*---------------------------------------------------------------------------
paulson@6498
    71
  * Finds the termination conditions in (highly massaged) definition and 
paulson@6498
    72
  * puts them into a goalstack.
paulson@6498
    73
  *--------------------------------------------------------------------------*)
paulson@6498
    74
 fun tgoalw thy defs rules = 
paulson@6498
    75
   case termination_goals rules of
paulson@6498
    76
       [] => error "tgoalw: no termination conditions to prove"
paulson@6498
    77
     | L  => goalw_cterm defs 
paulson@6498
    78
	       (Thm.cterm_of (Theory.sign_of thy) 
paulson@6498
    79
			 (HOLogic.mk_Trueprop(USyntax.list_mk_conj L)));
paulson@6498
    80
paulson@6498
    81
 fun tgoal thy = tgoalw thy [];
paulson@6498
    82
paulson@6498
    83
 (*---------------------------------------------------------------------------
paulson@6498
    84
 * Three postprocessors are applied to the definition.  It
paulson@6498
    85
 * attempts to prove wellfoundedness of the given relation, simplifies the
paulson@6498
    86
 * non-proved termination conditions, and finally attempts to prove the 
paulson@6498
    87
 * simplified termination conditions.
paulson@3405
    88
 *--------------------------------------------------------------------------*)
paulson@6498
    89
 fun std_postprocessor ss =
paulson@6498
    90
   Prim.postprocess
paulson@6498
    91
    {WFtac      = REPEAT (ares_tac [wf_empty, wf_pred_nat, 
paulson@6498
    92
				    wf_measure, wf_inv_image, 
paulson@6498
    93
				    wf_lex_prod, wf_less_than, wf_trancl] 1),
paulson@6498
    94
     terminator = asm_simp_tac ss 1
paulson@6498
    95
		  THEN TRY(CLASET' (fn cs => best_tac
paulson@6498
    96
			   (cs addSDs [not0_implies_Suc] addss ss)) 1),
paulson@6498
    97
     simplifier = Rules.simpl_conv ss []};
paulson@2112
    98
paulson@2112
    99
paulson@2112
   100
paulson@6498
   101
 val concl = #2 o Rules.dest_thm;
paulson@2112
   102
paulson@3191
   103
(*---------------------------------------------------------------------------
paulson@3191
   104
 * Postprocess a definition made by "define". This is a separate stage of 
paulson@3191
   105
 * processing from the definition stage.
paulson@2112
   106
 *---------------------------------------------------------------------------*)
paulson@6498
   107
 local 
paulson@6498
   108
 structure R = Rules
paulson@6498
   109
 structure U = Utils
paulson@2112
   110
paulson@6498
   111
 (* The rest of these local definitions are for the tricky nested case *)
paulson@6498
   112
 val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl
paulson@2112
   113
paulson@6498
   114
 fun id_thm th = 
paulson@6498
   115
    let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th))))
paulson@6498
   116
    in lhs aconv rhs
paulson@6498
   117
    end handle _ => false
paulson@2112
   118
paulson@6498
   119
 fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
paulson@6498
   120
 val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
paulson@6498
   121
 val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
paulson@6498
   122
 fun mk_meta_eq r = case concl_of r of
paulson@6498
   123
      Const("==",_)$_$_ => r
paulson@6498
   124
   |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
paulson@6498
   125
   |   _ => r RS P_imp_P_eq_True
paulson@3405
   126
paulson@6498
   127
 (*Is this the best way to invoke the simplifier??*)
paulson@6498
   128
 fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
paulson@2112
   129
paulson@6498
   130
 fun join_assums th = 
paulson@6498
   131
   let val {sign,...} = rep_thm th
paulson@6498
   132
       val tych = cterm_of sign
paulson@6498
   133
       val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
paulson@6498
   134
       val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
paulson@6498
   135
       val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
paulson@6498
   136
       val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
paulson@6498
   137
   in 
paulson@6498
   138
     R.GEN_ALL 
paulson@6498
   139
       (R.DISCH_ALL 
paulson@6498
   140
	  (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
paulson@6498
   141
   end
paulson@6498
   142
   val gen_all = S.gen_all
paulson@6498
   143
 in
paulson@6498
   144
 fun proof_stage ss theory {f, R, rules, full_pats_TCs, TCs} =
wenzelm@6524
   145
   let val dummy = message "Proving induction theorem ..."
paulson@6498
   146
       val ind = Prim.mk_induction theory 
paulson@6498
   147
                    {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
paulson@7262
   148
       val dummy = (message "Proved induction theorem."; 
paulson@7262
   149
		    message "Postprocessing ...");
paulson@6498
   150
       val {rules, induction, nested_tcs} = 
paulson@6498
   151
	   std_postprocessor ss theory {rules=rules, induction=ind, TCs=TCs}
paulson@6498
   152
   in
paulson@6498
   153
   case nested_tcs
wenzelm@6524
   154
   of [] => (message "Postprocessing done.";
paulson@6498
   155
	     {induction=induction, rules=rules,tcs=[]})
wenzelm@6524
   156
   | L  => let val dummy = message "Simplifying nested TCs ..."
paulson@6498
   157
	       val (solved,simplified,stubborn) =
paulson@6498
   158
		U.itlist (fn th => fn (So,Si,St) =>
paulson@6498
   159
		      if (id_thm th) then (So, Si, th::St) else
paulson@6498
   160
		      if (solved th) then (th::So, Si, St) 
paulson@6498
   161
		      else (So, th::Si, St)) nested_tcs ([],[],[])
paulson@6498
   162
	       val simplified' = map join_assums simplified
paulson@6498
   163
	       val rewr = full_simplify (ss addsimps (solved @ simplified'));
paulson@6498
   164
	       val induction' = rewr induction
paulson@6498
   165
	       and rules'     = rewr rules
wenzelm@6524
   166
	       val dummy = message "Postprocessing done."
paulson@6498
   167
	   in
paulson@6498
   168
	   {induction = induction',
paulson@6498
   169
		rules = rules',
paulson@6498
   170
		  tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
paulson@6498
   171
			    (simplified@stubborn)}
paulson@6498
   172
	   end
paulson@6498
   173
   end;
paulson@3191
   174
paulson@3191
   175
paulson@6498
   176
 (*lcp: curry the predicate of the induction rule*)
paulson@6498
   177
 fun curry_rule rl = split_rule_var
paulson@6498
   178
			 (head_of (HOLogic.dest_Trueprop (concl_of rl)), 
paulson@6498
   179
			  rl);
paulson@3191
   180
paulson@6498
   181
 (*lcp: put a theorem into Isabelle form, using meta-level connectives*)
paulson@6498
   182
 val meta_outer = 
paulson@6498
   183
     curry_rule o standard o 
paulson@6498
   184
     rule_by_tactic (REPEAT 
paulson@6498
   185
		     (FIRSTGOAL (resolve_tac [allI, impI, conjI]
paulson@6498
   186
				 ORELSE' etac conjE)));
paulson@2112
   187
paulson@6498
   188
 (*Strip off the outer !P*)
paulson@6498
   189
 val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
paulson@3459
   190
paulson@7262
   191
 (*this function could be combined with define_i --lcp*)
paulson@6498
   192
 fun simplify_defn (ss, tflCongs) (thy,(id,pats)) =
paulson@6498
   193
    let val dummy = deny (id mem (Sign.stamp_names_of (Theory.sign_of thy)))
paulson@6498
   194
			 ("Recursive definition " ^ id ^ 
paulson@6498
   195
			  " would clash with the theory of the same name!")
paulson@6498
   196
	val def = freezeT(get_def thy id)   RS   meta_eq_to_obj_eq
paulson@6498
   197
	val {theory,rules,TCs,full_pats_TCs,patterns} = 
paulson@6498
   198
		 Prim.post_definition (Prim.congs tflCongs)
paulson@6498
   199
		    (thy, (def,pats))
paulson@6498
   200
	val {lhs=f,rhs} = S.dest_eq(concl def)
paulson@6498
   201
	val (_,[R,_]) = S.strip_comb rhs
paulson@6498
   202
	val ss' = ss addsimps Prim.default_simps
paulson@6498
   203
	val {induction, rules, tcs} = 
paulson@6498
   204
	      proof_stage ss' theory 
paulson@6498
   205
		{f = f, R = R, rules = rules,
paulson@6498
   206
		 full_pats_TCs = full_pats_TCs,
paulson@6498
   207
		 TCs = TCs}
paulson@6498
   208
	val rules' = map (standard o normalize_thm [RSmp]) (R.CONJUNCTS rules)
paulson@6498
   209
    in  {induct = meta_outer
paulson@6498
   210
		   (normalize_thm [RSspec,RSmp] (induction RS spec')), 
paulson@6498
   211
	 rules = rules', 
paulson@6498
   212
	 tcs = (termination_goals rules') @ tcs}
paulson@6498
   213
    end
paulson@6498
   214
   handle Utils.ERR {mesg,func,module} => 
paulson@6498
   215
		error (mesg ^ 
paulson@6498
   216
		       "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
paulson@2112
   217
paulson@3191
   218
(*---------------------------------------------------------------------------
paulson@7262
   219
 * Defining a function with an associated termination relation. 
paulson@7262
   220
 *---------------------------------------------------------------------------*)
paulson@7262
   221
 fun define_i thy fid R ss_congs eqs = 
paulson@7262
   222
   let val {functional,pats} = Prim.mk_functional thy eqs
paulson@7262
   223
       val thy = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional
paulson@7262
   224
   in (thy, simplify_defn ss_congs (thy, (fid, pats)))
paulson@7262
   225
   end;
paulson@7262
   226
paulson@7262
   227
 fun define thy fid R ss_congs seqs = 
paulson@7262
   228
   let val _ =  writeln ("Recursive function " ^ fid)
paulson@7262
   229
       val read = readtm (Theory.sign_of thy) (TVar(("DUMMY",0),[])) 
paulson@7262
   230
   in  define_i thy fid (read R) ss_congs (map read seqs)  end
paulson@7262
   231
   handle Utils.ERR {mesg,...} => error mesg;
paulson@7262
   232
paulson@7262
   233
paulson@7262
   234
(*---------------------------------------------------------------------------
paulson@3191
   235
 *
paulson@6498
   236
 *     Definitions with synthesized termination relation
paulson@3191
   237
 *
paulson@3191
   238
 *---------------------------------------------------------------------------*)
paulson@6498
   239
paulson@6498
   240
 local open USyntax
paulson@6498
   241
 in 
paulson@6498
   242
 fun func_of_cond_eqn tm =
paulson@6498
   243
   #1(strip_comb(#lhs(dest_eq(#2 (strip_forall(#2(strip_imp tm)))))))
paulson@6498
   244
 end;
paulson@6498
   245
wenzelm@6554
   246
 fun defer_i thy fid congs eqs = 
wenzelm@6554
   247
  let val {rules,R,theory,full_pats_TCs,SV,...} = 
wenzelm@6554
   248
	      Prim.lazyR_def thy (Sign.base_name fid) congs eqs
paulson@6498
   249
      val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules))
paulson@7262
   250
      val dummy = (message "Definition made."; 
paulson@7262
   251
		   message "Proving induction theorem ...");
paulson@6498
   252
      val induction = Prim.mk_induction theory 
paulson@6498
   253
                         {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
wenzelm@6524
   254
      val dummy = message "Induction theorem proved."
paulson@6498
   255
  in (theory, 
paulson@6498
   256
      (*return the conjoined induction rule and recursion equations, 
paulson@6498
   257
	with assumptions remaining to discharge*)
paulson@6498
   258
      standard (induction RS (rules RS conjI)))
paulson@6498
   259
  end
paulson@6498
   260
wenzelm@6554
   261
 fun defer thy fid congs seqs = 
paulson@7262
   262
   let val read = readtm (Theory.sign_of thy) (TVar(("DUMMY",0),[]))
paulson@7262
   263
   in  defer_i thy fid congs (map read seqs)  end
paulson@6498
   264
   handle Utils.ERR {mesg,...} => error mesg;
paulson@6498
   265
 end;
paulson@6498
   266
paulson@2112
   267
end;