src/Pure/Isar/calculation.ML
author wenzelm
Thu, 06 Jun 2024 22:26:40 +0200
changeset 80274 cff00b3dddf5
parent 74561 8e6c973003c8
permissions -rw-r--r--
clarified names;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/calculation.ML
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     3
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
     4
Generic calculational proofs.
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     5
*)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     6
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     7
signature CALCULATION =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
     8
sig
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21445
diff changeset
     9
  val print_rules: Proof.context -> unit
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    10
  val check: Proof.state -> thm list option
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    11
  val trans_add: attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    12
  val trans_del: attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    13
  val sym_add: attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    14
  val sym_del: attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    15
  val symmetric: attribute
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
    16
  val also: thm list option -> bool -> Proof.state -> Proof.state Seq.result Seq.seq
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 56894
diff changeset
    17
  val also_cmd: (Facts.ref * Token.src list) list option ->
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
    18
    bool -> Proof.state -> Proof.state Seq.result Seq.seq
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
    19
  val finally: thm list option -> bool -> Proof.state -> Proof.state Seq.result Seq.seq
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 56894
diff changeset
    20
  val finally_cmd: (Facts.ref * Token.src list) list option -> bool ->
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
    21
    Proof.state -> Proof.state Seq.result Seq.seq
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
    22
  val moreover: bool -> Proof.state -> Proof.state
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
    23
  val ultimately: bool -> Proof.state -> Proof.state
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    24
end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    25
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    26
structure Calculation: CALCULATION =
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    27
struct
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    28
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
    29
(** calculation data **)
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    30
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    31
type calculation = {result: thm list, level: int, serial: serial, pos: Position.T};
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    32
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
    33
structure Data = Generic_Data
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
    34
(
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    35
  type T = (thm Item_Net.T * thm Item_Net.T) * calculation option;
74152
069f6b2c5a07 tuned signature;
wenzelm
parents: 74149
diff changeset
    36
  val empty = ((Thm.item_net_elim, Thm.item_net), NONE);
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33373
diff changeset
    37
  fun merge (((trans1, sym1), _), ((trans2, sym2), _)) =
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    38
    ((Item_Net.merge (trans1, trans2), Item_Net.merge (sym1, sym2)), NONE);
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
    39
);
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
    40
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
    41
val get_rules = #1 o Data.get o Context.Proof;
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    42
val get_trans_rules = Item_Net.content o #1 o get_rules;
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    43
val get_sym_rules = Item_Net.content o #2 o get_rules;
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    44
val get_calculation = #2 o Data.get o Context.Proof;
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
    45
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22573
diff changeset
    46
fun print_rules ctxt =
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    47
  let val pretty_thm = Thm.pretty_thm_item ctxt in
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    48
   [Pretty.big_list "transitivity rules:" (map pretty_thm (get_trans_rules ctxt)),
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
    49
    Pretty.big_list "symmetry rules:" (map pretty_thm (get_sym_rules ctxt))]
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 55141
diff changeset
    50
  end |> Pretty.writeln_chunks;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    51
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    52
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    53
(* access calculation *)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    54
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    55
fun check_calculation state =
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    56
  (case get_calculation (Proof.context_of state) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    57
    NONE => NONE
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    58
  | SOME calculation =>
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    59
      if #level calculation = Proof.level state then SOME calculation else NONE);
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    60
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    61
val check = Option.map #result o check_calculation;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    62
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
    63
val calculationN = "calculation";
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    64
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    65
fun update_calculation calc state =
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    66
  let
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    67
    fun report def serial pos =
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    68
      Context_Position.report (Proof.context_of state)
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    69
        (Position.thread_data ())
74183
af81e4a307be clarified signature;
wenzelm
parents: 74152
diff changeset
    70
        (Position.make_entity_markup def serial calculationN ("", pos));
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    71
    val calculation =
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    72
      (case calc of
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    73
        NONE => NONE
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    74
      | SOME result =>
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    75
          (case check_calculation state of
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    76
            NONE =>
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    77
              let
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    78
                val level = Proof.level state;
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    79
                val serial = serial ();
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    80
                val pos = Position.thread_data ();
74262
839a6e284545 tuned signature;
wenzelm
parents: 74183
diff changeset
    81
                val _ = report {def = true} serial pos;
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    82
              in SOME {result = result, level = level, serial = serial, pos = pos} end
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    83
          | SOME {level, serial, pos, ...} =>
74262
839a6e284545 tuned signature;
wenzelm
parents: 74183
diff changeset
    84
              (report {def = false} serial pos;
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    85
                SOME {result = result, level = level, serial = serial, pos = pos})));
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    86
  in
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    87
    state
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    88
    |> (Proof.map_context o Context.proof_map o Data.map o apsnd) (K calculation)
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    89
    |> Proof.map_context (Proof_Context.put_thms false (calculationN, calc))
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
    90
  end;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    91
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    92
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
    93
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    94
(** attributes **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
    95
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    96
(* add/del rules *)
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
    97
67627
5cca859b2d2e trim context of persistent data;
wenzelm
parents: 67147
diff changeset
    98
val trans_add =
5cca859b2d2e trim context of persistent data;
wenzelm
parents: 67147
diff changeset
    99
  Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.update o Thm.trim_context);
5cca859b2d2e trim context of persistent data;
wenzelm
parents: 67147
diff changeset
   100
5cca859b2d2e trim context of persistent data;
wenzelm
parents: 67147
diff changeset
   101
val trans_del =
5cca859b2d2e trim context of persistent data;
wenzelm
parents: 67147
diff changeset
   102
  Thm.declaration_attribute (Data.map o apfst o apfst o Item_Net.remove);
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   103
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
   104
val sym_add =
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 42360
diff changeset
   105
  Thm.declaration_attribute (fn th =>
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   106
    (Data.map o apfst o apsnd) (Item_Net.update (Thm.trim_context th)) #>
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 42360
diff changeset
   107
    Thm.attribute_declaration (Context_Rules.elim_query NONE) th);
33369
470a7b233ee5 modernized structure Context_Rules;
wenzelm
parents: 32091
diff changeset
   108
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
   109
val sym_del =
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 42360
diff changeset
   110
  Thm.declaration_attribute (fn th =>
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   111
    (Data.map o apfst o apsnd) (Item_Net.remove th) #>
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 42360
diff changeset
   112
    Thm.attribute_declaration Context_Rules.rule_del th);
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   113
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   114
18637
33a6f6caa617 generic data and attributes;
wenzelm
parents: 18377
diff changeset
   115
(* symmetric *)
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   116
61853
fb7756087101 rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
wenzelm
parents: 61268
diff changeset
   117
val symmetric =
fb7756087101 rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
wenzelm
parents: 61268
diff changeset
   118
  Thm.rule_attribute [] (fn context => fn th =>
74149
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   119
    let val ctxt = Context.proof_of context in
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   120
      (case Seq.chop 2 (Drule.multi_resolves (SOME ctxt) [th] (get_sym_rules ctxt)) of
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   121
        ([th'], _) => Drule.zero_var_indexes th'
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   122
      | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   123
      | _ => raise THM ("symmetric: multiple unifiers", 1, [th]))
9e73600ec75d more scalable data structures;
wenzelm
parents: 67627
diff changeset
   124
    end);
12379
c74d160ff0c5 added 'sym' and 'symmetric' atts;
wenzelm
parents: 12311
diff changeset
   125
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   126
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   127
(* concrete syntax *)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   128
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51584
diff changeset
   129
val _ = Theory.setup
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 63543
diff changeset
   130
 (Attrib.setup \<^binding>\<open>trans\<close> (Attrib.add_del trans_add trans_del)
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30279
diff changeset
   131
    "declaration of transitivity rule" #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 63543
diff changeset
   132
  Attrib.setup \<^binding>\<open>sym\<close> (Attrib.add_del sym_add sym_del)
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30279
diff changeset
   133
    "declaration of symmetry rule" #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 63543
diff changeset
   134
  Attrib.setup \<^binding>\<open>symmetric\<close> (Scan.succeed symmetric)
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30279
diff changeset
   135
    "resolution with symmetry rule" #>
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38332
diff changeset
   136
  Global_Theory.add_thms
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 28965
diff changeset
   137
   [((Binding.empty, transitive_thm), [trans_add]),
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 51584
diff changeset
   138
    ((Binding.empty, symmetric_thm), [sym_add])] #> snd);
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15570
diff changeset
   139
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   140
6787
wenzelm
parents: 6782
diff changeset
   141
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   142
(** proof commands **)
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   143
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   144
fun assert_sane final =
59126
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   145
  if final then Proof.assert_forward
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   146
  else
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   147
    Proof.assert_forward_or_chain #>
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   148
    tap (fn state =>
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   149
      if can Proof.assert_chain state then
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   150
        Context_Position.report (Proof.context_of state) (Position.thread_data ()) Markup.improper
ff0703716c51 more markup for improper situations;
wenzelm
parents: 59058
diff changeset
   151
      else ());
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   152
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   153
fun maintain_calculation int final calc state =
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   154
  let
63541
e308f15cc8a7 more markup for unstructured calculation;
wenzelm
parents: 63368
diff changeset
   155
    val state' = state
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
   156
      |> update_calculation (SOME calc)
63541
e308f15cc8a7 more markup for unstructured calculation;
wenzelm
parents: 63368
diff changeset
   157
      |> Proof.improper_reset_facts;
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   158
    val ctxt' = Proof.context_of state';
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   159
    val _ =
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   160
      if int then
56894
fa12200276bf more emphatic output for Proof General;
wenzelm
parents: 56334
diff changeset
   161
        Proof_Context.pretty_fact ctxt'
fa12200276bf more emphatic output for Proof General;
wenzelm
parents: 56334
diff changeset
   162
          (Proof_Context.full_name ctxt' (Binding.name calculationN), calc)
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 58028
diff changeset
   163
        |> Pretty.string_of |> writeln
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   164
      else ();
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
   165
  in state' |> final ? (update_calculation NONE #> Proof.chain_facts calc) end;
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   166
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   167
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   168
(* also and finally *)
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   169
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   170
fun calculate prep_rules final raw_rules int state =
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   171
  let
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
   172
    val ctxt = Proof.context_of state;
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 61084
diff changeset
   173
    val pretty_thm = Thm.pretty_thm ctxt;
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 61084
diff changeset
   174
    val pretty_thm_item = Thm.pretty_thm_item ctxt;
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
   175
12805
3be853cf19cf Thm.prop_of;
wenzelm
parents: 12402
diff changeset
   176
    val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58950
diff changeset
   177
    val eq_prop = op aconv o apply2 (Envir.beta_eta_contract o strip_assums_concl);
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   178
    fun check_projection ths th =
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   179
      (case find_index (curry eq_prop th) ths of
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   180
        ~1 => Seq.Result [th]
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   181
      | i =>
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   182
          Seq.Error (fn () =>
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   183
            (Pretty.string_of o Pretty.chunks)
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   184
             [Pretty.block [Pretty.str "Vacuous calculation result:", Pretty.brk 1, pretty_thm th],
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   185
              (Pretty.block o Pretty.fbreaks)
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   186
                (Pretty.str ("derived as projection (" ^ string_of_int (i + 1) ^ ") from:") ::
51584
98029ceda8ce more item markup;
wenzelm
parents: 51580
diff changeset
   187
                  map pretty_thm_item ths)]));
8300
4c3f83414de3 use NetRules;
wenzelm
parents: 8150
diff changeset
   188
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
   189
    val opt_rules = Option.map (prep_rules ctxt) raw_rules;
11097
c1be9f2dff4c more robust selection of calculational rules;
wenzelm
parents: 10478
diff changeset
   190
    fun combine ths =
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   191
      Seq.append
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   192
        ((case opt_rules of
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   193
          SOME rules => rules
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   194
        | NONE =>
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   195
            (case ths of
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   196
              [] => Item_Net.content (#1 (get_rules ctxt))
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   197
            | th :: _ => Item_Net.retrieve (#1 (get_rules ctxt)) (strip_assums_concl th)))
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58893
diff changeset
   198
        |> Seq.of_list |> Seq.maps (Drule.multi_resolve (SOME ctxt) ths)
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   199
        |> Seq.map (check_projection ths))
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   200
        (Seq.single (Seq.Error (fn () =>
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   201
          (Pretty.string_of o Pretty.block o Pretty.fbreaks)
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   202
            (Pretty.str "No matching trans rules for calculation:" ::
51584
98029ceda8ce more item markup;
wenzelm
parents: 51580
diff changeset
   203
              map pretty_thm_item ths))));
7414
9bc7797d1249 calculation: thm list;
wenzelm
parents: 7197
diff changeset
   204
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   205
    val facts = Proof.the_facts (assert_sane final state);
6903
682f8a9ec75d improved errors;
wenzelm
parents: 6877
diff changeset
   206
    val (initial, calculations) =
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
   207
      (case check state of
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   208
        NONE => (true, Seq.single (Seq.Result facts))
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   209
      | SOME calc => (false, combine (calc @ facts)));
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   210
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   211
    val _ = initial andalso final andalso error "No calculation yet";
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   212
    val _ = initial andalso is_some opt_rules andalso
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   213
      error "Initial calculation -- no rules to be given";
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   214
  in
49868
3039922ffd8d more informative errors for 'also' and 'finally';
wenzelm
parents: 45375
diff changeset
   215
    calculations |> Seq.map_result (fn calc => maintain_calculation int final calc state)
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   216
  end;
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   217
36323
655e2d74de3a modernized naming conventions of main Isar proof elements;
wenzelm
parents: 33519
diff changeset
   218
val also = calculate (K I) false;
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
   219
val also_cmd = calculate Attrib.eval_thms false;
36323
655e2d74de3a modernized naming conventions of main Isar proof elements;
wenzelm
parents: 33519
diff changeset
   220
val finally = calculate (K I) true;
38332
6551e310e7cb prefer plain Attrib.eval_thmss -- also means the assert_forward of Proof.get_thmss_cmd is skipped, leading to uniform (albeit odd) behaviour concerning forward chaining;
wenzelm
parents: 37047
diff changeset
   221
val finally_cmd = calculate Attrib.eval_thms true;
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   222
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   223
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   224
(* moreover and ultimately *)
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   225
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   226
fun collect final int state =
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   227
  let
14555
341908d6c792 'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents: 14549
diff changeset
   228
    val facts = Proof.the_facts (assert_sane final state);
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   229
    val (initial, thms) =
63543
e6e057c01401 entity markup for calculation;
wenzelm
parents: 63541
diff changeset
   230
      (case check state of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   231
        NONE => (true, [])
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   232
      | SOME thms => (false, thms));
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   233
    val calc = thms @ facts;
37047
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   234
    val _ = initial andalso final andalso error "No calculation yet";
4a95a50d0ec4 print calculation result in the context where the fact is actually defined -- proper externing;
wenzelm
parents: 36323
diff changeset
   235
  in maintain_calculation int final calc state end;
8588
b7c3f264f8ac added 'ultimately';
wenzelm
parents: 8562
diff changeset
   236
17350
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   237
val moreover = collect false;
26cd3756377a more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents: 16571
diff changeset
   238
val ultimately = collect true;
8562
ce0e2b8e8844 added 'moreover' command;
wenzelm
parents: 8461
diff changeset
   239
6778
2f66eea8a025 Support for calculational proofs.
wenzelm
parents:
diff changeset
   240
end;