src/HOL/Tools/TFL/post.ML
author bulwahn
Fri, 18 Mar 2011 18:19:42 +0100
changeset 42025 cb5b1e85b32e
parent 41895 a2e9af953b90
child 42361 23f352990944
permissions -rw-r--r--
adding eval option to quickcheck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/TFL/post.ML
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     Konrad Slind, Cambridge University Computer Laboratory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     3
    Copyright   1997  University of Cambridge
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     4
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     5
Second part of main module (postprocessing of TFL definitions).
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     6
*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     7
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     8
signature TFL =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     9
sig
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    10
  val define_i: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35625
diff changeset
    11
    term -> term list -> theory * {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list}
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    12
  val define: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35625
diff changeset
    13
    string -> string list -> theory * {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list}
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    14
  val defer_i: theory -> thm list -> xstring -> term list -> theory * thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    15
  val defer: theory -> thm list -> xstring -> string list -> theory * thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    16
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    17
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    18
structure Tfl: TFL =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    19
struct
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    20
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    21
(* misc *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    22
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    23
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    24
 * Extract termination goals so that they can be put it into a goalstack, or
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    25
 * have a tactic directly applied to them.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    26
 *--------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    27
fun termination_goals rules =
33832
cff42395c246 explicitly mark some legacy freeze operations;
wenzelm
parents: 33339
diff changeset
    28
    map (Type.legacy_freeze o HOLogic.dest_Trueprop)
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
    29
      (fold_rev (union (op aconv) o prems_of) rules []);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    30
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    31
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    32
 * Three postprocessors are applied to the definition.  It
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    33
 * attempts to prove wellfoundedness of the given relation, simplifies the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    34
 * non-proved termination conditions, and finally attempts to prove the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    35
 * simplified termination conditions.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    36
 *--------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    37
fun std_postprocessor strict cs ss wfs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    38
  Prim.postprocess strict
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    39
   {wf_tac     = REPEAT (ares_tac wfs 1),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    40
    terminator = asm_simp_tac ss 1
30686
47a32dd1b86e moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
haftmann
parents: 30364
diff changeset
    41
                 THEN TRY (Arith_Data.arith_tac (Simplifier.the_context ss) 1 ORELSE
23880
64b9806e160b dropped Nat.ML legacy bindings
haftmann
parents: 23150
diff changeset
    42
                           fast_tac (cs addSDs [@{thm not0_implies_Suc}] addss ss) 1),
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    43
    simplifier = Rules.simpl_conv ss []};
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    44
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    45
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    46
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    47
val concl = #2 o Rules.dest_thm;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    48
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    49
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    50
 * Postprocess a definition made by "define". This is a separate stage of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    51
 * processing from the definition stage.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    52
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    53
local
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    54
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    55
(* The rest of these local definitions are for the tricky nested case *)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    56
val solved = not o can USyntax.dest_eq o #2 o USyntax.strip_forall o concl
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    57
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    58
fun id_thm th =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    59
   let val {lhs,rhs} = USyntax.dest_eq (#2 (USyntax.strip_forall (#2 (Rules.dest_thm th))));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    60
   in lhs aconv rhs end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    61
   handle Utils.ERR _ => false;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    62
   
30737
9ffd27558916 dropped legacy goal package call
haftmann
parents: 30686
diff changeset
    63
val P_imp_P_eq_True = @{thm eqTrueI} RS eq_reflection;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    64
fun mk_meta_eq r = case concl_of r of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    65
     Const("==",_)$_$_ => r
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38549
diff changeset
    66
  |   _ $(Const(@{const_name HOL.eq},_)$_$_) => r RS eq_reflection
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    67
  |   _ => r RS P_imp_P_eq_True
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    68
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    69
(*Is this the best way to invoke the simplifier??*)
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33245
diff changeset
    70
fun rewrite L = rewrite_rule (map mk_meta_eq (filter_out id_thm L))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    71
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    72
fun join_assums th =
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26478
diff changeset
    73
  let val thy = Thm.theory_of_thm th
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    74
      val tych = cterm_of thy
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    75
      val {lhs,rhs} = USyntax.dest_eq(#2 (USyntax.strip_forall (concl th)))
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    76
      val cntxtl = (#1 o USyntax.strip_imp) lhs  (* cntxtl should = cntxtr *)
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    77
      val cntxtr = (#1 o USyntax.strip_imp) rhs  (* but union is solider *)
33042
ddf1f03a9ad9 curried union as canonical list operation
haftmann
parents: 33038
diff changeset
    78
      val cntxt = union (op aconv) cntxtl cntxtr
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    79
  in
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    80
    Rules.GEN_ALL
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    81
      (Rules.DISCH_ALL
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    82
         (rewrite (map (Rules.ASSUME o tych) cntxt) (Rules.SPEC_ALL th)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    83
  end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
    84
  val gen_all = USyntax.gen_all
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    85
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    86
fun proof_stage strict cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    87
  let
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
    88
    val _ = writeln "Proving induction theorem ..."
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    89
    val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
    90
    val _ = writeln "Postprocessing ...";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    91
    val {rules, induction, nested_tcs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    92
      std_postprocessor strict cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    93
  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    94
  case nested_tcs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    95
  of [] => {induction=induction, rules=rules,tcs=[]}
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
    96
  | L  => let val dummy = writeln "Simplifying nested TCs ..."
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    97
              val (solved,simplified,stubborn) =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    98
               fold_rev (fn th => fn (So,Si,St) =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    99
                     if (id_thm th) then (So, Si, th::St) else
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   100
                     if (solved th) then (th::So, Si, St)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   101
                     else (So, th::Si, St)) nested_tcs ([],[],[])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   102
              val simplified' = map join_assums simplified
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   103
              val dummy = (Prim.trace_thms "solved =" solved;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   104
                           Prim.trace_thms "simplified' =" simplified')
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   105
              val rewr = full_simplify (ss addsimps (solved @ simplified'));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   106
              val dummy = Prim.trace_thms "Simplifying the induction rule..."
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   107
                                          [induction]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   108
              val induction' = rewr induction
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   109
              val dummy = Prim.trace_thms "Simplifying the recursion rules..."
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   110
                                          [rules]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   111
              val rules'     = rewr rules
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
   112
              val _ = writeln "... Postprocessing finished";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   113
          in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   114
          {induction = induction',
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   115
               rules = rules',
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   116
                 tcs = map (gen_all o USyntax.rhs o #2 o USyntax.strip_forall o concl)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   117
                           (simplified@stubborn)}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   118
          end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   119
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   120
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   121
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   122
(*lcp: curry the predicate of the induction rule*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   123
fun curry_rule rl =
35365
2fcd08c62495 modernized structure Split_Rule;
wenzelm
parents: 35021
diff changeset
   124
  Split_Rule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl))) rl;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   125
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   126
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
36546
a9873318fe30 proper context for rule_by_tactic;
wenzelm
parents: 35799
diff changeset
   127
fun meta_outer ctxt =
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 33832
diff changeset
   128
  curry_rule o Drule.export_without_context o
36546
a9873318fe30 proper context for rule_by_tactic;
wenzelm
parents: 35799
diff changeset
   129
  rule_by_tactic ctxt (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   130
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   131
(*Strip off the outer !P*)
27239
f2f42f9fa09d pervasive RuleInsts;
wenzelm
parents: 27230
diff changeset
   132
val spec'= read_instantiate @{context} [(("x", 0), "P::?'b=>bool")] spec;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   133
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   134
fun tracing true _ = ()
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   135
  | tracing false msg = writeln msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   136
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   137
fun simplify_defn strict thy cs ss congs wfs id pats def0 =
36546
a9873318fe30 proper context for rule_by_tactic;
wenzelm
parents: 35799
diff changeset
   138
   let
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36546
diff changeset
   139
       val ctxt = ProofContext.init_global thy
36616
712724c32ac8 replaced Thm.legacy_freezeT by Thm.unvarify_global -- these facts stem from closed definitions, i.e. there are no term Vars;
wenzelm
parents: 36615
diff changeset
   140
       val def = Thm.unvarify_global def0 RS meta_eq_to_obj_eq
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   141
       val {rules,rows,TCs,full_pats_TCs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   142
           Prim.post_definition congs (thy, (def,pats))
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   143
       val {lhs=f,rhs} = USyntax.dest_eq (concl def)
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   144
       val (_,[R,_]) = USyntax.strip_comb rhs
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   145
       val dummy = Prim.trace_thms "congs =" congs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   146
       (*the next step has caused simplifier looping in some cases*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   147
       val {induction, rules, tcs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   148
             proof_stage strict cs ss wfs thy
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   149
               {f = f, R = R, rules = rules,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   150
                full_pats_TCs = full_pats_TCs,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   151
                TCs = TCs}
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35365
diff changeset
   152
       val rules' = map (Drule.export_without_context o Object_Logic.rulify_no_asm)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   153
                        (Rules.CONJUNCTS rules)
36546
a9873318fe30 proper context for rule_by_tactic;
wenzelm
parents: 35799
diff changeset
   154
         in  {induct = meta_outer ctxt (Object_Logic.rulify_no_asm (induction RS spec')),
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   155
        rules = ListPair.zip(rules', rows),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   156
        tcs = (termination_goals rules') @ tcs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   157
   end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   158
  handle Utils.ERR {mesg,func,module} =>
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   159
               error (mesg ^
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   160
                      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   161
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   162
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   163
(* Derive the initial equations from the case-split rules to meet the
41895
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   164
users specification of the recursive function. *)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   165
local
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   166
  fun get_related_thms i = 
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 30737
diff changeset
   167
      map_filter ((fn (r,x) => if x = i then SOME r else NONE));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   168
41895
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   169
  fun solve_eq (th, [], i) =  error "derive_init_eqs: missing rules"
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   170
    | solve_eq (th, [a], i) = [(a, i)]
41895
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   171
    | solve_eq (th, splitths, i) =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   172
      (writeln "Proving unsplit equation...";
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35365
diff changeset
   173
      [((Drule.export_without_context o Object_Logic.rulify_no_asm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   174
          (CaseSplit.splitto splitths th), i)])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   175
      handle ERROR s => 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   176
             (warning ("recdef (solve_eq): " ^ s); map (fn x => (x,i)) splitths);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   177
in
41895
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   178
fun derive_init_eqs thy rules eqs =
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   179
  map (Thm.trivial o Thm.cterm_of thy o HOLogic.mk_Trueprop) eqs
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   180
  |> map_index (fn (i, e) => solve_eq (e, (get_related_thms i rules), i))
a2e9af953b90 clarified
krauss
parents: 41164
diff changeset
   181
  |> flat;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   182
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   183
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   184
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   185
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   186
 * Defining a function with an associated termination relation.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   187
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   188
fun define_i strict thy cs ss congs wfs fid R eqs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   189
  let val {functional,pats} = Prim.mk_functional thy eqs
35799
7adb03f27b28 preserve full const name more carefully, and avoid slightly odd Sign.intern_term;
wenzelm
parents: 35756
diff changeset
   190
      val (thy, def) = Prim.wfrec_definition0 thy fid R functional
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35625
diff changeset
   191
      val (lhs, _) = Logic.dest_equals (prop_of def)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   192
      val {induct, rules, tcs} = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   193
          simplify_defn strict thy cs ss congs wfs fid pats def
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   194
      val rules' = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   195
          if strict then derive_init_eqs thy rules eqs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   196
          else rules
35756
cfde251d03a5 refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
bulwahn
parents: 35625
diff changeset
   197
  in (thy, {lhs = lhs, rules = rules', induct = induct, tcs = tcs}) end;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   198
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   199
fun define strict thy cs ss congs wfs fid R seqs =
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   200
  define_i strict thy cs ss congs wfs fid
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   201
      (Syntax.read_term_global thy R) (map (Syntax.read_term_global thy) seqs)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   202
    handle Utils.ERR {mesg,...} => error mesg;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   203
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   204
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   205
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   206
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   207
 *     Definitions with synthesized termination relation
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   208
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   209
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   210
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   211
fun func_of_cond_eqn tm =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   212
  #1 (USyntax.strip_comb (#lhs (USyntax.dest_eq (#2 (USyntax.strip_forall (#2 (USyntax.strip_imp tm)))))));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   213
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   214
fun defer_i thy congs fid eqs =
35799
7adb03f27b28 preserve full const name more carefully, and avoid slightly odd Sign.intern_term;
wenzelm
parents: 35756
diff changeset
   215
 let val {rules,R,theory,full_pats_TCs,SV,...} = Prim.lazyR_def thy fid congs eqs
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   216
     val f = func_of_cond_eqn (concl (Rules.CONJUNCT1 rules handle Utils.ERR _ => rules));
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
   217
     val dummy = writeln "Proving induction theorem ...";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   218
     val induction = Prim.mk_induction theory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   219
                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   220
 in (theory,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   221
     (*return the conjoined induction rule and recursion equations,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   222
       with assumptions remaining to discharge*)
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 33832
diff changeset
   223
     Drule.export_without_context (induction RS (rules RS conjI)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   224
 end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   225
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   226
fun defer thy congs fid seqs =
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   227
  defer_i thy congs fid (map (Syntax.read_term_global thy) seqs)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38864
diff changeset
   228
    handle Utils.ERR {mesg,...} => error mesg;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   229
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   230
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   231
end;