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