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