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