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