src/Pure/Isar/attrib.ML
author wenzelm
Thu, 19 Jan 2006 21:22:08 +0100
changeset 18708 4b3dadb4fe33
parent 18678 dd0c569fa43d
child 18734 f5ea6b0d3501
permissions -rw-r--r--
setup: theory -> theory;
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
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    11
  val Attribute: bstring ->
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    12
    (Args.src -> theory attribute) * (Args.src -> ProofContext.context attribute) ->
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    13
    string -> unit
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    14
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    15
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    16
signature ATTRIB =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    17
sig
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    18
  include BASIC_ATTRIB
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    19
  val rule: ('a -> thm -> thm) -> 'a attribute
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    20
  val declaration: (thm -> 'a -> 'a) -> 'a attribute
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    21
  type src
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    22
  val theory: Context.generic attribute -> theory attribute
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    23
  val context: Context.generic attribute -> ProofContext.context attribute
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    24
  val generic: theory attribute * ProofContext.context attribute -> Context.generic attribute
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    25
  val common: (src -> Context.generic attribute) ->
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    26
    (src -> theory attribute) * (src -> ProofContext.context attribute)
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
    27
  exception ATTRIB_FAIL of (string * Position.T) * exn
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    28
  val intern: theory -> xstring -> string
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    29
  val intern_src: theory -> src -> src
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    30
  val global_attribute_i: theory -> src -> theory attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    31
  val global_attribute: theory -> src -> theory attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    32
  val local_attribute_i: theory -> src -> ProofContext.context attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    33
  val local_attribute: theory -> src -> ProofContext.context attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    34
  val context_attribute_i: ProofContext.context -> Args.src -> ProofContext.context attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    35
  val context_attribute: ProofContext.context -> Args.src -> ProofContext.context attribute
7673
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
    36
  val undef_global_attribute: theory attribute
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    37
  val undef_local_attribute: ProofContext.context attribute
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    38
  val generic_attribute_i: theory -> src -> Context.generic attribute
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    39
  val generic_attribute: theory -> src -> Context.generic attribute
17105
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
    40
  val map_specs: ('a -> 'b attribute) ->
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
    41
    (('c * 'a list) * 'd) list -> (('c * 'b attribute list) * 'd) list
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
    42
  val map_facts: ('a -> 'b attribute) ->
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
    43
    (('c * 'a list) * ('d * 'a list) list) list ->
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
    44
    (('c * 'b attribute list) * ('d * 'b attribute list) list) list
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    45
  val crude_closure: ProofContext.context -> src -> src
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    46
  val add_attributes: (bstring * ((src -> theory attribute) *
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    47
      (src -> ProofContext.context attribute)) * string) list -> theory -> theory
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    48
  val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
    49
  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
    50
  val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    51
  val local_thm: ProofContext.context * Args.T list ->
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    52
    thm * (ProofContext.context * Args.T list)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    53
  val local_thms: ProofContext.context * Args.T list ->
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    54
    thm list * (ProofContext.context * Args.T list)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    55
  val local_thmss: ProofContext.context * Args.T list ->
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    56
    thm list * (ProofContext.context * Args.T list)
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    57
  val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    58
  val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    59
  val thmss: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    60
  val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> src -> 'a attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    61
  val no_args: 'a attribute -> src -> 'a attribute
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    62
  val add_del_args: 'a attribute -> 'a attribute -> src -> 'a attribute
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    63
  val attribute: Context.generic attribute -> src
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    64
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    65
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    66
structure Attrib: ATTRIB =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    67
struct
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    68
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    69
val rule = Drule.rule_attribute;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    70
val declaration = Drule.declaration_attribute;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    71
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    72
type src = Args.src;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    73
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    74
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    75
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    76
(** generic attributes **)
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    77
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    78
fun generic (global_att, local_att) =
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    79
   fn (Context.Theory thy, th) => apfst Context.Theory (global_att (thy, th))
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    80
    | (Context.Proof ctxt, th) => apfst Context.Proof (local_att (ctxt, th));
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    81
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    82
fun theory att (thy, th) = apfst Context.the_theory (att (Context.Theory thy, th));
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    83
fun context att (ctxt, th) = apfst Context.the_proof (att (Context.Proof ctxt, th));
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    84
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    85
fun common att = (theory o att, context o att);
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    86
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    87
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
    88
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    89
(** attributes theory data **)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    90
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    91
(* datatype attributes *)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    92
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    93
structure AttributesData = TheoryDataFun
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
    94
(struct
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    95
  val name = "Isar/attributes";
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    96
  type T =
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
    97
    ((((src -> theory attribute) * (src -> ProofContext.context attribute))
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
    98
        * string) * stamp) NameSpace.table;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
    99
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   100
  val empty = NameSpace.empty_table;
6546
995a66249a9b theory data: copy;
wenzelm
parents: 6448
diff changeset
   101
  val copy = I;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   102
  val extend = I;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   103
17496
26535df536ae slight adaptions to library changes
haftmann
parents: 17412
diff changeset
   104
  fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   105
    error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   106
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   107
  fun print _ attribs =
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   108
    let
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   109
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
6846
f2380295d4dd cond_extern_table;
wenzelm
parents: 6772
diff changeset
   110
        [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   111
    in
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   112
      [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
9216
0842edfc8245 removed help_attributes;
wenzelm
parents: 9008
diff changeset
   113
      |> Pretty.chunks |> Pretty.writeln
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   114
    end;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   115
end);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   116
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18678
diff changeset
   117
val _ = Context.add_setup AttributesData.init;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   118
val print_attributes = AttributesData.print;
7611
5b5aba10c8f6 help: unkown theory context;
wenzelm
parents: 7598
diff changeset
   119
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   120
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   121
(* interning *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   122
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   123
val intern = NameSpace.intern o #1 o AttributesData.get;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   124
val intern_src = Args.map_name o intern;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   125
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   126
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   127
(* get global / local attributes *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   128
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   129
exception ATTRIB_FAIL of (string * Position.T) * exn;
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   130
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   131
fun gen_attribute which thy =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   132
  let
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   133
    val attrs = #2 (AttributesData.get thy);
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   134
    fun attr src =
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   135
      let val ((name, _), pos) = Args.dest_src src in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   136
        (case Symtab.lookup attrs name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15456
diff changeset
   137
          NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15456
diff changeset
   138
        | SOME ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   139
      end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   140
  in attr end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   141
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   142
val global_attribute_i = gen_attribute fst;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   143
fun global_attribute thy = global_attribute_i thy o intern_src thy;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   144
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   145
val local_attribute_i = gen_attribute snd;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   146
fun local_attribute thy = local_attribute_i thy o intern_src thy;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   147
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   148
val context_attribute_i = local_attribute_i o ProofContext.theory_of;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   149
val context_attribute = local_attribute o ProofContext.theory_of;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   150
7673
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
   151
val undef_global_attribute: theory attribute =
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
   152
  fn _ => error "attribute undefined in theory context";
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
   153
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   154
val undef_local_attribute: ProofContext.context attribute =
7673
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
   155
  fn _ => error "attribute undefined in proof context";
b8e7fa177d62 added undef_global_attribute, undef_local_attribute;
wenzelm
parents: 7668
diff changeset
   156
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   157
fun generic_attribute thy src = generic (global_attribute thy src, local_attribute thy src);
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   158
fun generic_attribute_i thy src = generic (global_attribute_i thy src, local_attribute_i thy src);
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   159
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   160
17105
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   161
(* attributed declarations *)
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   162
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   163
fun map_specs f = map (apfst (apsnd (map f)));
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   164
fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   165
1b07a176a880 added map_specs/facts operators (from locale.ML);
wenzelm
parents: 16934
diff changeset
   166
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   167
(* crude_closure *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   168
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   169
(*Produce closure without knowing facts in advance! The following
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   170
  should work reasonably well for attribute parsers that do not peek
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   171
  at the thm structure.*)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   172
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   173
fun crude_closure ctxt src =
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18636
diff changeset
   174
 (try (fn () => context_attribute_i ctxt src (ctxt, Drule.asm_rl)) ();
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   175
  Args.closure src);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   176
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   177
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   178
(* add_attributes *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   179
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   180
fun add_attributes raw_attrs thy =
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   181
  let
16141
1e2bed9c06f7 renamed cond_extern to extern;
wenzelm
parents: 15973
diff changeset
   182
    val new_attrs = raw_attrs |> map (fn (name, (f, g), comment) =>
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   183
      (name, (((f, g), comment), stamp ())));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   184
    fun add attrs = NameSpace.extend_table (Sign.naming_of thy) (attrs, new_attrs)
16344
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   185
      handle Symtab.DUPS dups =>
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   186
        error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
a52fe1277902 attribs: NameSpace.table;
wenzelm
parents: 16141
diff changeset
   187
  in AttributesData.map add thy end;
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   188
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   189
(*implicit version*)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   190
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   191
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   192
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   193
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   194
(** attribute parsers **)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   195
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   196
(* tags *)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   197
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   198
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   199
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   200
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   201
(* theorems *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   202
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   203
local
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   204
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   205
fun gen_thm theory_of attrib get pick = Scan.depend (fn st =>
18037
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   206
 (Scan.ahead Args.alt_name -- Args.named_fact (get st o Fact)
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   207
    >> (fn (s, fact) => ("", Fact s, fact)) ||
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   208
  Scan.ahead Args.name -- Args.named_fact (get st o Name) -- Args.thm_sel
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   209
    >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   210
  Scan.ahead Args.name -- Args.named_fact (get st o Name)
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   211
    >> (fn (name, fact) => (name, Name name, fact))) --
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   212
  Args.opt_attribs (intern (theory_of st))
1095d2213b9d syntax for literal facts;
wenzelm
parents: 17756
diff changeset
   213
  >> (fn ((name, thmref, fact), srcs) =>
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   214
    let
16498
9d265401fee0 thmref: Name vs. NameSelection;
wenzelm
parents: 16458
diff changeset
   215
      val ths = PureThy.select_thm thmref fact;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   216
      val atts = map (attrib (theory_of st)) srcs;
18418
bf448d999b7e re-arranged tuples (theory * 'a) to ('a * theory) in Pure
haftmann
parents: 18243
diff changeset
   217
      val (st', ths') = Thm.applys_attributes atts (st, ths);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   218
    in (st', pick name ths') end));
15456
956d6acacf89 Specific theorems in a named list of theorems can now be referred to
berghofe
parents: 15117
diff changeset
   219
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   220
val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   221
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   222
in
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   223
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   224
val global_thm = gen_thm I global_attribute_i PureThy.get_thms PureThy.single_thm;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   225
val global_thms = gen_thm I global_attribute_i PureThy.get_thms (K I);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   226
val global_thmss = Scan.repeat global_thms >> List.concat;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   227
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   228
val local_thm =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   229
  gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms PureThy.single_thm;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   230
val local_thms =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   231
  gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms (K I);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   232
val local_thmss = Scan.repeat local_thms >> List.concat;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   233
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   234
val thm = gen_thm Context.theory_of generic_attribute_i get_thms PureThy.single_thm;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   235
val thms = gen_thm Context.theory_of generic_attribute_i get_thms (K I);
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   236
val thmss = Scan.repeat thms >> List.concat;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   237
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   238
end;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   239
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   240
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   241
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   242
(** attribute syntax **)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   243
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   244
fun syntax scan src (st, th) =
8282
58a33fd5b30c tuned syntax wrapper;
wenzelm
parents: 8164
diff changeset
   245
  let val (st', f) = Args.syntax "attribute" scan src st
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   246
  in f (st', th) end;
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   247
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   248
fun no_args x = syntax (Scan.succeed x);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   249
10034
4bca6b2d2589 tuned args;
wenzelm
parents: 9952
diff changeset
   250
fun add_del_args add del x = syntax
4bca6b2d2589 tuned args;
wenzelm
parents: 9952
diff changeset
   251
  (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
8633
427ead639d8a added add_del_args;
wenzelm
parents: 8496
diff changeset
   252
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   253
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   254
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   255
(** basic attributes **)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   256
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   257
(* tags *)
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   258
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   259
fun tagged x = syntax (tag >> Drule.tag) x;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   260
fun untagged x = syntax (Scan.lift Args.name >> Drule.untag) x;
6772
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   261
111845fce1b7 added COMP attribute;
wenzelm
parents: 6546
diff changeset
   262
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   263
(* rule composition *)
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   264
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   265
val COMP_att =
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   266
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   267
    >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   268
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   269
val THEN_att =
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   270
  syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   271
    >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => A RSN (i, B))));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   272
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   273
val OF_att =
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   274
  syntax (thmss >> (fn Bs => Drule.rule_attribute (fn _ => fn A => Bs MRS A)));
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   275
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   276
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   277
(* read_instantiate: named instantiation of type and term variables *)
14718
wenzelm
parents: 14287
diff changeset
   278
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   279
local
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   280
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   281
fun is_tvar (x, _) = (case Symbol.explode x of "'" :: _ => true | _ => false);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   282
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   283
fun error_var msg xi = error (msg ^ Syntax.string_of_vname xi);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   284
15973
wenzelm
parents: 15926
diff changeset
   285
fun the_sort sorts xi = the (sorts xi)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   286
  handle Option.Option => error_var "No such type variable in theorem: " xi;
10807
ae001d5119fc export read_inst', inst';
wenzelm
parents: 10528
diff changeset
   287
15973
wenzelm
parents: 15926
diff changeset
   288
fun the_type types xi = the (types xi)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   289
  handle Option.Option => error_var "No such variable in theorem: " xi;
14287
f630017ed01c Isar: where attribute supports instantiation of type variables.
ballarin
parents: 14257
diff changeset
   290
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   291
fun unify_types thy types ((unifier, maxidx), (xi, u)) =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   292
  let
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   293
    val T = the_type types xi;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   294
    val U = Term.fastype_of u;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   295
    val maxidx' = Int.max (maxidx, Int.max (#2 xi, Term.maxidx_of_term u));
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   296
  in
16934
9ef19e3c7fdd Sign.typ_unify;
wenzelm
parents: 16498
diff changeset
   297
    Sign.typ_unify thy (T, U) (unifier, maxidx')
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   298
      handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   299
  end;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   300
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   301
fun typ_subst env = apsnd (Term.typ_subst_TVars env);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   302
fun subst env = apsnd (Term.subst_TVars env);
14287
f630017ed01c Isar: where attribute supports instantiation of type variables.
ballarin
parents: 14257
diff changeset
   303
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   304
fun instantiate thy envT env thm =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   305
  let
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15719
diff changeset
   306
    val (_, sorts) = Drule.types_sorts thm;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   307
    fun prepT (a, T) = (Thm.ctyp_of thy (TVar (a, the_sort sorts a)), Thm.ctyp_of thy T);
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   308
    fun prep (xi, t) = pairself (Thm.cterm_of thy) (Var (xi, Term.fastype_of t), t);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   309
  in
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   310
    Drule.instantiate (map prepT (distinct envT),
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15719
diff changeset
   311
      map prep (gen_distinct (fn ((xi, t), (yj, u)) => xi = yj andalso t aconv u) env)) thm
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   312
  end;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   313
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   314
in
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   315
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   316
fun read_instantiate mixed_insts (generic, thm) =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   317
  let
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   318
    val thy = Context.theory_of generic;
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   319
    val ctxt = Context.proof_of generic;
14287
f630017ed01c Isar: where attribute supports instantiation of type variables.
ballarin
parents: 14257
diff changeset
   320
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17496
diff changeset
   321
    val (type_insts, term_insts) = List.partition (is_tvar o fst) (map snd mixed_insts);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   322
    val internal_insts = term_insts |> List.mapPartial
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   323
      (fn (xi, Args.Term t) => SOME (xi, t)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   324
      | (_, Args.Name _) => NONE
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   325
      | (xi, _) => error_var "Term argument expected for " xi);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   326
    val external_insts = term_insts |> List.mapPartial
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   327
      (fn (xi, Args.Name s) => SOME (xi, s) | _ => NONE);
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   328
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   329
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   330
    (* type instantiations *)
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   331
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   332
    val sorts = #2 (Drule.types_sorts thm);
14718
wenzelm
parents: 14287
diff changeset
   333
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   334
    fun readT (xi, arg) =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   335
      let
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   336
        val S = the_sort sorts xi;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   337
        val T =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   338
          (case arg of
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   339
            Args.Name s => ProofContext.read_typ ctxt s
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   340
          | Args.Typ T => T
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   341
          | _ => error_var "Type argument expected for " xi);
10807
ae001d5119fc export read_inst', inst';
wenzelm
parents: 10528
diff changeset
   342
      in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   343
        if Sign.of_sort thy (T, S) then (xi, T)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   344
        else error_var "Incompatible sort for typ instantiation of " xi
10807
ae001d5119fc export read_inst', inst';
wenzelm
parents: 10528
diff changeset
   345
      end;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   346
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   347
    val type_insts' = map readT type_insts;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   348
    val thm' = instantiate thy type_insts' [] thm;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   349
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   350
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   351
    (* internal term instantiations *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   352
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   353
    val types' = #1 (Drule.types_sorts thm');
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15719
diff changeset
   354
    val unifier = map (apsnd snd) (Vartab.dest (#1
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   355
      (Library.foldl (unify_types thy types') ((Vartab.empty, 0), internal_insts))));
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   356
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   357
    val type_insts'' = map (typ_subst unifier) type_insts';
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   358
    val internal_insts'' = map (subst unifier) internal_insts;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   359
    val thm'' = instantiate thy unifier internal_insts'' thm';
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   360
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   361
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   362
    (* external term instantiations *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   363
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   364
    val types'' = #1 (Drule.types_sorts thm'');
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   365
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   366
    val (xs, ss) = split_list external_insts;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   367
    val Ts = map (the_type types'') xs;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   368
    val (ts, inferred) = ProofContext.read_termTs ctxt (K false)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   369
        (K NONE) (K NONE) (Drule.add_used thm'' []) (ss ~~ Ts);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   370
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   371
    val type_insts''' = map (typ_subst inferred) type_insts'';
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   372
    val internal_insts''' = map (subst inferred) internal_insts'';
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   373
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   374
    val external_insts''' = xs ~~ ts;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   375
    val term_insts''' = internal_insts''' @ external_insts''';
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16344
diff changeset
   376
    val thm''' = instantiate thy inferred external_insts''' thm'';
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   377
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   378
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   379
    (* assign internalized values *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   380
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   381
    val _ =
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   382
      mixed_insts |> List.app (fn (arg, (xi, _)) =>
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   383
        if is_tvar xi then
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   384
          Args.assign (SOME (Args.Typ (the (AList.lookup (op =) type_insts''' xi)))) arg
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   385
        else
3d80209e9a53 use AList operations;
wenzelm
parents: 17105
diff changeset
   386
          Args.assign (SOME (Args.Term (the (AList.lookup (op =) term_insts''' xi)))) arg);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   387
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   388
  in (generic, thm''' |> RuleCases.save thm) end;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   389
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   390
end;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   391
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   392
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   393
(* where: named instantiation *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   394
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   395
local
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   396
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   397
val value =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   398
  Args.internal_typ >> Args.Typ ||
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   399
  Args.internal_term >> Args.Term ||
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   400
  Args.name >> Args.Name;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   401
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   402
val inst = Args.var -- (Args.$$$ "=" |-- Args.ahead -- value)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   403
  >> (fn (xi, (a, v)) => (a, (xi, v)));
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   404
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   405
in
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   406
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   407
val where_att = syntax (Args.and_list (Scan.lift inst) >> read_instantiate);
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   408
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   409
end;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   410
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   411
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   412
(* of: positional instantiation (term arguments only) *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   413
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   414
local
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   415
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   416
fun read_instantiate' (args, concl_args) (generic, thm) =
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   417
  let
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   418
    fun zip_vars _ [] = []
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   419
      | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   420
      | zip_vars ((x, _) :: xs) ((a, SOME t) :: rest) = (a, (x, t)) :: zip_vars xs rest
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   421
      | zip_vars [] _ = error "More instantiations than variables in theorem";
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   422
    val insts =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   423
      zip_vars (Drule.vars_of_terms [Thm.prop_of thm]) args @
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   424
      zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   425
  in read_instantiate insts (generic, thm) end;
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   426
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   427
val value =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   428
  Args.internal_term >> Args.Term ||
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   429
  Args.name >> Args.Name;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   430
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   431
val inst = Args.ahead -- Args.maybe value;
10807
ae001d5119fc export read_inst', inst';
wenzelm
parents: 10528
diff changeset
   432
val concl = Args.$$$ "concl" -- Args.colon;
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   433
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   434
val insts =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   435
  Scan.repeat (Scan.unless concl inst) --
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   436
  Scan.optional (concl |-- Scan.repeat inst) [];
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   437
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   438
in
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   439
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   440
val of_att = syntax (Scan.lift insts >> read_instantiate');
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   441
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   442
end;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   443
5912
3f95adea10c0 exception ATTRIB_FAIL;
wenzelm
parents: 5894
diff changeset
   444
13782
44de406a7273 Added rename_abs attribute for renaming bound variables.
berghofe
parents: 13414
diff changeset
   445
(* rename_abs *)
44de406a7273 Added rename_abs attribute for renaming bound variables.
berghofe
parents: 13414
diff changeset
   446
44de406a7273 Added rename_abs attribute for renaming bound variables.
berghofe
parents: 13414
diff changeset
   447
fun rename_abs src = syntax
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   448
  (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
13782
44de406a7273 Added rename_abs attribute for renaming bound variables.
berghofe
parents: 13414
diff changeset
   449
44de406a7273 Added rename_abs attribute for renaming bound variables.
berghofe
parents: 13414
diff changeset
   450
7598
af320257c902 unfold / fold defs;
wenzelm
parents: 7367
diff changeset
   451
(* unfold / fold definitions *)
af320257c902 unfold / fold defs;
wenzelm
parents: 7367
diff changeset
   452
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   453
val unfolded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.rewrite_rule defs))));
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   454
val folded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.fold_rule defs))));
7598
af320257c902 unfold / fold defs;
wenzelm
parents: 7367
diff changeset
   455
af320257c902 unfold / fold defs;
wenzelm
parents: 7367
diff changeset
   456
8368
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   457
(* rule cases *)
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   458
10528
a0483268262d added "consumes" attribute;
wenzelm
parents: 10151
diff changeset
   459
fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
8368
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   460
fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
18236
dd445f5cb28e added case_conclusion attribute;
wenzelm
parents: 18037
diff changeset
   461
fun case_conclusion x =
dd445f5cb28e added case_conclusion attribute;
wenzelm
parents: 18037
diff changeset
   462
  syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
8368
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   463
fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   464
bdc3ee0d8cb6 added 'case_names' and 'params';
wenzelm
parents: 8282
diff changeset
   465
11770
b6bb7a853dd2 moved rulify to ObjectLogic;
wenzelm
parents: 11763
diff changeset
   466
(* rule_format *)
b6bb7a853dd2 moved rulify to ObjectLogic;
wenzelm
parents: 11763
diff changeset
   467
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   468
fun rule_format_att x = syntax (Args.mode "no_asm"
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   469
  >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
11770
b6bb7a853dd2 moved rulify to ObjectLogic;
wenzelm
parents: 11763
diff changeset
   470
b6bb7a853dd2 moved rulify to ObjectLogic;
wenzelm
parents: 11763
diff changeset
   471
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   472
(* misc rules *)
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   473
6091
e3cdbd929a24 eliminated tthm type and Attribute structure;
wenzelm
parents: 5912
diff changeset
   474
fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
9941
fe05af7ec816 renamed atts: rulify to rule_format, elimify to elim_format;
wenzelm
parents: 9902
diff changeset
   475
fun elim_format x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
9216
0842edfc8245 removed help_attributes;
wenzelm
parents: 9008
diff changeset
   476
fun no_vars x = no_args (Drule.rule_attribute (K (#1 o Drule.freeze_thaw))) x;
15926
09fad9a8bc47 Added eta_long attribute.
berghofe
parents: 15828
diff changeset
   477
fun eta_long x = no_args (Drule.rule_attribute (K (Drule.fconv_rule Drule.eta_long_conversion))) x;
5879
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   478
18b8f048d93a several args parsers;
wenzelm
parents: 5823
diff changeset
   479
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   480
(* internal attribute *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   481
15828
ad483e324b59 qualified name Pure.attribute;
wenzelm
parents: 15801
diff changeset
   482
fun attribute att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   483
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   484
fun internal_att x = syntax (Scan.lift Args.internal_attribute) x;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   485
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   486
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   487
(* theory setup *)
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   488
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   489
val _ = Context.add_setup
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18678
diff changeset
   490
 (add_attributes
18636
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   491
   [("tagged", common tagged, "tagged theorem"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   492
    ("untagged", common untagged, "untagged theorem"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   493
    ("COMP", common COMP_att, "direct composition with rules (no lifting)"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   494
    ("THEN", common THEN_att, "resolution with rule"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   495
    ("OF", common OF_att, "rule applied to facts"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   496
    ("where", common where_att, "named instantiation of theorem"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   497
    ("of", common of_att, "rule applied to terms"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   498
    ("rename_abs", common rename_abs, "rename bound variables in abstractions"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   499
    ("unfolded", common unfolded, "unfolded definitions"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   500
    ("folded", common folded, "folded definitions"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   501
    ("standard", common standard, "result put into standard form"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   502
    ("elim_format", common elim_format, "destruct rule turned into elimination rule format"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   503
    ("no_vars", common no_vars, "frozen schematic vars"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   504
    ("eta_long", common eta_long, "put theorem into eta long beta normal form"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   505
    ("consumes", common consumes, "number of consumed facts"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   506
    ("case_names", common case_names, "named rule cases"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   507
    ("case_conclusion", common case_conclusion, "named conclusion of rule cases"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   508
    ("params", common params, "named rule parameters"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   509
    ("atomize", (no_args ObjectLogic.declare_atomize, no_args undef_local_attribute),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   510
      "declaration of atomize rule"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   511
    ("rulify", (no_args ObjectLogic.declare_rulify, no_args undef_local_attribute),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   512
      "declaration of rulify rule"),
cb068cfdcac8 added rule, declaration;
wenzelm
parents: 18418
diff changeset
   513
    ("rule_format", common rule_format_att, "result put into standard rule format"),
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18678
diff changeset
   514
    ("attribute", common internal_att, "internal attribute")]);
5823
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   515
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   516
end;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   517
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   518
structure BasicAttrib: BASIC_ATTRIB = Attrib;
ee7c198a2154 Symbolic theorem attributes.
wenzelm
parents:
diff changeset
   519
open BasicAttrib;