src/Pure/Isar/args.ML
author wenzelm
Tue Jan 10 19:33:34 2006 +0100 (2006-01-10)
changeset 18635 58bbff56a914
parent 18037 1095d2213b9d
child 18728 6790126ab5f6
permissions -rw-r--r--
added generic syntax;
mk_attribute: generic;
wenzelm@5822
     1
(*  Title:      Pure/Isar/args.ML
wenzelm@5822
     2
    ID:         $Id$
wenzelm@5822
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5822
     4
wenzelm@15703
     5
Concrete argument syntax of attributes, methods etc. -- with special
wenzelm@15703
     6
support for embedded values and static binding.
wenzelm@5822
     7
*)
wenzelm@5822
     8
wenzelm@5822
     9
signature ARGS =
wenzelm@5822
    10
sig
wenzelm@15703
    11
  datatype value = Name of string | Typ of typ | Term of term | Fact of thm list |
wenzelm@18635
    12
    Attribute of Context.generic attribute
wenzelm@5822
    13
  type T
wenzelm@9748
    14
  val string_of: T -> string
wenzelm@15703
    15
  val mk_ident: string * Position.T -> T
wenzelm@15703
    16
  val mk_string: string * Position.T -> T
wenzelm@18037
    17
  val mk_alt_string: string * Position.T -> T
wenzelm@15703
    18
  val mk_keyword: string * Position.T -> T
wenzelm@15703
    19
  val mk_name: string -> T
wenzelm@15703
    20
  val mk_typ: typ -> T
wenzelm@15703
    21
  val mk_term: term -> T
wenzelm@15703
    22
  val mk_fact: thm list -> T
wenzelm@18635
    23
  val mk_attribute: Context.generic attribute -> T
wenzelm@5822
    24
  val stopper: T * (T -> bool)
wenzelm@5822
    25
  val not_eof: T -> bool
wenzelm@15703
    26
  type src
wenzelm@15703
    27
  val src: (string * T list) * Position.T -> src
wenzelm@15703
    28
  val dest_src: src -> (string * T list) * Position.T
wenzelm@15703
    29
  val map_name: (string -> string) -> src -> src
wenzelm@15703
    30
  val map_values: (string -> string) -> (typ -> typ) -> (term -> term) -> (thm -> thm)
wenzelm@15703
    31
    -> src -> src
wenzelm@15703
    32
  val assignable: src -> src
wenzelm@15703
    33
  val assign: value option -> T -> unit
wenzelm@15703
    34
  val closure: src -> src
wenzelm@5878
    35
  val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
wenzelm@5878
    36
  val !!! : (T list -> 'a) -> T list -> 'a
wenzelm@5822
    37
  val $$$ : string -> T list -> string * T list
wenzelm@10035
    38
  val add: T list -> string * T list
wenzelm@10035
    39
  val del: T list -> string * T list
wenzelm@8803
    40
  val colon: T list -> string * T list
wenzelm@10035
    41
  val query: T list -> string * T list
wenzelm@10035
    42
  val bang: T list -> string * T list
wenzelm@10035
    43
  val query_colon: T list -> string * T list
wenzelm@10035
    44
  val bang_colon: T list -> string * T list
wenzelm@8803
    45
  val parens: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@10150
    46
  val bracks: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@9809
    47
  val mode: string -> 'a * T list -> bool * ('a * T list)
wenzelm@15703
    48
  val maybe: (T list -> 'a * T list) -> T list -> 'a option * T list
wenzelm@5822
    49
  val name: T list -> string * T list
wenzelm@18037
    50
  val alt_name: T list -> string * T list
wenzelm@16140
    51
  val symbol: T list -> string * T list
wenzelm@17064
    52
  val liberal_name: T list -> string * T list
wenzelm@5878
    53
  val nat: T list -> int * T list
wenzelm@9538
    54
  val int: T list -> int * T list
wenzelm@5878
    55
  val var: T list -> indexname * T list
wenzelm@15703
    56
  val list: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@15703
    57
  val list1: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@6447
    58
  val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    59
  val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    60
  val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    61
  val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@15703
    62
  val ahead: T list -> T * T list
wenzelm@15703
    63
  val internal_name: T list -> string * T list
wenzelm@15703
    64
  val internal_typ: T list -> typ * T list
wenzelm@15703
    65
  val internal_term: T list -> term * T list
wenzelm@15703
    66
  val internal_fact: T list -> thm list * T list
wenzelm@18635
    67
  val internal_attribute: T list -> Context.generic attribute * T list
wenzelm@15703
    68
  val named_name: (string -> string) -> T list -> string * T list
wenzelm@15703
    69
  val named_typ: (string -> typ) -> T list -> typ * T list
wenzelm@15703
    70
  val named_term: (string -> term) -> T list -> term * T list
wenzelm@15703
    71
  val named_fact: (string -> thm list) -> T list -> thm list * T list
wenzelm@16343
    72
  val global_typ_abbrev: theory * T list -> typ * (theory * T list)
wenzelm@5878
    73
  val global_typ: theory * T list -> typ * (theory * T list)
wenzelm@5878
    74
  val global_term: theory * T list -> term * (theory * T list)
wenzelm@5878
    75
  val global_prop: theory * T list -> term * (theory * T list)
wenzelm@16343
    76
  val local_typ_abbrev: ProofContext.context * T list -> typ * (ProofContext.context * T list)
ballarin@15596
    77
  val local_typ: ProofContext.context * T list -> typ * (ProofContext.context * T list)
ballarin@15596
    78
  val local_term: ProofContext.context * T list -> term * (ProofContext.context * T list)
ballarin@15596
    79
  val local_prop: ProofContext.context * T list -> term * (ProofContext.context * T list)
wenzelm@18635
    80
  val typ_abbrev: Context.generic * T list -> typ * (Context.generic * T list)
wenzelm@18635
    81
  val typ: Context.generic * T list -> typ * (Context.generic * T list)
wenzelm@18635
    82
  val term: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@18635
    83
  val prop: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@15703
    84
  val global_tyname: theory * T list -> string * (theory * T list)
wenzelm@15703
    85
  val global_const: theory * T list -> string * (theory * T list)
wenzelm@15703
    86
  val local_tyname: ProofContext.context * T list -> string * (ProofContext.context * T list)
wenzelm@15703
    87
  val local_const: ProofContext.context * T list -> string * (ProofContext.context * T list)
wenzelm@18635
    88
  val tyname: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@18635
    89
  val const: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@15703
    90
  val thm_sel: T list -> PureThy.interval list * T list
ballarin@15596
    91
  val bang_facts: ProofContext.context * T list -> thm list * (ProofContext.context * T list)
wenzelm@8536
    92
  val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
wenzelm@8536
    93
    -> ((int -> tactic) -> tactic) * ('a * T list)
wenzelm@15703
    94
  val attribs: (string -> string) -> T list -> src list * T list
wenzelm@15703
    95
  val opt_attribs: (string -> string) -> T list -> src list * T list
wenzelm@8282
    96
  val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> src -> 'a -> 'a * 'b
wenzelm@15703
    97
  val pretty_src: ProofContext.context -> src -> Pretty.T
wenzelm@15703
    98
  val pretty_attribs: ProofContext.context -> src list -> Pretty.T list
wenzelm@5822
    99
end;
wenzelm@5822
   100
wenzelm@5822
   101
structure Args: ARGS =
wenzelm@5822
   102
struct
wenzelm@5822
   103
wenzelm@5822
   104
wenzelm@5822
   105
(** datatype T **)
wenzelm@5822
   106
wenzelm@15703
   107
(*An argument token is a symbol (with raw string value), together with
wenzelm@15703
   108
  an optional assigned internal value.  Note that an assignable ref
wenzelm@15703
   109
  designates an intermediate state of internalization -- it is NOT
wenzelm@15703
   110
  meant to persist.*)
wenzelm@15703
   111
wenzelm@18037
   112
datatype kind = Ident | String | AltString | Keyword | EOF;
wenzelm@15703
   113
wenzelm@15703
   114
type symbol = kind * string * Position.T;
wenzelm@5822
   115
wenzelm@15703
   116
datatype value =
wenzelm@15703
   117
  Name of string |
wenzelm@15703
   118
  Typ of typ |
wenzelm@15703
   119
  Term of term |
wenzelm@15703
   120
  Fact of thm list |
wenzelm@18635
   121
  Attribute of Context.generic attribute;
wenzelm@15703
   122
wenzelm@15703
   123
datatype slot =
wenzelm@15703
   124
  Empty |
wenzelm@15703
   125
  Value of value option |
wenzelm@15703
   126
  Assignable of value option ref;
wenzelm@15703
   127
wenzelm@15703
   128
datatype T = Arg of symbol * slot;
wenzelm@5822
   129
wenzelm@15703
   130
fun string_of (Arg ((Ident, x, _), _)) = x
wenzelm@18037
   131
  | string_of (Arg ((String, x, _), _)) = quote x
wenzelm@18037
   132
  | string_of (Arg ((AltString, x, _), _)) = enclose "`" "`" x
wenzelm@15703
   133
  | string_of (Arg ((Keyword, x, _), _)) = x
wenzelm@15703
   134
  | string_of (Arg ((EOF, _, _), _)) = "";
wenzelm@15703
   135
wenzelm@15703
   136
fun sym_of (Arg ((_, x, _), _)) = x;
wenzelm@15703
   137
fun pos_of (Arg ((_, _, pos), _)) = pos;
wenzelm@15703
   138
wenzelm@15703
   139
wenzelm@15703
   140
(* basic constructors *)
wenzelm@5822
   141
wenzelm@15703
   142
fun mk_symbol k (x, p) = Arg ((k, x, p), Empty);
wenzelm@15703
   143
fun mk_value k v = Arg ((Keyword, k, Position.none), Value (SOME v));
wenzelm@9748
   144
wenzelm@15703
   145
val mk_ident = mk_symbol Ident;
wenzelm@15703
   146
val mk_string = mk_symbol String;
wenzelm@18037
   147
val mk_alt_string = mk_symbol AltString;
wenzelm@15703
   148
val mk_keyword = mk_symbol Keyword;
wenzelm@15703
   149
val mk_name = mk_value "<name>" o Name;
wenzelm@15703
   150
val mk_typ = mk_value "<typ>" o Typ;
wenzelm@15703
   151
val mk_term = mk_value "<term>" o Term;
wenzelm@15703
   152
val mk_fact = mk_value "<fact>" o Fact;
wenzelm@15703
   153
val mk_attribute = mk_value "<attribute>" o Attribute;
wenzelm@5822
   154
wenzelm@5822
   155
wenzelm@5822
   156
(* eof *)
wenzelm@5822
   157
wenzelm@15703
   158
val eof = mk_symbol EOF ("", Position.none);
wenzelm@5822
   159
wenzelm@15703
   160
fun is_eof (Arg ((EOF, _, _), _)) = true
wenzelm@5822
   161
  | is_eof _ = false;
wenzelm@5822
   162
wenzelm@5822
   163
val stopper = (eof, is_eof);
wenzelm@5822
   164
val not_eof = not o is_eof;
wenzelm@5822
   165
wenzelm@5822
   166
wenzelm@5822
   167
wenzelm@15703
   168
(** datatype src **)
wenzelm@15703
   169
wenzelm@15703
   170
datatype src = Src of (string * T list) * Position.T;
wenzelm@15703
   171
wenzelm@15703
   172
val src = Src;
wenzelm@15703
   173
fun dest_src (Src src) = src;
wenzelm@15703
   174
wenzelm@15703
   175
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
wenzelm@15703
   176
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
wenzelm@15703
   177
wenzelm@15703
   178
wenzelm@15703
   179
(* values *)
wenzelm@15703
   180
wenzelm@15703
   181
fun map_value f (Arg (s, Value (SOME v))) = Arg (s, Value (SOME (f v)))
wenzelm@15703
   182
  | map_value _ arg = arg;
wenzelm@15703
   183
wenzelm@15703
   184
fun map_values f g h i = map_args (map_value
wenzelm@15703
   185
  (fn Name s => Name (f s)
wenzelm@15703
   186
    | Typ T => Typ (g T)
wenzelm@15703
   187
    | Term t => Term (h t)
wenzelm@15703
   188
    | Fact ths => Fact (map i ths)
wenzelm@15703
   189
    | Attribute att => Attribute att));
wenzelm@15703
   190
wenzelm@15703
   191
wenzelm@15703
   192
(* static binding *)
wenzelm@15703
   193
wenzelm@15703
   194
(*1st stage: make empty slots assignable*)
wenzelm@15703
   195
val assignable =
wenzelm@15703
   196
  map_args (fn Arg (s, Empty) => Arg (s, Assignable (ref NONE)) | arg => arg);
wenzelm@15703
   197
wenzelm@15703
   198
(*2nd stage: assign values as side-effect of scanning*)
wenzelm@15703
   199
fun assign v (arg as Arg (_, Assignable r)) = r := v
wenzelm@15703
   200
  | assign _ _ = ();
wenzelm@15703
   201
wenzelm@15703
   202
val ahead = Scan.ahead (Scan.one not_eof);
wenzelm@15703
   203
fun touch scan = ahead -- scan >> (fn (arg, y) => (assign NONE arg; y));
wenzelm@15703
   204
wenzelm@15703
   205
(*3rd stage: static closure of final values*)
wenzelm@15703
   206
val closure =
wenzelm@15703
   207
  map_args (fn Arg (s, Assignable (ref v)) => Arg (s, Value v) | arg => arg);
wenzelm@15703
   208
wenzelm@15703
   209
wenzelm@15703
   210
wenzelm@5822
   211
(** scanners **)
wenzelm@5822
   212
wenzelm@5878
   213
(* position *)
wenzelm@5878
   214
wenzelm@15703
   215
fun position scan =
wenzelm@15703
   216
  (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
   217
wenzelm@5878
   218
wenzelm@5878
   219
(* cut *)
wenzelm@5878
   220
wenzelm@5878
   221
fun !!! scan =
wenzelm@5878
   222
  let
wenzelm@5878
   223
    fun get_pos [] = " (past end-of-text!)"
wenzelm@15703
   224
      | get_pos (arg :: _) = Position.str_of (pos_of arg);
wenzelm@5878
   225
skalberg@15531
   226
    fun err (args, NONE) = "Argument syntax error" ^ get_pos args
skalberg@15531
   227
      | err (args, SOME msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
   228
  in Scan.!! err scan end;
wenzelm@5878
   229
wenzelm@5878
   230
wenzelm@5822
   231
(* basic *)
wenzelm@5822
   232
wenzelm@15703
   233
fun some_ident f =
wenzelm@15703
   234
  touch (Scan.some (fn Arg ((Ident, x, _), _) => f x | _ => NONE));
wenzelm@15703
   235
wenzelm@15703
   236
val named =
wenzelm@15703
   237
  touch (Scan.one (fn Arg ((k, _, _), _) => k = Ident orelse k = String));
wenzelm@15703
   238
wenzelm@18037
   239
val alt_string =
wenzelm@18037
   240
  touch (Scan.one (fn Arg ((k, _, _), _) => k = AltString));
wenzelm@18037
   241
wenzelm@15703
   242
val symbolic =
wenzelm@15703
   243
  touch (Scan.one (fn Arg ((k, x, _), _) => k = Keyword andalso OuterLex.is_sid x));
wenzelm@15703
   244
wenzelm@15703
   245
fun &&& x =
wenzelm@15703
   246
  touch (Scan.one (fn Arg ((k, y, _), _) => k = Keyword andalso x = y));
wenzelm@15703
   247
wenzelm@15703
   248
fun $$$ x =
wenzelm@16140
   249
  touch (Scan.one (fn Arg ((k, y, _), _) => (k = Ident orelse k = Keyword) andalso x = y))
wenzelm@16140
   250
  >> sym_of;
wenzelm@5878
   251
wenzelm@10035
   252
val add = $$$ "add";
wenzelm@10035
   253
val del = $$$ "del";
wenzelm@8803
   254
val colon = $$$ ":";
wenzelm@10035
   255
val query = $$$ "?";
wenzelm@10035
   256
val bang = $$$ "!";
wenzelm@10035
   257
val query_colon = $$$ "?:";
wenzelm@10035
   258
val bang_colon = $$$ "!:";
wenzelm@10035
   259
wenzelm@8803
   260
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   261
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@15703
   262
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
wenzelm@15703
   263
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
wenzelm@5878
   264
wenzelm@15703
   265
val name = named >> sym_of;
wenzelm@18037
   266
val alt_name = alt_string >> sym_of;
wenzelm@16140
   267
val symbol = symbolic >> sym_of;
wenzelm@17064
   268
val liberal_name = symbol || name;
wenzelm@8233
   269
wenzelm@15703
   270
val nat = some_ident Syntax.read_nat;
wenzelm@9538
   271
val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *;
wenzelm@15987
   272
val var = some_ident Syntax.read_variable;
wenzelm@14563
   273
wenzelm@5822
   274
wenzelm@5878
   275
(* enumerations *)
wenzelm@5878
   276
wenzelm@15703
   277
fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::;
wenzelm@15703
   278
fun list scan = list1 scan || Scan.succeed [];
wenzelm@15703
   279
wenzelm@6447
   280
fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::;
wenzelm@5878
   281
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   282
wenzelm@6447
   283
fun and_list1 scan = enum1 "and" scan;
wenzelm@6447
   284
fun and_list scan = enum "and" scan;
wenzelm@5878
   285
wenzelm@5822
   286
wenzelm@15703
   287
(* values *)
wenzelm@15703
   288
wenzelm@15703
   289
fun value dest = Scan.some  (*no touch here*)
wenzelm@15703
   290
  (fn Arg (_, Value (SOME v)) => (SOME (dest v) handle Match => NONE)
wenzelm@15703
   291
    | Arg _ => NONE);
wenzelm@15703
   292
wenzelm@15703
   293
fun evaluate mk eval arg =
wenzelm@15703
   294
  let val x = eval (sym_of arg) in (assign (SOME (mk x)) arg; x) end;
wenzelm@15703
   295
wenzelm@15703
   296
val internal_name = value (fn Name s => s);
wenzelm@15703
   297
val internal_typ = value (fn Typ T => T);
wenzelm@15703
   298
val internal_term = value (fn Term t => t);
wenzelm@15703
   299
val internal_fact = value (fn Fact ths => ths);
wenzelm@15703
   300
val internal_attribute = value (fn Attribute att => att);
wenzelm@15703
   301
wenzelm@15703
   302
fun named_name intern = internal_name || named >> evaluate Name intern;
wenzelm@15703
   303
fun named_typ readT = internal_typ || named >> evaluate Typ readT;
wenzelm@15703
   304
fun named_term read = internal_term || named >> evaluate Term read;
wenzelm@18037
   305
fun named_fact get = internal_fact || (alt_string || named) >> evaluate Fact get;
wenzelm@15703
   306
wenzelm@15703
   307
wenzelm@5878
   308
(* terms and types *)
wenzelm@5878
   309
wenzelm@16343
   310
val global_typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o ProofContext.init);
wenzelm@15703
   311
val global_typ = Scan.peek (named_typ o ProofContext.read_typ o ProofContext.init);
wenzelm@15703
   312
val global_term = Scan.peek (named_term o ProofContext.read_term o ProofContext.init);
wenzelm@15703
   313
val global_prop = Scan.peek (named_term o ProofContext.read_prop o ProofContext.init);
wenzelm@5878
   314
wenzelm@16343
   315
val local_typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev);
wenzelm@15703
   316
val local_typ = Scan.peek (named_typ o ProofContext.read_typ);
wenzelm@15703
   317
val local_term = Scan.peek (named_term o ProofContext.read_term);
wenzelm@15703
   318
val local_prop = Scan.peek (named_term o ProofContext.read_prop);
wenzelm@5878
   319
wenzelm@18635
   320
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
wenzelm@18635
   321
val typ = Scan.peek (named_typ o ProofContext.read_typ o Context.proof_of);
wenzelm@18635
   322
val term = Scan.peek (named_term o ProofContext.read_term o Context.proof_of);
wenzelm@18635
   323
val prop = Scan.peek (named_term o ProofContext.read_prop o Context.proof_of);
wenzelm@18635
   324
wenzelm@5878
   325
wenzelm@15703
   326
(* type and constant names *)
wenzelm@15703
   327
wenzelm@15703
   328
val dest_tyname = fn Type (c, _) => c | TFree (a, _) => a | _ => "";
wenzelm@15703
   329
val dest_const = fn Const (c, _) => c | Free (x, _) => x | _ => "";
wenzelm@15703
   330
wenzelm@17104
   331
val global_tyname = Scan.peek (named_typ o Sign.read_tyname) >> dest_tyname;
wenzelm@17104
   332
val global_const = Scan.peek (named_term o Sign.read_const) >> dest_const;
wenzelm@15703
   333
val local_tyname = Scan.peek (named_typ o ProofContext.read_tyname) >> dest_tyname;
wenzelm@15703
   334
val local_const = Scan.peek (named_term o ProofContext.read_const) >> dest_const;
wenzelm@18635
   335
val tyname =
wenzelm@18635
   336
  Scan.peek (named_typ o Context.cases Sign.read_tyname ProofContext.read_tyname) >> dest_tyname;
wenzelm@18635
   337
val const =
wenzelm@18635
   338
  Scan.peek (named_term o Context.cases Sign.read_const ProofContext.read_const) >> dest_const;
wenzelm@7553
   339
wenzelm@15703
   340
wenzelm@15703
   341
(* misc *)
wenzelm@15703
   342
wenzelm@15703
   343
val thm_sel = parens (list1
wenzelm@15703
   344
 (nat --| $$$ "-" -- nat >> PureThy.FromTo ||
wenzelm@15703
   345
  nat --| $$$ "-" >> PureThy.From ||
wenzelm@15703
   346
  nat >> PureThy.Single));
wenzelm@15703
   347
wenzelm@15703
   348
val bang_facts = Scan.peek (fn ctxt =>
wenzelm@15703
   349
  $$$ "!" >> K (ProofContext.prems_of ctxt) || Scan.succeed []);
wenzelm@7553
   350
wenzelm@7553
   351
wenzelm@8536
   352
(* goal specification *)
wenzelm@8536
   353
wenzelm@8536
   354
(* range *)
wenzelm@8233
   355
wenzelm@8536
   356
val from_to =
wenzelm@15703
   357
  nat -- ($$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@15703
   358
  nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@8549
   359
  nat >> (fn i => fn tac => tac i) ||
wenzelm@15703
   360
  $$$ "!" >> K ALLGOALS;
wenzelm@8536
   361
wenzelm@15703
   362
val goal = $$$ "[" |-- !!! (from_to --| $$$ "]");
wenzelm@8536
   363
fun goal_spec def = Scan.lift (Scan.optional goal def);
wenzelm@8233
   364
wenzelm@8233
   365
wenzelm@15703
   366
wenzelm@15703
   367
(* attribs *)
wenzelm@15703
   368
wenzelm@15703
   369
local
wenzelm@5878
   370
wenzelm@9126
   371
val exclude = explode "()[],";
wenzelm@5822
   372
wenzelm@15703
   373
fun atomic blk = touch (Scan.one (fn Arg ((k, x, _), _) =>
wenzelm@15703
   374
    k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ","));
wenzelm@5822
   375
wenzelm@5878
   376
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5878
   377
and args1 blk x =
wenzelm@5878
   378
  ((Scan.repeat1
wenzelm@15703
   379
    (Scan.repeat1 (atomic blk) ||
wenzelm@15703
   380
      argsp "(" ")" ||
wenzelm@15703
   381
      argsp "[" "]")) >> List.concat) x
wenzelm@15703
   382
and argsp l r x =
wenzelm@15703
   383
  (Scan.trace (&&& l -- !!! (args true -- &&& r)) >> #2) x;
wenzelm@15703
   384
wenzelm@15703
   385
in
wenzelm@5822
   386
wenzelm@15703
   387
fun attribs intern =
wenzelm@15703
   388
  let
wenzelm@15703
   389
    val attrib_name = internal_name || (symbolic || named) >> evaluate Name intern;
wenzelm@15703
   390
    val attrib = position (attrib_name -- !!! (args false)) >> src;
wenzelm@15703
   391
  in $$$ "[" |-- !!! (list attrib --| $$$ "]") end;
wenzelm@15703
   392
wenzelm@15703
   393
fun opt_attribs intern = Scan.optional (attribs intern) [];
wenzelm@15703
   394
wenzelm@15703
   395
end;
wenzelm@5822
   396
wenzelm@5822
   397
wenzelm@15703
   398
(* syntax interface *)
wenzelm@5822
   399
wenzelm@8282
   400
fun syntax kind scan (src as Src ((s, args), pos)) st =
wenzelm@9901
   401
  (case Scan.error (Scan.finite' stopper (Scan.option scan)) (st, args) of
skalberg@15531
   402
    (SOME x, (st', [])) => (st', x)
wenzelm@15703
   403
  | (_, (_, args')) =>
wenzelm@15703
   404
      error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
wenzelm@15703
   405
        space_implode " " (map string_of args')));
wenzelm@15703
   406
wenzelm@5878
   407
wenzelm@5878
   408
wenzelm@15703
   409
(** pretty printing **)
wenzelm@6447
   410
wenzelm@15703
   411
fun pretty_src ctxt src =
wenzelm@15703
   412
  let
wenzelm@15703
   413
    fun prt (Arg (_, Value (SOME (Name s)))) = Pretty.str (quote s)
wenzelm@15703
   414
      | prt (Arg (_, Value (SOME (Typ T)))) = ProofContext.pretty_typ ctxt T
wenzelm@15703
   415
      | prt (Arg (_, Value (SOME (Term t)))) = ProofContext.pretty_term ctxt t
wenzelm@15703
   416
      | prt (Arg (_, Value (SOME (Fact ths)))) = ProofContext.pretty_thms ctxt ths
wenzelm@15703
   417
      | prt arg = Pretty.str (string_of arg);
wenzelm@15703
   418
    val (s, args) = #1 (dest_src src);
wenzelm@15703
   419
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
wenzelm@5822
   420
wenzelm@15703
   421
fun pretty_attribs _ [] = []
wenzelm@15703
   422
  | pretty_attribs ctxt srcs =
wenzelm@15703
   423
      [Pretty.enclose "[" "]" (Pretty.commas (map (pretty_src ctxt) srcs))];
wenzelm@5822
   424
wenzelm@5822
   425
end;