src/Pure/Isar/calculation.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 39557 fe5722fce758
child 42360 da8817d01e7c
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
wenzelm@6778
     1
(*  Title:      Pure/Isar/calculation.ML
wenzelm@6778
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6778
     3
wenzelm@17350
     4
Generic calculational proofs.
wenzelm@6778
     5
*)
wenzelm@6778
     6
wenzelm@6778
     7
signature CALCULATION =
wenzelm@6778
     8
sig
wenzelm@21506
     9
  val print_rules: Proof.context -> unit
wenzelm@16571
    10
  val get_calculation: Proof.state -> thm list option
wenzelm@18728
    11
  val trans_add: attribute
wenzelm@18728
    12
  val trans_del: attribute
wenzelm@18728
    13
  val sym_add: attribute
wenzelm@18728
    14
  val sym_del: attribute
wenzelm@18728
    15
  val symmetric: attribute
wenzelm@36323
    16
  val also: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@37047
    17
  val also_cmd: (Facts.ref * Attrib.src list) list option ->
wenzelm@37047
    18
    bool -> Proof.state -> Proof.state Seq.seq
wenzelm@36323
    19
  val finally: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
wenzelm@36323
    20
  val finally_cmd: (Facts.ref * Attrib.src list) list option -> bool ->
wenzelm@22573
    21
    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@37047
    31
structure Data = Generic_Data
wenzelm@18637
    32
(
wenzelm@30560
    33
  type T = (thm Item_Net.T * thm list) * (thm list * int) option;
wenzelm@30560
    34
  val empty = ((Thm.elim_rules, []), NONE);
wenzelm@18637
    35
  val extend = I;
wenzelm@33519
    36
  fun merge (((trans1, sym1), _), ((trans2, sym2), _)) =
wenzelm@30560
    37
    ((Item_Net.merge (trans1, trans2), Thm.merge_thms (sym1, sym2)), NONE);
wenzelm@18637
    38
);
wenzelm@18637
    39
wenzelm@38332
    40
val get_rules = #1 o Data.get o Context.Proof;
wenzelm@38332
    41
wenzelm@22846
    42
fun print_rules ctxt =
wenzelm@38332
    43
  let val (trans, sym) = get_rules ctxt in
wenzelm@22846
    44
    [Pretty.big_list "transitivity rules:"
wenzelm@32091
    45
        (map (Display.pretty_thm ctxt) (Item_Net.content trans)),
wenzelm@32091
    46
      Pretty.big_list "symmetry rules:" (map (Display.pretty_thm ctxt) sym)]
wenzelm@22846
    47
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
    48
  end;
wenzelm@6778
    49
wenzelm@6778
    50
wenzelm@6778
    51
(* access calculation *)
wenzelm@6778
    52
wenzelm@6778
    53
fun get_calculation state =
wenzelm@37047
    54
  (case #2 (Data.get (Context.Proof (Proof.context_of state))) of
skalberg@15531
    55
    NONE => NONE
skalberg@15531
    56
  | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
wenzelm@6778
    57
wenzelm@17350
    58
val calculationN = "calculation";
wenzelm@6778
    59
wenzelm@17350
    60
fun put_calculation calc =
wenzelm@18728
    61
  `Proof.level #-> (fn lev => Proof.map_context (Context.proof_map
wenzelm@37047
    62
     (Data.map (apsnd (K (Option.map (rpair lev) calc))))))
wenzelm@26252
    63
  #> Proof.put_thms false (calculationN, calc);
wenzelm@6778
    64
wenzelm@6778
    65
wenzelm@18637
    66
wenzelm@6778
    67
(** attributes **)
wenzelm@6778
    68
wenzelm@12379
    69
(* add/del rules *)
wenzelm@12379
    70
wenzelm@37047
    71
val trans_add = Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.update);
wenzelm@37047
    72
val trans_del = Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.remove);
wenzelm@6778
    73
wenzelm@18637
    74
val sym_add =
wenzelm@37047
    75
  Thm.declaration_attribute (Data.map o apfst o apsnd o Thm.add_thm)
wenzelm@33369
    76
  #> Context_Rules.elim_query NONE;
wenzelm@33369
    77
wenzelm@18637
    78
val sym_del =
wenzelm@37047
    79
  Thm.declaration_attribute (Data.map o apfst o apsnd o Thm.del_thm)
wenzelm@33369
    80
  #> Context_Rules.rule_del;
wenzelm@12379
    81
wenzelm@12379
    82
wenzelm@18637
    83
(* symmetric *)
wenzelm@12379
    84
wenzelm@18728
    85
val symmetric = Thm.rule_attribute (fn x => fn th =>
wenzelm@37047
    86
  (case Seq.chop 2 (Drule.multi_resolves [th] (#2 (#1 (Data.get x)))) of
wenzelm@20898
    87
    ([th'], _) => Drule.zero_var_indexes th'
wenzelm@12379
    88
  | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
wenzelm@12379
    89
  | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
wenzelm@12379
    90
wenzelm@6778
    91
wenzelm@6778
    92
(* concrete syntax *)
wenzelm@6778
    93
wenzelm@26463
    94
val _ = Context.>> (Context.map_theory
wenzelm@30528
    95
 (Attrib.setup (Binding.name "trans") (Attrib.add_del trans_add trans_del)
wenzelm@30528
    96
    "declaration of transitivity rule" #>
wenzelm@30528
    97
  Attrib.setup (Binding.name "sym") (Attrib.add_del sym_add sym_del)
wenzelm@30528
    98
    "declaration of symmetry rule" #>
wenzelm@30528
    99
  Attrib.setup (Binding.name "symmetric") (Scan.succeed symmetric)
wenzelm@30528
   100
    "resolution with symmetry rule" #>
wenzelm@39557
   101
  Global_Theory.add_thms
haftmann@29579
   102
   [((Binding.empty, transitive_thm), [trans_add]),
haftmann@29579
   103
    ((Binding.empty, symmetric_thm), [sym_add])] #> snd));
wenzelm@15801
   104
wenzelm@6778
   105
wenzelm@6787
   106
wenzelm@6778
   107
(** proof commands **)
wenzelm@6778
   108
wenzelm@14555
   109
fun assert_sane final =
wenzelm@14555
   110
  if final then Proof.assert_forward else Proof.assert_forward_or_chain;
wenzelm@14555
   111
wenzelm@37047
   112
fun maintain_calculation int final calc state =
wenzelm@37047
   113
  let
wenzelm@37047
   114
    val state' = put_calculation (SOME calc) state;
wenzelm@37047
   115
    val ctxt' = Proof.context_of state';
wenzelm@37047
   116
    val _ =
wenzelm@37047
   117
      if int then
wenzelm@37047
   118
        Pretty.writeln
wenzelm@37047
   119
          (ProofContext.pretty_fact ctxt'
wenzelm@37047
   120
            (ProofContext.full_name ctxt' (Binding.name calculationN), calc))
wenzelm@37047
   121
      else ();
wenzelm@37047
   122
  in state' |> final ? (put_calculation NONE #> Proof.chain_facts calc) end;
wenzelm@8562
   123
wenzelm@8562
   124
wenzelm@17350
   125
(* also and finally *)
wenzelm@8562
   126
wenzelm@17350
   127
fun calculate prep_rules final raw_rules int state =
wenzelm@6778
   128
  let
wenzelm@38332
   129
    val ctxt = Proof.context_of state;
wenzelm@38332
   130
wenzelm@12805
   131
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
wenzelm@18947
   132
    val eq_prop = op aconv o pairself (Envir.beta_eta_contract o strip_assums_concl);
wenzelm@37047
   133
    fun projection ths th = exists (curry eq_prop th) ths;
wenzelm@8300
   134
wenzelm@38332
   135
    val opt_rules = Option.map (prep_rules ctxt) raw_rules;
wenzelm@11097
   136
    fun combine ths =
skalberg@15531
   137
      (case opt_rules of SOME rules => rules
skalberg@15531
   138
      | NONE =>
wenzelm@33373
   139
          (case ths of
wenzelm@38332
   140
            [] => Item_Net.content (#1 (get_rules ctxt))
wenzelm@38332
   141
          | th :: _ => Item_Net.retrieve (#1 (get_rules ctxt)) (strip_assums_concl th)))
wenzelm@18223
   142
      |> Seq.of_list |> Seq.maps (Drule.multi_resolve ths)
wenzelm@11097
   143
      |> Seq.filter (not o projection ths);
wenzelm@7414
   144
wenzelm@14555
   145
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@6903
   146
    val (initial, calculations) =
wenzelm@6778
   147
      (case get_calculation state of
skalberg@15531
   148
        NONE => (true, Seq.single facts)
skalberg@15531
   149
      | SOME calc => (false, Seq.map single (combine (calc @ facts))));
wenzelm@37047
   150
wenzelm@37047
   151
    val _ = initial andalso final andalso error "No calculation yet";
wenzelm@37047
   152
    val _ = initial andalso is_some opt_rules andalso
wenzelm@37047
   153
      error "Initial calculation -- no rules to be given";
wenzelm@6778
   154
  in
wenzelm@37047
   155
    calculations |> Seq.map (fn calc => maintain_calculation int final calc state)
wenzelm@6778
   156
  end;
wenzelm@6778
   157
wenzelm@36323
   158
val also = calculate (K I) false;
wenzelm@38332
   159
val also_cmd = calculate Attrib.eval_thms false;
wenzelm@36323
   160
val finally = calculate (K I) true;
wenzelm@38332
   161
val finally_cmd = calculate Attrib.eval_thms true;
wenzelm@6778
   162
wenzelm@6778
   163
wenzelm@17350
   164
(* moreover and ultimately *)
wenzelm@8562
   165
wenzelm@17350
   166
fun collect final int state =
wenzelm@8588
   167
  let
wenzelm@14555
   168
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@8588
   169
    val (initial, thms) =
wenzelm@8588
   170
      (case get_calculation state of
skalberg@15531
   171
        NONE => (true, [])
skalberg@15531
   172
      | SOME thms => (false, thms));
wenzelm@8588
   173
    val calc = thms @ facts;
wenzelm@37047
   174
    val _ = initial andalso final andalso error "No calculation yet";
wenzelm@37047
   175
  in maintain_calculation int final calc state 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;