src/HOL/Tools/TFL/casesplit.ML
author wenzelm
Fri Jul 17 23:11:40 2009 +0200 (2009-07-17)
changeset 32035 8e77b6a250d5
parent 31784 bd3486c57ba3
child 32091 30e2ffbba718
permissions -rw-r--r--
tuned/modernized Envir.subst_XXX;
wenzelm@23150
     1
(*  Title:      HOL/Tools/TFL/casesplit.ML
wenzelm@23150
     2
    Author:     Lucas Dixon, University of Edinburgh
wenzelm@23150
     3
wenzelm@23150
     4
A structure that defines a tactic to program case splits.
wenzelm@23150
     5
wenzelm@23150
     6
    casesplit_free :
wenzelm@23150
     7
      string * typ -> int -> thm -> thm Seq.seq
wenzelm@23150
     8
wenzelm@23150
     9
    casesplit_name :
wenzelm@23150
    10
      string -> int -> thm -> thm Seq.seq
wenzelm@23150
    11
wenzelm@23150
    12
These use the induction theorem associated with the recursive data
wenzelm@23150
    13
type to be split.
wenzelm@23150
    14
wenzelm@23150
    15
The structure includes a function to try and recursively split a
wenzelm@23150
    16
conjecture into a list sub-theorems:
wenzelm@23150
    17
wenzelm@23150
    18
    splitto : thm list -> thm -> thm
wenzelm@23150
    19
*)
wenzelm@23150
    20
wenzelm@23150
    21
(* logic-specific *)
wenzelm@23150
    22
signature CASE_SPLIT_DATA =
wenzelm@23150
    23
sig
wenzelm@23150
    24
  val dest_Trueprop : term -> term
wenzelm@23150
    25
  val mk_Trueprop : term -> term
wenzelm@23150
    26
  val atomize : thm list
wenzelm@23150
    27
  val rulify : thm list
wenzelm@23150
    28
end;
wenzelm@23150
    29
wenzelm@23150
    30
structure CaseSplitData_HOL : CASE_SPLIT_DATA =
wenzelm@23150
    31
struct
wenzelm@23150
    32
val dest_Trueprop = HOLogic.dest_Trueprop;
wenzelm@23150
    33
val mk_Trueprop = HOLogic.mk_Trueprop;
wenzelm@23150
    34
wenzelm@23150
    35
val atomize = thms "induct_atomize";
wenzelm@23150
    36
val rulify = thms "induct_rulify";
wenzelm@23150
    37
val rulify_fallback = thms "induct_rulify_fallback";
wenzelm@23150
    38
wenzelm@23150
    39
end;
wenzelm@23150
    40
wenzelm@23150
    41
wenzelm@23150
    42
signature CASE_SPLIT =
wenzelm@23150
    43
sig
wenzelm@23150
    44
  (* failure to find a free to split on *)
wenzelm@23150
    45
  exception find_split_exp of string
wenzelm@23150
    46
wenzelm@23150
    47
  (* getting a case split thm from the induction thm *)
wenzelm@23150
    48
  val case_thm_of_ty : theory -> typ -> thm
wenzelm@23150
    49
  val cases_thm_of_induct_thm : thm -> thm
wenzelm@23150
    50
wenzelm@23150
    51
  (* case split tactics *)
wenzelm@23150
    52
  val casesplit_free :
wenzelm@23150
    53
      string * typ -> int -> thm -> thm Seq.seq
wenzelm@23150
    54
  val casesplit_name : string -> int -> thm -> thm Seq.seq
wenzelm@23150
    55
wenzelm@23150
    56
  (* finding a free var to split *)
wenzelm@23150
    57
  val find_term_split :
wenzelm@23150
    58
      term * term -> (string * typ) option
wenzelm@23150
    59
  val find_thm_split :
wenzelm@23150
    60
      thm -> int -> thm -> (string * typ) option
wenzelm@23150
    61
  val find_thms_split :
wenzelm@23150
    62
      thm list -> int -> thm -> (string * typ) option
wenzelm@23150
    63
wenzelm@23150
    64
  (* try to recursively split conjectured thm to given list of thms *)
wenzelm@23150
    65
  val splitto : thm list -> thm -> thm
wenzelm@23150
    66
wenzelm@23150
    67
  (* for use with the recdef package *)
wenzelm@23150
    68
  val derive_init_eqs :
wenzelm@23150
    69
      theory ->
wenzelm@23150
    70
      (thm * int) list -> term list -> (thm * int) list
wenzelm@23150
    71
end;
wenzelm@23150
    72
wenzelm@23150
    73
functor CaseSplitFUN(Data : CASE_SPLIT_DATA) =
wenzelm@23150
    74
struct
wenzelm@23150
    75
wenzelm@23150
    76
val rulify_goals = MetaSimplifier.rewrite_goals_rule Data.rulify;
wenzelm@23150
    77
val atomize_goals = MetaSimplifier.rewrite_goals_rule Data.atomize;
wenzelm@23150
    78
wenzelm@23150
    79
(* beta-eta contract the theorem *)
wenzelm@23150
    80
fun beta_eta_contract thm =
wenzelm@23150
    81
    let
wenzelm@23150
    82
      val thm2 = equal_elim (Thm.beta_conversion true (Thm.cprop_of thm)) thm
wenzelm@23150
    83
      val thm3 = equal_elim (Thm.eta_conversion (Thm.cprop_of thm2)) thm2
wenzelm@23150
    84
    in thm3 end;
wenzelm@23150
    85
wenzelm@23150
    86
(* make a casethm from an induction thm *)
wenzelm@23150
    87
val cases_thm_of_induct_thm =
wenzelm@23150
    88
     Seq.hd o (ALLGOALS (fn i => REPEAT (etac Drule.thin_rl i)));
wenzelm@23150
    89
wenzelm@23150
    90
(* get the case_thm (my version) from a type *)
haftmann@31784
    91
fun case_thm_of_ty thy ty  =
wenzelm@23150
    92
    let
wenzelm@23150
    93
      val ty_str = case ty of
wenzelm@23150
    94
                     Type(ty_str, _) => ty_str
wenzelm@23150
    95
                   | TFree(s,_)  => error ("Free type: " ^ s)
wenzelm@23150
    96
                   | TVar((s,i),_) => error ("Free variable: " ^ s)
haftmann@31784
    97
      val dt = Datatype.the_info thy ty_str
wenzelm@23150
    98
    in
wenzelm@23150
    99
      cases_thm_of_induct_thm (#induction dt)
wenzelm@23150
   100
    end;
wenzelm@23150
   101
wenzelm@23150
   102
(*
wenzelm@29265
   103
 val ty = (snd o hd o map Term.dest_Free o OldTerm.term_frees) t;
wenzelm@23150
   104
*)
wenzelm@23150
   105
wenzelm@23150
   106
wenzelm@23150
   107
(* for use when there are no prems to the subgoal *)
wenzelm@23150
   108
(* does a case split on the given variable *)
wenzelm@23150
   109
fun mk_casesplit_goal_thm sgn (vstr,ty) gt =
wenzelm@23150
   110
    let
wenzelm@23150
   111
      val x = Free(vstr,ty)
wenzelm@23150
   112
      val abst = Abs(vstr, ty, Term.abstract_over (x, gt));
wenzelm@23150
   113
wenzelm@23150
   114
      val ctermify = Thm.cterm_of sgn;
wenzelm@23150
   115
      val ctypify = Thm.ctyp_of sgn;
wenzelm@23150
   116
      val case_thm = case_thm_of_ty sgn ty;
wenzelm@23150
   117
wenzelm@23150
   118
      val abs_ct = ctermify abst;
wenzelm@23150
   119
      val free_ct = ctermify x;
wenzelm@23150
   120
wenzelm@29265
   121
      val casethm_vars = rev (OldTerm.term_vars (Thm.concl_of case_thm));
wenzelm@23150
   122
wenzelm@29270
   123
      val casethm_tvars = OldTerm.term_tvars (Thm.concl_of case_thm);
wenzelm@23150
   124
      val (Pv, Dv, type_insts) =
wenzelm@23150
   125
          case (Thm.concl_of case_thm) of
wenzelm@23150
   126
            (_ $ ((Pv as Var(P,Pty)) $ (Dv as Var(D, Dty)))) =>
wenzelm@23150
   127
            (Pv, Dv,
wenzelm@23150
   128
             Sign.typ_match sgn (Dty, ty) Vartab.empty)
wenzelm@23150
   129
          | _ => error "not a valid case thm";
wenzelm@23150
   130
      val type_cinsts = map (fn (ixn, (S, T)) => (ctypify (TVar (ixn, S)), ctypify T))
wenzelm@23150
   131
        (Vartab.dest type_insts);
wenzelm@32035
   132
      val cPv = ctermify (Envir.subst_term_types type_insts Pv);
wenzelm@32035
   133
      val cDv = ctermify (Envir.subst_term_types type_insts Dv);
wenzelm@23150
   134
    in
wenzelm@23150
   135
      (beta_eta_contract
wenzelm@23150
   136
         (case_thm
wenzelm@23150
   137
            |> Thm.instantiate (type_cinsts, [])
wenzelm@23150
   138
            |> Thm.instantiate ([], [(cPv, abs_ct), (cDv, free_ct)])))
wenzelm@23150
   139
    end;
wenzelm@23150
   140
wenzelm@23150
   141
wenzelm@23150
   142
(* for use when there are no prems to the subgoal *)
wenzelm@23150
   143
(* does a case split on the given variable (Free fv) *)
wenzelm@23150
   144
fun casesplit_free fv i th =
wenzelm@23150
   145
    let
wenzelm@23150
   146
      val (subgoalth, exp) = IsaND.fix_alls i th;
wenzelm@23150
   147
      val subgoalth' = atomize_goals subgoalth;
wenzelm@23150
   148
      val gt = Data.dest_Trueprop (Logic.get_goal (Thm.prop_of subgoalth') 1);
wenzelm@23150
   149
      val sgn = Thm.theory_of_thm th;
wenzelm@23150
   150
wenzelm@23150
   151
      val splitter_thm = mk_casesplit_goal_thm sgn fv gt;
wenzelm@23150
   152
      val nsplits = Thm.nprems_of splitter_thm;
wenzelm@23150
   153
wenzelm@23150
   154
      val split_goal_th = splitter_thm RS subgoalth';
wenzelm@23150
   155
      val rulified_split_goal_th = rulify_goals split_goal_th;
wenzelm@23150
   156
    in
wenzelm@23150
   157
      IsaND.export_back exp rulified_split_goal_th
wenzelm@23150
   158
    end;
wenzelm@23150
   159
wenzelm@23150
   160
wenzelm@23150
   161
(* for use when there are no prems to the subgoal *)
wenzelm@23150
   162
(* does a case split on the given variable *)
wenzelm@23150
   163
fun casesplit_name vstr i th =
wenzelm@23150
   164
    let
wenzelm@23150
   165
      val (subgoalth, exp) = IsaND.fix_alls i th;
wenzelm@23150
   166
      val subgoalth' = atomize_goals subgoalth;
wenzelm@23150
   167
      val gt = Data.dest_Trueprop (Logic.get_goal (Thm.prop_of subgoalth') 1);
wenzelm@23150
   168
wenzelm@29265
   169
      val freets = OldTerm.term_frees gt;
wenzelm@23150
   170
      fun getter x =
wenzelm@23150
   171
          let val (n,ty) = Term.dest_Free x in
wenzelm@23150
   172
            (if vstr = n orelse vstr = Name.dest_skolem n
wenzelm@23150
   173
             then SOME (n,ty) else NONE )
wenzelm@23150
   174
            handle Fail _ => NONE (* dest_skolem *)
wenzelm@23150
   175
          end;
wenzelm@23150
   176
      val (n,ty) = case Library.get_first getter freets
wenzelm@23150
   177
                of SOME (n, ty) => (n, ty)
wenzelm@23150
   178
                 | _ => error ("no such variable " ^ vstr);
wenzelm@23150
   179
      val sgn = Thm.theory_of_thm th;
wenzelm@23150
   180
wenzelm@23150
   181
      val splitter_thm = mk_casesplit_goal_thm sgn (n,ty) gt;
wenzelm@23150
   182
      val nsplits = Thm.nprems_of splitter_thm;
wenzelm@23150
   183
wenzelm@23150
   184
      val split_goal_th = splitter_thm RS subgoalth';
wenzelm@23150
   185
wenzelm@23150
   186
      val rulified_split_goal_th = rulify_goals split_goal_th;
wenzelm@23150
   187
    in
wenzelm@23150
   188
      IsaND.export_back exp rulified_split_goal_th
wenzelm@23150
   189
    end;
wenzelm@23150
   190
wenzelm@23150
   191
wenzelm@23150
   192
(* small example:
wenzelm@23150
   193
Goal "P (x :: nat) & (C y --> Q (y :: nat))";
wenzelm@23150
   194
by (rtac (thm "conjI") 1);
wenzelm@23150
   195
val th = topthm();
wenzelm@23150
   196
val i = 2;
wenzelm@23150
   197
val vstr = "y";
wenzelm@23150
   198
wenzelm@23150
   199
by (casesplit_name "y" 2);
wenzelm@23150
   200
wenzelm@23150
   201
val th = topthm();
wenzelm@23150
   202
val i = 1;
wenzelm@23150
   203
val th' = casesplit_name "x" i th;
wenzelm@23150
   204
*)
wenzelm@23150
   205
wenzelm@23150
   206
wenzelm@23150
   207
(* the find_XXX_split functions are simply doing a lightwieght (I
wenzelm@23150
   208
think) term matching equivalent to find where to do the next split *)
wenzelm@23150
   209
wenzelm@23150
   210
(* assuming two twems are identical except for a free in one at a
wenzelm@23150
   211
subterm, or constant in another, ie assume that one term is a plit of
wenzelm@23150
   212
another, then gives back the free variable that has been split. *)
wenzelm@23150
   213
exception find_split_exp of string
wenzelm@23150
   214
fun find_term_split (Free v, _ $ _) = SOME v
wenzelm@23150
   215
  | find_term_split (Free v, Const _) = SOME v
wenzelm@23150
   216
  | find_term_split (Free v, Abs _) = SOME v (* do we really want this case? *)
wenzelm@23150
   217
  | find_term_split (Free v, Var _) = NONE (* keep searching *)
wenzelm@23150
   218
  | find_term_split (a $ b, a2 $ b2) =
wenzelm@23150
   219
    (case find_term_split (a, a2) of
wenzelm@23150
   220
       NONE => find_term_split (b,b2)
wenzelm@23150
   221
     | vopt => vopt)
wenzelm@23150
   222
  | find_term_split (Abs(_,ty,t1), Abs(_,ty2,t2)) =
wenzelm@23150
   223
    find_term_split (t1, t2)
wenzelm@23150
   224
  | find_term_split (Const (x,ty), Const(x2,ty2)) =
wenzelm@23150
   225
    if x = x2 then NONE else (* keep searching *)
wenzelm@23150
   226
    raise find_split_exp (* stop now *)
wenzelm@23150
   227
            "Terms are not identical upto a free varaible! (Consts)"
wenzelm@23150
   228
  | find_term_split (Bound i, Bound j) =
wenzelm@23150
   229
    if i = j then NONE else (* keep searching *)
wenzelm@23150
   230
    raise find_split_exp (* stop now *)
wenzelm@23150
   231
            "Terms are not identical upto a free varaible! (Bound)"
wenzelm@23150
   232
  | find_term_split (a, b) =
wenzelm@23150
   233
    raise find_split_exp (* stop now *)
wenzelm@23150
   234
            "Terms are not identical upto a free varaible! (Other)";
wenzelm@23150
   235
wenzelm@23150
   236
(* assume that "splitth" is a case split form of subgoal i of "genth",
wenzelm@23150
   237
then look for a free variable to split, breaking the subgoal closer to
wenzelm@23150
   238
splitth. *)
wenzelm@23150
   239
fun find_thm_split splitth i genth =
wenzelm@23150
   240
    find_term_split (Logic.get_goal (Thm.prop_of genth) i,
wenzelm@23150
   241
                     Thm.concl_of splitth) handle find_split_exp _ => NONE;
wenzelm@23150
   242
wenzelm@23150
   243
(* as above but searches "splitths" for a theorem that suggest a case split *)
wenzelm@23150
   244
fun find_thms_split splitths i genth =
wenzelm@23150
   245
    Library.get_first (fn sth => find_thm_split sth i genth) splitths;
wenzelm@23150
   246
wenzelm@23150
   247
wenzelm@23150
   248
(* split the subgoal i of "genth" until we get to a member of
wenzelm@23150
   249
splitths. Assumes that genth will be a general form of splitths, that
wenzelm@23150
   250
can be case-split, as needed. Otherwise fails. Note: We assume that
wenzelm@23150
   251
all of "splitths" are split to the same level, and thus it doesn't
wenzelm@23150
   252
matter which one we choose to look for the next split. Simply add
wenzelm@23150
   253
search on splitthms and split variable, to change this.  *)
wenzelm@23150
   254
(* Note: possible efficiency measure: when a case theorem is no longer
wenzelm@23150
   255
useful, drop it? *)
wenzelm@23150
   256
(* Note: This should not be a separate tactic but integrated into the
wenzelm@23150
   257
case split done during recdef's case analysis, this would avoid us
wenzelm@23150
   258
having to (re)search for variables to split. *)
wenzelm@23150
   259
fun splitto splitths genth =
wenzelm@23150
   260
    let
wenzelm@23150
   261
      val _ = not (null splitths) orelse error "splitto: no given splitths";
wenzelm@23150
   262
      val sgn = Thm.theory_of_thm genth;
wenzelm@23150
   263
wenzelm@23150
   264
      (* check if we are a member of splitths - FIXME: quicker and
wenzelm@23150
   265
      more flexible with discrim net. *)
wenzelm@23150
   266
      fun solve_by_splitth th split =
wenzelm@23150
   267
          Thm.biresolution false [(false,split)] 1 th;
wenzelm@23150
   268
wenzelm@23150
   269
      fun split th =
wenzelm@23150
   270
          (case find_thms_split splitths 1 th of
wenzelm@23150
   271
             NONE =>
wenzelm@23150
   272
             (writeln "th:";
wenzelm@23150
   273
              Display.print_thm th; writeln "split ths:";
wenzelm@23150
   274
              Display.print_thms splitths; writeln "\n--";
wenzelm@23150
   275
              error "splitto: cannot find variable to split on")
wenzelm@23150
   276
            | SOME v =>
wenzelm@23150
   277
             let
wenzelm@23150
   278
               val gt = Data.dest_Trueprop (List.nth(Thm.prems_of th, 0));
wenzelm@23150
   279
               val split_thm = mk_casesplit_goal_thm sgn v gt;
wenzelm@23150
   280
               val (subthms, expf) = IsaND.fixed_subgoal_thms split_thm;
wenzelm@23150
   281
             in
wenzelm@23150
   282
               expf (map recsplitf subthms)
wenzelm@23150
   283
             end)
wenzelm@23150
   284
wenzelm@23150
   285
      and recsplitf th =
wenzelm@23150
   286
          (* note: multiple unifiers! we only take the first element,
wenzelm@23150
   287
             probably fine -- there is probably only one anyway. *)
wenzelm@23150
   288
          (case Library.get_first (Seq.pull o solve_by_splitth th) splitths of
wenzelm@23150
   289
             NONE => split th
wenzelm@23150
   290
           | SOME (solved_th, more) => solved_th)
wenzelm@23150
   291
    in
wenzelm@23150
   292
      recsplitf genth
wenzelm@23150
   293
    end;
wenzelm@23150
   294
wenzelm@23150
   295
wenzelm@23150
   296
(* Note: We dont do this if wf conditions fail to be solved, as each
wenzelm@23150
   297
case may have a different wf condition - we could group the conditions
wenzelm@23150
   298
togeather and say that they must be true to solve the general case,
wenzelm@23150
   299
but that would hide from the user which sub-case they were related
wenzelm@23150
   300
to. Probably this is not important, and it would work fine, but I
wenzelm@23150
   301
prefer leaving more fine grain control to the user. *)
wenzelm@23150
   302
wenzelm@23150
   303
(* derive eqs, assuming strict, ie the rules have no assumptions = all
wenzelm@23150
   304
   the well-foundness conditions have been solved. *)
wenzelm@23150
   305
fun derive_init_eqs sgn rules eqs =
wenzelm@23150
   306
  let
wenzelm@23150
   307
    fun get_related_thms i =
wenzelm@23150
   308
      List.mapPartial ((fn (r, x) => if x = i then SOME r else NONE));
wenzelm@23150
   309
    fun add_eq (i, e) xs =
wenzelm@23150
   310
      (e, (get_related_thms i rules), i) :: xs
wenzelm@23150
   311
    fun solve_eq (th, [], i) =
wenzelm@23150
   312
          error "derive_init_eqs: missing rules"
wenzelm@23150
   313
      | solve_eq (th, [a], i) = (a, i)
wenzelm@23150
   314
      | solve_eq (th, splitths as (_ :: _), i) = (splitto splitths th, i);
wenzelm@23150
   315
    val eqths =
wenzelm@23150
   316
      map (Thm.trivial o Thm.cterm_of sgn o Data.mk_Trueprop) eqs;
wenzelm@23150
   317
  in
wenzelm@23150
   318
    []
wenzelm@23150
   319
    |> fold_index add_eq eqths
wenzelm@23150
   320
    |> map solve_eq
wenzelm@23150
   321
    |> rev
wenzelm@23150
   322
  end;
wenzelm@23150
   323
wenzelm@23150
   324
end;
wenzelm@23150
   325
wenzelm@23150
   326
wenzelm@23150
   327
structure CaseSplit = CaseSplitFUN(CaseSplitData_HOL);