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