src/Pure/Isar/proof.ML
author wenzelm
Wed Oct 31 22:04:29 2001 +0100 (2001-10-31)
changeset 12010 e1d4df962ac9
parent 12000 715fe3909682
child 12015 68b2a53161e6
permissions -rw-r--r--
theorem(_i): locale argument;
contexts 0 and 1 in state now refer to theory and locale, respectively;
unified export (no longer uses global "standard");
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5820
     5
wenzelm@5820
     6
Proof states and methods.
wenzelm@5820
     7
*)
wenzelm@5820
     8
wenzelm@8152
     9
signature BASIC_PROOF =
wenzelm@8152
    10
sig
wenzelm@8374
    11
  val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8374
    12
  val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
wenzelm@8152
    13
end;
wenzelm@8152
    14
wenzelm@5820
    15
signature PROOF =
wenzelm@5820
    16
sig
wenzelm@8152
    17
  include BASIC_PROOF
wenzelm@5820
    18
  type context
wenzelm@5820
    19
  type state
wenzelm@5820
    20
  exception STATE of string * state
wenzelm@6871
    21
  val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
wenzelm@12010
    22
  val init_state: theory -> string option -> state
wenzelm@5820
    23
  val context_of: state -> context
wenzelm@5820
    24
  val theory_of: state -> theory
wenzelm@5820
    25
  val sign_of: state -> Sign.sg
wenzelm@7924
    26
  val warn_extra_tfrees: state -> state -> state
wenzelm@7605
    27
  val reset_thms: string -> state -> state
wenzelm@6091
    28
  val the_facts: state -> thm list
wenzelm@9469
    29
  val the_fact: state -> thm
wenzelm@11079
    30
  val get_goal: state -> context * (thm list * thm)
wenzelm@6091
    31
  val goal_facts: (state -> thm list) -> state -> state
wenzelm@5820
    32
  val use_facts: state -> state
wenzelm@5820
    33
  val reset_facts: state -> state
wenzelm@9469
    34
  val is_chain: state -> bool
wenzelm@6891
    35
  val assert_forward: state -> state
wenzelm@9469
    36
  val assert_forward_or_chain: state -> state
wenzelm@5820
    37
  val assert_backward: state -> state
wenzelm@8206
    38
  val assert_no_chain: state -> state
wenzelm@5820
    39
  val enter_forward: state -> state
wenzelm@6982
    40
  val show_hyps: bool ref
wenzelm@6982
    41
  val pretty_thm: thm -> Pretty.T
wenzelm@7412
    42
  val pretty_thms: thm list -> Pretty.T
wenzelm@6529
    43
  val verbose: bool ref
wenzelm@7201
    44
  val print_state: int -> state -> unit
wenzelm@10360
    45
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@6776
    46
  val level: state -> int
wenzelm@5820
    47
  type method
wenzelm@6848
    48
  val method: (thm list -> tactic) -> method
wenzelm@8374
    49
  val method_cases: (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> method
wenzelm@5820
    50
  val refine: (context -> method) -> state -> state Seq.seq
wenzelm@8234
    51
  val refine_end: (context -> method) -> state -> state Seq.seq
wenzelm@5936
    52
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    53
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@8617
    54
  val let_bind: (string list * string) list -> state -> state
wenzelm@8617
    55
  val let_bind_i: (term list * term) list -> state -> state
wenzelm@6876
    56
  val simple_have_thms: string -> thm list -> state -> state
wenzelm@9196
    57
  val have_thmss: ((string * context attribute list) *
wenzelm@9196
    58
    (thm list * context attribute list) list) list -> state -> state
wenzelm@9196
    59
  val with_thmss: ((string * context attribute list) *
wenzelm@9196
    60
    (thm list * context attribute list) list) list -> state -> state
wenzelm@7412
    61
  val fix: (string list * string option) list -> state -> state
wenzelm@7665
    62
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@9469
    63
  val assm: ProofContext.exporter
wenzelm@10464
    64
    -> ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    65
    -> state -> state
wenzelm@9469
    66
  val assm_i: ProofContext.exporter
wenzelm@10464
    67
    -> ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    68
    -> state -> state
wenzelm@10464
    69
  val assume:
wenzelm@10464
    70
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@7271
    71
    -> state -> state
wenzelm@10464
    72
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    73
    -> state -> state
wenzelm@10464
    74
  val presume:
wenzelm@10464
    75
    ((string * context attribute list) * (string * (string list * string list)) list) list
wenzelm@6932
    76
    -> state -> state
wenzelm@10464
    77
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@6932
    78
    -> state -> state
wenzelm@11891
    79
  val def: string -> context attribute list -> string *  (string * string list) -> state -> state
wenzelm@11891
    80
  val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
wenzelm@11793
    81
  val invoke_case: string * string option list * context attribute list -> state -> state
wenzelm@12010
    82
  val theorem: string -> string option -> bstring -> theory attribute list
wenzelm@12010
    83
    -> string * (string list * string list) -> theory -> state
wenzelm@12010
    84
  val theorem_i: string -> string option -> bstring -> theory attribute list
wenzelm@12010
    85
    -> term * (term list * term list) -> theory -> state
wenzelm@5820
    86
  val chain: state -> state
wenzelm@6091
    87
  val from_facts: thm list -> state -> state
wenzelm@10936
    88
  val show: (bool -> state -> state) -> (state -> state Seq.seq) -> string
wenzelm@10936
    89
    -> context attribute list -> string * (string list * string list) -> bool -> state -> state
wenzelm@10936
    90
  val show_i: (bool -> state -> state) -> (state -> state Seq.seq) -> string
wenzelm@10936
    91
    -> context attribute list -> term * (term list * term list) -> bool -> state -> state
wenzelm@7176
    92
  val have: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    93
    -> string * (string list * string list) -> state -> state
wenzelm@7176
    94
  val have_i: (state -> state Seq.seq) -> string -> context attribute list
wenzelm@7176
    95
    -> term * (term list * term list) -> state -> state
wenzelm@6404
    96
  val at_bottom: state -> bool
wenzelm@6982
    97
  val local_qed: (state -> state Seq.seq)
wenzelm@6982
    98
    -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit) -> state -> state Seq.seq
wenzelm@6950
    99
  val global_qed: (state -> state Seq.seq) -> state
wenzelm@6950
   100
    -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
wenzelm@6896
   101
  val begin_block: state -> state
wenzelm@6932
   102
  val end_block: state -> state Seq.seq
wenzelm@6896
   103
  val next_block: state -> state
wenzelm@5820
   104
end;
wenzelm@5820
   105
wenzelm@8152
   106
signature PRIVATE_PROOF =
wenzelm@5820
   107
sig
wenzelm@5820
   108
  include PROOF
wenzelm@5820
   109
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
wenzelm@5820
   110
end;
wenzelm@5820
   111
wenzelm@8152
   112
structure Proof: PRIVATE_PROOF =
wenzelm@5820
   113
struct
wenzelm@5820
   114
wenzelm@5820
   115
wenzelm@5820
   116
(** proof state **)
wenzelm@5820
   117
wenzelm@5820
   118
type context = ProofContext.context;
wenzelm@5820
   119
wenzelm@5820
   120
wenzelm@5820
   121
(* type goal *)
wenzelm@5820
   122
wenzelm@5820
   123
datatype kind =
wenzelm@12010
   124
  Theorem of string * string option * theory attribute list |    (*theorem with kind and locale*)
wenzelm@11742
   125
  Show of context attribute list |               (*intermediate result, solving subgoal*)
wenzelm@11985
   126
  Have of context attribute list;                (*intermediate result*)
wenzelm@5820
   127
wenzelm@12010
   128
val kind_name =
wenzelm@12010
   129
  fn Theorem (s, None, _) => s
wenzelm@12010
   130
   | Theorem (s, Some loc, _) => s ^ " (in " ^ loc ^ ")"    (* FIXME cond_extern *)
wenzelm@12010
   131
   | Show _ => "show"
wenzelm@12010
   132
   | Have _ => "have";
wenzelm@5820
   133
wenzelm@5820
   134
type goal =
wenzelm@6932
   135
 (kind *        (*result kind*)
wenzelm@6932
   136
  string *      (*result name*)
wenzelm@6932
   137
  term) *       (*result statement*)
wenzelm@6932
   138
 (thm list *    (*use facts*)
wenzelm@6932
   139
  thm);         (*goal: subgoals ==> statement*)
wenzelm@5820
   140
wenzelm@5820
   141
wenzelm@5820
   142
(* type mode *)
wenzelm@5820
   143
wenzelm@5820
   144
datatype mode = Forward | ForwardChain | Backward;
wenzelm@7201
   145
val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
wenzelm@5820
   146
wenzelm@5820
   147
wenzelm@5820
   148
(* datatype state *)
wenzelm@5820
   149
wenzelm@7176
   150
datatype node =
wenzelm@7176
   151
  Node of
wenzelm@7176
   152
   {context: context,
wenzelm@7176
   153
    facts: thm list option,
wenzelm@7176
   154
    mode: mode,
wenzelm@7176
   155
    goal: (goal * (state -> state Seq.seq)) option}
wenzelm@7176
   156
and state =
wenzelm@5820
   157
  State of
wenzelm@5820
   158
    node *              (*current*)
wenzelm@5820
   159
    node list;          (*parents wrt. block structure*)
wenzelm@5820
   160
wenzelm@7176
   161
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   162
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   163
wenzelm@7176
   164
wenzelm@5820
   165
exception STATE of string * state;
wenzelm@5820
   166
wenzelm@5820
   167
fun err_malformed name state =
wenzelm@5820
   168
  raise STATE (name ^ ": internal error -- malformed proof state", state);
wenzelm@5820
   169
wenzelm@6871
   170
fun check_result msg state sq =
wenzelm@6871
   171
  (case Seq.pull sq of
wenzelm@6871
   172
    None => raise STATE (msg, state)
wenzelm@6871
   173
  | Some s_sq => Seq.cons s_sq);
wenzelm@6871
   174
wenzelm@5820
   175
wenzelm@7176
   176
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   177
  State (make_node (f (context, facts, mode, goal)), nodes);
wenzelm@5820
   178
wenzelm@5820
   179
wenzelm@5820
   180
wenzelm@5820
   181
(** basic proof state operations **)
wenzelm@5820
   182
wenzelm@5820
   183
(* context *)
wenzelm@5820
   184
wenzelm@7176
   185
fun context_of (State (Node {context, ...}, _)) = context;
wenzelm@5820
   186
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   187
val sign_of = ProofContext.sign_of o context_of;
wenzelm@5820
   188
wenzelm@5820
   189
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   190
wenzelm@7176
   191
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
wenzelm@5820
   192
  let val (context', result) = f context
wenzelm@5820
   193
  in (State (make_node (context', facts, mode, goal), nodes), result) end;
wenzelm@5820
   194
wenzelm@5820
   195
wenzelm@5820
   196
fun put_data kind f = map_context o ProofContext.put_data kind f;
wenzelm@7924
   197
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@10809
   198
val add_binds_i = map_context o ProofContext.add_binds_i;
wenzelm@6790
   199
val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
wenzelm@6798
   200
val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
wenzelm@6091
   201
val put_thms = map_context o ProofContext.put_thms;
wenzelm@6091
   202
val put_thmss = map_context o ProofContext.put_thmss;
wenzelm@7605
   203
val reset_thms = map_context o ProofContext.reset_thms;
wenzelm@6932
   204
val assumptions = ProofContext.assumptions o context_of;
wenzelm@8374
   205
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   206
wenzelm@5820
   207
wenzelm@5820
   208
(* facts *)
wenzelm@5820
   209
wenzelm@7176
   210
fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
wenzelm@5820
   211
  | the_facts state = raise STATE ("No current facts available", state);
wenzelm@5820
   212
wenzelm@9469
   213
fun the_fact state =
wenzelm@9469
   214
  (case the_facts state of
wenzelm@9469
   215
    [thm] => thm
wenzelm@9469
   216
  | _ => raise STATE ("Single fact expected", state));
wenzelm@9469
   217
wenzelm@6848
   218
fun assert_facts state = (the_facts state; state);
wenzelm@7176
   219
fun get_facts (State (Node {facts, ...}, _)) = facts;
wenzelm@6848
   220
wenzelm@7605
   221
wenzelm@7605
   222
val thisN = "this";
wenzelm@7605
   223
wenzelm@5820
   224
fun put_facts facts state =
wenzelm@5820
   225
  state
wenzelm@5820
   226
  |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@7605
   227
  |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
wenzelm@5820
   228
wenzelm@5820
   229
val reset_facts = put_facts None;
wenzelm@5820
   230
wenzelm@9196
   231
fun these_factss (state, named_factss) =
wenzelm@5820
   232
  state
wenzelm@9196
   233
  |> put_thmss named_factss
wenzelm@9196
   234
  |> put_facts (Some (flat (map #2 named_factss)));
wenzelm@5820
   235
wenzelm@5820
   236
wenzelm@5820
   237
(* goal *)
wenzelm@5820
   238
wenzelm@10553
   239
local
wenzelm@10553
   240
  fun find i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
wenzelm@10553
   241
    | find i (State (Node {goal = None, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
wenzelm@10553
   242
    | find _ (state as State (_, [])) = err_malformed "find_goal" state;
wenzelm@10553
   243
in val find_goal = find 0 end;
wenzelm@7176
   244
wenzelm@11079
   245
fun get_goal state =
wenzelm@11079
   246
  let val (ctxt, (_, ((_, x), _))) = find_goal state
wenzelm@11079
   247
  in (ctxt, x) end;
wenzelm@11079
   248
wenzelm@5820
   249
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
wenzelm@5820
   250
wenzelm@8374
   251
fun map_goal f g (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
wenzelm@8374
   252
      State (make_node (f context, facts, mode, Some (g goal)), nodes)
wenzelm@8374
   253
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@8374
   254
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@8374
   255
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@8374
   256
  | map_goal _ _ state = state;
wenzelm@5820
   257
wenzelm@5820
   258
fun goal_facts get state =
wenzelm@5820
   259
  state
wenzelm@8374
   260
  |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
wenzelm@5820
   261
wenzelm@5820
   262
fun use_facts state =
wenzelm@5820
   263
  state
wenzelm@5820
   264
  |> goal_facts the_facts
wenzelm@5820
   265
  |> reset_facts;
wenzelm@5820
   266
wenzelm@5820
   267
wenzelm@5820
   268
(* mode *)
wenzelm@5820
   269
wenzelm@7176
   270
fun get_mode (State (Node {mode, ...}, _)) = mode;
wenzelm@5820
   271
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   272
wenzelm@5820
   273
val enter_forward = put_mode Forward;
wenzelm@5820
   274
val enter_forward_chain = put_mode ForwardChain;
wenzelm@5820
   275
val enter_backward = put_mode Backward;
wenzelm@5820
   276
wenzelm@5820
   277
fun assert_mode pred state =
wenzelm@5820
   278
  let val mode = get_mode state in
wenzelm@5820
   279
    if pred mode then state
wenzelm@12010
   280
    else raise STATE ("Illegal application of proof command in "
wenzelm@12010
   281
      ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   282
  end;
wenzelm@5820
   283
wenzelm@5820
   284
fun is_chain state = get_mode state = ForwardChain;
wenzelm@5820
   285
val assert_forward = assert_mode (equal Forward);
wenzelm@5820
   286
val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
wenzelm@5820
   287
val assert_backward = assert_mode (equal Backward);
wenzelm@8206
   288
val assert_no_chain = assert_mode (not_equal ForwardChain);
wenzelm@5820
   289
wenzelm@5820
   290
wenzelm@5820
   291
(* blocks *)
wenzelm@5820
   292
wenzelm@6776
   293
fun level (State (_, nodes)) = length nodes;
wenzelm@6776
   294
wenzelm@5820
   295
fun open_block (State (node, nodes)) = State (node, node :: nodes);
wenzelm@5820
   296
wenzelm@5820
   297
fun new_block state =
wenzelm@5820
   298
  state
wenzelm@5820
   299
  |> open_block
wenzelm@5820
   300
  |> put_goal None;
wenzelm@5820
   301
wenzelm@7487
   302
fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
wenzelm@5820
   303
  | close_block state = raise STATE ("Unbalanced block parentheses", state);
wenzelm@5820
   304
wenzelm@5820
   305
wenzelm@12010
   306
(* init_state *)
wenzelm@12010
   307
wenzelm@12010
   308
fun init_state thy locale =  (* FIXME locale unused *)
wenzelm@12010
   309
  State (make_node (ProofContext.init thy, None, Forward, None), [])
wenzelm@12010
   310
  |> open_block |> open_block;
wenzelm@12010
   311
wenzelm@12010
   312
wenzelm@5820
   313
wenzelm@5820
   314
(** print_state **)
wenzelm@5820
   315
wenzelm@6982
   316
val show_hyps = ProofContext.show_hyps;
wenzelm@6982
   317
val pretty_thm = ProofContext.pretty_thm;
wenzelm@6982
   318
wenzelm@7412
   319
fun pretty_thms [th] = pretty_thm th
wenzelm@7580
   320
  | pretty_thms ths = Pretty.blk (0, Pretty.fbreaks (map pretty_thm ths));
wenzelm@7412
   321
wenzelm@7412
   322
wenzelm@6529
   323
val verbose = ProofContext.verbose;
wenzelm@6529
   324
wenzelm@8462
   325
fun pretty_facts _ None = []
wenzelm@8462
   326
  | pretty_facts s (Some ths) =
wenzelm@8462
   327
      [Pretty.big_list (s ^ "this:") (map pretty_thm ths), Pretty.str ""];
wenzelm@6756
   328
wenzelm@7201
   329
fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
wenzelm@5820
   330
  let
wenzelm@11891
   331
    val ref (_, _, begin_goal) = Display.current_goals_markers;
wenzelm@5820
   332
wenzelm@5820
   333
    fun levels_up 0 = ""
wenzelm@7575
   334
      | levels_up 1 = ", 1 level up"
wenzelm@7575
   335
      | levels_up i = ", " ^ string_of_int i ^ " levels up";
wenzelm@5820
   336
wenzelm@11556
   337
    fun subgoals 0 = ""
wenzelm@11556
   338
      | subgoals 1 = ", 1 subgoal"
wenzelm@11556
   339
      | subgoals n = ", " ^ string_of_int n ^ " subgoals";
wenzelm@11556
   340
      
wenzelm@8462
   341
    fun pretty_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
wenzelm@8462
   342
      pretty_facts "using "
wenzelm@8462
   343
        (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
wenzelm@11556
   344
      [Pretty.str ("goal (" ^ kind_name kind ^
wenzelm@11556
   345
          (if name = "" then "" else " " ^ name) ^
wenzelm@11556
   346
            levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
wenzelm@11891
   347
      Display.pretty_goals_marker begin_goal (! goals_limit) thm;
wenzelm@6848
   348
wenzelm@8462
   349
    val ctxt_prts =
wenzelm@8462
   350
      if ! verbose orelse mode = Forward then ProofContext.pretty_context context
wenzelm@8462
   351
      else if mode = Backward then ProofContext.pretty_prems context
wenzelm@7575
   352
      else [];
wenzelm@8462
   353
wenzelm@8462
   354
    val prts =
wenzelm@8582
   355
     [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@12010
   356
        (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
wenzelm@8561
   357
        else "")), Pretty.str ""] @
wenzelm@8462
   358
     (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
wenzelm@8462
   359
     (if ! verbose orelse mode = Forward then
wenzelm@10553
   360
       (pretty_facts "" facts @ pretty_goal (find_goal state))
wenzelm@8462
   361
      else if mode = ForwardChain then pretty_facts "picking " facts
wenzelm@10553
   362
      else pretty_goal (find_goal state))
wenzelm@8462
   363
  in Pretty.writeln (Pretty.chunks prts) end;
wenzelm@5820
   364
wenzelm@10360
   365
fun pretty_goals main_goal state =
wenzelm@10553
   366
  let val (_, (_, ((_, (_, thm)), _))) = find_goal state
wenzelm@11891
   367
  in Display.pretty_sub_goals main_goal (! goals_limit) thm end;
wenzelm@10320
   368
wenzelm@5820
   369
wenzelm@5820
   370
wenzelm@5820
   371
(** proof steps **)
wenzelm@5820
   372
wenzelm@5820
   373
(* datatype method *)
wenzelm@5820
   374
wenzelm@8374
   375
datatype method =
wenzelm@8374
   376
  Method of thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq;
wenzelm@8374
   377
wenzelm@8374
   378
fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st));
wenzelm@8374
   379
val method_cases = Method;
wenzelm@5820
   380
wenzelm@5820
   381
wenzelm@5820
   382
(* refine goal *)
wenzelm@5820
   383
wenzelm@8234
   384
local
wenzelm@8234
   385
wenzelm@5820
   386
fun check_sign sg state =
wenzelm@5820
   387
  if Sign.subsig (sg, sign_of state) then state
wenzelm@5820
   388
  else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
wenzelm@5820
   389
wenzelm@8234
   390
fun gen_refine current_context meth_fun state =
wenzelm@6848
   391
  let
wenzelm@10553
   392
    val (goal_ctxt, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@8234
   393
    val Method meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@5820
   394
wenzelm@8374
   395
    fun refn (thm', cases) =
wenzelm@6848
   396
      state
wenzelm@6848
   397
      |> check_sign (Thm.sign_of_thm thm')
wenzelm@8374
   398
      |> map_goal (ProofContext.add_cases cases) (K ((result, (facts, thm')), f));
wenzelm@6848
   399
  in Seq.map refn (meth facts thm) end;
wenzelm@5820
   400
wenzelm@8234
   401
in
wenzelm@8234
   402
wenzelm@8234
   403
val refine = gen_refine true;
wenzelm@8234
   404
val refine_end = gen_refine false;
wenzelm@8234
   405
wenzelm@8234
   406
end;
wenzelm@8234
   407
wenzelm@5820
   408
wenzelm@11917
   409
(* goal addressing *)
wenzelm@6932
   410
wenzelm@8152
   411
fun FINDGOAL tac st =
wenzelm@8374
   412
  let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n))
wenzelm@8374
   413
  in find (1, Thm.nprems_of st) st end;
wenzelm@8152
   414
wenzelm@8166
   415
fun HEADGOAL tac = tac 1;
wenzelm@8166
   416
wenzelm@9469
   417
wenzelm@9469
   418
(* export results *)
wenzelm@9469
   419
wenzelm@11816
   420
fun refine_tac rule =
wenzelm@11816
   421
  Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@11816
   422
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
wenzelm@11816
   423
      Tactic.etac Drule.triv_goal i
wenzelm@11816
   424
    else all_tac));
wenzelm@11816
   425
wenzelm@6982
   426
fun export_goal print_rule raw_rule inner state =
wenzelm@6932
   427
  let
wenzelm@10553
   428
    val (outer, (_, ((result, (facts, thm)), f))) = find_goal state;
wenzelm@12010
   429
    val exp_tac = ProofContext.export true inner outer;
wenzelm@9469
   430
    fun exp_rule rule =
wenzelm@9469
   431
      let
wenzelm@9469
   432
        val _ = print_rule rule;
wenzelm@11816
   433
        val thmq = FINDGOAL (refine_tac rule) thm;
wenzelm@9469
   434
      in Seq.map (fn th => map_goal I (K ((result, (facts, th)), f)) state) thmq end;
wenzelm@9469
   435
  in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end;
wenzelm@6932
   436
wenzelm@6932
   437
fun transfer_facts inner_state state =
wenzelm@6932
   438
  (case get_facts inner_state of
wenzelm@6932
   439
    None => Seq.single (reset_facts state)
wenzelm@8617
   440
  | Some thms =>
wenzelm@8617
   441
      let
wenzelm@12010
   442
        val exp_tac = ProofContext.export false (context_of inner_state) (context_of state);
wenzelm@11816
   443
        val thmqs = map exp_tac thms;
wenzelm@8617
   444
      in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end);
wenzelm@6932
   445
wenzelm@6932
   446
wenzelm@6932
   447
(* prepare result *)
wenzelm@6932
   448
wenzelm@6932
   449
fun prep_result state t raw_thm =
wenzelm@5820
   450
  let
wenzelm@5820
   451
    val ctxt = context_of state;
wenzelm@5820
   452
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   453
wenzelm@5820
   454
    val ngoals = Thm.nprems_of raw_thm;
wenzelm@5820
   455
    val _ =
wenzelm@5820
   456
      if ngoals = 0 then ()
wenzelm@11891
   457
      else (Display.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
wenzelm@5820
   458
wenzelm@6932
   459
    val thm = raw_thm RS Drule.rev_triv_goal;
wenzelm@8617
   460
    val {hyps, prop, sign, ...} = Thm.rep_thm thm;
wenzelm@5820
   461
    val tsig = Sign.tsig_of sign;
wenzelm@6932
   462
wenzelm@9469
   463
    val casms = flat (map #1 (assumptions state));
wenzelm@11816
   464
    val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms);
wenzelm@5820
   465
  in
wenzelm@6932
   466
    if not (null bad_hyps) then
wenzelm@6932
   467
      err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) bad_hyps))
wenzelm@10553
   468
    else if not (Pattern.matches (Sign.tsig_of (sign_of state)) (t, prop)) then
wenzelm@7605
   469
      err ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
wenzelm@8617
   470
    else thm
wenzelm@5820
   471
  end;
wenzelm@5820
   472
wenzelm@5820
   473
wenzelm@5820
   474
wenzelm@5820
   475
(*** structured proof commands ***)
wenzelm@5820
   476
wenzelm@5820
   477
(** context **)
wenzelm@5820
   478
wenzelm@5820
   479
(* bind *)
wenzelm@5820
   480
wenzelm@5820
   481
fun gen_bind f x state =
wenzelm@5820
   482
  state
wenzelm@5820
   483
  |> assert_forward
wenzelm@5820
   484
  |> map_context (f x)
wenzelm@5820
   485
  |> reset_facts;
wenzelm@5820
   486
wenzelm@8617
   487
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   488
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   489
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   490
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   491
wenzelm@5820
   492
wenzelm@9196
   493
(* have_thmss etc. *)
wenzelm@5820
   494
wenzelm@9196
   495
fun have_thmss args state =
wenzelm@5820
   496
  state
wenzelm@5820
   497
  |> assert_forward
wenzelm@9196
   498
  |> map_context_result (ProofContext.have_thmss args)
wenzelm@9196
   499
  |> these_factss;
wenzelm@5820
   500
wenzelm@9196
   501
fun simple_have_thms name thms = have_thmss [((name, []), [(thms, [])])];
wenzelm@9196
   502
wenzelm@9196
   503
fun with_thmss args state =
wenzelm@9196
   504
  let val state' = state |> have_thmss args
wenzelm@9196
   505
  in state' |> simple_have_thms "" (the_facts state' @ the_facts state) end;
wenzelm@6876
   506
wenzelm@5820
   507
wenzelm@5820
   508
(* fix *)
wenzelm@5820
   509
wenzelm@5820
   510
fun gen_fix f xs state =
wenzelm@5820
   511
  state
wenzelm@5820
   512
  |> assert_forward
wenzelm@5820
   513
  |> map_context (f xs)
wenzelm@5820
   514
  |> reset_facts;
wenzelm@5820
   515
wenzelm@5820
   516
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   517
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   518
wenzelm@5820
   519
wenzelm@11891
   520
(* assume and presume *)
wenzelm@5820
   521
wenzelm@9469
   522
fun gen_assume f exp args state =
wenzelm@5820
   523
  state
wenzelm@5820
   524
  |> assert_forward
wenzelm@9469
   525
  |> map_context_result (f exp args)
wenzelm@11924
   526
  |> these_factss;
wenzelm@6932
   527
wenzelm@7271
   528
val assm = gen_assume ProofContext.assume;
wenzelm@7271
   529
val assm_i = gen_assume ProofContext.assume_i;
wenzelm@11917
   530
val assume = assm ProofContext.export_assume;
wenzelm@11917
   531
val assume_i = assm_i ProofContext.export_assume;
wenzelm@11917
   532
val presume = assm ProofContext.export_presume;
wenzelm@11917
   533
val presume_i = assm_i ProofContext.export_presume;
wenzelm@5820
   534
wenzelm@7271
   535
wenzelm@5820
   536
wenzelm@11891
   537
(** local definitions **)
wenzelm@11891
   538
wenzelm@11891
   539
fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
wenzelm@11891
   540
  let
wenzelm@11891
   541
    val _ = assert_forward state;
wenzelm@11891
   542
    val ctxt = context_of state;
wenzelm@11891
   543
wenzelm@11891
   544
    (*rhs*)
wenzelm@11891
   545
    val name = if raw_name = "" then Thm.def_name x else raw_name;
wenzelm@11891
   546
    val rhs = prep_term ctxt raw_rhs;
wenzelm@11891
   547
    val T = Term.fastype_of rhs;
wenzelm@11891
   548
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
wenzelm@11891
   549
wenzelm@11891
   550
    (*lhs*)
wenzelm@11891
   551
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
wenzelm@11891
   552
  in
wenzelm@11891
   553
    state
wenzelm@11891
   554
    |> fix [([x], None)]
wenzelm@11891
   555
    |> match_bind_i [(pats, rhs)]
wenzelm@11917
   556
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
wenzelm@11891
   557
  end;
wenzelm@11891
   558
wenzelm@11891
   559
val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
wenzelm@11891
   560
val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
wenzelm@11891
   561
wenzelm@11891
   562
wenzelm@8374
   563
(* invoke_case *)
wenzelm@8374
   564
wenzelm@11793
   565
fun invoke_case (name, xs, atts) state =
wenzelm@9292
   566
  let
wenzelm@10830
   567
    val (state', (lets, asms)) =
wenzelm@11793
   568
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
wenzelm@9292
   569
  in
wenzelm@10830
   570
    state'
wenzelm@10809
   571
    |> add_binds_i lets
wenzelm@10830
   572
    |> assume_i [((name, atts), map (rpair ([], [])) asms)]
wenzelm@8374
   573
  end;
wenzelm@8374
   574
wenzelm@8374
   575
wenzelm@5820
   576
wenzelm@5820
   577
(** goals **)
wenzelm@5820
   578
wenzelm@5820
   579
(* forward chaining *)
wenzelm@5820
   580
wenzelm@5820
   581
fun chain state =
wenzelm@5820
   582
  state
wenzelm@5820
   583
  |> assert_forward
wenzelm@6848
   584
  |> assert_facts
wenzelm@5820
   585
  |> enter_forward_chain;
wenzelm@5820
   586
wenzelm@5820
   587
fun from_facts facts state =
wenzelm@5820
   588
  state
wenzelm@5820
   589
  |> put_facts (Some facts)
wenzelm@5820
   590
  |> chain;
wenzelm@5820
   591
wenzelm@5820
   592
wenzelm@5820
   593
(* setup goals *)
wenzelm@5820
   594
wenzelm@11549
   595
val rule_contextN = "rule_context";
wenzelm@8991
   596
wenzelm@12010
   597
fun setup_goal opt_block prepp autofix kind after_qed name raw_propp state =
wenzelm@5820
   598
  let
wenzelm@10464
   599
    val (state', ([[prop]], gen_binds)) =
wenzelm@5936
   600
      state
wenzelm@5936
   601
      |> assert_forward_or_chain
wenzelm@5936
   602
      |> enter_forward
wenzelm@8617
   603
      |> opt_block
wenzelm@10464
   604
      |> map_context_result (fn ct => prepp (ct, [[raw_propp]]));
wenzelm@6932
   605
    val cprop = Thm.cterm_of (sign_of state') prop;
wenzelm@7556
   606
    val goal = Drule.mk_triv_goal cprop;
wenzelm@10553
   607
wenzelm@10553
   608
    val tvars = Term.term_tvars prop;
wenzelm@10553
   609
    val vars = Term.term_vars prop;
wenzelm@5820
   610
  in
wenzelm@10553
   611
    if null tvars then ()
wenzelm@10553
   612
    else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
wenzelm@10553
   613
      commas (map (Syntax.string_of_vname o #1) tvars), state);
wenzelm@10553
   614
    if null vars then ()
wenzelm@10553
   615
    else warning ("Goal statement contains unbound schematic variable(s): " ^
wenzelm@10553
   616
      commas (map (Sign.string_of_term (sign_of state)) vars));
wenzelm@5936
   617
    state'
wenzelm@12010
   618
    |> map_context (autofix prop)
wenzelm@12010
   619
    |> put_goal (Some (((kind, name, prop), ([], goal)), after_qed o map_context gen_binds))
wenzelm@11549
   620
    |> map_context (ProofContext.add_cases (RuleCases.make true goal [rule_contextN]))
wenzelm@6790
   621
    |> auto_bind_goal prop
wenzelm@5820
   622
    |> (if is_chain state then use_facts else reset_facts)
wenzelm@5820
   623
    |> new_block
wenzelm@5820
   624
    |> enter_backward
wenzelm@5820
   625
  end;
wenzelm@5820
   626
wenzelm@12010
   627
fun autofix_frees t =
wenzelm@12010
   628
  let val frees = foldl_aterms (fn (vs, Free v) => v ins vs | (vs, _) => vs) ([], t)
wenzelm@12010
   629
  in ProofContext.fix_direct (map (fn (x, T) => ([x], Some T)) frees) end;
wenzelm@5820
   630
wenzelm@5820
   631
(*global goals*)
wenzelm@12010
   632
fun global_goal prepp kind locale name atts x thy =
wenzelm@12010
   633
  setup_goal I prepp autofix_frees (Theorem (kind, locale, atts))
wenzelm@12010
   634
    Seq.single name x (init_state thy locale);
wenzelm@5820
   635
wenzelm@11742
   636
val theorem = global_goal ProofContext.bind_propp_schematic;
wenzelm@11742
   637
val theorem_i = global_goal ProofContext.bind_propp_schematic_i;
wenzelm@5820
   638
wenzelm@5820
   639
wenzelm@5820
   640
(*local goals*)
wenzelm@10938
   641
fun local_goal' prepp kind (check: bool -> state -> state)
wenzelm@10938
   642
    f name atts args int state =
wenzelm@7928
   643
  state
wenzelm@12010
   644
  |> setup_goal open_block prepp (K I) (kind atts) f name args
wenzelm@10936
   645
  |> warn_extra_tfrees state
wenzelm@10936
   646
  |> check int;
wenzelm@5820
   647
wenzelm@10936
   648
fun local_goal prepp kind f name atts args =
wenzelm@10936
   649
  local_goal' prepp kind (K I) f name atts args false;
wenzelm@10936
   650
wenzelm@10936
   651
val show = local_goal' ProofContext.bind_propp Show;
wenzelm@10936
   652
val show_i = local_goal' ProofContext.bind_propp_i Show;
wenzelm@10936
   653
val have = local_goal ProofContext.bind_propp Have;
wenzelm@10380
   654
val have_i = local_goal ProofContext.bind_propp_i Have;
wenzelm@5820
   655
wenzelm@5820
   656
wenzelm@5820
   657
wenzelm@5820
   658
(** conclusions **)
wenzelm@5820
   659
wenzelm@5820
   660
(* current goal *)
wenzelm@5820
   661
wenzelm@7176
   662
fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
wenzelm@5820
   663
  | current_goal state = raise STATE ("No current goal!", state);
wenzelm@5820
   664
wenzelm@7176
   665
fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
wenzelm@6404
   666
      raise STATE ("No goal in this block!", state)
wenzelm@7176
   667
  | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
wenzelm@6404
   668
      raise STATE ("Goal present in this block!", state)
wenzelm@6404
   669
  | assert_current_goal _ state = state;
wenzelm@5820
   670
wenzelm@12010
   671
fun assert_bottom b (state as State (_, nodes)) =
wenzelm@12010
   672
  let val bot = (length nodes <= 2) in
wenzelm@12010
   673
    if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
wenzelm@12010
   674
    else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
wenzelm@12010
   675
    else state
wenzelm@12010
   676
  end;
wenzelm@5820
   677
wenzelm@6404
   678
val at_bottom = can (assert_bottom true o close_block);
wenzelm@6404
   679
wenzelm@6932
   680
fun end_proof bot state =
wenzelm@5820
   681
  state
wenzelm@5820
   682
  |> assert_forward
wenzelm@5820
   683
  |> close_block
wenzelm@5820
   684
  |> assert_bottom bot
wenzelm@7011
   685
  |> assert_current_goal true
wenzelm@7011
   686
  |> goal_facts (K []);
wenzelm@5820
   687
wenzelm@5820
   688
wenzelm@6404
   689
(* local_qed *)
wenzelm@5820
   690
wenzelm@6982
   691
fun finish_local (print_result, print_rule) state =
wenzelm@5820
   692
  let
wenzelm@8617
   693
    val (goal_ctxt, (((kind, name, t), (_, raw_thm)), after_qed)) = current_goal state;
wenzelm@8617
   694
    val outer_state = state |> close_block;
wenzelm@8617
   695
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   696
wenzelm@6932
   697
    val result = prep_result state t raw_thm;
wenzelm@12010
   698
    val resultq = ProofContext.export false goal_ctxt outer_ctxt result;
wenzelm@9469
   699
wenzelm@5820
   700
    val (atts, opt_solve) =
wenzelm@5820
   701
      (case kind of
wenzelm@10380
   702
        Show atts => (atts, export_goal print_rule result goal_ctxt)
wenzelm@10380
   703
      | Have atts => (atts, Seq.single)
wenzelm@6932
   704
      | _ => err_malformed "finish_local" state);
wenzelm@5820
   705
  in
wenzelm@6731
   706
    print_result {kind = kind_name kind, name = name, thm = result};
wenzelm@8617
   707
    outer_state
wenzelm@8617
   708
    |> auto_bind_facts name [ProofContext.generalize goal_ctxt outer_ctxt t]
wenzelm@9469
   709
    |> (fn st => Seq.map (fn res =>
wenzelm@9469
   710
      have_thmss [((name, atts), [Thm.no_attributes [res]])] st) resultq)
wenzelm@9469
   711
    |> (Seq.flat o Seq.map opt_solve)
wenzelm@7176
   712
    |> (Seq.flat o Seq.map after_qed)
wenzelm@5820
   713
  end;
wenzelm@5820
   714
wenzelm@6982
   715
fun local_qed finalize print state =
wenzelm@6404
   716
  state
wenzelm@6932
   717
  |> end_proof false
wenzelm@6871
   718
  |> finalize
wenzelm@6982
   719
  |> (Seq.flat o Seq.map (finish_local print));
wenzelm@5820
   720
wenzelm@5820
   721
wenzelm@6404
   722
(* global_qed *)
wenzelm@5820
   723
wenzelm@6950
   724
fun finish_global state =
wenzelm@5820
   725
  let
wenzelm@12010
   726
    val (goal_ctxt, (((kind, name, t), (_, raw_thm)), _)) = current_goal state;
wenzelm@12010
   727
    val full_name = if name = "" then "" else Sign.full_name (sign_of state) name;
wenzelm@12010
   728
    val locale_context = context_of (state |> close_block);  (* FIXME unused *)
wenzelm@12010
   729
    val theory_context = context_of (state |> close_block |> close_block);
wenzelm@12010
   730
wenzelm@12010
   731
    val result = prep_result state t raw_thm;
wenzelm@12010
   732
    val resultq =
wenzelm@12010
   733
      ProofContext.export false goal_ctxt theory_context result
wenzelm@12010
   734
      |> Seq.map Drule.local_standard;
wenzelm@5820
   735
wenzelm@12010
   736
    val (atts, opt_locale) =
wenzelm@12010
   737
      (case kind of Theorem (s, loc, atts) => (atts @ [Drule.kind s], loc)
wenzelm@6932
   738
      | _ => err_malformed "finish_global" state);
wenzelm@12010
   739
    val thy = theory_of state;
wenzelm@12010
   740
  in
wenzelm@12010
   741
    resultq |> Seq.map (fn res =>
wenzelm@12010
   742
      let val (thy', res') = PureThy.store_thm ((name, res), atts) thy
wenzelm@12010
   743
      in (thy', {kind = kind_name kind, name = name, thm = res'}) end)
wenzelm@12010
   744
  end;
wenzelm@5820
   745
wenzelm@10553
   746
(*note: clients should inspect first result only, as backtracking may destroy theory*)
wenzelm@6950
   747
fun global_qed finalize state =
wenzelm@5820
   748
  state
wenzelm@6932
   749
  |> end_proof true
wenzelm@6871
   750
  |> finalize
wenzelm@12010
   751
  |> Seq.map finish_global
wenzelm@12010
   752
  |> Seq.flat;
wenzelm@5820
   753
wenzelm@5820
   754
wenzelm@6896
   755
wenzelm@6896
   756
(** blocks **)
wenzelm@6896
   757
wenzelm@6896
   758
(* begin_block *)
wenzelm@6896
   759
wenzelm@6896
   760
fun begin_block state =
wenzelm@6896
   761
  state
wenzelm@6896
   762
  |> assert_forward
wenzelm@6896
   763
  |> new_block
wenzelm@6896
   764
  |> open_block;
wenzelm@6896
   765
wenzelm@6896
   766
wenzelm@6896
   767
(* end_block *)
wenzelm@6896
   768
wenzelm@6896
   769
fun end_block state =
wenzelm@6896
   770
  state
wenzelm@6896
   771
  |> assert_forward
wenzelm@6896
   772
  |> close_block
wenzelm@6896
   773
  |> assert_current_goal false
wenzelm@6896
   774
  |> close_block
wenzelm@6932
   775
  |> transfer_facts state;
wenzelm@6896
   776
wenzelm@6896
   777
wenzelm@6896
   778
(* next_block *)
wenzelm@6896
   779
wenzelm@6896
   780
fun next_block state =
wenzelm@6896
   781
  state
wenzelm@6896
   782
  |> assert_forward
wenzelm@6896
   783
  |> close_block
wenzelm@8718
   784
  |> new_block
wenzelm@8718
   785
  |> reset_facts;
wenzelm@6896
   786
wenzelm@6896
   787
wenzelm@5820
   788
end;
wenzelm@8152
   789
wenzelm@8152
   790
structure BasicProof: BASIC_PROOF = Proof;
wenzelm@8152
   791
open BasicProof;