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