src/Pure/Isar/calculation.ML
author wenzelm
Mon Aug 09 22:21:08 1999 +0200 (1999-08-09)
changeset 7197 3ddf4a55d765
parent 6903 682f8a9ec75d
child 7414 9bc7797d1249
permissions -rw-r--r--
append user rules;
wenzelm@6778
     1
(*  Title:      Pure/Isar/calculation.ML
wenzelm@6778
     2
    ID:         $Id$
wenzelm@6778
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6778
     4
wenzelm@6778
     5
Support for calculational proofs.
wenzelm@6778
     6
*)
wenzelm@6778
     7
wenzelm@6778
     8
signature CALCULATION =
wenzelm@6778
     9
sig
wenzelm@6778
    10
  val print_global_rules: theory -> unit
wenzelm@6778
    11
  val print_local_rules: Proof.context -> unit
wenzelm@6778
    12
  val trans_add_global: theory attribute
wenzelm@6778
    13
  val trans_del_global: theory attribute
wenzelm@6778
    14
  val trans_add_local: Proof.context attribute
wenzelm@6778
    15
  val trans_del_local: Proof.context attribute
wenzelm@6877
    16
  val also: thm list option -> (thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@6877
    17
  val finally: thm list option -> (thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@6778
    18
  val setup: (theory -> theory) list
wenzelm@6778
    19
end;
wenzelm@6778
    20
wenzelm@6778
    21
structure Calculation: CALCULATION =
wenzelm@6778
    22
struct
wenzelm@6778
    23
wenzelm@6778
    24
wenzelm@6778
    25
(** global and local calculation data **)
wenzelm@6778
    26
wenzelm@6778
    27
fun print_rules ths =
wenzelm@6778
    28
  Pretty.writeln (Pretty.big_list "calculation rules:" (map Display.pretty_thm ths));
wenzelm@6778
    29
wenzelm@6778
    30
(* theory data kind 'Isar/calculation' *)
wenzelm@6778
    31
wenzelm@6778
    32
structure GlobalCalculationArgs =
wenzelm@6778
    33
struct
wenzelm@6778
    34
  val name = "Isar/calculation";
wenzelm@6778
    35
  type T = thm list;
wenzelm@6778
    36
wenzelm@6778
    37
  val empty = [];
wenzelm@6778
    38
  val copy = I;
wenzelm@6778
    39
  val prep_ext = I;
wenzelm@6778
    40
  fun merge (ths1, ths2) = Library.generic_merge Thm.eq_thm I I ths1 ths2;
wenzelm@6778
    41
  fun print _ = print_rules;
wenzelm@6778
    42
end;
wenzelm@6778
    43
wenzelm@6778
    44
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
wenzelm@6778
    45
val print_global_rules = GlobalCalculation.print;
wenzelm@6778
    46
wenzelm@6778
    47
wenzelm@6778
    48
(* proof data kind 'Isar/calculation' *)
wenzelm@6778
    49
wenzelm@6778
    50
structure LocalCalculationArgs =
wenzelm@6778
    51
struct
wenzelm@6778
    52
  val name = "Isar/calculation";
wenzelm@6778
    53
  type T = thm list * (thm * int) option;
wenzelm@6778
    54
wenzelm@6778
    55
  fun init thy = (GlobalCalculation.get thy, None);
wenzelm@6778
    56
  fun print _ (ths, _) = print_rules ths;
wenzelm@6778
    57
end;
wenzelm@6778
    58
wenzelm@6778
    59
structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
wenzelm@6787
    60
val get_local_rules = #1 o LocalCalculation.get_st;
wenzelm@6778
    61
val print_local_rules = LocalCalculation.print;
wenzelm@6778
    62
wenzelm@6778
    63
wenzelm@6778
    64
(* access calculation *)
wenzelm@6778
    65
wenzelm@6778
    66
fun get_calculation state =
wenzelm@6787
    67
  (case #2 (LocalCalculation.get_st state) of
wenzelm@6778
    68
    None => None
wenzelm@6778
    69
  | Some (thm, lev) => if lev = Proof.level state then Some thm else None);
wenzelm@6778
    70
wenzelm@6778
    71
fun put_calculation thm state =
wenzelm@6778
    72
  LocalCalculation.put_st (get_local_rules state, Some (thm, Proof.level state)) state;
wenzelm@6778
    73
wenzelm@6787
    74
fun reset_calculation state =
wenzelm@6787
    75
  LocalCalculation.put_st (get_local_rules state, None) state;
wenzelm@6787
    76
wenzelm@6778
    77
wenzelm@6778
    78
wenzelm@6778
    79
(** attributes **)
wenzelm@6778
    80
wenzelm@6778
    81
(* trans add/del *)
wenzelm@6778
    82
wenzelm@6778
    83
local
wenzelm@6778
    84
wenzelm@6778
    85
fun map_rules_global f thy = GlobalCalculation.put (f (GlobalCalculation.get thy)) thy;
wenzelm@6778
    86
fun map_rules_local f ctxt = LocalCalculation.put (f (LocalCalculation.get ctxt)) ctxt;
wenzelm@6778
    87
wenzelm@6778
    88
fun add_trans thm rules = Library.gen_ins Thm.eq_thm (thm, rules);
wenzelm@6778
    89
fun del_trans thm rules = Library.gen_rem Thm.eq_thm (rules, thm);
wenzelm@6778
    90
wenzelm@6778
    91
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    92
wenzelm@6778
    93
in
wenzelm@6778
    94
wenzelm@6778
    95
val trans_add_global = mk_att map_rules_global add_trans;
wenzelm@6778
    96
val trans_del_global = mk_att map_rules_global del_trans;
wenzelm@6778
    97
val trans_add_local = mk_att map_rules_local (Library.apfst o add_trans);
wenzelm@6778
    98
val trans_del_local = mk_att map_rules_local (Library.apfst o del_trans);
wenzelm@6778
    99
wenzelm@6778
   100
end;
wenzelm@6778
   101
wenzelm@6778
   102
wenzelm@6778
   103
(* concrete syntax *)
wenzelm@6778
   104
wenzelm@6778
   105
val transN = "trans";
wenzelm@6778
   106
val addN = "add";
wenzelm@6778
   107
val delN = "del";
wenzelm@6778
   108
wenzelm@6778
   109
fun trans_att add del =
wenzelm@6778
   110
  Attrib.syntax (Scan.lift (Args.$$$ addN >> K add || Args.$$$ delN >> K del || Scan.succeed add));
wenzelm@6778
   111
wenzelm@6778
   112
val trans_attr =
wenzelm@6778
   113
  (trans_att trans_add_global trans_del_global, trans_att trans_add_local trans_del_local);
wenzelm@6778
   114
wenzelm@6778
   115
wenzelm@6787
   116
wenzelm@6778
   117
(** proof commands **)
wenzelm@6778
   118
wenzelm@6778
   119
val calculationN = "calculation";
wenzelm@6778
   120
wenzelm@6877
   121
fun calculate final opt_rules print state =
wenzelm@6778
   122
  let
wenzelm@6903
   123
    fun err_if b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@6778
   124
    val fact = Proof.the_fact state;
wenzelm@7197
   125
    val rules = Seq.of_list (if_none opt_rules [] @ get_local_rules state);
wenzelm@6903
   126
    val (initial, calculations) =
wenzelm@6778
   127
      (case get_calculation state of
wenzelm@6903
   128
        None => (true, Seq.single fact)
wenzelm@6903
   129
      | Some thm => (false, Seq.flat (Seq.map (Method.multi_resolve [thm, fact]) rules)));
wenzelm@6778
   130
  in
wenzelm@6903
   131
    err_if (initial andalso final) "No calculation yet";
wenzelm@6903
   132
    err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@6782
   133
    calculations |> Seq.map (fn calc =>
wenzelm@6782
   134
      (print calc;
wenzelm@6787
   135
        (if final then
wenzelm@6787
   136
          state
wenzelm@6787
   137
          |> reset_calculation
wenzelm@6877
   138
          |> Proof.simple_have_thms calculationN []
wenzelm@6877
   139
          |> Proof.simple_have_thms "" [calc]
wenzelm@6787
   140
          |> Proof.chain
wenzelm@6787
   141
        else
wenzelm@6787
   142
          state
wenzelm@6787
   143
          |> put_calculation calc
wenzelm@6877
   144
          |> Proof.simple_have_thms calculationN [calc]
wenzelm@6787
   145
          |> Proof.reset_facts)))
wenzelm@6778
   146
  end;
wenzelm@6778
   147
wenzelm@6782
   148
fun also print = calculate false print;
wenzelm@6782
   149
fun finally print = calculate true print;
wenzelm@6778
   150
wenzelm@6778
   151
wenzelm@6778
   152
wenzelm@6778
   153
(** theory setup **)
wenzelm@6778
   154
wenzelm@6778
   155
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@6778
   156
  Attrib.add_attributes [(transN, trans_attr, "transitivity rule")]];
wenzelm@6778
   157
wenzelm@6778
   158
wenzelm@6778
   159
end;