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