src/Pure/Isar/args.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22932 53005f898665
child 24002 9fe28da848b0
permissions -rw-r--r--
tuned Pure/General/name_space.ML
     1 (*  Title:      Pure/Isar/args.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Concrete argument syntax of attributes, methods etc. -- with special
     6 support for embedded values and static binding.
     7 *)
     8 
     9 signature ARGS =
    10 sig
    11   datatype value =
    12     Text of string | Typ of typ | Term of term | Fact of thm list |
    13     Attribute of morphism -> attribute
    14   type T
    15   val string_of: T -> string
    16   val mk_ident: string * Position.T -> T
    17   val mk_string: string * Position.T -> T
    18   val mk_alt_string: string * Position.T -> T
    19   val mk_keyword: string * Position.T -> T
    20   val mk_text: string -> T
    21   val mk_typ: typ -> T
    22   val mk_term: term -> T
    23   val mk_fact: thm list -> T
    24   val mk_attribute: (morphism -> attribute) -> T
    25   val stopper: T * (T -> bool)
    26   val not_eof: T -> bool
    27   type src
    28   val src: (string * T list) * Position.T -> src
    29   val dest_src: src -> (string * T list) * Position.T
    30   val pretty_src: Proof.context -> src -> Pretty.T
    31   val map_name: (string -> string) -> src -> src
    32   val morph_values: morphism -> src -> src
    33   val maxidx_values: src -> int -> int
    34   val assignable: src -> src
    35   val assign: value option -> T -> unit
    36   val closure: src -> src
    37   val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
    38   val !!! : (T list -> 'a) -> T list -> 'a
    39   val $$$ : string -> T list -> string * T list
    40   val add: T list -> string * T list
    41   val del: T list -> string * T list
    42   val colon: T list -> string * T list
    43   val query: T list -> string * T list
    44   val bang: T list -> string * T list
    45   val query_colon: T list -> string * T list
    46   val bang_colon: T list -> string * T list
    47   val parens: (T list -> 'a * T list) -> T list -> 'a * T list
    48   val bracks: (T list -> 'a * T list) -> T list -> 'a * T list
    49   val mode: string -> 'a * T list -> bool * ('a * T list)
    50   val maybe: (T list -> 'a * T list) -> T list -> 'a option * T list
    51   val name: T list -> string * T list
    52   val alt_name: T list -> string * T list
    53   val symbol: T list -> string * T list
    54   val liberal_name: T list -> string * T list
    55   val nat: T list -> int * T list
    56   val int: T list -> int * T list
    57   val var: T list -> indexname * T list
    58   val list: (T list -> 'a * T list) -> T list -> 'a list * T list
    59   val list1: (T list -> 'a * T list) -> T list -> 'a list * T list
    60   val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    61   val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    62   val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    63   val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    64   val ahead: T list -> T * T list
    65   val internal_text: T list -> string * T list
    66   val internal_typ: T list -> typ * T list
    67   val internal_term: T list -> term * T list
    68   val internal_fact: T list -> thm list * T list
    69   val internal_attribute: T list -> (morphism -> attribute) * T list
    70   val named_text: (string -> string) -> T list -> string * T list
    71   val named_typ: (string -> typ) -> T list -> typ * T list
    72   val named_term: (string -> term) -> T list -> term * T list
    73   val named_fact: (string -> thm list) -> T list -> thm list * T list
    74   val typ_abbrev: Context.generic * T list -> typ * (Context.generic * T list)
    75   val typ: Context.generic * T list -> typ * (Context.generic * T list)
    76   val term: Context.generic * T list -> term * (Context.generic * T list)
    77   val term_abbrev: Context.generic * T list -> term * (Context.generic * T list)
    78   val prop: Context.generic * T list -> term * (Context.generic * T list)
    79   val tyname: Context.generic * T list -> string * (Context.generic * T list)
    80   val const: Context.generic * T list -> string * (Context.generic * T list)
    81   val thm_sel: T list -> PureThy.interval list * T list
    82   val bang_facts: Context.generic * T list -> thm list * (Context.generic * T list)
    83   val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
    84     -> ((int -> tactic) -> tactic) * ('a * T list)
    85   val attribs: (string -> string) -> T list -> src list * T list
    86   val opt_attribs: (string -> string) -> T list -> src list * T list
    87   val syntax: string -> ('b * T list -> 'a * ('b * T list)) -> src -> 'b -> 'a * 'b
    88   val context_syntax: string -> (Context.generic * T list -> 'a * (Context.generic * T list)) ->
    89     src -> Proof.context -> 'a * Proof.context
    90 end;
    91 
    92 structure Args: ARGS =
    93 struct
    94 
    95 
    96 (** datatype T **)
    97 
    98 (*An argument token is a symbol (with raw string value), together with
    99   an optional assigned internal value.  Note that an assignable ref
   100   designates an intermediate state of internalization -- it is NOT
   101   meant to persist.*)
   102 
   103 datatype kind = Ident | String | AltString | Keyword | EOF;
   104 
   105 type symbol = kind * string * Position.T;
   106 
   107 datatype value =
   108   Text of string |
   109   Typ of typ |
   110   Term of term |
   111   Fact of thm list |
   112   Attribute of morphism -> attribute;
   113 
   114 datatype slot =
   115   Empty |
   116   Value of value option |
   117   Assignable of value option ref;
   118 
   119 datatype T = Arg of symbol * slot;
   120 
   121 fun string_of (Arg ((Ident, x, _), _)) = x
   122   | string_of (Arg ((String, x, _), _)) = quote x
   123   | string_of (Arg ((AltString, x, _), _)) = enclose "`" "`" x
   124   | string_of (Arg ((Keyword, x, _), _)) = x
   125   | string_of (Arg ((EOF, _, _), _)) = "";
   126 
   127 fun sym_of (Arg ((_, x, _), _)) = x;
   128 fun pos_of (Arg ((_, _, pos), _)) = pos;
   129 
   130 
   131 (* basic constructors *)
   132 
   133 fun mk_symbol k (x, p) = Arg ((k, x, p), Empty);
   134 fun mk_value k v = Arg ((Keyword, k, Position.none), Value (SOME v));
   135 
   136 val mk_ident = mk_symbol Ident;
   137 val mk_string = mk_symbol String;
   138 val mk_alt_string = mk_symbol AltString;
   139 val mk_keyword = mk_symbol Keyword;
   140 val mk_text = mk_value "<text>" o Text;
   141 val mk_typ = mk_value "<typ>" o Typ;
   142 val mk_term = mk_value "<term>" o Term;
   143 val mk_fact = mk_value "<fact>" o Fact;
   144 val mk_attribute = mk_value "<attribute>" o Attribute;
   145 
   146 
   147 (* eof *)
   148 
   149 val eof = mk_symbol EOF ("", Position.none);
   150 
   151 fun is_eof (Arg ((EOF, _, _), _)) = true
   152   | is_eof _ = false;
   153 
   154 val stopper = (eof, is_eof);
   155 val not_eof = not o is_eof;
   156 
   157 
   158 
   159 (** datatype src **)
   160 
   161 datatype src = Src of (string * T list) * Position.T;
   162 
   163 val src = Src;
   164 fun dest_src (Src src) = src;
   165 
   166 fun pretty_src ctxt src =
   167   let
   168     val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
   169     fun prt (Arg (_, Value (SOME (Text s)))) = Pretty.str (quote s)
   170       | prt (Arg (_, Value (SOME (Typ T)))) = ProofContext.pretty_typ ctxt T
   171       | prt (Arg (_, Value (SOME (Term t)))) = ProofContext.pretty_term ctxt t
   172       | prt (Arg (_, Value (SOME (Fact ths)))) =
   173           Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
   174       | prt arg = Pretty.str (string_of arg);
   175     val (s, args) = #1 (dest_src src);
   176   in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
   177 
   178 fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
   179 fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
   180 
   181 
   182 (* values *)
   183 
   184 fun map_value f (Arg (s, Value (SOME v))) = Arg (s, Value (SOME (f v)))
   185   | map_value _ arg = arg;
   186 
   187 fun morph_values phi = map_args (map_value
   188   (fn Text s => Text s
   189     | Typ T => Typ (Morphism.typ phi T)
   190     | Term t => Term (Morphism.term phi t)
   191     | Fact ths => Fact (Morphism.fact phi ths)
   192     | Attribute att => Attribute (Morphism.transform phi att)));
   193 
   194 fun maxidx_values (Src ((_, args), _)) = args |> fold
   195   (fn (Arg (_, Value (SOME (Typ T)))) => Term.maxidx_typ T
   196     | (Arg (_, Value (SOME (Term t)))) => Term.maxidx_term t
   197     | (Arg (_, Value (SOME (Fact ths)))) => fold Thm.maxidx_thm ths
   198     | _ => I);
   199 
   200 
   201 (* static binding *)
   202 
   203 (*1st stage: make empty slots assignable*)
   204 val assignable =
   205   map_args (fn Arg (s, Empty) => Arg (s, Assignable (ref NONE)) | arg => arg);
   206 
   207 (*2nd stage: assign values as side-effect of scanning*)
   208 fun assign v (arg as Arg (_, Assignable r)) = r := v
   209   | assign _ _ = ();
   210 
   211 val ahead = Scan.ahead (Scan.one not_eof);
   212 fun touch scan = ahead -- scan >> (fn (arg, y) => (assign NONE arg; y));
   213 
   214 (*3rd stage: static closure of final values*)
   215 val closure =
   216   map_args (fn Arg (s, Assignable (ref v)) => Arg (s, Value v) | arg => arg);
   217 
   218 
   219 
   220 (** scanners **)
   221 
   222 (* position *)
   223 
   224 fun position scan =
   225   (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
   226 
   227 
   228 (* cut *)
   229 
   230 fun !!! scan =
   231   let
   232     fun get_pos [] = " (past end-of-text!)"
   233       | get_pos (arg :: _) = Position.str_of (pos_of arg);
   234 
   235     fun err (args, NONE) = "Argument syntax error" ^ get_pos args
   236       | err (args, SOME msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
   237   in Scan.!! err scan end;
   238 
   239 
   240 (* basic *)
   241 
   242 fun some_ident f =
   243   touch (Scan.some (fn Arg ((Ident, x, _), _) => f x | _ => NONE));
   244 
   245 val named =
   246   touch (Scan.one (fn Arg ((k, _, _), _) => k = Ident orelse k = String));
   247 
   248 val alt_string =
   249   touch (Scan.one (fn Arg ((k, _, _), _) => k = AltString));
   250 
   251 val symbolic =
   252   touch (Scan.one (fn Arg ((k, x, _), _) => k = Keyword andalso OuterLex.is_sid x));
   253 
   254 fun &&& x =
   255   touch (Scan.one (fn Arg ((k, y, _), _) => k = Keyword andalso x = y));
   256 
   257 fun $$$ x =
   258   touch (Scan.one (fn Arg ((k, y, _), _) => (k = Ident orelse k = Keyword) andalso x = y))
   259   >> sym_of;
   260 
   261 val add = $$$ "add";
   262 val del = $$$ "del";
   263 val colon = $$$ ":";
   264 val query = $$$ "?";
   265 val bang = $$$ "!";
   266 val query_colon = $$$ "?" ^^ $$$ ":";
   267 val bang_colon = $$$ "!" ^^ $$$ ":";
   268 
   269 fun parens scan = $$$ "(" |-- scan --| $$$ ")";
   270 fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
   271 fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
   272 fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
   273 
   274 val name = named >> sym_of;
   275 val alt_name = alt_string >> sym_of;
   276 val symbol = symbolic >> sym_of;
   277 val liberal_name = symbol || name;
   278 
   279 val nat = some_ident Syntax.read_nat;
   280 val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *;
   281 val var = some_ident Syntax.read_variable;
   282 
   283 
   284 (* enumerations *)
   285 
   286 fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::;
   287 fun list scan = list1 scan || Scan.succeed [];
   288 
   289 fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::;
   290 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   291 
   292 fun and_list1 scan = enum1 "and" scan;
   293 fun and_list scan = enum "and" scan;
   294 
   295 
   296 (* values *)
   297 
   298 fun value dest = Scan.some  (*no touch here*)
   299   (fn Arg (_, Value (SOME v)) => (SOME (dest v) handle Match => NONE)
   300     | Arg _ => NONE);
   301 
   302 fun evaluate mk eval arg =
   303   let val x = eval (sym_of arg) in (assign (SOME (mk x)) arg; x) end;
   304 
   305 val internal_text = value (fn Text s => s);
   306 val internal_typ = value (fn Typ T => T);
   307 val internal_term = value (fn Term t => t);
   308 val internal_fact = value (fn Fact ths => ths);
   309 val internal_attribute = value (fn Attribute att => att);
   310 
   311 fun named_text intern = internal_text || named >> evaluate Text intern;
   312 fun named_typ readT = internal_typ || named >> evaluate Typ readT;
   313 fun named_term read = internal_term || named >> evaluate Term read;
   314 fun named_fact get = internal_fact || (alt_string || named) >> evaluate Fact get;
   315 
   316 
   317 (* terms and types *)
   318 
   319 val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
   320 val typ = Scan.peek (named_typ o ProofContext.read_typ o Context.proof_of);
   321 val term = Scan.peek (named_term o ProofContext.read_term o Context.proof_of);
   322 val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
   323 val prop = Scan.peek (named_term o ProofContext.read_prop o Context.proof_of);
   324 
   325 
   326 (* type and constant names *)
   327 
   328 val tyname = Scan.peek (named_typ o Context.cases Sign.read_tyname ProofContext.read_tyname)
   329   >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
   330 
   331 val const = Scan.peek (named_term o Context.cases Sign.read_const ProofContext.read_const)
   332   >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
   333 
   334 
   335 (* misc *)
   336 
   337 val thm_sel = parens (list1
   338  (nat --| $$$ "-" -- nat >> PureThy.FromTo ||
   339   nat --| $$$ "-" >> PureThy.From ||
   340   nat >> PureThy.Single));
   341 
   342 val bang_facts = Scan.peek (fn context =>
   343   $$$ "!" >> (fn _ => (warning "use of prems in proof method";
   344     Assumption.prems_of (Context.proof_of context))) || Scan.succeed []);
   345 
   346 
   347 (* goal specification *)
   348 
   349 (* range *)
   350 
   351 val from_to =
   352   nat -- ($$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
   353   nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
   354   nat >> (fn i => fn tac => tac i) ||
   355   $$$ "!" >> K ALLGOALS;
   356 
   357 val goal = $$$ "[" |-- !!! (from_to --| $$$ "]");
   358 fun goal_spec def = Scan.lift (Scan.optional goal def);
   359 
   360 
   361 
   362 (* attribs *)
   363 
   364 local
   365 
   366 val exclude = member (op =) (explode "()[],");
   367 
   368 fun atomic blk = touch (Scan.one (fn Arg ((k, x, _), _) =>
   369     k <> Keyword orelse not (exclude x) orelse blk andalso x = ","));
   370 
   371 fun args blk x = Scan.optional (args1 blk) [] x
   372 and args1 blk x =
   373   ((Scan.repeat1
   374     (Scan.repeat1 (atomic blk) ||
   375       argsp "(" ")" ||
   376       argsp "[" "]")) >> flat) x
   377 and argsp l r x =
   378   (Scan.trace (&&& l -- !!! (args true -- &&& r)) >> #2) x;
   379 
   380 in
   381 
   382 fun attribs intern =
   383   let
   384     val attrib_name = internal_text || (symbolic || named) >> evaluate Text intern;
   385     val attrib = position (attrib_name -- !!! (args false)) >> src;
   386   in $$$ "[" |-- !!! (list attrib --| $$$ "]") end;
   387 
   388 fun opt_attribs intern = Scan.optional (attribs intern) [];
   389 
   390 end;
   391 
   392 
   393 (* syntax interface *)
   394 
   395 fun syntax kind scan (src as Src ((s, args), pos)) st =
   396   (case Scan.error (Scan.finite' stopper (Scan.option scan)) (st, args) of
   397     (SOME x, (st', [])) => (x, st')
   398   | (_, (_, args')) =>
   399       error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
   400         space_implode " " (map string_of args')));
   401 
   402 fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
   403 
   404 end;