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