src/Pure/Isar/induct_attrib.ML
author wenzelm
Tue May 07 14:26:32 2002 +0200 (2002-05-07)
changeset 13105 3d1e7a199bdc
parent 12381 5177845a34f5
child 14981 e73f8140af78
permissions -rw-r--r--
use eq_thm_prop instead of slightly inadequate eq_thm;
     1 (*  Title:      Pure/Isar/induct_attrib.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Declaration of rules for cases and induction.
     7 *)
     8 
     9 signature INDUCT_ATTRIB =
    10 sig
    11   val vars_of: term -> term list
    12   val dest_global_rules: theory ->
    13     {type_cases: (string * thm) list, set_cases: (string * thm) list,
    14       type_induct: (string * thm) list, set_induct: (string * thm) list}
    15   val print_global_rules: theory -> unit
    16   val dest_local_rules: Proof.context ->
    17     {type_cases: (string * thm) list, set_cases: (string * thm) list,
    18       type_induct: (string * thm) list, set_induct: (string * thm) list}
    19   val print_local_rules: Proof.context -> unit
    20   val lookup_casesT : Proof.context -> string -> thm option
    21   val lookup_casesS : Proof.context -> string -> thm option
    22   val lookup_inductT : Proof.context -> string -> thm option
    23   val lookup_inductS : Proof.context -> string -> thm option
    24   val find_casesT: Proof.context -> typ -> thm list
    25   val find_casesS: Proof.context -> thm -> thm list
    26   val find_inductT: Proof.context -> typ -> thm list
    27   val find_inductS: Proof.context -> thm -> thm list
    28   val cases_type_global: string -> theory attribute
    29   val cases_set_global: string -> theory attribute
    30   val cases_type_local: string -> Proof.context attribute
    31   val cases_set_local: string -> Proof.context attribute
    32   val induct_type_global: string -> theory attribute
    33   val induct_set_global: string -> theory attribute
    34   val induct_type_local: string -> Proof.context attribute
    35   val induct_set_local: string -> Proof.context attribute
    36   val casesN: string
    37   val inductN: string
    38   val typeN: string
    39   val setN: string
    40   val setup: (theory -> theory) list
    41 end;
    42 
    43 structure InductAttrib: INDUCT_ATTRIB =
    44 struct
    45 
    46 
    47 (** misc utils **)
    48 
    49 (* encode_type -- for indexing purposes *)
    50 
    51 fun encode_type (Type (c, Ts)) = Term.list_comb (Const (c, dummyT), map encode_type Ts)
    52   | encode_type (TFree (a, _)) = Free (a, dummyT)
    53   | encode_type (TVar (a, _)) = Var (a, dummyT);
    54 
    55 
    56 (* variables -- ordered left-to-right, preferring right *)
    57 
    58 local
    59 
    60 fun rev_vars_of tm =
    61   Term.foldl_aterms (fn (ts, t as Var _) => t :: ts | (ts, _) => ts) ([], tm)
    62   |> Library.distinct;
    63 
    64 val mk_var = encode_type o #2 o Term.dest_Var;
    65 
    66 in
    67 
    68 val vars_of = rev o rev_vars_of;
    69 
    70 fun first_var thm = mk_var (hd (vars_of (hd (Thm.prems_of thm)))) handle LIST _ =>
    71   raise THM ("No variables in first premise of rule", 0, [thm]);
    72 
    73 fun last_var thm = mk_var (hd (rev_vars_of (Thm.concl_of thm))) handle LIST _ =>
    74   raise THM ("No variables in conclusion of rule", 0, [thm]);
    75 
    76 end;
    77 
    78 
    79 
    80 (** global and local induct data **)
    81 
    82 (* rules *)
    83 
    84 type rules = (string * thm) NetRules.T;
    85 
    86 val init_rules =
    87   NetRules.init (fn ((s1: string, th1), (s2, th2)) => s1 = s2 andalso
    88     Drule.eq_thm_prop (th1, th2));
    89 
    90 fun lookup_rule (rs: rules) name = Library.assoc (NetRules.rules rs, name);
    91 
    92 fun print_rules prt kind x rs =
    93   let val thms = map snd (NetRules.rules rs)
    94   in Pretty.writeln (Pretty.big_list kind (map (prt x) thms)) end;
    95 
    96 fun print_all_rules prt x ((casesT, casesS), (inductT, inductS)) =
    97  (print_rules prt "induct type:" x inductT;
    98   print_rules prt "induct set:" x inductS;
    99   print_rules prt "cases type:" x casesT;
   100   print_rules prt "cases set:" x casesS);
   101 
   102 
   103 (* theory data kind 'Isar/induction' *)
   104 
   105 structure GlobalInductArgs =
   106 struct
   107   val name = "Isar/induction";
   108   type T = (rules * rules) * (rules * rules);
   109 
   110   val empty =
   111     ((init_rules (first_var o #2), init_rules (Thm.major_prem_of o #2)),
   112      (init_rules (last_var o #2), init_rules (Thm.major_prem_of o #2)));
   113   val copy = I;
   114   val prep_ext = I;
   115   fun merge (((casesT1, casesS1), (inductT1, inductS1)),
   116       ((casesT2, casesS2), (inductT2, inductS2))) =
   117     ((NetRules.merge (casesT1, casesT2), NetRules.merge (casesS1, casesS2)),
   118       (NetRules.merge (inductT1, inductT2), NetRules.merge (inductS1, inductS2)));
   119 
   120   fun print x = print_all_rules Display.pretty_thm_sg x;
   121 
   122   fun dest ((casesT, casesS), (inductT, inductS)) =
   123     {type_cases = NetRules.rules casesT,
   124      set_cases = NetRules.rules casesS,
   125      type_induct = NetRules.rules inductT,
   126      set_induct = NetRules.rules inductS};
   127 end;
   128 
   129 structure GlobalInduct = TheoryDataFun(GlobalInductArgs);
   130 val print_global_rules = GlobalInduct.print;
   131 val dest_global_rules = GlobalInductArgs.dest o GlobalInduct.get;
   132 
   133 
   134 (* proof data kind 'Isar/induction' *)
   135 
   136 structure LocalInductArgs =
   137 struct
   138   val name = "Isar/induction";
   139   type T = GlobalInductArgs.T;
   140 
   141   fun init thy = GlobalInduct.get thy;
   142   fun print x = print_all_rules ProofContext.pretty_thm x;
   143 end;
   144 
   145 structure LocalInduct = ProofDataFun(LocalInductArgs);
   146 val print_local_rules = LocalInduct.print;
   147 val dest_local_rules = GlobalInductArgs.dest o LocalInduct.get;
   148 
   149 
   150 (* access rules *)
   151 
   152 val lookup_casesT = lookup_rule o #1 o #1 o LocalInduct.get;
   153 val lookup_casesS = lookup_rule o #2 o #1 o LocalInduct.get;
   154 val lookup_inductT = lookup_rule o #1 o #2 o LocalInduct.get;
   155 val lookup_inductS = lookup_rule o #2 o #2 o LocalInduct.get;
   156 
   157 
   158 fun find_rules which how ctxt x =
   159   map snd (NetRules.retrieve (which (LocalInduct.get ctxt)) (how x));
   160 
   161 val find_casesT = find_rules (#1 o #1) encode_type;
   162 val find_casesS = find_rules (#2 o #1) Thm.concl_of;
   163 val find_inductT = find_rules (#1 o #2) encode_type;
   164 val find_inductS = find_rules (#2 o #2) Thm.concl_of;
   165 
   166 
   167 
   168 (** attributes **)
   169 
   170 local
   171 
   172 fun mk_att f g h name arg =
   173   let val (x, thm) = h arg in (f (g (name, thm)) x, thm) end;
   174 
   175 fun add_casesT rule x = apfst (apfst (NetRules.insert rule)) x;
   176 fun add_casesS rule x = apfst (apsnd (NetRules.insert rule)) x;
   177 fun add_inductT rule x = apsnd (apfst (NetRules.insert rule)) x;
   178 fun add_inductS rule x = apsnd (apsnd (NetRules.insert rule)) x;
   179 
   180 fun consumes0 x = RuleCases.consumes_default 0 x;
   181 fun consumes1 x = RuleCases.consumes_default 1 x;
   182 
   183 in
   184 
   185 val cases_type_global = mk_att GlobalInduct.map add_casesT consumes0;
   186 val cases_set_global = mk_att GlobalInduct.map add_casesS consumes1;
   187 val induct_type_global = mk_att GlobalInduct.map add_inductT consumes0;
   188 val induct_set_global = mk_att GlobalInduct.map add_inductS consumes1;
   189 
   190 val cases_type_local = mk_att LocalInduct.map add_casesT consumes0;
   191 val cases_set_local = mk_att LocalInduct.map add_casesS consumes1;
   192 val induct_type_local = mk_att LocalInduct.map add_inductT consumes0;
   193 val induct_set_local = mk_att LocalInduct.map add_inductS consumes1;
   194 
   195 end;
   196 
   197 
   198 (** concrete syntax **)
   199 
   200 val casesN = "cases";
   201 val inductN = "induct";
   202 
   203 val typeN = "type";
   204 val setN = "set";
   205 
   206 local
   207 
   208 fun spec k cert =
   209   (Args.$$$ k -- Args.colon) |-- Args.!!! (Args.name >> cert) ||
   210   Args.$$$ k >> K "";
   211 
   212 fun attrib sign_of add_type add_set = Scan.depend (fn x =>
   213   let val sg = sign_of x in
   214     spec typeN (Sign.certify_tyname sg o Sign.intern_tycon sg) >> add_type ||
   215     spec setN (Sign.certify_const sg o Sign.intern_const sg) >> add_set
   216   end >> pair x);
   217 
   218 in
   219 
   220 val cases_attr =
   221   (Attrib.syntax (attrib Theory.sign_of cases_type_global cases_set_global),
   222    Attrib.syntax (attrib ProofContext.sign_of cases_type_local cases_set_local));
   223 
   224 val induct_attr =
   225   (Attrib.syntax (attrib Theory.sign_of induct_type_global induct_set_global),
   226    Attrib.syntax (attrib ProofContext.sign_of induct_type_local induct_set_local));
   227 
   228 end;
   229 
   230 
   231 
   232 (** theory setup **)
   233 
   234 val setup =
   235   [GlobalInduct.init, LocalInduct.init,
   236    Attrib.add_attributes
   237     [(casesN, cases_attr, "declaration of cases rule for type or set"),
   238      (inductN, induct_attr, "declaration of induction rule for type or set")]];
   239 
   240 end;