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