src/Pure/Isar/context_rules.ML
author wenzelm
Mon Dec 03 21:31:55 2001 +0100 (2001-12-03 ago)
changeset 12350 5fad0e7129c3
child 12380 3402d300f5ef
permissions -rw-r--r--
renamed rule_context.ML to context_rules.ML;
     1 (*  Title:      Pure/Isar/context_rules.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Declarations of intro/elim/dest rules in Pure; see
     7 Provers/classical.ML for a more specialized version of the same idea.
     8 *)
     9 
    10 signature CONTEXT_RULES =
    11 sig
    12   type netpair
    13   type T
    14   val netpair_bang: Proof.context -> netpair
    15   val netpair: Proof.context -> netpair
    16   val netpairs: Proof.context -> netpair list
    17   val orderlist: ((int * int) * 'a) list -> 'a list
    18   val orderlist_no_weight: ((int * int) * 'a) list -> 'a list
    19   val print_global_rules: theory -> unit
    20   val print_local_rules: Proof.context -> unit
    21   val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    22   val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
    23   val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
    24   val wrap: Proof.context -> (int -> tactic) -> int -> tactic
    25   val intro_bang_global: int option -> theory attribute
    26   val elim_bang_global: int option -> theory attribute
    27   val dest_bang_global: int option -> theory attribute
    28   val intro_global: int option -> theory attribute
    29   val elim_global: int option -> theory attribute
    30   val dest_global: int option -> theory attribute
    31   val intro_query_global: int option -> theory attribute
    32   val elim_query_global: int option -> theory attribute
    33   val dest_query_global: int option -> theory attribute
    34   val rule_del_global: theory attribute
    35   val intro_bang_local: int option -> Proof.context attribute
    36   val elim_bang_local: int option -> Proof.context attribute
    37   val dest_bang_local: int option -> Proof.context attribute
    38   val intro_local: int option -> Proof.context attribute
    39   val elim_local: int option -> Proof.context attribute
    40   val dest_local: int option -> Proof.context attribute
    41   val intro_query_local: int option -> Proof.context attribute
    42   val elim_query_local: int option -> Proof.context attribute
    43   val dest_query_local: int option -> Proof.context attribute
    44   val rule_del_local: Proof.context attribute
    45   val setup: (theory -> theory) list
    46 end;
    47 
    48 structure ContextRules: CONTEXT_RULES =
    49 struct
    50 
    51 
    52 (** rule declaration contexts **)
    53 
    54 (* rule kinds *)
    55 
    56 val intro_bangK = (0, false);
    57 val elim_bangK = (0, true);
    58 val introK = (1, false);
    59 val elimK = (1, true);
    60 val intro_queryK = (2, false);
    61 val elim_queryK = (2, true);
    62 
    63 val kind_names =
    64  [(intro_bangK, "safe introduction rules (intro!)"),
    65   (elim_bangK, "safe elimination rules (elim!)"),
    66   (introK, "introduction rules (intro)"),
    67   (elimK, "elimination rules (elim)"),
    68   (intro_queryK, "extra introduction rules (intro?)"),
    69   (elim_queryK, "extra elimination rules (elim?)")];
    70 
    71 val rule_kinds = map #1 kind_names;
    72 val rule_indexes = distinct (map #1 rule_kinds);
    73 
    74 
    75 (* raw data *)
    76 
    77 type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
    78 val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
    79 
    80 datatype T = Rules of
    81  {next: int,
    82   rules: (int * ((int * bool) * thm)) list,
    83   netpairs: netpair list,
    84   wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
    85     (((int -> tactic) -> int -> tactic) * stamp) list};
    86 
    87 fun make_rules next rules netpairs wrappers =
    88   Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
    89 
    90 fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
    91   let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in
    92     make_rules (next - 1) ((w, ((i, b), th)) :: rules)
    93       (map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
    94   end;
    95 
    96 fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
    97   let
    98     fun eq_th (_, (_, th')) = Thm.eq_thm (th, th');
    99     fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair;
   100   in
   101     if not (exists eq_th rules) then rs
   102     else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
   103   end;
   104 
   105 fun print_rules prt x (Rules {rules, ...}) =
   106   let
   107     fun prt_kind (i, b) =
   108       Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":")
   109         (mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None)
   110           (sort (int_ord o pairself fst) rules));
   111   in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
   112 
   113 
   114 (* theory and proof data *)
   115 
   116 structure GlobalRulesArgs =
   117 struct
   118   val name = "Isar/rule_context";
   119   type T = T;
   120 
   121   val empty = make_rules ~1 [] empty_netpairs ([], []);
   122   val copy = I;
   123   val prep_ext = I;
   124 
   125   fun merge (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
   126       Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
   127     let
   128       val wrappers = (gen_merge_lists' eq_snd ws1 ws2, gen_merge_lists' eq_snd ws1' ws2');
   129       val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
   130           k1 = k2 andalso Thm.eq_thm (th1, th2)) rules1 rules2;
   131       val next = ~ (length rules);
   132       val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) =>
   133           map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps)
   134         (empty_netpairs, next upto ~1 ~~ rules);
   135     in make_rules (next - 1) rules netpairs wrappers end;
   136 
   137   val print = print_rules Display.pretty_thm_sg;
   138 end;
   139 
   140 structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
   141 val print_global_rules = GlobalRules.print;
   142 
   143 structure LocalRulesArgs =
   144 struct
   145   val name = GlobalRulesArgs.name;
   146   type T = GlobalRulesArgs.T;
   147   val init = GlobalRules.get;
   148   val print = print_rules ProofContext.pretty_thm;
   149 end;
   150 
   151 structure LocalRules = ProofDataFun(LocalRulesArgs);
   152 val print_local_rules = LocalRules.print;
   153 
   154 
   155 (* access data *)
   156 
   157 fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end;
   158 val netpair_bang = hd o netpairs;
   159 val netpair = hd o tl o netpairs;
   160 
   161 
   162 fun untaglist [] = []
   163   | untaglist [(k : int * int, x)] = [x]
   164   | untaglist ((k, x) :: (rest as (k', x') :: _)) =
   165       if k = k' then untaglist rest
   166       else x :: untaglist rest;
   167 
   168 fun orderlist brls = untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
   169 fun orderlist_no_weight brls = untaglist (sort (int_ord o pairself (snd o fst)) brls);
   170 
   171 
   172 fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
   173   make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
   174 
   175 val addSWrapper = gen_add_wrapper Library.apfst;
   176 val addWrapper = gen_add_wrapper Library.apsnd;
   177 
   178 
   179 fun gen_wrap which ctxt =
   180   let val Rules {wrappers, ...} = LocalRules.get ctxt
   181   in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end;
   182 
   183 val Swrap = gen_wrap #1;
   184 val wrap = gen_wrap #2;
   185 
   186 
   187 
   188 (** attributes **)
   189 
   190 (* add and del rules *)
   191 
   192 local
   193 
   194 fun del map_data (x, th) =
   195   (map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
   196 
   197 fun add k view map_data opt_w =
   198   (fn (x, th) => (map_data (add_rule k opt_w th) x, th)) o del map_data;
   199 
   200 in
   201 
   202 val intro_bang_global  = add intro_bangK I GlobalRules.map;
   203 val elim_bang_global   = add elim_bangK I GlobalRules.map;
   204 val dest_bang_global   = add elim_bangK Tactic.make_elim GlobalRules.map;
   205 val intro_global       = add introK I GlobalRules.map;
   206 val elim_global        = add elimK I GlobalRules.map;
   207 val dest_global        = add elimK Tactic.make_elim GlobalRules.map;
   208 val intro_query_global = add intro_queryK I GlobalRules.map;
   209 val elim_query_global  = add elim_queryK I GlobalRules.map;
   210 val dest_query_global  = add elim_queryK Tactic.make_elim GlobalRules.map;
   211 val rule_del_global    = del GlobalRules.map;
   212 
   213 val intro_bang_local   = add intro_bangK I LocalRules.map;
   214 val elim_bang_local    = add elim_bangK I LocalRules.map;
   215 val dest_bang_local    = add elim_bangK Tactic.make_elim LocalRules.map;
   216 val intro_local        = add introK I LocalRules.map;
   217 val elim_local         = add elimK I LocalRules.map;
   218 val dest_local         = add elimK Tactic.make_elim LocalRules.map;
   219 val intro_query_local  = add intro_queryK I LocalRules.map;
   220 val elim_query_local   = add elim_queryK I LocalRules.map;
   221 val dest_query_local   = add elim_queryK Tactic.make_elim LocalRules.map;
   222 val rule_del_local     = del LocalRules.map;
   223 
   224 end;
   225 
   226 
   227 (* concrete syntax *)
   228 
   229 fun add_args a b c x = Attrib.syntax
   230   (Scan.lift (Scan.option Args.nat --
   231     (Args.bang >> K a || Args.query >> K c || Scan.succeed b) >> op |>)) x;
   232 
   233 fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
   234 
   235 
   236 val rule_atts =
   237  [("intro",
   238    (add_args intro_bang_global intro_global intro_query_global,
   239     add_args intro_bang_local intro_local intro_query_local),
   240     "declaration of introduction rule"),
   241   ("elim",
   242    (add_args elim_bang_global elim_global elim_query_global,
   243     add_args elim_bang_local elim_local elim_query_local),
   244     "declaration of elimination rule"),
   245   ("dest",
   246    (add_args dest_bang_global dest_global dest_query_global,
   247     add_args dest_bang_local dest_local dest_query_local),
   248     "declaration of destruction rule"),
   249   ("rule", (del_args rule_del_global, del_args rule_del_local),
   250     "remove declaration of intro/elim/dest rule")];
   251 
   252 
   253 
   254 (** theory setup **)
   255 
   256 val setup =
   257  [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts];
   258 
   259 
   260 end;