src/Pure/Isar/obtain.ML
author wenzelm
Tue Sep 13 22:19:42 2005 +0200 (2005-09-13)
changeset 17357 ee2bdca144c7
parent 17111 d2ea9c974570
child 17858 bc4db8cfd92f
permissions -rw-r--r--
tuned Isar proof elements;
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@8543
     5
The 'obtain' language element -- generalized existence at the level of
wenzelm@8543
     6
proof texts.
wenzelm@7674
     7
wenzelm@9468
     8
  <chain_facts>
wenzelm@9468
     9
  obtain x where "P x" <proof> ==
wenzelm@7674
    10
wenzelm@12970
    11
  have "!!thesis. (!!x. P x ==> thesis) ==> thesis"
wenzelm@12970
    12
  proof succeed
wenzelm@9468
    13
    fix thesis
wenzelm@12970
    14
    assume that [intro?]: "!!x. P x ==> thesis"
wenzelm@12970
    15
    <chain_facts> show thesis <proof (insert that)>
wenzelm@12970
    16
  qed
wenzelm@10379
    17
  fix x assm (obtained) "P x"
wenzelm@8094
    18
*)
wenzelm@7674
    19
wenzelm@7674
    20
signature OBTAIN =
wenzelm@7674
    21
sig
wenzelm@11890
    22
  val obtain: (string list * string option) list ->
wenzelm@17111
    23
    ((string * Attrib.src list) * (string * (string list * string list)) list) list
wenzelm@17357
    24
    -> bool -> Proof.state -> Proof.state
wenzelm@11890
    25
  val obtain_i: (string list * typ option) list ->
wenzelm@11890
    26
    ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
wenzelm@17357
    27
    -> bool -> Proof.state -> Proof.state
wenzelm@7674
    28
end;
wenzelm@7674
    29
wenzelm@10379
    30
structure Obtain: OBTAIN =
wenzelm@7674
    31
struct
wenzelm@7674
    32
wenzelm@8094
    33
wenzelm@11816
    34
(** export_obtain **)
wenzelm@9468
    35
wenzelm@11816
    36
fun export_obtain state parms rule _ cprops thm =
wenzelm@9468
    37
  let
wenzelm@17111
    38
    val {thy, prop, maxidx, ...} = Thm.rep_thm thm;
wenzelm@17111
    39
    val cparms = map (Thm.cterm_of thy) parms;
wenzelm@9468
    40
wenzelm@9468
    41
    val thm' = thm
wenzelm@11816
    42
      |> Drule.implies_intr_goals cprops
wenzelm@9468
    43
      |> Drule.forall_intr_list cparms
wenzelm@9468
    44
      |> Drule.forall_elim_vars (maxidx + 1);
wenzelm@11816
    45
    val elim_tacs = replicate (length cprops) (Tactic.etac Drule.triv_goal);
wenzelm@9468
    46
wenzelm@9468
    47
    val concl = Logic.strip_assums_concl prop;
wenzelm@9468
    48
    val bads = parms inter (Term.term_frees concl);
wenzelm@9468
    49
  in
wenzelm@9468
    50
    if not (null bads) then
wenzelm@9481
    51
      raise Proof.STATE ("Conclusion contains obtained parameters: " ^
wenzelm@12055
    52
        space_implode " " (map (ProofContext.string_of_term (Proof.context_of state)) bads), state)
wenzelm@17111
    53
    else if not (ObjectLogic.is_judgment thy (Logic.strip_assums_concl prop)) then
wenzelm@10379
    54
      raise Proof.STATE ("Conclusions of 'obtain' context must be object-logic judgments", state)
wenzelm@9468
    55
    else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule
wenzelm@9468
    56
  end;
wenzelm@9468
    57
wenzelm@9468
    58
wenzelm@9468
    59
wenzelm@8094
    60
(** obtain(_i) **)
wenzelm@8094
    61
wenzelm@7674
    62
val thatN = "that";
wenzelm@7674
    63
wenzelm@17357
    64
fun gen_obtain prep_att prep_vars prep_propp raw_vars raw_asms int state =
wenzelm@7674
    65
  let
wenzelm@9468
    66
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17357
    67
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@10464
    68
    val thy = Proof.theory_of state;
wenzelm@7674
    69
wenzelm@8543
    70
    (*obtain vars*)
wenzelm@11890
    71
    val (vars_ctxt, vars) = foldl_map prep_vars (Proof.context_of state, raw_vars);
skalberg@15570
    72
    val xs = List.concat (map fst vars);
wenzelm@10582
    73
    val fix_ctxt = vars_ctxt |> ProofContext.fix_i vars;
wenzelm@7674
    74
wenzelm@8543
    75
    (*obtain asms*)
wenzelm@11890
    76
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
skalberg@15570
    77
    val asm_props = List.concat (map (map fst) proppss);
wenzelm@17111
    78
    val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
wenzelm@10464
    79
wenzelm@10582
    80
    val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt;
wenzelm@7674
    81
wenzelm@12970
    82
    (*obtain statements*)
wenzelm@16606
    83
    val thesisN = Term.variant xs AutoBind.thesisN;
wenzelm@12970
    84
    val bind_thesis = ProofContext.bind_skolem fix_ctxt [thesisN];
wenzelm@17111
    85
    val bound_thesis = bind_thesis (ObjectLogic.fixed_judgment thy thesisN);
wenzelm@12970
    86
    val bound_thesis_raw as (bound_thesis_name, _) =
wenzelm@12970
    87
      Term.dest_Free (bind_thesis (Free (thesisN, propT)));
wenzelm@12970
    88
    val bound_thesis_var =
haftmann@16787
    89
      fold_aterms (fn Free (x, T) => (fn v => if x = bound_thesis_name then (x, T) else v)
haftmann@16787
    90
        | _ => I) bound_thesis bound_thesis_raw;
wenzelm@9468
    91
wenzelm@10582
    92
    fun occs_var x = Library.get_first (fn t =>
wenzelm@10582
    93
      ProofContext.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
wenzelm@10582
    94
    val raw_parms = map occs_var xs;
skalberg@15570
    95
    val parms = List.mapPartial I raw_parms;
wenzelm@10582
    96
    val parm_names =
skalberg@15570
    97
      List.mapPartial (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (raw_parms ~~ xs);
wenzelm@10582
    98
wenzelm@10582
    99
    val that_prop =
wenzelm@10582
   100
      Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, bound_thesis))
wenzelm@10582
   101
      |> Library.curry Logic.list_rename_params (map #2 parm_names);
wenzelm@12970
   102
    val obtain_prop =
wenzelm@12970
   103
      Logic.list_rename_params ([AutoBind.thesisN],
wenzelm@12970
   104
        Term.list_all_free ([bound_thesis_var], Logic.mk_implies (that_prop, bound_thesis)));
wenzelm@7674
   105
wenzelm@17357
   106
    fun after_qed _ _ =
wenzelm@17357
   107
      Proof.local_qed (NONE, false)
wenzelm@17357
   108
      #> Seq.map (`Proof.the_fact #-> (fn this =>
wenzelm@17357
   109
        Proof.fix_i vars
wenzelm@17357
   110
        #> Proof.assm_i (export_obtain state parms this) asms));
wenzelm@7674
   111
  in
wenzelm@8094
   112
    state
wenzelm@9468
   113
    |> Proof.enter_forward
wenzelm@17357
   114
    |> Proof.have_i (K (K Seq.single)) [(("", []), [(obtain_prop, ([], []))])] int
wenzelm@17111
   115
    |> Proof.proof (SOME (Method.Basic (K Method.succeed))) |> Seq.hd
skalberg@15531
   116
    |> Proof.fix_i [([thesisN], NONE)]
skalberg@15531
   117
    |> Proof.assume_i [((thatN, [ContextRules.intro_query_local NONE]), [(that_prop, ([], []))])]
wenzelm@16842
   118
    |> `Proof.the_facts
wenzelm@17357
   119
    ||> Proof.chain_facts chain_facts
wenzelm@17357
   120
    ||> Proof.show_i after_qed [(("", []), [(bound_thesis, ([], []))])] false
wenzelm@17357
   121
    |-> (Proof.refine o Method.Basic o K o Method.insert) |> Seq.hd
wenzelm@7674
   122
  end;
wenzelm@7674
   123
wenzelm@17111
   124
val obtain = gen_obtain Attrib.local_attribute ProofContext.read_vars ProofContext.read_propp;
wenzelm@17111
   125
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
wenzelm@8094
   126
wenzelm@8094
   127
end;