src/Pure/Isar/proof.ML
author Walther Neuper <walther.neuper@jku.at>
Thu, 17 Dec 2020 18:00:27 +0100
changeset 60138 209f8c177b5b
parent 60134 85ce6e27e130
child 60139 c3cb65678c47
permissions -rw-r--r--
step 4.4: call hierarchy up from Syntax.check_terms, partially
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     3
wenzelm@19000
     4
The Isar/VM proof language interpreter: maintains a structured flow of
wenzelm@19000
     5
context elements, goals, refinements, and facts.
wenzelm@5820
     6
*)
wenzelm@5820
     7
wenzelm@5820
     8
signature PROOF =
wenzelm@5820
     9
sig
wenzelm@33031
    10
  type context = Proof.context
wenzelm@23639
    11
  type method = Method.method
wenzelm@5820
    12
  type state
wenzelm@17359
    13
  val init: context -> state
wenzelm@17359
    14
  val level: state -> int
wenzelm@17359
    15
  val assert_bottom: bool -> state -> state
wenzelm@5820
    16
  val context_of: state -> context
wenzelm@5820
    17
  val theory_of: state -> theory
wenzelm@13377
    18
  val map_context: (context -> context) -> state -> state
bulwahn@40890
    19
  val map_context_result : (context -> 'a * context) -> state -> 'a * state
wenzelm@28278
    20
  val map_contexts: (context -> context) -> state -> state
wenzelm@38212
    21
  val propagate_ml_env: state -> state
wneuper@59324
    22
  val report_improper: state -> unit
wenzelm@6091
    23
  val the_facts: state -> thm list
wenzelm@9469
    24
  val the_fact: state -> thm
wenzelm@47945
    25
  val set_facts: thm list -> state -> state
wenzelm@47945
    26
  val reset_facts: state -> state
wneuper@59324
    27
  val improper_reset_facts: state -> state
wenzelm@6891
    28
  val assert_forward: state -> state
wenzelm@17359
    29
  val assert_chain: state -> state
wenzelm@9469
    30
  val assert_forward_or_chain: state -> state
wenzelm@5820
    31
  val assert_backward: state -> state
wenzelm@8206
    32
  val assert_no_chain: state -> state
wenzelm@5820
    33
  val enter_forward: state -> state
wneuper@59324
    34
  val enter_chain: state -> state
wneuper@59324
    35
  val enter_backward: state -> state
wneuper@59324
    36
  val using_facts: thm list -> state -> state
wneuper@59324
    37
  val pretty_state: state -> Pretty.T list
wneuper@59324
    38
  val refine: Method.text -> state -> state Seq.result Seq.seq
wneuper@59324
    39
  val refine_end: Method.text -> state -> state Seq.result Seq.seq
wneuper@59324
    40
  val refine_singleton: Method.text -> state -> state
wenzelm@18908
    41
  val refine_insert: thm list -> state -> state
wneuper@59324
    42
  val refine_primitive: (Proof.context -> thm -> thm) -> state -> state
wenzelm@33288
    43
  val raw_goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    44
  val goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    45
  val simple_goal: state -> {context: context, goal: thm}
wenzelm@36334
    46
  val let_bind: (term list * term) list -> state -> state
wenzelm@36334
    47
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36521
    48
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36521
    49
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36334
    50
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36334
    51
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wneuper@59324
    52
  val assm: Assumption.export -> (binding * typ option * mixfix) list ->
wneuper@59324
    53
    (term * term list) list list -> (Thm.binding * (term * term list) list) list ->
wneuper@59324
    54
    state -> state
wneuper@59324
    55
  val assm_cmd: Assumption.export -> (binding * string option * mixfix) list ->
wneuper@59324
    56
    (string * string list) list list -> (Attrib.binding * (string * string list) list) list ->
wneuper@59324
    57
    state -> state
wneuper@59324
    58
  val assume: (binding * typ option * mixfix) list ->
wneuper@59324
    59
    (term * term list) list list -> (Thm.binding * (term * term list) list) list ->
wneuper@59324
    60
    state -> state
wneuper@59324
    61
  val assume_cmd: (binding * string option * mixfix) list ->
wneuper@59324
    62
    (string * string list) list list -> (Attrib.binding * (string * string list) list) list ->
wneuper@59324
    63
    state -> state
wneuper@59324
    64
  val presume: (binding * typ option * mixfix) list ->
wneuper@59324
    65
    (term * term list) list list -> (Thm.binding * (term * term list) list) list ->
wneuper@59324
    66
    state -> state
wneuper@59324
    67
  val presume_cmd: (binding * string option * mixfix) list ->
wneuper@59324
    68
    (string * string list) list list -> (Attrib.binding * (string * string list) list) list ->
wneuper@59324
    69
    state -> state
wenzelm@17359
    70
  val chain: state -> state
wenzelm@17359
    71
  val chain_facts: thm list -> state -> state
wenzelm@36334
    72
  val note_thmss: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wneuper@59180
    73
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36334
    74
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wneuper@59180
    75
  val from_thmss_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36334
    76
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wneuper@59180
    77
  val with_thmss_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wneuper@59324
    78
  val supply: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wneuper@59324
    79
  val supply_cmd: (Attrib.binding * (Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36334
    80
  val using: ((thm list * attribute list) list) list -> state -> state
wneuper@59180
    81
  val using_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36334
    82
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wneuper@59180
    83
  val unfolding_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wneuper@59324
    84
  val case_: Thm.binding * ((string * Position.T) * binding option list) -> state -> state
wneuper@59324
    85
  val case_cmd: Attrib.binding * ((string * Position.T) * binding option list) -> state -> state
wneuper@59324
    86
  val define: (binding * typ option * mixfix) list ->
wneuper@59324
    87
    (binding * typ option * mixfix) list ->
wneuper@59324
    88
    (Thm.binding * (term * term list) list) list -> state -> state
wneuper@59324
    89
  val define_cmd: (binding * string option * mixfix) list ->
wneuper@59324
    90
    (binding * string option * mixfix) list ->
wneuper@59324
    91
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@17359
    92
  val begin_block: state -> state
wenzelm@17359
    93
  val next_block: state -> state
wenzelm@20309
    94
  val end_block: state -> state
wenzelm@50057
    95
  val begin_notepad: context -> state
wenzelm@50057
    96
  val end_notepad: state -> context
walther@59606
    97
  val is_notepad: state -> bool
walther@59606
    98
  val reset_notepad: state -> state
wneuper@59324
    99
  val proof: Method.text_range option -> state -> state Seq.result Seq.seq
wenzelm@50880
   100
  val defer: int -> state -> state
wenzelm@50880
   101
  val prefer: int -> state -> state
wneuper@59324
   102
  val apply: Method.text_range -> state -> state Seq.result Seq.seq
wneuper@59324
   103
  val apply_end: Method.text_range -> state -> state Seq.result Seq.seq
wenzelm@50904
   104
  val local_qed: Method.text_range option * bool -> state -> state
wenzelm@21442
   105
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36334
   106
    (term * term list) list list -> context -> state
wenzelm@36334
   107
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
   108
    (string * string list) list list -> context -> state
wenzelm@50904
   109
  val global_qed: Method.text_range option * bool -> state -> context
wneuper@59451
   110
  val schematic_goal: state -> bool
wneuper@59451
   111
  val is_relevant: state -> bool
wenzelm@50904
   112
  val local_terminal_proof: Method.text_range * Method.text_range option -> state -> state
wenzelm@29383
   113
  val local_default_proof: state -> state
wenzelm@29383
   114
  val local_immediate_proof: state -> state
wenzelm@29383
   115
  val local_skip_proof: bool -> state -> state
wenzelm@29383
   116
  val local_done_proof: state -> state
wenzelm@50904
   117
  val global_terminal_proof: Method.text_range * Method.text_range option -> state -> context
wenzelm@20363
   118
  val global_default_proof: state -> context
wenzelm@20363
   119
  val global_immediate_proof: state -> context
wenzelm@29383
   120
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   121
  val global_done_proof: state -> context
wneuper@59324
   122
  val internal_goal: (context -> (string * string) * (string * thm list) list -> unit) ->
wneuper@59324
   123
    Proof_Context.mode -> bool -> string -> Method.text option ->
wneuper@59324
   124
    (context * thm list list -> state -> state) ->
wneuper@59324
   125
    (binding * typ option * mixfix) list ->
wneuper@59324
   126
    (Thm.binding * (term * term list) list) list ->
wneuper@59324
   127
    (Thm.binding * (term * term list) list) list -> state -> thm list * state
wneuper@59324
   128
  val have: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wneuper@59324
   129
    (binding * typ option * mixfix) list ->
wneuper@59324
   130
    (Thm.binding * (term * term list) list) list ->
wneuper@59324
   131
    (Thm.binding * (term * term list) list) list -> bool -> state -> thm list * state
wneuper@59324
   132
  val have_cmd: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wneuper@59324
   133
    (binding * string option * mixfix) list ->
wneuper@59324
   134
    (Attrib.binding * (string * string list) list) list ->
wneuper@59324
   135
    (Attrib.binding * (string * string list) list) list -> bool -> state -> thm list * state
wneuper@59324
   136
  val show: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wneuper@59324
   137
    (binding * typ option * mixfix) list ->
wneuper@59324
   138
    (Thm.binding * (term * term list) list) list ->
wneuper@59324
   139
    (Thm.binding * (term * term list) list) list -> bool -> state -> thm list * state
wneuper@59324
   140
  val show_cmd: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wneuper@59324
   141
    (binding * string option * mixfix) list ->
wneuper@59324
   142
    (Attrib.binding * (string * string list) list) list ->
wneuper@59324
   143
    (Attrib.binding * (string * string list) list) list -> bool -> state -> thm list * state
wenzelm@52455
   144
  val future_proof: (state -> ('a * context) future) -> state -> 'a future * state
wneuper@59324
   145
  val local_future_terminal_proof: Method.text_range * Method.text_range option -> state -> state
wneuper@59324
   146
  val global_future_terminal_proof: Method.text_range * Method.text_range option -> state -> context
wenzelm@5820
   147
end;
wenzelm@5820
   148
walther@60138
   149
structure Proof(**): PROOF(**) =
wenzelm@5820
   150
struct
wenzelm@5820
   151
wenzelm@33031
   152
type context = Proof.context;
wenzelm@17112
   153
type method = Method.method;
wenzelm@16813
   154
wenzelm@5820
   155
wenzelm@5820
   156
(** proof state **)
wenzelm@5820
   157
wenzelm@17359
   158
(* datatype state *)
wenzelm@5820
   159
wenzelm@17112
   160
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   161
wenzelm@17359
   162
datatype state =
wenzelm@17359
   163
  State of node Stack.T
wenzelm@17359
   164
and node =
wenzelm@7176
   165
  Node of
wenzelm@7176
   166
   {context: context,
wneuper@59324
   167
    facts: (thm list * bool) option,
wenzelm@7176
   168
    mode: mode,
wenzelm@17359
   169
    goal: goal option}
wenzelm@17359
   170
and goal =
wenzelm@17359
   171
  Goal of
wenzelm@29346
   172
   {statement: (string * Position.T) * term list list * term,
wenzelm@28410
   173
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   174
    using: thm list,                      (*goal facts*)
wneuper@59451
   175
    goal: thm,                            (*subgoals \<Longrightarrow> statement*)
wenzelm@17859
   176
    before_qed: Method.text option,
wenzelm@18124
   177
    after_qed:
wneuper@59324
   178
      (context * thm list list -> state -> state) *
wneuper@59324
   179
      (context * thm list list -> context -> context)};
wneuper@59324
   180
wneuper@59324
   181
val _ =
wneuper@59324
   182
  ML_system_pp (fn _ => fn _ => fn _: state =>
wneuper@59324
   183
    Pretty.to_polyml (Pretty.str "<Proof.state>"));
wenzelm@17359
   184
wneuper@59180
   185
fun make_goal (statement, using, goal, before_qed, after_qed) =
wneuper@59180
   186
  Goal {statement = statement, using = using, goal = goal,
wenzelm@17859
   187
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   188
wenzelm@7176
   189
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   190
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   191
wenzelm@17359
   192
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   193
  make_node (f (context, facts, mode, goal));
wenzelm@7176
   194
wenzelm@21727
   195
val init_context =
wenzelm@43231
   196
  Proof_Context.set_stmt true #>
wenzelm@47876
   197
  Proof_Context.map_naming (K Name_Space.local_naming);
wenzelm@21727
   198
wenzelm@21466
   199
fun init ctxt =
wenzelm@21727
   200
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   201
wneuper@59180
   202
fun top (State stack) = Stack.top stack |> (fn Node node => node);
wneuper@59180
   203
fun map_top f (State stack) = State (Stack.map_top (map_node f) stack);
wneuper@59180
   204
fun map_all f (State stack) = State (Stack.map_all (map_node f) stack);
wenzelm@12045
   205
wenzelm@5820
   206
wenzelm@5820
   207
wenzelm@5820
   208
(** basic proof state operations **)
wenzelm@5820
   209
wenzelm@17359
   210
(* block structure *)
wenzelm@17359
   211
wneuper@59180
   212
fun open_block (State stack) = State (Stack.push stack);
wenzelm@17359
   213
wneuper@59180
   214
fun close_block (State stack) = State (Stack.pop stack)
wenzelm@47937
   215
  handle List.Empty => error "Unbalanced block parentheses";
wenzelm@17359
   216
wneuper@59180
   217
fun level (State stack) = Stack.level stack;
wenzelm@17359
   218
wenzelm@17359
   219
fun assert_bottom b state =
wenzelm@47942
   220
  let val b' = level state <= 2 in
wenzelm@47942
   221
    if b andalso not b' then error "Not at bottom of proof"
wenzelm@47942
   222
    else if not b andalso b' then error "Already at bottom of proof"
wenzelm@17359
   223
    else state
wenzelm@17359
   224
  end;
wenzelm@17359
   225
wenzelm@17359
   226
wenzelm@5820
   227
(* context *)
wenzelm@5820
   228
wenzelm@50026
   229
val context_of = #context o top;
wenzelm@43231
   230
val theory_of = Proof_Context.theory_of o context_of;
wenzelm@5820
   231
wenzelm@17359
   232
fun map_context f =
wenzelm@50026
   233
  map_top (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   234
wenzelm@17359
   235
fun map_context_result f state =
wenzelm@17859
   236
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   237
wenzelm@28278
   238
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   239
wenzelm@38212
   240
fun propagate_ml_env state = map_contexts
walther@59606
   241
  (Context.proof_map (ML_Env.inherit [Context.Proof (context_of state)])) state;
wenzelm@38212
   242
wenzelm@5820
   243
wenzelm@5820
   244
(* facts *)
wenzelm@5820
   245
wneuper@59324
   246
fun report_improper state =
wneuper@59324
   247
  Context_Position.report (context_of state) (Position.thread_data ()) Markup.improper;
wneuper@59324
   248
wenzelm@50026
   249
val get_facts = #facts o top;
wenzelm@17359
   250
wenzelm@17359
   251
fun the_facts state =
wneuper@59324
   252
  (case get_facts state of
wneuper@59324
   253
    SOME (facts, proper) => (if proper then () else report_improper state; facts)
wenzelm@18678
   254
  | NONE => error "No current facts available");
wenzelm@5820
   255
wenzelm@9469
   256
fun the_fact state =
wneuper@59324
   257
  (case the_facts state of
wneuper@59324
   258
    [thm] => thm
wenzelm@18678
   259
  | _ => error "Single theorem expected");
wenzelm@9469
   260
wneuper@59324
   261
fun put_facts index facts =
wenzelm@50026
   262
  map_top (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wneuper@59324
   263
  map_context (Proof_Context.put_thms index (Auto_Bind.thisN, Option.map #1 facts));
wenzelm@6848
   264
wneuper@59324
   265
fun set_facts thms = put_facts false (SOME (thms, true));
wneuper@59324
   266
val reset_facts = put_facts false NONE;
wneuper@59324
   267
wneuper@59324
   268
fun improper_reset_facts state =
wneuper@59324
   269
  (case get_facts state of
wneuper@59324
   270
    SOME (thms, _) => put_facts false (SOME (thms, false)) state
wneuper@59324
   271
  | NONE => state);
wenzelm@47945
   272
wenzelm@17359
   273
fun these_factss more_facts (named_factss, state) =
wenzelm@47945
   274
  (named_factss, state |> set_facts (maps snd named_factss @ more_facts));
wenzelm@7605
   275
wenzelm@17359
   276
fun export_facts inner outer =
wenzelm@17359
   277
  (case get_facts inner of
wenzelm@47945
   278
    NONE => reset_facts outer
wneuper@59324
   279
  | SOME (thms, proper) =>
wneuper@59324
   280
      let val thms' = Proof_Context.export (context_of inner) (context_of outer) thms
wneuper@59324
   281
      in put_facts true (SOME (thms', proper)) outer end);
wenzelm@5820
   282
wenzelm@5820
   283
wenzelm@5820
   284
(* mode *)
wenzelm@5820
   285
wenzelm@50026
   286
val get_mode = #mode o top;
wenzelm@50026
   287
fun put_mode mode = map_top (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   288
wenzelm@17359
   289
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   290
wenzelm@5820
   291
fun assert_mode pred state =
wenzelm@5820
   292
  let val mode = get_mode state in
wenzelm@5820
   293
    if pred mode then state
wenzelm@18678
   294
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   295
  end;
wenzelm@5820
   296
wenzelm@19308
   297
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   298
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   299
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   300
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   301
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   302
wenzelm@17359
   303
val enter_forward = put_mode Forward;
wenzelm@17359
   304
val enter_chain = put_mode Chain;
wenzelm@17359
   305
val enter_backward = put_mode Backward;
wenzelm@5820
   306
wenzelm@5820
   307
wenzelm@17359
   308
(* current goal *)
wenzelm@6776
   309
wenzelm@17359
   310
fun current_goal state =
wenzelm@50026
   311
  (case top state of
wneuper@59324
   312
    {context = ctxt, goal = SOME (Goal goal), ...} => (ctxt, goal)
wenzelm@47942
   313
  | _ => error "No current goal");
wenzelm@5820
   314
wenzelm@17359
   315
fun assert_current_goal g state =
wenzelm@17359
   316
  let val g' = can current_goal state in
wenzelm@47942
   317
    if g andalso not g' then error "No goal in this block"
wenzelm@47942
   318
    else if not g andalso g' then error "Goal present in this block"
wenzelm@17359
   319
    else state
wenzelm@17359
   320
  end;
wenzelm@5820
   321
wneuper@59324
   322
fun put_goal goal = map_top (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@17359
   323
wenzelm@47945
   324
val set_goal = put_goal o SOME;
wenzelm@47945
   325
val reset_goal = put_goal NONE;
wenzelm@47945
   326
wenzelm@17859
   327
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   328
wenzelm@17359
   329
wenzelm@17359
   330
(* nested goal *)
wenzelm@17359
   331
wneuper@59324
   332
fun map_goal f (State stack) =
wneuper@59180
   333
  (case Stack.dest stack of
wneuper@59324
   334
    (Node {context = ctxt, facts, mode, goal = SOME goal}, node :: nodes) =>
wenzelm@17359
   335
      let
wneuper@59180
   336
        val Goal {statement, using, goal, before_qed, after_qed} = goal;
wneuper@59324
   337
        val (ctxt', statement', using', goal', before_qed', after_qed') =
wneuper@59324
   338
          f (ctxt, statement, using, goal, before_qed, after_qed);
wneuper@59324
   339
        val goal' = make_goal (statement', using', goal', before_qed', after_qed');
wneuper@59324
   340
      in State (Stack.make (make_node (ctxt', facts, mode, SOME goal')) (node :: nodes)) end
wneuper@59324
   341
  | (top_node, node :: nodes) =>
wenzelm@48308
   342
      let
wneuper@59324
   343
        val State stack' = map_goal f (State (Stack.make node nodes));
wneuper@59180
   344
        val (node', nodes') = Stack.dest stack';
wneuper@59324
   345
      in State (Stack.make top_node (node' :: nodes')) end
wneuper@59180
   346
  | _ => State stack);
wenzelm@17359
   347
wneuper@59324
   348
fun provide_goal goal =
wneuper@59324
   349
  map_goal (fn (ctxt, statement, using, _, before_qed, after_qed) =>
wneuper@59324
   350
    (ctxt, statement, using, goal, before_qed, after_qed));
wenzelm@19188
   351
wneuper@59324
   352
fun using_facts using =
wneuper@59324
   353
  map_goal (fn (ctxt, statement, _, goal, before_qed, after_qed) =>
wneuper@59324
   354
    (ctxt, statement, using, goal, before_qed, after_qed));
wenzelm@17359
   355
wneuper@59324
   356
fun find_goal state =
wneuper@59324
   357
  (case try current_goal state of
wneuper@59324
   358
    SOME ctxt_goal => ctxt_goal
wneuper@59324
   359
  | NONE => find_goal (close_block state handle ERROR _ => error "No proof goal"));
wenzelm@17359
   360
wenzelm@17359
   361
fun get_goal state =
wneuper@59324
   362
  let val (ctxt, {using, goal, ...}) = find_goal state
wenzelm@17359
   363
  in (ctxt, (using, goal)) end;
wenzelm@5820
   364
wenzelm@5820
   365
wenzelm@5820
   366
wenzelm@12423
   367
(** pretty_state **)
wenzelm@5820
   368
wneuper@59180
   369
local
wenzelm@53778
   370
skalberg@15531
   371
fun pretty_facts _ _ NONE = []
wneuper@59180
   372
  | pretty_facts ctxt s (SOME ths) = [Proof_Display.pretty_goal_facts ctxt s ths];
wneuper@59180
   373
wneuper@59180
   374
fun pretty_sep prts [] = prts
wneuper@59180
   375
  | pretty_sep [] prts = prts
wneuper@59180
   376
  | pretty_sep prts1 prts2 = prts1 @ [Pretty.str ""] @ prts2;
wneuper@59180
   377
wneuper@59180
   378
in
wenzelm@6756
   379
wneuper@59324
   380
fun pretty_state state =
wenzelm@5820
   381
  let
wenzelm@50026
   382
    val {context = ctxt, facts, mode, goal = _} = top state;
wenzelm@43630
   383
    val verbose = Config.get ctxt Proof_Context.verbose;
wenzelm@5820
   384
wneuper@59324
   385
    fun prt_goal (SOME (_, {statement = _, using, goal, before_qed = _, after_qed = _})) =
wneuper@59180
   386
          pretty_sep
wneuper@59180
   387
            (pretty_facts ctxt "using"
wneuper@59180
   388
              (if mode <> Backward orelse null using then NONE else SOME using))
wneuper@59180
   389
            ([Proof_Display.pretty_goal_header goal] @ Goal_Display.pretty_goals ctxt goal)
wenzelm@17359
   390
      | prt_goal NONE = [];
wenzelm@6848
   391
wenzelm@17359
   392
    val prt_ctxt =
wenzelm@43630
   393
      if verbose orelse mode = Forward then Proof_Context.pretty_context ctxt
wenzelm@43231
   394
      else if mode = Backward then Proof_Context.pretty_ctxt ctxt
wenzelm@7575
   395
      else [];
wenzelm@58254
   396
wenzelm@58254
   397
    val position_markup = Position.markup (Position.thread_data ()) Markup.position;
walther@60134
   398
(** )val _ = @{print} {a = "### Proof.pretty_state:"};( *..NOT yet available*)
walther@60134
   399
(**)val _ = writeln "### Proof.pretty_state:";(*..NOT yet available*)
wenzelm@17359
   400
  in
wenzelm@58254
   401
    [Pretty.block
wneuper@59324
   402
      [Pretty.mark_str (position_markup, "proof"), Pretty.str (" (" ^ mode_name mode ^ ")")]] @
wenzelm@17359
   403
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@43630
   404
    (if verbose orelse mode = Forward then
wneuper@59324
   405
       pretty_sep (pretty_facts ctxt "" (Option.map #1 facts)) (prt_goal (try find_goal state))
wneuper@59324
   406
     else if mode = Chain then pretty_facts ctxt "picking" (Option.map #1 facts)
wenzelm@17359
   407
     else prt_goal (try find_goal state))
wenzelm@17359
   408
  end;
wenzelm@5820
   409
wneuper@59180
   410
end;
wneuper@59180
   411
wenzelm@5820
   412
wenzelm@5820
   413
wenzelm@5820
   414
(** proof steps **)
wenzelm@5820
   415
wenzelm@17359
   416
(* refine via method *)
wenzelm@5820
   417
wenzelm@8234
   418
local
wenzelm@8234
   419
wneuper@59180
   420
fun apply_method text ctxt state =
wneuper@59324
   421
  find_goal state |> (fn (goal_ctxt, {statement, using, goal, before_qed, after_qed}) =>
wneuper@59324
   422
    Method.evaluate text ctxt using (goal_ctxt, goal)
wneuper@59324
   423
    |> Seq.map_result (fn (goal_ctxt', goal') =>
walther@59606
   424
        state |> map_goal (K (goal_ctxt', statement, using, goal', before_qed, after_qed))));
wenzelm@17112
   425
wenzelm@8234
   426
in
wenzelm@8234
   427
wneuper@59180
   428
fun refine text state = apply_method text (context_of state) state;
wneuper@59180
   429
fun refine_end text state = apply_method text (#1 (find_goal state)) state;
wneuper@59180
   430
wneuper@59324
   431
fun refine_singleton text = refine text #> Seq.the_result "";
wneuper@59324
   432
wneuper@59180
   433
fun refine_insert ths =
wneuper@59324
   434
  refine_singleton (Method.Basic (K (Method.insert ths)));
wneuper@59324
   435
wneuper@59324
   436
fun refine_primitive r =
walther@60065
   437
  refine_singleton (Method.Basic (fn ctxt => fn _ => CONTEXT_TACTIC (PRIMITIVE (r ctxt))));
wenzelm@18908
   438
wenzelm@8234
   439
end;
wenzelm@8234
   440
wenzelm@5820
   441
wenzelm@17359
   442
(* refine via sub-proof *)
wenzelm@17359
   443
wenzelm@47342
   444
local
wenzelm@47342
   445
wenzelm@56084
   446
fun finish_tac _ 0 = K all_tac
wenzelm@56084
   447
  | finish_tac ctxt n =
wenzelm@56084
   448
      Goal.norm_hhf_tac ctxt THEN'
wenzelm@30559
   449
      SUBGOAL (fn (goal, i) =>
wenzelm@30559
   450
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wneuper@59180
   451
          eresolve_tac ctxt [Drule.protectI] i THEN finish_tac ctxt (n - 1) i
wenzelm@56084
   452
        else finish_tac ctxt (n - 1) (i + 1));
wenzelm@30559
   453
wenzelm@56084
   454
fun goal_tac ctxt rule =
wenzelm@56084
   455
  Goal.norm_hhf_tac ctxt THEN'
wneuper@59180
   456
  resolve_tac ctxt [rule] THEN'
wenzelm@56084
   457
  finish_tac ctxt (Thm.nprems_of rule);
wenzelm@11816
   458
wenzelm@47342
   459
fun FINDGOAL tac st =
wenzelm@47342
   460
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@47342
   461
  in find 1 (Thm.nprems_of st) st end;
wenzelm@47342
   462
wneuper@59324
   463
fun protect_prem i th =
wneuper@59324
   464
  Thm.bicompose NONE {flatten = false, match = false, incremented = true}
wneuper@59324
   465
    (false, Drule.incr_indexes th Drule.protectD, 1) i th
wneuper@59324
   466
  |> Seq.hd;
wneuper@59324
   467
wneuper@59324
   468
fun protect_prems th =
wneuper@59324
   469
  fold_rev protect_prem (1 upto Thm.nprems_of th) th;
wneuper@59324
   470
wenzelm@47342
   471
in
wenzelm@47342
   472
wneuper@59324
   473
fun refine_goals print_rule result_ctxt result state =
wenzelm@19915
   474
  let
wneuper@59324
   475
    val (goal_ctxt, (_, goal)) = get_goal state;
wneuper@59324
   476
    fun refine rule st =
wneuper@59324
   477
      (print_rule goal_ctxt rule; FINDGOAL (goal_tac goal_ctxt rule) st);
wenzelm@19915
   478
  in
wneuper@59324
   479
    result
wneuper@59324
   480
    |> map (Raw_Simplifier.norm_hhf result_ctxt #> protect_prems)
wneuper@59324
   481
    |> Proof_Context.goal_export result_ctxt goal_ctxt
wenzelm@47945
   482
    |> (fn rules => Seq.lift provide_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   483
  end;
wenzelm@6932
   484
wenzelm@47342
   485
end;
wenzelm@47342
   486
wenzelm@6932
   487
wenzelm@50861
   488
(* conclude goal *)
wenzelm@50861
   489
wenzelm@28627
   490
fun conclude_goal ctxt goal propss =
wenzelm@5820
   491
  let
wenzelm@43231
   492
    val thy = Proof_Context.theory_of ctxt;
wenzelm@5820
   493
wenzelm@56323
   494
    val _ =
wneuper@59324
   495
      Context.subthy_id (Thm.theory_id goal, Context.theory_id thy) orelse
wneuper@59180
   496
        error "Bad background theory of goal state";
wenzelm@50875
   497
    val _ = Thm.no_prems goal orelse error (Proof_Display.string_of_goal ctxt goal);
wenzelm@20224
   498
wneuper@59324
   499
    fun err_lost () = error ("Lost goal structure:\n" ^ Thm.string_of_thm ctxt goal);
wenzelm@5820
   500
wenzelm@55940
   501
    val th =
walther@60065
   502
      (Goal.conclude (Thm.close_derivation \<^here> goal) handle THM _ => err_lost ())
wneuper@59180
   503
      |> Drule.flexflex_unique (SOME ctxt)
wneuper@59324
   504
      |> Thm.check_shyps ctxt
wenzelm@56335
   505
      |> Thm.check_hyps (Context.Proof ctxt);
wenzelm@23418
   506
wenzelm@23418
   507
    val goal_propss = filter_out null propss;
wenzelm@23418
   508
    val results =
wenzelm@23418
   509
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   510
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wneuper@59324
   511
      handle THM _ => err_lost ();
wneuper@59180
   512
    val _ =
walther@60065
   513
      Unify.matches_list (Context.Proof ctxt)
walther@60065
   514
        (map (Soft_Type_System.purge ctxt) (flat goal_propss)) (map Thm.prop_of (flat results))
wneuper@59324
   515
        orelse error ("Proved a different theorem:\n" ^ Thm.string_of_thm ctxt th);
wenzelm@23418
   516
wenzelm@23418
   517
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   518
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   519
      | recover_result [] [] = []
wneuper@59324
   520
      | recover_result _ _ = err_lost ();
wenzelm@23418
   521
  in recover_result propss results end;
wenzelm@5820
   522
wenzelm@50874
   523
val finished_goal_error = "Failed to finish proof";
wenzelm@50874
   524
wenzelm@50904
   525
fun finished_goal pos state =
wenzelm@50874
   526
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@50874
   527
    if Thm.no_prems goal then Seq.Result state
wenzelm@50904
   528
    else
wenzelm@50904
   529
      Seq.Error (fn () =>
wenzelm@50904
   530
        finished_goal_error ^ Position.here pos ^ ":\n" ^
wenzelm@50904
   531
          Proof_Display.string_of_goal ctxt goal)
wenzelm@50874
   532
  end;
wenzelm@50861
   533
wenzelm@5820
   534
wenzelm@33288
   535
(* goal views -- corresponding to methods *)
wenzelm@33288
   536
wenzelm@33288
   537
fun raw_goal state =
wneuper@59324
   538
  let val (ctxt, (using, goal)) = get_goal state
wneuper@59324
   539
  in {context = ctxt, facts = using, goal = goal} end;
wenzelm@33288
   540
wenzelm@33288
   541
val goal = raw_goal o refine_insert [];
wenzelm@33288
   542
wenzelm@33288
   543
fun simple_goal state =
wenzelm@33288
   544
  let
wneuper@59324
   545
    val (_, (using, _)) = get_goal state;
wneuper@59324
   546
    val (ctxt, (_, goal)) = get_goal (refine_insert using state);
wenzelm@33288
   547
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   548
wenzelm@50881
   549
fun method_error kind pos state =
wenzelm@50881
   550
  Seq.single (Proof_Display.method_error kind pos (raw_goal state));
wenzelm@50876
   551
wenzelm@33288
   552
wenzelm@5820
   553
wenzelm@5820
   554
(*** structured proof commands ***)
wenzelm@5820
   555
wenzelm@17112
   556
(** context elements **)
wenzelm@5820
   557
wenzelm@36335
   558
(* let bindings *)
wenzelm@5820
   559
walther@60138
   560
(**)local(**)
wenzelm@16813
   561
walther@60065
   562
fun gen_bind prep_terms raw_binds =
walther@60065
   563
  assert_forward #> map_context (fn ctxt =>
walther@60065
   564
    let
walther@60065
   565
      fun prep_bind (raw_pats, t) ctxt1 =
walther@60065
   566
        let
walther@60065
   567
          val T = Term.fastype_of t;
walther@60065
   568
          val ctxt2 = Variable.declare_term t ctxt1;
walther@60065
   569
          val pats = prep_terms (Proof_Context.set_mode Proof_Context.mode_pattern ctxt2) T raw_pats;
walther@60065
   570
          val binds = Proof_Context.simult_matches ctxt2 (t, pats);
walther@60065
   571
        in (binds, ctxt2) end;
walther@60065
   572
walther@60065
   573
      val ts = prep_terms ctxt dummyT (map snd raw_binds);
walther@60065
   574
      val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
walther@60065
   575
      val binds' = map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds);
walther@60065
   576
walther@60065
   577
      val ctxt'' =
walther@60065
   578
        ctxt
walther@60065
   579
        |> fold Variable.declare_term (map #2 binds')
walther@60065
   580
        |> fold Proof_Context.bind_term binds';
walther@60065
   581
      val _ = Variable.warn_extra_tfrees ctxt ctxt'';
walther@60065
   582
    in ctxt'' end)
walther@60065
   583
  #> reset_facts;
walther@60065
   584
walther@60138
   585
(*val read_terms: context -> typ -> string list -> term list*)
walther@60065
   586
fun read_terms ctxt T =
walther@60065
   587
  map (Syntax.parse_term ctxt #> Type.constraint T) #> Syntax.check_terms ctxt;
wenzelm@5820
   588
walther@60138
   589
(**)in(**)
wenzelm@16813
   590
walther@60138
   591
(*val let_bind: (term list * term) list -> state -> state*)
walther@60138
   592
val let_bind =
walther@60138
   593
  (writeln "#### Proof.let_bind";
walther@60138
   594
    gen_bind (fn ctxt => fn _ => map (Proof_Context.cert_term ctxt)));
walther@60138
   595
(*val let_bind_cmd: (string list * string) list -> state -> state*)
walther@60138
   596
val let_bind_cmd =
walther@60138
   597
  (writeln "#### Proof.let_bind_cmd";
walther@60138
   598
    gen_bind read_terms);
wenzelm@5820
   599
walther@60138
   600
(**)end;(**)
wenzelm@16813
   601
wenzelm@5820
   602
wenzelm@36521
   603
(* concrete syntax *)
wenzelm@36521
   604
wenzelm@36521
   605
local
wenzelm@36521
   606
wneuper@59324
   607
fun read_arg (c, mx) ctxt =
wneuper@59324
   608
  (case Proof_Context.read_const {proper = false, strict = false} ctxt c of
wneuper@59324
   609
    Free (x, _) =>
wneuper@59324
   610
      let
wneuper@59324
   611
        val ctxt' =
wneuper@59324
   612
          ctxt |> is_none (Variable.default_type ctxt x) ?
wneuper@59324
   613
            Variable.declare_constraints (Free (x, Mixfix.default_constraint mx));
wneuper@59324
   614
        val t = Free (#1 (Proof_Context.inferred_param x ctxt'));
wneuper@59324
   615
      in ((t, mx), ctxt') end
wneuper@59324
   616
  | t => ((t, mx), ctxt));
wneuper@59324
   617
wenzelm@36523
   618
fun gen_write prep_arg mode args =
wenzelm@36523
   619
  assert_forward
wneuper@59324
   620
  #> map_context (fold_map prep_arg args #-> Proof_Context.notation true mode)
wenzelm@47945
   621
  #> reset_facts;
wenzelm@36521
   622
wenzelm@36521
   623
in
wenzelm@36521
   624
wneuper@59324
   625
val write = gen_write pair;
wenzelm@57297
   626
val write_cmd = gen_write read_arg;
wenzelm@36521
   627
wenzelm@36521
   628
end;
wenzelm@36521
   629
wenzelm@36521
   630
wenzelm@17359
   631
(* fix *)
wenzelm@5820
   632
wenzelm@12714
   633
local
wenzelm@12714
   634
wneuper@59324
   635
fun gen_fix add_fixes raw_fixes =
wenzelm@17359
   636
  assert_forward
wneuper@59324
   637
  #> map_context (snd o add_fixes raw_fixes)
wenzelm@47945
   638
  #> reset_facts;
wenzelm@5820
   639
wenzelm@16813
   640
in
wenzelm@16813
   641
wneuper@59324
   642
val fix = gen_fix Proof_Context.add_fixes;
wneuper@59324
   643
val fix_cmd = gen_fix Proof_Context.add_fixes_cmd;
wenzelm@5820
   644
wenzelm@16813
   645
end;
wenzelm@16813
   646
wenzelm@5820
   647
wneuper@59324
   648
(* assume *)
wenzelm@5820
   649
wenzelm@16813
   650
local
wenzelm@16813
   651
wneuper@59324
   652
fun gen_assume prep_statement prep_att export raw_fixes raw_prems raw_concls state =
wneuper@59324
   653
  let
wneuper@59324
   654
    val ctxt = context_of state;
wneuper@59324
   655
wneuper@59324
   656
    val bindings = map (apsnd (map (prep_att ctxt)) o fst) raw_concls;
walther@60065
   657
    val {fixes = params, assumes = prems_propss, shows = concl_propss, result_binds, text, ...} =
walther@60065
   658
      #1 (prep_statement raw_fixes raw_prems (map snd raw_concls) ctxt);
wneuper@59324
   659
    val propss = (map o map) (Logic.close_prop params (flat prems_propss)) concl_propss;
wneuper@59324
   660
  in
wneuper@59324
   661
    state
wneuper@59324
   662
    |> assert_forward
wneuper@59324
   663
    |> map_context_result (fn ctxt =>
wneuper@59324
   664
      ctxt
walther@60065
   665
      |> Proof_Context.augment text
wneuper@59324
   666
      |> fold Variable.maybe_bind_term result_binds
wneuper@59324
   667
      |> fold_burrow (Assumption.add_assms export o map (Thm.cterm_of ctxt)) propss
wneuper@59324
   668
      |-> (fn premss =>
wneuper@59324
   669
        Proof_Context.note_thmss "" (bindings ~~ (map o map) (fn th => ([th], [])) premss)))
wneuper@59324
   670
    |> these_factss [] |> #2
wneuper@59324
   671
  end;
wenzelm@6932
   672
wenzelm@16813
   673
in
wenzelm@16813
   674
wneuper@59324
   675
val assm = gen_assume Proof_Context.cert_statement (K I);
wneuper@59324
   676
val assm_cmd = gen_assume Proof_Context.read_statement Attrib.attribute_cmd;
wneuper@59324
   677
wenzelm@36334
   678
val assume = assm Assumption.assume_export;
wenzelm@36334
   679
val assume_cmd = assm_cmd Assumption.assume_export;
wneuper@59324
   680
wenzelm@36334
   681
val presume = assm Assumption.presume_export;
wenzelm@36334
   682
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   683
wenzelm@16813
   684
end;
wenzelm@16813
   685
wenzelm@7271
   686
wenzelm@8374
   687
wenzelm@17112
   688
(** facts **)
wenzelm@5820
   689
wenzelm@17359
   690
(* chain *)
wenzelm@5820
   691
wenzelm@17359
   692
val chain =
wenzelm@17359
   693
  assert_forward
wneuper@59324
   694
  #> (fn state => set_facts (Method.clean_facts (the_facts state)) state)
wenzelm@17359
   695
  #> enter_chain;
wenzelm@5820
   696
wenzelm@17359
   697
fun chain_facts facts =
wenzelm@47945
   698
  set_facts facts
wenzelm@17359
   699
  #> chain;
wenzelm@5820
   700
wenzelm@5820
   701
wenzelm@17359
   702
(* note etc. *)
wenzelm@17112
   703
wneuper@59324
   704
fun empty_bindings args = map (pair Binding.empty_atts) args;
wenzelm@17112
   705
wenzelm@17112
   706
local
wenzelm@17112
   707
wenzelm@30767
   708
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   709
  state
wenzelm@17112
   710
  |> assert_forward
wenzelm@48686
   711
  |> map_context_result (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@48686
   712
    (Attrib.map_facts_refs (map (prep_atts ctxt)) (prep_fact ctxt) args))
wenzelm@17112
   713
  |> these_factss (more_facts state)
wenzelm@17359
   714
  ||> opt_chain
wenzelm@17359
   715
  |> opt_result;
wenzelm@17112
   716
wenzelm@17112
   717
in
wenzelm@17112
   718
wenzelm@36334
   719
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@48686
   720
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   721
wneuper@59324
   722
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o empty_bindings;
wenzelm@48686
   723
val from_thmss_cmd =
wneuper@59324
   724
  gen_thmss (K []) chain #2 Attrib.attribute_cmd Proof_Context.get_fact o empty_bindings;
wenzelm@17112
   725
wneuper@59324
   726
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o empty_bindings;
wenzelm@48686
   727
val with_thmss_cmd =
wneuper@59324
   728
  gen_thmss the_facts chain #2 Attrib.attribute_cmd Proof_Context.get_fact o empty_bindings;
wenzelm@17359
   729
wenzelm@30767
   730
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   731
wenzelm@17112
   732
end;
wenzelm@17112
   733
wenzelm@17112
   734
wneuper@59324
   735
(* facts during goal refinement *)
wneuper@59324
   736
wneuper@59324
   737
local
wneuper@59324
   738
wneuper@59324
   739
fun gen_supply prep_att prep_fact args state =
wneuper@59324
   740
  state
wneuper@59324
   741
  |> assert_backward
wneuper@59324
   742
  |> map_context (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wneuper@59324
   743
       (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) args) |> snd);
wneuper@59324
   744
wneuper@59324
   745
in
wneuper@59324
   746
wneuper@59324
   747
val supply = gen_supply (K I) (K I);
wneuper@59324
   748
val supply_cmd = gen_supply Attrib.attribute_cmd Proof_Context.get_fact;
wneuper@59324
   749
wneuper@59324
   750
end;
wneuper@59324
   751
wneuper@59324
   752
wenzelm@18548
   753
(* using/unfolding *)
wenzelm@17112
   754
wenzelm@17112
   755
local
wenzelm@17112
   756
wenzelm@46261
   757
fun gen_using f g prep_att prep_fact args state =
wenzelm@17112
   758
  state
wenzelm@17112
   759
  |> assert_backward
wenzelm@21442
   760
  |> map_context_result
wenzelm@48686
   761
    (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wneuper@59324
   762
      (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) (empty_bindings args)))
wenzelm@18843
   763
  |> (fn (named_facts, state') =>
wneuper@59324
   764
    state' |> map_goal (fn (goal_ctxt, statement, using, goal, before_qed, after_qed) =>
wenzelm@18843
   765
      let
wenzelm@18843
   766
        val ctxt = context_of state';
wenzelm@19482
   767
        val ths = maps snd named_facts;
wneuper@59324
   768
      in (goal_ctxt, statement, f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   769
wenzelm@24050
   770
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   771
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   772
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   773
wenzelm@17112
   774
in
wenzelm@17112
   775
wenzelm@36334
   776
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@48686
   777
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@36334
   778
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@48686
   779
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   780
wenzelm@17112
   781
end;
wenzelm@17112
   782
wenzelm@17112
   783
wenzelm@17112
   784
(* case *)
wenzelm@17112
   785
wenzelm@17112
   786
local
wenzelm@17112
   787
wneuper@59324
   788
fun gen_case internal prep_att ((raw_binding, raw_atts), ((name, pos), xs)) state =
wenzelm@17112
   789
  let
wneuper@59324
   790
    val ctxt = context_of state;
wneuper@59324
   791
wneuper@59324
   792
    val binding = if Binding.is_empty raw_binding then Binding.make (name, pos) else raw_binding;
wneuper@59324
   793
    val atts = map (prep_att ctxt) raw_atts;
wneuper@59324
   794
wenzelm@19078
   795
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@58828
   796
      ctxt |> Proof_Context.apply_case (Proof_Context.check_case ctxt internal (name, pos) xs));
wenzelm@54517
   797
    val assumptions =
wneuper@59324
   798
      asms |> map (fn (a, ts) => ((Binding.qualify_name true binding a, []), map (rpair []) ts));
wenzelm@17112
   799
  in
wenzelm@17112
   800
    state'
wneuper@59324
   801
    |> assume [] [] assumptions
wneuper@59324
   802
    |> map_context (fold Variable.unbind_term Auto_Bind.no_facts)
wneuper@59324
   803
    |> `the_facts |-> (fn thms => note_thmss [((binding, atts), [(thms, [])])])
wenzelm@17112
   804
  end;
wenzelm@17112
   805
wenzelm@17112
   806
in
wenzelm@17112
   807
wneuper@59324
   808
val case_ = gen_case true (K I);
wneuper@59324
   809
val case_cmd = gen_case false Attrib.attribute_cmd;
wneuper@59324
   810
wneuper@59324
   811
end;
wneuper@59324
   812
wneuper@59324
   813
wneuper@59324
   814
(* define *)
wneuper@59324
   815
wneuper@59324
   816
local
wneuper@59324
   817
wneuper@59324
   818
fun gen_define prep_stmt prep_att raw_decls raw_fixes raw_defs state =
wneuper@59324
   819
  let
wneuper@59324
   820
    val _ = assert_forward state;
wneuper@59324
   821
    val ctxt = context_of state;
wneuper@59324
   822
wneuper@59324
   823
    (*vars*)
walther@60065
   824
    val ({vars, propss, result_binds, ...}, vars_ctxt) =
wneuper@59324
   825
      prep_stmt (raw_decls @ raw_fixes) (map snd raw_defs) ctxt;
wneuper@59324
   826
    val (decls, fixes) = chop (length raw_decls) vars;
wneuper@59324
   827
    val show_term = Syntax.string_of_term vars_ctxt;
wneuper@59324
   828
wneuper@59324
   829
    (*defs*)
wneuper@59324
   830
    fun match_defs (((b, _, mx), (_, Free (x, T))) :: more_decls) ((((y, U), t), _) :: more_defs) =
wneuper@59324
   831
          if x = y then ((b, mx), (Binding.empty_atts, t)) :: match_defs more_decls more_defs
wneuper@59324
   832
          else
wneuper@59324
   833
            error ("Mismatch of declaration " ^ show_term (Free (x, T)) ^ " wrt. definition " ^
wneuper@59324
   834
              show_term (Free (y, U)))
wneuper@59324
   835
      | match_defs [] [] = []
wneuper@59324
   836
      | match_defs more_decls more_defs =
wneuper@59324
   837
          error ("Mismatch of declarations " ^ commas (map (show_term o #2 o #2) more_decls) ^
wneuper@59324
   838
            (if null more_decls then "" else " ") ^
wneuper@59324
   839
            "wrt. definitions " ^ commas (map (show_term o Free o #1 o #1) more_defs));
wneuper@59324
   840
wneuper@59324
   841
    val derived_def = Local_Defs.derived_def ctxt (K  []) {conditional = false};
wneuper@59324
   842
    val defs1 = map (derived_def o Logic.close_prop (map #2 fixes) []) (flat propss);
wneuper@59324
   843
    val defs2 = match_defs decls defs1;
wneuper@59324
   844
    val (defs3, defs_ctxt) = Local_Defs.define defs2 ctxt;
wneuper@59324
   845
wneuper@59324
   846
    (*notes*)
wneuper@59324
   847
    val def_thmss =
wneuper@59324
   848
      map (fn (((_, prove), ((b, _), _)), (_, (_, th))) => (b, prove defs_ctxt th))
wneuper@59324
   849
        (defs1 ~~ defs2 ~~ defs3)
wneuper@59324
   850
      |> unflat (map snd raw_defs);
wneuper@59324
   851
    val notes =
wneuper@59324
   852
      map2 (fn ((a, raw_atts), _) => fn def_thms =>
wneuper@59324
   853
        ((Thm.def_binding_optional (Binding.conglomerate (map #1 def_thms)) a,
wneuper@59324
   854
          map (prep_att defs_ctxt) raw_atts), map (fn (_, th) => ([th], [])) def_thms))
wneuper@59324
   855
        raw_defs def_thmss;
wneuper@59324
   856
  in
wneuper@59324
   857
    state
walther@60065
   858
    |> map_context (K defs_ctxt #> fold Variable.bind_term result_binds)
wneuper@59324
   859
    |> note_thmss notes
wneuper@59324
   860
  end;
wneuper@59324
   861
wneuper@59324
   862
in
wneuper@59324
   863
wneuper@59324
   864
val define = gen_define Proof_Context.cert_stmt (K I);
wneuper@59324
   865
val define_cmd = gen_define Proof_Context.read_stmt Attrib.attribute_cmd;
wenzelm@17112
   866
wenzelm@17112
   867
end;
wenzelm@17112
   868
wenzelm@17112
   869
wenzelm@17112
   870
wenzelm@17359
   871
(** proof structure **)
wenzelm@17359
   872
wenzelm@17359
   873
(* blocks *)
wenzelm@17359
   874
wenzelm@17359
   875
val begin_block =
wenzelm@17359
   876
  assert_forward
wenzelm@17359
   877
  #> open_block
wenzelm@47945
   878
  #> reset_goal
wenzelm@17359
   879
  #> open_block;
wenzelm@17359
   880
wenzelm@17359
   881
val next_block =
wenzelm@17359
   882
  assert_forward
wenzelm@17359
   883
  #> close_block
wenzelm@17359
   884
  #> open_block
wenzelm@47945
   885
  #> reset_goal
wenzelm@47945
   886
  #> reset_facts;
wenzelm@17359
   887
wenzelm@17359
   888
fun end_block state =
wenzelm@17359
   889
  state
wenzelm@17359
   890
  |> assert_forward
wenzelm@41208
   891
  |> assert_bottom false
wenzelm@17359
   892
  |> close_block
wenzelm@17359
   893
  |> assert_current_goal false
wenzelm@17359
   894
  |> close_block
wenzelm@17359
   895
  |> export_facts state;
wenzelm@17359
   896
wenzelm@17359
   897
wenzelm@41208
   898
(* global notepad *)
wenzelm@41208
   899
wenzelm@41208
   900
val begin_notepad =
wenzelm@41208
   901
  init
wenzelm@41208
   902
  #> open_block
wenzelm@41208
   903
  #> map_context (Variable.set_body true)
wenzelm@41208
   904
  #> open_block;
wenzelm@41208
   905
wenzelm@41208
   906
val end_notepad =
wenzelm@41208
   907
  assert_forward
wenzelm@41208
   908
  #> assert_bottom true
wenzelm@41208
   909
  #> close_block
wenzelm@41208
   910
  #> assert_current_goal false
wenzelm@41208
   911
  #> close_block
wenzelm@41208
   912
  #> context_of;
wenzelm@41208
   913
walther@59606
   914
fun get_notepad_context (State stack) =
walther@59606
   915
  let
walther@59606
   916
    fun escape [Node {goal = SOME _, ...}, Node {goal = NONE, ...}] = NONE
walther@59606
   917
      | escape [Node {goal = SOME _, ...}] = NONE
walther@59606
   918
      | escape [Node {goal = NONE, context = ctxt, ...}] = SOME ctxt
walther@59606
   919
      | escape [] = NONE
walther@59606
   920
      | escape (_ :: rest) = escape rest;
walther@59606
   921
  in escape (op :: (Stack.dest stack)) end;
walther@59606
   922
walther@59606
   923
val is_notepad = is_some o get_notepad_context;
walther@59606
   924
walther@59606
   925
fun reset_notepad state =
walther@59606
   926
  begin_notepad (the_default (context_of state) (get_notepad_context state));
walther@59606
   927
wenzelm@41208
   928
wenzelm@17359
   929
(* sub-proofs *)
wenzelm@17359
   930
wenzelm@17359
   931
fun proof opt_text =
wneuper@59324
   932
  Seq.APPEND
wneuper@59324
   933
    (assert_backward
wneuper@59324
   934
      #> refine (the_default Method.standard_text (Method.text opt_text))
wneuper@59324
   935
      #> Seq.map_result
wneuper@59324
   936
        (using_facts []
wneuper@59324
   937
          #> enter_forward
wneuper@59324
   938
          #> open_block
wneuper@59324
   939
          #> reset_goal),
wneuper@59324
   940
     method_error "initial" (Method.position opt_text));
wenzelm@50878
   941
wenzelm@50904
   942
fun end_proof bot (prev_pos, (opt_text, immed)) =
wenzelm@50904
   943
  let
wenzelm@50904
   944
    val (finish_text, terminal_pos, finished_pos) =
wenzelm@50904
   945
      (case opt_text of
wenzelm@50904
   946
        NONE => (Method.finish_text (NONE, immed), Position.none, prev_pos)
wenzelm@57051
   947
      | SOME (text, (pos, end_pos)) => (Method.finish_text (SOME text, immed), pos, end_pos));
wenzelm@50904
   948
  in
wenzelm@50904
   949
    Seq.APPEND (fn state =>
wenzelm@50904
   950
      state
wenzelm@50904
   951
      |> assert_forward
wenzelm@50904
   952
      |> assert_bottom bot
wenzelm@50904
   953
      |> close_block
wenzelm@50904
   954
      |> assert_current_goal true
wenzelm@50904
   955
      |> using_facts []
wenzelm@50904
   956
      |> `before_qed |-> (refine o the_default Method.succeed_text)
wneuper@59324
   957
      |> Seq.maps_results (refine finish_text),
wneuper@59324
   958
      method_error "terminal" terminal_pos)
wenzelm@50904
   959
    #> Seq.maps_results (Seq.single o finished_goal finished_pos)
wenzelm@50904
   960
  end;
wenzelm@17359
   961
wenzelm@29383
   962
fun check_result msg sq =
wenzelm@29383
   963
  (case Seq.pull sq of
wenzelm@29383
   964
    NONE => error msg
wenzelm@29383
   965
  | SOME (s, _) => s);
wenzelm@29383
   966
wenzelm@17359
   967
wenzelm@17359
   968
(* unstructured refinement *)
wenzelm@17359
   969
wenzelm@50880
   970
fun defer i =
wenzelm@50880
   971
  assert_no_chain #>
wneuper@59324
   972
  refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i)));
wenzelm@50880
   973
wenzelm@50880
   974
fun prefer i =
wenzelm@50880
   975
  assert_no_chain #>
wneuper@59324
   976
  refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i)));
wenzelm@17359
   977
wneuper@59324
   978
fun apply (text, (pos, _)) =
wneuper@59324
   979
  Seq.APPEND (assert_backward #> refine text #> Seq.map_result (using_facts []),
wneuper@59324
   980
    method_error "" pos);
wenzelm@50881
   981
wneuper@59324
   982
fun apply_end (text, (pos, _)) =
wneuper@59324
   983
  Seq.APPEND (assert_forward #> refine_end text, method_error "" pos);
wenzelm@50878
   984
wenzelm@17359
   985
wenzelm@17359
   986
wenzelm@17112
   987
(** goals **)
wenzelm@17112
   988
wenzelm@17359
   989
(* generic goals *)
wenzelm@17359
   990
wenzelm@19774
   991
local
wenzelm@17359
   992
wenzelm@36333
   993
val is_var =
wenzelm@36333
   994
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36333
   995
  can (dest_Var o Logic.dest_term);
wenzelm@36333
   996
wenzelm@36333
   997
fun implicit_vars props =
wenzelm@19846
   998
  let
wneuper@59451
   999
    val var_props = take_prefix is_var props;
wenzelm@36333
  1000
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36333
  1001
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36333
  1002
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
  1003
wenzelm@19774
  1004
fun refine_terms n =
walther@60065
  1005
  refine_singleton (Method.Basic (fn ctxt => CONTEXT_TACTIC o
wneuper@59180
  1006
    K (HEADGOAL (PRECISE_CONJUNCTS n
wneuper@59324
  1007
      (HEADGOAL (CONJUNCTS (ALLGOALS (resolve_tac ctxt [Drule.termI]))))))));
wenzelm@19774
  1008
wenzelm@19774
  1009
in
wenzelm@17359
  1010
wneuper@59324
  1011
fun generic_goal kind before_qed after_qed goal_ctxt goal_propss result_binds state =
wenzelm@5936
  1012
  let
wenzelm@17359
  1013
    val chaining = can assert_chain state;
wenzelm@25958
  1014
    val pos = Position.thread_data ();
wenzelm@17359
  1015
wneuper@59324
  1016
    val goal_props = flat goal_propss;
wneuper@59324
  1017
    val vars = implicit_vars goal_props;
wneuper@59324
  1018
    val propss' = vars :: goal_propss;
wneuper@59324
  1019
    val goal_propss = filter_out null propss';
wenzelm@15703
  1020
wenzelm@29346
  1021
    val goal =
wneuper@59180
  1022
      Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss)
wneuper@59324
  1023
      |> Thm.cterm_of goal_ctxt
wneuper@59324
  1024
      |> Thm.weaken_sorts' goal_ctxt;
wenzelm@29346
  1025
    val statement = ((kind, pos), propss', Thm.term_of goal);
wneuper@59324
  1026
wneuper@59324
  1027
    val after_qed' = after_qed |>> (fn after_local => fn results =>
wneuper@59324
  1028
      map_context (fold Variable.maybe_bind_term result_binds) #> after_local results);
wenzelm@5820
  1029
  in
wneuper@59324
  1030
    state
wneuper@59324
  1031
    |> assert_forward_or_chain
wneuper@59324
  1032
    |> enter_forward
wneuper@59324
  1033
    |> open_block
wneuper@59324
  1034
    |> enter_backward
wneuper@59324
  1035
    |> map_context
wneuper@59324
  1036
      (K goal_ctxt
wneuper@59324
  1037
        #> init_context
wneuper@59324
  1038
        #> Variable.set_body true
wneuper@59324
  1039
        #> Proof_Context.auto_bind_goal goal_props)
wneuper@59180
  1040
    |> set_goal (make_goal (statement, [], Goal.init goal, before_qed, after_qed'))
wenzelm@21565
  1041
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@47945
  1042
    |> reset_facts
wenzelm@23418
  1043
    |> not (null vars) ? refine_terms (length goal_propss)
wneuper@59324
  1044
    |> null goal_props ? refine_singleton (Method.Basic Method.assumption)
wenzelm@5820
  1045
  end;
wenzelm@5820
  1046
wneuper@59324
  1047
fun generic_qed state =
wenzelm@12503
  1048
  let
wneuper@59324
  1049
    val (goal_ctxt, {statement = (_, propss, _), goal, after_qed, ...}) =
wneuper@59324
  1050
      current_goal state;
wneuper@59324
  1051
    val results = tl (conclude_goal goal_ctxt goal propss);
wneuper@59324
  1052
  in state |> close_block |> pair (after_qed, (goal_ctxt, results)) end;
wenzelm@5820
  1053
wenzelm@19774
  1054
end;
wenzelm@19774
  1055
wenzelm@5820
  1056
wenzelm@17359
  1057
(* local goals *)
wenzelm@5820
  1058
wneuper@59324
  1059
fun local_goal print_results prep_statement prep_att strict_asm
wneuper@59324
  1060
    kind before_qed after_qed raw_fixes raw_assumes raw_shows state =
wenzelm@17359
  1061
  let
wneuper@59324
  1062
    val ctxt = context_of state;
wenzelm@17359
  1063
wneuper@59324
  1064
    val add_assumes =
wneuper@59324
  1065
      Assumption.add_assms
wneuper@59324
  1066
        (if strict_asm then Assumption.assume_export else Assumption.presume_export);
wneuper@59324
  1067
wneuper@59324
  1068
    (*params*)
walther@60065
  1069
    val ({fixes = params, assumes = assumes_propss, shows = shows_propss,
walther@60065
  1070
          result_binds, result_text, text}, params_ctxt) = ctxt
wneuper@59324
  1071
      |> prep_statement raw_fixes (map snd raw_assumes) (map snd raw_shows);
wneuper@59324
  1072
wneuper@59324
  1073
    (*prems*)
wneuper@59324
  1074
    val (assumes_bindings, shows_bindings) =
wneuper@59324
  1075
      apply2 (map (apsnd (map (prep_att ctxt)) o fst)) (raw_assumes, raw_shows);
wneuper@59324
  1076
    val (that_fact, goal_ctxt) = params_ctxt
walther@60065
  1077
      |> fold Proof_Context.augment (text :: flat (assumes_propss @ shows_propss))
wneuper@59324
  1078
      |> fold_burrow add_assumes ((map o map) (Thm.cterm_of params_ctxt) assumes_propss)
wneuper@59324
  1079
      |> (fn (premss, ctxt') =>
wneuper@59324
  1080
          let
wneuper@59324
  1081
            val prems = Assumption.local_prems_of ctxt' ctxt;
wneuper@59324
  1082
            val ctxt'' =
wneuper@59324
  1083
              ctxt'
wneuper@59324
  1084
              |> not (null assumes_propss) ?
wneuper@59451
  1085
                (snd o Proof_Context.note_thms ""
wneuper@59451
  1086
                  ((Binding.name Auto_Bind.thatN, []), [(prems, [])]))
wneuper@59324
  1087
              |> (snd o Proof_Context.note_thmss ""
wneuper@59324
  1088
                  (assumes_bindings ~~ map (map (fn th => ([th], []))) premss))
wneuper@59324
  1089
          in (prems, ctxt'') end);
wneuper@59324
  1090
wneuper@59324
  1091
    (*result*)
wneuper@59324
  1092
    val results_bindings = map (apfst Binding.default_pos) shows_bindings;
wneuper@59324
  1093
    fun after_qed' (result_ctxt, results) state' =
wneuper@59324
  1094
      let
wneuper@59324
  1095
        val ctxt' = context_of state';
wneuper@59324
  1096
        val export0 =
walther@60065
  1097
          Assumption.export false result_ctxt (Proof_Context.augment result_text ctxt') #>
wneuper@59324
  1098
          fold_rev (fn (x, v) => Thm.forall_intr_name (x, Thm.cterm_of params_ctxt v)) params #>
wneuper@59324
  1099
          Raw_Simplifier.norm_hhf_protect ctxt';
wneuper@59324
  1100
        val export = map export0 #> Variable.export result_ctxt ctxt';
wneuper@59324
  1101
      in
wneuper@59324
  1102
        state'
walther@60065
  1103
        |> map_context (Proof_Context.augment result_text)
wneuper@59324
  1104
        |> local_results (results_bindings ~~ burrow export results)
wneuper@59324
  1105
        |-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wneuper@59324
  1106
        |> after_qed (result_ctxt, results)
wneuper@59324
  1107
      end;
wenzelm@17359
  1108
  in
wenzelm@17359
  1109
    state
wneuper@59324
  1110
    |> generic_goal kind before_qed (after_qed', K I) goal_ctxt shows_propss result_binds
wneuper@59324
  1111
    |> pair that_fact
wenzelm@17359
  1112
  end;
wenzelm@17359
  1113
wenzelm@50881
  1114
fun local_qeds arg =
wenzelm@50881
  1115
  end_proof false arg
wneuper@59324
  1116
  #> Seq.map_result (generic_qed #-> (fn ((after_qed, _), results) => after_qed results));
wenzelm@17359
  1117
wenzelm@50904
  1118
fun local_qed arg =
wenzelm@50904
  1119
  local_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@29383
  1120
wenzelm@17359
  1121
wenzelm@17359
  1122
(* global goals *)
wenzelm@17359
  1123
wneuper@59324
  1124
fun global_goal prep_propp before_qed after_qed propp ctxt =
wneuper@59324
  1125
  let
wneuper@59324
  1126
    val (propss, binds) =
wneuper@59324
  1127
      prep_propp (Proof_Context.set_mode Proof_Context.mode_schematic ctxt) propp;
wneuper@59324
  1128
    val goal_ctxt = ctxt
walther@60065
  1129
      |> (fold o fold) Proof_Context.augment propss
wneuper@59324
  1130
      |> fold Variable.bind_term binds;
wneuper@59324
  1131
    fun after_qed' (result_ctxt, results) ctxt' = ctxt'
wneuper@59324
  1132
      |> Proof_Context.restore_naming ctxt
wneuper@59324
  1133
      |> after_qed (burrow (Proof_Context.export result_ctxt ctxt') results);
wneuper@59324
  1134
  in
wneuper@59324
  1135
    ctxt
wneuper@59324
  1136
    |> init
wneuper@59324
  1137
    |> generic_goal "" before_qed (K I, after_qed') goal_ctxt propss []
wneuper@59324
  1138
  end;
wenzelm@46203
  1139
wneuper@59324
  1140
val theorem = global_goal Proof_Context.cert_propp;
wneuper@59324
  1141
val theorem_cmd = global_goal Proof_Context.read_propp;
wenzelm@12065
  1142
wenzelm@50881
  1143
fun global_qeds arg =
wenzelm@50881
  1144
  end_proof true arg
wneuper@59324
  1145
  #> Seq.map_result (generic_qed #> (fn (((_, after_qed), results), state) =>
wenzelm@50874
  1146
    after_qed results (context_of state)));
wenzelm@5820
  1147
wenzelm@50904
  1148
fun global_qed arg =
wenzelm@50904
  1149
  global_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@17112
  1150
wenzelm@17112
  1151
wneuper@59451
  1152
(* relevant proof states *)
wneuper@59451
  1153
wneuper@59451
  1154
fun schematic_goal state =
wneuper@59451
  1155
  let val (_, {statement = (_, _, prop), ...}) = find_goal state
wneuper@59451
  1156
  in Goal.is_schematic prop end;
wneuper@59451
  1157
wneuper@59451
  1158
fun is_relevant state =
wneuper@59451
  1159
  (case try find_goal state of
wneuper@59451
  1160
    NONE => true
wneuper@59451
  1161
  | SOME (_, {statement = (_, _, prop), goal, ...}) =>
wneuper@59451
  1162
      Goal.is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wneuper@59451
  1163
wneuper@59451
  1164
wenzelm@50922
  1165
(* terminal proof steps *)
wenzelm@17112
  1166
wenzelm@50905
  1167
local
wenzelm@50905
  1168
wneuper@59324
  1169
fun terminal_proof qeds initial terminal state =
wneuper@59324
  1170
  let
wneuper@59324
  1171
    val ctxt = context_of state;
wneuper@59324
  1172
    val check_closure = Method.check_text ctxt #> Method.map_source (Method.method_closure ctxt);
wneuper@59324
  1173
    val initial' = apfst check_closure initial;
wneuper@59324
  1174
    val terminal' = (apfst o Option.map o apfst) check_closure terminal;
wneuper@59324
  1175
  in
wneuper@59451
  1176
    if Goal.skip_proofs_enabled () andalso not (is_relevant state) then
wneuper@59451
  1177
      state
wneuper@59451
  1178
      |> proof (SOME (Method.sorry_text true, #2 initial'))
wneuper@59451
  1179
      |> Seq.maps_results (qeds (#2 (#2 initial), (NONE, #2 terminal)))
wneuper@59451
  1180
    else
wneuper@59451
  1181
      state
wneuper@59451
  1182
      |> proof (SOME initial')
wneuper@59451
  1183
      |> Seq.maps_results (qeds (#2 (#2 initial), terminal'))
wneuper@59451
  1184
  end |> Seq.the_result "";
wenzelm@50863
  1185
wenzelm@50905
  1186
in
wenzelm@50905
  1187
wenzelm@29383
  1188
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wneuper@59324
  1189
val local_default_proof = local_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@50904
  1190
val local_immediate_proof = local_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@50904
  1191
val local_done_proof = terminal_proof local_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@17112
  1192
wenzelm@29383
  1193
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wneuper@59324
  1194
val global_default_proof = global_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@50904
  1195
val global_immediate_proof = global_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@50904
  1196
val global_done_proof = terminal_proof global_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@17112
  1197
wenzelm@50905
  1198
end;
wenzelm@50905
  1199
wenzelm@17112
  1200
wenzelm@50922
  1201
(* skip proofs *)
wenzelm@50922
  1202
wenzelm@50922
  1203
fun local_skip_proof int state =
wenzelm@50922
  1204
  local_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@52688
  1205
  Skip_Proof.report (context_of state);
wenzelm@50922
  1206
wenzelm@50922
  1207
fun global_skip_proof int state =
wenzelm@50922
  1208
  global_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@52688
  1209
  Skip_Proof.report (context_of state);
wenzelm@50922
  1210
wenzelm@50922
  1211
wenzelm@17359
  1212
(* common goal statements *)
wenzelm@17112
  1213
wneuper@59324
  1214
fun internal_goal print_results mode =
wneuper@59324
  1215
  local_goal print_results
wneuper@59324
  1216
    (fn a => fn b => fn c => Proof_Context.cert_statement a b c o Proof_Context.set_mode mode) (K I);
wneuper@59324
  1217
wenzelm@17359
  1218
local
wenzelm@17112
  1219
wneuper@59324
  1220
fun gen_have prep_statement prep_att strict_asm before_qed after_qed fixes assumes shows int =
wenzelm@58274
  1221
  local_goal (Proof_Display.print_results int (Position.thread_data ()))
wneuper@59324
  1222
    prep_statement prep_att strict_asm "have" before_qed after_qed fixes assumes shows;
wenzelm@5820
  1223
wneuper@59324
  1224
fun gen_show prep_statement prep_att strict_asm before_qed after_qed fixes assumes shows int state =
wenzelm@17359
  1225
  let
wenzelm@32738
  1226
    val testing = Unsynchronized.ref false;
wenzelm@32738
  1227
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
  1228
    fun fail_msg ctxt =
wenzelm@51330
  1229
      "Local statement fails to refine any pending goal" ::
wenzelm@33389
  1230
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
  1231
      |> cat_lines;
wenzelm@5820
  1232
wenzelm@58274
  1233
    val pos = Position.thread_data ();
wenzelm@17359
  1234
    fun print_results ctxt res =
wenzelm@47602
  1235
      if ! testing then ()
wenzelm@58274
  1236
      else Proof_Display.print_results int pos ctxt res;
wenzelm@17359
  1237
    fun print_rule ctxt th =
wenzelm@17359
  1238
      if ! testing then rule := SOME th
wenzelm@41429
  1239
      else if int then
wenzelm@58275
  1240
        Proof_Display.string_of_rule ctxt "Successful" th
wenzelm@58275
  1241
        |> Markup.markup Markup.text_fold
wneuper@59180
  1242
        |> Output.state
wenzelm@17359
  1243
      else ();
wenzelm@17359
  1244
    val test_proof =
wenzelm@51330
  1245
      local_skip_proof true
wenzelm@39862
  1246
      |> Unsynchronized.setmp testing true
wenzelm@42923
  1247
      |> Exn.interruptible_capture;
wenzelm@6896
  1248
wneuper@59324
  1249
    fun after_qed' (result_ctxt, results) state' = state'
wneuper@59324
  1250
      |> refine_goals print_rule result_ctxt (flat results)
wneuper@59324
  1251
      |> check_result "Failed to refine any pending goal"
wneuper@59324
  1252
      |> after_qed (result_ctxt, results);
wenzelm@17359
  1253
  in
wenzelm@17359
  1254
    state
wneuper@59324
  1255
    |> local_goal print_results prep_statement prep_att strict_asm
wneuper@59324
  1256
      "show" before_qed after_qed' fixes assumes shows
wneuper@59324
  1257
    ||> int ? (fn goal_state =>
wenzelm@50922
  1258
      (case test_proof (map_context (Context_Position.set_visible false) goal_state) of
wenzelm@51330
  1259
        Exn.Res _ => goal_state
wenzelm@42923
  1260
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1261
  end;
wenzelm@6896
  1262
wenzelm@17359
  1263
in
wenzelm@6896
  1264
wneuper@59324
  1265
val have = gen_have Proof_Context.cert_statement (K I);
wneuper@59324
  1266
val have_cmd = gen_have Proof_Context.read_statement Attrib.attribute_cmd;
wneuper@59324
  1267
val show = gen_show Proof_Context.cert_statement (K I);
wneuper@59324
  1268
val show_cmd = gen_show Proof_Context.read_statement Attrib.attribute_cmd;
wenzelm@6896
  1269
wenzelm@5820
  1270
end;
wenzelm@17359
  1271
wenzelm@28410
  1272
wenzelm@29385
  1273
wenzelm@29385
  1274
(** future proofs **)
wenzelm@29385
  1275
wenzelm@29385
  1276
(* full proofs *)
wenzelm@28410
  1277
wenzelm@29346
  1278
local
wenzelm@29346
  1279
wenzelm@50927
  1280
structure Result = Proof_Data
wenzelm@50927
  1281
(
wenzelm@50927
  1282
  type T = thm option;
wneuper@59180
  1283
  fun init _ = NONE;
wenzelm@50927
  1284
);
wenzelm@50927
  1285
wenzelm@50927
  1286
fun the_result ctxt =
wenzelm@50927
  1287
  (case Result.get ctxt of
wenzelm@50927
  1288
    NONE => error "No result of forked proof"
wenzelm@50927
  1289
  | SOME th => th);
wenzelm@50927
  1290
wenzelm@50927
  1291
val set_result = Result.put o SOME;
wenzelm@50927
  1292
val reset_result = Result.put NONE;
wenzelm@50927
  1293
wenzelm@52455
  1294
in
wenzelm@52455
  1295
wenzelm@52455
  1296
fun future_proof fork_proof state =
wenzelm@28410
  1297
  let
wenzelm@29381
  1298
    val _ = assert_backward state;
wneuper@59324
  1299
    val (goal_ctxt, goal_info) = find_goal state;
wneuper@59324
  1300
    val {statement as (kind, _, prop), using, goal, before_qed, after_qed} = goal_info;
wenzelm@28410
  1301
wenzelm@29383
  1302
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1303
wneuper@59324
  1304
    val after_qed' =
wneuper@59324
  1305
      (fn (_, [[th]]) => map_context (set_result th),
wneuper@59324
  1306
       fn (_, [[th]]) => set_result th);
wenzelm@28973
  1307
    val result_ctxt =
wenzelm@28973
  1308
      state
wenzelm@50927
  1309
      |> map_context reset_result
wneuper@59324
  1310
      |> map_goal (K (goal_ctxt, (kind, [[], [prop]], prop), using, goal, before_qed, after_qed'))
wenzelm@29346
  1311
      |> fork_proof;
wenzelm@28973
  1312
wenzelm@52455
  1313
    val future_thm = Future.map (the_result o snd) result_ctxt;
wneuper@59324
  1314
    val finished_goal = Goal.protect 0 (Goal.future_result goal_ctxt future_thm prop);
wenzelm@28973
  1315
    val state' =
wenzelm@28973
  1316
      state
wneuper@59324
  1317
      |> map_goal (K (goal_ctxt, statement, using, finished_goal, NONE, after_qed));
wenzelm@52455
  1318
  in (Future.map fst result_ctxt, state') end;
wenzelm@29385
  1319
wenzelm@17359
  1320
end;
wenzelm@29346
  1321
wenzelm@29385
  1322
wenzelm@52799
  1323
(* terminal proofs *)  (* FIXME avoid toplevel imitation -- include in PIDE/document *)
wenzelm@29385
  1324
wenzelm@29385
  1325
local
wenzelm@29385
  1326
wneuper@59324
  1327
fun future_terminal_proof proof1 proof2 done state =
walther@60065
  1328
  if Future.proofs_enabled 3 andalso
walther@60065
  1329
    not (Proofterm.proofs_enabled ()) andalso
walther@60065
  1330
    not (is_relevant state)
walther@60065
  1331
  then
wenzelm@52455
  1332
    state |> future_proof (fn state' =>
wneuper@59451
  1333
      let val pos = Position.thread_data () in
wenzelm@54329
  1334
        Execution.fork {name = "Proof.future_terminal_proof", pos = pos, pri = ~1}
wneuper@59451
  1335
          (fn () => ((), Timing.protocol "by" pos proof2 state'))
wenzelm@52799
  1336
      end) |> snd |> done
wenzelm@52455
  1337
  else proof1 state;
wenzelm@29385
  1338
wenzelm@29385
  1339
in
wenzelm@29385
  1340
wenzelm@52455
  1341
fun local_future_terminal_proof meths =
wenzelm@52742
  1342
  future_terminal_proof
wenzelm@52455
  1343
    (local_terminal_proof meths)
wenzelm@52455
  1344
    (local_terminal_proof meths #> context_of) local_done_proof;
wenzelm@29385
  1345
wenzelm@52455
  1346
fun global_future_terminal_proof meths =
wenzelm@52742
  1347
  future_terminal_proof
wenzelm@52455
  1348
    (global_terminal_proof meths)
wenzelm@52455
  1349
    (global_terminal_proof meths) global_done_proof;
wenzelm@29350
  1350
wenzelm@29346
  1351
end;
wenzelm@29346
  1352
wenzelm@29385
  1353
end;