src/Pure/Isar/calculation.ML
author wenzelm
Thu Mar 23 21:37:13 2000 +0100 (2000-03-23)
changeset 8562 ce0e2b8e8844
parent 8461 2693a3a9fcc1
child 8588 b7c3f264f8ac
permissions -rw-r--r--
added 'moreover' command;
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@8562
    18
  val moreover: (thm list -> unit) -> Proof.state -> Proof.state
wenzelm@6778
    19
  val setup: (theory -> theory) list
wenzelm@6778
    20
end;
wenzelm@6778
    21
wenzelm@6778
    22
structure Calculation: CALCULATION =
wenzelm@6778
    23
struct
wenzelm@6778
    24
wenzelm@6778
    25
(** global and local calculation data **)
wenzelm@6778
    26
wenzelm@8300
    27
(* theory data kind 'Isar/calculation' *)
wenzelm@6778
    28
wenzelm@8300
    29
fun print_rules rs = Pretty.writeln (Pretty.big_list "calculation rules:"
wenzelm@8300
    30
  (map Display.pretty_thm (NetRules.rules rs)));
wenzelm@6778
    31
wenzelm@6778
    32
structure GlobalCalculationArgs =
wenzelm@6778
    33
struct
wenzelm@6778
    34
  val name = "Isar/calculation";
wenzelm@8300
    35
  type T = thm NetRules.T
wenzelm@6778
    36
wenzelm@8300
    37
  val empty = NetRules.init_elim;
wenzelm@6778
    38
  val copy = I;
wenzelm@6778
    39
  val prep_ext = I;
wenzelm@8300
    40
  val merge = NetRules.merge;
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@8300
    53
  type T = thm NetRules.T * (thm list * int) option;
wenzelm@6778
    54
wenzelm@6778
    55
  fun init thy = (GlobalCalculation.get thy, None);
wenzelm@8300
    56
  fun print _ (rs, _) = print_rules rs;
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@7414
    69
  | Some (thms, lev) => if lev = Proof.level state then Some thms else None);
wenzelm@6778
    70
wenzelm@7414
    71
fun put_calculation thms state =
wenzelm@7414
    72
  LocalCalculation.put_st (get_local_rules state, Some (thms, 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
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    84
wenzelm@8300
    85
val trans_add_global = mk_att GlobalCalculation.map NetRules.insert;
wenzelm@8300
    86
val trans_del_global = mk_att GlobalCalculation.map NetRules.delete;
wenzelm@8300
    87
val trans_add_local = mk_att LocalCalculation.map (Library.apfst o NetRules.insert);
wenzelm@8300
    88
val trans_del_local = mk_att LocalCalculation.map (Library.apfst o NetRules.delete);
wenzelm@6778
    89
wenzelm@6778
    90
wenzelm@6778
    91
(* concrete syntax *)
wenzelm@6778
    92
wenzelm@6778
    93
val transN = "trans";
wenzelm@6778
    94
val addN = "add";
wenzelm@6778
    95
val delN = "del";
wenzelm@6778
    96
wenzelm@6778
    97
fun trans_att add del =
wenzelm@6778
    98
  Attrib.syntax (Scan.lift (Args.$$$ addN >> K add || Args.$$$ delN >> K del || Scan.succeed add));
wenzelm@6778
    99
wenzelm@6778
   100
val trans_attr =
wenzelm@6778
   101
  (trans_att trans_add_global trans_del_global, trans_att trans_add_local trans_del_local);
wenzelm@6778
   102
wenzelm@6778
   103
wenzelm@6787
   104
wenzelm@6778
   105
(** proof commands **)
wenzelm@6778
   106
wenzelm@8562
   107
(* maintain calculation *)
wenzelm@8562
   108
wenzelm@6778
   109
val calculationN = "calculation";
wenzelm@6778
   110
wenzelm@8562
   111
fun maintain_calculation calc state =
wenzelm@8562
   112
  state
wenzelm@8562
   113
  |> put_calculation calc
wenzelm@8562
   114
  |> Proof.simple_have_thms calculationN calc
wenzelm@8562
   115
  |> Proof.reset_facts;
wenzelm@8562
   116
wenzelm@8562
   117
wenzelm@8562
   118
(* 'also' and 'finally' *)
wenzelm@8562
   119
wenzelm@6877
   120
fun calculate final opt_rules print state =
wenzelm@6778
   121
  let
wenzelm@6903
   122
    fun err_if b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@7414
   123
    val facts = Proof.the_facts state;
wenzelm@7414
   124
wenzelm@7554
   125
    val eq_prop = op aconv o pairself (#prop o Thm.rep_thm);
wenzelm@7554
   126
    fun differ thms thms' = not (Library.equal_lists eq_prop (thms, thms'));
wenzelm@8300
   127
wenzelm@7414
   128
    fun combine thms =
wenzelm@8300
   129
      let
wenzelm@8300
   130
        val ths = thms @ facts;
wenzelm@8300
   131
        val rs = NetRules.inserts (if_none opt_rules []) (get_local_rules state);
wenzelm@8300
   132
        val rules =
wenzelm@8300
   133
          (case ths of [] => NetRules.rules rs
wenzelm@8300
   134
          | th :: _ => NetRules.may_unify rs (Logic.strip_assums_concl (#prop (Thm.rep_thm th))));
wenzelm@8300
   135
        val ruleq = Seq.of_list rules;
wenzelm@8300
   136
      in Seq.map Library.single (Seq.flat (Seq.map (Method.multi_resolve ths) ruleq)) end;
wenzelm@7414
   137
wenzelm@6903
   138
    val (initial, calculations) =
wenzelm@6778
   139
      (case get_calculation state of
wenzelm@7414
   140
        None => (true, Seq.single facts)
wenzelm@7414
   141
      | Some thms => (false, Seq.filter (differ thms) (combine thms)))
wenzelm@6778
   142
  in
wenzelm@6903
   143
    err_if (initial andalso final) "No calculation yet";
wenzelm@6903
   144
    err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@6782
   145
    calculations |> Seq.map (fn calc =>
wenzelm@6782
   146
      (print calc;
wenzelm@6787
   147
        (if final then
wenzelm@6787
   148
          state
wenzelm@6787
   149
          |> reset_calculation
wenzelm@7600
   150
          |> Proof.reset_thms calculationN
wenzelm@7414
   151
          |> Proof.simple_have_thms "" calc
wenzelm@6787
   152
          |> Proof.chain
wenzelm@6787
   153
        else
wenzelm@6787
   154
          state
wenzelm@8562
   155
          |> maintain_calculation calc)))
wenzelm@6778
   156
  end;
wenzelm@6778
   157
wenzelm@6782
   158
fun also print = calculate false print;
wenzelm@6782
   159
fun finally print = calculate true print;
wenzelm@6778
   160
wenzelm@6778
   161
wenzelm@8562
   162
(* 'moreover' *)
wenzelm@8562
   163
wenzelm@8562
   164
fun moreover print state =
wenzelm@8562
   165
  let val calc = if_none (get_calculation state) [] @ Proof.the_facts state
wenzelm@8562
   166
  in print calc; state |> maintain_calculation calc end;
wenzelm@8562
   167
wenzelm@8562
   168
wenzelm@6778
   169
wenzelm@6778
   170
(** theory setup **)
wenzelm@6778
   171
wenzelm@6778
   172
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@8461
   173
  Attrib.add_attributes [(transN, trans_attr, "declare transitivity rule")]];
wenzelm@6778
   174
wenzelm@6778
   175
wenzelm@6778
   176
end;