src/Pure/Isar/calculation.ML
author wenzelm
Thu Dec 06 00:42:00 2001 +0100 (2001-12-06)
changeset 12402 cef751fff6b0
parent 12379 c74d160ff0c5
child 12805 3be853cf19cf
permissions -rw-r--r--
clarified sym_del;
     1 (*  Title:      Pure/Isar/calculation.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Support for calculational proofs.
     7 *)
     8 
     9 signature CALCULATION =
    10 sig
    11   val print_global_rules: theory -> unit
    12   val print_local_rules: Proof.context -> unit
    13   val trans_add_global: theory attribute
    14   val trans_del_global: theory attribute
    15   val trans_add_local: Proof.context attribute
    16   val trans_del_local: Proof.context attribute
    17   val sym_add_global: theory attribute
    18   val sym_del_global: theory attribute
    19   val sym_add_local: Proof.context attribute
    20   val sym_del_local: Proof.context attribute
    21   val symmetric_global: theory attribute
    22   val symmetric_local: Proof.context attribute
    23   val also: thm list option -> (Proof.context -> thm list -> unit)
    24     -> Proof.state -> Proof.state Seq.seq
    25   val finally: thm list option -> (Proof.context -> thm list -> unit)
    26     -> Proof.state -> Proof.state Seq.seq
    27   val moreover: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
    28   val ultimately: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state
    29   val setup: (theory -> theory) list
    30 end;
    31 
    32 structure Calculation: CALCULATION =
    33 struct
    34 
    35 (** global and local calculation data **)
    36 
    37 (* theory data kind 'Isar/calculation' *)
    38 
    39 fun print_rules prt x (trans, sym) =
    40   [Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules trans)),
    41     Pretty.big_list "symmetry rules:" (map (prt x) sym)]
    42   |> Pretty.chunks |> Pretty.writeln;
    43 
    44 structure GlobalCalculationArgs =
    45 struct
    46   val name = "Isar/calculation";
    47   type T = thm NetRules.T * thm list
    48 
    49   val empty = (NetRules.elim, []);
    50   val copy = I;
    51   val prep_ext = I;
    52   fun merge ((trans1, sym1), (trans2, sym2)) =
    53     (NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2));
    54   val print = print_rules Display.pretty_thm_sg;
    55 end;
    56 
    57 structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs);
    58 val print_global_rules = GlobalCalculation.print;
    59 
    60 
    61 (* proof data kind 'Isar/calculation' *)
    62 
    63 structure LocalCalculationArgs =
    64 struct
    65   val name = "Isar/calculation";
    66   type T = (thm NetRules.T * thm list) * (thm list * int) option;
    67 
    68   fun init thy = (GlobalCalculation.get thy, None);
    69   fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs;
    70 end;
    71 
    72 structure LocalCalculation = ProofDataFun(LocalCalculationArgs);
    73 val get_local_rules = #1 o LocalCalculation.get_st;
    74 val print_local_rules = LocalCalculation.print;
    75 
    76 
    77 (* access calculation *)
    78 
    79 fun get_calculation state =
    80   (case #2 (LocalCalculation.get_st state) of
    81     None => None
    82   | Some (thms, lev) => if lev = Proof.level state then Some thms else None);
    83 
    84 fun put_calculation thms state =
    85   LocalCalculation.put_st (get_local_rules state, Some (thms, Proof.level state)) state;
    86 
    87 fun reset_calculation state =
    88   LocalCalculation.put_st (get_local_rules state, None) state;
    89 
    90 
    91 
    92 (** attributes **)
    93 
    94 (* add/del rules *)
    95 
    96 fun global_att f (x, thm) = (GlobalCalculation.map (f thm) x, thm);
    97 fun local_att f (x, thm) = (LocalCalculation.map (apfst (f thm)) x, thm);
    98 
    99 val trans_add_global = global_att (apfst o NetRules.insert);
   100 val trans_del_global = global_att (apfst o NetRules.delete);
   101 val trans_add_local = local_att (apfst o NetRules.insert);
   102 val trans_del_local = local_att (apfst o NetRules.delete);
   103 
   104 val sym_add_global = global_att (apsnd o Drule.add_rule) o ContextRules.elim_query_global None;
   105 val sym_del_global = global_att (apsnd o Drule.del_rule) o ContextRules.rule_del_global;
   106 val sym_add_local = local_att (apsnd o Drule.add_rule) o ContextRules.elim_query_local None;
   107 val sym_del_local = local_att (apsnd o Drule.del_rule) o ContextRules.rule_del_local;
   108 
   109 
   110 (* symmetry *)
   111 
   112 fun gen_symmetric get_sym = Drule.rule_attribute (fn x => fn th =>
   113   (case Seq.chop (2, Method.multi_resolves [th] (get_sym x)) of
   114     ([th'], _) => th'
   115   | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
   116   | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
   117 
   118 val symmetric_global = gen_symmetric (#2 o GlobalCalculation.get);
   119 val symmetric_local = gen_symmetric (#2 o #1 o LocalCalculation.get);
   120 
   121 
   122 (* concrete syntax *)
   123 
   124 val trans_attr =
   125  (Attrib.add_del_args trans_add_global trans_del_global,
   126   Attrib.add_del_args trans_add_local trans_del_local);
   127 
   128 val sym_attr =
   129  (Attrib.add_del_args sym_add_global sym_del_global,
   130   Attrib.add_del_args sym_add_local sym_del_local);
   131 
   132 
   133 
   134 (** proof commands **)
   135 
   136 (* maintain calculation register *)
   137 
   138 val calculationN = "calculation";
   139 
   140 fun maintain_calculation false calc state =
   141       state
   142       |> put_calculation calc
   143       |> Proof.simple_have_thms calculationN calc
   144       |> Proof.reset_facts
   145   | maintain_calculation true calc state =
   146       state
   147       |> reset_calculation
   148       |> Proof.reset_thms calculationN
   149       |> Proof.simple_have_thms "" calc
   150       |> Proof.chain;
   151 
   152 
   153 (* 'also' and 'finally' *)
   154 
   155 fun err_if state b msg = if b then raise Proof.STATE (msg, state) else ();
   156 
   157 fun calculate final opt_rules print state =
   158   let
   159     val strip_assums_concl = Logic.strip_assums_concl o #prop o Thm.rep_thm;
   160     val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl);
   161     fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
   162 
   163     fun combine ths =
   164       (case opt_rules of Some rules => rules
   165       | None =>
   166           (case ths of [] => NetRules.rules (#1 (get_local_rules state))
   167           | th :: _ => NetRules.retrieve (#1 (get_local_rules state)) (strip_assums_concl th)))
   168       |> Seq.of_list |> Seq.map (Method.multi_resolve ths) |> Seq.flat
   169       |> Seq.filter (not o projection ths);
   170 
   171     val facts = Proof.the_facts (Proof.assert_forward state);
   172     val (initial, calculations) =
   173       (case get_calculation state of
   174         None => (true, Seq.single facts)
   175       | Some calc => (false, Seq.map single (combine (calc @ facts))));
   176   in
   177     err_if state (initial andalso final) "No calculation yet";
   178     err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
   179     calculations |> Seq.map (fn calc => (print (Proof.context_of state) calc;
   180         state |> maintain_calculation final calc))
   181   end;
   182 
   183 fun also print = calculate false print;
   184 fun finally print = calculate true print;
   185 
   186 
   187 (* 'moreover' and 'ultimately' *)
   188 
   189 fun collect final print state =
   190   let
   191     val facts = Proof.the_facts (Proof.assert_forward state);
   192     val (initial, thms) =
   193       (case get_calculation state of
   194         None => (true, [])
   195       | Some thms => (false, thms));
   196     val calc = thms @ facts;
   197   in
   198     err_if state (initial andalso final) "No calculation yet";
   199     print (Proof.context_of state) calc;
   200     state |> maintain_calculation final calc
   201   end;
   202 
   203 fun moreover print = collect false print;
   204 fun ultimately print = collect true print;
   205 
   206 
   207 
   208 (** theory setup **)
   209 
   210 val setup =
   211  [GlobalCalculation.init, LocalCalculation.init,
   212   Attrib.add_attributes
   213    [("trans", trans_attr, "declaration of transitivity rule"),
   214     ("sym", sym_attr, "declaration of symmetry rule"),
   215     ("symmetric", (Attrib.no_args symmetric_global, Attrib.no_args symmetric_local),
   216       "resolution with symmetry rule")],
   217   #1 o PureThy.add_thms
   218    [(("", transitive_thm), [trans_add_global]),
   219     (("", symmetric_thm), [sym_add_global])]];
   220 
   221 end;