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