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