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