src/Pure/Isar/args.ML
author wenzelm
Thu Mar 06 13:44:01 2014 +0100 (2014-03-06)
changeset 55954 a29aefc88c8d
parent 55951 c07d184aebe9
child 55955 e8f1bf005661
permissions -rw-r--r--
more uniform check_const/read_const;
     1 (*  Title:      Pure/Isar/args.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Parsing with implicit value assignment.  Concrete argument syntax of
     5 attributes, methods etc.
     6 *)
     7 
     8 signature ARGS =
     9 sig
    10   type src
    11   val src: (string * Token.T list) * Position.T -> src
    12   val dest_src: src -> (string * Token.T list) * Position.T
    13   val pretty_src: Proof.context -> src -> Pretty.T
    14   val map_name: (string -> string) -> src -> src
    15   val transform_values: morphism -> src -> src
    16   val init_assignable: src -> src
    17   val closure: src -> src
    18   val context: Proof.context context_parser
    19   val theory: theory context_parser
    20   val $$$ : string -> string parser
    21   val add: string parser
    22   val del: string parser
    23   val colon: string parser
    24   val query: string parser
    25   val bang: string parser
    26   val query_colon: string parser
    27   val bang_colon: string parser
    28   val parens: 'a parser -> 'a parser
    29   val bracks: 'a parser -> 'a parser
    30   val mode: string -> bool parser
    31   val maybe: 'a parser -> 'a option parser
    32   val cartouche_inner_syntax: string parser
    33   val cartouche_source_position: Symbol_Pos.source parser
    34   val name_inner_syntax: string parser
    35   val name_source_position: Symbol_Pos.source parser
    36   val name: string parser
    37   val binding: binding parser
    38   val alt_name: string parser
    39   val symbol: string parser
    40   val liberal_name: string parser
    41   val var: indexname parser
    42   val internal_text: string parser
    43   val internal_typ: typ parser
    44   val internal_term: term parser
    45   val internal_fact: thm list parser
    46   val internal_attribute: (morphism -> attribute) parser
    47   val named_text: (string -> string) -> string parser
    48   val named_typ: (string -> typ) -> typ parser
    49   val named_term: (string -> term) -> term parser
    50   val named_fact: (string -> thm list) -> thm list parser
    51   val named_attribute:
    52     (string * Position.T -> morphism -> attribute) -> (morphism -> attribute) parser
    53   val typ_abbrev: typ context_parser
    54   val typ: typ context_parser
    55   val term: term context_parser
    56   val term_pattern: term context_parser
    57   val term_abbrev: term context_parser
    58   val prop: term context_parser
    59   val type_name: {proper: bool, strict: bool} -> string context_parser
    60   val const: {proper: bool, strict: bool} -> string context_parser
    61   val goal_spec: ((int -> tactic) -> tactic) context_parser
    62   val parse: Token.T list parser
    63   val parse1: (string -> bool) -> Token.T list parser
    64   val attribs: (string -> string) -> src list parser
    65   val opt_attribs: (string -> string) -> src list parser
    66   val thm_name: (string -> string) -> string -> (binding * src list) parser
    67   val opt_thm_name: (string -> string) -> string -> (binding * src list) parser
    68   val syntax: string -> 'a context_parser -> src -> Context.generic -> 'a * Context.generic
    69   val context_syntax: string -> 'a context_parser -> src -> Proof.context -> 'a * Proof.context
    70 end;
    71 
    72 structure Args: ARGS =
    73 struct
    74 
    75 (** datatype src **)
    76 
    77 datatype src = Src of (string * Token.T list) * Position.T;
    78 
    79 val src = Src;
    80 fun dest_src (Src src) = src;
    81 
    82 fun pretty_src ctxt src =
    83   let
    84     val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
    85     fun prt arg =
    86       (case Token.get_value arg of
    87         SOME (v as Token.Literal s) => Pretty.marks_str (Token.value_markup v, s)
    88       | SOME (Token.Text s) => Pretty.str (quote s)
    89       | SOME (Token.Typ T) => Syntax.pretty_typ ctxt T
    90       | SOME (Token.Term t) => Syntax.pretty_term ctxt t
    91       | SOME (Token.Fact ths) => Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
    92       | _ => Pretty.mark_str (Token.markup arg, Token.unparse arg));
    93     val (s, args) = #1 (dest_src src);
    94   in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
    95 
    96 fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
    97 fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
    98 
    99 
   100 (* values *)
   101 
   102 fun transform_values phi = map_args (Token.map_value
   103   (fn Token.Typ T => Token.Typ (Morphism.typ phi T)
   104     | Token.Term t => Token.Term (Morphism.term phi t)
   105     | Token.Fact ths => Token.Fact (Morphism.fact phi ths)
   106     | Token.Attribute att => Token.Attribute (Morphism.transform phi att)
   107     | tok => tok));
   108 
   109 val init_assignable = map_args Token.init_assignable;
   110 val closure = map_args Token.closure;
   111 
   112 
   113 
   114 (** argument scanners **)
   115 
   116 (* context *)
   117 
   118 fun context x = (Scan.state >> Context.proof_of) x;
   119 fun theory x = (Scan.state >> Context.theory_of) x;
   120 
   121 
   122 (* basic *)
   123 
   124 fun token atom = Scan.ahead Parse.not_eof --| atom;
   125 
   126 val ident = token
   127   (Parse.short_ident || Parse.long_ident || Parse.sym_ident || Parse.term_var ||
   128     Parse.type_ident || Parse.type_var || Parse.number);
   129 
   130 val string = token (Parse.string || Parse.verbatim);
   131 val alt_string = token Parse.alt_string;
   132 val symbolic = token Parse.keyword_ident_or_symbolic;
   133 
   134 fun $$$ x =
   135   (ident || token Parse.keyword) :|-- (fn tok =>
   136     let val y = Token.content_of tok
   137     in if x = y then (Token.assign (SOME (Token.Literal x)) tok; Scan.succeed x) else Scan.fail end);
   138 
   139 
   140 val named = ident || string;
   141 
   142 val add = $$$ "add";
   143 val del = $$$ "del";
   144 val colon = $$$ ":";
   145 val query = $$$ "?";
   146 val bang = $$$ "!";
   147 val query_colon = $$$ "?" ^^ $$$ ":";
   148 val bang_colon = $$$ "!" ^^ $$$ ":";
   149 
   150 fun parens scan = $$$ "(" |-- scan --| $$$ ")";
   151 fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
   152 fun mode s = Scan.optional (parens ($$$ s) >> K true) false;
   153 fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
   154 
   155 val cartouche = token Parse.cartouche;
   156 val cartouche_inner_syntax = cartouche >> Token.inner_syntax_of;
   157 val cartouche_source_position = cartouche >> Token.source_position_of;
   158 
   159 val name_inner_syntax = named >> Token.inner_syntax_of;
   160 val name_source_position = named >> Token.source_position_of;
   161 
   162 val name = named >> Token.content_of;
   163 val binding = Parse.position name >> Binding.make;
   164 val alt_name = alt_string >> Token.content_of;
   165 val symbol = symbolic >> Token.content_of;
   166 val liberal_name = symbol || name;
   167 
   168 val var = (ident >> Token.content_of) :|-- (fn x =>
   169   (case Lexicon.read_variable x of SOME v => Scan.succeed v | NONE => Scan.fail));
   170 
   171 
   172 (* values *)
   173 
   174 fun value dest = Scan.some (fn arg =>
   175   (case Token.get_value arg of SOME v => (SOME (dest v) handle Match => NONE) | NONE => NONE));
   176 
   177 fun evaluate mk eval arg =
   178   let val x = eval arg in (Token.assign (SOME (mk x)) arg; x) end;
   179 
   180 val internal_text = value (fn Token.Text s => s);
   181 val internal_typ = value (fn Token.Typ T => T);
   182 val internal_term = value (fn Token.Term t => t);
   183 val internal_fact = value (fn Token.Fact ths => ths);
   184 val internal_attribute = value (fn Token.Attribute att => att);
   185 
   186 fun named_text intern = internal_text || named >> evaluate Token.Text (intern o Token.content_of);
   187 fun named_typ readT = internal_typ || named >> evaluate Token.Typ (readT o Token.inner_syntax_of);
   188 fun named_term read = internal_term || named >> evaluate Token.Term (read o Token.inner_syntax_of);
   189 
   190 fun named_fact get = internal_fact || named >> evaluate Token.Fact (get o Token.content_of) ||
   191   alt_string >> evaluate Token.Fact (get o Token.inner_syntax_of);
   192 
   193 fun named_attribute att =
   194   internal_attribute ||
   195   named >> evaluate Token.Attribute (fn tok => att (Token.content_of tok, Token.pos_of tok));
   196 
   197 
   198 (* terms and types *)
   199 
   200 val typ_abbrev = Scan.peek (named_typ o Proof_Context.read_typ_abbrev o Context.proof_of);
   201 val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
   202 val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
   203 val term_pattern = Scan.peek (named_term o Proof_Context.read_term_pattern o Context.proof_of);
   204 val term_abbrev = Scan.peek (named_term o Proof_Context.read_term_abbrev o Context.proof_of);
   205 val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
   206 
   207 
   208 (* type and constant names *)
   209 
   210 fun type_name flags =
   211   Scan.peek (fn ctxt => named_typ (Proof_Context.read_type_name (Context.proof_of ctxt) flags))
   212   >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
   213 
   214 fun const flags =
   215   Scan.peek (fn ctxt => named_term (Proof_Context.read_const (Context.proof_of ctxt) flags dummyT))
   216   >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
   217 
   218 
   219 (* improper method arguments *)
   220 
   221 val from_to =
   222   Parse.nat -- ($$$ "-" |-- Parse.nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
   223   Parse.nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
   224   Parse.nat >> (fn i => fn tac => tac i) ||
   225   $$$ "!" >> K ALLGOALS;
   226 
   227 val goal = $$$ "[" |-- Parse.!!! (from_to --| $$$ "]");
   228 fun goal_spec x = Scan.lift (Scan.optional goal (fn tac => tac 1)) x;
   229 
   230 
   231 (* arguments within outer syntax *)
   232 
   233 val argument_kinds =
   234  [Token.Ident, Token.LongIdent, Token.SymIdent, Token.Var, Token.TypeIdent, Token.TypeVar,
   235   Token.Nat, Token.Float, Token.String, Token.AltString, Token.Cartouche, Token.Verbatim];
   236 
   237 fun parse_args is_symid =
   238   let
   239     fun argument blk =
   240       Parse.group (fn () => "argument")
   241         (Scan.one (fn tok =>
   242           let val kind = Token.kind_of tok in
   243             member (op =) argument_kinds kind orelse
   244             Token.keyword_with is_symid tok orelse
   245             (blk andalso Token.keyword_with (fn s => s = ",") tok)
   246           end));
   247 
   248     fun args blk x = Scan.optional (args1 blk) [] x
   249     and args1 blk x =
   250       ((Scan.repeat1
   251         (Scan.repeat1 (argument blk) ||
   252           argsp "(" ")" ||
   253           argsp "[" "]")) >> flat) x
   254     and argsp l r x =
   255       (token (Parse.$$$ l) ::: Parse.!!! (args true @@@ (token (Parse.$$$ r) >> single))) x;
   256   in (args, args1) end;
   257 
   258 val parse = #1 (parse_args Token.ident_or_symbolic) false;
   259 fun parse1 is_symid = #2 (parse_args is_symid) false;
   260 
   261 
   262 (* attributes *)
   263 
   264 fun attribs intern =
   265   let
   266     val attrib_name = internal_text || (symbolic || named)
   267       >> evaluate Token.Text (intern o Token.content_of);
   268     val attrib = Parse.position (attrib_name -- Parse.!!! parse) >> src;
   269   in $$$ "[" |-- Parse.!!! (Parse.list attrib --| $$$ "]") end;
   270 
   271 fun opt_attribs intern = Scan.optional (attribs intern) [];
   272 
   273 
   274 (* theorem specifications *)
   275 
   276 fun thm_name intern s = binding -- opt_attribs intern --| $$$ s;
   277 
   278 fun opt_thm_name intern s =
   279   Scan.optional
   280     ((binding -- opt_attribs intern || attribs intern >> pair Binding.empty) --| $$$ s)
   281     (Binding.empty, []);
   282 
   283 
   284 
   285 (** syntax wrapper **)
   286 
   287 fun syntax kind scan (Src ((s, args0), pos)) context =
   288   let
   289     val args1 = map Token.init_assignable args0;
   290     fun reported_text () =
   291       if Context_Position.is_visible_generic context then
   292         maps (Token.reports_of_value o Token.closure) args1
   293         |> map (fn (p, m) => Position.reported_text p m "")
   294         |> implode
   295       else "";
   296   in
   297     (case Scan.error (Scan.finite' Token.stopper (Scan.option scan)) (context, args1) of
   298       (SOME x, (context', [])) => (Output.report (reported_text ()); (x, context'))
   299     | (_, (_, args2)) =>
   300         error ("Bad arguments for " ^ kind ^ " " ^ quote s ^ Position.here pos ^
   301           (if null args2 then "" else ":\n  " ^ space_implode " " (map Token.print args2)) ^
   302           Markup.markup Markup.report (reported_text ())))
   303   end;
   304 
   305 fun context_syntax kind scan src =
   306   apsnd Context.the_proof o syntax kind scan src o Context.Proof;
   307 
   308 end;