src/Pure/Isar/calculation.ML
author wenzelm
Fri Jun 04 20:10:07 1999 +0200 (1999-06-04)
changeset 6781 0db24da2a3c1
parent 6778 2f66eea8a025
child 6782 adf099e851ed
permissions -rw-r--r--
oops;
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@6778
    16
  val also: (thm -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@6778
    17
  val finally: (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
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@6778
    54
  type T = thm list * (thm * 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@6778
    61
val get_local_rules = #1 o LocalCalculation.get o Proof.context_of;
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@6778
    68
  (case #2 (LocalCalculation.get (Proof.context_of state)) of
wenzelm@6778
    69
    None => None
wenzelm@6778
    70
  | Some (thm, lev) => if lev = Proof.level state then Some thm else None);
wenzelm@6778
    71
wenzelm@6778
    72
fun put_calculation thm state =
wenzelm@6778
    73
  LocalCalculation.put_st (get_local_rules state, Some (thm, Proof.level state)) state;
wenzelm@6778
    74
wenzelm@6778
    75
wenzelm@6778
    76
wenzelm@6778
    77
(** attributes **)
wenzelm@6778
    78
wenzelm@6778
    79
(* trans add/del *)
wenzelm@6778
    80
wenzelm@6778
    81
local
wenzelm@6778
    82
wenzelm@6778
    83
fun map_rules_global f thy = GlobalCalculation.put (f (GlobalCalculation.get thy)) thy;
wenzelm@6778
    84
fun map_rules_local f ctxt = LocalCalculation.put (f (LocalCalculation.get ctxt)) ctxt;
wenzelm@6778
    85
wenzelm@6778
    86
fun add_trans thm rules = Library.gen_ins Thm.eq_thm (thm, rules);
wenzelm@6778
    87
fun del_trans thm rules = Library.gen_rem Thm.eq_thm (rules, thm);
wenzelm@6778
    88
wenzelm@6778
    89
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    90
wenzelm@6778
    91
in
wenzelm@6778
    92
wenzelm@6778
    93
val trans_add_global = mk_att map_rules_global add_trans;
wenzelm@6778
    94
val trans_del_global = mk_att map_rules_global del_trans;
wenzelm@6778
    95
val trans_add_local = mk_att map_rules_local (Library.apfst o add_trans);
wenzelm@6778
    96
val trans_del_local = mk_att map_rules_local (Library.apfst o del_trans);
wenzelm@6778
    97
wenzelm@6778
    98
end;
wenzelm@6778
    99
wenzelm@6778
   100
wenzelm@6778
   101
(* concrete syntax *)
wenzelm@6778
   102
wenzelm@6778
   103
val transN = "trans";
wenzelm@6778
   104
val addN = "add";
wenzelm@6778
   105
val delN = "del";
wenzelm@6778
   106
wenzelm@6778
   107
fun trans_att add del =
wenzelm@6778
   108
  Attrib.syntax (Scan.lift (Args.$$$ addN >> K add || Args.$$$ delN >> K del || Scan.succeed add));
wenzelm@6778
   109
wenzelm@6778
   110
val trans_attr =
wenzelm@6778
   111
  (trans_att trans_add_global trans_del_global, trans_att trans_add_local trans_del_local);
wenzelm@6778
   112
wenzelm@6778
   113
wenzelm@6778
   114
(** proof commands **)
wenzelm@6778
   115
wenzelm@6778
   116
val calculationN = "calculation";
wenzelm@6778
   117
wenzelm@6778
   118
fun calculate final prt state =
wenzelm@6778
   119
  let
wenzelm@6778
   120
    val fact = Proof.the_fact state;
wenzelm@6778
   121
    val dddot = ObjectLogic.dest_main_statement (#prop (Thm.rep_thm fact)) handle TERM _ =>
wenzelm@6778
   122
      raise Proof.STATE ("Unable to bind '...' pattern from current fact", state);
wenzelm@6778
   123
wenzelm@6778
   124
    val name = if final then "" else calculationN;
wenzelm@6778
   125
    fun have_thm thm = Proof.have_thmss name [] [([thm], [])];
wenzelm@6778
   126
wenzelm@6778
   127
    val rules = Seq.of_list (get_local_rules state);
wenzelm@6778
   128
    val calculations =
wenzelm@6778
   129
      (case get_calculation state of
wenzelm@6778
   130
        None => Seq.single fact
wenzelm@6778
   131
      | Some thm => Seq.flat (Seq.map (Method.multi_resolve [thm, fact]) rules));
wenzelm@6778
   132
  in
wenzelm@6778
   133
    calculations
wenzelm@6778
   134
    |> Seq.map (fn calc =>
wenzelm@6778
   135
      (prt calc;
wenzelm@6778
   136
        state
wenzelm@6778
   137
        |> Proof.bind_i [(Syntax.dddot_indexname, dddot)]
wenzelm@6781
   138
        |> put_calculation calc
wenzelm@6778
   139
        |> have_thm calc
wenzelm@6781
   140
        |> (if final then Proof.chain else Proof.reset_facts)))
wenzelm@6778
   141
  end;
wenzelm@6778
   142
wenzelm@6781
   143
fun also prt = calculate false prt;
wenzelm@6781
   144
fun finally prt = calculate true prt;
wenzelm@6778
   145
wenzelm@6778
   146
wenzelm@6778
   147
wenzelm@6778
   148
(** theory setup **)
wenzelm@6778
   149
wenzelm@6778
   150
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@6778
   151
  Attrib.add_attributes [(transN, trans_attr, "transitivity rule")]];
wenzelm@6778
   152
wenzelm@6778
   153
wenzelm@6778
   154
end;