src/Pure/Isar/obtain.ML
author wenzelm
Wed Aug 02 22:27:02 2006 +0200 (2006-08-02)
changeset 20308 ddb7e7129481
parent 20224 9c40a144ee0e
child 20804 0e2591606867
permissions -rw-r--r--
added tactical result;
simplified obtain_export: no Seq.seq, no lifting of result prems;
tuned;
wenzelm@7674
     1
(*  Title:      Pure/Isar/obtain.ML
wenzelm@7674
     2
    ID:         $Id$
wenzelm@7674
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@7674
     4
wenzelm@17858
     5
The 'obtain' and 'guess' language elements -- generalized existence at
wenzelm@17858
     6
the level of proof texts: 'obtain' involves a proof that certain
wenzelm@17858
     7
fixes/assumes may be introduced into the present context; 'guess' is
wenzelm@17858
     8
similar, but derives these elements from the course of reasoning!
wenzelm@7674
     9
wenzelm@9468
    10
  <chain_facts>
wenzelm@18870
    11
  obtain x where "A x" <proof> ==
wenzelm@7674
    12
wenzelm@18870
    13
  have "!!thesis. (!!x. A x ==> thesis) ==> thesis"
wenzelm@12970
    14
  proof succeed
wenzelm@9468
    15
    fix thesis
wenzelm@18870
    16
    assume that [intro?]: "!!x. A x ==> thesis"
wenzelm@18870
    17
    <chain_facts>
wenzelm@18870
    18
    show thesis
wenzelm@18870
    19
      apply (insert that)
wenzelm@18870
    20
      <proof>
wenzelm@12970
    21
  qed
wenzelm@18870
    22
  fix x assm <<obtain_export>> "A x"
wenzelm@17858
    23
wenzelm@17858
    24
wenzelm@17858
    25
  <chain_facts>
wenzelm@17858
    26
  guess x <proof body> <proof end> ==
wenzelm@17858
    27
wenzelm@17858
    28
  {
wenzelm@17858
    29
    fix thesis
wenzelm@17858
    30
    <chain_facts> have "PROP ?guess"
wenzelm@18870
    31
      apply magic      -- {* turns goal into "thesis ==> #thesis" *}
wenzelm@17858
    32
      <proof body>
wenzelm@18870
    33
      apply_end magic  -- {* turns final "(!!x. P x ==> thesis) ==> #thesis" into
wenzelm@18870
    34
        "#((!!x. A x ==> thesis) ==> thesis)" which is a finished goal state *}
wenzelm@17858
    35
      <proof end>
wenzelm@17858
    36
  }
wenzelm@18870
    37
  fix x assm <<obtain_export>> "A x"
wenzelm@8094
    38
*)
wenzelm@7674
    39
wenzelm@7674
    40
signature OBTAIN =
wenzelm@7674
    41
sig
wenzelm@19844
    42
  val obtain: string -> (string * string option * mixfix) list ->
wenzelm@20308
    43
    ((string * Attrib.src list) * (string * string list) list) list ->
wenzelm@20308
    44
    bool -> Proof.state -> Proof.state
wenzelm@19844
    45
  val obtain_i: string -> (string * typ option * mixfix) list ->
wenzelm@20308
    46
    ((string * attribute list) * (term * term list) list) list ->
wenzelm@20308
    47
    bool -> Proof.state -> Proof.state
wenzelm@20308
    48
  val result: (Proof.context -> tactic) -> thm list -> Proof.context ->
wenzelm@20308
    49
    (cterm list * thm list) * Proof.context
wenzelm@19844
    50
  val guess: (string * string option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@19844
    51
  val guess_i: (string * typ option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@18897
    52
  val statement: (string * ((string * 'typ option) list * 'term list)) list ->
wenzelm@18897
    53
    (('typ, 'term, 'fact) Element.ctxt list *
wenzelm@19585
    54
      ((string * Attrib.src list) * ('term * 'term list) list) list) *
wenzelm@19585
    55
    (((string * Attrib.src list) * (term * term list) list) list -> Proof.context ->
wenzelm@19585
    56
      (((string * Attrib.src list) * (term * term list) list) list * thm list) * Proof.context)
wenzelm@7674
    57
end;
wenzelm@7674
    58
wenzelm@10379
    59
structure Obtain: OBTAIN =
wenzelm@7674
    60
struct
wenzelm@7674
    61
wenzelm@18670
    62
(** obtain_export **)
wenzelm@18670
    63
wenzelm@18870
    64
(*
wenzelm@18897
    65
  [x, A x]
wenzelm@18897
    66
     :
wenzelm@18897
    67
     B
wenzelm@18897
    68
  --------
wenzelm@18897
    69
     B
wenzelm@18870
    70
*)
wenzelm@20308
    71
fun obtain_export fix_ctxt rule xs _ As thm =
wenzelm@9468
    72
  let
wenzelm@20308
    73
    val thy = ProofContext.theory_of fix_ctxt;
wenzelm@9468
    74
wenzelm@20308
    75
    val vs = map (dest_Free o Thm.term_of) xs;
wenzelm@20308
    76
    val bads = Term.fold_aterms (fn t as Free v =>
wenzelm@20308
    77
      if member (op =) vs v then insert (op aconv) t else I | _ => I) (Thm.prop_of thm) [];
wenzelm@20308
    78
    val _ = null bads orelse
wenzelm@20308
    79
      error ("Result contains obtained parameters: " ^
wenzelm@20308
    80
        space_implode " " (map (ProofContext.string_of_term fix_ctxt) bads));
wenzelm@20308
    81
    val _ = ObjectLogic.is_judgment thy (Thm.concl_of thm) orelse
wenzelm@20308
    82
      error "Conclusion in obtained context must be object-logic judgment";
wenzelm@20308
    83
wenzelm@20308
    84
    val ((_, [thm']), ctxt') = Variable.import true [thm] fix_ctxt;
wenzelm@20308
    85
    val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm'));
wenzelm@9468
    86
  in
wenzelm@20308
    87
    ((Drule.implies_elim_list thm' (map Thm.assume prems)
wenzelm@20308
    88
        |> Drule.implies_intr_list (map Drule.norm_hhf_cterm As)
wenzelm@20308
    89
        |> Drule.forall_intr_list xs)
wenzelm@20308
    90
      COMP rule)
wenzelm@20308
    91
    |> Drule.implies_intr_list prems
wenzelm@20308
    92
    |> singleton (Variable.export ctxt' fix_ctxt)
wenzelm@9468
    93
  end;
wenzelm@9468
    94
wenzelm@9468
    95
wenzelm@9468
    96
wenzelm@17858
    97
(** obtain **)
wenzelm@17858
    98
wenzelm@17858
    99
fun bind_judgment ctxt name =
wenzelm@18670
   100
  let
wenzelm@20308
   101
    val (bind, ctxt') = ProofContext.bind_fixes [name] ctxt;
wenzelm@18670
   102
    val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name);
wenzelm@20308
   103
  in ((v, t), ctxt') end;
wenzelm@17858
   104
wenzelm@18897
   105
val thatN = "that";
wenzelm@18897
   106
wenzelm@17858
   107
local
wenzelm@8094
   108
wenzelm@18897
   109
fun gen_obtain prep_att prep_vars prep_propp
wenzelm@18897
   110
    name raw_vars raw_asms int state =
wenzelm@7674
   111
  let
wenzelm@9468
   112
    val _ = Proof.assert_forward_or_chain state;
wenzelm@20308
   113
    val thy = Proof.theory_of state;
wenzelm@20308
   114
    val cert = Thm.cterm_of thy;
wenzelm@17858
   115
    val ctxt = Proof.context_of state;
wenzelm@17357
   116
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@7674
   117
wenzelm@8543
   118
    (*obtain vars*)
wenzelm@19844
   119
    val (vars, vars_ctxt) = prep_vars raw_vars ctxt;
wenzelm@18670
   120
    val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
   121
    val xs = map #1 vars;
wenzelm@7674
   122
wenzelm@8543
   123
    (*obtain asms*)
wenzelm@11890
   124
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
wenzelm@19482
   125
    val asm_props = maps (map fst) proppss;
wenzelm@18678
   126
    val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
wenzelm@10464
   127
wenzelm@19897
   128
    val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt;
wenzelm@7674
   129
wenzelm@12970
   130
    (*obtain statements*)
wenzelm@20085
   131
    val thesisN = Name.variant xs AutoBind.thesisN;
wenzelm@20308
   132
    val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN);
wenzelm@9468
   133
wenzelm@20308
   134
    val asm_frees = fold Term.add_frees asm_props [];
wenzelm@20308
   135
    val parms = xs |> map (fn x =>
wenzelm@20308
   136
      let val x' = ProofContext.get_skolem fix_ctxt x
wenzelm@20308
   137
      in (x', the_default propT (AList.lookup (op =) asm_frees x')) end);
wenzelm@10582
   138
wenzelm@18897
   139
    val that_name = if name = "" then thatN else name;
wenzelm@10582
   140
    val that_prop =
wenzelm@20308
   141
      Term.list_all_free (parms, Logic.list_implies (asm_props, thesis))
wenzelm@20308
   142
      |> Library.curry Logic.list_rename_params xs;
wenzelm@12970
   143
    val obtain_prop =
wenzelm@12970
   144
      Logic.list_rename_params ([AutoBind.thesisN],
wenzelm@17858
   145
        Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
wenzelm@7674
   146
wenzelm@18124
   147
    fun after_qed _ =
wenzelm@17357
   148
      Proof.local_qed (NONE, false)
wenzelm@17858
   149
      #> Seq.map (`Proof.the_fact #-> (fn rule =>
wenzelm@19844
   150
        Proof.fix_i (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars)
wenzelm@20308
   151
        #> Proof.assm_i (obtain_export fix_ctxt rule (map (cert o Free) parms)) asms));
wenzelm@7674
   152
  in
wenzelm@8094
   153
    state
wenzelm@9468
   154
    |> Proof.enter_forward
wenzelm@19585
   155
    |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, [])])] int
wenzelm@17858
   156
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
wenzelm@19844
   157
    |> Proof.fix_i [(thesisN, NONE, NoSyn)]
wenzelm@19585
   158
    |> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])]
wenzelm@16842
   159
    |> `Proof.the_facts
wenzelm@17357
   160
    ||> Proof.chain_facts chain_facts
wenzelm@19585
   161
    ||> Proof.show_i NONE after_qed [(("", []), [(thesis, [])])] false
wenzelm@18907
   162
    |-> Proof.refine_insert
wenzelm@7674
   163
  end;
wenzelm@7674
   164
wenzelm@17858
   165
in
wenzelm@17858
   166
wenzelm@18728
   167
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp;
wenzelm@17111
   168
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
wenzelm@8094
   169
wenzelm@8094
   170
end;
wenzelm@17858
   171
wenzelm@17858
   172
wenzelm@17858
   173
wenzelm@20308
   174
(** tactical result **)
wenzelm@20308
   175
wenzelm@20308
   176
fun check_result ctxt thesis th =
wenzelm@20308
   177
  (case Thm.prems_of th of
wenzelm@20308
   178
    [prem] =>
wenzelm@20308
   179
      if Thm.concl_of th aconv thesis andalso
wenzelm@20308
   180
        Logic.strip_assums_concl prem aconv thesis then th
wenzelm@20308
   181
      else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
wenzelm@20308
   182
  | [] => error "Goal solved -- nothing guessed."
wenzelm@20308
   183
  | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
wenzelm@20308
   184
wenzelm@20308
   185
fun result tac facts ctxt =
wenzelm@20308
   186
  let
wenzelm@20308
   187
    val thy = ProofContext.theory_of ctxt;
wenzelm@20308
   188
    val cert = Thm.cterm_of thy;
wenzelm@20308
   189
wenzelm@20308
   190
    val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt AutoBind.thesisN;
wenzelm@20308
   191
    val rule =
wenzelm@20308
   192
      (case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of
wenzelm@20308
   193
        NONE => raise THM ("Obtain.result: tactic failed", 0, facts)
wenzelm@20308
   194
      | SOME th => check_result ctxt thesis (norm_hhf (Goal.conclude th)));
wenzelm@20308
   195
wenzelm@20308
   196
    val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule;
wenzelm@20308
   197
    val ((_, [rule']), ctxt') = Variable.import false [closed_rule] ctxt;
wenzelm@20308
   198
    val obtain_rule = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
wenzelm@20308
   199
    val ((params, stmt), fix_ctxt) = Variable.focus (Thm.cprem_of obtain_rule 1) ctxt';
wenzelm@20308
   200
    val (prems, ctxt'') =
wenzelm@20308
   201
      Assumption.add_assms (obtain_export fix_ctxt obtain_rule params)
wenzelm@20308
   202
        (Drule.strip_imp_prems stmt) fix_ctxt;
wenzelm@20308
   203
  in ((params, prems), ctxt'') end;
wenzelm@20308
   204
wenzelm@20308
   205
wenzelm@20308
   206
wenzelm@17858
   207
(** guess **)
wenzelm@17858
   208
wenzelm@17858
   209
local
wenzelm@17858
   210
wenzelm@20308
   211
fun unify_params vars thesis_var raw_rule ctxt =
wenzelm@17858
   212
  let
wenzelm@18678
   213
    val thy = ProofContext.theory_of ctxt;
wenzelm@19978
   214
    val certT = Thm.ctyp_of thy;
wenzelm@19978
   215
    val cert = Thm.cterm_of thy;
wenzelm@17891
   216
    val string_of_typ = ProofContext.string_of_typ ctxt;
wenzelm@17858
   217
    val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
wenzelm@17891
   218
wenzelm@18678
   219
    fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
wenzelm@17858
   220
wenzelm@19978
   221
    val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1;
wenzelm@19779
   222
    val rule = Thm.incr_indexes (maxidx + 1) raw_rule;
wenzelm@19779
   223
wenzelm@17858
   224
    val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
wenzelm@17858
   225
    val m = length vars;
wenzelm@17858
   226
    val n = length params;
wenzelm@19779
   227
    val _ = m <= n orelse err "More variables than parameters in obtained rule" rule;
wenzelm@17858
   228
wenzelm@19779
   229
    fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max)
wenzelm@19779
   230
      handle Type.TUNIFY =>
wenzelm@19779
   231
        err ("Failed to unify variable " ^
wenzelm@19779
   232
          string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^
wenzelm@19779
   233
          string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule;
wenzelm@19978
   234
    val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params))
wenzelm@19779
   235
      (Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule));
wenzelm@17858
   236
    val norm_type = Envir.norm_type tyenv;
wenzelm@17858
   237
wenzelm@19978
   238
    val xs = map (apsnd norm_type o fst) vars;
wenzelm@19779
   239
    val ys = map (apsnd norm_type) (Library.drop (m, params));
wenzelm@20085
   240
    val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys;
wenzelm@19978
   241
    val terms = map (Drule.mk_term o cert o Free) (xs @ ys');
wenzelm@19779
   242
wenzelm@17858
   243
    val instT =
wenzelm@17858
   244
      fold (Term.add_tvarsT o #2) params []
wenzelm@19978
   245
      |> map (TVar #> (fn T => (certT T, certT (norm_type T))));
wenzelm@20308
   246
    val closed_rule = rule
wenzelm@20308
   247
      |> Thm.forall_intr (cert (Free thesis_var))
wenzelm@20308
   248
      |> Thm.instantiate (instT, []);
wenzelm@17891
   249
wenzelm@20308
   250
    val ((_, rule' :: terms'), ctxt') = Variable.import false (closed_rule :: terms) ctxt;
wenzelm@19978
   251
    val vars' =
wenzelm@19978
   252
      map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~
wenzelm@19978
   253
      (map snd vars @ replicate (length ys) NoSyn);
wenzelm@20308
   254
    val rule'' = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
wenzelm@19978
   255
  in ((vars', rule''), ctxt') end;
wenzelm@17858
   256
wenzelm@18693
   257
fun inferred_type (x, _, mx) ctxt =
wenzelm@18769
   258
  let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt
wenzelm@19779
   259
  in ((x, T, mx), ctxt') end;
wenzelm@19779
   260
wenzelm@20004
   261
fun polymorphic ctxt vars =
wenzelm@19897
   262
  let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars))
wenzelm@19779
   263
  in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end;
wenzelm@18693
   264
wenzelm@17858
   265
fun gen_guess prep_vars raw_vars int state =
wenzelm@17858
   266
  let
wenzelm@17858
   267
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   268
    val thy = Proof.theory_of state;
wenzelm@20308
   269
    val cert = Thm.cterm_of thy;
wenzelm@17858
   270
    val ctxt = Proof.context_of state;
wenzelm@17858
   271
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@17858
   272
wenzelm@20308
   273
    val (thesis_var, thesis) = #1 (bind_judgment ctxt AutoBind.thesisN);
wenzelm@20004
   274
    val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt;
wenzelm@17858
   275
wenzelm@19978
   276
    fun guess_context raw_rule state' =
wenzelm@17858
   277
      let
wenzelm@19978
   278
        val ((parms, rule), ctxt') =
wenzelm@20308
   279
          unify_params vars thesis_var raw_rule (Proof.context_of state');
wenzelm@19978
   280
        val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt';
wenzelm@19978
   281
        val ts = map (bind o Free o #1) parms;
wenzelm@17858
   282
        val ps = map dest_Free ts;
wenzelm@17858
   283
        val asms =
wenzelm@17858
   284
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
wenzelm@19585
   285
          |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), []));
wenzelm@19779
   286
        val _ = not (null asms) orelse error "Trivial result -- nothing guessed";
wenzelm@17858
   287
      in
wenzelm@19978
   288
        state'
wenzelm@19978
   289
        |> Proof.map_context (K ctxt')
wenzelm@19978
   290
        |> Proof.fix_i (map (fn ((x, T), mx) => (x, SOME T, mx)) parms)
wenzelm@20308
   291
        |> `Proof.context_of |-> (fn fix_ctxt =>
wenzelm@20308
   292
            Proof.assm_i (obtain_export fix_ctxt rule (map cert ts)) [(("", []), asms)])
wenzelm@19978
   293
        |> Proof.add_binds_i AutoBind.no_facts
wenzelm@17858
   294
      end;
wenzelm@17858
   295
wenzelm@19779
   296
    val goal = Var (("guess", 0), propT);
wenzelm@19779
   297
    fun print_result ctxt' (k, [(s, [_, th])]) =
wenzelm@19779
   298
      ProofDisplay.print_results int ctxt' (k, [(s, [th])]);
wenzelm@20308
   299
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> norm_hhf #>
wenzelm@20308
   300
        (fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th))));
wenzelm@19779
   301
    fun after_qed [[_, res]] =
wenzelm@20308
   302
      Proof.end_block #> guess_context (check_result ctxt thesis res) #> Seq.single;
wenzelm@17858
   303
  in
wenzelm@17858
   304
    state
wenzelm@17858
   305
    |> Proof.enter_forward
wenzelm@17858
   306
    |> Proof.begin_block
wenzelm@19844
   307
    |> Proof.fix_i [(AutoBind.thesisN, NONE, NoSyn)]
wenzelm@17858
   308
    |> Proof.chain_facts chain_facts
wenzelm@19779
   309
    |> Proof.local_goal print_result (K I) (apsnd (rpair I))
wenzelm@19779
   310
      "guess" before_qed after_qed [(("", []), [Logic.mk_term goal, goal])]
wenzelm@20308
   311
    |> Proof.refine (Method.primitive_text (K (Goal.init (cert thesis)))) |> Seq.hd
wenzelm@17858
   312
  end;
wenzelm@17858
   313
wenzelm@17858
   314
in
wenzelm@17858
   315
wenzelm@17858
   316
val guess = gen_guess ProofContext.read_vars;
wenzelm@17858
   317
val guess_i = gen_guess ProofContext.cert_vars;
wenzelm@17858
   318
wenzelm@17858
   319
end;
wenzelm@17858
   320
wenzelm@18897
   321
wenzelm@18897
   322
wenzelm@18897
   323
(** statements with several cases **)
wenzelm@18897
   324
wenzelm@18897
   325
fun statement cases =
wenzelm@18897
   326
  let
wenzelm@18907
   327
    val names =
wenzelm@18907
   328
      cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@18897
   329
    val elems = cases |> map (fn (_, (vars, _)) =>
wenzelm@19482
   330
      Element.Constrains (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE)));
wenzelm@19585
   331
    val concl = cases |> map (fn (_, (_, props)) => (("", []), map (rpair []) props));
wenzelm@18897
   332
wenzelm@18897
   333
    fun mk_stmt stmt ctxt =
wenzelm@18897
   334
      let
wenzelm@18897
   335
        val thesis =
wenzelm@18897
   336
          ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@18907
   337
        val atts = map Attrib.internal
wenzelm@18907
   338
          [RuleCases.consumes (~ (length cases)), RuleCases.case_names names];
wenzelm@18907
   339
wenzelm@18897
   340
        fun assume_case ((name, (vars, _)), (_, propp)) ctxt' =
wenzelm@18897
   341
          let
wenzelm@18897
   342
            val xs = map fst vars;
wenzelm@18897
   343
            val props = map fst propp;
wenzelm@18897
   344
            val (parms, ctxt'') =
wenzelm@18897
   345
              ctxt'
wenzelm@19897
   346
              |> fold Variable.declare_term props
wenzelm@18897
   347
              |> fold_map ProofContext.inferred_param xs;
wenzelm@18897
   348
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@18897
   349
          in
wenzelm@18897
   350
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@20224
   351
            ctxt' |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@19585
   352
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@18897
   353
            |>> (fn [(_, [th])] => th)
wenzelm@18897
   354
          end;
wenzelm@18897
   355
        val (ths, ctxt') = ctxt
wenzelm@18897
   356
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@18897
   357
          |> fold_map assume_case (cases ~~ stmt)
wenzelm@18897
   358
          |-> (fn ths => ProofContext.note_thmss_i [((thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@19585
   359
      in (([(("", atts), [(thesis, [])])], ths), ctxt') end;
wenzelm@18897
   360
  in ((elems, concl), mk_stmt) end;
wenzelm@18897
   361
wenzelm@17858
   362
end;