src/Pure/Isar/obtain.ML
author wenzelm
Fri May 05 22:09:41 2000 +0200 (2000-05-05)
changeset 8807 0046be1769f9
parent 8614 30cc975727f1
child 9293 3da2533e0a61
permissions -rw-r--r--
GPLed;
     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 The common case:
    10 
    11     <goal_facts>
    12     have/show C
    13       obtain a in P[a] <proof>          ==
    14 
    15     <goal_facts>
    16     have/show C
    17     proof succeed
    18       def thesis == C
    19       presume that: !!a. P a ==> thesis
    20       from goal_facts show thesis <proof>
    21     next
    22       fix a
    23       assume P a
    24 
    25 The general case:
    26 
    27     <goal_facts>
    28     have/show !!x. G x ==> C x
    29       obtain a in P[a] <proof>          ==
    30 
    31     <goal_facts>
    32     have/show !!x. G x ==> C x
    33     proof succeed
    34       fix x
    35       assume hyps: G x
    36       def thesis == C x
    37       presume that: !!a. P a ==> thesis
    38       from goal_facts show thesis <proof>
    39     next
    40       fix a
    41       assume P a
    42 *)
    43 
    44 signature OBTAIN_DATA =
    45 sig
    46   val that_atts: Proof.context attribute list
    47 end;
    48 
    49 signature OBTAIN =
    50 sig
    51   val obtain: ((string list * string option) * Comment.text) list
    52     * ((string * Args.src list * (string * (string list * string list)) list)
    53       * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    54   val obtain_i: ((string list * typ option) * Comment.text) list
    55     * ((string * Proof.context attribute list * (term * (term list * term list)) list)
    56       * Comment.text) list -> ProofHistory.T -> ProofHistory.T
    57 end;
    58 
    59 functor ObtainFun(Data: OBTAIN_DATA): OBTAIN =
    60 struct
    61 
    62 
    63 (** obtain(_i) **)
    64 
    65 val thatN = "that";
    66 val hypsN = "hyps";
    67 
    68 fun gen_obtain prep_vars prep_propp prep_att (raw_vars, raw_asms) state =
    69   let
    70     val _ = Proof.assert_backward state;
    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 
    77     (*obtain asms*)
    78     fun prep_asm (ctxt, (name, src, raw_propps)) =
    79       let
    80         val atts = map (prep_att (ProofContext.theory_of ctxt)) src;
    81         val (ctxt', propps) = foldl_map prep_propp (ctxt, raw_propps);
    82       in (ctxt', (name, atts, propps)) end;
    83 
    84     val (asms_ctxt, asms) = foldl_map prep_asm (vars_ctxt, map Comment.ignore raw_asms);
    85     val asm_props = flat (map (map fst o #3) asms);
    86     val _ = ProofContext.warn_extra_tfrees vars_ctxt asms_ctxt;
    87 
    88     (*thesis*)
    89     val (prop, (goal_facts, goal)) = Proof.get_goal state;
    90 
    91     val parms = Logic.strip_params prop;
    92     val parm_names = Term.variantlist (map #1 parms, Term.add_term_names (prop, xs));
    93     val parm_types = map #2 parms;
    94     val parm_vars = map Library.single parm_names ~~ map Some parm_types;
    95 
    96     val frees = map2 Free (parm_names, parm_types);
    97     val rev_frees = rev frees;
    98 
    99     val hyps = map (fn t => Term.subst_bounds (rev_frees, t)) (Logic.strip_assums_hyp prop);
   100     val concl = Term.subst_bounds (rev_frees, Logic.strip_assums_concl prop);
   101     val ((thesis_name, thesis_term), atomic_thesis) = AutoBind.atomic_thesis concl;
   102 
   103     (*that_prop*)
   104     fun find_free x t =
   105       (case ProofContext.find_free t x of Some (Free a) => Some a | _ => None);
   106     fun occs_var x = Library.get_first (find_free x) asm_props;
   107     val that_prop =
   108       Term.list_all_free (mapfilter occs_var xs, Logic.list_implies (asm_props, atomic_thesis));
   109 
   110     fun after_qed st =
   111       st
   112       |> Proof.next_block
   113       |> Proof.fix_i vars
   114       |> Proof.assume_i asms
   115       |> Seq.single;
   116   in
   117     state
   118     |> Method.proof (Some (Method.Basic (K Method.succeed)))
   119     |> Seq.map (fn st => st
   120       |> Proof.fix_i parm_vars
   121       |> Proof.assume_i [(hypsN, [], map (rpair ([], [])) hyps)]
   122       |> LocalDefs.def_i "" [] ((thesis_name, None), (thesis_term, []))
   123       |> Proof.presume_i [(thatN, Data.that_atts, [(that_prop, ([], []))])]
   124       |> Proof.from_facts goal_facts
   125       |> Proof.show_i after_qed "" [] (atomic_thesis, ([], [])))
   126   end;
   127 
   128 
   129 val obtain = ProofHistory.applys o
   130   (gen_obtain ProofContext.read_vars ProofContext.read_propp Attrib.local_attribute);
   131 
   132 val obtain_i = ProofHistory.applys o
   133   (gen_obtain ProofContext.cert_vars ProofContext.cert_propp (K I));
   134 
   135 
   136 
   137 (** outer syntax **)
   138 
   139 local structure P = OuterParse and K = OuterSyntax.Keyword in
   140 
   141 val obtainP =
   142   OuterSyntax.command "obtain" "generalized existence"
   143     K.prf_asm_goal
   144     (Scan.optional
   145       (P.and_list1 (Scan.repeat1 P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- P.marg_comment)
   146         --| P.$$$ "where") [] --
   147       P.and_list1 ((P.opt_thm_name ":" -- Scan.repeat1 P.propp >> P.triple1) -- P.marg_comment)
   148     >> (Toplevel.print oo (Toplevel.proof o obtain)));
   149 
   150 val _ = OuterSyntax.add_keywords ["where"];
   151 val _ = OuterSyntax.add_parsers [obtainP];
   152 
   153 end;
   154 
   155 
   156 end;