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