src/Pure/Isar/calculation.ML
author wenzelm
Wed Sep 29 13:49:07 1999 +0200 (1999-09-29)
changeset 7632 25a0d2ba3a87
parent 7600 73f91da46230
child 8150 7021549ef32d
permissions -rw-r--r--
removed extra shyps error;
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@7414
    16
  val also: thm list option -> (thm list -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@7414
    17
  val finally: thm list option -> (thm list -> 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@7414
    30
wenzelm@6778
    31
(* theory data kind 'Isar/calculation' *)
wenzelm@6778
    32
wenzelm@6778
    33
structure GlobalCalculationArgs =
wenzelm@6778
    34
struct
wenzelm@6778
    35
  val name = "Isar/calculation";
wenzelm@6778
    36
  type T = thm list;
wenzelm@6778
    37
wenzelm@6778
    38
  val empty = [];
wenzelm@6778
    39
  val copy = I;
wenzelm@6778
    40
  val prep_ext = I;
wenzelm@6778
    41
  fun merge (ths1, ths2) = Library.generic_merge Thm.eq_thm I I ths1 ths2;
wenzelm@6778
    42
  fun print _ = print_rules;
wenzelm@6778
    43
end;
wenzelm@6778
    44
wenzelm@6778
    45
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
wenzelm@6778
    46
val print_global_rules = GlobalCalculation.print;
wenzelm@6778
    47
wenzelm@6778
    48
wenzelm@6778
    49
(* proof data kind 'Isar/calculation' *)
wenzelm@6778
    50
wenzelm@6778
    51
structure LocalCalculationArgs =
wenzelm@6778
    52
struct
wenzelm@6778
    53
  val name = "Isar/calculation";
wenzelm@7414
    54
  type T = thm list * (thm list * int) option;
wenzelm@6778
    55
wenzelm@6778
    56
  fun init thy = (GlobalCalculation.get thy, None);
wenzelm@6778
    57
  fun print _ (ths, _) = print_rules ths;
wenzelm@6778
    58
end;
wenzelm@6778
    59
wenzelm@6778
    60
structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
wenzelm@6787
    61
val get_local_rules = #1 o LocalCalculation.get_st;
wenzelm@6778
    62
val print_local_rules = LocalCalculation.print;
wenzelm@6778
    63
wenzelm@6778
    64
wenzelm@6778
    65
(* access calculation *)
wenzelm@6778
    66
wenzelm@6778
    67
fun get_calculation state =
wenzelm@6787
    68
  (case #2 (LocalCalculation.get_st state) of
wenzelm@6778
    69
    None => None
wenzelm@7414
    70
  | Some (thms, lev) => if lev = Proof.level state then Some thms else None);
wenzelm@6778
    71
wenzelm@7414
    72
fun put_calculation thms state =
wenzelm@7414
    73
  LocalCalculation.put_st (get_local_rules state, Some (thms, Proof.level state)) state;
wenzelm@6778
    74
wenzelm@6787
    75
fun reset_calculation state =
wenzelm@6787
    76
  LocalCalculation.put_st (get_local_rules state, None) state;
wenzelm@6787
    77
wenzelm@6778
    78
wenzelm@6778
    79
wenzelm@6778
    80
(** attributes **)
wenzelm@6778
    81
wenzelm@6778
    82
(* trans add/del *)
wenzelm@6778
    83
wenzelm@6778
    84
local
wenzelm@6778
    85
wenzelm@6778
    86
fun map_rules_global f thy = GlobalCalculation.put (f (GlobalCalculation.get thy)) thy;
wenzelm@6778
    87
fun map_rules_local f ctxt = LocalCalculation.put (f (LocalCalculation.get ctxt)) ctxt;
wenzelm@6778
    88
wenzelm@6778
    89
fun add_trans thm rules = Library.gen_ins Thm.eq_thm (thm, rules);
wenzelm@6778
    90
fun del_trans thm rules = Library.gen_rem Thm.eq_thm (rules, thm);
wenzelm@6778
    91
wenzelm@6778
    92
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    93
wenzelm@6778
    94
in
wenzelm@6778
    95
wenzelm@6778
    96
val trans_add_global = mk_att map_rules_global add_trans;
wenzelm@6778
    97
val trans_del_global = mk_att map_rules_global del_trans;
wenzelm@6778
    98
val trans_add_local = mk_att map_rules_local (Library.apfst o add_trans);
wenzelm@6778
    99
val trans_del_local = mk_att map_rules_local (Library.apfst o del_trans);
wenzelm@6778
   100
wenzelm@6778
   101
end;
wenzelm@6778
   102
wenzelm@6778
   103
wenzelm@6778
   104
(* concrete syntax *)
wenzelm@6778
   105
wenzelm@6778
   106
val transN = "trans";
wenzelm@6778
   107
val addN = "add";
wenzelm@6778
   108
val delN = "del";
wenzelm@6778
   109
wenzelm@6778
   110
fun trans_att add del =
wenzelm@6778
   111
  Attrib.syntax (Scan.lift (Args.$$$ addN >> K add || Args.$$$ delN >> K del || Scan.succeed add));
wenzelm@6778
   112
wenzelm@6778
   113
val trans_attr =
wenzelm@6778
   114
  (trans_att trans_add_global trans_del_global, trans_att trans_add_local trans_del_local);
wenzelm@6778
   115
wenzelm@6778
   116
wenzelm@6787
   117
wenzelm@6778
   118
(** proof commands **)
wenzelm@6778
   119
wenzelm@6778
   120
val calculationN = "calculation";
wenzelm@6778
   121
wenzelm@6877
   122
fun calculate final opt_rules print state =
wenzelm@6778
   123
  let
wenzelm@6903
   124
    fun err_if b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@7414
   125
    val facts = Proof.the_facts state;
wenzelm@7197
   126
    val rules = Seq.of_list (if_none opt_rules [] @ get_local_rules state);
wenzelm@7414
   127
wenzelm@7554
   128
    val eq_prop = op aconv o pairself (#prop o Thm.rep_thm);
wenzelm@7554
   129
    fun differ thms thms' = not (Library.equal_lists eq_prop (thms, thms'));
wenzelm@7414
   130
    fun combine thms =
wenzelm@7414
   131
      Seq.map Library.single (Seq.flat (Seq.map (Method.multi_resolve (thms @ facts)) rules));
wenzelm@7414
   132
wenzelm@6903
   133
    val (initial, calculations) =
wenzelm@6778
   134
      (case get_calculation state of
wenzelm@7414
   135
        None => (true, Seq.single facts)
wenzelm@7414
   136
      | Some thms => (false, Seq.filter (differ thms) (combine thms)))
wenzelm@6778
   137
  in
wenzelm@6903
   138
    err_if (initial andalso final) "No calculation yet";
wenzelm@6903
   139
    err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@6782
   140
    calculations |> Seq.map (fn calc =>
wenzelm@6782
   141
      (print calc;
wenzelm@6787
   142
        (if final then
wenzelm@6787
   143
          state
wenzelm@6787
   144
          |> reset_calculation
wenzelm@7600
   145
          |> Proof.reset_thms calculationN
wenzelm@7414
   146
          |> Proof.simple_have_thms "" calc
wenzelm@6787
   147
          |> Proof.chain
wenzelm@6787
   148
        else
wenzelm@6787
   149
          state
wenzelm@6787
   150
          |> put_calculation calc
wenzelm@7414
   151
          |> Proof.simple_have_thms calculationN calc
wenzelm@6787
   152
          |> Proof.reset_facts)))
wenzelm@6778
   153
  end;
wenzelm@6778
   154
wenzelm@6782
   155
fun also print = calculate false print;
wenzelm@6782
   156
fun finally print = calculate true print;
wenzelm@6778
   157
wenzelm@6778
   158
wenzelm@6778
   159
wenzelm@6778
   160
(** theory setup **)
wenzelm@6778
   161
wenzelm@6778
   162
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@6778
   163
  Attrib.add_attributes [(transN, trans_attr, "transitivity rule")]];
wenzelm@6778
   164
wenzelm@6778
   165
wenzelm@6778
   166
end;