src/Pure/Isar/calculation.ML
author wenzelm
Sun May 30 21:34:19 2010 +0200 (2010-05-30)
changeset 37198 3af985b10550
parent 37047 4a95a50d0ec4
child 38332 6551e310e7cb
permissions -rw-r--r--
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
ML_Context.eval/expression expect explicit ML_Lex source, which allows surrounding further text without loosing position information;
fall back on ML_Context.eval_text if there is no position or no surrounding text;
proper Args.name_source_position for method "tactic" and "raw_tactic";
tuned;
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@22846
    40
fun print_rules ctxt =
wenzelm@37047
    41
  let val ((trans, sym), _) = Data.get (Context.Proof ctxt) in
wenzelm@22846
    42
    [Pretty.big_list "transitivity rules:"
wenzelm@32091
    43
        (map (Display.pretty_thm ctxt) (Item_Net.content trans)),
wenzelm@32091
    44
      Pretty.big_list "symmetry rules:" (map (Display.pretty_thm ctxt) sym)]
wenzelm@22846
    45
    |> Pretty.chunks |> Pretty.writeln
wenzelm@22846
    46
  end;
wenzelm@6778
    47
wenzelm@6778
    48
wenzelm@6778
    49
(* access calculation *)
wenzelm@6778
    50
wenzelm@6778
    51
fun get_calculation state =
wenzelm@37047
    52
  (case #2 (Data.get (Context.Proof (Proof.context_of state))) of
skalberg@15531
    53
    NONE => NONE
skalberg@15531
    54
  | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
wenzelm@6778
    55
wenzelm@17350
    56
val calculationN = "calculation";
wenzelm@6778
    57
wenzelm@17350
    58
fun put_calculation calc =
wenzelm@18728
    59
  `Proof.level #-> (fn lev => Proof.map_context (Context.proof_map
wenzelm@37047
    60
     (Data.map (apsnd (K (Option.map (rpair lev) calc))))))
wenzelm@26252
    61
  #> Proof.put_thms false (calculationN, calc);
wenzelm@6778
    62
wenzelm@6778
    63
wenzelm@18637
    64
wenzelm@6778
    65
(** attributes **)
wenzelm@6778
    66
wenzelm@12379
    67
(* add/del rules *)
wenzelm@12379
    68
wenzelm@37047
    69
val trans_add = Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.update);
wenzelm@37047
    70
val trans_del = Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.remove);
wenzelm@6778
    71
wenzelm@18637
    72
val sym_add =
wenzelm@37047
    73
  Thm.declaration_attribute (Data.map o apfst o apsnd o Thm.add_thm)
wenzelm@33369
    74
  #> Context_Rules.elim_query NONE;
wenzelm@33369
    75
wenzelm@18637
    76
val sym_del =
wenzelm@37047
    77
  Thm.declaration_attribute (Data.map o apfst o apsnd o Thm.del_thm)
wenzelm@33369
    78
  #> Context_Rules.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@37047
    84
  (case Seq.chop 2 (Drule.multi_resolves [th] (#2 (#1 (Data.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@26463
    92
val _ = Context.>> (Context.map_theory
wenzelm@30528
    93
 (Attrib.setup (Binding.name "trans") (Attrib.add_del trans_add trans_del)
wenzelm@30528
    94
    "declaration of transitivity rule" #>
wenzelm@30528
    95
  Attrib.setup (Binding.name "sym") (Attrib.add_del sym_add sym_del)
wenzelm@30528
    96
    "declaration of symmetry rule" #>
wenzelm@30528
    97
  Attrib.setup (Binding.name "symmetric") (Scan.succeed symmetric)
wenzelm@30528
    98
    "resolution with symmetry rule" #>
wenzelm@18708
    99
  PureThy.add_thms
haftmann@29579
   100
   [((Binding.empty, transitive_thm), [trans_add]),
haftmann@29579
   101
    ((Binding.empty, symmetric_thm), [sym_add])] #> snd));
wenzelm@15801
   102
wenzelm@6778
   103
wenzelm@6787
   104
wenzelm@6778
   105
(** proof commands **)
wenzelm@6778
   106
wenzelm@14555
   107
fun assert_sane final =
wenzelm@14555
   108
  if final then Proof.assert_forward else Proof.assert_forward_or_chain;
wenzelm@14555
   109
wenzelm@37047
   110
fun maintain_calculation int final calc state =
wenzelm@37047
   111
  let
wenzelm@37047
   112
    val state' = put_calculation (SOME calc) state;
wenzelm@37047
   113
    val ctxt' = Proof.context_of state';
wenzelm@37047
   114
    val _ =
wenzelm@37047
   115
      if int then
wenzelm@37047
   116
        Pretty.writeln
wenzelm@37047
   117
          (ProofContext.pretty_fact ctxt'
wenzelm@37047
   118
            (ProofContext.full_name ctxt' (Binding.name calculationN), calc))
wenzelm@37047
   119
      else ();
wenzelm@37047
   120
  in state' |> final ? (put_calculation NONE #> Proof.chain_facts calc) end;
wenzelm@8562
   121
wenzelm@8562
   122
wenzelm@17350
   123
(* also and finally *)
wenzelm@8562
   124
wenzelm@37047
   125
val get_rules = #1 o Data.get o Context.Proof o Proof.context_of;
wenzelm@18637
   126
wenzelm@17350
   127
fun calculate prep_rules final raw_rules int state =
wenzelm@6778
   128
  let
wenzelm@12805
   129
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
wenzelm@18947
   130
    val eq_prop = op aconv o pairself (Envir.beta_eta_contract o strip_assums_concl);
wenzelm@37047
   131
    fun projection ths th = exists (curry eq_prop th) ths;
wenzelm@8300
   132
wenzelm@17350
   133
    val opt_rules = Option.map (prep_rules state) raw_rules;
wenzelm@11097
   134
    fun combine ths =
skalberg@15531
   135
      (case opt_rules of SOME rules => rules
skalberg@15531
   136
      | NONE =>
wenzelm@33373
   137
          (case ths of
wenzelm@33373
   138
            [] => Item_Net.content (#1 (get_rules state))
wenzelm@30560
   139
          | th :: _ => Item_Net.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@37047
   148
wenzelm@37047
   149
    val _ = initial andalso final andalso error "No calculation yet";
wenzelm@37047
   150
    val _ = initial andalso is_some opt_rules andalso
wenzelm@37047
   151
      error "Initial calculation -- no rules to be given";
wenzelm@6778
   152
  in
wenzelm@37047
   153
    calculations |> Seq.map (fn calc => maintain_calculation int final calc state)
wenzelm@6778
   154
  end;
wenzelm@6778
   155
wenzelm@36323
   156
val also = calculate (K I) false;
wenzelm@36323
   157
val also_cmd = calculate Proof.get_thmss_cmd false;
wenzelm@36323
   158
val finally = calculate (K I) true;
wenzelm@36323
   159
val finally_cmd = calculate Proof.get_thmss_cmd true;
wenzelm@6778
   160
wenzelm@6778
   161
wenzelm@17350
   162
(* moreover and ultimately *)
wenzelm@8562
   163
wenzelm@17350
   164
fun collect final int state =
wenzelm@8588
   165
  let
wenzelm@14555
   166
    val facts = Proof.the_facts (assert_sane final state);
wenzelm@8588
   167
    val (initial, thms) =
wenzelm@8588
   168
      (case get_calculation state of
skalberg@15531
   169
        NONE => (true, [])
skalberg@15531
   170
      | SOME thms => (false, thms));
wenzelm@8588
   171
    val calc = thms @ facts;
wenzelm@37047
   172
    val _ = initial andalso final andalso error "No calculation yet";
wenzelm@37047
   173
  in maintain_calculation int final calc state end;
wenzelm@8588
   174
wenzelm@17350
   175
val moreover = collect false;
wenzelm@17350
   176
val ultimately = collect true;
wenzelm@8562
   177
wenzelm@6778
   178
end;