src/Pure/Isar/calculation.ML
author haftmann
Fri Dec 09 09:06:45 2005 +0100 (2005-12-09)
changeset 18377 0e1d025d57b3
parent 18223 20830cb4428c
child 18637 33a6f6caa617
permissions -rw-r--r--
oriented result pairs in PureThy
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@17350
     5
Generic calculational proofs.
wenzelm@6778
     6
*)
wenzelm@6778
     7
wenzelm@6778
     8
signature CALCULATION =
wenzelm@6778
     9
sig
wenzelm@16571
    10
  val get_calculation: Proof.state -> thm list option
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@12379
    17
  val sym_add_global: theory attribute
wenzelm@12379
    18
  val sym_del_global: theory attribute
wenzelm@12379
    19
  val sym_add_local: Proof.context attribute
wenzelm@12379
    20
  val sym_del_local: Proof.context attribute
wenzelm@12379
    21
  val symmetric_global: theory attribute
wenzelm@12379
    22
  val symmetric_local: Proof.context attribute
wenzelm@17350
    23
  val also: (thmref * Attrib.src list) list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@17350
    24
  val also_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@17350
    25
  val finally: (thmref * Attrib.src list) list option -> bool ->
wenzelm@17350
    26
    Proof.state -> Proof.state Seq.seq
wenzelm@17350
    27
  val finally_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@17350
    28
  val moreover: bool -> Proof.state -> Proof.state
wenzelm@17350
    29
  val ultimately: bool -> Proof.state -> Proof.state
wenzelm@6778
    30
end;
wenzelm@6778
    31
wenzelm@6778
    32
structure Calculation: CALCULATION =
wenzelm@6778
    33
struct
wenzelm@6778
    34
wenzelm@6778
    35
(** global and local calculation data **)
wenzelm@6778
    36
wenzelm@16424
    37
(* global calculation *)
wenzelm@6778
    38
wenzelm@12379
    39
fun print_rules prt x (trans, sym) =
wenzelm@12379
    40
  [Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules trans)),
wenzelm@12379
    41
    Pretty.big_list "symmetry rules:" (map (prt x) sym)]
wenzelm@12379
    42
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@6778
    43
wenzelm@16424
    44
structure GlobalCalculation = TheoryDataFun
wenzelm@16424
    45
(struct
wenzelm@6778
    46
  val name = "Isar/calculation";
wenzelm@12379
    47
  type T = thm NetRules.T * thm list
wenzelm@6778
    48
wenzelm@12379
    49
  val empty = (NetRules.elim, []);
wenzelm@6778
    50
  val copy = I;
wenzelm@16424
    51
  val extend = I;
wenzelm@16424
    52
  fun merge _ ((trans1, sym1), (trans2, sym2)) =
wenzelm@12379
    53
    (NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2));
wenzelm@12055
    54
  val print = print_rules Display.pretty_thm_sg;
wenzelm@16424
    55
end);
wenzelm@6778
    56
wenzelm@15801
    57
val _ = Context.add_setup [GlobalCalculation.init];
wenzelm@6778
    58
val print_global_rules = GlobalCalculation.print;
wenzelm@6778
    59
wenzelm@6778
    60
wenzelm@16424
    61
(* local calculation *)
wenzelm@6778
    62
wenzelm@16424
    63
structure LocalCalculation = ProofDataFun
wenzelm@16424
    64
(struct
wenzelm@6778
    65
  val name = "Isar/calculation";
wenzelm@12379
    66
  type T = (thm NetRules.T * thm list) * (thm list * int) option;
wenzelm@6778
    67
skalberg@15531
    68
  fun init thy = (GlobalCalculation.get thy, NONE);
wenzelm@12055
    69
  fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs;
wenzelm@16424
    70
end);
wenzelm@6778
    71
wenzelm@15801
    72
val _ = Context.add_setup [LocalCalculation.init];
wenzelm@13371
    73
val get_local_rules = #1 o LocalCalculation.get o Proof.context_of;
wenzelm@6778
    74
val print_local_rules = LocalCalculation.print;
wenzelm@6778
    75
wenzelm@6778
    76
wenzelm@6778
    77
(* access calculation *)
wenzelm@6778
    78
wenzelm@6778
    79
fun get_calculation state =
wenzelm@13371
    80
  (case #2 (LocalCalculation.get (Proof.context_of state)) of
skalberg@15531
    81
    NONE => NONE
skalberg@15531
    82
  | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
wenzelm@6778
    83
wenzelm@17350
    84
val calculationN = "calculation";
wenzelm@6778
    85
wenzelm@17350
    86
fun put_calculation calc =
wenzelm@17350
    87
  `Proof.level #-> (fn lev => Proof.map_context
wenzelm@17350
    88
    (LocalCalculation.map (apsnd (K (Option.map (rpair lev) calc)))))
wenzelm@17350
    89
  #> Proof.put_thms (calculationN, calc);
wenzelm@6778
    90
wenzelm@6778
    91
wenzelm@6778
    92
(** attributes **)
wenzelm@6778
    93
wenzelm@12379
    94
(* add/del rules *)
wenzelm@12379
    95
wenzelm@12379
    96
fun global_att f (x, thm) = (GlobalCalculation.map (f thm) x, thm);
wenzelm@12379
    97
fun local_att f (x, thm) = (LocalCalculation.map (apfst (f thm)) x, thm);
wenzelm@6778
    98
wenzelm@12379
    99
val trans_add_global = global_att (apfst o NetRules.insert);
wenzelm@12379
   100
val trans_del_global = global_att (apfst o NetRules.delete);
wenzelm@12379
   101
val trans_add_local = local_att (apfst o NetRules.insert);
wenzelm@12379
   102
val trans_del_local = local_att (apfst o NetRules.delete);
wenzelm@6778
   103
skalberg@15531
   104
val sym_add_global = global_att (apsnd o Drule.add_rule) o ContextRules.elim_query_global NONE;
wenzelm@12402
   105
val sym_del_global = global_att (apsnd o Drule.del_rule) o ContextRules.rule_del_global;
skalberg@15531
   106
val sym_add_local = local_att (apsnd o Drule.add_rule) o ContextRules.elim_query_local NONE;
wenzelm@12402
   107
val sym_del_local = local_att (apsnd o Drule.del_rule) o ContextRules.rule_del_local;
wenzelm@12379
   108
wenzelm@12379
   109
wenzelm@12379
   110
(* symmetry *)
wenzelm@12379
   111
wenzelm@12379
   112
fun gen_symmetric get_sym = Drule.rule_attribute (fn x => fn th =>
wenzelm@18223
   113
  (case Seq.chop (2, Drule.multi_resolves [th] (get_sym x)) of
wenzelm@12379
   114
    ([th'], _) => th'
wenzelm@12379
   115
  | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
wenzelm@12379
   116
  | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
wenzelm@12379
   117
wenzelm@12379
   118
val symmetric_global = gen_symmetric (#2 o GlobalCalculation.get);
wenzelm@12379
   119
val symmetric_local = gen_symmetric (#2 o #1 o LocalCalculation.get);
wenzelm@6778
   120
wenzelm@6778
   121
wenzelm@6778
   122
(* concrete syntax *)
wenzelm@6778
   123
wenzelm@6778
   124
val trans_attr =
wenzelm@8634
   125
 (Attrib.add_del_args trans_add_global trans_del_global,
wenzelm@8634
   126
  Attrib.add_del_args trans_add_local trans_del_local);
wenzelm@6778
   127
wenzelm@12379
   128
val sym_attr =
wenzelm@12379
   129
 (Attrib.add_del_args sym_add_global sym_del_global,
wenzelm@12379
   130
  Attrib.add_del_args sym_add_local sym_del_local);
wenzelm@12379
   131
wenzelm@15801
   132
val _ = Context.add_setup
wenzelm@15801
   133
 [Attrib.add_attributes
wenzelm@15801
   134
   [("trans", trans_attr, "declaration of transitivity rule"),
wenzelm@15801
   135
    ("sym", sym_attr, "declaration of symmetry rule"),
wenzelm@15801
   136
    ("symmetric", (Attrib.no_args symmetric_global, Attrib.no_args symmetric_local),
wenzelm@15801
   137
      "resolution with symmetry rule")],
haftmann@18377
   138
  snd o PureThy.add_thms
wenzelm@15801
   139
   [(("", transitive_thm), [trans_add_global]),
wenzelm@15801
   140
    (("", symmetric_thm), [sym_add_global])]];
wenzelm@15801
   141
wenzelm@6778
   142
wenzelm@6787
   143
wenzelm@6778
   144
(** proof commands **)
wenzelm@6778
   145
wenzelm@17350
   146
fun err_if state b msg = if b then raise Proof.STATE (msg, state) else ();
wenzelm@17350
   147
wenzelm@14555
   148
fun assert_sane final =
wenzelm@14555
   149
  if final then Proof.assert_forward else Proof.assert_forward_or_chain;
wenzelm@14555
   150
wenzelm@17350
   151
fun maintain_calculation false calc = put_calculation (SOME calc)
wenzelm@17350
   152
  | maintain_calculation true calc = put_calculation NONE #> Proof.chain_facts calc;
wenzelm@6778
   153
wenzelm@17350
   154
fun print_calculation false _ _ = ()
wenzelm@17350
   155
  | print_calculation true ctxt calc =
wenzelm@17350
   156
      Pretty.writeln (ProofContext.pretty_fact ctxt (calculationN, calc));
wenzelm@8562
   157
wenzelm@8562
   158
wenzelm@17350
   159
(* also and finally *)
wenzelm@8562
   160
wenzelm@17350
   161
fun calculate prep_rules final raw_rules int state =
wenzelm@6778
   162
  let
wenzelm@12805
   163
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
wenzelm@9322
   164
    val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl);
wenzelm@11097
   165
    fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
wenzelm@8300
   166
wenzelm@17350
   167
    val opt_rules = Option.map (prep_rules state) raw_rules;
wenzelm@11097
   168
    fun combine ths =
skalberg@15531
   169
      (case opt_rules of SOME rules => rules
skalberg@15531
   170
      | NONE =>
wenzelm@12379
   171
          (case ths of [] => NetRules.rules (#1 (get_local_rules state))
wenzelm@12379
   172
          | th :: _ => NetRules.retrieve (#1 (get_local_rules state)) (strip_assums_concl th)))
wenzelm@18223
   173
      |> Seq.of_list |> Seq.maps (Drule.multi_resolve ths)
wenzelm@11097
   174
      |> Seq.filter (not o projection ths);
wenzelm@7414
   175
wenzelm@14555
   176
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@6903
   177
    val (initial, calculations) =
wenzelm@6778
   178
      (case get_calculation state of
skalberg@15531
   179
        NONE => (true, Seq.single facts)
skalberg@15531
   180
      | SOME calc => (false, Seq.map single (combine (calc @ facts))));
wenzelm@6778
   181
  in
wenzelm@8588
   182
    err_if state (initial andalso final) "No calculation yet";
wenzelm@15973
   183
    err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@17350
   184
    calculations |> Seq.map (fn calc => (print_calculation int (Proof.context_of state) calc;
wenzelm@12055
   185
        state |> maintain_calculation final calc))
wenzelm@6778
   186
  end;
wenzelm@6778
   187
wenzelm@17350
   188
val also = calculate Proof.get_thmss false;
wenzelm@17350
   189
val also_i = calculate (K I) false;
wenzelm@17350
   190
val finally = calculate Proof.get_thmss true;
wenzelm@17350
   191
val finally_i = calculate (K I) true;
wenzelm@6778
   192
wenzelm@6778
   193
wenzelm@17350
   194
(* moreover and ultimately *)
wenzelm@8562
   195
wenzelm@17350
   196
fun collect final int state =
wenzelm@8588
   197
  let
wenzelm@14555
   198
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@8588
   199
    val (initial, thms) =
wenzelm@8588
   200
      (case get_calculation state of
skalberg@15531
   201
        NONE => (true, [])
skalberg@15531
   202
      | SOME thms => (false, thms));
wenzelm@8588
   203
    val calc = thms @ facts;
wenzelm@8588
   204
  in
wenzelm@8588
   205
    err_if state (initial andalso final) "No calculation yet";
wenzelm@17350
   206
    print_calculation int (Proof.context_of state) calc;
wenzelm@8588
   207
    state |> maintain_calculation final calc
wenzelm@8588
   208
  end;
wenzelm@8588
   209
wenzelm@17350
   210
val moreover = collect false;
wenzelm@17350
   211
val ultimately = collect true;
wenzelm@8562
   212
wenzelm@6778
   213
end;