src/Pure/Isar/args.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36505 79c1d2bbe5a9
child 36950 75b8f26f2f07
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
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@27811
    74
structure P = OuterParse;
wenzelm@5822
    75
wenzelm@30513
    76
type token = T.token;
wenzelm@30513
    77
wenzelm@5822
    78
wenzelm@5822
    79
wenzelm@15703
    80
(** datatype src **)
wenzelm@15703
    81
wenzelm@30513
    82
datatype src = Src of (string * token list) * Position.T;
wenzelm@15703
    83
wenzelm@15703
    84
val src = Src;
wenzelm@15703
    85
fun dest_src (Src src) = src;
wenzelm@15703
    86
wenzelm@21030
    87
fun pretty_src ctxt src =
wenzelm@21030
    88
  let
wenzelm@32091
    89
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
wenzelm@27811
    90
    fun prt arg =
wenzelm@27811
    91
      (case T.get_value arg of
wenzelm@27811
    92
        SOME (T.Text s) => Pretty.str (quote s)
wenzelm@27811
    93
      | SOME (T.Typ T) => Syntax.pretty_typ ctxt T
wenzelm@27811
    94
      | SOME (T.Term t) => Syntax.pretty_term ctxt t
wenzelm@27811
    95
      | SOME (T.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
wenzelm@27811
    96
      | _ => Pretty.str (T.unparse arg));
wenzelm@21030
    97
    val (s, args) = #1 (dest_src src);
wenzelm@21030
    98
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
wenzelm@21030
    99
wenzelm@15703
   100
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
wenzelm@15703
   101
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
wenzelm@15703
   102
wenzelm@15703
   103
wenzelm@15703
   104
(* values *)
wenzelm@15703
   105
wenzelm@27811
   106
fun morph_values phi = map_args (T.map_value
wenzelm@27811
   107
  (fn T.Text s => T.Text s
wenzelm@27811
   108
    | T.Typ T => T.Typ (Morphism.typ phi T)
wenzelm@27811
   109
    | T.Term t => T.Term (Morphism.term phi t)
wenzelm@27811
   110
    | T.Fact ths => T.Fact (Morphism.fact phi ths)
wenzelm@27811
   111
    | T.Attribute att => T.Attribute (Morphism.transform phi att)));
wenzelm@15703
   112
wenzelm@27811
   113
val assignable = map_args T.assignable;
wenzelm@27811
   114
val closure = map_args T.closure;
wenzelm@15703
   115
wenzelm@15703
   116
wenzelm@15703
   117
wenzelm@27811
   118
(** argument scanners **)
wenzelm@5822
   119
wenzelm@27371
   120
(* context *)
wenzelm@27371
   121
wenzelm@27371
   122
fun context x = (Scan.state >> Context.proof_of) x;
wenzelm@27371
   123
fun theory x = (Scan.state >> Context.theory_of) x;
wenzelm@27371
   124
wenzelm@27371
   125
wenzelm@27811
   126
(* basic *)
wenzelm@5878
   127
wenzelm@27811
   128
fun token atom = Scan.ahead P.not_eof --| atom;
wenzelm@5878
   129
wenzelm@27811
   130
val ident = token
wenzelm@27811
   131
  (P.short_ident || P.long_ident || P.sym_ident || P.term_var ||
wenzelm@27811
   132
    P.type_ident || P.type_var || P.number);
wenzelm@5878
   133
wenzelm@27811
   134
val string = token (P.string || P.verbatim);
wenzelm@27811
   135
val alt_string = token P.alt_string;
wenzelm@27811
   136
val symbolic = token P.keyword_ident_or_symbolic;
wenzelm@27811
   137
wenzelm@27811
   138
fun $$$ x = (ident >> T.content_of || P.keyword)
wenzelm@27811
   139
  :|-- (fn y => if x = y then Scan.succeed x else Scan.fail);
wenzelm@5878
   140
wenzelm@5878
   141
wenzelm@27811
   142
val named = ident || string;
wenzelm@5878
   143
wenzelm@10035
   144
val add = $$$ "add";
wenzelm@10035
   145
val del = $$$ "del";
wenzelm@8803
   146
val colon = $$$ ":";
wenzelm@10035
   147
val query = $$$ "?";
wenzelm@10035
   148
val bang = $$$ "!";
wenzelm@20111
   149
val query_colon = $$$ "?" ^^ $$$ ":";
wenzelm@20111
   150
val bang_colon = $$$ "!" ^^ $$$ ":";
wenzelm@10035
   151
wenzelm@8803
   152
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   153
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@15703
   154
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
wenzelm@15703
   155
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
wenzelm@5878
   156
wenzelm@27882
   157
val name_source = named >> T.source_of;
wenzelm@27882
   158
val name_source_position = named >> T.source_position_of;
wenzelm@27882
   159
wenzelm@27811
   160
val name = named >> T.content_of;
wenzelm@30223
   161
val binding = P.position name >> Binding.make;
wenzelm@27811
   162
val alt_name = alt_string >> T.content_of;
wenzelm@27811
   163
val symbol = symbolic >> T.content_of;
wenzelm@17064
   164
val liberal_name = symbol || name;
wenzelm@8233
   165
wenzelm@27811
   166
val var = (ident >> T.content_of) :|-- (fn x =>
wenzelm@27811
   167
  (case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));
wenzelm@5878
   168
wenzelm@5822
   169
wenzelm@15703
   170
(* values *)
wenzelm@15703
   171
wenzelm@27811
   172
fun value dest = Scan.some (fn arg =>
wenzelm@27811
   173
  (case T.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));
wenzelm@15703
   174
wenzelm@15703
   175
fun evaluate mk eval arg =
wenzelm@27819
   176
  let val x = eval arg in (T.assign (SOME (mk x)) arg; x) end;
wenzelm@15703
   177
wenzelm@27811
   178
val internal_text = value (fn T.Text s => s);
wenzelm@27811
   179
val internal_typ = value (fn T.Typ T => T);
wenzelm@27811
   180
val internal_term = value (fn T.Term t => t);
wenzelm@27811
   181
val internal_fact = value (fn T.Fact ths => ths);
wenzelm@27811
   182
val internal_attribute = value (fn T.Attribute att => att);
wenzelm@15703
   183
wenzelm@27819
   184
fun named_text intern = internal_text || named >> evaluate T.Text (intern o T.content_of);
wenzelm@27819
   185
fun named_typ readT = internal_typ || named >> evaluate T.Typ (readT o T.source_of);
wenzelm@27819
   186
fun named_term read = internal_term || named >> evaluate T.Term (read o T.source_of);
wenzelm@27819
   187
fun named_fact get = internal_fact || named >> evaluate T.Fact (get o T.content_of) ||
wenzelm@27819
   188
  alt_string >> evaluate T.Fact (get o T.source_of);
wenzelm@27819
   189
fun named_attribute att = internal_attribute || named >> evaluate T.Attribute (att o T.content_of);
wenzelm@15703
   190
wenzelm@15703
   191
wenzelm@5878
   192
(* terms and types *)
wenzelm@5878
   193
wenzelm@18635
   194
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
wenzelm@25331
   195
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
wenzelm@24508
   196
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
wenzelm@21724
   197
val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
wenzelm@24508
   198
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
wenzelm@18635
   199
wenzelm@5878
   200
wenzelm@15703
   201
(* type and constant names *)
wenzelm@15703
   202
wenzelm@35360
   203
fun type_name strict =
wenzelm@35360
   204
  Scan.peek (fn ctxt => named_typ (ProofContext.read_type_name (Context.proof_of ctxt) strict))
wenzelm@18998
   205
  >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
wenzelm@15703
   206
wenzelm@35399
   207
fun const strict =
wenzelm@36505
   208
  Scan.peek (fn ctxt => named_term (ProofContext.read_const (Context.proof_of ctxt) strict dummyT))
wenzelm@18998
   209
  >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
wenzelm@7553
   210
wenzelm@35399
   211
fun const_proper strict =
wenzelm@35399
   212
  Scan.peek (fn ctxt => named_term (ProofContext.read_const_proper (Context.proof_of ctxt) strict))
wenzelm@25343
   213
  >> (fn Const (c, _) => c | _ => "");
wenzelm@25343
   214
wenzelm@15703
   215
wenzelm@27811
   216
(* improper method arguments *)
wenzelm@15703
   217
wenzelm@8536
   218
val from_to =
wenzelm@27811
   219
  P.nat -- ($$$ "-" |-- P.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@27811
   220
  P.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@27811
   221
  P.nat >> (fn i => fn tac => tac i) ||
wenzelm@15703
   222
  $$$ "!" >> K ALLGOALS;
wenzelm@8536
   223
wenzelm@27811
   224
val goal = $$$ "[" |-- P.!!! (from_to --| $$$ "]");
wenzelm@30514
   225
fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;
wenzelm@8233
   226
wenzelm@8233
   227
wenzelm@27811
   228
(* arguments within outer syntax *)
wenzelm@5878
   229
wenzelm@27382
   230
fun parse_args is_symid =
wenzelm@27382
   231
  let
wenzelm@27811
   232
    val keyword_symid = token (P.keyword_with is_symid);
wenzelm@27811
   233
    fun atom blk = P.group "argument"
wenzelm@27811
   234
      (ident || keyword_symid || string || alt_string ||
wenzelm@27811
   235
        (if blk then token (P.$$$ ",") else Scan.fail));
wenzelm@5822
   236
wenzelm@27382
   237
    fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@27382
   238
    and args1 blk x =
wenzelm@27382
   239
      ((Scan.repeat1
wenzelm@27382
   240
        (Scan.repeat1 (atom blk) ||
wenzelm@27382
   241
          argsp "(" ")" ||
wenzelm@27382
   242
          argsp "[" "]")) >> flat) x
wenzelm@27811
   243
    and argsp l r x = (token (P.$$$ l) ::: P.!!! (args true @@@ (token (P.$$$ 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@27811
   256
    val attrib = P.position (attrib_name -- P.!!! parse) >> src;
wenzelm@27811
   257
  in $$$ "[" |-- P.!!! (P.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;