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