src/Pure/Isar/proof.ML
author wenzelm
Fri Jun 17 18:33:35 2005 +0200 (2005-06-17)
changeset 16450 66667013ca6e
parent 16169 b59202511b8a
child 16539 60adb8b28377
permissions -rw-r--r--
RuleCases.tactic;
accomodate identification of type Sign.sg and theory;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     4
wenzelm@15757
     5
The Isar/VM proof language interpreter.
wenzelm@5820
     6
*)
wenzelm@5820
     7
paulson@15194
     8
paulson@15194
     9
(*Jia Meng: added in atp_hook and modified enter_forward. *)
paulson@15194
    10
wenzelm@8152
    11
signature BASIC_PROOF =
wenzelm@8152
    12
sig
wenzelm@8374
    13
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8374
    14
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8152
    15
end;
wenzelm@8152
    16
wenzelm@5820
    17
signature PROOF =
wenzelm@5820
    18
sig
wenzelm@8152
    19
  include BASIC_PROOF
wenzelm@5820
    20
  type context
wenzelm@5820
    21
  type state
berghofe@14129
    22
  datatype mode = Forward | ForwardChain | Backward
wenzelm@5820
    23
  exception STATE of string * state
wenzelm@6871
    24
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
wenzelm@12045
    25
  val init_state: theory -> state
wenzelm@5820
    26
  val context_of: state -> context
wenzelm@5820
    27
  val theory_of: state -> theory
wenzelm@16450
    28
  val sign_of: state -> theory    (*obsolete*)
wenzelm@13377
    29
  val map_context: (context -> context) -> state -> state
wenzelm@7924
    30
  val warn_extra_tfrees: state -> state -> state
wenzelm@14554
    31
  val put_thms: string * thm list -> state -> state
wenzelm@7605
    32
  val reset_thms: string -> state -> state
wenzelm@6091
    33
  val the_facts: state -> thm list
wenzelm@9469
    34
  val the_fact: state -> thm
wenzelm@11079
    35
  val get_goal: state -> context * (thm list * thm)
wenzelm@6091
    36
  val goal_facts: (state -> thm list) -> state -> state
berghofe@14129
    37
  val get_mode: state -> mode
wenzelm@9469
    38
  val is_chain: state -> bool
wenzelm@6891
    39
  val assert_forward: state -> state
wenzelm@9469
    40
  val assert_forward_or_chain: state -> state
wenzelm@5820
    41
  val assert_backward: state -> state
wenzelm@8206
    42
  val assert_no_chain: state -> state
paulson@15452
    43
  val atp_hook: (ProofContext.context * Thm.thm -> unit) ref
wenzelm@5820
    44
  val enter_forward: state -> state
wenzelm@6529
    45
  val verbose: bool ref
nipkow@13698
    46
  val show_main_goal: bool ref
wenzelm@12423
    47
  val pretty_state: int -> state -> Pretty.T list
wenzelm@10360
    48
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@6776
    49
  val level: state -> int
wenzelm@5820
    50
  type method
wenzelm@6848
    51
  val method: (thm list -> tactic) -> method
wenzelm@16450
    52
  val method_cases: (thm list -> RuleCases.tactic) -> method
wenzelm@5820
    53
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    54
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@5936
    55
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    56
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@8617
    57
  val let_bind: (string list * string) list -> state -> state
wenzelm@8617
    58
  val let_bind_i: (term list * term) list -> state -> state
wenzelm@14564
    59
  val simple_note_thms: string -> thm list -> state -> state
wenzelm@14564
    60
  val note_thmss: ((bstring * context attribute list) *
berghofe@15456
    61
    (thmref * context attribute list) list) list -> state -> state
wenzelm@14564
    62
  val note_thmss_i: ((bstring * context attribute list) *
wenzelm@9196
    63
    (thm list * context attribute list) list) list -> state -> state
wenzelm@12714
    64
  val with_thmss: ((bstring * context attribute list) *
berghofe@15456
    65
    (thmref * context attribute list) list) list -> state -> state
wenzelm@12714
    66
  val with_thmss_i: ((bstring * context attribute list) *
wenzelm@9196
    67
    (thm list * context attribute list) list) list -> state -> state
berghofe@15456
    68
  val using_thmss: ((thmref * context attribute list) list) list -> state -> state
wenzelm@12930
    69
  val using_thmss_i: ((thm list * context attribute list) list) list -> state -> state
wenzelm@7412
    70
  val fix: (string list * string option) list -> state -> state
wenzelm@7665
    71
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@9469
    72
  val assm: ProofContext.exporter
wenzelm@10464
    73
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    74
    -> state -> state
wenzelm@9469
    75
  val assm_i: ProofContext.exporter
wenzelm@10464
    76
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    77
    -> state -> state
wenzelm@10464
    78
  val assume:
wenzelm@10464
    79
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    80
    -> state -> state
wenzelm@10464
    81
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    82
    -> state -> state
wenzelm@10464
    83
  val presume:
wenzelm@10464
    84
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@6932
    85
    -> state -> state
wenzelm@10464
    86
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    87
    -> state -> state
wenzelm@11891
    88
  val def: string -> context attribute list -> string *  (string * string list) -> state -> state
wenzelm@11891
    89
  val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
wenzelm@11793
    90
  val invoke_case: string * string option list * context attribute list -> state -> state
wenzelm@15703
    91
  val multi_theorem: string -> (theory -> theory) ->
ballarin@15696
    92
    (cterm list -> context -> context -> thm -> thm) ->
wenzelm@15703
    93
    (xstring * (Attrib.src list * Attrib.src list list)) option ->
wenzelm@15703
    94
    bstring * theory attribute list -> Locale.element Locale.elem_expr list
wenzelm@12146
    95
    -> ((bstring * theory attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
    96
    -> theory -> state
wenzelm@15703
    97
  val multi_theorem_i: string -> (theory -> theory) ->
ballarin@15696
    98
    (cterm list -> context -> context -> thm -> thm) ->
wenzelm@15703
    99
    (string * (Attrib.src list * Attrib.src list list)) option
wenzelm@12959
   100
    -> bstring * theory attribute list
wenzelm@15703
   101
    -> Locale.element_i Locale.elem_expr list
wenzelm@12146
   102
    -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
   103
    -> theory -> state
wenzelm@5820
   104
  val chain: state -> state
wenzelm@6091
   105
  val from_facts: thm list -> state -> state
wenzelm@12971
   106
  val show: (bool -> state -> state) -> (state -> state Seq.seq) -> bool
wenzelm@12146
   107
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
   108
    -> bool -> state -> state
wenzelm@12971
   109
  val show_i: (bool -> state -> state) -> (state -> state Seq.seq) -> bool
wenzelm@12146
   110
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
   111
    -> bool -> state -> state
wenzelm@12971
   112
  val have: (state -> state Seq.seq) -> bool
wenzelm@12146
   113
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@12146
   114
    -> state -> state
wenzelm@12971
   115
  val have_i: (state -> state Seq.seq) -> bool
wenzelm@12146
   116
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@12146
   117
    -> state -> state
wenzelm@6404
   118
  val at_bottom: state -> bool
wenzelm@6982
   119
  val local_qed: (state -> state Seq.seq)
wenzelm@12146
   120
    -> (context -> string * (string * thm list) list -> unit) * (context -> thm -> unit)
wenzelm@12146
   121
    -> state -> state Seq.seq
wenzelm@6950
   122
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@12244
   123
    -> (theory * ((string * string) * (string * thm list) list)) Seq.seq
wenzelm@6896
   124
  val begin_block: state -> state
wenzelm@6932
   125
  val end_block: state -> state Seq.seq
wenzelm@6896
   126
  val next_block: state -> state
kleing@14548
   127
  val thisN: string
paulson@15194
   128
  val call_atp: bool ref;
wenzelm@5820
   129
end;
wenzelm@5820
   130
wenzelm@13377
   131
structure Proof: PROOF =
wenzelm@5820
   132
struct
wenzelm@5820
   133
paulson@15194
   134
(*Jia Meng: by default, no ATP is called. *)
paulson@15194
   135
val call_atp = ref false;
wenzelm@5820
   136
wenzelm@5820
   137
(** proof state **)
wenzelm@5820
   138
wenzelm@5820
   139
type context = ProofContext.context;
wenzelm@5820
   140
wenzelm@5820
   141
wenzelm@5820
   142
(* type goal *)
wenzelm@5820
   143
wenzelm@5820
   144
datatype kind =
wenzelm@12959
   145
  Theorem of {kind: string,
wenzelm@12959
   146
    theory_spec: (bstring * theory attribute list) * theory attribute list list,
wenzelm@15703
   147
    locale_spec: (string * (Attrib.src list * Attrib.src list list)) option,
wenzelm@15703
   148
    view: cterm list * cterm list,  (* target view and includes view *)
wenzelm@15703
   149
    export: cterm list -> context -> context -> thm -> thm} |
ballarin@15696
   150
                                   (* exporter to be used in finish_global *)
wenzelm@12959
   151
  Show of context attribute list list |
wenzelm@15703
   152
  Have of context attribute list list;
ballarin@15099
   153
wenzelm@13415
   154
fun kind_name _ (Theorem {kind = s, theory_spec = ((a, _), _),
ballarin@15596
   155
        locale_spec = NONE, ...}) = s ^ (if a = "" then "" else " " ^ a)
wenzelm@16450
   156
  | kind_name thy (Theorem {kind = s, theory_spec = ((a, _), _),
ballarin@15596
   157
        locale_spec = SOME (name, _), ...}) =
wenzelm@16450
   158
      s ^ " (in " ^ Locale.extern thy name ^ ")" ^ (if a = "" then "" else " " ^ a)
wenzelm@12015
   159
  | kind_name _ (Show _) = "show"
wenzelm@15703
   160
  | kind_name _ (Have _) = "have";
wenzelm@5820
   161
wenzelm@5820
   162
type goal =
wenzelm@12146
   163
 (kind *             (*result kind*)
wenzelm@12146
   164
  string list *      (*result names*)
wenzelm@12146
   165
  term list list) *  (*result statements*)
wenzelm@12146
   166
 (thm list *         (*use facts*)
wenzelm@12146
   167
  thm);              (*goal: subgoals ==> statement*)
wenzelm@5820
   168
wenzelm@5820
   169
wenzelm@5820
   170
(* type mode *)
wenzelm@5820
   171
wenzelm@5820
   172
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   173
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   174
wenzelm@5820
   175
wenzelm@5820
   176
(* datatype state *)
wenzelm@5820
   177
wenzelm@7176
   178
datatype node =
wenzelm@7176
   179
  Node of
wenzelm@7176
   180
   {context: context,
wenzelm@7176
   181
    facts: thm list option,
wenzelm@7176
   182
    mode: mode,
wenzelm@15703
   183
    goal: (goal * (after_qed * bool)) option}
wenzelm@7176
   184
and state =
wenzelm@5820
   185
  State of
wenzelm@5820
   186
    node *              (*current*)
wenzelm@15703
   187
    node list           (*parents wrt. block structure*)
wenzelm@15703
   188
and after_qed =
wenzelm@15703
   189
  AfterQed of (state -> state Seq.seq) * (theory -> theory);
wenzelm@5820
   190
wenzelm@7176
   191
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   192
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   193
wenzelm@7176
   194
wenzelm@5820
   195
exception STATE of string * state;
wenzelm@5820
   196
wenzelm@5820
   197
fun err_malformed name state =
wenzelm@5820
   198
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   199
wenzelm@6871
   200
fun check_result msg state sq =
wenzelm@6871
   201
  (case Seq.pull sq of
skalberg@15531
   202
    NONE => raise STATE (msg, state)
skalberg@15531
   203
  | SOME s_sq => Seq.cons s_sq);
wenzelm@6871
   204
wenzelm@5820
   205
wenzelm@7176
   206
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   207
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   208
wenzelm@12045
   209
fun init_state thy =
skalberg@15531
   210
  State (make_node (ProofContext.init thy, NONE, Forward, NONE), []);
wenzelm@12045
   211
wenzelm@5820
   212
wenzelm@5820
   213
wenzelm@5820
   214
(** basic proof state operations **)
wenzelm@5820
   215
wenzelm@5820
   216
(* context *)
wenzelm@5820
   217
wenzelm@7176
   218
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   219
val theory_of = ProofContext.theory_of o context_of;
wenzelm@16450
   220
val sign_of = theory_of;
wenzelm@5820
   221
wenzelm@5820
   222
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   223
wenzelm@7176
   224
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   225
  let val (context', result) = f context
wenzelm@5820
   226
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   227
wenzelm@5820
   228
wenzelm@7924
   229
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@10809
   230
val add_binds_i = map_context o ProofContext.add_binds_i;
wenzelm@6790
   231
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@12146
   232
val auto_bind_facts = map_context o ProofContext.auto_bind_facts;
wenzelm@6091
   233
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   234
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   235
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@8374
   236
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   237
wenzelm@5820
   238
wenzelm@5820
   239
(* facts *)
wenzelm@5820
   240
skalberg@15531
   241
fun the_facts (State (Node {facts = SOME facts, ...}, _)) = facts
wenzelm@5820
   242
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   243
wenzelm@9469
   244
fun the_fact state =
wenzelm@9469
   245
  (case the_facts state of
wenzelm@9469
   246
    [thm] => thm
wenzelm@9469
   247
  | _ => raise STATE ("Single fact expected", state));
wenzelm@9469
   248
wenzelm@6848
   249
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   250
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   251
wenzelm@7605
   252
wenzelm@7605
   253
val thisN = "this";
wenzelm@7605
   254
wenzelm@5820
   255
fun put_facts facts state =
wenzelm@5820
   256
  state
wenzelm@5820
   257
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
skalberg@15531
   258
  |> (case facts of NONE => reset_thms thisN | SOME ths => put_thms (thisN, ths));
wenzelm@5820
   259
skalberg@15531
   260
val reset_facts = put_facts NONE;
wenzelm@5820
   261
wenzelm@9196
   262
fun these_factss (state, named_factss) =
skalberg@15570
   263
  state |> put_facts (SOME (List.concat (map snd named_factss)));
wenzelm@5820
   264
wenzelm@5820
   265
wenzelm@5820
   266
(* goal *)
wenzelm@5820
   267
wenzelm@10553
   268
local
skalberg@15531
   269
  fun find i (state as State (Node {goal = SOME goal, ...}, _)) = (context_of state, (i, goal))
skalberg@15531
   270
    | find i (State (Node {goal = NONE, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
wenzelm@10553
   271
    | find _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@10553
   272
in val find_goal = find 0 end;
wenzelm@7176
   273
wenzelm@11079
   274
fun get_goal state =
wenzelm@11079
   275
  let val (ctxt, (_, ((_, x), _))) = find_goal state
wenzelm@11079
   276
  in (ctxt, x) end;
wenzelm@11079
   277
paulson@15452
   278
paulson@15452
   279
(*
paulson@15452
   280
(* Jia: added here: get all goals from the state 10/01/05 *)
wenzelm@16146
   281
fun find_all i (state as State (Node {goal = SOME goal, ...}, node::nodes)) =
paulson@15452
   282
    let val others = find_all (i+1) (State (node, nodes))
paulson@15452
   283
    in
wenzelm@16146
   284
        (context_of state, (i, goal)) :: others
paulson@15452
   285
    end
skalberg@15531
   286
  | find_all i (State (Node {goal = NONE, ...}, node :: nodes)) = find_all (i + 1) (State (node, nodes))
paulson@15452
   287
  | find_all _ (state as State (_, [])) = [];
paulson@15452
   288
paulson@15452
   289
val find_all_goals = find_all 0;
paulson@15452
   290
paulson@15452
   291
fun find_all_goals_ctxts state =
paulson@15452
   292
    let val all_goals = find_all_goals state
wenzelm@16146
   293
        fun find_ctxt_g [] = []
wenzelm@16146
   294
          | find_ctxt_g ((ctxt, (_, ((_, (_, thm)), _)))::others) = (ctxt,thm) :: (find_ctxt_g others)
paulson@15452
   295
    in
wenzelm@16146
   296
        find_ctxt_g all_goals
paulson@15452
   297
    end;
paulson@15452
   298
*)
paulson@15452
   299
wenzelm@5820
   300
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   301
skalberg@15531
   302
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
skalberg@15531
   303
      State (make_node (f context, facts, mode, SOME (g goal)), nodes)
wenzelm@8374
   304
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@8374
   305
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@8374
   306
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@8374
   307
  | map_goal _ _ state = state;
wenzelm@5820
   308
wenzelm@5820
   309
fun goal_facts get state =
wenzelm@5820
   310
  state
wenzelm@8374
   311
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   312
wenzelm@5820
   313
fun use_facts state =
wenzelm@5820
   314
  state
wenzelm@5820
   315
  |> goal_facts the_facts
wenzelm@5820
   316
  |> reset_facts;
wenzelm@5820
   317
wenzelm@5820
   318
wenzelm@5820
   319
(* mode *)
wenzelm@5820
   320
wenzelm@7176
   321
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   322
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   323
paulson@15194
   324
val enter_forward_aux = put_mode Forward;
wenzelm@5820
   325
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   326
val enter_backward = put_mode Backward;
wenzelm@5820
   327
wenzelm@5820
   328
fun assert_mode pred state =
wenzelm@5820
   329
  let val mode = get_mode state in
wenzelm@5820
   330
    if pred mode then state
wenzelm@12010
   331
    else raise STATE ("Illegal application of proof command in "
wenzelm@12010
   332
      ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   333
  end;
wenzelm@5820
   334
wenzelm@5820
   335
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   336
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   337
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   338
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   339
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   340
wenzelm@5820
   341
paulson@15194
   342
(*Jia Meng: a hook to be used for calling ATPs. *)
paulson@15452
   343
(* send the entire proof context *)
paulson@15452
   344
val atp_hook = ref (fn ctxt_state: ProofContext.context * Thm.thm => ());
paulson@15194
   345
paulson@15194
   346
(*Jia Meng: the modified version of enter_forward. *)
paulson@15194
   347
(*Jia Meng: atp: Proof.state -> unit *)
paulson@15194
   348
local
wenzelm@16146
   349
 fun atp state =
paulson@15452
   350
     let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
paulson@15452
   351
     in
wenzelm@16146
   352
         (!atp_hook) (ctxt,thm)
paulson@15452
   353
     end;
wenzelm@16146
   354
paulson@15194
   355
in
paulson@15194
   356
wenzelm@16146
   357
 fun enter_forward state =
wenzelm@16146
   358
   if (!call_atp) then
paulson@15194
   359
   ((atp state; enter_forward_aux state)
paulson@15194
   360
                           handle (STATE _) => enter_forward_aux state)
paulson@15194
   361
       else (enter_forward_aux state);
paulson@15194
   362
paulson@15194
   363
end;
paulson@15194
   364
paulson@15194
   365
wenzelm@5820
   366
(* blocks *)
wenzelm@5820
   367
wenzelm@6776
   368
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   369
wenzelm@5820
   370
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   371
wenzelm@5820
   372
fun new_block state =
wenzelm@5820
   373
  state
wenzelm@5820
   374
  |> open_block
skalberg@15531
   375
  |> put_goal NONE;
wenzelm@5820
   376
wenzelm@7487
   377
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   378
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   379
wenzelm@5820
   380
wenzelm@5820
   381
wenzelm@12423
   382
(** pretty_state **)
wenzelm@5820
   383
nipkow@13869
   384
val show_main_goal = ref false;
nipkow@13698
   385
wenzelm@6529
   386
val verbose = ProofContext.verbose;
wenzelm@6529
   387
wenzelm@14876
   388
val pretty_goals_local = Display.pretty_goals_aux o ProofContext.pp;
wenzelm@12085
   389
skalberg@15531
   390
fun pretty_facts _ _ NONE = []
skalberg@15531
   391
  | pretty_facts s ctxt (SOME ths) =
wenzelm@12055
   392
      [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   393
wenzelm@12423
   394
fun pretty_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   395
  let
wenzelm@11891
   396
    val ref (_, _, begin_goal) = Display.current_goals_markers;
wenzelm@5820
   397
wenzelm@5820
   398
    fun levels_up 0 = ""
wenzelm@7575
   399
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   400
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   401
wenzelm@11556
   402
    fun subgoals 0 = ""
wenzelm@11556
   403
      | subgoals 1 = ", 1 subgoal"
wenzelm@11556
   404
      | subgoals n = ", " ^ string_of_int n ^ " subgoals";
wenzelm@12146
   405
wenzelm@12146
   406
    fun prt_names names =
wenzelm@12242
   407
      (case filter_out (equal "") names of [] => ""
skalberg@15570
   408
      | nms => " " ^ enclose "(" ")" (space_implode " and " (Library.take (7, nms) @
wenzelm@12308
   409
          (if length nms > 7 then ["..."] else []))));
wenzelm@12146
   410
wenzelm@12146
   411
    fun prt_goal (_, (i, (((kind, names, _), (goal_facts, thm)), _))) =
wenzelm@12085
   412
      pretty_facts "using " ctxt
skalberg@15531
   413
        (if mode <> Backward orelse null goal_facts then NONE else SOME goal_facts) @
wenzelm@16450
   414
      [Pretty.str ("goal (" ^ kind_name (theory_of state) kind ^ prt_names names ^
wenzelm@12146
   415
          levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
wenzelm@16146
   416
      pretty_goals_local ctxt begin_goal (true, ! show_main_goal) (! Display.goals_limit) thm;
wenzelm@6848
   417
wenzelm@8462
   418
    val ctxt_prts =
wenzelm@12085
   419
      if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt
wenzelm@12085
   420
      else if mode = Backward then ProofContext.pretty_asms ctxt
wenzelm@7575
   421
      else [];
wenzelm@8462
   422
wenzelm@8462
   423
    val prts =
wenzelm@8582
   424
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@12010
   425
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
wenzelm@8561
   426
        else "")), Pretty.str ""] @
wenzelm@8462
   427
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
wenzelm@8462
   428
     (if ! verbose orelse mode = Forward then
wenzelm@12146
   429
       (pretty_facts "" ctxt facts @ prt_goal (find_goal state))
wenzelm@12085
   430
      else if mode = ForwardChain then pretty_facts "picking " ctxt facts
wenzelm@12146
   431
      else prt_goal (find_goal state))
wenzelm@12423
   432
  in prts end;
wenzelm@5820
   433
wenzelm@12085
   434
fun pretty_goals main state =
wenzelm@12085
   435
  let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
wenzelm@12085
   436
  in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end;
wenzelm@10320
   437
wenzelm@5820
   438
wenzelm@5820
   439
wenzelm@5820
   440
(** proof steps **)
wenzelm@5820
   441
wenzelm@5820
   442
(* datatype method *)
wenzelm@5820
   443
wenzelm@16450
   444
datatype method = Method of thm list -> RuleCases.tactic;
wenzelm@8374
   445
wenzelm@8374
   446
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
wenzelm@8374
   447
val method_cases = Method;
wenzelm@5820
   448
wenzelm@5820
   449
wenzelm@5820
   450
(* refine goal *)
wenzelm@5820
   451
wenzelm@8234
   452
local
wenzelm@8234
   453
wenzelm@16146
   454
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   455
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   456
wenzelm@16146
   457
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   458
wenzelm@16146
   459
fun goal_cases st =
wenzelm@16450
   460
  RuleCases.make true NONE (Thm.theory_of_thm st, Thm.prop_of st) (goals st);
wenzelm@16146
   461
wenzelm@16450
   462
fun check_theory thy state =
wenzelm@16450
   463
  if subthy (thy, theory_of state) then state
wenzelm@16450
   464
  else raise STATE ("Bad theory of method result: " ^ Context.str_of_thy thy, state);
wenzelm@5820
   465
wenzelm@8234
   466
fun gen_refine current_context meth_fun state =
wenzelm@6848
   467
  let
wenzelm@16146
   468
    val (goal_ctxt, (_, ((result, (facts, st)), x))) = find_goal state;
wenzelm@8234
   469
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@16146
   470
  in
wenzelm@16146
   471
    meth facts st |> Seq.map (fn (st', meth_cases) =>
wenzelm@6848
   472
      state
wenzelm@16450
   473
      |> check_theory (Thm.theory_of_thm st')
wenzelm@16146
   474
      |> map_goal
wenzelm@16146
   475
          (ProofContext.add_cases (no_goal_cases st @ goal_cases st' @ meth_cases))
wenzelm@16146
   476
          (K ((result, (facts, st')), x)))
wenzelm@16146
   477
  end;
wenzelm@5820
   478
wenzelm@8234
   479
in
wenzelm@8234
   480
wenzelm@8234
   481
val refine = gen_refine true;
wenzelm@8234
   482
val refine_end = gen_refine false;
wenzelm@8234
   483
wenzelm@8234
   484
end;
wenzelm@8234
   485
wenzelm@5820
   486
wenzelm@11917
   487
(* goal addressing *)
wenzelm@6932
   488
wenzelm@8152
   489
fun FINDGOAL tac st =
wenzelm@8374
   490
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
wenzelm@8374
   491
  in find (1, Thm.nprems_of st) st end;
wenzelm@8152
   492
wenzelm@8166
   493
fun HEADGOAL tac = tac 1;
wenzelm@8166
   494
wenzelm@9469
   495
wenzelm@9469
   496
(* export results *)
wenzelm@9469
   497
wenzelm@11816
   498
fun refine_tac rule =
wenzelm@12703
   499
  Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@11816
   500
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
wenzelm@11816
   501
      Tactic.etac Drule.triv_goal i
wenzelm@11816
   502
    else all_tac));
wenzelm@11816
   503
wenzelm@12146
   504
fun export_goal inner print_rule raw_rule state =
wenzelm@6932
   505
  let
wenzelm@12971
   506
    val (outer, (_, ((result, (facts, thm)), x))) = find_goal state;
wenzelm@12010
   507
    val exp_tac = ProofContext.export true inner outer;
wenzelm@9469
   508
    fun exp_rule rule =
wenzelm@9469
   509
      let
wenzelm@12055
   510
        val _ = print_rule outer rule;
wenzelm@11816
   511
        val thmq = FINDGOAL (refine_tac rule) thm;
wenzelm@12971
   512
      in Seq.map (fn th => map_goal I (K ((result, (facts, th)), x)) state) thmq end;
wenzelm@9469
   513
  in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end;
wenzelm@6932
   514
wenzelm@12146
   515
fun export_goals inner print_rule raw_rules =
wenzelm@12146
   516
  Seq.EVERY (map (export_goal inner print_rule) raw_rules);
wenzelm@12146
   517
wenzelm@6932
   518
fun transfer_facts inner_state state =
wenzelm@6932
   519
  (case get_facts inner_state of
skalberg@15531
   520
    NONE => Seq.single (reset_facts state)
skalberg@15531
   521
  | SOME thms =>
wenzelm@8617
   522
      let
wenzelm@12010
   523
        val exp_tac = ProofContext.export false (context_of inner_state) (context_of state);
wenzelm@11816
   524
        val thmqs = map exp_tac thms;
skalberg@15531
   525
      in Seq.map (fn ths => put_facts (SOME ths) state) (Seq.commute thmqs) end);
wenzelm@6932
   526
wenzelm@6932
   527
wenzelm@6932
   528
(* prepare result *)
wenzelm@6932
   529
wenzelm@12146
   530
fun prep_result state ts raw_th =
wenzelm@5820
   531
  let
wenzelm@5820
   532
    val ctxt = context_of state;
wenzelm@5820
   533
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   534
wenzelm@12146
   535
    val ngoals = Thm.nprems_of raw_th;
wenzelm@5820
   536
    val _ =
wenzelm@5820
   537
      if ngoals = 0 then ()
wenzelm@16146
   538
      else (err (Pretty.string_of (Pretty.chunks
wenzelm@16146
   539
        (pretty_goals_local ctxt "" (true, !show_main_goal) (! Display.goals_limit) raw_th @
wenzelm@12085
   540
          [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
wenzelm@5820
   541
wenzelm@16450
   542
    val {hyps, thy, ...} = Thm.rep_thm raw_th;
wenzelm@16450
   543
    val tsig = Sign.tsig_of thy;
skalberg@15570
   544
    val casms = List.concat (map #1 (ProofContext.assumptions_of (context_of state)));
wenzelm@11816
   545
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms);
wenzelm@12146
   546
wenzelm@12146
   547
    val th = raw_th RS Drule.rev_triv_goal;
wenzelm@12146
   548
    val ths = Drule.conj_elim_precise (length ts) th
wenzelm@12146
   549
      handle THM _ => err "Main goal structure corrupted";
wenzelm@5820
   550
  in
wenzelm@12146
   551
    conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^
wenzelm@12146
   552
        cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps)));
wenzelm@12808
   553
    conditional (exists (not o Pattern.matches tsig) (ts ~~ map Thm.prop_of ths)) (fn () =>
wenzelm@12146
   554
      err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th)));
wenzelm@12146
   555
    ths
wenzelm@5820
   556
  end;
wenzelm@5820
   557
wenzelm@5820
   558
wenzelm@5820
   559
wenzelm@5820
   560
(*** structured proof commands ***)
wenzelm@5820
   561
wenzelm@5820
   562
(** context **)
wenzelm@5820
   563
wenzelm@5820
   564
(* bind *)
wenzelm@5820
   565
wenzelm@5820
   566
fun gen_bind f x state =
wenzelm@5820
   567
  state
wenzelm@5820
   568
  |> assert_forward
wenzelm@5820
   569
  |> map_context (f x)
wenzelm@5820
   570
  |> reset_facts;
wenzelm@5820
   571
wenzelm@8617
   572
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   573
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   574
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   575
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   576
wenzelm@5820
   577
wenzelm@14564
   578
(* note_thmss *)
wenzelm@5820
   579
wenzelm@12714
   580
local
wenzelm@12714
   581
wenzelm@14564
   582
fun gen_note_thmss f args state =
wenzelm@5820
   583
  state
wenzelm@5820
   584
  |> assert_forward
wenzelm@12714
   585
  |> map_context_result (f args)
wenzelm@9196
   586
  |> these_factss;
wenzelm@5820
   587
wenzelm@12714
   588
in
wenzelm@12714
   589
wenzelm@14564
   590
val note_thmss = gen_note_thmss ProofContext.note_thmss;
wenzelm@14564
   591
val note_thmss_i = gen_note_thmss ProofContext.note_thmss_i;
wenzelm@12714
   592
wenzelm@14564
   593
fun simple_note_thms name thms = note_thmss_i [((name, []), [(thms, [])])];
wenzelm@12714
   594
wenzelm@12714
   595
end;
wenzelm@12714
   596
wenzelm@12714
   597
wenzelm@12714
   598
(* with_thmss *)
wenzelm@9196
   599
wenzelm@12714
   600
local
wenzelm@12714
   601
wenzelm@12714
   602
fun gen_with_thmss f args state =
wenzelm@12714
   603
  let val state' = state |> f args
wenzelm@14564
   604
  in state' |> simple_note_thms "" (the_facts state' @ the_facts state) end;
wenzelm@6876
   605
wenzelm@12714
   606
in
wenzelm@12714
   607
wenzelm@14564
   608
val with_thmss = gen_with_thmss note_thmss;
wenzelm@14564
   609
val with_thmss_i = gen_with_thmss note_thmss_i;
wenzelm@12714
   610
wenzelm@12714
   611
end;
wenzelm@12714
   612
wenzelm@5820
   613
wenzelm@12930
   614
(* using_thmss *)
wenzelm@12930
   615
wenzelm@12930
   616
local
wenzelm@12930
   617
wenzelm@12930
   618
fun gen_using_thmss f args state =
wenzelm@12930
   619
  state
wenzelm@12930
   620
  |> assert_backward
wenzelm@12930
   621
  |> map_context_result (f (map (pair ("", [])) args))
wenzelm@12930
   622
  |> (fn (st, named_facts) =>
wenzelm@12971
   623
    let val (_, (_, ((result, (facts, thm)), x))) = find_goal st;
skalberg@15570
   624
    in st |> map_goal I (K ((result, (facts @ List.concat (map snd named_facts), thm)), x)) end);
wenzelm@12930
   625
wenzelm@12930
   626
in
wenzelm@12930
   627
wenzelm@14564
   628
val using_thmss = gen_using_thmss ProofContext.note_thmss;
wenzelm@14564
   629
val using_thmss_i = gen_using_thmss ProofContext.note_thmss_i;
wenzelm@12930
   630
wenzelm@12930
   631
end;
wenzelm@12930
   632
wenzelm@12930
   633
wenzelm@5820
   634
(* fix *)
wenzelm@5820
   635
wenzelm@5820
   636
fun gen_fix f xs state =
wenzelm@5820
   637
  state
wenzelm@5820
   638
  |> assert_forward
wenzelm@5820
   639
  |> map_context (f xs)
wenzelm@5820
   640
  |> reset_facts;
wenzelm@5820
   641
wenzelm@5820
   642
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   643
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   644
wenzelm@5820
   645
wenzelm@11891
   646
(* assume and presume *)
wenzelm@5820
   647
wenzelm@9469
   648
fun gen_assume f exp args state =
wenzelm@5820
   649
  state
wenzelm@5820
   650
  |> assert_forward
wenzelm@9469
   651
  |> map_context_result (f exp args)
wenzelm@11924
   652
  |> these_factss;
wenzelm@6932
   653
wenzelm@7271
   654
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   655
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@11917
   656
val assume = assm ProofContext.export_assume;
wenzelm@11917
   657
val assume_i = assm_i ProofContext.export_assume;
wenzelm@11917
   658
val presume = assm ProofContext.export_presume;
wenzelm@11917
   659
val presume_i = assm_i ProofContext.export_presume;
wenzelm@5820
   660
wenzelm@7271
   661
wenzelm@5820
   662
wenzelm@11891
   663
(** local definitions **)
wenzelm@11891
   664
wenzelm@11891
   665
fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
wenzelm@11891
   666
  let
wenzelm@11891
   667
    val _ = assert_forward state;
wenzelm@11891
   668
    val ctxt = context_of state;
wenzelm@11891
   669
wenzelm@11891
   670
    (*rhs*)
wenzelm@11891
   671
    val name = if raw_name = "" then Thm.def_name x else raw_name;
wenzelm@11891
   672
    val rhs = prep_term ctxt raw_rhs;
wenzelm@11891
   673
    val T = Term.fastype_of rhs;
wenzelm@11891
   674
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
wenzelm@11891
   675
wenzelm@11891
   676
    (*lhs*)
wenzelm@11891
   677
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
wenzelm@11891
   678
  in
wenzelm@11891
   679
    state
skalberg@15531
   680
    |> fix [([x], NONE)]
wenzelm@11891
   681
    |> match_bind_i [(pats, rhs)]
wenzelm@11917
   682
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
wenzelm@11891
   683
  end;
wenzelm@11891
   684
wenzelm@11891
   685
val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
wenzelm@11891
   686
val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
wenzelm@11891
   687
wenzelm@11891
   688
wenzelm@8374
   689
(* invoke_case *)
wenzelm@8374
   690
wenzelm@11793
   691
fun invoke_case (name, xs, atts) state =
wenzelm@9292
   692
  let
wenzelm@10830
   693
    val (state', (lets, asms)) =
wenzelm@11793
   694
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
wenzelm@13425
   695
    val assumptions = asms |> map (fn (a, ts) =>
wenzelm@16146
   696
      ((NameSpace.qualified name a, atts), map (rpair ([], [])) ts));
wenzelm@9292
   697
  in
wenzelm@10830
   698
    state'
wenzelm@10809
   699
    |> add_binds_i lets
wenzelm@16146
   700
    |> map_context (ProofContext.no_base_names o ProofContext.qualified_names)
wenzelm@13425
   701
    |> assume_i assumptions
wenzelm@16146
   702
    |> map_context (ProofContext.restore_naming (context_of state))
wenzelm@14564
   703
    |> (fn st => simple_note_thms name (the_facts st) st)
wenzelm@8374
   704
  end;
wenzelm@8374
   705
wenzelm@8374
   706
wenzelm@5820
   707
wenzelm@5820
   708
(** goals **)
wenzelm@5820
   709
wenzelm@5820
   710
(* forward chaining *)
wenzelm@5820
   711
wenzelm@5820
   712
fun chain state =
wenzelm@5820
   713
  state
wenzelm@5820
   714
  |> assert_forward
wenzelm@6848
   715
  |> assert_facts
wenzelm@5820
   716
  |> enter_forward_chain;
wenzelm@5820
   717
wenzelm@5820
   718
fun from_facts facts state =
wenzelm@5820
   719
  state
skalberg@15531
   720
  |> put_facts (SOME facts)
wenzelm@5820
   721
  |> chain;
wenzelm@5820
   722
wenzelm@5820
   723
wenzelm@5820
   724
(* setup goals *)
wenzelm@5820
   725
wenzelm@11549
   726
val rule_contextN = "rule_context";
wenzelm@8991
   727
wenzelm@12971
   728
fun setup_goal opt_block prepp autofix kind after_qed pr names raw_propp state =
wenzelm@5820
   729
  let
wenzelm@12146
   730
    val (state', (propss, gen_binds)) =
wenzelm@5936
   731
      state
wenzelm@5936
   732
      |> assert_forward_or_chain
wenzelm@5936
   733
      |> enter_forward
wenzelm@8617
   734
      |> opt_block
wenzelm@12534
   735
      |> map_context_result (fn ctxt => prepp (ctxt, raw_propp));
wenzelm@16450
   736
    val thy' = theory_of state';
wenzelm@12146
   737
wenzelm@15703
   738
    val AfterQed (after_local, after_global) = after_qed;
wenzelm@15703
   739
    val after_qed' = AfterQed (after_local o map_context gen_binds, after_global);
wenzelm@15703
   740
skalberg@15570
   741
    val props = List.concat propss;
wenzelm@16450
   742
    val cprop = Thm.cterm_of thy' (Logic.mk_conjunction_list props);
wenzelm@7556
   743
    val goal = Drule.mk_triv_goal cprop;
wenzelm@10553
   744
skalberg@15574
   745
    val tvars = foldr Term.add_term_tvars [] props;
skalberg@15574
   746
    val vars = foldr Term.add_term_vars [] props;
wenzelm@5820
   747
  in
wenzelm@10553
   748
    if null tvars then ()
wenzelm@10553
   749
    else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
wenzelm@10553
   750
      commas (map (Syntax.string_of_vname o #1) tvars), state);
wenzelm@10553
   751
    if null vars then ()
wenzelm@10553
   752
    else warning ("Goal statement contains unbound schematic variable(s): " ^
wenzelm@12055
   753
      commas (map (ProofContext.string_of_term (context_of state')) vars));
wenzelm@5936
   754
    state'
wenzelm@12146
   755
    |> map_context (autofix props)
wenzelm@15703
   756
    |> put_goal (SOME (((kind, names, propss), ([], goal)), (after_qed', pr)))
wenzelm@12167
   757
    |> map_context (ProofContext.add_cases
wenzelm@12808
   758
     (if length props = 1 then
wenzelm@16450
   759
      RuleCases.make true NONE (Thm.theory_of_thm goal, Thm.prop_of goal) [rule_contextN]
wenzelm@16146
   760
      else [(rule_contextN, NONE)]))
wenzelm@12146
   761
    |> auto_bind_goal props
wenzelm@5820
   762
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   763
    |> new_block
wenzelm@5820
   764
    |> enter_backward
wenzelm@5820
   765
  end;
wenzelm@5820
   766
wenzelm@5820
   767
(*global goals*)
wenzelm@15703
   768
fun global_goal prep kind after_global export raw_locale a elems concl thy =
wenzelm@12503
   769
  let
wenzelm@12549
   770
    val init = init_state thy;
wenzelm@13399
   771
    val (opt_name, view, locale_ctxt, elems_ctxt, propp) =
skalberg@15570
   772
      prep (Option.map fst raw_locale) elems (map snd concl) (context_of init);
wenzelm@12503
   773
  in
wenzelm@12549
   774
    init
wenzelm@12549
   775
    |> open_block
wenzelm@12511
   776
    |> map_context (K locale_ctxt)
wenzelm@12065
   777
    |> open_block
wenzelm@12511
   778
    |> map_context (K elems_ctxt)
wenzelm@12959
   779
    |> setup_goal I ProofContext.bind_propp_schematic_i ProofContext.fix_frees
wenzelm@13415
   780
      (Theorem {kind = kind,
wenzelm@13415
   781
        theory_spec = (a, map (snd o fst) concl),
wenzelm@15973
   782
        locale_spec = (case raw_locale of NONE => NONE | SOME (_, x) => SOME (the opt_name, x)),
ballarin@15596
   783
        view = view,
ballarin@15696
   784
        export = export})
wenzelm@15703
   785
      (AfterQed (Seq.single, after_global))
wenzelm@15703
   786
      true (map (fst o fst) concl) propp
wenzelm@12065
   787
  end;
wenzelm@5820
   788
wenzelm@12534
   789
val multi_theorem = global_goal Locale.read_context_statement;
wenzelm@12534
   790
val multi_theorem_i = global_goal Locale.cert_context_statement;
wenzelm@12146
   791
wenzelm@5820
   792
wenzelm@5820
   793
(*local goals*)
wenzelm@15703
   794
fun local_goal' prepp kind (check: bool -> state -> state) after_local pr args int state =
wenzelm@7928
   795
  state
wenzelm@12146
   796
  |> setup_goal open_block prepp (K I) (kind (map (snd o fst) args))
wenzelm@15703
   797
    (AfterQed (after_local, I)) pr (map (fst o fst) args) (map snd args)
wenzelm@10936
   798
  |> warn_extra_tfrees state
wenzelm@10936
   799
  |> check int;
wenzelm@5820
   800
wenzelm@12971
   801
fun local_goal prepp kind f pr args = local_goal' prepp kind (K I) f pr args false;
wenzelm@10936
   802
wenzelm@10936
   803
val show = local_goal' ProofContext.bind_propp Show;
wenzelm@10936
   804
val show_i = local_goal' ProofContext.bind_propp_i Show;
wenzelm@10936
   805
val have = local_goal ProofContext.bind_propp Have;
wenzelm@10380
   806
val have_i = local_goal ProofContext.bind_propp_i Have;
wenzelm@15703
   807
wenzelm@5820
   808
wenzelm@5820
   809
wenzelm@5820
   810
(** conclusions **)
wenzelm@5820
   811
wenzelm@5820
   812
(* current goal *)
wenzelm@5820
   813
skalberg@15531
   814
fun current_goal (State (Node {context, goal = SOME goal, ...}, _)) = (context, goal)
wenzelm@5820
   815
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   816
skalberg@15531
   817
fun assert_current_goal true (state as State (Node {goal = NONE, ...}, _)) =
wenzelm@6404
   818
      raise STATE ("No goal in this block!", state)
skalberg@15531
   819
  | assert_current_goal false (state as State (Node {goal = SOME _, ...}, _)) =
wenzelm@6404
   820
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   821
  | assert_current_goal _ state = state;
wenzelm@5820
   822
wenzelm@12010
   823
fun assert_bottom b (state as State (_, nodes)) =
wenzelm@12010
   824
  let val bot = (length nodes <= 2) in
wenzelm@12010
   825
    if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
wenzelm@12010
   826
    else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
wenzelm@12010
   827
    else state
wenzelm@12010
   828
  end;
wenzelm@5820
   829
wenzelm@6404
   830
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   831
wenzelm@6932
   832
fun end_proof bot state =
wenzelm@5820
   833
  state
wenzelm@5820
   834
  |> assert_forward
wenzelm@5820
   835
  |> close_block
wenzelm@5820
   836
  |> assert_bottom bot
wenzelm@7011
   837
  |> assert_current_goal true
wenzelm@7011
   838
  |> goal_facts (K []);
wenzelm@5820
   839
wenzelm@5820
   840
wenzelm@6404
   841
(* local_qed *)
wenzelm@5820
   842
wenzelm@6982
   843
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   844
  let
wenzelm@15703
   845
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (after_local, _), pr))) =
wenzelm@15703
   846
      current_goal state;
wenzelm@8617
   847
    val outer_state = state |> close_block;
wenzelm@8617
   848
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   849
skalberg@15570
   850
    val ts = List.concat tss;
wenzelm@12146
   851
    val results = prep_result state ts raw_thm;
wenzelm@12146
   852
    val resultq =
wenzelm@12146
   853
      results |> map (ProofContext.export false goal_ctxt outer_ctxt)
wenzelm@12146
   854
      |> Seq.commute |> Seq.map (Library.unflat tss);
wenzelm@9469
   855
wenzelm@15703
   856
    val (attss, opt_solve) =
wenzelm@5820
   857
      (case kind of
wenzelm@12971
   858
        Show attss => (attss,
wenzelm@15703
   859
          export_goals goal_ctxt (if pr then print_rule else (K (K ()))) results)
wenzelm@15703
   860
      | Have attss => (attss, Seq.single)
wenzelm@6932
   861
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   862
  in
wenzelm@12971
   863
    conditional pr (fn () => print_result goal_ctxt
wenzelm@16450
   864
      (kind_name (theory_of state) kind, names ~~ Library.unflat tss results));
wenzelm@8617
   865
    outer_state
wenzelm@12549
   866
    |> auto_bind_facts (ProofContext.generalize goal_ctxt outer_ctxt ts)
wenzelm@9469
   867
    |> (fn st => Seq.map (fn res =>
wenzelm@15703
   868
      note_thmss_i ((names ~~ attss) ~~ map (single o Thm.no_attributes) res) st) resultq)
wenzelm@9469
   869
    |> (Seq.flat o Seq.map opt_solve)
wenzelm@15703
   870
    |> (Seq.flat o Seq.map after_local)
wenzelm@5820
   871
  end;
wenzelm@5820
   872
wenzelm@6982
   873
fun local_qed finalize print state =
wenzelm@6404
   874
  state
wenzelm@6932
   875
  |> end_proof false
wenzelm@6871
   876
  |> finalize
wenzelm@6982
   877
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   878
wenzelm@5820
   879
wenzelm@12703
   880
(* global_qed *)
wenzelm@12065
   881
wenzelm@6950
   882
fun finish_global state =
wenzelm@5820
   883
  let
wenzelm@15703
   884
    val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (_, after_global), _))) =
wenzelm@15703
   885
      current_goal state;
wenzelm@12065
   886
    val locale_ctxt = context_of (state |> close_block);
wenzelm@12065
   887
    val theory_ctxt = context_of (state |> close_block |> close_block);
wenzelm@15703
   888
    val {kind = k, theory_spec = ((name, atts), attss),
wenzelm@15703
   889
        locale_spec, view = (target_view, elems_view), export} =
wenzelm@13399
   890
      (case kind of Theorem x => x | _ => err_malformed "finish_global" state);
wenzelm@15703
   891
    val locale_name = Option.map fst locale_spec;
wenzelm@5820
   892
skalberg@15570
   893
    val ts = List.concat tss;
ballarin@15696
   894
    val locale_results = map (export elems_view goal_ctxt locale_ctxt)
wenzelm@13377
   895
      (prep_result state ts raw_thm);
ballarin@14215
   896
wenzelm@13377
   897
    val results = map (Drule.strip_shyps_warning o
ballarin@15696
   898
      export target_view locale_ctxt theory_ctxt) locale_results;
wenzelm@12703
   899
wenzelm@12959
   900
    val (theory', results') =
wenzelm@12959
   901
      theory_of state
ballarin@15696
   902
      |> (case locale_spec of NONE => I
ballarin@15696
   903
                            | SOME (loc, (loc_atts, loc_attss)) => fn thy =>
wenzelm@12959
   904
        if length names <> length loc_attss then
wenzelm@12959
   905
          raise THEORY ("Bad number of locale attributes", [thy])
wenzelm@13335
   906
        else (thy, locale_ctxt)
ballarin@15696
   907
          |> Locale.add_thmss k loc ((names ~~ Library.unflat tss locale_results) ~~ loc_attss)
wenzelm@13335
   908
          |> (fn ((thy', ctxt'), res) =>
wenzelm@12959
   909
            if name = "" andalso null loc_atts then thy'
wenzelm@13335
   910
            else (thy', ctxt')
ballarin@15696
   911
              |> (#1 o #1 o Locale.add_thmss k loc [((name, List.concat (map #2 res)), loc_atts)])))
wenzelm@15703
   912
      |> Locale.smart_note_thmss k locale_name
wenzelm@12703
   913
        ((names ~~ attss) ~~ map (single o Thm.no_attributes) (Library.unflat tss results))
wenzelm@12959
   914
      |> (fn (thy, res) => (thy, res)
wenzelm@15703
   915
          |>> (#1 o Locale.smart_note_thmss k locale_name
skalberg@15570
   916
            [((name, atts), [(List.concat (map #2 res), [])])]));
wenzelm@15703
   917
  in (after_global theory', ((k, name), results')) end;
wenzelm@12959
   918
wenzelm@5820
   919
wenzelm@10553
   920
(*note: clients should inspect first result only, as backtracking may destroy theory*)
wenzelm@6950
   921
fun global_qed finalize state =
wenzelm@5820
   922
  state
wenzelm@6932
   923
  |> end_proof true
wenzelm@6871
   924
  |> finalize
wenzelm@12065
   925
  |> Seq.map finish_global;
wenzelm@5820
   926
wenzelm@5820
   927
wenzelm@6896
   928
wenzelm@6896
   929
(** blocks **)
wenzelm@6896
   930
wenzelm@6896
   931
(* begin_block *)
wenzelm@6896
   932
wenzelm@6896
   933
fun begin_block state =
wenzelm@6896
   934
  state
wenzelm@6896
   935
  |> assert_forward
wenzelm@6896
   936
  |> new_block
wenzelm@6896
   937
  |> open_block;
wenzelm@6896
   938
wenzelm@6896
   939
wenzelm@6896
   940
(* end_block *)
wenzelm@6896
   941
wenzelm@6896
   942
fun end_block state =
wenzelm@6896
   943
  state
wenzelm@6896
   944
  |> assert_forward
wenzelm@6896
   945
  |> close_block
wenzelm@6896
   946
  |> assert_current_goal false
wenzelm@6896
   947
  |> close_block
wenzelm@6932
   948
  |> transfer_facts state;
wenzelm@6896
   949
wenzelm@6896
   950
wenzelm@6896
   951
(* next_block *)
wenzelm@6896
   952
wenzelm@6896
   953
fun next_block state =
wenzelm@6896
   954
  state
wenzelm@6896
   955
  |> assert_forward
wenzelm@6896
   956
  |> close_block
wenzelm@8718
   957
  |> new_block
wenzelm@8718
   958
  |> reset_facts;
wenzelm@6896
   959
wenzelm@6896
   960
wenzelm@5820
   961
end;
wenzelm@8152
   962
wenzelm@8152
   963
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   964
open BasicProof;