src/Pure/Isar/calculation.ML
author wenzelm
Thu, 07 Sep 2000 20:56:58 +0200
changeset 9900 8035a13c61a0
parent 9322 b5bd2709a2c2
child 10008 61eb9f3aa92a
permissions -rw-r--r--
tuned msgs;
wenzelm@6778
     1
(*  Title:      Pure/Isar/calculation.ML
wenzelm@6778
     2
    ID:         $Id$
wenzelm@6778
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6778
     5
wenzelm@6778
     6
Support for calculational proofs.
wenzelm@6778
     7
*)
wenzelm@6778
     8
wenzelm@6778
     9
signature CALCULATION =
wenzelm@6778
    10
sig
wenzelm@6778
    11
  val print_global_rules: theory -> unit
wenzelm@6778
    12
  val print_local_rules: Proof.context -> unit
wenzelm@6778
    13
  val trans_add_global: theory attribute
wenzelm@6778
    14
  val trans_del_global: theory attribute
wenzelm@6778
    15
  val trans_add_local: Proof.context attribute
wenzelm@6778
    16
  val trans_del_local: Proof.context attribute
wenzelm@7414
    17
  val also: thm list option -> (thm list -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@7414
    18
  val finally: thm list option -> (thm list -> unit) -> Proof.state -> Proof.state Seq.seq
wenzelm@8562
    19
  val moreover: (thm list -> unit) -> Proof.state -> Proof.state
wenzelm@8588
    20
  val ultimately: (thm list -> unit) -> Proof.state -> Proof.state
wenzelm@6778
    21
  val setup: (theory -> theory) list
wenzelm@6778
    22
end;
wenzelm@6778
    23
wenzelm@6778
    24
structure Calculation: CALCULATION =
wenzelm@6778
    25
struct
wenzelm@6778
    26
wenzelm@6778
    27
(** global and local calculation data **)
wenzelm@6778
    28
wenzelm@8300
    29
(* theory data kind 'Isar/calculation' *)
wenzelm@6778
    30
wenzelm@9217
    31
fun print_rules rs = Pretty.writeln (Pretty.big_list "transitivity rules:"
wenzelm@8300
    32
  (map Display.pretty_thm (NetRules.rules rs)));
wenzelm@6778
    33
wenzelm@6778
    34
structure GlobalCalculationArgs =
wenzelm@6778
    35
struct
wenzelm@6778
    36
  val name = "Isar/calculation";
wenzelm@8300
    37
  type T = thm NetRules.T
wenzelm@6778
    38
wenzelm@8300
    39
  val empty = NetRules.init_elim;
wenzelm@6778
    40
  val copy = I;
wenzelm@6778
    41
  val prep_ext = I;
wenzelm@8300
    42
  val merge = NetRules.merge;
wenzelm@6778
    43
  fun print _ = print_rules;
wenzelm@6778
    44
end;
wenzelm@6778
    45
wenzelm@6778
    46
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
wenzelm@6778
    47
val print_global_rules = GlobalCalculation.print;
wenzelm@6778
    48
wenzelm@6778
    49
wenzelm@6778
    50
(* proof data kind 'Isar/calculation' *)
wenzelm@6778
    51
wenzelm@6778
    52
structure LocalCalculationArgs =
wenzelm@6778
    53
struct
wenzelm@6778
    54
  val name = "Isar/calculation";
wenzelm@8300
    55
  type T = thm NetRules.T * (thm list * int) option;
wenzelm@6778
    56
wenzelm@6778
    57
  fun init thy = (GlobalCalculation.get thy, None);
wenzelm@8300
    58
  fun print _ (rs, _) = print_rules rs;
wenzelm@6778
    59
end;
wenzelm@6778
    60
wenzelm@6778
    61
structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
wenzelm@6787
    62
val get_local_rules = #1 o LocalCalculation.get_st;
wenzelm@6778
    63
val print_local_rules = LocalCalculation.print;
wenzelm@6778
    64
wenzelm@6778
    65
wenzelm@6778
    66
(* access calculation *)
wenzelm@6778
    67
wenzelm@6778
    68
fun get_calculation state =
wenzelm@6787
    69
  (case #2 (LocalCalculation.get_st state) of
wenzelm@6778
    70
    None => None
wenzelm@7414
    71
  | Some (thms, lev) => if lev = Proof.level state then Some thms else None);
wenzelm@6778
    72
wenzelm@7414
    73
fun put_calculation thms state =
wenzelm@7414
    74
  LocalCalculation.put_st (get_local_rules state, Some (thms, Proof.level state)) state;
wenzelm@6778
    75
wenzelm@6787
    76
fun reset_calculation state =
wenzelm@6787
    77
  LocalCalculation.put_st (get_local_rules state, None) state;
wenzelm@6787
    78
wenzelm@6778
    79
wenzelm@6778
    80
wenzelm@6778
    81
(** attributes **)
wenzelm@6778
    82
wenzelm@6778
    83
(* trans add/del *)
wenzelm@6778
    84
wenzelm@6778
    85
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    86
wenzelm@8300
    87
val trans_add_global = mk_att GlobalCalculation.map NetRules.insert;
wenzelm@8300
    88
val trans_del_global = mk_att GlobalCalculation.map NetRules.delete;
wenzelm@8300
    89
val trans_add_local = mk_att LocalCalculation.map (Library.apfst o NetRules.insert);
wenzelm@8300
    90
val trans_del_local = mk_att LocalCalculation.map (Library.apfst o NetRules.delete);
wenzelm@6778
    91
wenzelm@6778
    92
wenzelm@6778
    93
(* concrete syntax *)
wenzelm@6778
    94
wenzelm@6778
    95
val trans_attr =
wenzelm@8634
    96
 (Attrib.add_del_args trans_add_global trans_del_global,
wenzelm@8634
    97
  Attrib.add_del_args trans_add_local trans_del_local);
wenzelm@6778
    98
wenzelm@6778
    99
wenzelm@6787
   100
wenzelm@6778
   101
(** proof commands **)
wenzelm@6778
   102
wenzelm@8588
   103
(* maintain calculation register *)
wenzelm@8562
   104
wenzelm@6778
   105
val calculationN = "calculation";
wenzelm@6778
   106
wenzelm@8588
   107
fun maintain_calculation false calc state =
wenzelm@8588
   108
      state
wenzelm@8588
   109
      |> put_calculation calc
wenzelm@8588
   110
      |> Proof.simple_have_thms calculationN calc
wenzelm@8588
   111
      |> Proof.reset_facts
wenzelm@8588
   112
  | maintain_calculation true calc state =
wenzelm@8588
   113
      state
wenzelm@8588
   114
      |> reset_calculation
wenzelm@8588
   115
      |> Proof.reset_thms calculationN
wenzelm@8588
   116
      |> Proof.simple_have_thms "" calc
wenzelm@8588
   117
      |> Proof.chain;
wenzelm@8562
   118
wenzelm@8562
   119
wenzelm@8562
   120
(* 'also' and 'finally' *)
wenzelm@8562
   121
wenzelm@8588
   122
fun err_if state b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@8588
   123
wenzelm@6877
   124
fun calculate final opt_rules print state =
wenzelm@6778
   125
  let
wenzelm@7414
   126
    val facts = Proof.the_facts state;
wenzelm@7414
   127
wenzelm@9322
   128
    val strip_assums_concl = Logic.strip_assums_concl o #prop o Thm.rep_thm;
wenzelm@9322
   129
    val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl);
wenzelm@7554
   130
    fun differ thms thms' = not (Library.equal_lists eq_prop (thms, thms'));
wenzelm@8300
   131
wenzelm@7414
   132
    fun combine thms =
wenzelm@8300
   133
      let
wenzelm@8300
   134
        val ths = thms @ facts;
wenzelm@8649
   135
        val rs = get_local_rules state;
wenzelm@8300
   136
        val rules =
wenzelm@8300
   137
          (case ths of [] => NetRules.rules rs
wenzelm@9322
   138
          | th :: _ => NetRules.may_unify rs (strip_assums_concl th));
wenzelm@8649
   139
        val ruleq = Seq.of_list (if_none opt_rules [] @ rules);
wenzelm@8300
   140
      in Seq.map Library.single (Seq.flat (Seq.map (Method.multi_resolve ths) ruleq)) end;
wenzelm@7414
   141
wenzelm@6903
   142
    val (initial, calculations) =
wenzelm@6778
   143
      (case get_calculation state of
wenzelm@7414
   144
        None => (true, Seq.single facts)
wenzelm@7414
   145
      | Some thms => (false, Seq.filter (differ thms) (combine thms)))
wenzelm@6778
   146
  in
wenzelm@8588
   147
    err_if state (initial andalso final) "No calculation yet";
wenzelm@8588
   148
    err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@8588
   149
    calculations |> Seq.map (fn calc => (print calc; state |> maintain_calculation final calc))
wenzelm@6778
   150
  end;
wenzelm@6778
   151
wenzelm@6782
   152
fun also print = calculate false print;
wenzelm@6782
   153
fun finally print = calculate true print;
wenzelm@6778
   154
wenzelm@6778
   155
wenzelm@8588
   156
(* 'moreover' and 'ultimately' *)
wenzelm@8562
   157
wenzelm@8588
   158
fun collect final print state =
wenzelm@8588
   159
  let
wenzelm@8588
   160
    val facts = Proof.the_facts state;
wenzelm@8588
   161
    val (initial, thms) =
wenzelm@8588
   162
      (case get_calculation state of
wenzelm@8588
   163
        None => (true, [])
wenzelm@8588
   164
      | Some thms => (false, thms));
wenzelm@8588
   165
    val calc = thms @ facts;
wenzelm@8588
   166
  in
wenzelm@8588
   167
    err_if state (initial andalso final) "No calculation yet";
wenzelm@8588
   168
    print calc;
wenzelm@8588
   169
    state |> maintain_calculation final calc
wenzelm@8588
   170
  end;
wenzelm@8588
   171
wenzelm@8588
   172
fun moreover print = collect false print;
wenzelm@8588
   173
fun ultimately print = collect true print;
wenzelm@8562
   174
wenzelm@8562
   175
wenzelm@6778
   176
wenzelm@6778
   177
(** theory setup **)
wenzelm@6778
   178
wenzelm@6778
   179
val setup = [GlobalCalculation.init, LocalCalculation.init,
wenzelm@9900
   180
  Attrib.add_attributes [("trans", trans_attr, "declaration of transitivity rule")]];
wenzelm@6778
   181
wenzelm@6778
   182
wenzelm@6778
   183
end;