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