TFL/post.ML
author paulson
Mon, 06 Aug 2001 12:42:43 +0200
changeset 11461 ffeac9aa1967
parent 11038 932d66879fe7
child 11632 6fc8de600f58
permissions -rw-r--r--
removed an unsuitable default simprule
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      TFL/post.ML
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     4
    Copyright   1997  University of Cambridge
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     5
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     6
Second part of main module (postprocessing of TFL definitions).
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     7
*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     8
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     9
signature TFL =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    10
sig
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    11
  val trace: bool ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    12
  val quiet_mode: bool ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    13
  val message: string -> unit
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    14
  val tgoalw: theory -> thm list -> thm list -> thm list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    15
  val tgoal: theory -> thm list -> thm list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    16
  val std_postprocessor: claset -> simpset -> thm list -> theory ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    17
    {induction: thm, rules: thm, TCs: term list list} ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    18
    {induction: thm, rules: thm, nested_tcs: thm list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    19
  val define_i: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    20
    term -> term list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    21
  val define: theory -> claset -> simpset -> thm list -> thm list -> xstring ->
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    22
    string -> string list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    23
  val defer_i: theory -> thm list -> xstring -> term list -> theory * thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    24
  val defer: theory -> thm list -> xstring -> string list -> theory * thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    25
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    26
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    27
structure Tfl: TFL =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    28
struct
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    29
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    30
structure S = USyntax
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    31
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    32
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    33
(* messages *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    34
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    35
val trace = Prim.trace
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    36
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    37
val quiet_mode = ref false;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    38
fun message s = if ! quiet_mode then () else writeln s;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    39
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    40
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    41
(* misc *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    42
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    43
fun read_term thy = Sign.simple_read_term (Theory.sign_of thy) HOLogic.termT;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    44
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    45
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    46
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    47
 * Extract termination goals so that they can be put it into a goalstack, or
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    48
 * have a tactic directly applied to them.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    49
 *--------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    50
fun termination_goals rules =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    51
    map (#1 o Type.freeze_thaw o HOLogic.dest_Trueprop)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    52
      (foldr (fn (th,A) => union_term (prems_of th, A)) (rules, []));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    53
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    54
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    55
 * Finds the termination conditions in (highly massaged) definition and
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    56
 * puts them into a goalstack.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    57
 *--------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    58
fun tgoalw thy defs rules =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    59
  case termination_goals rules of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    60
      [] => error "tgoalw: no termination conditions to prove"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    61
    | L  => goalw_cterm defs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    62
              (Thm.cterm_of (Theory.sign_of thy)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    63
                        (HOLogic.mk_Trueprop(USyntax.list_mk_conj L)));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    64
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    65
fun tgoal thy = tgoalw thy [];
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    66
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    67
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    68
 * Three postprocessors are applied to the definition.  It
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    69
 * attempts to prove wellfoundedness of the given relation, simplifies the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    70
 * non-proved termination conditions, and finally attempts to prove the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    71
 * simplified termination conditions.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    72
 *--------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    73
fun std_postprocessor cs ss wfs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    74
  Prim.postprocess
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    75
   {wf_tac     = REPEAT (ares_tac wfs 1),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    76
    terminator = asm_simp_tac ss 1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    77
                 THEN TRY (fast_tac (cs addSDs [not0_implies_Suc] addss ss) 1),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    78
    simplifier = Rules.simpl_conv ss []};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    79
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    80
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    81
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    82
val concl = #2 o Rules.dest_thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    83
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    84
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    85
 * Postprocess a definition made by "define". This is a separate stage of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    86
 * processing from the definition stage.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    87
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    88
local
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    89
structure R = Rules
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    90
structure U = Utils
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    91
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    92
(* The rest of these local definitions are for the tricky nested case *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    93
val solved = not o can S.dest_eq o #2 o S.strip_forall o concl
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    94
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    95
fun id_thm th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    96
   let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    97
   in lhs aconv rhs end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    98
   handle U.ERR _ => false;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    99
   
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   100
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   101
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   102
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   103
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   104
fun mk_meta_eq r = case concl_of r of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   105
     Const("==",_)$_$_ => r
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   106
  |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   107
  |   _ => r RS P_imp_P_eq_True
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   108
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   109
(*Is this the best way to invoke the simplifier??*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   110
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   111
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   112
fun join_assums th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   113
  let val {sign,...} = rep_thm th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   114
      val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   115
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   116
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   117
      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   118
      val cntxt = gen_union (op aconv) (cntxtl, cntxtr)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   119
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   120
    R.GEN_ALL
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   121
      (R.DISCH_ALL
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   122
         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   123
  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   124
  val gen_all = S.gen_all
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   125
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   126
fun proof_stage cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   127
  let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   128
    val _ = message "Proving induction theorem ..."
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   129
    val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   130
    val _ = message "Postprocessing ...";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   131
    val {rules, induction, nested_tcs} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   132
      std_postprocessor cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   133
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   134
  case nested_tcs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   135
  of [] => {induction=induction, rules=rules,tcs=[]}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   136
  | L  => let val dummy = message "Simplifying nested TCs ..."
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   137
              val (solved,simplified,stubborn) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   138
               U.itlist (fn th => fn (So,Si,St) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   139
                     if (id_thm th) then (So, Si, th::St) else
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   140
                     if (solved th) then (th::So, Si, St)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   141
                     else (So, th::Si, St)) nested_tcs ([],[],[])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   142
              val simplified' = map join_assums simplified
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   143
              val rewr = full_simplify (ss addsimps (solved @ simplified'));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   144
              val induction' = rewr induction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   145
              and rules'     = rewr rules
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   146
          in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   147
          {induction = induction',
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   148
               rules = rules',
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   149
                 tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   150
                           (simplified@stubborn)}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   151
          end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   152
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   153
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   154
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   155
(*lcp: curry the predicate of the induction rule*)
11038
wenzelm
parents: 10769
diff changeset
   156
fun curry_rule rl =
wenzelm
parents: 10769
diff changeset
   157
  SplitRule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl)), rl);
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   158
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   159
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   160
val meta_outer =
11038
wenzelm
parents: 10769
diff changeset
   161
  curry_rule o standard o
wenzelm
parents: 10769
diff changeset
   162
  rule_by_tactic (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   163
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   164
(*Strip off the outer !P*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   165
val spec'= read_instantiate [("x","P::?'b=>bool")] spec;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   166
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   167
fun simplify_defn thy cs ss congs wfs id pats def0 =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   168
   let val def = freezeT def0 RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   169
       val {theory,rules,rows,TCs,full_pats_TCs} = Prim.post_definition congs (thy, (def,pats))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   170
       val {lhs=f,rhs} = S.dest_eq (concl def)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   171
       val (_,[R,_]) = S.strip_comb rhs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   172
       val {induction, rules, tcs} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   173
             proof_stage cs ss wfs theory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   174
               {f = f, R = R, rules = rules,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   175
                full_pats_TCs = full_pats_TCs,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   176
                TCs = TCs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   177
       val rules' = map (standard o Rulify.rulify_no_asm) (R.CONJUNCTS rules)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   178
   in  {induct = meta_outer (Rulify.rulify_no_asm (induction RS spec')),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   179
        rules = ListPair.zip(rules', rows),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   180
        tcs = (termination_goals rules') @ tcs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   181
   end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   182
  handle U.ERR {mesg,func,module} =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   183
               error (mesg ^
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   184
                      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   185
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   186
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   187
 * Defining a function with an associated termination relation.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   188
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   189
fun define_i thy cs ss congs wfs fid R eqs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   190
  let val {functional,pats} = Prim.mk_functional thy eqs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   191
      val (thy, def) = Prim.wfrec_definition0 thy (Sign.base_name fid) R functional
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   192
  in (thy, simplify_defn thy cs ss congs wfs fid pats def) end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   193
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   194
fun define thy cs ss congs wfs fid R seqs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   195
  define_i thy cs ss congs wfs fid (read_term thy R) (map (read_term thy) seqs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   196
    handle U.ERR {mesg,...} => error mesg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   197
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   198
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   199
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   200
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   201
 *     Definitions with synthesized termination relation
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   202
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   203
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   204
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   205
fun func_of_cond_eqn tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   206
  #1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   207
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   208
fun defer_i thy congs fid eqs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   209
 let val {rules,R,theory,full_pats_TCs,SV,...} =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   210
             Prim.lazyR_def thy (Sign.base_name fid) congs eqs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   211
     val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   212
     val dummy = message "Proving induction theorem ...";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   213
     val induction = Prim.mk_induction theory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   214
                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   215
 in (theory,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   216
     (*return the conjoined induction rule and recursion equations,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   217
       with assumptions remaining to discharge*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   218
     standard (induction RS (rules RS conjI)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   219
 end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   220
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   221
fun defer thy congs fid seqs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   222
  defer_i thy congs fid (map (read_term thy) seqs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   223
    handle U.ERR {mesg,...} => error mesg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   224
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   225
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   226
end;