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