src/Pure/Isar/calculation.ML
author wenzelm
Mon Nov 05 20:59:35 2001 +0100 (2001-11-05)
changeset 12055 a9c44895cc8c
parent 12021 8809efda06d3
child 12123 739eba13e2cd
permissions -rw-r--r--
pretty/print functions with context;
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@12055
    17
  val also: thm list option -> (Proof.context -> thm list -> unit)
wenzelm@12055
    18
    -> Proof.state -> Proof.state Seq.seq
wenzelm@12055
    19
  val finally: thm list option -> (Proof.context -> thm list -> unit)
wenzelm@12055
    20
    -> Proof.state -> Proof.state Seq.seq
wenzelm@12055
    21
  val moreover: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
wenzelm@12055
    22
  val ultimately: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
wenzelm@6778
    23
  val setup: (theory -> theory) list
wenzelm@6778
    24
end;
wenzelm@6778
    25
wenzelm@6778
    26
structure Calculation: CALCULATION =
wenzelm@6778
    27
struct
wenzelm@6778
    28
wenzelm@6778
    29
(** global and local calculation data **)
wenzelm@6778
    30
wenzelm@8300
    31
(* theory data kind 'Isar/calculation' *)
wenzelm@6778
    32
wenzelm@12055
    33
fun print_rules prt x rs =
wenzelm@12055
    34
  Pretty.writeln (Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules rs)));
wenzelm@6778
    35
wenzelm@6778
    36
structure GlobalCalculationArgs =
wenzelm@6778
    37
struct
wenzelm@6778
    38
  val name = "Isar/calculation";
wenzelm@8300
    39
  type T = thm NetRules.T
wenzelm@6778
    40
wenzelm@11784
    41
  val empty = NetRules.elim;
wenzelm@6778
    42
  val copy = I;
wenzelm@6778
    43
  val prep_ext = I;
wenzelm@8300
    44
  val merge = NetRules.merge;
wenzelm@12055
    45
  val print = print_rules Display.pretty_thm_sg;
wenzelm@6778
    46
end;
wenzelm@6778
    47
wenzelm@6778
    48
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
wenzelm@6778
    49
val print_global_rules = GlobalCalculation.print;
wenzelm@6778
    50
wenzelm@6778
    51
wenzelm@6778
    52
(* proof data kind 'Isar/calculation' *)
wenzelm@6778
    53
wenzelm@6778
    54
structure LocalCalculationArgs =
wenzelm@6778
    55
struct
wenzelm@6778
    56
  val name = "Isar/calculation";
wenzelm@8300
    57
  type T = thm NetRules.T * (thm list * int) option;
wenzelm@6778
    58
wenzelm@6778
    59
  fun init thy = (GlobalCalculation.get thy, None);
wenzelm@12055
    60
  fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs;
wenzelm@6778
    61
end;
wenzelm@6778
    62
wenzelm@6778
    63
structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
wenzelm@6787
    64
val get_local_rules = #1 o LocalCalculation.get_st;
wenzelm@6778
    65
val print_local_rules = LocalCalculation.print;
wenzelm@6778
    66
wenzelm@6778
    67
wenzelm@6778
    68
(* access calculation *)
wenzelm@6778
    69
wenzelm@6778
    70
fun get_calculation state =
wenzelm@6787
    71
  (case #2 (LocalCalculation.get_st state) of
wenzelm@6778
    72
    None => None
wenzelm@7414
    73
  | Some (thms, lev) => if lev = Proof.level state then Some thms else None);
wenzelm@6778
    74
wenzelm@7414
    75
fun put_calculation thms state =
wenzelm@7414
    76
  LocalCalculation.put_st (get_local_rules state, Some (thms, Proof.level state)) state;
wenzelm@6778
    77
wenzelm@6787
    78
fun reset_calculation state =
wenzelm@6787
    79
  LocalCalculation.put_st (get_local_rules state, None) state;
wenzelm@6787
    80
wenzelm@6778
    81
wenzelm@6778
    82
wenzelm@6778
    83
(** attributes **)
wenzelm@6778
    84
wenzelm@6778
    85
(* trans add/del *)
wenzelm@6778
    86
wenzelm@6778
    87
fun mk_att f g (x, thm) = (f (g thm) x, thm);
wenzelm@6778
    88
wenzelm@8300
    89
val trans_add_global = mk_att GlobalCalculation.map NetRules.insert;
wenzelm@8300
    90
val trans_del_global = mk_att GlobalCalculation.map NetRules.delete;
wenzelm@8300
    91
val trans_add_local = mk_att LocalCalculation.map (Library.apfst o NetRules.insert);
wenzelm@8300
    92
val trans_del_local = mk_att LocalCalculation.map (Library.apfst o NetRules.delete);
wenzelm@6778
    93
wenzelm@6778
    94
wenzelm@6778
    95
(* concrete syntax *)
wenzelm@6778
    96
wenzelm@6778
    97
val trans_attr =
wenzelm@8634
    98
 (Attrib.add_del_args trans_add_global trans_del_global,
wenzelm@8634
    99
  Attrib.add_del_args trans_add_local trans_del_local);
wenzelm@6778
   100
wenzelm@6778
   101
wenzelm@6787
   102
wenzelm@6778
   103
(** proof commands **)
wenzelm@6778
   104
wenzelm@8588
   105
(* maintain calculation register *)
wenzelm@8562
   106
wenzelm@6778
   107
val calculationN = "calculation";
wenzelm@6778
   108
wenzelm@8588
   109
fun maintain_calculation false calc state =
wenzelm@8588
   110
      state
wenzelm@8588
   111
      |> put_calculation calc
wenzelm@8588
   112
      |> Proof.simple_have_thms calculationN calc
wenzelm@8588
   113
      |> Proof.reset_facts
wenzelm@8588
   114
  | maintain_calculation true calc state =
wenzelm@8588
   115
      state
wenzelm@8588
   116
      |> reset_calculation
wenzelm@8588
   117
      |> Proof.reset_thms calculationN
wenzelm@8588
   118
      |> Proof.simple_have_thms "" calc
wenzelm@8588
   119
      |> Proof.chain;
wenzelm@8562
   120
wenzelm@8562
   121
wenzelm@8562
   122
(* 'also' and 'finally' *)
wenzelm@8562
   123
wenzelm@8588
   124
fun err_if state b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@8588
   125
wenzelm@6877
   126
fun calculate final opt_rules print state =
wenzelm@6778
   127
  let
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@11097
   130
    fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
wenzelm@8300
   131
wenzelm@11097
   132
    fun combine ths =
wenzelm@11097
   133
      (case opt_rules of Some rules => rules
wenzelm@11097
   134
      | None =>
wenzelm@11097
   135
          (case ths of [] => NetRules.rules (get_local_rules state)
wenzelm@11784
   136
          | th :: _ => NetRules.retrieve (get_local_rules state) (strip_assums_concl th)))
wenzelm@11097
   137
      |> Seq.of_list |> Seq.map (Method.multi_resolve ths) |> Seq.flat
wenzelm@11097
   138
      |> Seq.filter (not o projection ths);
wenzelm@7414
   139
wenzelm@11097
   140
    val facts = Proof.the_facts (Proof.assert_forward state);
wenzelm@6903
   141
    val (initial, calculations) =
wenzelm@6778
   142
      (case get_calculation state of
wenzelm@7414
   143
        None => (true, Seq.single facts)
wenzelm@11097
   144
      | Some calc => (false, Seq.map single (combine (calc @ facts))));
wenzelm@6778
   145
  in
wenzelm@8588
   146
    err_if state (initial andalso final) "No calculation yet";
wenzelm@8588
   147
    err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@12055
   148
    calculations |> Seq.map (fn calc => (print (Proof.context_of state) calc;
wenzelm@12055
   149
        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@10478
   160
    val facts = Proof.the_facts (Proof.assert_forward 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@12055
   168
    print (Proof.context_of state) 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@12021
   180
  Attrib.add_attributes [("trans", trans_attr, "declaration of transitivity rule")],
wenzelm@12021
   181
  #1 o PureThy.add_thms [(("", transitive_thm), [trans_add_global])]];
wenzelm@6778
   182
wenzelm@6778
   183
wenzelm@6778
   184
end;