src/Pure/Isar/obtain.ML
author wenzelm
Tue Jul 11 12:17:11 2006 +0200 (2006-07-11)
changeset 20085 c5d60752587f
parent 20004 e6d3f2b031e6
child 20201 24b49cbd438b
permissions -rw-r--r--
replaced Term.variant(list) by Name.variant(_list);
Name.internal;
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@19585
    43
    ((string * Attrib.src list) * (string * string list) list) list
wenzelm@17357
    44
    -> bool -> Proof.state -> Proof.state
wenzelm@19844
    45
  val obtain_i: string -> (string * typ option * mixfix) list ->
wenzelm@19585
    46
    ((string * attribute list) * (term * term list) list) list
wenzelm@17357
    47
    -> bool -> Proof.state -> Proof.state
wenzelm@19844
    48
  val guess: (string * string option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@19844
    49
  val guess_i: (string * typ option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@18897
    50
  val statement: (string * ((string * 'typ option) list * 'term list)) list ->
wenzelm@18897
    51
    (('typ, 'term, 'fact) Element.ctxt list *
wenzelm@19585
    52
      ((string * Attrib.src list) * ('term * 'term list) list) list) *
wenzelm@19585
    53
    (((string * Attrib.src list) * (term * term list) list) list -> Proof.context ->
wenzelm@19585
    54
      (((string * Attrib.src list) * (term * term list) list) list * thm list) * Proof.context)
wenzelm@7674
    55
end;
wenzelm@7674
    56
wenzelm@10379
    57
structure Obtain: OBTAIN =
wenzelm@7674
    58
struct
wenzelm@7674
    59
wenzelm@8094
    60
wenzelm@18670
    61
(** obtain_export **)
wenzelm@18670
    62
wenzelm@18870
    63
(*
wenzelm@18897
    64
  [x, A x]
wenzelm@18897
    65
     :
wenzelm@18897
    66
     B
wenzelm@18897
    67
  --------
wenzelm@18897
    68
     B
wenzelm@18870
    69
*)
wenzelm@18678
    70
fun obtain_export ctxt parms rule cprops thm =
wenzelm@9468
    71
  let
wenzelm@19978
    72
    val {thy, prop, ...} = Thm.rep_thm thm;
wenzelm@19978
    73
    val concl = Logic.strip_assums_concl prop;
wenzelm@19978
    74
    val bads = Term.fold_aterms (fn v as Free (x, _) =>
wenzelm@19978
    75
      if member (op =) parms x then insert (op aconv) v else I | _ => I) concl [];
wenzelm@9468
    76
wenzelm@20004
    77
    val thm' = thm |> Drule.implies_intr_protected cprops |> Drule.generalize ([], parms);
wenzelm@18040
    78
    val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI);
wenzelm@9468
    79
  in
wenzelm@9468
    80
    if not (null bads) then
wenzelm@18678
    81
      error ("Conclusion contains obtained parameters: " ^
wenzelm@18678
    82
        space_implode " " (map (ProofContext.string_of_term ctxt) bads))
wenzelm@17858
    83
    else if not (ObjectLogic.is_judgment thy concl) then
wenzelm@19978
    84
      error "Conclusion in obtained context must be object-logic judgment"
wenzelm@20004
    85
    else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule
wenzelm@9468
    86
  end;
wenzelm@9468
    87
wenzelm@9468
    88
wenzelm@9468
    89
wenzelm@17858
    90
(** obtain **)
wenzelm@17858
    91
wenzelm@17858
    92
fun bind_judgment ctxt name =
wenzelm@18670
    93
  let
wenzelm@18670
    94
    val (bind, _) = ProofContext.bind_fixes [name] ctxt;
wenzelm@18670
    95
    val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name);
wenzelm@17858
    96
  in (v, t) end;
wenzelm@17858
    97
wenzelm@18897
    98
val thatN = "that";
wenzelm@18897
    99
wenzelm@17858
   100
local
wenzelm@8094
   101
wenzelm@18897
   102
fun gen_obtain prep_att prep_vars prep_propp
wenzelm@18897
   103
    name raw_vars raw_asms int state =
wenzelm@7674
   104
  let
wenzelm@9468
   105
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   106
    val ctxt = Proof.context_of state;
wenzelm@18678
   107
    val thy = Proof.theory_of state;
wenzelm@17357
   108
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@7674
   109
wenzelm@8543
   110
    (*obtain vars*)
wenzelm@19844
   111
    val (vars, vars_ctxt) = prep_vars raw_vars ctxt;
wenzelm@18670
   112
    val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
   113
    val xs = map #1 vars;
wenzelm@7674
   114
wenzelm@8543
   115
    (*obtain asms*)
wenzelm@11890
   116
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
wenzelm@19482
   117
    val asm_props = maps (map fst) proppss;
wenzelm@18678
   118
    val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
wenzelm@10464
   119
wenzelm@19897
   120
    val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt;
wenzelm@7674
   121
wenzelm@12970
   122
    (*obtain statements*)
wenzelm@20085
   123
    val thesisN = Name.variant xs AutoBind.thesisN;
wenzelm@17858
   124
    val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN;
wenzelm@9468
   125
wenzelm@10582
   126
    fun occs_var x = Library.get_first (fn t =>
wenzelm@18151
   127
      Term.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
wenzelm@19978
   128
    val parms =
wenzelm@19978
   129
      map_filter (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (map occs_var xs ~~ xs);
wenzelm@10582
   130
wenzelm@18897
   131
    val that_name = if name = "" then thatN else name;
wenzelm@10582
   132
    val that_prop =
wenzelm@19978
   133
      Term.list_all_free (map #1 parms, Logic.list_implies (asm_props, thesis))
wenzelm@19978
   134
      |> Library.curry Logic.list_rename_params (map #2 parms);
wenzelm@12970
   135
    val obtain_prop =
wenzelm@12970
   136
      Logic.list_rename_params ([AutoBind.thesisN],
wenzelm@17858
   137
        Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
wenzelm@7674
   138
wenzelm@18124
   139
    fun after_qed _ =
wenzelm@17357
   140
      Proof.local_qed (NONE, false)
wenzelm@17858
   141
      #> Seq.map (`Proof.the_fact #-> (fn rule =>
wenzelm@19844
   142
        Proof.fix_i (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars)
wenzelm@19978
   143
        #> Proof.assm_i (K (obtain_export ctxt (map (#1 o #1) parms) rule)) asms));
wenzelm@7674
   144
  in
wenzelm@8094
   145
    state
wenzelm@9468
   146
    |> Proof.enter_forward
wenzelm@19585
   147
    |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, [])])] int
wenzelm@17858
   148
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
wenzelm@19844
   149
    |> Proof.fix_i [(thesisN, NONE, NoSyn)]
wenzelm@19585
   150
    |> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])]
wenzelm@16842
   151
    |> `Proof.the_facts
wenzelm@17357
   152
    ||> Proof.chain_facts chain_facts
wenzelm@19585
   153
    ||> Proof.show_i NONE after_qed [(("", []), [(thesis, [])])] false
wenzelm@18907
   154
    |-> Proof.refine_insert
wenzelm@7674
   155
  end;
wenzelm@7674
   156
wenzelm@17858
   157
in
wenzelm@17858
   158
wenzelm@18728
   159
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp;
wenzelm@17111
   160
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
wenzelm@8094
   161
wenzelm@8094
   162
end;
wenzelm@17858
   163
wenzelm@17858
   164
wenzelm@17858
   165
wenzelm@17858
   166
(** guess **)
wenzelm@17858
   167
wenzelm@17858
   168
local
wenzelm@17858
   169
wenzelm@19978
   170
fun unify_params vars thesis_name raw_rule ctxt =
wenzelm@17858
   171
  let
wenzelm@18678
   172
    val thy = ProofContext.theory_of ctxt;
wenzelm@19978
   173
    val certT = Thm.ctyp_of thy;
wenzelm@19978
   174
    val cert = Thm.cterm_of thy;
wenzelm@17891
   175
    val string_of_typ = ProofContext.string_of_typ ctxt;
wenzelm@17858
   176
    val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
wenzelm@17891
   177
wenzelm@18678
   178
    fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
wenzelm@17858
   179
wenzelm@19978
   180
    val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1;
wenzelm@19779
   181
    val rule = Thm.incr_indexes (maxidx + 1) raw_rule;
wenzelm@19779
   182
wenzelm@17858
   183
    val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
wenzelm@17858
   184
    val m = length vars;
wenzelm@17858
   185
    val n = length params;
wenzelm@19779
   186
    val _ = m <= n orelse err "More variables than parameters in obtained rule" rule;
wenzelm@17858
   187
wenzelm@19779
   188
    fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max)
wenzelm@19779
   189
      handle Type.TUNIFY =>
wenzelm@19779
   190
        err ("Failed to unify variable " ^
wenzelm@19779
   191
          string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^
wenzelm@19779
   192
          string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule;
wenzelm@19978
   193
    val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params))
wenzelm@19779
   194
      (Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule));
wenzelm@17858
   195
    val norm_type = Envir.norm_type tyenv;
wenzelm@17858
   196
wenzelm@19978
   197
    val xs = map (apsnd norm_type o fst) vars;
wenzelm@19779
   198
    val ys = map (apsnd norm_type) (Library.drop (m, params));
wenzelm@20085
   199
    val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys;
wenzelm@19978
   200
    val terms = map (Drule.mk_term o cert o Free) (xs @ ys');
wenzelm@19779
   201
wenzelm@17858
   202
    val instT =
wenzelm@17858
   203
      fold (Term.add_tvarsT o #2) params []
wenzelm@19978
   204
      |> map (TVar #> (fn T => (certT T, certT (norm_type T))));
wenzelm@19978
   205
    val (rule' :: terms', ctxt') =
wenzelm@19978
   206
      Variable.import false (Thm.instantiate (instT, []) rule :: terms) ctxt;
wenzelm@17891
   207
wenzelm@19978
   208
    val vars' =
wenzelm@19978
   209
      map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~
wenzelm@19978
   210
      (map snd vars @ replicate (length ys) NoSyn);
wenzelm@20004
   211
    val rule'' = Drule.generalize ([], [thesis_name]) rule';
wenzelm@19978
   212
  in ((vars', rule''), ctxt') end;
wenzelm@17858
   213
wenzelm@18693
   214
fun inferred_type (x, _, mx) ctxt =
wenzelm@18769
   215
  let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt
wenzelm@19779
   216
  in ((x, T, mx), ctxt') end;
wenzelm@19779
   217
wenzelm@20004
   218
fun polymorphic ctxt vars =
wenzelm@19897
   219
  let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars))
wenzelm@19779
   220
  in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end;
wenzelm@18693
   221
wenzelm@17858
   222
fun gen_guess prep_vars raw_vars int state =
wenzelm@17858
   223
  let
wenzelm@17858
   224
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   225
    val thy = Proof.theory_of state;
wenzelm@17858
   226
    val ctxt = Proof.context_of state;
wenzelm@17858
   227
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@17858
   228
wenzelm@19978
   229
    val ((thesis_name, _), thesis) = bind_judgment ctxt AutoBind.thesisN;
wenzelm@20004
   230
    val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt;
wenzelm@17858
   231
wenzelm@17974
   232
    fun check_result th =
wenzelm@17974
   233
      (case Thm.prems_of th of
wenzelm@17974
   234
        [prem] =>
wenzelm@17974
   235
          if Thm.concl_of th aconv thesis andalso
wenzelm@17974
   236
            Logic.strip_assums_concl prem aconv thesis then ()
wenzelm@18678
   237
          else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
wenzelm@18678
   238
      | [] => error "Goal solved -- nothing guessed."
wenzelm@18678
   239
      | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
wenzelm@17891
   240
wenzelm@19978
   241
    fun guess_context raw_rule state' =
wenzelm@17858
   242
      let
wenzelm@19978
   243
        val ((parms, rule), ctxt') =
wenzelm@19978
   244
          unify_params vars thesis_name raw_rule (Proof.context_of state');
wenzelm@19978
   245
        val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt';
wenzelm@19978
   246
        val ts = map (bind o Free o #1) parms;
wenzelm@17858
   247
        val ps = map dest_Free ts;
wenzelm@17858
   248
        val asms =
wenzelm@17858
   249
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
wenzelm@19585
   250
          |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), []));
wenzelm@19779
   251
        val _ = not (null asms) orelse error "Trivial result -- nothing guessed";
wenzelm@17858
   252
      in
wenzelm@19978
   253
        state'
wenzelm@19978
   254
        |> Proof.map_context (K ctxt')
wenzelm@19978
   255
        |> Proof.fix_i (map (fn ((x, T), mx) => (x, SOME T, mx)) parms)
wenzelm@19978
   256
        |> Proof.assm_i (K (obtain_export ctxt' (map #1 ps) rule)) [(("", []), asms)]
wenzelm@19978
   257
        |> Proof.add_binds_i AutoBind.no_facts
wenzelm@17858
   258
      end;
wenzelm@17858
   259
wenzelm@19779
   260
    val goal = Var (("guess", 0), propT);
wenzelm@19779
   261
    fun print_result ctxt' (k, [(s, [_, th])]) =
wenzelm@19779
   262
      ProofDisplay.print_results int ctxt' (k, [(s, [th])]);
wenzelm@19779
   263
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> (fn th =>
wenzelm@19779
   264
      Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th))));
wenzelm@19779
   265
    fun after_qed [[_, res]] =
wenzelm@19978
   266
      (check_result res; Proof.end_block #> Seq.map (guess_context res));
wenzelm@17858
   267
  in
wenzelm@17858
   268
    state
wenzelm@17858
   269
    |> Proof.enter_forward
wenzelm@17858
   270
    |> Proof.begin_block
wenzelm@19844
   271
    |> Proof.fix_i [(AutoBind.thesisN, NONE, NoSyn)]
wenzelm@17858
   272
    |> Proof.chain_facts chain_facts
wenzelm@19779
   273
    |> Proof.local_goal print_result (K I) (apsnd (rpair I))
wenzelm@19779
   274
      "guess" before_qed after_qed [(("", []), [Logic.mk_term goal, goal])]
wenzelm@18151
   275
    |> Proof.refine (Method.primitive_text (K (Goal.init (Thm.cterm_of thy thesis)))) |> Seq.hd
wenzelm@17858
   276
  end;
wenzelm@17858
   277
wenzelm@17858
   278
in
wenzelm@17858
   279
wenzelm@17858
   280
val guess = gen_guess ProofContext.read_vars;
wenzelm@17858
   281
val guess_i = gen_guess ProofContext.cert_vars;
wenzelm@17858
   282
wenzelm@17858
   283
end;
wenzelm@17858
   284
wenzelm@18897
   285
wenzelm@18897
   286
wenzelm@18897
   287
(** statements with several cases **)
wenzelm@18897
   288
wenzelm@18897
   289
fun statement cases =
wenzelm@18897
   290
  let
wenzelm@18907
   291
    val names =
wenzelm@18907
   292
      cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@18897
   293
    val elems = cases |> map (fn (_, (vars, _)) =>
wenzelm@19482
   294
      Element.Constrains (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE)));
wenzelm@19585
   295
    val concl = cases |> map (fn (_, (_, props)) => (("", []), map (rpair []) props));
wenzelm@18897
   296
wenzelm@18897
   297
    fun mk_stmt stmt ctxt =
wenzelm@18897
   298
      let
wenzelm@18897
   299
        val thesis =
wenzelm@18897
   300
          ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@18907
   301
        val atts = map Attrib.internal
wenzelm@18907
   302
          [RuleCases.consumes (~ (length cases)), RuleCases.case_names names];
wenzelm@18907
   303
wenzelm@18897
   304
        fun assume_case ((name, (vars, _)), (_, propp)) ctxt' =
wenzelm@18897
   305
          let
wenzelm@18897
   306
            val xs = map fst vars;
wenzelm@18897
   307
            val props = map fst propp;
wenzelm@18897
   308
            val (parms, ctxt'') =
wenzelm@18897
   309
              ctxt'
wenzelm@19897
   310
              |> fold Variable.declare_term props
wenzelm@18897
   311
              |> fold_map ProofContext.inferred_param xs;
wenzelm@18897
   312
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@18897
   313
          in
wenzelm@18897
   314
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@18897
   315
            ctxt' |> ProofContext.add_assms_i ProofContext.assume_export
wenzelm@19585
   316
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@18897
   317
            |>> (fn [(_, [th])] => th)
wenzelm@18897
   318
          end;
wenzelm@18897
   319
        val (ths, ctxt') = ctxt
wenzelm@18897
   320
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@18897
   321
          |> fold_map assume_case (cases ~~ stmt)
wenzelm@18897
   322
          |-> (fn ths => ProofContext.note_thmss_i [((thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@19585
   323
      in (([(("", atts), [(thesis, [])])], ths), ctxt') end;
wenzelm@18897
   324
  in ((elems, concl), mk_stmt) end;
wenzelm@18897
   325
wenzelm@17858
   326
end;