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