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