src/Pure/Isar/obtain.ML
author wenzelm
Sun Oct 14 20:09:19 2001 +0200 (2001-10-14)
changeset 11764 fd780dd6e0b4
parent 11021 41de937d338b
child 11816 545aab7410ac
permissions -rw-r--r--
use ObjectLogic;
     1 (*  Title:      Pure/Isar/obtain.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 The 'obtain' language element -- generalized existence at the level of
     7 proof texts.
     8 
     9   <chain_facts>
    10   obtain x where "P x" <proof> ==
    11 
    12   {
    13     fix thesis
    14     assume that [intro]: "!!x. P x ==> thesis"
    15     <chain_facts> have thesis <proof (insert that)>
    16   }
    17   fix x assm (obtained) "P x"
    18 
    19 *)
    20 
    21 signature OBTAIN =
    22 sig
    23   val obtain: ((string list * string option) * Comment.text) list
    24     * (((string * Args.src list) * (string * (string list * string list)) list)
    25       * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    26   val obtain_i: ((string list * typ option) * Comment.text) list
    27     * (((string * Proof.context attribute list) * (term * (term list * term list)) list)
    28       * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    29 end;
    30 
    31 structure Obtain: OBTAIN =
    32 struct
    33 
    34 
    35 (** disch_obtained **)
    36 
    37 fun disch_obtained state parms rule cprops thm =
    38   let
    39     val {sign, prop, maxidx, ...} = Thm.rep_thm thm;
    40     val cparms = map (Thm.cterm_of sign) parms;
    41 
    42     val thm' = thm
    43       |> Drule.implies_intr_list cprops
    44       |> Drule.forall_intr_list cparms
    45       |> Drule.forall_elim_vars (maxidx + 1);
    46     val elim_tacs = replicate (length cprops) Proof.hard_asm_tac;
    47 
    48     val concl = Logic.strip_assums_concl prop;
    49     val bads = parms inter (Term.term_frees concl);
    50   in
    51     if not (null bads) then
    52       raise Proof.STATE ("Conclusion contains obtained parameters: " ^
    53         space_implode " " (map (Sign.string_of_term sign) bads), state)
    54     else if not (ObjectLogic.is_judgment sign (Logic.strip_assums_concl prop)) then
    55       raise Proof.STATE ("Conclusions of 'obtain' context must be object-logic judgments", state)
    56     else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule
    57   end;
    58 
    59 
    60 
    61 (** obtain(_i) **)
    62 
    63 val thatN = "that";
    64 
    65 fun gen_obtain prep_vars prep_propp prep_att (raw_vars, raw_asms) state =
    66   let
    67     val _ = Proof.assert_forward_or_chain state;
    68     val chain_facts = if Proof.is_chain state then Proof.the_facts state else [];
    69     val thy = Proof.theory_of state;
    70     val sign = Theory.sign_of thy;
    71 
    72     (*obtain vars*)
    73     val (vars_ctxt, vars) =
    74       foldl_map prep_vars (Proof.context_of state, map Comment.ignore raw_vars);
    75     val xs = flat (map fst vars);
    76     val fix_ctxt = vars_ctxt |> ProofContext.fix_i vars;
    77 
    78     (*obtain asms*)
    79     val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map (snd o Comment.ignore) raw_asms);
    80     val asm_props = flat (map (map fst) proppss);
    81 
    82     fun prep_asm ((name, src), propps) = ((name, map (prep_att thy) src), propps);
    83     val asms = map2 prep_asm (map (fst o Comment.ignore) raw_asms, proppss);
    84 
    85     val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt;
    86 
    87     (*that_prop*)
    88     val thesisN = Term.variant xs AutoBind.thesisN;
    89     val bound_thesis =
    90       ProofContext.bind_skolem fix_ctxt [thesisN] (ObjectLogic.fixed_judgment sign thesisN);
    91 
    92     fun occs_var x = Library.get_first (fn t =>
    93       ProofContext.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
    94     val raw_parms = map occs_var xs;
    95     val parms = mapfilter I raw_parms;
    96     val parm_names =
    97       mapfilter (fn (Some (Free a), x) => Some (a, x) | _ => None) (raw_parms ~~ xs);
    98 
    99     val that_prop =
   100       Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, bound_thesis))
   101       |> Library.curry Logic.list_rename_params (map #2 parm_names);
   102 
   103     fun export_obtained rule =
   104       (disch_obtained state parms rule, fn _ => fn _ => []);
   105 
   106     fun after_qed st = st
   107       |> Proof.end_block
   108       |> Seq.map (fn st' => st'
   109         |> Proof.fix_i vars
   110         |> Proof.assm_i (export_obtained (Proof.the_fact st')) asms);
   111   in
   112     state
   113     |> Proof.enter_forward
   114     |> Proof.begin_block
   115     |> Proof.fix_i [([thesisN], None)]
   116     |> Proof.assume_i [((thatN, [Method.intro_local]), [(that_prop, ([], []))])]
   117     |> (fn state' =>
   118       state'
   119       |> Proof.from_facts chain_facts
   120       |> Proof.have_i after_qed "" [] (bound_thesis, ([], []))
   121       |> Method.refine (Method.Basic (K (Method.insert (Proof.the_facts state')))))
   122   end;
   123 
   124 
   125 val obtain = ProofHistory.applys o
   126   (gen_obtain ProofContext.read_vars ProofContext.read_propp Attrib.local_attribute);
   127 
   128 val obtain_i = ProofHistory.applys o
   129   (gen_obtain ProofContext.cert_vars ProofContext.cert_propp (K I));
   130 
   131 
   132 
   133 (** outer syntax **)
   134 
   135 local structure P = OuterParse and K = OuterSyntax.Keyword in
   136 
   137 val obtainP =
   138   OuterSyntax.command "obtain" "generalized existence"
   139     K.prf_asm_goal
   140     (Scan.optional
   141       (P.and_list1 (Scan.repeat1 P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- P.marg_comment)
   142         --| P.$$$ "where") [] --
   143       P.and_list1 (P.opt_thm_name ":" -- Scan.repeat1 P.propp -- P.marg_comment)
   144     >> (Toplevel.print oo (Toplevel.proof o obtain)));
   145 
   146 val _ = OuterSyntax.add_keywords ["where"];
   147 val _ = OuterSyntax.add_parsers [obtainP];
   148 
   149 end;
   150 
   151 end;