src/Pure/Isar/obtain.ML
author wenzelm
Tue Jan 31 18:19:25 2006 +0100 (2006-01-31)
changeset 18870 020e242c02a0
parent 18769 e90eb0bc0ddd
child 18897 b31293969d4f
permissions -rw-r--r--
tuned comments;
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@18670
    42
  val obtain: (string * string option) list ->
wenzelm@17111
    43
    ((string * Attrib.src list) * (string * (string list * string list)) list) list
wenzelm@17357
    44
    -> bool -> Proof.state -> Proof.state
wenzelm@18670
    45
  val obtain_i: (string * typ option) list ->
wenzelm@18728
    46
    ((string * attribute list) * (term * (term list * term list)) list) list
wenzelm@17357
    47
    -> bool -> Proof.state -> Proof.state
wenzelm@18670
    48
  val guess: (string * string option) list -> bool -> Proof.state -> Proof.state
wenzelm@18670
    49
  val guess_i: (string * typ option) list -> bool -> Proof.state -> Proof.state
wenzelm@7674
    50
end;
wenzelm@7674
    51
wenzelm@10379
    52
structure Obtain: OBTAIN =
wenzelm@7674
    53
struct
wenzelm@7674
    54
wenzelm@8094
    55
wenzelm@18670
    56
(** obtain_export **)
wenzelm@18670
    57
wenzelm@18870
    58
(*
wenzelm@18870
    59
    [x]
wenzelm@18870
    60
    [A x]
wenzelm@18870
    61
      :
wenzelm@18870
    62
      B
wenzelm@18870
    63
    -----
wenzelm@18870
    64
      B
wenzelm@18870
    65
*)
wenzelm@18678
    66
fun obtain_export ctxt parms rule cprops thm =
wenzelm@9468
    67
  let
wenzelm@17111
    68
    val {thy, prop, maxidx, ...} = Thm.rep_thm thm;
wenzelm@17111
    69
    val cparms = map (Thm.cterm_of thy) parms;
wenzelm@9468
    70
wenzelm@9468
    71
    val thm' = thm
wenzelm@18040
    72
      |> Drule.implies_intr_protected cprops
wenzelm@9468
    73
      |> Drule.forall_intr_list cparms
wenzelm@9468
    74
      |> Drule.forall_elim_vars (maxidx + 1);
wenzelm@18040
    75
    val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI);
wenzelm@9468
    76
wenzelm@9468
    77
    val concl = Logic.strip_assums_concl prop;
wenzelm@9468
    78
    val bads = parms inter (Term.term_frees concl);
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@18678
    84
      error "Conclusion in obtained context must be object-logic judgments"
wenzelm@9468
    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@17858
    98
local
wenzelm@8094
    99
wenzelm@7674
   100
val thatN = "that";
wenzelm@7674
   101
wenzelm@17357
   102
fun gen_obtain prep_att prep_vars prep_propp raw_vars raw_asms int state =
wenzelm@7674
   103
  let
wenzelm@9468
   104
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   105
    val ctxt = Proof.context_of state;
wenzelm@18678
   106
    val thy = Proof.theory_of state;
wenzelm@17357
   107
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@7674
   108
wenzelm@8543
   109
    (*obtain vars*)
wenzelm@18670
   110
    val (vars, vars_ctxt) = prep_vars (map Syntax.no_syn raw_vars) ctxt;
wenzelm@18670
   111
    val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
   112
    val xs = map #1 vars;
wenzelm@7674
   113
wenzelm@8543
   114
    (*obtain asms*)
wenzelm@11890
   115
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
skalberg@15570
   116
    val asm_props = List.concat (map (map fst) proppss);
wenzelm@18678
   117
    val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
wenzelm@10464
   118
wenzelm@10582
   119
    val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt;
wenzelm@7674
   120
wenzelm@12970
   121
    (*obtain statements*)
wenzelm@16606
   122
    val thesisN = Term.variant xs AutoBind.thesisN;
wenzelm@17858
   123
    val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN;
wenzelm@9468
   124
wenzelm@10582
   125
    fun occs_var x = Library.get_first (fn t =>
wenzelm@18151
   126
      Term.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
wenzelm@10582
   127
    val raw_parms = map occs_var xs;
skalberg@15570
   128
    val parms = List.mapPartial I raw_parms;
wenzelm@10582
   129
    val parm_names =
skalberg@15570
   130
      List.mapPartial (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (raw_parms ~~ xs);
wenzelm@10582
   131
wenzelm@10582
   132
    val that_prop =
wenzelm@17858
   133
      Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, thesis))
wenzelm@10582
   134
      |> Library.curry Logic.list_rename_params (map #2 parm_names);
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@18693
   142
        Proof.fix_i (xs ~~ map #2 vars)
wenzelm@18678
   143
        #> Proof.assm_i (K (obtain_export ctxt parms rule)) asms));
wenzelm@7674
   144
  in
wenzelm@8094
   145
    state
wenzelm@9468
   146
    |> Proof.enter_forward
wenzelm@18124
   147
    |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, ([], []))])] int
wenzelm@17858
   148
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
wenzelm@18670
   149
    |> Proof.fix_i [(thesisN, NONE)]
wenzelm@18870
   150
    |> Proof.assume_i [((thatN, [ContextRules.intro_query NONE]), [(that_prop, ([], []))])]
wenzelm@16842
   151
    |> `Proof.the_facts
wenzelm@17357
   152
    ||> Proof.chain_facts chain_facts
wenzelm@17858
   153
    ||> Proof.show_i NONE after_qed [(("", []), [(thesis, ([], []))])] false
wenzelm@17357
   154
    |-> (Proof.refine o Method.Basic o K o Method.insert) |> Seq.hd
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@18678
   170
fun match_params ctxt vars rule =
wenzelm@17858
   171
  let
wenzelm@18678
   172
    val thy = ProofContext.theory_of ctxt;
wenzelm@17891
   173
    val string_of_typ = ProofContext.string_of_typ ctxt;
wenzelm@17858
   174
    val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
wenzelm@17891
   175
wenzelm@18678
   176
    fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
wenzelm@17858
   177
wenzelm@17858
   178
    val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
wenzelm@17858
   179
    val m = length vars;
wenzelm@17858
   180
    val n = length params;
wenzelm@17891
   181
    val _ = conditional (m > n)
wenzelm@17891
   182
      (fn () => err "More variables than parameters in obtained rule" rule);
wenzelm@17858
   183
wenzelm@17858
   184
    fun match ((x, SOME T), (y, U)) tyenv =
wenzelm@17858
   185
        ((x, T), Sign.typ_match thy (U, T) tyenv handle Type.TYPE_MATCH =>
wenzelm@17891
   186
          err ("Failed to match variable " ^
wenzelm@17858
   187
            string_of_term (Free (x, T)) ^ " against parameter " ^
wenzelm@17891
   188
            string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule)
wenzelm@17858
   189
      | match ((x, NONE), (_, U)) tyenv = ((x, U), tyenv);
wenzelm@17858
   190
    val (xs, tyenv) = fold_map match (vars ~~ Library.take (m, params)) Vartab.empty;
wenzelm@17858
   191
    val ys = Library.drop (m, params);
wenzelm@17858
   192
    val norm_type = Envir.norm_type tyenv;
wenzelm@17858
   193
wenzelm@17858
   194
    val xs' = xs |> map (apsnd norm_type);
wenzelm@17858
   195
    val ys' =
wenzelm@17858
   196
      map Syntax.internal (Term.variantlist (map fst ys, map fst xs)) ~~
wenzelm@17858
   197
      map (norm_type o snd) ys;
wenzelm@17858
   198
    val instT =
wenzelm@17858
   199
      fold (Term.add_tvarsT o #2) params []
wenzelm@17858
   200
      |> map (TVar #> (fn T => (Thm.ctyp_of thy T, Thm.ctyp_of thy (norm_type T))));
wenzelm@17858
   201
    val rule' = rule |> Thm.instantiate (instT, []);
wenzelm@17891
   202
wenzelm@17891
   203
    val tvars = Drule.tvars_of rule';
wenzelm@17891
   204
    val vars = fold (remove op =) (Term.add_vars (Thm.concl_of rule') []) (Drule.vars_of rule');
wenzelm@17891
   205
    val _ =
wenzelm@17891
   206
      if null tvars andalso null vars then ()
wenzelm@17891
   207
      else err ("Illegal schematic variable(s) " ^
wenzelm@17891
   208
        commas (map (string_of_typ o TVar) tvars @ map (string_of_term o Var) vars) ^ " in") rule';
wenzelm@17858
   209
  in (xs' @ ys', rule') end;
wenzelm@17858
   210
wenzelm@18693
   211
fun inferred_type (x, _, mx) ctxt =
wenzelm@18769
   212
  let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt
wenzelm@18693
   213
  in ((x, SOME T, mx), ctxt') end;
wenzelm@18693
   214
wenzelm@17858
   215
fun gen_guess prep_vars raw_vars int state =
wenzelm@17858
   216
  let
wenzelm@17858
   217
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   218
    val thy = Proof.theory_of state;
wenzelm@17858
   219
    val ctxt = Proof.context_of state;
wenzelm@17858
   220
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@17858
   221
wenzelm@17858
   222
    val (thesis_var, thesis) = bind_judgment ctxt AutoBind.thesisN;
wenzelm@18693
   223
    val (vars, _) = ctxt |> prep_vars (map Syntax.no_syn raw_vars) |-> fold_map inferred_type;
wenzelm@17858
   224
wenzelm@17974
   225
    fun check_result th =
wenzelm@17974
   226
      (case Thm.prems_of th of
wenzelm@17974
   227
        [prem] =>
wenzelm@17974
   228
          if Thm.concl_of th aconv thesis andalso
wenzelm@17974
   229
            Logic.strip_assums_concl prem aconv thesis then ()
wenzelm@18678
   230
          else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
wenzelm@18678
   231
      | [] => error "Goal solved -- nothing guessed."
wenzelm@18678
   232
      | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
wenzelm@17891
   233
wenzelm@17858
   234
    fun guess_context raw_rule =
wenzelm@17858
   235
      let
wenzelm@18678
   236
        val (parms, rule) = match_params ctxt (map (fn (x, T, _) => (x, T)) vars) raw_rule;
wenzelm@18670
   237
        val (bind, _) = ProofContext.bind_fixes (map #1 parms) ctxt;
wenzelm@18670
   238
        val ts = map (bind o Free) parms;
wenzelm@17858
   239
        val ps = map dest_Free ts;
wenzelm@17858
   240
        val asms =
wenzelm@17858
   241
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
wenzelm@18185
   242
          |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), ([], [])));
wenzelm@18678
   243
        val _ = conditional (null asms) (fn () => error "Trivial result -- nothing guessed");
wenzelm@17858
   244
      in
wenzelm@18670
   245
        Proof.fix_i (map (apsnd SOME) parms)
wenzelm@18678
   246
        #> Proof.assm_i (K (obtain_export ctxt ts rule)) [(("", []), asms)]
wenzelm@17974
   247
        #> Proof.add_binds_i AutoBind.no_facts
wenzelm@17858
   248
      end;
wenzelm@17858
   249
wenzelm@18040
   250
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> Goal.protect));
wenzelm@18124
   251
    fun after_qed [[res]] =
wenzelm@17974
   252
      (check_result res; Proof.end_block #> Seq.map (`Proof.the_fact #-> guess_context));
wenzelm@17858
   253
  in
wenzelm@17858
   254
    state
wenzelm@17858
   255
    |> Proof.enter_forward
wenzelm@17858
   256
    |> Proof.begin_block
wenzelm@18670
   257
    |> Proof.fix_i [(AutoBind.thesisN, NONE)]
wenzelm@17858
   258
    |> Proof.chain_facts chain_facts
wenzelm@17858
   259
    |> Proof.local_goal (ProofDisplay.print_results int) (K I) (apsnd (rpair I))
wenzelm@17858
   260
      "guess" before_qed after_qed [(("", []), [Var (("guess", 0), propT)])]
wenzelm@18151
   261
    |> Proof.refine (Method.primitive_text (K (Goal.init (Thm.cterm_of thy thesis)))) |> Seq.hd
wenzelm@17858
   262
  end;
wenzelm@17858
   263
wenzelm@17858
   264
in
wenzelm@17858
   265
wenzelm@17858
   266
val guess = gen_guess ProofContext.read_vars;
wenzelm@17858
   267
val guess_i = gen_guess ProofContext.cert_vars;
wenzelm@17858
   268
wenzelm@17858
   269
end;
wenzelm@17858
   270
wenzelm@17858
   271
end;