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