src/Pure/Isar/args.ML
author wenzelm
Sat May 15 23:16:32 2010 +0200 (2010-05-15)
changeset 36950 75b8f26f2f07
parent 36505 79c1d2bbe5a9
child 36959 f5417836dbea
permissions -rw-r--r--
refer directly to structure Keyword and Parse;
eliminated old-style structure aliases K and P;
wenzelm@5822
     1
(*  Title:      Pure/Isar/args.ML
wenzelm@5822
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5822
     3
wenzelm@35013
     4
Parsing with implicit value assignment.  Concrete argument syntax of
wenzelm@27811
     5
attributes, methods etc.
wenzelm@5822
     6
*)
wenzelm@5822
     7
wenzelm@5822
     8
signature ARGS =
wenzelm@5822
     9
sig
wenzelm@30513
    10
  type token = OuterLex.token
wenzelm@15703
    11
  type src
wenzelm@30513
    12
  val src: (string * token list) * Position.T -> src
wenzelm@30513
    13
  val dest_src: src -> (string * token list) * Position.T
wenzelm@21030
    14
  val pretty_src: Proof.context -> src -> Pretty.T
wenzelm@15703
    15
  val map_name: (string -> string) -> src -> src
wenzelm@21480
    16
  val morph_values: morphism -> src -> src
wenzelm@15703
    17
  val assignable: src -> src
wenzelm@15703
    18
  val closure: src -> src
wenzelm@30513
    19
  val context: Proof.context context_parser
wenzelm@30513
    20
  val theory: theory context_parser
wenzelm@30513
    21
  val $$$ : string -> string parser
wenzelm@30513
    22
  val add: string parser
wenzelm@30513
    23
  val del: string parser
wenzelm@30513
    24
  val colon: string parser
wenzelm@30513
    25
  val query: string parser
wenzelm@30513
    26
  val bang: string parser
wenzelm@30513
    27
  val query_colon: string parser
wenzelm@30513
    28
  val bang_colon: string parser
wenzelm@30513
    29
  val parens: ('a parser) -> 'a parser
wenzelm@30513
    30
  val bracks: ('a parser) -> 'a parser
wenzelm@30513
    31
  val mode: string -> bool context_parser
wenzelm@30513
    32
  val maybe: 'a parser -> 'a option parser
wenzelm@30513
    33
  val name_source: string parser
wenzelm@30573
    34
  val name_source_position: (Symbol_Pos.text * Position.T) parser
wenzelm@30513
    35
  val name: string parser
wenzelm@30513
    36
  val binding: binding parser
wenzelm@30513
    37
  val alt_name: string parser
wenzelm@30513
    38
  val symbol: string parser
wenzelm@30513
    39
  val liberal_name: string parser
wenzelm@30513
    40
  val var: indexname parser
wenzelm@30513
    41
  val internal_text: string parser
wenzelm@30513
    42
  val internal_typ: typ parser
wenzelm@30513
    43
  val internal_term: term parser
wenzelm@30513
    44
  val internal_fact: thm list parser
wenzelm@30513
    45
  val internal_attribute: (morphism -> attribute) parser
wenzelm@30513
    46
  val named_text: (string -> string) -> string parser
wenzelm@30513
    47
  val named_typ: (string -> typ) -> typ parser
wenzelm@30513
    48
  val named_term: (string -> term) -> term parser
wenzelm@30513
    49
  val named_fact: (string -> thm list) -> thm list parser
wenzelm@30513
    50
  val named_attribute: (string -> morphism -> attribute) -> (morphism -> attribute) parser
wenzelm@30513
    51
  val typ_abbrev: typ context_parser
wenzelm@30513
    52
  val typ: typ context_parser
wenzelm@30513
    53
  val term: term context_parser
wenzelm@30513
    54
  val term_abbrev: term context_parser
wenzelm@30513
    55
  val prop: term context_parser
wenzelm@35360
    56
  val type_name: bool -> string context_parser
wenzelm@35399
    57
  val const: bool -> string context_parser
wenzelm@35399
    58
  val const_proper: bool -> string context_parser
wenzelm@30514
    59
  val goal_spec: ((int -> tactic) -> tactic) context_parser
wenzelm@30513
    60
  val parse: token list parser
wenzelm@30513
    61
  val parse1: (string -> bool) -> token list parser
wenzelm@30513
    62
  val attribs: (string -> string) -> src list parser
wenzelm@30513
    63
  val opt_attribs: (string -> string) -> src list parser
wenzelm@30513
    64
  val thm_name: (string -> string) -> string -> (binding * src list) parser
wenzelm@30513
    65
  val opt_thm_name: (string -> string) -> string -> (binding * src list) parser
wenzelm@30513
    66
  val syntax: string -> 'a context_parser -> src -> Context.generic -> 'a * Context.generic
wenzelm@30513
    67
  val context_syntax: string -> 'a context_parser -> src -> Proof.context -> 'a * Proof.context
wenzelm@5822
    68
end;
wenzelm@5822
    69
wenzelm@5822
    70
structure Args: ARGS =
wenzelm@5822
    71
struct
wenzelm@5822
    72
wenzelm@27811
    73
structure T = OuterLex;
wenzelm@5822
    74
wenzelm@30513
    75
type token = T.token;
wenzelm@30513
    76
wenzelm@5822
    77
wenzelm@5822
    78
wenzelm@15703
    79
(** datatype src **)
wenzelm@15703
    80
wenzelm@30513
    81
datatype src = Src of (string * token list) * Position.T;
wenzelm@15703
    82
wenzelm@15703
    83
val src = Src;
wenzelm@15703
    84
fun dest_src (Src src) = src;
wenzelm@15703
    85
wenzelm@21030
    86
fun pretty_src ctxt src =
wenzelm@21030
    87
  let
wenzelm@32091
    88
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
wenzelm@27811
    89
    fun prt arg =
wenzelm@27811
    90
      (case T.get_value arg of
wenzelm@27811
    91
        SOME (T.Text s) => Pretty.str (quote s)
wenzelm@27811
    92
      | SOME (T.Typ T) => Syntax.pretty_typ ctxt T
wenzelm@27811
    93
      | SOME (T.Term t) => Syntax.pretty_term ctxt t
wenzelm@27811
    94
      | SOME (T.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
wenzelm@27811
    95
      | _ => Pretty.str (T.unparse arg));
wenzelm@21030
    96
    val (s, args) = #1 (dest_src src);
wenzelm@21030
    97
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
wenzelm@21030
    98
wenzelm@15703
    99
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
wenzelm@15703
   100
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
wenzelm@15703
   101
wenzelm@15703
   102
wenzelm@15703
   103
(* values *)
wenzelm@15703
   104
wenzelm@27811
   105
fun morph_values phi = map_args (T.map_value
wenzelm@27811
   106
  (fn T.Text s => T.Text s
wenzelm@27811
   107
    | T.Typ T => T.Typ (Morphism.typ phi T)
wenzelm@27811
   108
    | T.Term t => T.Term (Morphism.term phi t)
wenzelm@27811
   109
    | T.Fact ths => T.Fact (Morphism.fact phi ths)
wenzelm@27811
   110
    | T.Attribute att => T.Attribute (Morphism.transform phi att)));
wenzelm@15703
   111
wenzelm@27811
   112
val assignable = map_args T.assignable;
wenzelm@27811
   113
val closure = map_args T.closure;
wenzelm@15703
   114
wenzelm@15703
   115
wenzelm@15703
   116
wenzelm@27811
   117
(** argument scanners **)
wenzelm@5822
   118
wenzelm@27371
   119
(* context *)
wenzelm@27371
   120
wenzelm@27371
   121
fun context x = (Scan.state >> Context.proof_of) x;
wenzelm@27371
   122
fun theory x = (Scan.state >> Context.theory_of) x;
wenzelm@27371
   123
wenzelm@27371
   124
wenzelm@27811
   125
(* basic *)
wenzelm@5878
   126
wenzelm@36950
   127
fun token atom = Scan.ahead Parse.not_eof --| atom;
wenzelm@5878
   128
wenzelm@27811
   129
val ident = token
wenzelm@36950
   130
  (Parse.short_ident || Parse.long_ident || Parse.sym_ident || Parse.term_var ||
wenzelm@36950
   131
    Parse.type_ident || Parse.type_var || Parse.number);
wenzelm@5878
   132
wenzelm@36950
   133
val string = token (Parse.string || Parse.verbatim);
wenzelm@36950
   134
val alt_string = token Parse.alt_string;
wenzelm@36950
   135
val symbolic = token Parse.keyword_ident_or_symbolic;
wenzelm@27811
   136
wenzelm@36950
   137
fun $$$ x = (ident >> T.content_of || Parse.keyword)
wenzelm@27811
   138
  :|-- (fn y => if x = y then Scan.succeed x else Scan.fail);
wenzelm@5878
   139
wenzelm@5878
   140
wenzelm@27811
   141
val named = ident || string;
wenzelm@5878
   142
wenzelm@10035
   143
val add = $$$ "add";
wenzelm@10035
   144
val del = $$$ "del";
wenzelm@8803
   145
val colon = $$$ ":";
wenzelm@10035
   146
val query = $$$ "?";
wenzelm@10035
   147
val bang = $$$ "!";
wenzelm@20111
   148
val query_colon = $$$ "?" ^^ $$$ ":";
wenzelm@20111
   149
val bang_colon = $$$ "!" ^^ $$$ ":";
wenzelm@10035
   150
wenzelm@8803
   151
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   152
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@15703
   153
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
wenzelm@15703
   154
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
wenzelm@5878
   155
wenzelm@27882
   156
val name_source = named >> T.source_of;
wenzelm@27882
   157
val name_source_position = named >> T.source_position_of;
wenzelm@27882
   158
wenzelm@27811
   159
val name = named >> T.content_of;
wenzelm@36950
   160
val binding = Parse.position name >> Binding.make;
wenzelm@27811
   161
val alt_name = alt_string >> T.content_of;
wenzelm@27811
   162
val symbol = symbolic >> T.content_of;
wenzelm@17064
   163
val liberal_name = symbol || name;
wenzelm@8233
   164
wenzelm@27811
   165
val var = (ident >> T.content_of) :|-- (fn x =>
wenzelm@27811
   166
  (case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));
wenzelm@5878
   167
wenzelm@5822
   168
wenzelm@15703
   169
(* values *)
wenzelm@15703
   170
wenzelm@27811
   171
fun value dest = Scan.some (fn arg =>
wenzelm@27811
   172
  (case T.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));
wenzelm@15703
   173
wenzelm@15703
   174
fun evaluate mk eval arg =
wenzelm@27819
   175
  let val x = eval arg in (T.assign (SOME (mk x)) arg; x) end;
wenzelm@15703
   176
wenzelm@27811
   177
val internal_text = value (fn T.Text s => s);
wenzelm@27811
   178
val internal_typ = value (fn T.Typ T => T);
wenzelm@27811
   179
val internal_term = value (fn T.Term t => t);
wenzelm@27811
   180
val internal_fact = value (fn T.Fact ths => ths);
wenzelm@27811
   181
val internal_attribute = value (fn T.Attribute att => att);
wenzelm@15703
   182
wenzelm@27819
   183
fun named_text intern = internal_text || named >> evaluate T.Text (intern o T.content_of);
wenzelm@27819
   184
fun named_typ readT = internal_typ || named >> evaluate T.Typ (readT o T.source_of);
wenzelm@27819
   185
fun named_term read = internal_term || named >> evaluate T.Term (read o T.source_of);
wenzelm@27819
   186
fun named_fact get = internal_fact || named >> evaluate T.Fact (get o T.content_of) ||
wenzelm@27819
   187
  alt_string >> evaluate T.Fact (get o T.source_of);
wenzelm@27819
   188
fun named_attribute att = internal_attribute || named >> evaluate T.Attribute (att o T.content_of);
wenzelm@15703
   189
wenzelm@15703
   190
wenzelm@5878
   191
(* terms and types *)
wenzelm@5878
   192
wenzelm@18635
   193
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
wenzelm@25331
   194
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
wenzelm@24508
   195
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
wenzelm@21724
   196
val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
wenzelm@24508
   197
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
wenzelm@18635
   198
wenzelm@5878
   199
wenzelm@15703
   200
(* type and constant names *)
wenzelm@15703
   201
wenzelm@35360
   202
fun type_name strict =
wenzelm@35360
   203
  Scan.peek (fn ctxt => named_typ (ProofContext.read_type_name (Context.proof_of ctxt) strict))
wenzelm@18998
   204
  >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
wenzelm@15703
   205
wenzelm@35399
   206
fun const strict =
wenzelm@36505
   207
  Scan.peek (fn ctxt => named_term (ProofContext.read_const (Context.proof_of ctxt) strict dummyT))
wenzelm@18998
   208
  >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
wenzelm@7553
   209
wenzelm@35399
   210
fun const_proper strict =
wenzelm@35399
   211
  Scan.peek (fn ctxt => named_term (ProofContext.read_const_proper (Context.proof_of ctxt) strict))
wenzelm@25343
   212
  >> (fn Const (c, _) => c | _ => "");
wenzelm@25343
   213
wenzelm@15703
   214
wenzelm@27811
   215
(* improper method arguments *)
wenzelm@15703
   216
wenzelm@8536
   217
val from_to =
wenzelm@36950
   218
  Parse.nat -- ($$$ "-" |-- Parse.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@36950
   219
  Parse.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@36950
   220
  Parse.nat >> (fn i => fn tac => tac i) ||
wenzelm@15703
   221
  $$$ "!" >> K ALLGOALS;
wenzelm@8536
   222
wenzelm@36950
   223
val goal = $$$ "[" |-- Parse.!!! (from_to --| $$$ "]");
wenzelm@30514
   224
fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;
wenzelm@8233
   225
wenzelm@8233
   226
wenzelm@27811
   227
(* arguments within outer syntax *)
wenzelm@5878
   228
wenzelm@27382
   229
fun parse_args is_symid =
wenzelm@27382
   230
  let
wenzelm@36950
   231
    val keyword_symid = token (Parse.keyword_with is_symid);
wenzelm@36950
   232
    fun atom blk = Parse.group "argument"
wenzelm@27811
   233
      (ident || keyword_symid || string || alt_string ||
wenzelm@36950
   234
        (if blk then token (Parse.$$$ ",") else Scan.fail));
wenzelm@5822
   235
wenzelm@27382
   236
    fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@27382
   237
    and args1 blk x =
wenzelm@27382
   238
      ((Scan.repeat1
wenzelm@27382
   239
        (Scan.repeat1 (atom blk) ||
wenzelm@27382
   240
          argsp "(" ")" ||
wenzelm@27382
   241
          argsp "[" "]")) >> flat) x
wenzelm@36950
   242
    and argsp l r x =
wenzelm@36950
   243
      (token (Parse.$$$ l) ::: Parse.!!! (args true @@@ (token (Parse.$$$ r) >> single))) x;
wenzelm@27382
   244
  in (args, args1) end;
wenzelm@15703
   245
wenzelm@27811
   246
val parse = #1 (parse_args T.ident_or_symbolic) false;
wenzelm@27811
   247
fun parse1 is_symid = #2 (parse_args is_symid) false;
wenzelm@5822
   248
wenzelm@27811
   249
wenzelm@27811
   250
(* attributes *)
wenzelm@27382
   251
wenzelm@15703
   252
fun attribs intern =
wenzelm@15703
   253
  let
wenzelm@27819
   254
    val attrib_name = internal_text || (symbolic || named)
wenzelm@27819
   255
      >> evaluate T.Text (intern o T.content_of);
wenzelm@36950
   256
    val attrib = Parse.position (attrib_name -- Parse.!!! parse) >> src;
wenzelm@36950
   257
  in $$$ "[" |-- Parse.!!! (Parse.list attrib --| $$$ "]") end;
wenzelm@15703
   258
wenzelm@15703
   259
fun opt_attribs intern = Scan.optional (attribs intern) [];
wenzelm@15703
   260
wenzelm@5822
   261
wenzelm@27377
   262
(* theorem specifications *)
wenzelm@27377
   263
wenzelm@28078
   264
fun thm_name intern s = binding -- opt_attribs intern --| $$$ s;
wenzelm@27811
   265
wenzelm@27377
   266
fun opt_thm_name intern s =
wenzelm@28078
   267
  Scan.optional
haftmann@28965
   268
    ((binding -- opt_attribs intern || attribs intern >> pair Binding.empty) --| $$$ s)
haftmann@28965
   269
    (Binding.empty, []);
wenzelm@27377
   270
wenzelm@27377
   271
wenzelm@27382
   272
wenzelm@27382
   273
(** syntax wrapper **)
wenzelm@5822
   274
wenzelm@32784
   275
fun syntax kind scan (Src ((s, args), pos)) st =
wenzelm@27811
   276
  (case Scan.error (Scan.finite' T.stopper (Scan.option scan)) (st, args) of
haftmann@21879
   277
    (SOME x, (st', [])) => (x, st')
wenzelm@15703
   278
  | (_, (_, args')) =>
wenzelm@15703
   279
      error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
wenzelm@27811
   280
        space_implode " " (map T.unparse args')));
wenzelm@15703
   281
haftmann@21879
   282
fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
wenzelm@18998
   283
wenzelm@5822
   284
end;