src/Pure/Isar/calculation.ML
author wenzelm
Thu Jul 01 21:25:58 1999 +0200 (1999-07-01)
changeset 6877 3d5e5e6f9e20
parent 6787 25265c6807c3
child 6903 682f8a9ec75d
permissions -rw-r--r--
also, finally: opt_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@6778
   123
    val fact = Proof.the_fact state;
wenzelm@6877
   124
    val rules = Seq.of_list (case opt_rules of None => get_local_rules state | Some thms => thms);
wenzelm@6778
   125
    val calculations =
wenzelm@6778
   126
      (case get_calculation state of
wenzelm@6778
   127
        None => Seq.single fact
wenzelm@6778
   128
      | Some thm => Seq.flat (Seq.map (Method.multi_resolve [thm, fact]) rules));
wenzelm@6778
   129
  in
wenzelm@6782
   130
    calculations |> Seq.map (fn calc =>
wenzelm@6782
   131
      (print calc;
wenzelm@6787
   132
        (if final then
wenzelm@6787
   133
          state
wenzelm@6787
   134
          |> reset_calculation
wenzelm@6877
   135
          |> Proof.simple_have_thms calculationN []
wenzelm@6877
   136
          |> Proof.simple_have_thms "" [calc]
wenzelm@6787
   137
          |> Proof.chain
wenzelm@6787
   138
        else
wenzelm@6787
   139
          state
wenzelm@6787
   140
          |> put_calculation calc
wenzelm@6877
   141
          |> Proof.simple_have_thms calculationN [calc]
wenzelm@6787
   142
          |> Proof.reset_facts)))
wenzelm@6778
   143
  end;
wenzelm@6778
   144
wenzelm@6782
   145
fun also print = calculate false print;
wenzelm@6782
   146
fun finally print = calculate true print;
wenzelm@6778
   147
wenzelm@6778
   148
wenzelm@6778
   149
wenzelm@6778
   150
(** theory setup **)
wenzelm@6778
   151
wenzelm@6778
   152
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@6778
   153
  Attrib.add_attributes [(transN, trans_attr, "transitivity rule")]];
wenzelm@6778
   154
wenzelm@6778
   155
wenzelm@6778
   156
end;