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