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