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