src/Pure/Isar/obtain.ML
author wenzelm
Thu Nov 10 20:57:21 2005 +0100 (2005-11-10)
changeset 18151 32538cf750ca
parent 18124 a310c78298f9
child 18185 9d51fad6bb1f
permissions -rw-r--r--
guess: Seq.hd;
Term.find_free;
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@9468
    11
  obtain x where "P x" <proof> ==
wenzelm@7674
    12
wenzelm@12970
    13
  have "!!thesis. (!!x. P x ==> thesis) ==> thesis"
wenzelm@12970
    14
  proof succeed
wenzelm@9468
    15
    fix thesis
wenzelm@12970
    16
    assume that [intro?]: "!!x. P x ==> thesis"
wenzelm@12970
    17
    <chain_facts> show thesis <proof (insert that)>
wenzelm@12970
    18
  qed
wenzelm@10379
    19
  fix x assm (obtained) "P x"
wenzelm@17858
    20
wenzelm@17858
    21
wenzelm@17858
    22
  <chain_facts>
wenzelm@17858
    23
  guess x <proof body> <proof end> ==
wenzelm@17858
    24
wenzelm@17858
    25
  {
wenzelm@17858
    26
    fix thesis
wenzelm@17858
    27
    <chain_facts> have "PROP ?guess"
wenzelm@17974
    28
      apply magic      -- {* turns goal into "thesis ==> Goal thesis" *}
wenzelm@17858
    29
      <proof body>
wenzelm@17974
    30
      apply_end magic  -- {* turns final "(!!x. P x ==> thesis) ==> Goal thesis" into
wenzelm@17974
    31
        "Goal ((!!x. P x ==> thesis) ==> thesis)" which is a finished goal state *}
wenzelm@17858
    32
      <proof end>
wenzelm@17858
    33
  }
wenzelm@17858
    34
  fix x assm (obtained) "P x"
wenzelm@8094
    35
*)
wenzelm@7674
    36
wenzelm@7674
    37
signature OBTAIN =
wenzelm@7674
    38
sig
wenzelm@11890
    39
  val obtain: (string list * string option) list ->
wenzelm@17111
    40
    ((string * Attrib.src list) * (string * (string list * string list)) list) list
wenzelm@17357
    41
    -> bool -> Proof.state -> Proof.state
wenzelm@11890
    42
  val obtain_i: (string list * typ option) list ->
wenzelm@11890
    43
    ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
wenzelm@17357
    44
    -> bool -> Proof.state -> Proof.state
wenzelm@18151
    45
  val guess: (string list * string option) list -> bool -> Proof.state -> Proof.state
wenzelm@18151
    46
  val guess_i: (string list * typ option) list -> bool -> Proof.state -> Proof.state
wenzelm@7674
    47
end;
wenzelm@7674
    48
wenzelm@10379
    49
structure Obtain: OBTAIN =
wenzelm@7674
    50
struct
wenzelm@7674
    51
wenzelm@17858
    52
(** export_obtained **)
wenzelm@8094
    53
wenzelm@17858
    54
fun export_obtained state parms rule cprops thm =
wenzelm@9468
    55
  let
wenzelm@17111
    56
    val {thy, prop, maxidx, ...} = Thm.rep_thm thm;
wenzelm@17111
    57
    val cparms = map (Thm.cterm_of thy) parms;
wenzelm@9468
    58
wenzelm@9468
    59
    val thm' = thm
wenzelm@18040
    60
      |> Drule.implies_intr_protected cprops
wenzelm@9468
    61
      |> Drule.forall_intr_list cparms
wenzelm@9468
    62
      |> Drule.forall_elim_vars (maxidx + 1);
wenzelm@18040
    63
    val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI);
wenzelm@9468
    64
wenzelm@9468
    65
    val concl = Logic.strip_assums_concl prop;
wenzelm@9468
    66
    val bads = parms inter (Term.term_frees concl);
wenzelm@9468
    67
  in
wenzelm@9468
    68
    if not (null bads) then
wenzelm@9481
    69
      raise Proof.STATE ("Conclusion contains obtained parameters: " ^
wenzelm@12055
    70
        space_implode " " (map (ProofContext.string_of_term (Proof.context_of state)) bads), state)
wenzelm@17858
    71
    else if not (ObjectLogic.is_judgment thy concl) then
wenzelm@17858
    72
      raise Proof.STATE ("Conclusion in obtained context must be object-logic judgments", state)
wenzelm@9468
    73
    else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule
wenzelm@9468
    74
  end;
wenzelm@9468
    75
wenzelm@9468
    76
wenzelm@9468
    77
wenzelm@17858
    78
(** obtain **)
wenzelm@17858
    79
wenzelm@17858
    80
fun bind_judgment ctxt name =
wenzelm@17858
    81
  let val (t as _ $ Free v) =
wenzelm@17858
    82
    ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name
wenzelm@17858
    83
    |> ProofContext.bind_skolem ctxt [name]
wenzelm@17858
    84
  in (v, t) end;
wenzelm@17858
    85
wenzelm@17858
    86
local
wenzelm@8094
    87
wenzelm@7674
    88
val thatN = "that";
wenzelm@7674
    89
wenzelm@17357
    90
fun gen_obtain prep_att prep_vars prep_propp raw_vars raw_asms int state =
wenzelm@7674
    91
  let
wenzelm@9468
    92
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
    93
    val ctxt = Proof.context_of state;
wenzelm@17357
    94
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@7674
    95
wenzelm@8543
    96
    (*obtain vars*)
wenzelm@17858
    97
    val (vars, vars_ctxt) = fold_map prep_vars raw_vars ctxt;
wenzelm@17858
    98
    val fix_ctxt = vars_ctxt |> ProofContext.fix_i vars;
skalberg@15570
    99
    val xs = List.concat (map fst vars);
wenzelm@7674
   100
wenzelm@8543
   101
    (*obtain asms*)
wenzelm@11890
   102
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
skalberg@15570
   103
    val asm_props = List.concat (map (map fst) proppss);
wenzelm@17858
   104
    val asms = map fst (Attrib.map_specs (prep_att (Proof.theory_of state)) raw_asms) ~~ proppss;
wenzelm@10464
   105
wenzelm@10582
   106
    val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt;
wenzelm@7674
   107
wenzelm@12970
   108
    (*obtain statements*)
wenzelm@16606
   109
    val thesisN = Term.variant xs AutoBind.thesisN;
wenzelm@17858
   110
    val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN;
wenzelm@9468
   111
wenzelm@10582
   112
    fun occs_var x = Library.get_first (fn t =>
wenzelm@18151
   113
      Term.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
wenzelm@10582
   114
    val raw_parms = map occs_var xs;
skalberg@15570
   115
    val parms = List.mapPartial I raw_parms;
wenzelm@10582
   116
    val parm_names =
skalberg@15570
   117
      List.mapPartial (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (raw_parms ~~ xs);
wenzelm@10582
   118
wenzelm@10582
   119
    val that_prop =
wenzelm@17858
   120
      Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, thesis))
wenzelm@10582
   121
      |> Library.curry Logic.list_rename_params (map #2 parm_names);
wenzelm@12970
   122
    val obtain_prop =
wenzelm@12970
   123
      Logic.list_rename_params ([AutoBind.thesisN],
wenzelm@17858
   124
        Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
wenzelm@7674
   125
wenzelm@18124
   126
    fun after_qed _ =
wenzelm@17357
   127
      Proof.local_qed (NONE, false)
wenzelm@17858
   128
      #> Seq.map (`Proof.the_fact #-> (fn rule =>
wenzelm@17357
   129
        Proof.fix_i vars
wenzelm@17858
   130
        #> Proof.assm_i (K (export_obtained state parms rule)) asms));
wenzelm@7674
   131
  in
wenzelm@8094
   132
    state
wenzelm@9468
   133
    |> Proof.enter_forward
wenzelm@18124
   134
    |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, ([], []))])] int
wenzelm@17858
   135
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
skalberg@15531
   136
    |> Proof.fix_i [([thesisN], NONE)]
skalberg@15531
   137
    |> Proof.assume_i [((thatN, [ContextRules.intro_query_local NONE]), [(that_prop, ([], []))])]
wenzelm@16842
   138
    |> `Proof.the_facts
wenzelm@17357
   139
    ||> Proof.chain_facts chain_facts
wenzelm@17858
   140
    ||> Proof.show_i NONE after_qed [(("", []), [(thesis, ([], []))])] false
wenzelm@17357
   141
    |-> (Proof.refine o Method.Basic o K o Method.insert) |> Seq.hd
wenzelm@7674
   142
  end;
wenzelm@7674
   143
wenzelm@17858
   144
in
wenzelm@17858
   145
wenzelm@17111
   146
val obtain = gen_obtain Attrib.local_attribute ProofContext.read_vars ProofContext.read_propp;
wenzelm@17111
   147
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
wenzelm@8094
   148
wenzelm@8094
   149
end;
wenzelm@17858
   150
wenzelm@17858
   151
wenzelm@17858
   152
wenzelm@17858
   153
(** guess **)
wenzelm@17858
   154
wenzelm@17858
   155
local
wenzelm@17858
   156
wenzelm@17858
   157
fun match_params state vars rule =
wenzelm@17858
   158
  let
wenzelm@17858
   159
    val ctxt = Proof.context_of state;
wenzelm@17858
   160
    val thy = Proof.theory_of state;
wenzelm@17891
   161
    val string_of_typ = ProofContext.string_of_typ ctxt;
wenzelm@17858
   162
    val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
wenzelm@17891
   163
wenzelm@17891
   164
    fun err msg th = raise Proof.STATE (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th, state);
wenzelm@17858
   165
wenzelm@17858
   166
    val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
wenzelm@17858
   167
    val m = length vars;
wenzelm@17858
   168
    val n = length params;
wenzelm@17891
   169
    val _ = conditional (m > n)
wenzelm@17891
   170
      (fn () => err "More variables than parameters in obtained rule" rule);
wenzelm@17858
   171
wenzelm@17858
   172
    fun match ((x, SOME T), (y, U)) tyenv =
wenzelm@17858
   173
        ((x, T), Sign.typ_match thy (U, T) tyenv handle Type.TYPE_MATCH =>
wenzelm@17891
   174
          err ("Failed to match variable " ^
wenzelm@17858
   175
            string_of_term (Free (x, T)) ^ " against parameter " ^
wenzelm@17891
   176
            string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule)
wenzelm@17858
   177
      | match ((x, NONE), (_, U)) tyenv = ((x, U), tyenv);
wenzelm@17858
   178
    val (xs, tyenv) = fold_map match (vars ~~ Library.take (m, params)) Vartab.empty;
wenzelm@17858
   179
    val ys = Library.drop (m, params);
wenzelm@17858
   180
    val norm_type = Envir.norm_type tyenv;
wenzelm@17858
   181
wenzelm@17858
   182
    val xs' = xs |> map (apsnd norm_type);
wenzelm@17858
   183
    val ys' =
wenzelm@17858
   184
      map Syntax.internal (Term.variantlist (map fst ys, map fst xs)) ~~
wenzelm@17858
   185
      map (norm_type o snd) ys;
wenzelm@17858
   186
    val instT =
wenzelm@17858
   187
      fold (Term.add_tvarsT o #2) params []
wenzelm@17858
   188
      |> map (TVar #> (fn T => (Thm.ctyp_of thy T, Thm.ctyp_of thy (norm_type T))));
wenzelm@17858
   189
    val rule' = rule |> Thm.instantiate (instT, []);
wenzelm@17891
   190
wenzelm@17891
   191
    val tvars = Drule.tvars_of rule';
wenzelm@17891
   192
    val vars = fold (remove op =) (Term.add_vars (Thm.concl_of rule') []) (Drule.vars_of rule');
wenzelm@17891
   193
    val _ =
wenzelm@17891
   194
      if null tvars andalso null vars then ()
wenzelm@17891
   195
      else err ("Illegal schematic variable(s) " ^
wenzelm@17891
   196
        commas (map (string_of_typ o TVar) tvars @ map (string_of_term o Var) vars) ^ " in") rule';
wenzelm@17858
   197
  in (xs' @ ys', rule') end;
wenzelm@17858
   198
wenzelm@17858
   199
fun gen_guess prep_vars raw_vars int state =
wenzelm@17858
   200
  let
wenzelm@17858
   201
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   202
    val thy = Proof.theory_of state;
wenzelm@17858
   203
    val ctxt = Proof.context_of state;
wenzelm@17858
   204
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@17858
   205
wenzelm@17858
   206
    val (thesis_var, thesis) = bind_judgment ctxt AutoBind.thesisN;
wenzelm@17858
   207
    val varss = #1 (fold_map prep_vars raw_vars ctxt);
wenzelm@17858
   208
    val vars = List.concat (map (fn (xs, T) => map (rpair T) xs) varss);
wenzelm@17858
   209
wenzelm@17974
   210
    fun check_result th =
wenzelm@17974
   211
      (case Thm.prems_of th of
wenzelm@17974
   212
        [prem] =>
wenzelm@17974
   213
          if Thm.concl_of th aconv thesis andalso
wenzelm@17974
   214
            Logic.strip_assums_concl prem aconv thesis then ()
wenzelm@17974
   215
          else raise Proof.STATE ("Guessed a different clause:\n" ^
wenzelm@17974
   216
            ProofContext.string_of_thm ctxt th, state)
wenzelm@17974
   217
      | [] => raise Proof.STATE ("Goal solved -- nothing guessed.", state)
wenzelm@17858
   218
      | _ => raise Proof.STATE ("Guess split into several cases:\n" ^
wenzelm@17974
   219
        ProofContext.string_of_thm ctxt th, state));
wenzelm@17891
   220
wenzelm@17858
   221
    fun guess_context raw_rule =
wenzelm@17858
   222
      let
wenzelm@17858
   223
        val (parms, rule) = match_params state vars raw_rule;
wenzelm@17858
   224
        val ts = map (ProofContext.bind_skolem ctxt (map #1 parms) o Free) parms;
wenzelm@17858
   225
        val ps = map dest_Free ts;
wenzelm@17858
   226
        val asms =
wenzelm@17858
   227
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
wenzelm@17858
   228
          |> map (fn asm => (Library.foldl Term.betapply (Term.list_abs (ps, asm), ts), ([], [])));
wenzelm@17974
   229
        val _ = conditional (null asms) (fn () =>
wenzelm@17974
   230
          raise Proof.STATE ("Trivial result -- nothing guessed", state));
wenzelm@17858
   231
      in
wenzelm@17858
   232
        Proof.fix_i (map (fn (x, T) => ([x], SOME T)) parms)
wenzelm@17858
   233
        #> Proof.assm_i (K (export_obtained state ts rule)) [(("", []), asms)]
wenzelm@17974
   234
        #> Proof.add_binds_i AutoBind.no_facts
wenzelm@17858
   235
      end;
wenzelm@17858
   236
wenzelm@18040
   237
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> Goal.protect));
wenzelm@18124
   238
    fun after_qed [[res]] =
wenzelm@17974
   239
      (check_result res; Proof.end_block #> Seq.map (`Proof.the_fact #-> guess_context));
wenzelm@17858
   240
  in
wenzelm@17858
   241
    state
wenzelm@17858
   242
    |> Proof.enter_forward
wenzelm@17858
   243
    |> Proof.begin_block
wenzelm@17858
   244
    |> Proof.fix_i [([AutoBind.thesisN], NONE)]
wenzelm@17858
   245
    |> Proof.chain_facts chain_facts
wenzelm@17858
   246
    |> Proof.local_goal (ProofDisplay.print_results int) (K I) (apsnd (rpair I))
wenzelm@17858
   247
      "guess" before_qed after_qed [(("", []), [Var (("guess", 0), propT)])]
wenzelm@18151
   248
    |> Proof.refine (Method.primitive_text (K (Goal.init (Thm.cterm_of thy thesis)))) |> Seq.hd
wenzelm@17858
   249
  end;
wenzelm@17858
   250
wenzelm@17858
   251
in
wenzelm@17858
   252
wenzelm@17858
   253
val guess = gen_guess ProofContext.read_vars;
wenzelm@17858
   254
val guess_i = gen_guess ProofContext.cert_vars;
wenzelm@17858
   255
wenzelm@17858
   256
end;
wenzelm@17858
   257
wenzelm@17858
   258
end;