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