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