src/Pure/Isar/rule_cases.ML
author wenzelm
Wed Jun 24 21:28:02 2009 +0200 (2009-06-24)
changeset 31794 71af1fd6a5e4
parent 30364 577edc39b501
child 33303 1e1210f31207
permissions -rw-r--r--
renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
renamed Variable.importT_thms to Variable.importT (again);
wenzelm@8364
     1
(*  Title:      Pure/Isar/rule_cases.ML
wenzelm@8364
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8364
     3
wenzelm@18229
     4
Annotations and local contexts of rules.
wenzelm@8364
     5
*)
wenzelm@8364
     6
wenzelm@18188
     7
infix 1 THEN_ALL_NEW_CASES;
wenzelm@18188
     8
wenzelm@18229
     9
signature BASIC_RULE_CASES =
wenzelm@18229
    10
sig
wenzelm@18229
    11
  type cases
wenzelm@18229
    12
  type cases_tactic
wenzelm@18229
    13
  val CASES: cases -> tactic -> cases_tactic
wenzelm@18229
    14
  val NO_CASES: tactic -> cases_tactic
wenzelm@18229
    15
  val SUBGOAL_CASES: ((term * int) -> cases_tactic) -> int -> cases_tactic
wenzelm@18229
    16
  val THEN_ALL_NEW_CASES: (int -> cases_tactic) * (int -> tactic) -> int -> cases_tactic
wenzelm@18229
    17
end
wenzelm@18229
    18
wenzelm@8364
    19
signature RULE_CASES =
wenzelm@8364
    20
sig
wenzelm@18229
    21
  include BASIC_RULE_CASES
wenzelm@18608
    22
  datatype T = Case of
wenzelm@18608
    23
   {fixes: (string * typ) list,
wenzelm@18608
    24
    assumes: (string * term list) list,
wenzelm@18608
    25
    binds: (indexname * term option) list,
wenzelm@18608
    26
    cases: (string * T) list}
wenzelm@18608
    27
  val strip_params: term -> (string * typ) list
wenzelm@18608
    28
  val make_common: bool -> theory * term -> (string * string list) list -> cases
wenzelm@18608
    29
  val make_nested: bool -> term -> theory * term -> (string * string list) list -> cases
wenzelm@18608
    30
  val apply: term list -> T -> T
wenzelm@18237
    31
  val consume: thm list -> thm list -> ('a * int) * thm ->
wenzelm@18237
    32
    (('a * (int * thm list)) * thm) Seq.seq
wenzelm@18477
    33
  val add_consumes: int -> thm -> thm
wenzelm@24862
    34
  val get_consumes: thm -> int
wenzelm@18728
    35
  val consumes: int -> attribute
wenzelm@18728
    36
  val consumes_default: int -> attribute
wenzelm@8364
    37
  val name: string list -> thm -> thm
wenzelm@18728
    38
  val case_names: string list -> attribute
wenzelm@18728
    39
  val case_conclusion: string * string list -> attribute
wenzelm@12761
    40
  val save: thm -> thm -> thm
wenzelm@18237
    41
  val get: thm -> (string * string list) list * int
wenzelm@8427
    42
  val rename_params: string list list -> thm -> thm
wenzelm@18728
    43
  val params: string list list -> attribute
wenzelm@20289
    44
  val mutual_rule: Proof.context -> thm list -> (int list * thm) option
wenzelm@20289
    45
  val strict_mutual_rule: Proof.context -> thm list -> int list * thm
wenzelm@8364
    46
end;
wenzelm@8364
    47
wenzelm@8364
    48
structure RuleCases: RULE_CASES =
wenzelm@8364
    49
struct
wenzelm@8364
    50
wenzelm@18608
    51
(** cases **)
wenzelm@18608
    52
wenzelm@18608
    53
datatype T = Case of
wenzelm@18608
    54
 {fixes: (string * typ) list,
wenzelm@18608
    55
  assumes: (string * term list) list,
wenzelm@18608
    56
  binds: (indexname * term option) list,
wenzelm@18608
    57
  cases: (string * T) list};
wenzelm@18608
    58
wenzelm@18608
    59
type cases = (string * T option) list;
wenzelm@18608
    60
wenzelm@18608
    61
val case_conclN = "case";
wenzelm@18608
    62
val case_hypsN = "hyps";
wenzelm@18608
    63
val case_premsN = "prems";
wenzelm@18608
    64
wenzelm@20087
    65
val strip_params = map (apfst (perhaps (try Name.dest_skolem))) o Logic.strip_params;
wenzelm@18608
    66
wenzelm@18608
    67
local
wenzelm@18608
    68
wenzelm@18608
    69
fun abs xs t = Term.list_abs (xs, t);
wenzelm@18608
    70
fun app us t = Term.betapplys (t, us);
wenzelm@18608
    71
wenzelm@18608
    72
fun dest_binops cs tm =
wenzelm@18608
    73
  let
wenzelm@18608
    74
    val n = length cs;
wenzelm@18608
    75
    fun dest 0 _ = []
wenzelm@18608
    76
      | dest 1 t = [t]
wenzelm@18608
    77
      | dest k (_ $ t $ u) = t :: dest (k - 1) u
wenzelm@18608
    78
      | dest _ _ = raise TERM ("Expected " ^ string_of_int n ^ " binop arguments", [tm]);
wenzelm@18608
    79
  in cs ~~ dest n tm end;
wenzelm@18608
    80
wenzelm@18608
    81
fun extract_fixes NONE prop = (strip_params prop, [])
wenzelm@18608
    82
  | extract_fixes (SOME outline) prop =
wenzelm@19012
    83
      chop (length (Logic.strip_params outline)) (strip_params prop);
wenzelm@18608
    84
wenzelm@18608
    85
fun extract_assumes _ NONE prop = ([("", Logic.strip_assums_hyp prop)], [])
wenzelm@18608
    86
  | extract_assumes qual (SOME outline) prop =
wenzelm@18608
    87
      let val (hyps, prems) =
wenzelm@19012
    88
        chop (length (Logic.strip_assums_hyp outline)) (Logic.strip_assums_hyp prop)
wenzelm@18608
    89
      in ([(qual case_hypsN, hyps)], [(qual case_premsN, prems)]) end;
wenzelm@18608
    90
wenzelm@18608
    91
fun extract_case is_open thy (case_outline, raw_prop) name concls =
wenzelm@18608
    92
  let
wenzelm@21745
    93
    val rename = if is_open then I else (apfst (Name.internal o Name.clean));
wenzelm@18608
    94
wenzelm@18608
    95
    val props = Logic.dest_conjunctions (Drule.norm_hhf thy raw_prop);
wenzelm@18608
    96
    val len = length props;
wenzelm@18608
    97
    val nested = is_some case_outline andalso len > 1;
wenzelm@18608
    98
wenzelm@18608
    99
    fun extract prop =
wenzelm@18608
   100
      let
wenzelm@18608
   101
        val (fixes1, fixes2) = extract_fixes case_outline prop
wenzelm@18608
   102
          |> apfst (map rename);
wenzelm@18608
   103
        val abs_fixes = abs (fixes1 @ fixes2);
wenzelm@18608
   104
        fun abs_fixes1 t =
wenzelm@18608
   105
          if not nested then abs_fixes t
wenzelm@18608
   106
          else abs fixes1 (app (map (Term.dummy_pattern o #2) fixes2) (abs fixes2 t));
wenzelm@18608
   107
wenzelm@30364
   108
        val (assumes1, assumes2) = extract_assumes (Long_Name.qualify name) case_outline prop
wenzelm@19482
   109
          |> pairself (map (apsnd (maps Logic.dest_conjunctions)));
wenzelm@18608
   110
wenzelm@18608
   111
        val concl = ObjectLogic.drop_judgment thy (Logic.strip_assums_concl prop);
wenzelm@18608
   112
        val binds =
wenzelm@18608
   113
          (case_conclN, concl) :: dest_binops concls concl
wenzelm@18608
   114
          |> map (fn (x, t) => ((x, 0), SOME (abs_fixes t)));
wenzelm@18608
   115
      in
wenzelm@18608
   116
       ((fixes1, map (apsnd (map abs_fixes1)) assumes1),
wenzelm@18608
   117
        ((fixes2, map (apsnd (map abs_fixes)) assumes2), binds))
wenzelm@18608
   118
      end;
wenzelm@18608
   119
wenzelm@18608
   120
    val cases = map extract props;
wenzelm@18608
   121
wenzelm@18608
   122
    fun common_case ((fixes1, assumes1), ((fixes2, assumes2), binds)) =
wenzelm@18608
   123
      Case {fixes = fixes1 @ fixes2, assumes = assumes1 @ assumes2, binds = binds, cases = []};
wenzelm@18608
   124
    fun inner_case (_, ((fixes2, assumes2), binds)) =
wenzelm@18608
   125
      Case {fixes = fixes2, assumes = assumes2, binds = binds, cases = []};
wenzelm@18608
   126
    fun nested_case ((fixes1, assumes1), _) =
wenzelm@18608
   127
      Case {fixes = fixes1, assumes = assumes1, binds = [],
wenzelm@18608
   128
        cases = map string_of_int (1 upto len) ~~ map inner_case cases};
wenzelm@18608
   129
  in
wenzelm@18608
   130
    if len = 0 then NONE
wenzelm@18608
   131
    else if len = 1 then SOME (common_case (hd cases))
wenzelm@19046
   132
    else if is_none case_outline orelse length (distinct (op =) (map fst cases)) > 1 then NONE
wenzelm@18608
   133
    else SOME (nested_case (hd cases))
wenzelm@18608
   134
  end;
wenzelm@18608
   135
wenzelm@18608
   136
fun make is_open rule_struct (thy, prop) cases =
wenzelm@18608
   137
  let
wenzelm@18608
   138
    val n = length cases;
wenzelm@21576
   139
    val nprems = Logic.count_prems prop;
wenzelm@18608
   140
    fun add_case (name, concls) (cs, i) =
wenzelm@18608
   141
      ((case try (fn () =>
wenzelm@18608
   142
          (Option.map (curry Logic.nth_prem i) rule_struct, Logic.nth_prem (i, prop))) () of
wenzelm@18608
   143
        NONE => (name, NONE)
wenzelm@18608
   144
      | SOME p => (name, extract_case is_open thy p name concls)) :: cs, i - 1);
wenzelm@18608
   145
  in fold_rev add_case (Library.drop (n - nprems, cases)) ([], n) |> #1 end;
wenzelm@18608
   146
wenzelm@18608
   147
in
wenzelm@18608
   148
wenzelm@18608
   149
fun make_common is_open = make is_open NONE;
wenzelm@18608
   150
fun make_nested is_open rule_struct = make is_open (SOME rule_struct);
wenzelm@18608
   151
wenzelm@18608
   152
fun apply args =
wenzelm@18608
   153
  let
wenzelm@18608
   154
    fun appl (Case {fixes, assumes, binds, cases}) =
wenzelm@18608
   155
      let
wenzelm@18608
   156
        val assumes' = map (apsnd (map (app args))) assumes;
wenzelm@18608
   157
        val binds' = map (apsnd (Option.map (app args))) binds;
wenzelm@18608
   158
        val cases' = map (apsnd appl) cases;
wenzelm@18608
   159
      in Case {fixes = fixes, assumes = assumes', binds = binds', cases = cases'} end;
wenzelm@18608
   160
  in appl end;
wenzelm@18608
   161
wenzelm@18608
   162
end;
wenzelm@18608
   163
wenzelm@18608
   164
wenzelm@18608
   165
wenzelm@18229
   166
(** tactics with cases **)
wenzelm@18229
   167
wenzelm@18229
   168
type cases_tactic = thm -> (cases * thm) Seq.seq;
wenzelm@18229
   169
wenzelm@18229
   170
fun CASES cases tac st = Seq.map (pair cases) (tac st);
wenzelm@18229
   171
fun NO_CASES tac = CASES [] tac;
wenzelm@18229
   172
wenzelm@18229
   173
fun SUBGOAL_CASES tac i st =
wenzelm@18229
   174
  (case try Logic.nth_prem (i, Thm.prop_of st) of
wenzelm@18229
   175
    SOME goal => tac (goal, i) st
wenzelm@18229
   176
  | NONE => Seq.empty);
wenzelm@18229
   177
wenzelm@18229
   178
fun (tac1 THEN_ALL_NEW_CASES tac2) i st =
wenzelm@18229
   179
  st |> tac1 i |> Seq.maps (fn (cases, st') =>
wenzelm@18237
   180
    CASES cases (Seq.INTERVAL tac2 i (i + nprems_of st' - nprems_of st)) st');
wenzelm@18229
   181
wenzelm@18229
   182
wenzelm@18229
   183
wenzelm@18229
   184
(** consume facts **)
wenzelm@18229
   185
wenzelm@18477
   186
local
wenzelm@18477
   187
wenzelm@18477
   188
fun unfold_prems n defs th =
wenzelm@18477
   189
  if null defs then th
wenzelm@23584
   190
  else Conv.fconv_rule (Conv.prems_conv n (MetaSimplifier.rewrite true defs)) th;
wenzelm@18477
   191
wenzelm@18477
   192
fun unfold_prems_concls defs th =
wenzelm@18477
   193
  if null defs orelse not (can Logic.dest_conjunction (Thm.concl_of th)) then th
wenzelm@18477
   194
  else
wenzelm@22900
   195
    Conv.fconv_rule
wenzelm@23418
   196
      (Conv.concl_conv ~1 (Conjunction.convs
wenzelm@23584
   197
        (Conv.prems_conv ~1 (MetaSimplifier.rewrite true defs)))) th;
wenzelm@18477
   198
wenzelm@18477
   199
in
wenzelm@18477
   200
wenzelm@18477
   201
fun consume defs facts ((xx, n), th) =
wenzelm@18237
   202
  let val m = Int.min (length facts, n) in
wenzelm@18477
   203
    th
wenzelm@18477
   204
    |> unfold_prems n defs
wenzelm@18477
   205
    |> unfold_prems_concls defs
wenzelm@18237
   206
    |> Drule.multi_resolve (Library.take (m, facts))
wenzelm@18477
   207
    |> Seq.map (pair (xx, (n - m, Library.drop (m, facts))))
wenzelm@18237
   208
  end;
wenzelm@10811
   209
wenzelm@18477
   210
end;
wenzelm@18477
   211
wenzelm@10811
   212
val consumes_tagN = "consumes";
wenzelm@10811
   213
wenzelm@18229
   214
fun lookup_consumes th =
wenzelm@23657
   215
  (case AList.lookup (op =) (Thm.get_tags th) (consumes_tagN) of
wenzelm@23657
   216
    NONE => NONE
wenzelm@23657
   217
  | SOME s =>
wenzelm@24244
   218
      (case Lexicon.read_nat s of SOME n => SOME n
wenzelm@23657
   219
      | _ => raise THM ("Malformed 'consumes' tag of theorem", 0, [th])));
wenzelm@10530
   220
wenzelm@18477
   221
fun get_consumes th = the_default 0 (lookup_consumes th);
wenzelm@18477
   222
skalberg@15531
   223
fun put_consumes NONE th = th
skalberg@15531
   224
  | put_consumes (SOME n) th = th
wenzelm@27865
   225
      |> Thm.untag_rule consumes_tagN
wenzelm@27865
   226
      |> Thm.tag_rule
wenzelm@23657
   227
        (consumes_tagN, Library.string_of_int (if n < 0 then Thm.nprems_of th + n else n));
wenzelm@10530
   228
wenzelm@18477
   229
fun add_consumes k th = put_consumes (SOME (k + get_consumes th)) th;
wenzelm@18477
   230
wenzelm@12761
   231
val save_consumes = put_consumes o lookup_consumes;
wenzelm@12761
   232
wenzelm@18728
   233
fun consumes n x = Thm.rule_attribute (K (put_consumes (SOME n))) x;
wenzelm@21395
   234
wenzelm@16148
   235
fun consumes_default n x =
wenzelm@21395
   236
  if is_some (lookup_consumes (#2 x)) then x else consumes n x;
wenzelm@8364
   237
wenzelm@8364
   238
wenzelm@18229
   239
wenzelm@18229
   240
(** case names **)
wenzelm@18229
   241
wenzelm@23657
   242
val implode_args = space_implode ";";
wenzelm@23657
   243
val explode_args = space_explode ";";
wenzelm@23657
   244
wenzelm@18237
   245
val case_names_tagN = "case_names";
wenzelm@8364
   246
wenzelm@18229
   247
fun add_case_names NONE = I
wenzelm@18229
   248
  | add_case_names (SOME names) =
wenzelm@27865
   249
      Thm.untag_rule case_names_tagN
wenzelm@27865
   250
      #> Thm.tag_rule (case_names_tagN, implode_args names);
wenzelm@8364
   251
wenzelm@23657
   252
fun lookup_case_names th =
wenzelm@23657
   253
  AList.lookup (op =) (Thm.get_tags th) case_names_tagN
wenzelm@23657
   254
  |> Option.map explode_args;
wenzelm@10530
   255
wenzelm@18229
   256
val save_case_names = add_case_names o lookup_case_names;
wenzelm@18229
   257
val name = add_case_names o SOME;
wenzelm@18728
   258
fun case_names ss = Thm.rule_attribute (K (name ss));
wenzelm@8364
   259
wenzelm@10530
   260
wenzelm@18229
   261
wenzelm@18229
   262
(** case conclusions **)
wenzelm@18229
   263
wenzelm@18229
   264
val case_concl_tagN = "case_conclusion";
wenzelm@18229
   265
wenzelm@23657
   266
fun get_case_concl name (a, b) =
wenzelm@23657
   267
  if a = case_concl_tagN then
wenzelm@23657
   268
    (case explode_args b of c :: cs => if c = name then SOME cs else NONE)
wenzelm@23657
   269
  else NONE;
wenzelm@18229
   270
wenzelm@21646
   271
fun add_case_concl (name, cs) = Thm.map_tags (fn tags =>
wenzelm@23657
   272
  filter_out (is_some o get_case_concl name) tags @
wenzelm@23657
   273
    [(case_concl_tagN, implode_args (name :: cs))]);
wenzelm@18229
   274
wenzelm@18229
   275
fun get_case_concls th name =
wenzelm@23657
   276
  these (get_first (get_case_concl name) (Thm.get_tags th));
wenzelm@18229
   277
wenzelm@18229
   278
fun save_case_concls th =
wenzelm@21646
   279
  let val concls = Thm.get_tags th |> map_filter
wenzelm@23657
   280
    (fn (a, b) =>
wenzelm@23657
   281
      if a = case_concl_tagN then (case explode_args b of c :: cs => SOME (c, cs) | _ => NONE)
wenzelm@23657
   282
      else NONE)
wenzelm@18229
   283
  in fold add_case_concl concls end;
wenzelm@18229
   284
wenzelm@18728
   285
fun case_conclusion concl = Thm.rule_attribute (fn _ => add_case_concl concl);
wenzelm@18229
   286
wenzelm@18229
   287
wenzelm@18229
   288
wenzelm@18229
   289
(** case declarations **)
wenzelm@18229
   290
wenzelm@10530
   291
(* access hints *)
wenzelm@10530
   292
wenzelm@18229
   293
fun save th = save_consumes th #> save_case_names th #> save_case_concls th;
wenzelm@12761
   294
wenzelm@18229
   295
fun get th =
wenzelm@12761
   296
  let
wenzelm@18477
   297
    val n = get_consumes th;
wenzelm@18229
   298
    val cases =
wenzelm@18229
   299
      (case lookup_case_names th of
wenzelm@18229
   300
        NONE => map (rpair [] o Library.string_of_int) (1 upto (Thm.nprems_of th - n))
wenzelm@18229
   301
      | SOME names => map (fn name => (name, get_case_concls th name)) names);
wenzelm@18229
   302
  in (cases, n) end;
wenzelm@10530
   303
wenzelm@8364
   304
wenzelm@8427
   305
(* params *)
wenzelm@8364
   306
wenzelm@18229
   307
fun rename_params xss th =
wenzelm@18229
   308
  th
wenzelm@18229
   309
  |> fold_index (fn (i, xs) => Thm.rename_params_rule (xs, i + 1)) xss
wenzelm@18229
   310
  |> save th;
wenzelm@8364
   311
wenzelm@18728
   312
fun params xss = Thm.rule_attribute (K (rename_params xss));
wenzelm@8364
   313
wenzelm@18477
   314
wenzelm@18477
   315
wenzelm@18477
   316
(** mutual_rule **)
wenzelm@18477
   317
wenzelm@18477
   318
local
wenzelm@18477
   319
wenzelm@18477
   320
fun equal_cterms ts us =
wenzelm@29269
   321
  is_equal (list_ord (TermOrd.fast_term_ord o pairself Thm.term_of) (ts, us));
wenzelm@18477
   322
wenzelm@19917
   323
fun prep_rule n th =
wenzelm@18477
   324
  let
wenzelm@19917
   325
    val th' = Thm.permute_prems 0 n th;
wenzelm@18477
   326
    val prems = Library.take (Thm.nprems_of th' - n, Drule.cprems_of th');
wenzelm@18477
   327
    val th'' = Drule.implies_elim_list th' (map Thm.assume prems);
wenzelm@18477
   328
  in (prems, (n, th'')) end;
wenzelm@18477
   329
wenzelm@18477
   330
in
wenzelm@18477
   331
wenzelm@19917
   332
fun mutual_rule _ [] = NONE
wenzelm@19917
   333
  | mutual_rule _ [th] = SOME ([0], th)
wenzelm@19917
   334
  | mutual_rule ctxt (ths as th :: _) =
wenzelm@18477
   335
      let
wenzelm@31794
   336
        val ((_, ths'), ctxt') = Variable.import true ths ctxt;
wenzelm@19917
   337
        val rules as (prems, _) :: _ = map (prep_rule (get_consumes th)) ths';
wenzelm@19917
   338
        val (ns, rls) = split_list (map #2 rules);
wenzelm@18477
   339
      in
wenzelm@18477
   340
        if not (forall (equal_cterms prems o #1) rules) then NONE
wenzelm@18477
   341
        else
wenzelm@18477
   342
          SOME (ns,
wenzelm@19917
   343
            rls
wenzelm@23418
   344
            |> Conjunction.intr_balanced
wenzelm@18477
   345
            |> Drule.implies_intr_list prems
wenzelm@19917
   346
            |> singleton (Variable.export ctxt' ctxt)
wenzelm@19917
   347
            |> save th
wenzelm@18477
   348
            |> put_consumes (SOME 0))
wenzelm@18477
   349
      end;
wenzelm@18477
   350
wenzelm@18477
   351
end;
wenzelm@18477
   352
wenzelm@19917
   353
fun strict_mutual_rule ctxt ths =
wenzelm@19917
   354
  (case mutual_rule ctxt ths of
wenzelm@18581
   355
    NONE => error "Failed to join given rules into one mutual rule"
wenzelm@18581
   356
  | SOME res => res);
wenzelm@18581
   357
wenzelm@8364
   358
end;
wenzelm@17113
   359
wenzelm@18229
   360
structure BasicRuleCases: BASIC_RULE_CASES = RuleCases;
wenzelm@18229
   361
open BasicRuleCases;