TFL/post.sml
author paulson
Tue, 20 May 1997 11:49:57 +0200
changeset 3245 241838c01caf
parent 3208 8336393de482
child 3271 b873969b05d3
permissions -rw-r--r--
Removal of redundant code (unused or already present in Isabelle. This eliminates HOL compatibility but makes the code smaller and more readable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     1
(*-------------------------------------------------------------------------
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     2
there are 3 postprocessors that get applied to the definition:
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     3
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     4
    - a wellfoundedness prover (WF_TAC)
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     5
    - 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
     6
    - a termination prover
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     7
*-------------------------------------------------------------------------*)
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
     8
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
     9
signature TFL = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    10
  sig
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
   structure Prim : TFL_sig
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    13
   val tgoalw : theory -> thm list -> thm list -> thm list
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    14
   val tgoal: theory -> thm list -> thm list
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
   val WF_TAC : thm list -> tactic
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
   val simplifier : thm -> thm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
   val std_postprocessor : theory 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
                           -> {induction:thm, rules:thm, TCs:term list list} 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
                           -> {induction:thm, rules:thm, nested_tcs:thm list}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    23
   val define_i : theory -> term -> term -> theory * (thm * Prim.pattern list)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    24
   val define   : theory -> string -> string list -> theory * Prim.pattern list
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    26
   val simplify_defn : theory * (string * Prim.pattern list)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    27
                        -> {rules:thm list, induct:thm, tcs:term list}
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    29
  (*-------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    30
       val function : theory -> term -> {theory:theory, eq_ind : thm}
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    31
       val lazyR_def: theory -> term -> {theory:theory, eqns : thm}
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    32
   *-------------------------------------------------------------------------*)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
   val tflcongs : theory -> thm list
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
  end;
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    37
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    38
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    39
structure Tfl: TFL =
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
 structure Prim = Prim
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    42
 structure S = Prim.USyntax
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    44
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    45
 * 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
    46
 * have a tactic directly applied to them.
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
fun termination_goals rules = 
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    49
    map (Logic.freeze_vars o HOLogic.dest_Trueprop)
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    50
      (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
    51
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    52
 (*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    53
  * Finds the termination conditions in (highly massaged) definition and 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    54
  * puts them into a goalstack.
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    55
  *--------------------------------------------------------------------------*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    56
 fun tgoalw thy defs rules = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    57
    let val L = termination_goals rules
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
        open USyntax
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    59
        val g = cterm_of (sign_of thy) (HOLogic.mk_Trueprop(list_mk_conj L))
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
    in goalw_cterm defs g
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
    end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    63
 fun tgoal thy = tgoalw thy [];
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
3191
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
  * Simple wellfoundedness prover.
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    67
  *--------------------------------------------------------------------------*)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
 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
    69
 val WFtac = WF_TAC[wf_measure, wf_inv_image, wf_lex_prod, wf_less_than, 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    70
                    wf_pred_list, wf_trancl];
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    72
 val terminator = simp_tac(!simpset addsimps [less_Suc_eq, pred_list_def]) 1
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
    73
                  THEN TRY(best_tac
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    74
                           (!claset addSDs [not0_implies_Suc]
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
    75
                                    addss (!simpset)) 1);
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    76
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
 val simpls = [less_eq RS eq_reflection,
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
    78
               lex_prod_def, rprod_def, measure_def, inv_image_def];
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    80
 (*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    81
  * 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
    82
  * attempts to prove wellfoundedness of the given relation; simplifies the
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    83
  * non-proven termination conditions; and finally attempts to prove the 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    84
  * simplified termination conditions.
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    85
  *--------------------------------------------------------------------------*)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
 val std_postprocessor = Prim.postprocess{WFtac = WFtac,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
                                    terminator = terminator, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
                                    simplifier = Prim.Rules.simpl_conv simpls};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
    90
 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
    91
                                [pred_list_def]);
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    92
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
 fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
val concl = #2 o Prim.Rules.dest_thm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
(*---------------------------------------------------------------------------
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
    99
 * Defining a function with an associated termination relation. 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   100
 *---------------------------------------------------------------------------*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   101
fun define_i thy R eqs = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   102
  let val dummy = require_thy thy "WF_Rel" "recursive function definitions";
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   103
      
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   104
      val {functional,pats} = Prim.mk_functional thy eqs
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   105
      val (thm,thry) = Prim.wfrec_definition0 thy  R functional
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   106
  in (thry,(thm,pats))
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   107
  end;
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   108
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   109
(*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
   110
        (whose signature is a draft and therefore useless) *)
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   111
fun define thy R eqs = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   112
  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
   113
      val (thy',(_,pats)) =
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   114
             define_i thy (read thy R) 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   115
                      (fold_bal (app Ind_Syntax.conj) (map (read thy) eqs))
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   116
  in  (thy',pats)  end
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   117
  handle Utils.ERR {mesg,...} => error mesg;
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   118
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   119
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   120
 * 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
   121
 * processing from the definition stage.
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
local 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
structure R = Prim.Rules
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
structure U = Utils
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   127
(* The rest of these local definitions are for the tricky nested case *)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
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
   129
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
fun id_thm th = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
   let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th))))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
   in S.aconv lhs rhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
   end handle _ => false
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
fun mk_meta_eq r = case concl_of r of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
     Const("==",_)$_$_ => r
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
  |   _$(Const("op =",_)$_$_) => r RS eq_reflection
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
  |   _ => r RS P_imp_P_eq_True
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   142
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   143
fun reducer thl = rewrite (map standard thl @ #simps(rep_ss HOL_ss))
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   145
fun join_assums th = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
  let val {sign,...} = rep_thm th
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
      val tych = cterm_of sign
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   150
      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
   151
      val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
  in 
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   153
    R.GEN_ALL 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   154
      (R.DISCH_ALL 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   155
         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   156
  end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   157
  val gen_all = S.gen_all
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
in
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   159
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   160
 * The "reducer" argument is 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   161
 *  (fn thl => rewrite (map standard thl @ #simps(rep_ss HOL_ss))); 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   162
 *---------------------------------------------------------------------------*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   163
fun proof_stage theory reducer {f, R, rules, full_pats_TCs, TCs} =
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   164
  let val dummy = output(std_out, "Proving induction theorem..  ")
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   165
      val ind = Prim.mk_induction theory f R full_pats_TCs
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   166
      val dummy = output(std_out, "Proved induction theorem.\n")
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   167
      val pp = std_postprocessor theory
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   168
      val dummy = output(std_out, "Postprocessing..  ")
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   169
      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
   170
  in
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   171
  case nested_tcs
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   172
  of [] => (output(std_out, "Postprocessing done.\n");
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   173
            {induction=induction, rules=rules,tcs=[]})
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   174
  | L  => let val dummy = output(std_out, "Simplifying nested TCs..  ")
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
              val (solved,simplified,stubborn) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
               U.itlist (fn th => fn (So,Si,St) =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   177
                     if (id_thm th) then (So, Si, th::St) else
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
                     if (solved th) then (th::So, Si, St) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
                     else (So, th::Si, St)) nested_tcs ([],[],[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
              val simplified' = map join_assums simplified
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
              val induction' = reducer (solved@simplified') induction
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
              val rules' = reducer (solved@simplified') rules
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   183
              val dummy = output(std_out, "Postprocessing done.\n")
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
          in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
          {induction = induction',
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
               rules = rules',
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   187
                 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
   188
                           (simplified@stubborn)}
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
          end
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   190
  end handle (e as Utils.ERR _) => Utils.Raise e
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   191
          |   e                 => print_exn e;
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   192
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   193
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   194
(*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
   195
val meta_outer = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   196
    standard o rule_by_tactic (REPEAT_FIRST (resolve_tac [allI, impI, conjI]));
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   197
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   198
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   199
(*Strip off the outer !P*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   200
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   203
fun simplify_defn (thy,(id,pats)) =
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   204
   let val dummy = deny (id  mem  map ! (stamps_of_thy thy))
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   205
                        ("Recursive definition " ^ id ^ 
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   206
                         " would clash with the theory of the same name!")
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   207
       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
   208
       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
   209
                Prim.post_definition (thy,(def,pats))
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   210
       val {lhs=f,rhs} = S.dest_eq(concl def)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   211
       val (_,[R,_]) = S.strip_comb rhs
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   212
       val {induction, rules, tcs} = 
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   213
             proof_stage theory reducer
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   214
               {f = f, R = R, rules = rules,
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   215
                full_pats_TCs = full_pats_TCs,
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   216
                TCs = TCs}
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   217
       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
   218
   in  {induct = meta_outer
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   219
                  (normalize_thm [RSspec,RSmp] (induction RS spec')), 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   220
        rules = rules', 
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3208
diff changeset
   221
        tcs = (termination_goals rules') @ tcs}
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   222
   end
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   223
  handle Utils.ERR {mesg,...} => error mesg
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   226
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   227
 *
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   228
 *     Definitions with synthesized termination relation temporarily
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   229
 *     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
   230
 *     the Isabelle theory file scheme, which restricts
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   231
 *     inference at theory-construction time.
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   232
 *
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   234
local structure R = Prim.Rules
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   235
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
fun function theory eqs = 
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   237
 let val dummy = prs "Making definition..   "
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   238
     val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
     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
   240
     val dummy = prs "Definition made.\n"
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   241
     val dummy = prs "Proving induction theorem..  "
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
     val induction = Prim.mk_induction theory f R full_pats_TCs
3208
8336393de482 Subst now moved to directory HOL
paulson
parents: 3191
diff changeset
   243
     val dummy = prs "Induction theorem proved.\n"
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   244
 in {theory = theory, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
     eq_ind = standard (induction RS (rules RS conjI))}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
 end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
 handle (e as Utils.ERR _) => Utils.Raise e
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
      |     e              => print_exn e
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   250
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
fun lazyR_def theory eqs = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
   let val {rules,theory, ...} = Prim.lazyR_def theory eqs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   254
   in {eqns=rules, theory=theory}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
   end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
   handle (e as Utils.ERR _) => Utils.Raise e
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   257
        |     e              => print_exn e;
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   258
 *
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   259
 *
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   260
 *---------------------------------------------------------------------------*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   261
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   262
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   263
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   264
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   265
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   266
 * 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
   267
 * have already been added in thry.sml
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   268
 *---------------------------------------------------------------------------*)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2467
diff changeset
   269
val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG];
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   270
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
end;