src/Pure/Isar/attrib.ML
author wenzelm
Thu, 08 Jul 1999 18:35:11 +0200
changeset 6933 0890fde41522
parent 6874 747f656e04ec
child 6948 01f3c7866ead
permissions -rw-r--r--
'export';
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/attrib.ML
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     4
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     5
Symbolic theorem attributes.
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     6
*)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     7
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     8
signature BASIC_ATTRIB =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
     9
sig
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    10
  val print_attributes: theory -> unit
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    11
  val Attribute: bstring -> (Args.src -> theory attribute) * (Args.src -> Proof.context attribute)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    12
    -> string -> unit
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    13
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    14
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    15
signature ATTRIB =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    16
sig
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    17
  include BASIC_ATTRIB
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    18
  exception ATTRIB_FAIL of (string * Position.T) * exn
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    19
  val global_attribute: theory -> Args.src -> theory attribute
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    20
  val local_attribute: theory -> Args.src -> Proof.context attribute
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    21
  val local_attribute': Proof.context -> Args.src -> Proof.context attribute
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    22
  val add_attributes: (bstring * ((Args.src -> theory attribute) *
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    23
      (Args.src -> Proof.context attribute)) * string) list -> theory -> theory
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    24
  val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    25
  val global_thms: theory * Args.T list -> thm list * (theory * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    26
  val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    27
  val local_thm: Proof.context * Args.T list -> thm * (Proof.context * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    28
  val local_thms: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    29
  val local_thmss: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    30
  val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> Args.src -> 'a attribute
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    31
  val no_args: 'a attribute -> Args.src -> 'a attribute
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    32
  val setup: (theory -> theory) list
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    33
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    34
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    35
structure Attrib: ATTRIB =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    36
struct
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    37
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    38
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    39
(** attributes theory data **)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    40
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    41
(* data kind 'Isar/attributes' *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    42
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    43
structure AttributesDataArgs =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    44
struct
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    45
  val name = "Isar/attributes";
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    46
  type T =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    47
    {space: NameSpace.T,
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    48
     attrs:
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    49
       ((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute))
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    50
         * string) * stamp) Symtab.table};
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    51
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    52
  val empty = {space = NameSpace.empty, attrs = Symtab.empty};
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6448
diff changeset
    53
  val copy = I;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    54
  val prep_ext = I;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    55
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    56
  fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    57
    {space = NameSpace.merge (space1, space2),
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    58
      attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups =>
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    59
        error ("Attempt to merge different versions of attributes " ^ commas_quote dups)};
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    60
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    61
  fun print _ ({space, attrs}) =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    62
    let
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    63
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
6846
f2380295d4dd cond_extern_table;
wenzelm
parents: 6772
diff changeset
    64
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    65
    in
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    66
      Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
6846
f2380295d4dd cond_extern_table;
wenzelm
parents: 6772
diff changeset
    67
      Pretty.writeln (Pretty.big_list "attributes:"
f2380295d4dd cond_extern_table;
wenzelm
parents: 6772
diff changeset
    68
        (map prt_attr (NameSpace.cond_extern_table space attrs)))
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    69
    end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    70
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    71
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    72
structure AttributesData = TheoryDataFun(AttributesDataArgs);
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    73
val print_attributes = AttributesData.print;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    74
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    75
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    76
(* get global / local attributes *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    77
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    78
exception ATTRIB_FAIL of (string * Position.T) * exn;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    79
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    80
fun gen_attribute which thy =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    81
  let
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    82
    val {space, attrs} = AttributesData.get thy;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    83
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    84
    fun attr src =
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    85
      let
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    86
        val ((raw_name, _), pos) = Args.dest_src src;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    87
        val name = NameSpace.intern space raw_name;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    88
      in
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    89
        (case Symtab.lookup (attrs, name) of
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    90
          None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    91
        | Some ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    92
      end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    93
  in attr end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    94
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    95
val global_attribute = gen_attribute fst;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    96
val local_attribute = gen_attribute snd;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
    97
val local_attribute' = local_attribute o ProofContext.theory_of;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    98
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    99
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   100
(* add_attributes *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   101
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   102
fun add_attributes raw_attrs thy =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   103
  let
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   104
    val full = Sign.full_name (Theory.sign_of thy);
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   105
    val new_attrs =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   106
      map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   107
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   108
    val {space, attrs} = AttributesData.get thy;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   109
    val space' = NameSpace.extend (space, map fst new_attrs);
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   110
    val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups =>
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   111
      error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   112
  in thy |> AttributesData.put {space = space', attrs = attrs'} end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   113
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   114
(*implicit version*)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   115
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   116
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   117
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   118
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   119
(** attribute parsers **)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   120
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   121
(* tags *)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   122
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   123
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   124
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   125
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   126
(* theorems *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   127
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   128
fun gen_thm get attrib app =
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   129
  Scan.depend (fn st => Args.name -- Args.opt_attribs >>
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   130
    (fn (name, srcs) => app ((st, get st name), map (attrib st) srcs)));
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   131
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   132
val global_thm = gen_thm PureThy.get_thm global_attribute Thm.apply_attributes;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   133
val global_thms = gen_thm PureThy.get_thms global_attribute Thm.applys_attributes;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   134
val global_thmss = Scan.repeat global_thms >> flat;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   135
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   136
val local_thm = gen_thm ProofContext.get_thm local_attribute' Thm.apply_attributes;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   137
val local_thms = gen_thm ProofContext.get_thms local_attribute' Thm.applys_attributes;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   138
val local_thmss = Scan.repeat local_thms >> flat;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   139
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   140
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   141
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   142
(** attribute syntax **)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   143
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   144
fun syntax scan src (st, th) =
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   145
  let val (st', f) = Args.syntax "attribute" scan st src
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   146
  in f (st', th) end;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   147
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   148
fun no_args x = syntax (Scan.succeed x);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   149
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   150
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   151
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   152
(** Pure attributes **)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   153
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   154
(* tags *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   155
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   156
fun gen_tag x = syntax (tag >> Drule.tag) x;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   157
fun gen_untag x = syntax (tag >> Drule.untag) x;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   158
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   159
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   160
(* transfer *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   161
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   162
fun gen_transfer theory_of = no_args (Drule.rule_attribute (fn st => Thm.transfer (theory_of st)));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   163
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   164
val global_transfer = gen_transfer I;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   165
val local_transfer = gen_transfer ProofContext.theory_of;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   166
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   167
6772
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   168
(* COMP *)
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   169
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   170
fun comp B (x, A) = (x, A COMP B);
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   171
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   172
fun gen_COMP thm = syntax (thm >> comp);
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   173
val global_COMP = gen_COMP global_thm;
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   174
val local_COMP = gen_COMP local_thm;
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   175
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   176
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   177
(* RS *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   178
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   179
fun resolve (i, B) (x, A) = (x, A RSN (i, B));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   180
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   181
fun gen_RS thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> resolve);
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   182
val global_RS = gen_RS global_thm;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   183
val local_RS = gen_RS local_thm;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   184
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   185
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   186
(* APP *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   187
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   188
fun apply Bs (x, A) = (x, Bs MRS A);
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   189
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   190
val global_APP = syntax (global_thmss >> apply);
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   191
val local_APP = syntax (local_thmss >> apply);
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   192
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   193
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   194
(* where: named instantiations *)
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   195
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   196
fun read_instantiate context_of insts x thm =
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   197
  let
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   198
    val ctxt = context_of x;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   199
    val sign = ProofContext.sign_of ctxt;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   200
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   201
    val vars = Drule.vars_of thm;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   202
    fun get_typ xi =
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   203
      (case assoc (vars, xi) of
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   204
        Some T => T
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   205
      | None => error ("No such variable in theorem: " ^ Syntax.string_of_vname xi));
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   206
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   207
    val (xs, ss) = Library.split_list insts;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   208
    val Ts = map get_typ xs;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   209
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   210
    val (ts, envT) = ProofContext.read_termTs (ctxt |> ProofContext.declare_thm thm) (ss ~~ Ts);
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   211
    val cenvT = map (apsnd (Thm.ctyp_of sign)) envT;
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   212
    val cenv =
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   213
      map (fn (xi, t) => pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t))
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   214
        (gen_distinct (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) (xs ~~ ts));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   215
  in Thm.instantiate (cenvT, cenv) thm end;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   216
6448
932f27366c8f and_list;
wenzelm
parents: 6091
diff changeset
   217
fun insts x = Args.and_list (Scan.lift (Args.var --| Args.$$$ "=" -- Args.name)) x;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   218
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   219
fun gen_where context_of = syntax (insts >> (Drule.rule_attribute o read_instantiate context_of));
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   220
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   221
val global_where = gen_where ProofContext.init;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   222
val local_where = gen_where I;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   223
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   224
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   225
(* with: positional instantiations *)
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   226
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   227
fun read_instantiate' context_of (args, concl_args) x thm =
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   228
  let
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   229
    fun zip_vars _ [] = []
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   230
      | zip_vars (_ :: xs) (None :: opt_ts) = zip_vars xs opt_ts
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   231
      | zip_vars ((x, _) :: xs) (Some t :: opt_ts) = (x, t) :: zip_vars xs opt_ts
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   232
      | zip_vars [] _ = error "More instantiations than variables in theorem";
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   233
    val insts =
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   234
      zip_vars (Drule.vars_of_terms [#prop (Thm.rep_thm thm)]) args @
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   235
      zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   236
  in read_instantiate context_of insts x thm end;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   237
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   238
val concl = Args.$$$ "concl" -- Args.$$$ ":";
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   239
val inst_arg = Scan.unless concl (Args.$$$ "_" >> K None || Args.name >> Some);
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   240
val inst_args = Scan.repeat inst_arg;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   241
fun insts' x = Scan.lift (inst_args -- Scan.optional (concl |-- Args.!!! inst_args) []) x;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   242
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   243
fun gen_with context_of = syntax (insts' >> (Drule.rule_attribute o read_instantiate' context_of));
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   244
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   245
val global_with = gen_with ProofContext.init;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   246
val local_with = gen_with I;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   247
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   248
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   249
(* misc rules *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   250
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   251
fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   252
fun elimify x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   253
6933
0890fde41522 'export';
wenzelm
parents: 6874
diff changeset
   254
fun global_export x = no_args (Drule.rule_attribute (Proof.export_thm o ProofContext.init)) x;
0890fde41522 'export';
wenzelm
parents: 6874
diff changeset
   255
fun local_export x = no_args (Drule.rule_attribute Proof.export_thm) x;
0890fde41522 'export';
wenzelm
parents: 6874
diff changeset
   256
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   257
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   258
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   259
(** theory setup **)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   260
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   261
(* pure_attributes *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   262
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   263
val pure_attributes =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   264
 [("tag", (gen_tag, gen_tag), "tag theorem"),
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   265
  ("untag", (gen_untag, gen_untag), "untag theorem"),
6772
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   266
  ("COMP", (global_COMP, local_COMP), "compose rules (no lifting)"),
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   267
  ("RS", (global_RS, local_RS), "resolve with rule"),
6874
747f656e04ec renamed with/APP to of/OF;
wenzelm
parents: 6846
diff changeset
   268
  ("OF", (global_APP, local_APP), "resolve with rule -- apply rule to rules"),
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   269
  ("where", (global_where, local_where), "named instantiation of theorem"),
6874
747f656e04ec renamed with/APP to of/OF;
wenzelm
parents: 6846
diff changeset
   270
  ("of", (global_with, local_with), "positional instantiation of theorem -- apply rule to terms"),
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   271
  ("standard", (standard, standard), "put theorem into standard form"),
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   272
  ("elimify", (elimify, elimify), "turn destruct rule into elimination rule"),
6933
0890fde41522 'export';
wenzelm
parents: 6874
diff changeset
   273
  ("transfer", (global_transfer, local_transfer), "transfer theorem to this theory"),
0890fde41522 'export';
wenzelm
parents: 6874
diff changeset
   274
  ("export", (global_export, local_export), "export theorem from context")];
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   275
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   276
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   277
(* setup *)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   278
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   279
val setup = [AttributesData.init, add_attributes pure_attributes];
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   280
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   281
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   282
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   283
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   284
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   285
structure BasicAttrib: BASIC_ATTRIB = Attrib;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   286
open BasicAttrib;