src/Pure/Isar/obtain.ML
author wenzelm
Wed, 09 Aug 2006 00:12:38 +0200
changeset 20366 867696dc64fc
parent 20308 ddb7e7129481
child 20804 0e2591606867
permissions -rw-r--r--
global goals/qeds: after_qed operates on Proof.context (potentially local_theory); theorem/interpretation: slightly more uniform treatment of after_qeds; theorem conclusion: proper fix_frees;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/obtain.ML
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
     4
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
     5
The 'obtain' and 'guess' language elements -- generalized existence at
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
     6
the level of proof texts: 'obtain' involves a proof that certain
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
     7
fixes/assumes may be introduced into the present context; 'guess' is
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
     8
similar, but derives these elements from the course of reasoning!
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
     9
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    10
  <chain_facts>
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    11
  obtain x where "A x" <proof> ==
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    12
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    13
  have "!!thesis. (!!x. A x ==> thesis) ==> thesis"
12970
c9b1838a2cc0 improved messages;
wenzelm
parents: 12404
diff changeset
    14
  proof succeed
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    15
    fix thesis
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    16
    assume that [intro?]: "!!x. A x ==> thesis"
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    17
    <chain_facts>
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    18
    show thesis
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    19
      apply (insert that)
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    20
      <proof>
12970
c9b1838a2cc0 improved messages;
wenzelm
parents: 12404
diff changeset
    21
  qed
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    22
  fix x assm <<obtain_export>> "A x"
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    23
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    24
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    25
  <chain_facts>
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    26
  guess x <proof body> <proof end> ==
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    27
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    28
  {
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    29
    fix thesis
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    30
    <chain_facts> have "PROP ?guess"
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    31
      apply magic      -- {* turns goal into "thesis ==> #thesis" *}
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    32
      <proof body>
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    33
      apply_end magic  -- {* turns final "(!!x. P x ==> thesis) ==> #thesis" into
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    34
        "#((!!x. A x ==> thesis) ==> thesis)" which is a finished goal state *}
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    35
      <proof end>
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    36
  }
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    37
  fix x assm <<obtain_export>> "A x"
8094
62b45a2e6ecb ObtainFun;
wenzelm
parents: 7923
diff changeset
    38
*)
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    39
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    40
signature OBTAIN =
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    41
sig
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
    42
  val obtain: string -> (string * string option * mixfix) list ->
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    43
    ((string * Attrib.src list) * (string * string list) list) list ->
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    44
    bool -> Proof.state -> Proof.state
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
    45
  val obtain_i: string -> (string * typ option * mixfix) list ->
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    46
    ((string * attribute list) * (term * term list) list) list ->
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    47
    bool -> Proof.state -> Proof.state
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    48
  val result: (Proof.context -> tactic) -> thm list -> Proof.context ->
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    49
    (cterm list * thm list) * Proof.context
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
    50
  val guess: (string * string option * mixfix) list -> bool -> Proof.state -> Proof.state
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
    51
  val guess_i: (string * typ option * mixfix) list -> bool -> Proof.state -> Proof.state
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    52
  val statement: (string * ((string * 'typ option) list * 'term list)) list ->
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    53
    (('typ, 'term, 'fact) Element.ctxt list *
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
    54
      ((string * Attrib.src list) * ('term * 'term list) list) list) *
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
    55
    (((string * Attrib.src list) * (term * term list) list) list -> Proof.context ->
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
    56
      (((string * Attrib.src list) * (term * term list) list) list * thm list) * Proof.context)
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    57
end;
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    58
10379
93630e0c5ae9 improved handling of "that": insert into goal, only declare as Pure "intro";
wenzelm
parents: 9481
diff changeset
    59
structure Obtain: OBTAIN =
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    60
struct
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
    61
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
    62
(** obtain_export **)
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
    63
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    64
(*
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    65
  [x, A x]
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    66
     :
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    67
     B
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    68
  --------
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
    69
     B
18870
020e242c02a0 tuned comments;
wenzelm
parents: 18769
diff changeset
    70
*)
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    71
fun obtain_export fix_ctxt rule xs _ As thm =
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    72
  let
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    73
    val thy = ProofContext.theory_of fix_ctxt;
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    74
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    75
    val vs = map (dest_Free o Thm.term_of) xs;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    76
    val bads = Term.fold_aterms (fn t as Free v =>
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    77
      if member (op =) vs v then insert (op aconv) t else I | _ => I) (Thm.prop_of thm) [];
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    78
    val _ = null bads orelse
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    79
      error ("Result contains obtained parameters: " ^
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    80
        space_implode " " (map (ProofContext.string_of_term fix_ctxt) bads));
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    81
    val _ = ObjectLogic.is_judgment thy (Thm.concl_of thm) orelse
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    82
      error "Conclusion in obtained context must be object-logic judgment";
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    83
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    84
    val ((_, [thm']), ctxt') = Variable.import true [thm] fix_ctxt;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    85
    val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm'));
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    86
  in
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    87
    ((Drule.implies_elim_list thm' (map Thm.assume prems)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    88
        |> Drule.implies_intr_list (map Drule.norm_hhf_cterm As)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    89
        |> Drule.forall_intr_list xs)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    90
      COMP rule)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    91
    |> Drule.implies_intr_list prems
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
    92
    |> singleton (Variable.export ctxt' fix_ctxt)
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    93
  end;
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    94
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    95
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
    96
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    97
(** obtain **)
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    98
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
    99
fun bind_judgment ctxt name =
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
   100
  let
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   101
    val (bind, ctxt') = ProofContext.bind_fixes [name] ctxt;
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
   102
    val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name);
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   103
  in ((v, t), ctxt') end;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   104
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   105
val thatN = "that";
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   106
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   107
local
8094
62b45a2e6ecb ObtainFun;
wenzelm
parents: 7923
diff changeset
   108
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   109
fun gen_obtain prep_att prep_vars prep_propp
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   110
    name raw_vars raw_asms int state =
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   111
  let
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
   112
    val _ = Proof.assert_forward_or_chain state;
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   113
    val thy = Proof.theory_of state;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   114
    val cert = Thm.cterm_of thy;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   115
    val ctxt = Proof.context_of state;
17357
ee2bdca144c7 tuned Isar proof elements;
wenzelm
parents: 17111
diff changeset
   116
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   117
8543
f54926bded7b handle general case: params and hyps of thesis;
wenzelm
parents: 8109
diff changeset
   118
    (*obtain vars*)
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
   119
    val (vars, vars_ctxt) = prep_vars raw_vars ctxt;
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
   120
    val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18643
diff changeset
   121
    val xs = map #1 vars;
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   122
8543
f54926bded7b handle general case: params and hyps of thesis;
wenzelm
parents: 8109
diff changeset
   123
    (*obtain asms*)
11890
28e42a90bea8 improved source arrangement of obtain;
wenzelm
parents: 11816
diff changeset
   124
    val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19300
diff changeset
   125
    val asm_props = maps (map fst) proppss;
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18670
diff changeset
   126
    val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
10464
b7b916a82dca tuned statement args;
wenzelm
parents: 10379
diff changeset
   127
19897
fe661eb3b0e7 ProofContext: moved variable operations to struct Variable;
wenzelm
parents: 19844
diff changeset
   128
    val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt;
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   129
12970
c9b1838a2cc0 improved messages;
wenzelm
parents: 12404
diff changeset
   130
    (*obtain statements*)
20085
c5d60752587f replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20004
diff changeset
   131
    val thesisN = Name.variant xs AutoBind.thesisN;
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   132
    val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN);
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
   133
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   134
    val asm_frees = fold Term.add_frees asm_props [];
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   135
    val parms = xs |> map (fn x =>
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   136
      let val x' = ProofContext.get_skolem fix_ctxt x
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   137
      in (x', the_default propT (AList.lookup (op =) asm_frees x')) end);
10582
49ebade930ea fixed binding of parameters;
wenzelm
parents: 10464
diff changeset
   138
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   139
    val that_name = if name = "" then thatN else name;
10582
49ebade930ea fixed binding of parameters;
wenzelm
parents: 10464
diff changeset
   140
    val that_prop =
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   141
      Term.list_all_free (parms, Logic.list_implies (asm_props, thesis))
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   142
      |> Library.curry Logic.list_rename_params xs;
12970
c9b1838a2cc0 improved messages;
wenzelm
parents: 12404
diff changeset
   143
    val obtain_prop =
c9b1838a2cc0 improved messages;
wenzelm
parents: 12404
diff changeset
   144
      Logic.list_rename_params ([AutoBind.thesisN],
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   145
        Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   146
18124
a310c78298f9 simplified after_qed;
wenzelm
parents: 18040
diff changeset
   147
    fun after_qed _ =
17357
ee2bdca144c7 tuned Isar proof elements;
wenzelm
parents: 17111
diff changeset
   148
      Proof.local_qed (NONE, false)
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   149
      #> Seq.map (`Proof.the_fact #-> (fn rule =>
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
   150
        Proof.fix_i (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars)
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   151
        #> Proof.assm_i (obtain_export fix_ctxt rule (map (cert o Free) parms)) asms));
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   152
  in
8094
62b45a2e6ecb ObtainFun;
wenzelm
parents: 7923
diff changeset
   153
    state
9468
9adbcf6375c1 turned into plain context element;
wenzelm
parents: 9293
diff changeset
   154
    |> Proof.enter_forward
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   155
    |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, [])])] int
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   156
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
   157
    |> Proof.fix_i [(thesisN, NONE, NoSyn)]
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   158
    |> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])]
16842
wenzelm
parents: 16787
diff changeset
   159
    |> `Proof.the_facts
17357
ee2bdca144c7 tuned Isar proof elements;
wenzelm
parents: 17111
diff changeset
   160
    ||> Proof.chain_facts chain_facts
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   161
    ||> Proof.show_i NONE after_qed [(("", []), [(thesis, [])])] false
18907
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   162
    |-> Proof.refine_insert
7674
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   163
  end;
99305245f6bd The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff changeset
   164
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   165
in
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   166
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18693
diff changeset
   167
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp;
17111
d2ea9c974570 prepare attributes here;
wenzelm
parents: 17034
diff changeset
   168
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
8094
62b45a2e6ecb ObtainFun;
wenzelm
parents: 7923
diff changeset
   169
62b45a2e6ecb ObtainFun;
wenzelm
parents: 7923
diff changeset
   170
end;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   171
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   172
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   173
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   174
(** tactical result **)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   175
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   176
fun check_result ctxt thesis th =
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   177
  (case Thm.prems_of th of
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   178
    [prem] =>
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   179
      if Thm.concl_of th aconv thesis andalso
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   180
        Logic.strip_assums_concl prem aconv thesis then th
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   181
      else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   182
  | [] => error "Goal solved -- nothing guessed."
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   183
  | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   184
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   185
fun result tac facts ctxt =
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   186
  let
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   187
    val thy = ProofContext.theory_of ctxt;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   188
    val cert = Thm.cterm_of thy;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   189
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   190
    val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt AutoBind.thesisN;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   191
    val rule =
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   192
      (case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   193
        NONE => raise THM ("Obtain.result: tactic failed", 0, facts)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   194
      | SOME th => check_result ctxt thesis (norm_hhf (Goal.conclude th)));
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   195
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   196
    val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   197
    val ((_, [rule']), ctxt') = Variable.import false [closed_rule] ctxt;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   198
    val obtain_rule = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   199
    val ((params, stmt), fix_ctxt) = Variable.focus (Thm.cprem_of obtain_rule 1) ctxt';
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   200
    val (prems, ctxt'') =
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   201
      Assumption.add_assms (obtain_export fix_ctxt obtain_rule params)
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   202
        (Drule.strip_imp_prems stmt) fix_ctxt;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   203
  in ((params, prems), ctxt'') end;
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   204
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   205
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   206
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   207
(** guess **)
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   208
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   209
local
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   210
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   211
fun unify_params vars thesis_var raw_rule ctxt =
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   212
  let
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18670
diff changeset
   213
    val thy = ProofContext.theory_of ctxt;
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   214
    val certT = Thm.ctyp_of thy;
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   215
    val cert = Thm.cterm_of thy;
17891
7a6c4d60a913 tuned error msg;
wenzelm
parents: 17858
diff changeset
   216
    val string_of_typ = ProofContext.string_of_typ ctxt;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   217
    val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
17891
7a6c4d60a913 tuned error msg;
wenzelm
parents: 17858
diff changeset
   218
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18670
diff changeset
   219
    fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   220
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   221
    val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1;
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   222
    val rule = Thm.incr_indexes (maxidx + 1) raw_rule;
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   223
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   224
    val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   225
    val m = length vars;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   226
    val n = length params;
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   227
    val _ = m <= n orelse err "More variables than parameters in obtained rule" rule;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   228
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   229
    fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max)
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   230
      handle Type.TUNIFY =>
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   231
        err ("Failed to unify variable " ^
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   232
          string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   233
          string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule;
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   234
    val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params))
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   235
      (Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule));
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   236
    val norm_type = Envir.norm_type tyenv;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   237
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   238
    val xs = map (apsnd norm_type o fst) vars;
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   239
    val ys = map (apsnd norm_type) (Library.drop (m, params));
20085
c5d60752587f replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 20004
diff changeset
   240
    val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys;
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   241
    val terms = map (Drule.mk_term o cert o Free) (xs @ ys');
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   242
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   243
    val instT =
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   244
      fold (Term.add_tvarsT o #2) params []
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   245
      |> map (TVar #> (fn T => (certT T, certT (norm_type T))));
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   246
    val closed_rule = rule
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   247
      |> Thm.forall_intr (cert (Free thesis_var))
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   248
      |> Thm.instantiate (instT, []);
17891
7a6c4d60a913 tuned error msg;
wenzelm
parents: 17858
diff changeset
   249
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   250
    val ((_, rule' :: terms'), ctxt') = Variable.import false (closed_rule :: terms) ctxt;
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   251
    val vars' =
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   252
      map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   253
      (map snd vars @ replicate (length ys) NoSyn);
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   254
    val rule'' = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   255
  in ((vars', rule''), ctxt') end;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   256
18693
8ae076ee5e19 guess: used fixed inferred_type of vars;
wenzelm
parents: 18678
diff changeset
   257
fun inferred_type (x, _, mx) ctxt =
18769
e90eb0bc0ddd ProofContext.inferred_param;
wenzelm
parents: 18728
diff changeset
   258
  let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   259
  in ((x, T, mx), ctxt') end;
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   260
20004
e6d3f2b031e6 guess: proper context for polymorphic parameters;
wenzelm
parents: 19978
diff changeset
   261
fun polymorphic ctxt vars =
19897
fe661eb3b0e7 ProofContext: moved variable operations to struct Variable;
wenzelm
parents: 19844
diff changeset
   262
  let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars))
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   263
  in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end;
18693
8ae076ee5e19 guess: used fixed inferred_type of vars;
wenzelm
parents: 18678
diff changeset
   264
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   265
fun gen_guess prep_vars raw_vars int state =
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   266
  let
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   267
    val _ = Proof.assert_forward_or_chain state;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   268
    val thy = Proof.theory_of state;
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   269
    val cert = Thm.cterm_of thy;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   270
    val ctxt = Proof.context_of state;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   271
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   272
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   273
    val (thesis_var, thesis) = #1 (bind_judgment ctxt AutoBind.thesisN);
20004
e6d3f2b031e6 guess: proper context for polymorphic parameters;
wenzelm
parents: 19978
diff changeset
   274
    val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   275
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   276
    fun guess_context raw_rule state' =
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   277
      let
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   278
        val ((parms, rule), ctxt') =
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   279
          unify_params vars thesis_var raw_rule (Proof.context_of state');
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   280
        val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt';
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   281
        val ts = map (bind o Free o #1) parms;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   282
        val ps = map dest_Free ts;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   283
        val asms =
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   284
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   285
          |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), []));
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   286
        val _ = not (null asms) orelse error "Trivial result -- nothing guessed";
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   287
      in
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   288
        state'
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   289
        |> Proof.map_context (K ctxt')
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   290
        |> Proof.fix_i (map (fn ((x, T), mx) => (x, SOME T, mx)) parms)
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   291
        |> `Proof.context_of |-> (fn fix_ctxt =>
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   292
            Proof.assm_i (obtain_export fix_ctxt rule (map cert ts)) [(("", []), asms)])
19978
df19a7876183 obtain_export: Thm.generalize;
wenzelm
parents: 19906
diff changeset
   293
        |> Proof.add_binds_i AutoBind.no_facts
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   294
      end;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   295
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   296
    val goal = Var (("guess", 0), propT);
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   297
    fun print_result ctxt' (k, [(s, [_, th])]) =
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   298
      ProofDisplay.print_results int ctxt' (k, [(s, [th])]);
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   299
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> norm_hhf #>
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   300
        (fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th))));
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   301
    fun after_qed [[_, res]] =
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   302
      Proof.end_block #> guess_context (check_result ctxt thesis res) #> Seq.single;
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   303
  in
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   304
    state
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   305
    |> Proof.enter_forward
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   306
    |> Proof.begin_block
19844
2c1fdc397ded fixes: include mixfix syntax;
wenzelm
parents: 19779
diff changeset
   307
    |> Proof.fix_i [(AutoBind.thesisN, NONE, NoSyn)]
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   308
    |> Proof.chain_facts chain_facts
19779
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   309
    |> Proof.local_goal print_result (K I) (apsnd (rpair I))
5c77dfb74c7b guess: more careful about local polymorphism;
wenzelm
parents: 19585
diff changeset
   310
      "guess" before_qed after_qed [(("", []), [Logic.mk_term goal, goal])]
20308
ddb7e7129481 added tactical result;
wenzelm
parents: 20224
diff changeset
   311
    |> Proof.refine (Method.primitive_text (K (Goal.init (cert thesis)))) |> Seq.hd
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   312
  end;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   313
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   314
in
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   315
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   316
val guess = gen_guess ProofContext.read_vars;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   317
val guess_i = gen_guess ProofContext.cert_vars;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   318
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   319
end;
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   320
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   321
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   322
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   323
(** statements with several cases **)
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   324
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   325
fun statement cases =
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   326
  let
18907
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   327
    val names =
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   328
      cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   329
    val elems = cases |> map (fn (_, (vars, _)) =>
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19300
diff changeset
   330
      Element.Constrains (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE)));
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   331
    val concl = cases |> map (fn (_, (_, props)) => (("", []), map (rpair []) props));
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   332
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   333
    fun mk_stmt stmt ctxt =
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   334
      let
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   335
        val thesis =
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   336
          ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
18907
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   337
        val atts = map Attrib.internal
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   338
          [RuleCases.consumes (~ (length cases)), RuleCases.case_names names];
f984f22f1cb4 Proof.refine_insert;
wenzelm
parents: 18897
diff changeset
   339
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   340
        fun assume_case ((name, (vars, _)), (_, propp)) ctxt' =
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   341
          let
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   342
            val xs = map fst vars;
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   343
            val props = map fst propp;
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   344
            val (parms, ctxt'') =
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   345
              ctxt'
19897
fe661eb3b0e7 ProofContext: moved variable operations to struct Variable;
wenzelm
parents: 19844
diff changeset
   346
              |> fold Variable.declare_term props
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   347
              |> fold_map ProofContext.inferred_param xs;
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   348
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   349
          in
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   350
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
20224
9c40a144ee0e moved basic assumption operations from structure ProofContext to Assumption;
wenzelm
parents: 20218
diff changeset
   351
            ctxt' |> ProofContext.add_assms_i Assumption.assume_export
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   352
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   353
            |>> (fn [(_, [th])] => th)
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   354
          end;
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   355
        val (ths, ctxt') = ctxt
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   356
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   357
          |> fold_map assume_case (cases ~~ stmt)
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   358
          |-> (fn ths => ProofContext.note_thmss_i [((thatN, []), [(ths, [])])] #> #2 #> pair ths);
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19482
diff changeset
   359
      in (([(("", atts), [(thesis, [])])], ths), ctxt') end;
18897
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   360
  in ((elems, concl), mk_stmt) end;
b31293969d4f obtain(_i): optional name for 'that';
wenzelm
parents: 18870
diff changeset
   361
17858
bc4db8cfd92f added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents: 17357
diff changeset
   362
end;