src/Pure/Isar/args.ML
author kuncar
Mon, 24 Feb 2014 18:12:39 +0100
changeset 55721 1c2cfc06c96a
parent 55708 f4b114070675
child 55745 b865c3035d5c
permissions -rw-r--r--
don't be so aggresive for a public test function and raise only BAD_THM instead of ERROR
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/args.ML
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     3
35013
f3d491658893 fixed spelling;
wenzelm
parents: 33550
diff changeset
     4
Parsing with implicit value assignment.  Concrete argument syntax of
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
     5
attributes, methods etc.
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     6
*)
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     7
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     8
signature ARGS =
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
     9
sig
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    10
  type src
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    11
  val src: (string * Token.T list) * Position.T -> src
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    12
  val dest_src: src -> (string * Token.T list) * Position.T
21030
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    13
  val pretty_src: Proof.context -> src -> Pretty.T
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    14
  val map_name: (string -> string) -> src -> src
45290
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44357
diff changeset
    15
  val transform_values: morphism -> src -> src
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    16
  val assignable: src -> src
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    17
  val closure: src -> src
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    18
  val context: Proof.context context_parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    19
  val theory: theory context_parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    20
  val $$$ : string -> string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    21
  val add: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    22
  val del: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    23
  val colon: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    24
  val query: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    25
  val bang: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    26
  val query_colon: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    27
  val bang_colon: string parser
53168
d998de7f0efc tuned signature;
wenzelm
parents: 53113
diff changeset
    28
  val parens: 'a parser -> 'a parser
d998de7f0efc tuned signature;
wenzelm
parents: 53113
diff changeset
    29
  val bracks: 'a parser -> 'a parser
d998de7f0efc tuned signature;
wenzelm
parents: 53113
diff changeset
    30
  val mode: string -> bool parser
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    31
  val maybe: 'a parser -> 'a option parser
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
    32
  val cartouche_inner_syntax: string parser
55045
99056d23e05b cartouche within nested args (attributes, methods, etc.);
wenzelm
parents: 55033
diff changeset
    33
  val cartouche_source_position: (Symbol_Pos.text * Position.T) parser
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
    34
  val name_inner_syntax: string parser
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30514
diff changeset
    35
  val name_source_position: (Symbol_Pos.text * Position.T) parser
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    36
  val name: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    37
  val binding: binding parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    38
  val alt_name: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    39
  val symbol: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    40
  val liberal_name: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    41
  val var: indexname parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    42
  val internal_text: string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    43
  val internal_typ: typ parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    44
  val internal_term: term parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    45
  val internal_fact: thm list parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    46
  val internal_attribute: (morphism -> attribute) parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    47
  val named_text: (string -> string) -> string parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    48
  val named_typ: (string -> typ) -> typ parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    49
  val named_term: (string -> term) -> term parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    50
  val named_fact: (string -> thm list) -> thm list parser
42464
ae16b8abf1a8 proper binding/report of defined simprocs;
wenzelm
parents: 42360
diff changeset
    51
  val named_attribute:
ae16b8abf1a8 proper binding/report of defined simprocs;
wenzelm
parents: 42360
diff changeset
    52
    (string * Position.T -> morphism -> attribute) -> (morphism -> attribute) parser
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    53
  val typ_abbrev: typ context_parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    54
  val typ: typ context_parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    55
  val term: term context_parser
55155
a1affe3eb3fd tuned signature;
wenzelm
parents: 55111
diff changeset
    56
  val term_pattern: term context_parser
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    57
  val term_abbrev: term context_parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    58
  val prop: term context_parser
35360
df2b2168e43a clarified ProofContext.read_type_name/Args.type_name wrt strict logical constructors;
wenzelm
parents: 35013
diff changeset
    59
  val type_name: bool -> string context_parser
35399
3881972fcfca clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
wenzelm
parents: 35360
diff changeset
    60
  val const: bool -> string context_parser
3881972fcfca clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
wenzelm
parents: 35360
diff changeset
    61
  val const_proper: bool -> string context_parser
30514
9455ecc7796d simplified goal_spec: default to first goal;
wenzelm
parents: 30513
diff changeset
    62
  val goal_spec: ((int -> tactic) -> tactic) context_parser
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    63
  val parse: Token.T list parser
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    64
  val parse1: (string -> bool) -> Token.T list parser
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    65
  val attribs: (string -> string) -> src list parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    66
  val opt_attribs: (string -> string) -> src list parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    67
  val thm_name: (string -> string) -> string -> (binding * src list) parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    68
  val opt_thm_name: (string -> string) -> string -> (binding * src list) parser
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    69
  val syntax: string -> 'a context_parser -> src -> Context.generic -> 'a * Context.generic
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30473
diff changeset
    70
  val context_syntax: string -> 'a context_parser -> src -> Proof.context -> 'a * Proof.context
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
    71
end;
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
    72
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
    73
structure Args: ARGS =
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
    74
struct
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
    75
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    76
(** datatype src **)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    77
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    78
datatype src = Src of (string * Token.T list) * Position.T;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    79
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    80
val src = Src;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    81
fun dest_src (Src src) = src;
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    82
21030
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    83
fun pretty_src ctxt src =
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    84
  let
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 30573
diff changeset
    85
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
    86
    fun prt arg =
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    87
      (case Token.get_value arg of
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    88
        SOME (Token.Text s) => Pretty.str (quote s)
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    89
      | SOME (Token.Typ T) => Syntax.pretty_typ ctxt T
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    90
      | SOME (Token.Term t) => Syntax.pretty_term ctxt t
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    91
      | SOME (Token.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
    92
      | _ => Pretty.str (Token.unparse arg));
21030
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    93
    val (s, args) = #1 (dest_src src);
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    94
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
8b21407de526 moved pretty_attribs to attrib.ML;
wenzelm
parents: 20664
diff changeset
    95
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    96
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    97
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    98
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
    99
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   100
(* values *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   101
45290
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44357
diff changeset
   102
fun transform_values phi = map_args (Token.map_value
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   103
  (fn Token.Text s => Token.Text s
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   104
    | Token.Typ T => Token.Typ (Morphism.typ phi T)
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   105
    | Token.Term t => Token.Term (Morphism.term phi t)
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   106
    | Token.Fact ths => Token.Fact (Morphism.fact phi ths)
53112
04d8af9ff64b proper exhaustive match (cf. e9beabf045ab);
wenzelm
parents: 48992
diff changeset
   107
    | Token.Attribute att => Token.Attribute (Morphism.transform phi att)
04d8af9ff64b proper exhaustive match (cf. e9beabf045ab);
wenzelm
parents: 48992
diff changeset
   108
    | tok as Token.Files _ => tok));
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   109
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   110
val assignable = map_args Token.assignable;
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   111
val closure = map_args Token.closure;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   112
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   113
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   114
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   115
(** argument scanners **)
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   116
27371
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   117
(* context *)
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   118
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   119
fun context x = (Scan.state >> Context.proof_of) x;
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   120
fun theory x = (Scan.state >> Context.theory_of) x;
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   121
f89aa7bd4602 added context/theory scanner;
wenzelm
parents: 27234
diff changeset
   122
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   123
(* basic *)
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   124
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   125
fun token atom = Scan.ahead Parse.not_eof --| atom;
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   126
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   127
val ident = token
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   128
  (Parse.short_ident || Parse.long_ident || Parse.sym_ident || Parse.term_var ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   129
    Parse.type_ident || Parse.type_var || Parse.number);
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   130
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   131
val string = token (Parse.string || Parse.verbatim);
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   132
val alt_string = token Parse.alt_string;
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   133
val symbolic = token Parse.keyword_ident_or_symbolic;
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   134
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   135
fun $$$ x = (ident >> Token.content_of || Parse.keyword)
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   136
  :|-- (fn y => if x = y then Scan.succeed x else Scan.fail);
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   137
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   138
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   139
val named = ident || string;
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   140
10035
c095955e7575 added common args keywords;
wenzelm
parents: 9952
diff changeset
   141
val add = $$$ "add";
c095955e7575 added common args keywords;
wenzelm
parents: 9952
diff changeset
   142
val del = $$$ "del";
8803
wenzelm
parents: 8687
diff changeset
   143
val colon = $$$ ":";
10035
c095955e7575 added common args keywords;
wenzelm
parents: 9952
diff changeset
   144
val query = $$$ "?";
c095955e7575 added common args keywords;
wenzelm
parents: 9952
diff changeset
   145
val bang = $$$ "!";
20111
ba1676dd3546 query/bang_colon: separate tokens;
wenzelm
parents: 19482
diff changeset
   146
val query_colon = $$$ "?" ^^ $$$ ":";
ba1676dd3546 query/bang_colon: separate tokens;
wenzelm
parents: 19482
diff changeset
   147
val bang_colon = $$$ "!" ^^ $$$ ":";
10035
c095955e7575 added common args keywords;
wenzelm
parents: 9952
diff changeset
   148
8803
wenzelm
parents: 8687
diff changeset
   149
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
10150
a068c666c53e added "bracks";
wenzelm
parents: 10035
diff changeset
   150
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
53168
d998de7f0efc tuned signature;
wenzelm
parents: 53113
diff changeset
   151
fun mode s = Scan.optional (parens ($$$ s) >> K true) false;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   152
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   153
55045
99056d23e05b cartouche within nested args (attributes, methods, etc.);
wenzelm
parents: 55033
diff changeset
   154
val cartouche = token Parse.cartouche;
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
   155
val cartouche_inner_syntax = cartouche >> Token.inner_syntax_of;
55045
99056d23e05b cartouche within nested args (attributes, methods, etc.);
wenzelm
parents: 55033
diff changeset
   156
val cartouche_source_position = cartouche >> Token.source_position_of;
99056d23e05b cartouche within nested args (attributes, methods, etc.);
wenzelm
parents: 55033
diff changeset
   157
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
   158
val name_inner_syntax = named >> Token.inner_syntax_of;
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   159
val name_source_position = named >> Token.source_position_of;
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27819
diff changeset
   160
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   161
val name = named >> Token.content_of;
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   162
val binding = Parse.position name >> Binding.make;
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   163
val alt_name = alt_string >> Token.content_of;
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   164
val symbol = symbolic >> Token.content_of;
17064
2fae6579fb2e added liberal_name;
wenzelm
parents: 16343
diff changeset
   165
val liberal_name = symbol || name;
8233
85169951d515 attrib: keyword_symid;
wenzelm
parents: 8089
diff changeset
   166
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   167
val var = (ident >> Token.content_of) :|-- (fn x =>
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   168
  (case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   169
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   170
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   171
(* values *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   172
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   173
fun value dest = Scan.some (fn arg =>
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   174
  (case Token.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   175
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   176
fun evaluate mk eval arg =
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   177
  let val x = eval arg in (Token.assign (SOME (mk x)) arg; x) end;
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   178
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   179
val internal_text = value (fn Token.Text s => s);
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   180
val internal_typ = value (fn Token.Typ T => T);
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   181
val internal_term = value (fn Token.Term t => t);
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   182
val internal_fact = value (fn Token.Fact ths => ths);
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   183
val internal_attribute = value (fn Token.Attribute att => att);
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   184
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   185
fun named_text intern = internal_text || named >> evaluate Token.Text (intern o Token.content_of);
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
   186
fun named_typ readT = internal_typ || named >> evaluate Token.Typ (readT o Token.inner_syntax_of);
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
   187
fun named_term read = internal_term || named >> evaluate Token.Term (read o Token.inner_syntax_of);
42464
ae16b8abf1a8 proper binding/report of defined simprocs;
wenzelm
parents: 42360
diff changeset
   188
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   189
fun named_fact get = internal_fact || named >> evaluate Token.Fact (get o Token.content_of) ||
55111
5792f5106c40 tuned signature;
wenzelm
parents: 55045
diff changeset
   190
  alt_string >> evaluate Token.Fact (get o Token.inner_syntax_of);
42464
ae16b8abf1a8 proper binding/report of defined simprocs;
wenzelm
parents: 42360
diff changeset
   191
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   192
fun named_attribute att =
42464
ae16b8abf1a8 proper binding/report of defined simprocs;
wenzelm
parents: 42360
diff changeset
   193
  internal_attribute ||
55708
f4b114070675 tuned signature;
wenzelm
parents: 55698
diff changeset
   194
  named >> evaluate Token.Attribute (fn tok => att (Token.content_of tok, Token.pos_of tok));
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   195
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   196
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   197
(* terms and types *)
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   198
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40291
diff changeset
   199
val typ_abbrev = Scan.peek (named_typ o Proof_Context.read_typ_abbrev o Context.proof_of);
25331
73072178e0ce Syntax.read_typ;
wenzelm
parents: 25323
diff changeset
   200
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24244
diff changeset
   201
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
55155
a1affe3eb3fd tuned signature;
wenzelm
parents: 55111
diff changeset
   202
val term_pattern = Scan.peek (named_term o Proof_Context.read_term_pattern o Context.proof_of);
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40291
diff changeset
   203
val term_abbrev = Scan.peek (named_term o Proof_Context.read_term_abbrev o Context.proof_of);
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24244
diff changeset
   204
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
18635
58bbff56a914 added generic syntax;
wenzelm
parents: 18037
diff changeset
   205
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   206
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   207
(* type and constant names *)
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   208
35360
df2b2168e43a clarified ProofContext.read_type_name/Args.type_name wrt strict logical constructors;
wenzelm
parents: 35013
diff changeset
   209
fun type_name strict =
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40291
diff changeset
   210
  Scan.peek (fn ctxt => named_typ (Proof_Context.read_type_name (Context.proof_of ctxt) strict))
18998
10c251f29847 Context.generic is canonical state of parsers;
wenzelm
parents: 18728
diff changeset
   211
  >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   212
35399
3881972fcfca clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
wenzelm
parents: 35360
diff changeset
   213
fun const strict =
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40291
diff changeset
   214
  Scan.peek (fn ctxt => named_term (Proof_Context.read_const (Context.proof_of ctxt) strict dummyT))
18998
10c251f29847 Context.generic is canonical state of parsers;
wenzelm
parents: 18728
diff changeset
   215
  >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
7553
af3a1fe87c42 added bang_facts;
wenzelm
parents: 6447
diff changeset
   216
35399
3881972fcfca clarified ProofContext.read_const(_proper)/Args.const(_proper) wrt. strict logical consts;
wenzelm
parents: 35360
diff changeset
   217
fun const_proper strict =
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40291
diff changeset
   218
  Scan.peek (fn ctxt => named_term (Proof_Context.read_const_proper (Context.proof_of ctxt) strict))
25343
31c55418de5a added const_proper;
wenzelm
parents: 25331
diff changeset
   219
  >> (fn Const (c, _) => c | _ => "");
31c55418de5a added const_proper;
wenzelm
parents: 25331
diff changeset
   220
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   221
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   222
(* improper method arguments *)
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   223
8536
de307f5bc89a goal_spec;
wenzelm
parents: 8282
diff changeset
   224
val from_to =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   225
  Parse.nat -- ($$$ "-" |-- Parse.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   226
  Parse.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   227
  Parse.nat >> (fn i => fn tac => tac i) ||
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   228
  $$$ "!" >> K ALLGOALS;
8536
de307f5bc89a goal_spec;
wenzelm
parents: 8282
diff changeset
   229
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   230
val goal = $$$ "[" |-- Parse.!!! (from_to --| $$$ "]");
30514
9455ecc7796d simplified goal_spec: default to first goal;
wenzelm
parents: 30513
diff changeset
   231
fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;
8233
85169951d515 attrib: keyword_symid;
wenzelm
parents: 8089
diff changeset
   232
85169951d515 attrib: keyword_symid;
wenzelm
parents: 8089
diff changeset
   233
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   234
(* arguments within outer syntax *)
5878
769abc29bb8e several args parsers;
wenzelm
parents: 5822
diff changeset
   235
53113
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   236
val argument_kinds =
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   237
 [Token.Ident, Token.LongIdent, Token.SymIdent, Token.Var, Token.TypeIdent, Token.TypeVar,
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53168
diff changeset
   238
  Token.Nat, Token.Float, Token.String, Token.AltString, Token.Cartouche, Token.Verbatim];
53113
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   239
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   240
fun parse_args is_symid =
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   241
  let
53113
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   242
    fun argument blk =
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   243
      Parse.group (fn () => "argument")
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   244
        (Scan.one (fn tok =>
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   245
          let val kind = Token.kind_of tok in
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   246
            member (op =) argument_kinds kind orelse
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   247
            Token.keyword_with is_symid tok orelse
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   248
            (blk andalso Token.keyword_with (fn s => s = ",") tok)
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   249
          end));
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   250
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   251
    fun args blk x = Scan.optional (args1 blk) [] x
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   252
    and args1 blk x =
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   253
      ((Scan.repeat1
53113
d9ba3417cb41 more careful parsing of nested argument tokens -- avoid Parse.RESET_VALUE on still uninterpreted material;
wenzelm
parents: 53112
diff changeset
   254
        (Scan.repeat1 (argument blk) ||
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   255
          argsp "(" ")" ||
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   256
          argsp "[" "]")) >> flat) x
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   257
    and argsp l r x =
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   258
      (token (Parse.$$$ l) ::: Parse.!!! (args true @@@ (token (Parse.$$$ r) >> single))) x;
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   259
  in (args, args1) end;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   260
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   261
val parse = #1 (parse_args Token.ident_or_symbolic) false;
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   262
fun parse1 is_symid = #2 (parse_args is_symid) false;
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   263
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   264
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   265
(* attributes *)
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   266
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   267
fun attribs intern =
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   268
  let
27819
6398f7aabdfc pass token source to typ/term etc.;
wenzelm
parents: 27811
diff changeset
   269
    val attrib_name = internal_text || (symbolic || named)
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   270
      >> evaluate Token.Text (intern o Token.content_of);
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   271
    val attrib = Parse.position (attrib_name -- Parse.!!! parse) >> src;
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36505
diff changeset
   272
  in $$$ "[" |-- Parse.!!! (Parse.list attrib --| $$$ "]") end;
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   273
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   274
fun opt_attribs intern = Scan.optional (attribs intern) [];
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   275
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   276
27377
0b9295c598f6 added thm_name, opt_thm_name;
wenzelm
parents: 27371
diff changeset
   277
(* theorem specifications *)
0b9295c598f6 added thm_name, opt_thm_name;
wenzelm
parents: 27371
diff changeset
   278
28078
0c420e7579a6 added binding;
wenzelm
parents: 27882
diff changeset
   279
fun thm_name intern s = binding -- opt_attribs intern --| $$$ s;
27811
44bc67675210 unified Args.T with OuterLex.token;
wenzelm
parents: 27732
diff changeset
   280
27377
0b9295c598f6 added thm_name, opt_thm_name;
wenzelm
parents: 27371
diff changeset
   281
fun opt_thm_name intern s =
28078
0c420e7579a6 added binding;
wenzelm
parents: 27882
diff changeset
   282
  Scan.optional
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   283
    ((binding -- opt_attribs intern || attribs intern >> pair Binding.empty) --| $$$ s)
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   284
    (Binding.empty, []);
27377
0b9295c598f6 added thm_name, opt_thm_name;
wenzelm
parents: 27371
diff changeset
   285
0b9295c598f6 added thm_name, opt_thm_name;
wenzelm
parents: 27371
diff changeset
   286
27382
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   287
b1285021424e tuned nested args parser;
wenzelm
parents: 27377
diff changeset
   288
(** syntax wrapper **)
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   289
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32091
diff changeset
   290
fun syntax kind scan (Src ((s, args), pos)) st =
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   291
  (case Scan.error (Scan.finite' Token.stopper (Scan.option scan)) (st, args) of
21879
a3efbae45735 switched argument order in *.syntax lifters
haftmann
parents: 21724
diff changeset
   292
    (SOME x, (st', [])) => (x, st')
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   293
  | (_, (_, args')) =>
55698
e42e5671612c tuned message;
wenzelm
parents: 55155
diff changeset
   294
      error (kind ^ " " ^ quote s ^ Position.here pos ^ ": bad arguments" ^
e42e5671612c tuned message;
wenzelm
parents: 55155
diff changeset
   295
        (if null args' then "" else "\n  " ^ space_implode " " (map Token.unparse args'))));
15703
727ef1b8b3ee *** empty log message ***
wenzelm
parents: 15596
diff changeset
   296
21879
a3efbae45735 switched argument order in *.syntax lifters
haftmann
parents: 21724
diff changeset
   297
fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
18998
10c251f29847 Context.generic is canonical state of parsers;
wenzelm
parents: 18728
diff changeset
   298
5822
3f824514ad88 Concrete argument syntax (for attributes, methods etc.).
wenzelm
parents:
diff changeset
   299
end;