src/Pure/Isar/obtain.ML
author wenzelm
Wed Jan 05 11:45:01 2000 +0100 (2000-01-05)
changeset 8094 62b45a2e6ecb
parent 7923 895d31b54da5
child 8109 aca11f954993
permissions -rw-r--r--
ObtainFun;
prepare vars / asms / pats only once;
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@8094
     5
The 'obtain' language element -- eliminated existential quantification
wenzelm@8094
     6
at the level of 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@7674
    34
      assume antecedent: 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@7674
    41
wenzelm@8094
    42
TODO:
wenzelm@8094
    43
  - bind terms for goal as well (done?);
wenzelm@8094
    44
  - improve block structure (admit subsequent occurences of 'next') (no?);
wenzelm@8094
    45
  - handle general case (easy??);
wenzelm@8094
    46
*)
wenzelm@7674
    47
wenzelm@8094
    48
signature OBTAIN_DATA =
wenzelm@8094
    49
sig
wenzelm@8094
    50
  val that_atts: Proof.context attribute list
wenzelm@8094
    51
end;
wenzelm@7674
    52
wenzelm@7674
    53
signature OBTAIN =
wenzelm@7674
    54
sig
wenzelm@8094
    55
  val obtain: ((string list * string option) * Comment.text) list
wenzelm@8094
    56
    * ((string * Args.src list * (string * (string list * string list)) list)
wenzelm@8094
    57
      * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@8094
    58
  val obtain_i: ((string list * typ option) * Comment.text) list
wenzelm@8094
    59
    * ((string * Proof.context attribute list * (term * (term list * term list)) list)
wenzelm@8094
    60
      * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@7674
    61
end;
wenzelm@7674
    62
wenzelm@8094
    63
functor ObtainFun(Data: OBTAIN_DATA): OBTAIN =
wenzelm@7674
    64
struct
wenzelm@7674
    65
wenzelm@8094
    66
wenzelm@8094
    67
(** obtain(_i) **)
wenzelm@8094
    68
wenzelm@7674
    69
val thatN = "that";
wenzelm@7674
    70
wenzelm@8094
    71
fun gen_obtain prep_vars prep_propp prep_att (raw_vars, raw_asms) state =
wenzelm@7674
    72
  let
wenzelm@8094
    73
    (*thesis*)
wenzelm@7674
    74
    val (prop, (goal_facts, goal)) = Proof.get_goal (Proof.assert_backward state);
wenzelm@7674
    75
wenzelm@8094
    76
    val parms = Logic.strip_params prop;        (* FIXME unused *)
wenzelm@7674
    77
    val _ =
wenzelm@7674
    78
      if null parms then () else raise Proof.STATE ("Cannot handle params in goal (yet)", state);
wenzelm@8094
    79
    val hyps = Logic.strip_assums_hyp prop;     (* FIXME unused *)
wenzelm@8094
    80
    val concl = Logic.strip_assums_concl prop;
wenzelm@7674
    81
    val ((thesis_name, thesis_term), atomic_thesis) = AutoBind.atomic_thesis concl;
wenzelm@7674
    82
wenzelm@8094
    83
    (*vars*)
wenzelm@8094
    84
    val (vars_ctxt, vars) =
wenzelm@8094
    85
      foldl_map prep_vars (Proof.context_of state, map Comment.ignore raw_vars);
wenzelm@8094
    86
    val xs = flat (map fst vars);
wenzelm@7674
    87
wenzelm@8094
    88
    (*asms*)
wenzelm@8094
    89
    fun prep_asm (ctxt, (name, src, raw_propps)) =
wenzelm@8094
    90
      let
wenzelm@8094
    91
        val atts = map (prep_att (ProofContext.theory_of ctxt)) src;
wenzelm@8094
    92
        val (ctxt', propps) = foldl_map prep_propp (ctxt, raw_propps);
wenzelm@8094
    93
      in (ctxt', (name, atts, propps)) end;
wenzelm@7674
    94
wenzelm@8094
    95
    val (asms_ctxt, asms) = foldl_map prep_asm (vars_ctxt, map Comment.ignore raw_asms);
wenzelm@8094
    96
    val asm_props = flat (map (map fst o #3) asms);
wenzelm@8094
    97
    val _ = ProofContext.warn_extra_tfrees vars_ctxt asms_ctxt;
wenzelm@7674
    98
wenzelm@8094
    99
    (*that_prop*)
wenzelm@7674
   100
    fun find_free x t =
wenzelm@7674
   101
      (case Proof.find_free t x of Some (Free a) => Some a | _ => None);
wenzelm@8094
   102
    fun occs_var x = Library.get_first (find_free x) asm_props;
wenzelm@7674
   103
    val that_prop =
wenzelm@8094
   104
      Term.list_all_free (mapfilter occs_var xs, Logic.list_implies (asm_props, atomic_thesis));
wenzelm@7674
   105
wenzelm@7674
   106
    fun after_qed st =
wenzelm@7674
   107
      st
wenzelm@7674
   108
      |> Proof.next_block
wenzelm@8094
   109
      |> Proof.fix_i vars
wenzelm@8094
   110
      |> Proof.assume_i asms
wenzelm@7674
   111
      |> Seq.single;
wenzelm@7674
   112
  in
wenzelm@8094
   113
    state
wenzelm@8094
   114
    |> Method.proof (Some (Method.Basic (K Method.succeed)))
wenzelm@7674
   115
    |> Seq.map (fn st => st
wenzelm@8094
   116
      |> LocalDefs.def_i "" [] ((thesis_name, None), (thesis_term, []))
wenzelm@8094
   117
      |> Proof.presume_i [(thatN, Data.that_atts, [(that_prop, ([], []))])]
wenzelm@7674
   118
      |> Proof.from_facts goal_facts
wenzelm@8094
   119
      |> Proof.show_i after_qed "" [] (atomic_thesis, ([], [])))
wenzelm@7674
   120
  end;
wenzelm@7674
   121
wenzelm@7674
   122
wenzelm@8094
   123
val obtain = ProofHistory.applys o
wenzelm@8094
   124
  (gen_obtain ProofContext.read_vars ProofContext.read_propp Attrib.local_attribute);
wenzelm@8094
   125
wenzelm@8094
   126
val obtain_i = ProofHistory.applys o
wenzelm@8094
   127
  (gen_obtain ProofContext.cert_vars ProofContext.cert_propp (K I));
wenzelm@8094
   128
wenzelm@8094
   129
wenzelm@8094
   130
wenzelm@8094
   131
(** outer syntax **)
wenzelm@8094
   132
wenzelm@8094
   133
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@8094
   134
wenzelm@8094
   135
val obtainP =
wenzelm@8094
   136
  OuterSyntax.command "obtain" "proof text-level existential quantifier"
wenzelm@8094
   137
    K.prf_asm_goal
wenzelm@8094
   138
    (Scan.optional
wenzelm@8094
   139
      (P.and_list1 (Scan.repeat1 P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- P.marg_comment)
wenzelm@8094
   140
        --| P.$$$ "in") [] --
wenzelm@8094
   141
      P.and_list1 ((P.opt_thm_name ":" -- Scan.repeat1 P.propp >> P.triple1) -- P.marg_comment)
wenzelm@8094
   142
    >> (Toplevel.print oo (Toplevel.proof o obtain)));
wenzelm@8094
   143
wenzelm@8094
   144
val _ = OuterSyntax.add_parsers [obtainP];
wenzelm@8094
   145
wenzelm@8094
   146
end;
wenzelm@7674
   147
wenzelm@7674
   148
wenzelm@7674
   149
end;