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