src/HOL/Tools/TFL/post.ML
author haftmann
Wed, 21 Oct 2009 08:14:38 +0200
changeset 33038 8f9594c31de4
parent 32959 23a8c5ac35f8
child 33042 ddf1f03a9ad9
permissions -rw-r--r--
dropped redundant gen_ prefix
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 ->
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    11
    term -> term list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    12
  val define: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    13
    string -> string list -> theory * {rules: (thm * int) list, induct: thm, tcs: term list}
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
structure S = USyntax
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    22
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    23
(* misc *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    24
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    25
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    26
 * 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
    27
 * have a tactic directly applied to them.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    28
 *--------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    29
fun termination_goals rules =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    30
    map (Type.freeze o HOLogic.dest_Trueprop)
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 32959
diff changeset
    31
      (List.foldr (fn (th,A) => union (op aconv) (prems_of th, A)) [] rules);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    32
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    33
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    34
 * Three postprocessors are applied to the definition.  It
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    35
 * attempts to prove wellfoundedness of the given relation, simplifies the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    36
 * non-proved termination conditions, and finally attempts to prove the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    37
 * simplified termination conditions.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    38
 *--------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    39
fun std_postprocessor strict cs ss wfs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    40
  Prim.postprocess strict
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    41
   {wf_tac     = REPEAT (ares_tac wfs 1),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    42
    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
    43
                 THEN TRY (Arith_Data.arith_tac (Simplifier.the_context ss) 1 ORELSE
23880
64b9806e160b dropped Nat.ML legacy bindings
haftmann
parents: 23150
diff changeset
    44
                           fast_tac (cs addSDs [@{thm not0_implies_Suc}] addss ss) 1),
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    45
    simplifier = Rules.simpl_conv ss []};
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    46
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    47
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    48
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    49
val concl = #2 o Rules.dest_thm;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    50
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    51
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    52
 * Postprocess a definition made by "define". This is a separate stage of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    53
 * processing from the definition stage.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    54
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    55
local
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    56
structure R = Rules
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    57
structure U = Utils
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    58
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    59
(* The rest of these local definitions are for the tricky nested case *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    60
val solved = not o can S.dest_eq o #2 o S.strip_forall o concl
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    61
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    62
fun id_thm th =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    63
   let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    64
   in lhs aconv rhs end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    65
   handle U.ERR _ => false;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    66
   
30737
9ffd27558916 dropped legacy goal package call
haftmann
parents: 30686
diff changeset
    67
val P_imp_P_eq_True = @{thm eqTrueI} RS eq_reflection;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    68
fun mk_meta_eq r = case concl_of r of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    69
     Const("==",_)$_$_ => r
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    70
  |   _ $(Const("op =",_)$_$_) => r RS eq_reflection
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    71
  |   _ => r RS P_imp_P_eq_True
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    72
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    73
(*Is this the best way to invoke the simplifier??*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    74
fun rewrite L = rewrite_rule (map mk_meta_eq (List.filter(not o id_thm) L))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    75
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    76
fun join_assums th =
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26478
diff changeset
    77
  let val thy = Thm.theory_of_thm th
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    78
      val tych = cterm_of thy
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    79
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    80
      val cntxtl = (#1 o S.strip_imp) lhs  (* cntxtl should = cntxtr *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    81
      val cntxtr = (#1 o S.strip_imp) rhs  (* but union is solider *)
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 32959
diff changeset
    82
      val cntxt = union (op aconv) (cntxtl, cntxtr)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    83
  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    84
    R.GEN_ALL
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    85
      (R.DISCH_ALL
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    86
         (rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    87
  end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    88
  val gen_all = S.gen_all
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    89
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    90
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
    91
  let
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
    92
    val _ = writeln "Proving induction theorem ..."
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    93
    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
    94
    val _ = writeln "Postprocessing ...";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    95
    val {rules, induction, nested_tcs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    96
      std_postprocessor strict cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    97
  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    98
  case nested_tcs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    99
  of [] => {induction=induction, rules=rules,tcs=[]}
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
   100
  | L  => let val dummy = writeln "Simplifying nested TCs ..."
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   101
              val (solved,simplified,stubborn) =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   102
               fold_rev (fn th => fn (So,Si,St) =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   103
                     if (id_thm th) then (So, Si, th::St) else
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   104
                     if (solved th) then (th::So, Si, St)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   105
                     else (So, th::Si, St)) nested_tcs ([],[],[])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   106
              val simplified' = map join_assums simplified
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   107
              val dummy = (Prim.trace_thms "solved =" solved;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   108
                           Prim.trace_thms "simplified' =" simplified')
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   109
              val rewr = full_simplify (ss addsimps (solved @ simplified'));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   110
              val dummy = Prim.trace_thms "Simplifying the induction rule..."
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   111
                                          [induction]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   112
              val induction' = rewr induction
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   113
              val dummy = Prim.trace_thms "Simplifying the recursion rules..."
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   114
                                          [rules]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   115
              val rules'     = rewr rules
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
   116
              val _ = writeln "... Postprocessing finished";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   117
          in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   118
          {induction = induction',
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   119
               rules = rules',
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   120
                 tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   121
                           (simplified@stubborn)}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   122
          end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   123
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   124
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: curry the predicate of the induction rule*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   127
fun curry_rule rl =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   128
  SplitRule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl))) rl;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   129
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   130
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   131
val meta_outer =
32957
675c0c7e6a37 explicitly qualify Drule.standard;
wenzelm
parents: 32952
diff changeset
   132
  curry_rule o Drule.standard o
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   133
  rule_by_tactic (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   134
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   135
(*Strip off the outer !P*)
27239
f2f42f9fa09d pervasive RuleInsts;
wenzelm
parents: 27230
diff changeset
   136
val spec'= read_instantiate @{context} [(("x", 0), "P::?'b=>bool")] spec;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   137
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   138
fun tracing true _ = ()
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   139
  | tracing false msg = writeln msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   140
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   141
fun simplify_defn strict thy cs ss congs wfs id pats def0 =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   142
   let val def = Thm.freezeT def0 RS meta_eq_to_obj_eq
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   143
       val {rules,rows,TCs,full_pats_TCs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   144
           Prim.post_definition congs (thy, (def,pats))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   145
       val {lhs=f,rhs} = S.dest_eq (concl def)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   146
       val (_,[R,_]) = S.strip_comb rhs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   147
       val dummy = Prim.trace_thms "congs =" congs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   148
       (*the next step has caused simplifier looping in some cases*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   149
       val {induction, rules, tcs} =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   150
             proof_stage strict cs ss wfs thy
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   151
               {f = f, R = R, rules = rules,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   152
                full_pats_TCs = full_pats_TCs,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   153
                TCs = TCs}
32957
675c0c7e6a37 explicitly qualify Drule.standard;
wenzelm
parents: 32952
diff changeset
   154
       val rules' = map (Drule.standard o ObjectLogic.rulify_no_asm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   155
                        (R.CONJUNCTS rules)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   156
         in  {induct = meta_outer (ObjectLogic.rulify_no_asm (induction RS spec')),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   157
        rules = ListPair.zip(rules', rows),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   158
        tcs = (termination_goals rules') @ tcs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   159
   end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   160
  handle U.ERR {mesg,func,module} =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   161
               error (mesg ^
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   162
                      "\n    (In TFL function " ^ module ^ "." ^ func ^ ")");
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   163
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   164
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   165
(* Derive the initial equations from the case-split rules to meet the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   166
users specification of the recursive function. 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   167
 Note: We don't do this if the wf conditions fail to be solved, as each
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   168
case may have a different wf condition. We could group the conditions
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   169
together and say that they must be true to solve the general case,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   170
but that would hide from the user which sub-case they were related
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   171
to. Probably this is not important, and it would work fine, but, for now, I
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   172
prefer leaving more fine-grain control to the user. 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   173
-- Lucas Dixon, Aug 2004 *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   174
local
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   175
  fun get_related_thms i = 
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 30737
diff changeset
   176
      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
   177
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   178
  fun solve_eq (th, [], i) = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   179
        error "derive_init_eqs: missing rules"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   180
    | solve_eq (th, [a], i) = [(a, i)]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   181
    | solve_eq (th, splitths as (_ :: _), i) = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   182
      (writeln "Proving unsplit equation...";
32957
675c0c7e6a37 explicitly qualify Drule.standard;
wenzelm
parents: 32952
diff changeset
   183
      [((Drule.standard o ObjectLogic.rulify_no_asm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   184
          (CaseSplit.splitto splitths th), i)])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   185
      (* if there's an error, pretend nothing happened with this definition 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   186
         We should probably print something out so that the user knows...? *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   187
      handle ERROR s => 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   188
             (warning ("recdef (solve_eq): " ^ s); map (fn x => (x,i)) splitths);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   189
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   190
fun derive_init_eqs sgn rules eqs = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   191
    let 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   192
      val eqths = map (Thm.trivial o (Thm.cterm_of sgn) o HOLogic.mk_Trueprop) 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   193
                      eqs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   194
      fun countlist l = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   195
          (rev o snd o (Library.foldl (fn ((i,L), e) => (i + 1,(e,i) :: L)))) ((0,[]), l)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   196
    in
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 30737
diff changeset
   197
      maps (fn (e,i) => solve_eq (e, (get_related_thms i rules), i)) (countlist eqths)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   198
    end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   199
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   200
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   201
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   202
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   203
 * Defining a function with an associated termination relation.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   204
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   205
fun define_i strict thy cs ss congs wfs fid R eqs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   206
  let val {functional,pats} = Prim.mk_functional thy eqs
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   207
      val (thy, def) = Prim.wfrec_definition0 thy (Long_Name.base_name fid) R functional
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   208
      val {induct, rules, tcs} = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   209
          simplify_defn strict thy cs ss congs wfs fid pats def
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   210
      val rules' = 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   211
          if strict then derive_init_eqs thy rules eqs
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   212
          else rules
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   213
  in (thy, {rules = rules', induct = induct, tcs = tcs}) end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   214
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   215
fun define strict thy cs ss congs wfs fid R seqs =
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   216
  define_i strict thy cs ss congs wfs fid
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   217
      (Syntax.read_term_global thy R) (map (Syntax.read_term_global thy) seqs)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   218
    handle U.ERR {mesg,...} => error mesg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   219
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   220
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   221
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   222
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   223
 *     Definitions with synthesized termination relation
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   224
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   225
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   226
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   227
fun func_of_cond_eqn tm =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   228
  #1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   229
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   230
fun defer_i thy congs fid eqs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   231
 let val {rules,R,theory,full_pats_TCs,SV,...} =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   232
             Prim.lazyR_def thy (Long_Name.base_name fid) congs eqs
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   233
     val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24707
diff changeset
   234
     val dummy = writeln "Proving induction theorem ...";
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   235
     val induction = Prim.mk_induction theory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   236
                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   237
 in (theory,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   238
     (*return the conjoined induction rule and recursion equations,
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   239
       with assumptions remaining to discharge*)
32957
675c0c7e6a37 explicitly qualify Drule.standard;
wenzelm
parents: 32952
diff changeset
   240
     Drule.standard (induction RS (rules RS conjI)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   241
 end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   242
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   243
fun defer thy congs fid seqs =
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24075
diff changeset
   244
  defer_i thy congs fid (map (Syntax.read_term_global thy) seqs)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   245
    handle U.ERR {mesg,...} => error mesg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   246
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   247
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   248
end;