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