src/Pure/Isar/calculation.ML
author skalberg
Sun, 13 Feb 2005 17:15:14 +0100
changeset 15531 08c8dad8e399
parent 14981 e73f8140af78
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/calculation.ML
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     4
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     5
Support for calculational proofs.
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     6
*)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     7
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     8
signature CALCULATION =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     9
sig
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    10
  val print_global_rules: theory -> unit
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    11
  val print_local_rules: Proof.context -> unit
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    12
  val trans_add_global: theory attribute
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    13
  val trans_del_global: theory attribute
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    14
  val trans_add_local: Proof.context attribute
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    15
  val trans_del_local: Proof.context attribute
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    16
  val sym_add_global: theory attribute
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    17
  val sym_del_global: theory attribute
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    18
  val sym_add_local: Proof.context attribute
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    19
  val sym_del_local: Proof.context attribute
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    20
  val symmetric_global: theory attribute
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    21
  val symmetric_local: Proof.context attribute
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    22
  val also: thm list option -> (Proof.context -> thm list -> unit)
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    23
    -> Proof.state -> Proof.state Seq.seq
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    24
  val finally: thm list option -> (Proof.context -> thm list -> unit)
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    25
    -> Proof.state -> Proof.state Seq.seq
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    26
  val moreover: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    27
  val ultimately: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    28
  val setup: (theory -> theory) list
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    29
end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    30
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    31
structure Calculation: CALCULATION =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    32
struct
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    33
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    34
(** global and local calculation data **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    35
8300
4c3f83414de3 use NetRules;
wenzelm
parents: 8150
diff changeset
    36
(* theory data kind 'Isar/calculation' *)
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    37
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    38
fun print_rules prt x (trans, sym) =
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    39
  [Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules trans)),
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    40
    Pretty.big_list "symmetry rules:" (map (prt x) sym)]
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    41
  |> Pretty.chunks |> Pretty.writeln;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    42
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    43
structure GlobalCalculationArgs =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    44
struct
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    45
  val name = "Isar/calculation";
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    46
  type T = thm NetRules.T * thm list
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    47
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    48
  val empty = (NetRules.elim, []);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    49
  val copy = I;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    50
  val prep_ext = I;
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    51
  fun merge ((trans1, sym1), (trans2, sym2)) =
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    52
    (NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2));
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    53
  val print = print_rules Display.pretty_thm_sg;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    54
end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    55
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    56
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    57
val print_global_rules = GlobalCalculation.print;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    58
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    59
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    60
(* proof data kind 'Isar/calculation' *)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    61
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    62
structure LocalCalculationArgs =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    63
struct
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    64
  val name = "Isar/calculation";
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    65
  type T = (thm NetRules.T * thm list) * (thm list * int) option;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    66
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    67
  fun init thy = (GlobalCalculation.get thy, NONE);
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
    68
  fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    69
end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    70
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    71
structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
13371
82a057cf4413 avoid "_st" versions of proof data;
wenzelm
parents: 12805
diff changeset
    72
val get_local_rules = #1 o LocalCalculation.get o Proof.context_of;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    73
val print_local_rules = LocalCalculation.print;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    74
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    75
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    76
(* access calculation *)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    77
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    78
fun get_calculation state =
13371
82a057cf4413 avoid "_st" versions of proof data;
wenzelm
parents: 12805
diff changeset
    79
  (case #2 (LocalCalculation.get (Proof.context_of state)) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    80
    NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    81
  | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    82
7414
9bc7797d1249 calculation: thm list;
wenzelm
parents: 7197
diff changeset
    83
fun put_calculation thms state =
13371
82a057cf4413 avoid "_st" versions of proof data;
wenzelm
parents: 12805
diff changeset
    84
  Proof.map_context
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    85
    (LocalCalculation.put (get_local_rules state, SOME (thms, Proof.level state))) state;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    86
6787
wenzelm
parents: 6782
diff changeset
    87
fun reset_calculation state =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    88
  Proof.map_context (LocalCalculation.put (get_local_rules state, NONE)) state;
6787
wenzelm
parents: 6782
diff changeset
    89
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    90
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    91
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    92
(** attributes **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    93
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    94
(* add/del rules *)
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    95
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    96
fun global_att f (x, thm) = (GlobalCalculation.map (f thm) x, thm);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    97
fun local_att f (x, thm) = (LocalCalculation.map (apfst (f thm)) x, thm);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    98
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    99
val trans_add_global = global_att (apfst o NetRules.insert);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   100
val trans_del_global = global_att (apfst o NetRules.delete);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   101
val trans_add_local = local_att (apfst o NetRules.insert);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   102
val trans_del_local = local_att (apfst o NetRules.delete);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   103
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   104
val sym_add_global = global_att (apsnd o Drule.add_rule) o ContextRules.elim_query_global NONE;
12402
cef751fff6b0 clarified sym_del;
wenzelm
parents: 12379
diff changeset
   105
val sym_del_global = global_att (apsnd o Drule.del_rule) o ContextRules.rule_del_global;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   106
val sym_add_local = local_att (apsnd o Drule.add_rule) o ContextRules.elim_query_local NONE;
12402
cef751fff6b0 clarified sym_del;
wenzelm
parents: 12379
diff changeset
   107
val sym_del_local = local_att (apsnd o Drule.del_rule) o ContextRules.rule_del_local;
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   108
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   109
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   110
(* symmetry *)
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   111
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   112
fun gen_symmetric get_sym = Drule.rule_attribute (fn x => fn th =>
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   113
  (case Seq.chop (2, Method.multi_resolves [th] (get_sym x)) of
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   114
    ([th'], _) => th'
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   115
  | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   116
  | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   117
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   118
val symmetric_global = gen_symmetric (#2 o GlobalCalculation.get);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   119
val symmetric_local = gen_symmetric (#2 o #1 o LocalCalculation.get);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   120
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   121
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   122
(* concrete syntax *)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   123
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   124
val trans_attr =
8634
3f34637cb9c0 use Attrib.add_del_args;
wenzelm
parents: 8588
diff changeset
   125
 (Attrib.add_del_args trans_add_global trans_del_global,
3f34637cb9c0 use Attrib.add_del_args;
wenzelm
parents: 8588
diff changeset
   126
  Attrib.add_del_args trans_add_local trans_del_local);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   127
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   128
val sym_attr =
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   129
 (Attrib.add_del_args sym_add_global sym_del_global,
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   130
  Attrib.add_del_args sym_add_local sym_del_local);
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   131
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   132
6787
wenzelm
parents: 6782
diff changeset
   133
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   134
(** proof commands **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   135
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   136
fun assert_sane final =
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   137
  if final then Proof.assert_forward else Proof.assert_forward_or_chain;
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   138
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   139
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   140
(* maintain calculation register *)
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   141
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   142
val calculationN = "calculation";
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   143
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   144
fun maintain_calculation false calc state =
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   145
      state
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   146
      |> put_calculation calc
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   147
      |> Proof.put_thms (calculationN, calc)
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   148
  | maintain_calculation true calc state =
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   149
      state
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   150
      |> reset_calculation
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   151
      |> Proof.reset_thms calculationN
14564
3667b4616e9a renamed have_thms to note_thms;
wenzelm
parents: 14555
diff changeset
   152
      |> Proof.simple_note_thms "" calc
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   153
      |> Proof.chain;
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   154
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   155
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   156
(* 'also' and 'finally' *)
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   157
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   158
fun err_if state b msg = if b then raise Proof.STATE (msg, state) else ();
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   159
6877
3d5e5e6f9e20 also, finally: opt_rules;
wenzelm
parents: 6787
diff changeset
   160
fun calculate final opt_rules print state =
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   161
  let
12805
3be853cf19cf Thm.prop_of;
wenzelm
parents: 12402
diff changeset
   162
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
9322
b5bd2709a2c2 eq_prop: strip_assums_concl;
wenzelm
parents: 9217
diff changeset
   163
    val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl);
11097
c1be9f2dff4c more robust selection of calculational rules;
wenzelm
parents: 10478
diff changeset
   164
    fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
8300
4c3f83414de3 use NetRules;
wenzelm
parents: 8150
diff changeset
   165
11097
c1be9f2dff4c more robust selection of calculational rules;
wenzelm
parents: 10478
diff changeset
   166
    fun combine ths =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   167
      (case opt_rules of SOME rules => rules
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   168
      | NONE =>
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   169
          (case ths of [] => NetRules.rules (#1 (get_local_rules state))
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   170
          | th :: _ => NetRules.retrieve (#1 (get_local_rules state)) (strip_assums_concl th)))
11097
c1be9f2dff4c more robust selection of calculational rules;
wenzelm
parents: 10478
diff changeset
   171
      |> Seq.of_list |> Seq.map (Method.multi_resolve ths) |> Seq.flat
c1be9f2dff4c more robust selection of calculational rules;
wenzelm
parents: 10478
diff changeset
   172
      |> Seq.filter (not o projection ths);
7414
9bc7797d1249 calculation: thm list;
wenzelm
parents: 7197
diff changeset
   173
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   174
    val facts = Proof.the_facts (assert_sane final state);
6903
682f8a9ec75d improved errors;
wenzelm
parents: 6877
diff changeset
   175
    val (initial, calculations) =
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   176
      (case get_calculation state of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   177
        NONE => (true, Seq.single facts)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   178
      | SOME calc => (false, Seq.map single (combine (calc @ facts))));
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   179
  in
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   180
    err_if state (initial andalso final) "No calculation yet";
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   181
    err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
   182
    calculations |> Seq.map (fn calc => (print (Proof.context_of state) calc;
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
   183
        state |> maintain_calculation final calc))
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   184
  end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   185
6782
adf099e851ed fixed "...": dest_arg;
wenzelm
parents: 6781
diff changeset
   186
fun also print = calculate false print;
adf099e851ed fixed "...": dest_arg;
wenzelm
parents: 6781
diff changeset
   187
fun finally print = calculate true print;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   188
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   189
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   190
(* 'moreover' and 'ultimately' *)
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   191
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   192
fun collect final print state =
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   193
  let
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   194
    val facts = Proof.the_facts (assert_sane final state);
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   195
    val (initial, thms) =
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   196
      (case get_calculation state of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   197
        NONE => (true, [])
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   198
      | SOME thms => (false, thms));
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   199
    val calc = thms @ facts;
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   200
  in
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   201
    err_if state (initial andalso final) "No calculation yet";
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 12021
diff changeset
   202
    print (Proof.context_of state) calc;
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   203
    state |> maintain_calculation final calc
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   204
  end;
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   205
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   206
fun moreover print = collect false print;
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   207
fun ultimately print = collect true print;
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   208
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   209
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   210
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   211
(** theory setup **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   212
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   213
val setup =
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   214
 [GlobalCalculation.init, LocalCalculation.init,
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   215
  Attrib.add_attributes
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   216
   [("trans", trans_attr, "declaration of transitivity rule"),
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   217
    ("sym", sym_attr, "declaration of symmetry rule"),
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   218
    ("symmetric", (Attrib.no_args symmetric_global, Attrib.no_args symmetric_local),
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   219
      "resolution with symmetry rule")],
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   220
  #1 o PureThy.add_thms
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   221
   [(("", transitive_thm), [trans_add_global]),
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   222
    (("", symmetric_thm), [sym_add_global])]];
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   223
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   224
end;