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