src/Pure/Isar/args.ML
author wenzelm
Sat Jun 28 15:17:24 2008 +0200 (2008-06-28)
changeset 27377 0b9295c598f6
parent 27371 f89aa7bd4602
child 27382 b1285021424e
permissions -rw-r--r--
added thm_name, opt_thm_name;
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@18728
    11
  datatype value =
wenzelm@21662
    12
    Text of string | Typ of typ | Term of term | Fact of thm list |
wenzelm@21662
    13
    Attribute of morphism -> attribute
wenzelm@5822
    14
  type T
wenzelm@9748
    15
  val string_of: T -> string
wenzelm@15703
    16
  val mk_ident: string * Position.T -> T
wenzelm@15703
    17
  val mk_string: string * Position.T -> T
wenzelm@18037
    18
  val mk_alt_string: string * Position.T -> T
wenzelm@15703
    19
  val mk_keyword: string * Position.T -> T
wenzelm@21496
    20
  val mk_text: string -> T
wenzelm@15703
    21
  val mk_typ: typ -> T
wenzelm@15703
    22
  val mk_term: term -> T
wenzelm@15703
    23
  val mk_fact: thm list -> T
wenzelm@21662
    24
  val mk_attribute: (morphism -> attribute) -> T
wenzelm@27234
    25
  val eof: T
wenzelm@5822
    26
  val stopper: T * (T -> bool)
wenzelm@5822
    27
  val not_eof: T -> bool
wenzelm@15703
    28
  type src
wenzelm@15703
    29
  val src: (string * T list) * Position.T -> src
wenzelm@15703
    30
  val dest_src: src -> (string * T list) * Position.T
wenzelm@21030
    31
  val pretty_src: Proof.context -> src -> Pretty.T
wenzelm@15703
    32
  val map_name: (string -> string) -> src -> src
wenzelm@21480
    33
  val morph_values: morphism -> src -> src
wenzelm@20263
    34
  val maxidx_values: src -> int -> int
wenzelm@15703
    35
  val assignable: src -> src
wenzelm@15703
    36
  val assign: value option -> T -> unit
wenzelm@15703
    37
  val closure: src -> src
wenzelm@27371
    38
  val context: Context.generic * T list -> Context.proof * (Context.generic * T list)
wenzelm@27371
    39
  val theory: Context.generic * T list -> Context.theory * (Context.generic * T list)
wenzelm@5878
    40
  val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
wenzelm@5878
    41
  val !!! : (T list -> 'a) -> T list -> 'a
wenzelm@5822
    42
  val $$$ : string -> T list -> string * T list
wenzelm@10035
    43
  val add: T list -> string * T list
wenzelm@10035
    44
  val del: T list -> string * T list
wenzelm@8803
    45
  val colon: T list -> string * T list
wenzelm@10035
    46
  val query: T list -> string * T list
wenzelm@10035
    47
  val bang: T list -> string * T list
wenzelm@10035
    48
  val query_colon: T list -> string * T list
wenzelm@10035
    49
  val bang_colon: T list -> string * T list
wenzelm@8803
    50
  val parens: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@10150
    51
  val bracks: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@9809
    52
  val mode: string -> 'a * T list -> bool * ('a * T list)
wenzelm@15703
    53
  val maybe: (T list -> 'a * T list) -> T list -> 'a option * T list
wenzelm@24002
    54
  val terminator: T list -> T * T list
wenzelm@5822
    55
  val name: T list -> string * T list
wenzelm@18037
    56
  val alt_name: T list -> string * T list
wenzelm@16140
    57
  val symbol: T list -> string * T list
wenzelm@17064
    58
  val liberal_name: T list -> string * T list
wenzelm@5878
    59
  val nat: T list -> int * T list
wenzelm@9538
    60
  val int: T list -> int * T list
wenzelm@5878
    61
  val var: T list -> indexname * T list
wenzelm@15703
    62
  val list: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@15703
    63
  val list1: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@6447
    64
  val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    65
  val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    66
  val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    67
  val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@15703
    68
  val ahead: T list -> T * T list
wenzelm@21496
    69
  val internal_text: T list -> string * T list
wenzelm@15703
    70
  val internal_typ: T list -> typ * T list
wenzelm@15703
    71
  val internal_term: T list -> term * T list
wenzelm@15703
    72
  val internal_fact: T list -> thm list * T list
wenzelm@21662
    73
  val internal_attribute: T list -> (morphism -> attribute) * T list
wenzelm@21496
    74
  val named_text: (string -> string) -> T list -> string * T list
wenzelm@15703
    75
  val named_typ: (string -> typ) -> T list -> typ * T list
wenzelm@15703
    76
  val named_term: (string -> term) -> T list -> term * T list
wenzelm@15703
    77
  val named_fact: (string -> thm list) -> T list -> thm list * T list
wenzelm@24002
    78
  val named_attribute: (string -> morphism -> attribute) -> T list ->
wenzelm@24002
    79
    (morphism -> attribute) * 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@21724
    83
  val term_abbrev: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@18635
    84
  val prop: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@18635
    85
  val tyname: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@18635
    86
  val const: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@25343
    87
  val const_proper: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@26336
    88
  val thm_sel: T list -> Facts.interval list * T list
wenzelm@18998
    89
  val bang_facts: Context.generic * T list -> thm list * (Context.generic * T list)
wenzelm@8536
    90
  val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
wenzelm@8536
    91
    -> ((int -> tactic) -> tactic) * ('a * T list)
wenzelm@15703
    92
  val attribs: (string -> string) -> T list -> src list * T list
wenzelm@15703
    93
  val opt_attribs: (string -> string) -> T list -> src list * T list
wenzelm@27377
    94
  val thm_name: (string -> string) -> string -> T list -> (string * src list) * T list
wenzelm@27377
    95
  val opt_thm_name: (string -> string) -> string -> T list -> (string * src list) * T list
haftmann@21879
    96
  val syntax: string -> ('b * T list -> 'a * ('b * T list)) -> src -> 'b -> 'a * 'b
wenzelm@18998
    97
  val context_syntax: string -> (Context.generic * T list -> 'a * (Context.generic * T list)) ->
haftmann@21879
    98
    src -> Proof.context -> 'a * Proof.context
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@21496
   117
  Text of string |
wenzelm@15703
   118
  Typ of typ |
wenzelm@15703
   119
  Term of term |
wenzelm@15703
   120
  Fact of thm list |
wenzelm@21662
   121
  Attribute of morphism -> 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@21496
   149
val mk_text = mk_value "<text>" o Text;
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@21030
   175
fun pretty_src ctxt src =
wenzelm@21030
   176
  let
wenzelm@21697
   177
    val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
wenzelm@21496
   178
    fun prt (Arg (_, Value (SOME (Text s)))) = Pretty.str (quote s)
wenzelm@24920
   179
      | prt (Arg (_, Value (SOME (Typ T)))) = Syntax.pretty_typ ctxt T
wenzelm@24920
   180
      | prt (Arg (_, Value (SOME (Term t)))) = Syntax.pretty_term ctxt t
wenzelm@21697
   181
      | prt (Arg (_, Value (SOME (Fact ths)))) =
wenzelm@21697
   182
          Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
wenzelm@21030
   183
      | prt arg = Pretty.str (string_of arg);
wenzelm@21030
   184
    val (s, args) = #1 (dest_src src);
wenzelm@21030
   185
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
wenzelm@21030
   186
wenzelm@15703
   187
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
wenzelm@15703
   188
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
wenzelm@15703
   189
wenzelm@15703
   190
wenzelm@15703
   191
(* values *)
wenzelm@15703
   192
wenzelm@15703
   193
fun map_value f (Arg (s, Value (SOME v))) = Arg (s, Value (SOME (f v)))
wenzelm@15703
   194
  | map_value _ arg = arg;
wenzelm@15703
   195
wenzelm@21480
   196
fun morph_values phi = map_args (map_value
wenzelm@21496
   197
  (fn Text s => Text s
wenzelm@21480
   198
    | Typ T => Typ (Morphism.typ phi T)
wenzelm@21480
   199
    | Term t => Term (Morphism.term phi t)
wenzelm@21976
   200
    | Fact ths => Fact (Morphism.fact phi ths)
wenzelm@22669
   201
    | Attribute att => Attribute (Morphism.transform phi att)));
wenzelm@15703
   202
wenzelm@20263
   203
fun maxidx_values (Src ((_, args), _)) = args |> fold
wenzelm@20263
   204
  (fn (Arg (_, Value (SOME (Typ T)))) => Term.maxidx_typ T
wenzelm@20263
   205
    | (Arg (_, Value (SOME (Term t)))) => Term.maxidx_term t
wenzelm@20263
   206
    | (Arg (_, Value (SOME (Fact ths)))) => fold Thm.maxidx_thm ths
wenzelm@20263
   207
    | _ => I);
wenzelm@20263
   208
wenzelm@15703
   209
wenzelm@15703
   210
(* static binding *)
wenzelm@15703
   211
wenzelm@15703
   212
(*1st stage: make empty slots assignable*)
wenzelm@15703
   213
val assignable =
wenzelm@15703
   214
  map_args (fn Arg (s, Empty) => Arg (s, Assignable (ref NONE)) | arg => arg);
wenzelm@15703
   215
wenzelm@15703
   216
(*2nd stage: assign values as side-effect of scanning*)
wenzelm@15703
   217
fun assign v (arg as Arg (_, Assignable r)) = r := v
wenzelm@15703
   218
  | assign _ _ = ();
wenzelm@15703
   219
wenzelm@15703
   220
val ahead = Scan.ahead (Scan.one not_eof);
wenzelm@15703
   221
fun touch scan = ahead -- scan >> (fn (arg, y) => (assign NONE arg; y));
wenzelm@15703
   222
wenzelm@15703
   223
(*3rd stage: static closure of final values*)
wenzelm@15703
   224
val closure =
wenzelm@15703
   225
  map_args (fn Arg (s, Assignable (ref v)) => Arg (s, Value v) | arg => arg);
wenzelm@15703
   226
wenzelm@15703
   227
wenzelm@15703
   228
wenzelm@5822
   229
(** scanners **)
wenzelm@5822
   230
wenzelm@27371
   231
(* context *)
wenzelm@27371
   232
wenzelm@27371
   233
fun context x = (Scan.state >> Context.proof_of) x;
wenzelm@27371
   234
fun theory x = (Scan.state >> Context.theory_of) x;
wenzelm@27371
   235
wenzelm@27371
   236
wenzelm@5878
   237
(* position *)
wenzelm@5878
   238
wenzelm@15703
   239
fun position scan =
wenzelm@15703
   240
  (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
   241
wenzelm@5878
   242
wenzelm@5878
   243
(* cut *)
wenzelm@5878
   244
wenzelm@5878
   245
fun !!! scan =
wenzelm@5878
   246
  let
wenzelm@5878
   247
    fun get_pos [] = " (past end-of-text!)"
wenzelm@15703
   248
      | get_pos (arg :: _) = Position.str_of (pos_of arg);
wenzelm@5878
   249
skalberg@15531
   250
    fun err (args, NONE) = "Argument syntax error" ^ get_pos args
skalberg@15531
   251
      | err (args, SOME msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
   252
  in Scan.!! err scan end;
wenzelm@5878
   253
wenzelm@5878
   254
wenzelm@5822
   255
(* basic *)
wenzelm@5822
   256
wenzelm@15703
   257
fun some_ident f =
wenzelm@15703
   258
  touch (Scan.some (fn Arg ((Ident, x, _), _) => f x | _ => NONE));
wenzelm@15703
   259
wenzelm@15703
   260
val named =
wenzelm@15703
   261
  touch (Scan.one (fn Arg ((k, _, _), _) => k = Ident orelse k = String));
wenzelm@15703
   262
wenzelm@18037
   263
val alt_string =
wenzelm@18037
   264
  touch (Scan.one (fn Arg ((k, _, _), _) => k = AltString));
wenzelm@18037
   265
wenzelm@15703
   266
val symbolic =
wenzelm@15703
   267
  touch (Scan.one (fn Arg ((k, x, _), _) => k = Keyword andalso OuterLex.is_sid x));
wenzelm@15703
   268
wenzelm@15703
   269
fun &&& x =
wenzelm@15703
   270
  touch (Scan.one (fn Arg ((k, y, _), _) => k = Keyword andalso x = y));
wenzelm@15703
   271
wenzelm@15703
   272
fun $$$ x =
wenzelm@16140
   273
  touch (Scan.one (fn Arg ((k, y, _), _) => (k = Ident orelse k = Keyword) andalso x = y))
wenzelm@16140
   274
  >> sym_of;
wenzelm@5878
   275
wenzelm@10035
   276
val add = $$$ "add";
wenzelm@10035
   277
val del = $$$ "del";
wenzelm@8803
   278
val colon = $$$ ":";
wenzelm@10035
   279
val query = $$$ "?";
wenzelm@10035
   280
val bang = $$$ "!";
wenzelm@20111
   281
val query_colon = $$$ "?" ^^ $$$ ":";
wenzelm@20111
   282
val bang_colon = $$$ "!" ^^ $$$ ":";
wenzelm@10035
   283
wenzelm@8803
   284
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   285
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@15703
   286
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
wenzelm@15703
   287
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
wenzelm@24002
   288
val terminator = Scan.ahead (Scan.one is_eof);
wenzelm@5878
   289
wenzelm@15703
   290
val name = named >> sym_of;
wenzelm@18037
   291
val alt_name = alt_string >> sym_of;
wenzelm@16140
   292
val symbol = symbolic >> sym_of;
wenzelm@17064
   293
val liberal_name = symbol || name;
wenzelm@8233
   294
wenzelm@24244
   295
val nat = some_ident Lexicon.read_nat;
wenzelm@9538
   296
val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *;
wenzelm@24244
   297
val var = some_ident Lexicon.read_variable;
wenzelm@14563
   298
wenzelm@5822
   299
wenzelm@5878
   300
(* enumerations *)
wenzelm@5878
   301
wenzelm@25999
   302
fun list1 scan = scan ::: Scan.repeat ($$$ "," |-- scan);
wenzelm@15703
   303
fun list scan = list1 scan || Scan.succeed [];
wenzelm@15703
   304
wenzelm@25999
   305
fun enum1 sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
wenzelm@5878
   306
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   307
wenzelm@6447
   308
fun and_list1 scan = enum1 "and" scan;
wenzelm@6447
   309
fun and_list scan = enum "and" scan;
wenzelm@5878
   310
wenzelm@5822
   311
wenzelm@15703
   312
(* values *)
wenzelm@15703
   313
wenzelm@15703
   314
fun value dest = Scan.some  (*no touch here*)
wenzelm@15703
   315
  (fn Arg (_, Value (SOME v)) => (SOME (dest v) handle Match => NONE)
wenzelm@15703
   316
    | Arg _ => NONE);
wenzelm@15703
   317
wenzelm@15703
   318
fun evaluate mk eval arg =
wenzelm@15703
   319
  let val x = eval (sym_of arg) in (assign (SOME (mk x)) arg; x) end;
wenzelm@15703
   320
wenzelm@21496
   321
val internal_text = value (fn Text s => s);
wenzelm@15703
   322
val internal_typ = value (fn Typ T => T);
wenzelm@15703
   323
val internal_term = value (fn Term t => t);
wenzelm@15703
   324
val internal_fact = value (fn Fact ths => ths);
wenzelm@15703
   325
val internal_attribute = value (fn Attribute att => att);
wenzelm@15703
   326
wenzelm@21496
   327
fun named_text intern = internal_text || named >> evaluate Text intern;
wenzelm@15703
   328
fun named_typ readT = internal_typ || named >> evaluate Typ readT;
wenzelm@15703
   329
fun named_term read = internal_term || named >> evaluate Term read;
wenzelm@18037
   330
fun named_fact get = internal_fact || (alt_string || named) >> evaluate Fact get;
wenzelm@24002
   331
fun named_attribute att = internal_attribute || named >> evaluate Attribute att;
wenzelm@15703
   332
wenzelm@15703
   333
wenzelm@5878
   334
(* terms and types *)
wenzelm@5878
   335
wenzelm@18635
   336
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
wenzelm@25331
   337
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
wenzelm@24508
   338
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
wenzelm@21724
   339
val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
wenzelm@24508
   340
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
wenzelm@18635
   341
wenzelm@5878
   342
wenzelm@15703
   343
(* type and constant names *)
wenzelm@15703
   344
wenzelm@25323
   345
val tyname = Scan.peek (named_typ o ProofContext.read_tyname o Context.proof_of)
wenzelm@18998
   346
  >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
wenzelm@15703
   347
wenzelm@25323
   348
val const = Scan.peek (named_term o ProofContext.read_const o Context.proof_of)
wenzelm@18998
   349
  >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
wenzelm@7553
   350
wenzelm@25343
   351
val const_proper = Scan.peek (named_term o ProofContext.read_const_proper o Context.proof_of)
wenzelm@25343
   352
  >> (fn Const (c, _) => c | _ => "");
wenzelm@25343
   353
wenzelm@15703
   354
wenzelm@15703
   355
(* misc *)
wenzelm@15703
   356
wenzelm@15703
   357
val thm_sel = parens (list1
wenzelm@26336
   358
 (nat --| $$$ "-" -- nat >> Facts.FromTo ||
wenzelm@26336
   359
  nat --| $$$ "-" >> Facts.From ||
wenzelm@26336
   360
  nat >> Facts.Single));
wenzelm@15703
   361
wenzelm@18998
   362
val bang_facts = Scan.peek (fn context =>
wenzelm@22932
   363
  $$$ "!" >> (fn _ => (warning "use of prems in proof method";
wenzelm@22932
   364
    Assumption.prems_of (Context.proof_of context))) || Scan.succeed []);
wenzelm@7553
   365
wenzelm@7553
   366
wenzelm@8536
   367
(* goal specification *)
wenzelm@8536
   368
wenzelm@8536
   369
(* range *)
wenzelm@8233
   370
wenzelm@8536
   371
val from_to =
wenzelm@15703
   372
  nat -- ($$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@15703
   373
  nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@8549
   374
  nat >> (fn i => fn tac => tac i) ||
wenzelm@15703
   375
  $$$ "!" >> K ALLGOALS;
wenzelm@8536
   376
wenzelm@15703
   377
val goal = $$$ "[" |-- !!! (from_to --| $$$ "]");
wenzelm@8536
   378
fun goal_spec def = Scan.lift (Scan.optional goal def);
wenzelm@8233
   379
wenzelm@8233
   380
wenzelm@15703
   381
wenzelm@15703
   382
(* attribs *)
wenzelm@15703
   383
wenzelm@15703
   384
local
wenzelm@5878
   385
wenzelm@20664
   386
val exclude = member (op =) (explode "()[],");
wenzelm@5822
   387
wenzelm@15703
   388
fun atomic blk = touch (Scan.one (fn Arg ((k, x, _), _) =>
wenzelm@20664
   389
    k <> Keyword orelse not (exclude x) orelse blk andalso x = ","));
wenzelm@5822
   390
wenzelm@5878
   391
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5878
   392
and args1 blk x =
wenzelm@5878
   393
  ((Scan.repeat1
wenzelm@15703
   394
    (Scan.repeat1 (atomic blk) ||
wenzelm@15703
   395
      argsp "(" ")" ||
wenzelm@19482
   396
      argsp "[" "]")) >> flat) x
wenzelm@15703
   397
and argsp l r x =
wenzelm@15703
   398
  (Scan.trace (&&& l -- !!! (args true -- &&& r)) >> #2) x;
wenzelm@15703
   399
wenzelm@15703
   400
in
wenzelm@5822
   401
wenzelm@15703
   402
fun attribs intern =
wenzelm@15703
   403
  let
wenzelm@21496
   404
    val attrib_name = internal_text || (symbolic || named) >> evaluate Text intern;
wenzelm@15703
   405
    val attrib = position (attrib_name -- !!! (args false)) >> src;
wenzelm@15703
   406
  in $$$ "[" |-- !!! (list attrib --| $$$ "]") end;
wenzelm@15703
   407
wenzelm@15703
   408
fun opt_attribs intern = Scan.optional (attribs intern) [];
wenzelm@15703
   409
wenzelm@15703
   410
end;
wenzelm@5822
   411
wenzelm@5822
   412
wenzelm@27377
   413
(* theorem specifications *)
wenzelm@27377
   414
wenzelm@27377
   415
fun thm_name intern s = name -- opt_attribs intern --| $$$ s;
wenzelm@27377
   416
fun opt_thm_name intern s =
wenzelm@27377
   417
  Scan.optional ((name -- opt_attribs intern || attribs intern >> pair "") --| $$$ s) ("", []);
wenzelm@27377
   418
wenzelm@27377
   419
wenzelm@15703
   420
(* syntax interface *)
wenzelm@5822
   421
wenzelm@8282
   422
fun syntax kind scan (src as Src ((s, args), pos)) st =
wenzelm@9901
   423
  (case Scan.error (Scan.finite' stopper (Scan.option scan)) (st, args) of
haftmann@21879
   424
    (SOME x, (st', [])) => (x, st')
wenzelm@15703
   425
  | (_, (_, args')) =>
wenzelm@15703
   426
      error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
wenzelm@15703
   427
        space_implode " " (map string_of args')));
wenzelm@15703
   428
haftmann@21879
   429
fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
wenzelm@18998
   430
wenzelm@5822
   431
end;