src/Pure/Isar/proof.ML
author wenzelm
Tue Apr 10 21:31:05 2012 +0200 (2012-04-10)
changeset 47416 df8fc0567a3d
parent 47068 2027ff3136cc
child 47431 d9dd4a9f18fc
permissions -rw-r--r--
static relevance of proof via syntax keywords;
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@30757
    22
  val bind_terms: (indexname * term option) list -> state -> state
wenzelm@26251
    23
  val put_thms: bool -> string * thm list option -> state -> state
wenzelm@6091
    24
  val the_facts: state -> thm list
wenzelm@9469
    25
  val the_fact: state -> thm
wenzelm@47068
    26
  val set_facts: thm list -> state -> state
wenzelm@47068
    27
  val reset_facts: state -> state
wenzelm@6891
    28
  val assert_forward: state -> state
wenzelm@17359
    29
  val assert_chain: state -> state
wenzelm@9469
    30
  val assert_forward_or_chain: state -> state
wenzelm@5820
    31
  val assert_backward: state -> state
wenzelm@8206
    32
  val assert_no_chain: state -> state
wenzelm@5820
    33
  val enter_forward: state -> state
wenzelm@24543
    34
  val goal_message: (unit -> Pretty.T) -> state -> state
wenzelm@12423
    35
  val pretty_state: int -> state -> Pretty.T list
wenzelm@10360
    36
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@17112
    37
  val refine: Method.text -> state -> state Seq.seq
wenzelm@17112
    38
  val refine_end: Method.text -> state -> state Seq.seq
wenzelm@18908
    39
  val refine_insert: thm list -> state -> state
wenzelm@17359
    40
  val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq
wenzelm@33288
    41
  val raw_goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    42
  val goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    43
  val simple_goal: state -> {context: context, goal: thm}
wenzelm@38721
    44
  val status_markup: state -> Markup.T
wenzelm@36323
    45
  val let_bind: (term list * term) list -> state -> state
wenzelm@36323
    46
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36505
    47
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36505
    48
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36323
    49
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36323
    50
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wenzelm@20224
    51
  val assm: Assumption.export ->
wenzelm@36323
    52
    (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    53
  val assm_cmd: Assumption.export ->
wenzelm@28084
    54
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    55
  val assume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    56
  val assume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    57
  val presume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    58
  val presume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    59
  val def: (Thm.binding * ((binding * mixfix) * (term * term list))) list -> state -> state
wenzelm@36323
    60
  val def_cmd: (Attrib.binding * ((binding * mixfix) * (string * string list))) list -> state -> state
wenzelm@17359
    61
  val chain: state -> state
wenzelm@17359
    62
  val chain_facts: thm list -> state -> state
wenzelm@36323
    63
  val note_thmss: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wenzelm@36323
    64
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    65
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    66
  val from_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    67
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    68
  val with_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    69
  val using: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    70
  val using_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    71
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    72
  val unfolding_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@42496
    73
  val invoke_case: string * binding option list * attribute list -> state -> state
wenzelm@42496
    74
  val invoke_case_cmd: string * binding option list * Attrib.src list -> state -> state
wenzelm@17359
    75
  val begin_block: state -> state
wenzelm@17359
    76
  val next_block: state -> state
wenzelm@20309
    77
  val end_block: state -> state
wenzelm@40960
    78
  val begin_notepad: Proof.context -> state
wenzelm@40960
    79
  val end_notepad: state -> Proof.context
wenzelm@17112
    80
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@17112
    81
  val defer: int option -> state -> state Seq.seq
wenzelm@17112
    82
  val prefer: int -> state -> state Seq.seq
wenzelm@17112
    83
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    84
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@17359
    85
  val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) ->
wenzelm@18728
    86
    (theory -> 'a -> attribute) ->
wenzelm@45327
    87
    ('b list -> context -> (term list list * (context -> context)) * context) ->
wenzelm@29383
    88
    string -> Method.text option -> (thm list list -> state -> state) ->
haftmann@29581
    89
    ((binding * 'a list) * 'b) list -> state -> state
wenzelm@29383
    90
  val local_qed: Method.text option * bool -> state -> state
wenzelm@21442
    91
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36323
    92
    (term * term list) list list -> context -> state
wenzelm@36323
    93
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
    94
    (string * string list) list list -> context -> state
wenzelm@20363
    95
  val global_qed: Method.text option * bool -> state -> context
wenzelm@29383
    96
  val local_terminal_proof: Method.text * Method.text option -> state -> state
wenzelm@29383
    97
  val local_default_proof: state -> state
wenzelm@29383
    98
  val local_immediate_proof: state -> state
wenzelm@29383
    99
  val local_skip_proof: bool -> state -> state
wenzelm@29383
   100
  val local_done_proof: state -> state
wenzelm@20363
   101
  val global_terminal_proof: Method.text * Method.text option -> state -> context
wenzelm@20363
   102
  val global_default_proof: state -> context
wenzelm@20363
   103
  val global_immediate_proof: state -> context
wenzelm@29383
   104
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   105
  val global_done_proof: state -> context
wenzelm@29383
   106
  val have: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   107
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   108
  val have_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   109
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29383
   110
  val show: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   111
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   112
  val show_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   113
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29385
   114
  val schematic_goal: state -> bool
wenzelm@47416
   115
  val local_future_proof: (state -> ('a * state) future) -> state -> 'a future * state
wenzelm@47416
   116
  val global_future_proof: (state -> ('a * Proof.context) future) -> state -> 'a future * context
wenzelm@29385
   117
  val local_future_terminal_proof: Method.text * Method.text option -> bool -> state -> state
wenzelm@29385
   118
  val global_future_terminal_proof: Method.text * Method.text option -> bool -> state -> context
wenzelm@5820
   119
end;
wenzelm@5820
   120
wenzelm@13377
   121
structure Proof: PROOF =
wenzelm@5820
   122
struct
wenzelm@5820
   123
wenzelm@33031
   124
type context = Proof.context;
wenzelm@17112
   125
type method = Method.method;
wenzelm@16813
   126
wenzelm@5820
   127
wenzelm@5820
   128
(** proof state **)
wenzelm@5820
   129
wenzelm@17359
   130
(* datatype state *)
wenzelm@5820
   131
wenzelm@17112
   132
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   133
wenzelm@17359
   134
datatype state =
wenzelm@17359
   135
  State of node Stack.T
wenzelm@17359
   136
and node =
wenzelm@7176
   137
  Node of
wenzelm@7176
   138
   {context: context,
wenzelm@7176
   139
    facts: thm list option,
wenzelm@7176
   140
    mode: mode,
wenzelm@17359
   141
    goal: goal option}
wenzelm@17359
   142
and goal =
wenzelm@17359
   143
  Goal of
wenzelm@29346
   144
   {statement: (string * Position.T) * term list list * term,
wenzelm@28410
   145
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   146
    messages: (unit -> Pretty.T) list,    (*persistent messages (hints etc.)*)
wenzelm@25958
   147
    using: thm list,                      (*goal facts*)
wenzelm@25958
   148
    goal: thm,                            (*subgoals ==> statement*)
wenzelm@17859
   149
    before_qed: Method.text option,
wenzelm@18124
   150
    after_qed:
wenzelm@29383
   151
      (thm list list -> state -> state) *
wenzelm@20363
   152
      (thm list list -> context -> context)};
wenzelm@17359
   153
wenzelm@24543
   154
fun make_goal (statement, messages, using, goal, before_qed, after_qed) =
wenzelm@24543
   155
  Goal {statement = statement, messages = messages, using = using, goal = goal,
wenzelm@17859
   156
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   157
wenzelm@7176
   158
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   159
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   160
wenzelm@17359
   161
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   162
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   163
wenzelm@21727
   164
val init_context =
wenzelm@42360
   165
  Proof_Context.set_stmt true #>
wenzelm@47005
   166
  Proof_Context.map_naming (K Name_Space.local_naming);
wenzelm@21727
   167
wenzelm@21466
   168
fun init ctxt =
wenzelm@21727
   169
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   170
wenzelm@17359
   171
fun current (State st) = Stack.top st |> (fn Node node => node);
wenzelm@21274
   172
fun map_current f (State st) = State (Stack.map_top (map_node f) st);
wenzelm@28278
   173
fun map_all f (State st) = State (Stack.map_all (map_node f) st);
wenzelm@12045
   174
wenzelm@5820
   175
wenzelm@5820
   176
wenzelm@5820
   177
(** basic proof state operations **)
wenzelm@5820
   178
wenzelm@17359
   179
(* block structure *)
wenzelm@17359
   180
wenzelm@17359
   181
fun open_block (State st) = State (Stack.push st);
wenzelm@17359
   182
wenzelm@18678
   183
fun close_block (State st) = State (Stack.pop st)
wenzelm@47060
   184
  handle List.Empty => error "Unbalanced block parentheses";
wenzelm@17359
   185
wenzelm@17359
   186
fun level (State st) = Stack.level st;
wenzelm@17359
   187
wenzelm@17359
   188
fun assert_bottom b state =
wenzelm@47065
   189
  let val b' = level state <= 2 in
wenzelm@47065
   190
    if b andalso not b' then error "Not at bottom of proof"
wenzelm@47065
   191
    else if not b andalso b' then error "Already at bottom of proof"
wenzelm@17359
   192
    else state
wenzelm@17359
   193
  end;
wenzelm@17359
   194
wenzelm@17359
   195
wenzelm@5820
   196
(* context *)
wenzelm@5820
   197
wenzelm@17359
   198
val context_of = #context o current;
wenzelm@42360
   199
val theory_of = Proof_Context.theory_of o context_of;
wenzelm@5820
   200
wenzelm@17359
   201
fun map_context f =
wenzelm@17359
   202
  map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   203
wenzelm@17359
   204
fun map_context_result f state =
wenzelm@17859
   205
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   206
wenzelm@28278
   207
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   208
wenzelm@37949
   209
fun propagate_ml_env state = map_contexts
wenzelm@37949
   210
  (Context.proof_map (ML_Env.inherit (Context.Proof (context_of state)))) state;
wenzelm@37949
   211
wenzelm@42360
   212
val bind_terms = map_context o Proof_Context.bind_terms;
wenzelm@42360
   213
val put_thms = map_context oo Proof_Context.put_thms;
wenzelm@5820
   214
wenzelm@5820
   215
wenzelm@5820
   216
(* facts *)
wenzelm@5820
   217
wenzelm@17359
   218
val get_facts = #facts o current;
wenzelm@17359
   219
wenzelm@17359
   220
fun the_facts state =
wenzelm@17359
   221
  (case get_facts state of SOME facts => facts
wenzelm@18678
   222
  | NONE => error "No current facts available");
wenzelm@5820
   223
wenzelm@9469
   224
fun the_fact state =
wenzelm@17359
   225
  (case the_facts state of [thm] => thm
wenzelm@18678
   226
  | _ => error "Single theorem expected");
wenzelm@7605
   227
wenzelm@17359
   228
fun put_facts facts =
wenzelm@19078
   229
  map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wenzelm@33386
   230
  put_thms true (Auto_Bind.thisN, facts);
wenzelm@5820
   231
wenzelm@47068
   232
val set_facts = put_facts o SOME;
wenzelm@47068
   233
val reset_facts = put_facts NONE;
wenzelm@47068
   234
wenzelm@17359
   235
fun these_factss more_facts (named_factss, state) =
wenzelm@47068
   236
  (named_factss, state |> set_facts (maps snd named_factss @ more_facts));
wenzelm@5820
   237
wenzelm@17359
   238
fun export_facts inner outer =
wenzelm@17359
   239
  (case get_facts inner of
wenzelm@47068
   240
    NONE => reset_facts outer
wenzelm@17359
   241
  | SOME thms =>
wenzelm@17359
   242
      thms
wenzelm@42360
   243
      |> Proof_Context.export (context_of inner) (context_of outer)
wenzelm@47068
   244
      |> (fn ths => set_facts ths outer));
wenzelm@5820
   245
wenzelm@5820
   246
wenzelm@5820
   247
(* mode *)
wenzelm@5820
   248
wenzelm@17359
   249
val get_mode = #mode o current;
wenzelm@5820
   250
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   251
wenzelm@17359
   252
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   253
wenzelm@5820
   254
fun assert_mode pred state =
wenzelm@5820
   255
  let val mode = get_mode state in
wenzelm@5820
   256
    if pred mode then state
wenzelm@18678
   257
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   258
  end;
wenzelm@5820
   259
wenzelm@19308
   260
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   261
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   262
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   263
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   264
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   265
wenzelm@17359
   266
val enter_forward = put_mode Forward;
wenzelm@17359
   267
val enter_chain = put_mode Chain;
wenzelm@17359
   268
val enter_backward = put_mode Backward;
wenzelm@5820
   269
wenzelm@17359
   270
wenzelm@17359
   271
(* current goal *)
wenzelm@17359
   272
wenzelm@17359
   273
fun current_goal state =
wenzelm@17359
   274
  (case current state of
wenzelm@17359
   275
    {context, goal = SOME (Goal goal), ...} => (context, goal)
wenzelm@47065
   276
  | _ => error "No current goal");
wenzelm@5820
   277
wenzelm@17359
   278
fun assert_current_goal g state =
wenzelm@17359
   279
  let val g' = can current_goal state in
wenzelm@47065
   280
    if g andalso not g' then error "No goal in this block"
wenzelm@47065
   281
    else if not g andalso g' then error "Goal present in this block"
wenzelm@17359
   282
    else state
wenzelm@17359
   283
  end;
wenzelm@6776
   284
wenzelm@17359
   285
fun put_goal goal = map_current (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   286
wenzelm@47068
   287
val set_goal = put_goal o SOME;
wenzelm@47068
   288
val reset_goal = put_goal NONE;
wenzelm@47068
   289
wenzelm@17859
   290
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   291
wenzelm@17359
   292
wenzelm@17359
   293
(* nested goal *)
wenzelm@5820
   294
wenzelm@17359
   295
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
wenzelm@17359
   296
      let
wenzelm@24543
   297
        val Goal {statement, messages, using, goal, before_qed, after_qed} = goal;
wenzelm@24543
   298
        val goal' = make_goal (g (statement, messages, using, goal, before_qed, after_qed));
wenzelm@17359
   299
      in State (make_node (f context, facts, mode, SOME goal'), nodes) end
wenzelm@17359
   300
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@17359
   301
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@17359
   302
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@17359
   303
  | map_goal _ _ state = state;
wenzelm@5820
   304
wenzelm@47068
   305
fun provide_goal goal = map_goal I (fn (statement, _, using, _, before_qed, after_qed) =>
wenzelm@24556
   306
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@19188
   307
wenzelm@24543
   308
fun goal_message msg = map_goal I (fn (statement, messages, using, goal, before_qed, after_qed) =>
wenzelm@24543
   309
  (statement, msg :: messages, using, goal, before_qed, after_qed));
wenzelm@24543
   310
wenzelm@24556
   311
fun using_facts using = map_goal I (fn (statement, _, _, goal, before_qed, after_qed) =>
wenzelm@24556
   312
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@17359
   313
wenzelm@17359
   314
local
wenzelm@17359
   315
  fun find i state =
wenzelm@17359
   316
    (case try current_goal state of
wenzelm@17359
   317
      SOME (ctxt, goal) => (ctxt, (i, goal))
wenzelm@18678
   318
    | NONE => find (i + 1) (close_block state handle ERROR _ => error "No goal present"));
wenzelm@17359
   319
in val find_goal = find 0 end;
wenzelm@17359
   320
wenzelm@17359
   321
fun get_goal state =
wenzelm@17359
   322
  let val (ctxt, (_, {using, goal, ...})) = find_goal state
wenzelm@17359
   323
  in (ctxt, (using, goal)) end;
wenzelm@5820
   324
wenzelm@5820
   325
wenzelm@5820
   326
wenzelm@12423
   327
(** pretty_state **)
wenzelm@5820
   328
skalberg@15531
   329
fun pretty_facts _ _ NONE = []
skalberg@15531
   330
  | pretty_facts s ctxt (SOME ths) =
wenzelm@32091
   331
      [Pretty.big_list (s ^ "this:") (map (Display.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   332
wenzelm@17359
   333
fun pretty_state nr state =
wenzelm@5820
   334
  let
wenzelm@39051
   335
    val {context = ctxt, facts, mode, goal = _} = current state;
wenzelm@42717
   336
    val verbose = Config.get ctxt Proof_Context.verbose;
wenzelm@5820
   337
wenzelm@5820
   338
    fun levels_up 0 = ""
wenzelm@17359
   339
      | levels_up 1 = "1 level up"
wenzelm@17359
   340
      | levels_up i = string_of_int i ^ " levels up";
wenzelm@5820
   341
wenzelm@11556
   342
    fun subgoals 0 = ""
wenzelm@17359
   343
      | subgoals 1 = "1 subgoal"
wenzelm@17359
   344
      | subgoals n = string_of_int n ^ " subgoals";
wenzelm@17359
   345
wenzelm@17359
   346
    fun description strs =
wenzelm@28375
   347
      (case filter_out (fn s => s = "") strs of [] => ""
wenzelm@17359
   348
      | strs' => enclose " (" ")" (commas strs'));
wenzelm@12146
   349
wenzelm@39051
   350
    fun prt_goal (SOME (_, (i,
wenzelm@29416
   351
      {statement = ((_, pos), _, _), messages, using, goal, before_qed = _, after_qed = _}))) =
wenzelm@17359
   352
          pretty_facts "using " ctxt
wenzelm@17359
   353
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@21442
   354
          [Pretty.str ("goal" ^
wenzelm@21442
   355
            description [levels_up (i div 2), subgoals (Thm.nprems_of goal)] ^ ":")] @
wenzelm@39125
   356
          Goal_Display.pretty_goals ctxt goal @
wenzelm@25958
   357
          (map (fn msg => Position.setmp_thread_data pos msg ()) (rev messages))
wenzelm@17359
   358
      | prt_goal NONE = [];
wenzelm@6848
   359
wenzelm@17359
   360
    val prt_ctxt =
wenzelm@42717
   361
      if verbose orelse mode = Forward then Proof_Context.pretty_context ctxt
wenzelm@42360
   362
      else if mode = Backward then Proof_Context.pretty_ctxt ctxt
wenzelm@7575
   363
      else [];
wenzelm@17359
   364
  in
wenzelm@17359
   365
    [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@42717
   366
      (if verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")),
wenzelm@17359
   367
      Pretty.str ""] @
wenzelm@17359
   368
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@42717
   369
    (if verbose orelse mode = Forward then
wenzelm@39051
   370
       pretty_facts "" ctxt facts @ prt_goal (try find_goal state)
wenzelm@39051
   371
     else if mode = Chain then pretty_facts "picking " ctxt facts
wenzelm@17359
   372
     else prt_goal (try find_goal state))
wenzelm@17359
   373
  end;
wenzelm@5820
   374
wenzelm@12085
   375
fun pretty_goals main state =
wenzelm@39125
   376
  let
wenzelm@39129
   377
    val (_, (_, goal)) = get_goal state;
wenzelm@39129
   378
    val ctxt = context_of state
wenzelm@39125
   379
      |> Config.put Goal_Display.show_main_goal main
wenzelm@39125
   380
      |> Config.put Goal_Display.goals_total false;
wenzelm@39129
   381
  in Goal_Display.pretty_goals ctxt goal end;
wenzelm@10320
   382
wenzelm@5820
   383
wenzelm@5820
   384
wenzelm@5820
   385
(** proof steps **)
wenzelm@5820
   386
wenzelm@17359
   387
(* refine via method *)
wenzelm@5820
   388
wenzelm@8234
   389
local
wenzelm@8234
   390
wenzelm@16146
   391
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   392
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   393
wenzelm@16146
   394
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   395
wenzelm@16146
   396
fun goal_cases st =
nipkow@44045
   397
  Rule_Cases.make_common (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair [] o rpair []) (goals st));
wenzelm@16146
   398
wenzelm@32193
   399
fun apply_method current_context meth state =
wenzelm@6848
   400
  let
wenzelm@24556
   401
    val (goal_ctxt, (_, {statement, messages = _, using, goal, before_qed, after_qed})) =
wenzelm@24543
   402
      find_goal state;
wenzelm@25958
   403
    val ctxt = if current_context then context_of state else goal_ctxt;
wenzelm@16146
   404
  in
wenzelm@32193
   405
    Method.apply meth ctxt using goal |> Seq.map (fn (meth_cases, goal') =>
wenzelm@6848
   406
      state
wenzelm@16146
   407
      |> map_goal
wenzelm@42360
   408
          (Proof_Context.add_cases false (no_goal_cases goal @ goal_cases goal') #>
wenzelm@42360
   409
           Proof_Context.add_cases true meth_cases)
wenzelm@24556
   410
          (K (statement, [], using, goal', before_qed, after_qed)))
wenzelm@16146
   411
  end;
wenzelm@5820
   412
wenzelm@19188
   413
fun select_goals n meth state =
wenzelm@19224
   414
  state
wenzelm@19224
   415
  |> (#2 o #2 o get_goal)
wenzelm@21687
   416
  |> ALLGOALS Goal.conjunction_tac
wenzelm@19224
   417
  |> Seq.maps (fn goal =>
wenzelm@19188
   418
    state
wenzelm@47068
   419
    |> Seq.lift provide_goal (Goal.extract 1 n goal |> Seq.maps (Goal.conjunction_tac 1))
wenzelm@19188
   420
    |> Seq.maps meth
wenzelm@19188
   421
    |> Seq.maps (fn state' => state'
wenzelm@47068
   422
      |> Seq.lift provide_goal (Goal.retrofit 1 n (#2 (#2 (get_goal state'))) goal))
wenzelm@32193
   423
    |> Seq.maps (apply_method true (K Method.succeed)));
wenzelm@19188
   424
wenzelm@17112
   425
fun apply_text cc text state =
wenzelm@17112
   426
  let
wenzelm@17112
   427
    val thy = theory_of state;
wenzelm@17112
   428
wenzelm@32193
   429
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@32193
   430
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@32193
   431
      | eval (Method.Source_i src) = apply_method cc (Method.method_i thy src)
wenzelm@17112
   432
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   433
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   434
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@19188
   435
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt)
wenzelm@19188
   436
      | eval (Method.SelectGoals (n, txt)) = select_goals n (eval txt);
wenzelm@17112
   437
  in eval text state end;
wenzelm@17112
   438
wenzelm@8234
   439
in
wenzelm@8234
   440
wenzelm@17112
   441
val refine = apply_text true;
wenzelm@17112
   442
val refine_end = apply_text false;
wenzelm@32856
   443
fun refine_insert ths = Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   444
wenzelm@8234
   445
end;
wenzelm@8234
   446
wenzelm@5820
   447
wenzelm@17359
   448
(* refine via sub-proof *)
wenzelm@17359
   449
wenzelm@46466
   450
local
wenzelm@46466
   451
wenzelm@30557
   452
fun finish_tac 0 = K all_tac
wenzelm@30557
   453
  | finish_tac n =
wenzelm@30557
   454
      Goal.norm_hhf_tac THEN'
wenzelm@30557
   455
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   456
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@30557
   457
          Tactic.etac Drule.protectI i THEN finish_tac (n - 1) i
wenzelm@30557
   458
        else finish_tac (n - 1) (i + 1));
wenzelm@30557
   459
wenzelm@20208
   460
fun goal_tac rule =
wenzelm@30557
   461
  Goal.norm_hhf_tac THEN'
wenzelm@30557
   462
  Tactic.rtac rule THEN'
wenzelm@30557
   463
  finish_tac (Thm.nprems_of rule);
wenzelm@11816
   464
wenzelm@46466
   465
fun FINDGOAL tac st =
wenzelm@46466
   466
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@46466
   467
  in find 1 (Thm.nprems_of st) st end;
wenzelm@46466
   468
wenzelm@46466
   469
in
wenzelm@46466
   470
wenzelm@19915
   471
fun refine_goals print_rule inner raw_rules state =
wenzelm@19915
   472
  let
wenzelm@19915
   473
    val (outer, (_, goal)) = get_goal state;
wenzelm@20208
   474
    fun refine rule st = (print_rule outer rule; FINDGOAL (goal_tac rule) st);
wenzelm@19915
   475
  in
wenzelm@19915
   476
    raw_rules
wenzelm@42360
   477
    |> Proof_Context.goal_export inner outer
wenzelm@47068
   478
    |> (fn rules => Seq.lift provide_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   479
  end;
wenzelm@17359
   480
wenzelm@46466
   481
end;
wenzelm@46466
   482
wenzelm@6932
   483
wenzelm@17359
   484
(* conclude_goal *)
wenzelm@6932
   485
wenzelm@28627
   486
fun conclude_goal ctxt goal propss =
wenzelm@5820
   487
  let
wenzelm@42360
   488
    val thy = Proof_Context.theory_of ctxt;
wenzelm@24920
   489
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@32091
   490
    val string_of_thm = Display.string_of_thm ctxt;
wenzelm@5820
   491
wenzelm@17359
   492
    val ngoals = Thm.nprems_of goal;
wenzelm@19774
   493
    val _ = ngoals = 0 orelse error (Pretty.string_of (Pretty.chunks
wenzelm@39125
   494
      (Goal_Display.pretty_goals ctxt goal @
wenzelm@47065
   495
        [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)")])));
wenzelm@20224
   496
wenzelm@20224
   497
    val extra_hyps = Assumption.extra_hyps ctxt goal;
wenzelm@20224
   498
    val _ = null extra_hyps orelse
wenzelm@20224
   499
      error ("Additional hypotheses:\n" ^ cat_lines (map string_of_term extra_hyps));
wenzelm@5820
   500
wenzelm@23418
   501
    fun lost_structure () = error ("Lost goal structure:\n" ^ string_of_thm goal);
wenzelm@23418
   502
berghofe@23782
   503
    val th = Goal.conclude
wenzelm@23806
   504
      (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
berghofe@23782
   505
      handle THM _ => lost_structure ();
wenzelm@23418
   506
    val goal_propss = filter_out null propss;
wenzelm@23418
   507
    val results =
wenzelm@23418
   508
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   509
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@23418
   510
      handle THM _ => lost_structure ();
wenzelm@23418
   511
    val _ = Unify.matches_list thy (flat goal_propss) (map Thm.prop_of (flat results)) orelse
wenzelm@19774
   512
      error ("Proved a different theorem:\n" ^ string_of_thm th);
wenzelm@28627
   513
    val _ = Thm.check_shyps (Variable.sorts_of ctxt) th;
wenzelm@23418
   514
wenzelm@23418
   515
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   516
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   517
      | recover_result [] [] = []
wenzelm@23418
   518
      | recover_result _ _ = lost_structure ();
wenzelm@23418
   519
  in recover_result propss results end;
wenzelm@5820
   520
wenzelm@5820
   521
wenzelm@33288
   522
(* goal views -- corresponding to methods *)
wenzelm@33288
   523
wenzelm@33288
   524
fun raw_goal state =
wenzelm@33288
   525
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   526
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   527
wenzelm@33288
   528
val goal = raw_goal o refine_insert [];
wenzelm@33288
   529
wenzelm@33288
   530
fun simple_goal state =
wenzelm@33288
   531
  let
wenzelm@33288
   532
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   533
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   534
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   535
wenzelm@38721
   536
fun status_markup state =
wenzelm@38721
   537
  (case try goal state of
wenzelm@45666
   538
    SOME {goal, ...} => Isabelle_Markup.proof_state (Thm.nprems_of goal)
wenzelm@38721
   539
  | NONE => Markup.empty);
wenzelm@38721
   540
wenzelm@33288
   541
wenzelm@5820
   542
wenzelm@5820
   543
(*** structured proof commands ***)
wenzelm@5820
   544
wenzelm@17112
   545
(** context elements **)
wenzelm@5820
   546
wenzelm@36324
   547
(* let bindings *)
wenzelm@5820
   548
wenzelm@16813
   549
local
wenzelm@16813
   550
wenzelm@17359
   551
fun gen_bind bind args state =
wenzelm@5820
   552
  state
wenzelm@5820
   553
  |> assert_forward
wenzelm@36324
   554
  |> map_context (bind true args #> snd)
wenzelm@47068
   555
  |> reset_facts;
wenzelm@5820
   556
wenzelm@16813
   557
in
wenzelm@16813
   558
wenzelm@42360
   559
val let_bind = gen_bind Proof_Context.match_bind_i;
wenzelm@42360
   560
val let_bind_cmd = gen_bind Proof_Context.match_bind;
wenzelm@5820
   561
wenzelm@16813
   562
end;
wenzelm@16813
   563
wenzelm@5820
   564
wenzelm@36505
   565
(* concrete syntax *)
wenzelm@36505
   566
wenzelm@36505
   567
local
wenzelm@36505
   568
wenzelm@36507
   569
fun gen_write prep_arg mode args =
wenzelm@36507
   570
  assert_forward
wenzelm@42360
   571
  #> map_context (fn ctxt => ctxt |> Proof_Context.notation true mode (map (prep_arg ctxt) args))
wenzelm@47068
   572
  #> reset_facts;
wenzelm@36505
   573
wenzelm@36505
   574
in
wenzelm@36505
   575
wenzelm@36505
   576
val write = gen_write (K I);
wenzelm@36505
   577
wenzelm@36505
   578
val write_cmd =
wenzelm@36505
   579
  gen_write (fn ctxt => fn (c, mx) =>
wenzelm@42360
   580
    (Proof_Context.read_const ctxt false (Mixfix.mixfixT mx) c, mx));
wenzelm@36505
   581
wenzelm@36505
   582
end;
wenzelm@36505
   583
wenzelm@36505
   584
wenzelm@17359
   585
(* fix *)
wenzelm@9196
   586
wenzelm@12714
   587
local
wenzelm@12714
   588
wenzelm@30763
   589
fun gen_fix prep_vars args =
wenzelm@17359
   590
  assert_forward
wenzelm@45597
   591
  #> map_context (fn ctxt => snd (Proof_Context.add_fixes (fst (prep_vars args ctxt)) ctxt))
wenzelm@47068
   592
  #> reset_facts;
wenzelm@5820
   593
wenzelm@16813
   594
in
wenzelm@16813
   595
wenzelm@45597
   596
val fix = gen_fix Proof_Context.cert_vars;
wenzelm@45597
   597
val fix_cmd = gen_fix Proof_Context.read_vars;
wenzelm@5820
   598
wenzelm@16813
   599
end;
wenzelm@16813
   600
wenzelm@5820
   601
wenzelm@17359
   602
(* assume etc. *)
wenzelm@5820
   603
wenzelm@16813
   604
local
wenzelm@16813
   605
wenzelm@17112
   606
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   607
  state
wenzelm@5820
   608
  |> assert_forward
wenzelm@45390
   609
  |> map_context_result (asm exp (Attrib.map_specs (map (prep_att (theory_of state))) args))
wenzelm@17359
   610
  |> these_factss [] |> #2;
wenzelm@6932
   611
wenzelm@16813
   612
in
wenzelm@16813
   613
wenzelm@42360
   614
val assm = gen_assume Proof_Context.add_assms_i (K I);
wenzelm@42360
   615
val assm_cmd = gen_assume Proof_Context.add_assms Attrib.attribute;
wenzelm@36323
   616
val assume = assm Assumption.assume_export;
wenzelm@36323
   617
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   618
val presume = assm Assumption.presume_export;
wenzelm@36323
   619
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   620
wenzelm@16813
   621
end;
wenzelm@16813
   622
wenzelm@7271
   623
wenzelm@17359
   624
(* def *)
wenzelm@11891
   625
wenzelm@16813
   626
local
wenzelm@16813
   627
wenzelm@20913
   628
fun gen_def prep_att prep_vars prep_binds args state =
wenzelm@11891
   629
  let
wenzelm@11891
   630
    val _ = assert_forward state;
wenzelm@17112
   631
    val thy = theory_of state;
wenzelm@11891
   632
wenzelm@20913
   633
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@20913
   634
    val name_atts = map (apsnd (map (prep_att thy))) raw_name_atts;
wenzelm@11891
   635
  in
wenzelm@20913
   636
    state
wenzelm@20913
   637
    |> map_context_result (prep_vars (map (fn (x, mx) => (x, NONE, mx)) raw_vars))
wenzelm@20913
   638
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   639
    |-> (fn vars =>
wenzelm@20913
   640
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@35624
   641
      #-> (fn rhss => map_context_result (Local_Defs.add_defs (vars ~~ (name_atts ~~ rhss)))))
wenzelm@47068
   642
    |-> (set_facts o map (#2 o #2))
wenzelm@11891
   643
  end;
wenzelm@11891
   644
wenzelm@16813
   645
in
wenzelm@16813
   646
wenzelm@42360
   647
val def = gen_def (K I) Proof_Context.cert_vars Proof_Context.match_bind_i;
wenzelm@42360
   648
val def_cmd = gen_def Attrib.attribute Proof_Context.read_vars Proof_Context.match_bind;
wenzelm@11891
   649
wenzelm@16813
   650
end;
wenzelm@16813
   651
wenzelm@11891
   652
wenzelm@8374
   653
wenzelm@17112
   654
(** facts **)
wenzelm@5820
   655
wenzelm@17359
   656
(* chain *)
wenzelm@5820
   657
wenzelm@24011
   658
fun clean_facts ctxt =
wenzelm@47068
   659
  set_facts (filter_out Thm.is_dummy (the_facts ctxt)) ctxt;
wenzelm@24011
   660
wenzelm@17359
   661
val chain =
wenzelm@17359
   662
  assert_forward
wenzelm@24011
   663
  #> clean_facts
wenzelm@17359
   664
  #> enter_chain;
wenzelm@5820
   665
wenzelm@17359
   666
fun chain_facts facts =
wenzelm@47068
   667
  set_facts facts
wenzelm@17359
   668
  #> chain;
wenzelm@5820
   669
wenzelm@5820
   670
wenzelm@17359
   671
(* note etc. *)
wenzelm@17112
   672
haftmann@28965
   673
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   674
wenzelm@17112
   675
local
wenzelm@17112
   676
wenzelm@30760
   677
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   678
  state
wenzelm@17112
   679
  |> assert_forward
wenzelm@42360
   680
  |> map_context_result (Proof_Context.note_thmss ""
wenzelm@45390
   681
    (Attrib.map_facts_refs
wenzelm@45390
   682
      (map (prep_atts (theory_of state)))
wenzelm@45390
   683
      (prep_fact (context_of state)) args))
wenzelm@17112
   684
  |> these_factss (more_facts state)
wenzelm@17359
   685
  ||> opt_chain
wenzelm@17359
   686
  |> opt_result;
wenzelm@17112
   687
wenzelm@17112
   688
in
wenzelm@17112
   689
wenzelm@36323
   690
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@42360
   691
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute Proof_Context.get_fact;
wenzelm@17112
   692
wenzelm@36323
   693
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@42360
   694
val from_thmss_cmd = gen_thmss (K []) chain #2 Attrib.attribute Proof_Context.get_fact o no_binding;
wenzelm@17359
   695
wenzelm@36323
   696
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@42360
   697
val with_thmss_cmd = gen_thmss the_facts chain #2 Attrib.attribute Proof_Context.get_fact o no_binding;
wenzelm@30760
   698
wenzelm@30760
   699
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   700
wenzelm@17112
   701
end;
wenzelm@17112
   702
wenzelm@17112
   703
wenzelm@18548
   704
(* using/unfolding *)
wenzelm@17112
   705
wenzelm@17112
   706
local
wenzelm@17112
   707
wenzelm@45390
   708
fun gen_using f g prep_att prep_fact args state =
wenzelm@17112
   709
  state
wenzelm@17112
   710
  |> assert_backward
wenzelm@21442
   711
  |> map_context_result
wenzelm@42360
   712
    (Proof_Context.note_thmss ""
wenzelm@45390
   713
      (Attrib.map_facts_refs (map (prep_att (theory_of state))) (prep_fact (context_of state))
wenzelm@30760
   714
        (no_binding args)))
wenzelm@18843
   715
  |> (fn (named_facts, state') =>
wenzelm@24556
   716
    state' |> map_goal I (fn (statement, _, using, goal, before_qed, after_qed) =>
wenzelm@18843
   717
      let
wenzelm@18843
   718
        val ctxt = context_of state';
wenzelm@19482
   719
        val ths = maps snd named_facts;
wenzelm@24556
   720
      in (statement, [], f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   721
wenzelm@24050
   722
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   723
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   724
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   725
wenzelm@17112
   726
in
wenzelm@17112
   727
wenzelm@36323
   728
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@42360
   729
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute Proof_Context.get_fact;
wenzelm@36323
   730
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@42360
   731
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute Proof_Context.get_fact;
wenzelm@17112
   732
wenzelm@17112
   733
end;
wenzelm@17112
   734
wenzelm@17112
   735
wenzelm@17112
   736
(* case *)
wenzelm@17112
   737
wenzelm@17112
   738
local
wenzelm@17112
   739
wenzelm@30419
   740
fun qualified_binding a =
wenzelm@30419
   741
  Binding.qualify true (Long_Name.qualifier a) (Binding.name (Long_Name.base_name a));
wenzelm@30419
   742
wenzelm@17112
   743
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   744
  let
wenzelm@17112
   745
    val atts = map (prep_att (theory_of state)) raw_atts;
wenzelm@19078
   746
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@42360
   747
      ctxt |> Proof_Context.apply_case (Proof_Context.get_case ctxt name xs));
wenzelm@30419
   748
    val assumptions = asms |> map (fn (a, ts) => ((qualified_binding a, atts), map (rpair []) ts));
wenzelm@17112
   749
  in
wenzelm@17112
   750
    state'
wenzelm@36323
   751
    |> assume assumptions
wenzelm@33386
   752
    |> bind_terms Auto_Bind.no_facts
wenzelm@36323
   753
    |> `the_facts |-> (fn thms => note_thmss [((Binding.name name, []), [(thms, [])])])
wenzelm@17112
   754
  end;
wenzelm@17112
   755
wenzelm@17112
   756
in
wenzelm@17112
   757
wenzelm@36323
   758
val invoke_case = gen_invoke_case (K I);
wenzelm@36323
   759
val invoke_case_cmd = gen_invoke_case Attrib.attribute;
wenzelm@17112
   760
wenzelm@17112
   761
end;
wenzelm@17112
   762
wenzelm@17112
   763
wenzelm@17112
   764
wenzelm@17359
   765
(** proof structure **)
wenzelm@17359
   766
wenzelm@17359
   767
(* blocks *)
wenzelm@17359
   768
wenzelm@17359
   769
val begin_block =
wenzelm@17359
   770
  assert_forward
wenzelm@17359
   771
  #> open_block
wenzelm@47068
   772
  #> reset_goal
wenzelm@17359
   773
  #> open_block;
wenzelm@17359
   774
wenzelm@17359
   775
val next_block =
wenzelm@17359
   776
  assert_forward
wenzelm@17359
   777
  #> close_block
wenzelm@17359
   778
  #> open_block
wenzelm@47068
   779
  #> reset_goal
wenzelm@47068
   780
  #> reset_facts;
wenzelm@17359
   781
wenzelm@17359
   782
fun end_block state =
wenzelm@17359
   783
  state
wenzelm@17359
   784
  |> assert_forward
wenzelm@40960
   785
  |> assert_bottom false
wenzelm@17359
   786
  |> close_block
wenzelm@17359
   787
  |> assert_current_goal false
wenzelm@17359
   788
  |> close_block
wenzelm@17359
   789
  |> export_facts state;
wenzelm@17359
   790
wenzelm@17359
   791
wenzelm@40960
   792
(* global notepad *)
wenzelm@40960
   793
wenzelm@40960
   794
val begin_notepad =
wenzelm@40960
   795
  init
wenzelm@40960
   796
  #> open_block
wenzelm@40960
   797
  #> map_context (Variable.set_body true)
wenzelm@40960
   798
  #> open_block;
wenzelm@40960
   799
wenzelm@40960
   800
val end_notepad =
wenzelm@40960
   801
  assert_forward
wenzelm@40960
   802
  #> assert_bottom true
wenzelm@40960
   803
  #> close_block
wenzelm@40960
   804
  #> assert_current_goal false
wenzelm@40960
   805
  #> close_block
wenzelm@40960
   806
  #> context_of;
wenzelm@40960
   807
wenzelm@40960
   808
wenzelm@17359
   809
(* sub-proofs *)
wenzelm@17359
   810
wenzelm@17359
   811
fun proof opt_text =
wenzelm@17359
   812
  assert_backward
wenzelm@17859
   813
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   814
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   815
wenzelm@23360
   816
fun end_proof bot txt state =
wenzelm@23360
   817
  state
wenzelm@23360
   818
  |> assert_forward
wenzelm@23360
   819
  |> assert_bottom bot
wenzelm@23360
   820
  |> close_block
wenzelm@23360
   821
  |> assert_current_goal true
wenzelm@23360
   822
  |> using_facts []
wenzelm@23360
   823
  |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@32193
   824
  |> Seq.maps (refine (Method.finish_text txt));
wenzelm@17359
   825
wenzelm@29383
   826
fun check_result msg sq =
wenzelm@29383
   827
  (case Seq.pull sq of
wenzelm@29383
   828
    NONE => error msg
wenzelm@29383
   829
  | SOME (s, _) => s);
wenzelm@29383
   830
wenzelm@29383
   831
fun check_finish sq = check_result "Failed to finish proof" sq;
wenzelm@29383
   832
wenzelm@17359
   833
wenzelm@17359
   834
(* unstructured refinement *)
wenzelm@17359
   835
wenzelm@32193
   836
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i)));
wenzelm@32193
   837
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i)));
wenzelm@17359
   838
wenzelm@17359
   839
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@17359
   840
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   841
wenzelm@17359
   842
wenzelm@17359
   843
wenzelm@17112
   844
(** goals **)
wenzelm@17112
   845
wenzelm@17359
   846
(* generic goals *)
wenzelm@17359
   847
wenzelm@19774
   848
local
wenzelm@19774
   849
wenzelm@36322
   850
val is_var =
wenzelm@36322
   851
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   852
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   853
wenzelm@36322
   854
fun implicit_vars props =
wenzelm@19846
   855
  let
wenzelm@36354
   856
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   857
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   858
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   859
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   860
wenzelm@19774
   861
fun refine_terms n =
wenzelm@30510
   862
  refine (Method.Basic (K (RAW_METHOD
wenzelm@19774
   863
    (K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@32193
   864
      (HEADGOAL (CONJUNCTS (ALLGOALS (rtac Drule.termI))))))))))
wenzelm@19774
   865
  #> Seq.hd;
wenzelm@19774
   866
wenzelm@19774
   867
in
wenzelm@17359
   868
wenzelm@17859
   869
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   870
  let
wenzelm@17359
   871
    val thy = theory_of state;
wenzelm@23418
   872
    val cert = Thm.cterm_of thy;
wenzelm@17359
   873
    val chaining = can assert_chain state;
wenzelm@25958
   874
    val pos = Position.thread_data ();
wenzelm@17359
   875
wenzelm@17359
   876
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   877
      state
wenzelm@5936
   878
      |> assert_forward_or_chain
wenzelm@5936
   879
      |> enter_forward
wenzelm@17359
   880
      |> open_block
wenzelm@45327
   881
      |> map_context_result (prepp raw_propp);
wenzelm@19482
   882
    val props = flat propss;
wenzelm@15703
   883
wenzelm@36322
   884
    val vars = implicit_vars props;
wenzelm@36322
   885
    val propss' = vars :: propss;
wenzelm@23418
   886
    val goal_propss = filter_out null propss';
wenzelm@29346
   887
    val goal =
wenzelm@29346
   888
      cert (Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss))
wenzelm@28627
   889
      |> Thm.weaken_sorts (Variable.sorts_of (context_of goal_state));
wenzelm@29346
   890
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@18124
   891
    val after_qed' = after_qed |>> (fn after_local =>
wenzelm@18124
   892
      fn results => map_context after_ctxt #> after_local results);
wenzelm@5820
   893
  in
wenzelm@17359
   894
    goal_state
wenzelm@21727
   895
    |> map_context (init_context #> Variable.set_body true)
wenzelm@47068
   896
    |> set_goal (make_goal (statement, [], [], Goal.init goal, before_qed, after_qed'))
wenzelm@42360
   897
    |> map_context (Proof_Context.auto_bind_goal props)
wenzelm@21565
   898
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@47068
   899
    |> reset_facts
wenzelm@17359
   900
    |> open_block
wenzelm@47068
   901
    |> reset_goal
wenzelm@5820
   902
    |> enter_backward
wenzelm@23418
   903
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@32193
   904
    |> null props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   905
  end;
wenzelm@5820
   906
wenzelm@29090
   907
fun generic_qed after_ctxt state =
wenzelm@12503
   908
  let
wenzelm@36354
   909
    val (goal_ctxt, {statement = (_, stmt, _), goal, after_qed, ...}) = current_goal state;
wenzelm@8617
   910
    val outer_state = state |> close_block;
wenzelm@8617
   911
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   912
wenzelm@18503
   913
    val props =
wenzelm@19774
   914
      flat (tl stmt)
wenzelm@19915
   915
      |> Variable.exportT_terms goal_ctxt outer_ctxt;
wenzelm@17359
   916
    val results =
wenzelm@28627
   917
      tl (conclude_goal goal_ctxt goal stmt)
wenzelm@42360
   918
      |> burrow (Proof_Context.export goal_ctxt outer_ctxt);
wenzelm@17359
   919
  in
wenzelm@17359
   920
    outer_state
wenzelm@29090
   921
    |> map_context (after_ctxt props)
wenzelm@34239
   922
    |> pair (after_qed, results)
wenzelm@17359
   923
  end;
wenzelm@17359
   924
wenzelm@19774
   925
end;
wenzelm@19774
   926
wenzelm@9469
   927
wenzelm@17359
   928
(* local goals *)
wenzelm@17359
   929
wenzelm@17859
   930
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   931
  let
wenzelm@21362
   932
    val thy = theory_of state;
wenzelm@21362
   933
    val ((names, attss), propp) =
wenzelm@45390
   934
      Attrib.map_specs (map (prep_att thy)) stmt |> split_list |>> split_list;
wenzelm@17359
   935
wenzelm@18124
   936
    fun after_qed' results =
wenzelm@18808
   937
      local_results ((names ~~ attss) ~~ results)
wenzelm@17359
   938
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@18124
   939
      #> after_qed results;
wenzelm@5820
   940
  in
wenzelm@17359
   941
    state
wenzelm@21362
   942
    |> generic_goal prepp kind before_qed (after_qed', K I) propp
wenzelm@19900
   943
    |> tap (Variable.warn_extra_tfrees (context_of state) o context_of)
wenzelm@5820
   944
  end;
wenzelm@5820
   945
wenzelm@29383
   946
fun local_qeds txt =
wenzelm@29383
   947
  end_proof false txt
wenzelm@42360
   948
  #> Seq.map (generic_qed Proof_Context.auto_bind_facts #->
wenzelm@29090
   949
    (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
   950
wenzelm@29383
   951
fun local_qed txt = local_qeds txt #> check_finish;
wenzelm@29383
   952
wenzelm@5820
   953
wenzelm@17359
   954
(* global goals *)
wenzelm@17359
   955
wenzelm@45327
   956
fun prepp_auto_fixes prepp args =
wenzelm@45327
   957
  prepp args #>
wenzelm@45327
   958
  (fn ((propss, a), ctxt) => ((propss, a), (fold o fold) Variable.auto_fixes propss ctxt));
wenzelm@45327
   959
wenzelm@45327
   960
fun global_goal prepp before_qed after_qed propp =
wenzelm@45327
   961
  init #>
wenzelm@45327
   962
  generic_goal (prepp_auto_fixes prepp) "" before_qed (K I, after_qed) propp;
wenzelm@17359
   963
wenzelm@42360
   964
val theorem = global_goal Proof_Context.bind_propp_schematic_i;
wenzelm@42360
   965
val theorem_cmd = global_goal Proof_Context.bind_propp_schematic;
wenzelm@12065
   966
wenzelm@17359
   967
fun global_qeds txt =
wenzelm@17359
   968
  end_proof true txt
wenzelm@29090
   969
  #> Seq.map (generic_qed (K I) #> (fn (((_, after_qed), results), state) =>
wenzelm@29383
   970
    after_qed results (context_of state)));
wenzelm@17112
   971
wenzelm@29383
   972
fun global_qed txt = global_qeds txt #> check_finish;
wenzelm@12959
   973
wenzelm@5820
   974
wenzelm@17359
   975
(* concluding steps *)
wenzelm@17112
   976
wenzelm@29383
   977
fun terminal_proof qed initial terminal =
wenzelm@29383
   978
  proof (SOME initial) #> Seq.maps (qed terminal) #> check_finish;
wenzelm@17112
   979
wenzelm@29383
   980
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@17112
   981
val local_default_proof = local_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   982
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   983
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   984
val local_done_proof = terminal_proof local_qeds Method.done_text (NONE, false);
wenzelm@17112
   985
wenzelm@29383
   986
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@17112
   987
val global_default_proof = global_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   988
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   989
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   990
val global_done_proof = terminal_proof global_qeds Method.done_text (NONE, false);
wenzelm@5820
   991
wenzelm@6896
   992
wenzelm@17359
   993
(* common goal statements *)
wenzelm@17359
   994
wenzelm@17359
   995
local
wenzelm@17359
   996
wenzelm@17859
   997
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@46728
   998
  local_goal (Proof_Display.print_results Isabelle_Markup.state int)
wenzelm@46728
   999
    prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
  1000
wenzelm@17859
  1001
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
  1002
  let
wenzelm@32738
  1003
    val testing = Unsynchronized.ref false;
wenzelm@32738
  1004
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
  1005
    fun fail_msg ctxt =
wenzelm@29383
  1006
      "Local statement will fail to refine any pending goal" ::
wenzelm@33389
  1007
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
  1008
      |> cat_lines;
wenzelm@6896
  1009
wenzelm@17359
  1010
    fun print_results ctxt res =
wenzelm@46728
  1011
      if ! testing then ()
wenzelm@46728
  1012
      else Proof_Display.print_results Isabelle_Markup.state int ctxt res;
wenzelm@17359
  1013
    fun print_rule ctxt th =
wenzelm@17359
  1014
      if ! testing then rule := SOME th
wenzelm@41181
  1015
      else if int then
wenzelm@45666
  1016
        writeln
wenzelm@45666
  1017
          (Markup.markup Isabelle_Markup.state (Proof_Display.string_of_rule ctxt "Successful" th))
wenzelm@17359
  1018
      else ();
wenzelm@17359
  1019
    val test_proof =
wenzelm@29383
  1020
      try (local_skip_proof true)
wenzelm@39616
  1021
      |> Unsynchronized.setmp testing true
wenzelm@42042
  1022
      |> Exn.interruptible_capture;
wenzelm@6896
  1023
wenzelm@18124
  1024
    fun after_qed' results =
wenzelm@19482
  1025
      refine_goals print_rule (context_of state) (flat results)
wenzelm@29383
  1026
      #> check_result "Failed to refine any pending goal"
wenzelm@29383
  1027
      #> after_qed results;
wenzelm@17359
  1028
  in
wenzelm@17359
  1029
    state
wenzelm@17859
  1030
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@21565
  1031
    |> int ? (fn goal_state =>
wenzelm@17359
  1032
      (case test_proof goal_state of
wenzelm@43761
  1033
        Exn.Res (SOME _) => goal_state
wenzelm@43761
  1034
      | Exn.Res NONE => error (fail_msg (context_of goal_state))
wenzelm@42042
  1035
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1036
  end;
wenzelm@17359
  1037
wenzelm@17359
  1038
in
wenzelm@17359
  1039
wenzelm@42360
  1040
val have = gen_have (K I) Proof_Context.bind_propp_i;
wenzelm@42360
  1041
val have_cmd = gen_have Attrib.attribute Proof_Context.bind_propp;
wenzelm@42360
  1042
val show = gen_show (K I) Proof_Context.bind_propp_i;
wenzelm@42360
  1043
val show_cmd = gen_show Attrib.attribute Proof_Context.bind_propp;
wenzelm@6896
  1044
wenzelm@5820
  1045
end;
wenzelm@17359
  1046
wenzelm@28410
  1047
wenzelm@29385
  1048
wenzelm@29385
  1049
(** future proofs **)
wenzelm@29385
  1050
wenzelm@29385
  1051
(* relevant proof states *)
wenzelm@29385
  1052
wenzelm@29385
  1053
fun is_schematic t =
wenzelm@29385
  1054
  Term.exists_subterm Term.is_Var t orelse
wenzelm@29385
  1055
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@29385
  1056
wenzelm@29385
  1057
fun schematic_goal state =
wenzelm@29385
  1058
  let val (_, (_, {statement = (_, _, prop), ...})) = find_goal state
wenzelm@29385
  1059
  in is_schematic prop end;
wenzelm@29385
  1060
wenzelm@29385
  1061
fun is_relevant state =
wenzelm@29385
  1062
  (case try find_goal state of
wenzelm@29385
  1063
    NONE => true
wenzelm@29385
  1064
  | SOME (_, (_, {statement = (_, _, prop), goal, ...})) =>
wenzelm@29385
  1065
      is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1066
wenzelm@29385
  1067
wenzelm@29385
  1068
(* full proofs *)
wenzelm@28410
  1069
wenzelm@29346
  1070
local
wenzelm@29346
  1071
wenzelm@29385
  1072
fun future_proof done get_context fork_proof state =
wenzelm@28410
  1073
  let
wenzelm@29381
  1074
    val _ = assert_backward state;
wenzelm@28410
  1075
    val (goal_ctxt, (_, goal)) = find_goal state;
wenzelm@32786
  1076
    val {statement as (kind, _, prop), messages, using, goal, before_qed, after_qed} = goal;
wenzelm@32062
  1077
    val goal_txt = prop :: map Thm.term_of (Assumption.all_assms_of goal_ctxt);
wenzelm@28410
  1078
wenzelm@29383
  1079
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1080
wenzelm@29346
  1081
    val prop' = Logic.protect prop;
wenzelm@29346
  1082
    val statement' = (kind, [[], [prop']], prop');
wenzelm@29346
  1083
    val goal' = Thm.adjust_maxidx_thm (Thm.maxidx_of goal)
wenzelm@29346
  1084
      (Drule.comp_no_flatten (goal, Thm.nprems_of goal) 1 Drule.protectI);
wenzelm@29385
  1085
wenzelm@33386
  1086
    fun after_local' [[th]] = put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@42360
  1087
    fun after_global' [[th]] = Proof_Context.put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@29385
  1088
    val after_qed' = (after_local', after_global');
wenzelm@42360
  1089
    val this_name = Proof_Context.full_name goal_ctxt (Binding.name Auto_Bind.thisN);
wenzelm@28410
  1090
wenzelm@28973
  1091
    val result_ctxt =
wenzelm@28973
  1092
      state
wenzelm@32062
  1093
      |> map_contexts (fold Variable.declare_term goal_txt)
wenzelm@29385
  1094
      |> map_goal I (K (statement', messages, using, goal', before_qed, after_qed'))
wenzelm@29346
  1095
      |> fork_proof;
wenzelm@28973
  1096
wenzelm@29385
  1097
    val future_thm = result_ctxt |> Future.map (fn (_, x) =>
wenzelm@42360
  1098
      Proof_Context.get_fact_single (get_context x) (Facts.named this_name));
wenzelm@32062
  1099
    val finished_goal = Goal.future_result goal_ctxt future_thm prop';
wenzelm@28973
  1100
    val state' =
wenzelm@28973
  1101
      state
wenzelm@28973
  1102
      |> map_goal I (K (statement, messages, using, finished_goal, NONE, after_qed))
wenzelm@29385
  1103
      |> done;
wenzelm@28973
  1104
  in (Future.map #1 result_ctxt, state') end;
wenzelm@28410
  1105
wenzelm@29385
  1106
in
wenzelm@29385
  1107
wenzelm@29385
  1108
fun local_future_proof x = future_proof local_done_proof context_of x;
wenzelm@29385
  1109
fun global_future_proof x = future_proof global_done_proof I x;
wenzelm@29385
  1110
wenzelm@17359
  1111
end;
wenzelm@29346
  1112
wenzelm@29385
  1113
wenzelm@29385
  1114
(* terminal proofs *)
wenzelm@29385
  1115
wenzelm@29385
  1116
local
wenzelm@29385
  1117
wenzelm@29385
  1118
fun future_terminal_proof proof1 proof2 meths int state =
wenzelm@38218
  1119
  if int orelse is_relevant state orelse not (Goal.local_future_enabled ())
wenzelm@29385
  1120
  then proof1 meths state
wenzelm@41677
  1121
  else snd (proof2 (fn state' =>
wenzelm@41677
  1122
    Goal.fork_name "Proof.future_terminal_proof" (fn () => ((), proof1 meths state'))) state);
wenzelm@29385
  1123
wenzelm@29385
  1124
in
wenzelm@29385
  1125
wenzelm@29385
  1126
fun local_future_terminal_proof x =
wenzelm@29385
  1127
  future_terminal_proof local_terminal_proof local_future_proof x;
wenzelm@29385
  1128
wenzelm@29385
  1129
fun global_future_terminal_proof x =
wenzelm@29385
  1130
  future_terminal_proof global_terminal_proof global_future_proof x;
wenzelm@29350
  1131
wenzelm@29346
  1132
end;
wenzelm@29346
  1133
wenzelm@29385
  1134
end;
wenzelm@29385
  1135