src/Pure/Isar/calculation.ML
author wenzelm
Sun Jul 08 19:51:58 2007 +0200 (2007-07-08)
changeset 23655 d2d1138e0ddc
parent 22846 fb79144af9a3
child 24039 273698405054
permissions -rw-r--r--
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
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@21506
    10
  val print_rules: Proof.context -> unit
wenzelm@16571
    11
  val get_calculation: Proof.state -> thm list option
wenzelm@18728
    12
  val trans_add: attribute
wenzelm@18728
    13
  val trans_del: attribute
wenzelm@18728
    14
  val sym_add: attribute
wenzelm@18728
    15
  val sym_del: attribute
wenzelm@18728
    16
  val symmetric: attribute
wenzelm@17350
    17
  val also: (thmref * Attrib.src list) list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@17350
    18
  val also_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@22573
    19
  val finally_: (thmref * Attrib.src list) list option -> bool ->
wenzelm@22573
    20
    Proof.state -> Proof.state Seq.seq
wenzelm@17350
    21
  val finally_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@17350
    22
  val moreover: bool -> Proof.state -> Proof.state
wenzelm@17350
    23
  val ultimately: bool -> Proof.state -> Proof.state
wenzelm@6778
    24
end;
wenzelm@6778
    25
wenzelm@6778
    26
structure Calculation: CALCULATION =
wenzelm@6778
    27
struct
wenzelm@6778
    28
wenzelm@18637
    29
(** calculation data **)
wenzelm@6778
    30
wenzelm@18637
    31
structure CalculationData = GenericDataFun
wenzelm@18637
    32
(
wenzelm@12379
    33
  type T = (thm NetRules.T * thm list) * (thm list * int) option;
wenzelm@18637
    34
  val empty = ((NetRules.elim, []), NONE);
wenzelm@18637
    35
  val extend = I;
wenzelm@6778
    36
wenzelm@18637
    37
  fun merge _ (((trans1, sym1), _), ((trans2, sym2), _)) =
wenzelm@18637
    38
    ((NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2)), NONE);
wenzelm@18637
    39
);
wenzelm@18637
    40
wenzelm@22846
    41
fun print_rules ctxt =
wenzelm@22846
    42
  let val ((trans, sym), _) = CalculationData.get (Context.Proof ctxt) in
wenzelm@22846
    43
    [Pretty.big_list "transitivity rules:"
wenzelm@22846
    44
        (map (ProofContext.pretty_thm ctxt) (NetRules.rules trans)),
wenzelm@22846
    45
      Pretty.big_list "symmetry rules:" (map (ProofContext.pretty_thm ctxt) sym)]
wenzelm@22846
    46
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
    47
  end;
wenzelm@6778
    48
wenzelm@6778
    49
wenzelm@6778
    50
(* access calculation *)
wenzelm@6778
    51
wenzelm@6778
    52
fun get_calculation state =
wenzelm@18637
    53
  (case #2 (CalculationData.get (Context.Proof (Proof.context_of state))) of
skalberg@15531
    54
    NONE => NONE
skalberg@15531
    55
  | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
wenzelm@6778
    56
wenzelm@17350
    57
val calculationN = "calculation";
wenzelm@6778
    58
wenzelm@17350
    59
fun put_calculation calc =
wenzelm@18728
    60
  `Proof.level #-> (fn lev => Proof.map_context (Context.proof_map
wenzelm@18728
    61
     (CalculationData.map (apsnd (K (Option.map (rpair lev) calc))))))
wenzelm@21445
    62
  #> Proof.put_thms (calculationN, calc);
wenzelm@6778
    63
wenzelm@6778
    64
wenzelm@18637
    65
wenzelm@6778
    66
(** attributes **)
wenzelm@6778
    67
wenzelm@12379
    68
(* add/del rules *)
wenzelm@12379
    69
wenzelm@18728
    70
val trans_add = Thm.declaration_attribute (CalculationData.map o apfst o apfst o NetRules.insert);
wenzelm@18728
    71
val trans_del = Thm.declaration_attribute (CalculationData.map o apfst o apfst o NetRules.delete);
wenzelm@6778
    72
wenzelm@18637
    73
val sym_add =
wenzelm@18728
    74
  Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Drule.add_rule)
wenzelm@18637
    75
  #> ContextRules.elim_query NONE;
wenzelm@18637
    76
val sym_del =
wenzelm@18728
    77
  Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Drule.del_rule)
wenzelm@18637
    78
  #> ContextRules.rule_del;
wenzelm@12379
    79
wenzelm@12379
    80
wenzelm@18637
    81
(* symmetric *)
wenzelm@12379
    82
wenzelm@18728
    83
val symmetric = Thm.rule_attribute (fn x => fn th =>
wenzelm@19861
    84
  (case Seq.chop 2 (Drule.multi_resolves [th] (#2 (#1 (CalculationData.get x)))) of
wenzelm@20898
    85
    ([th'], _) => Drule.zero_var_indexes th'
wenzelm@12379
    86
  | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
wenzelm@12379
    87
  | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
wenzelm@12379
    88
wenzelm@6778
    89
wenzelm@6778
    90
(* concrete syntax *)
wenzelm@6778
    91
wenzelm@18637
    92
val trans_att = Attrib.add_del_args trans_add trans_del;
wenzelm@18637
    93
val sym_att = Attrib.add_del_args sym_add sym_del;
wenzelm@12379
    94
wenzelm@15801
    95
val _ = Context.add_setup
wenzelm@18708
    96
 (Attrib.add_attributes
wenzelm@18728
    97
   [("trans", trans_att, "declaration of transitivity rule"),
wenzelm@18728
    98
    ("sym", sym_att, "declaration of symmetry rule"),
wenzelm@18728
    99
    ("symmetric", Attrib.no_args symmetric, "resolution with symmetry rule")] #>
wenzelm@18708
   100
  PureThy.add_thms
wenzelm@18728
   101
   [(("", transitive_thm), [trans_add]),
wenzelm@18728
   102
    (("", symmetric_thm), [sym_add])] #> snd);
wenzelm@15801
   103
wenzelm@6778
   104
wenzelm@6787
   105
wenzelm@6778
   106
(** proof commands **)
wenzelm@6778
   107
wenzelm@18678
   108
fun err_if b msg = if b then error msg else ();
wenzelm@17350
   109
wenzelm@14555
   110
fun assert_sane final =
wenzelm@14555
   111
  if final then Proof.assert_forward else Proof.assert_forward_or_chain;
wenzelm@14555
   112
wenzelm@17350
   113
fun maintain_calculation false calc = put_calculation (SOME calc)
wenzelm@17350
   114
  | maintain_calculation true calc = put_calculation NONE #> Proof.chain_facts calc;
wenzelm@6778
   115
wenzelm@17350
   116
fun print_calculation false _ _ = ()
wenzelm@17350
   117
  | print_calculation true ctxt calc =
wenzelm@17350
   118
      Pretty.writeln (ProofContext.pretty_fact ctxt (calculationN, calc));
wenzelm@8562
   119
wenzelm@8562
   120
wenzelm@17350
   121
(* also and finally *)
wenzelm@8562
   122
wenzelm@18637
   123
val get_rules = #1 o CalculationData.get o Context.Proof o Proof.context_of;
wenzelm@18637
   124
wenzelm@17350
   125
fun calculate prep_rules final raw_rules int state =
wenzelm@6778
   126
  let
wenzelm@12805
   127
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
wenzelm@18947
   128
    val eq_prop = op aconv o pairself (Envir.beta_eta_contract o strip_assums_concl);
wenzelm@11097
   129
    fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
wenzelm@8300
   130
wenzelm@17350
   131
    val opt_rules = Option.map (prep_rules state) raw_rules;
wenzelm@11097
   132
    fun combine ths =
skalberg@15531
   133
      (case opt_rules of SOME rules => rules
skalberg@15531
   134
      | NONE =>
wenzelm@18637
   135
          (case ths of [] => NetRules.rules (#1 (get_rules state))
wenzelm@18637
   136
          | th :: _ => NetRules.retrieve (#1 (get_rules state)) (strip_assums_concl th)))
wenzelm@18223
   137
      |> Seq.of_list |> Seq.maps (Drule.multi_resolve ths)
wenzelm@11097
   138
      |> Seq.filter (not o projection ths);
wenzelm@7414
   139
wenzelm@14555
   140
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@6903
   141
    val (initial, calculations) =
wenzelm@6778
   142
      (case get_calculation state of
skalberg@15531
   143
        NONE => (true, Seq.single facts)
skalberg@15531
   144
      | SOME calc => (false, Seq.map single (combine (calc @ facts))));
wenzelm@6778
   145
  in
wenzelm@18678
   146
    err_if (initial andalso final) "No calculation yet";
wenzelm@18678
   147
    err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
wenzelm@17350
   148
    calculations |> Seq.map (fn calc => (print_calculation int (Proof.context_of state) calc;
wenzelm@12055
   149
        state |> maintain_calculation final calc))
wenzelm@6778
   150
  end;
wenzelm@6778
   151
wenzelm@17350
   152
val also = calculate Proof.get_thmss false;
wenzelm@17350
   153
val also_i = calculate (K I) false;
wenzelm@22573
   154
val finally_ = calculate Proof.get_thmss true;
wenzelm@17350
   155
val finally_i = calculate (K I) true;
wenzelm@6778
   156
wenzelm@6778
   157
wenzelm@17350
   158
(* moreover and ultimately *)
wenzelm@8562
   159
wenzelm@17350
   160
fun collect final int state =
wenzelm@8588
   161
  let
wenzelm@14555
   162
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@8588
   163
    val (initial, thms) =
wenzelm@8588
   164
      (case get_calculation state of
skalberg@15531
   165
        NONE => (true, [])
skalberg@15531
   166
      | SOME thms => (false, thms));
wenzelm@8588
   167
    val calc = thms @ facts;
wenzelm@8588
   168
  in
wenzelm@18678
   169
    err_if (initial andalso final) "No calculation yet";
wenzelm@17350
   170
    print_calculation int (Proof.context_of state) calc;
wenzelm@8588
   171
    state |> maintain_calculation final calc
wenzelm@8588
   172
  end;
wenzelm@8588
   173
wenzelm@17350
   174
val moreover = collect false;
wenzelm@17350
   175
val ultimately = collect true;
wenzelm@8562
   176
wenzelm@6778
   177
end;