src/Pure/Isar/args.ML
author wenzelm
Tue Mar 21 17:43:54 2000 +0100 (2000-03-21 ago)
changeset 8549 851d39c10d9f
parent 8536 de307f5bc89a
child 8687 24bff69370f0
permissions -rw-r--r--
goal_spec: [!];
     1 (*  Title:      Pure/Isar/args.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Concrete argument syntax (of attributes and methods).
     6 *)
     7 
     8 signature ARGS =
     9 sig
    10   type T
    11   val val_of: T -> string
    12   val pos_of: T -> Position.T
    13   val str_of: T -> string
    14   val ident: string * Position.T -> T
    15   val string: string * Position.T -> T
    16   val keyword: string * Position.T -> T
    17   val stopper: T * (T -> bool)
    18   val not_eof: T -> bool
    19   val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
    20   val !!! : (T list -> 'a) -> T list -> 'a
    21   val $$$ : string -> T list -> string * T list
    22   val name: T list -> string * T list
    23   val nat: T list -> int * T list
    24   val var: T list -> indexname * T list
    25   val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    26   val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    27   val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    28   val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
    29   val global_typ: theory * T list -> typ * (theory * T list)
    30   val global_term: theory * T list -> term * (theory * T list)
    31   val global_prop: theory * T list -> term * (theory * T list)
    32   val local_typ: Proof.context * T list -> typ * (Proof.context * T list)
    33   val local_term: Proof.context * T list -> term * (Proof.context * T list)
    34   val local_prop: Proof.context * T list -> term * (Proof.context * T list)
    35   val bang_facts: Proof.context * T list -> thm list * (Proof.context * T list)
    36   val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
    37     -> ((int -> tactic) -> tactic) * ('a * T list)
    38   type src
    39   val src: (string * T list) * Position.T -> src
    40   val dest_src: src -> (string * T list) * Position.T
    41   val attribs: T list -> src list * T list
    42   val opt_attribs: T list -> src list * T list
    43   val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> src -> 'a -> 'a * 'b
    44 end;
    45 
    46 structure Args: ARGS =
    47 struct
    48 
    49 
    50 (** datatype T **)
    51 
    52 datatype kind = Ident | String | Keyword | EOF;
    53 datatype T = Arg of kind * (string * Position.T);
    54 
    55 fun val_of (Arg (_, (x, _))) = x;
    56 fun pos_of (Arg (_, (_, pos))) = pos;
    57 
    58 fun str_of (Arg (Ident, (x, _))) = enclose "'" "'" x
    59   | str_of (Arg (String, (x, _))) = quote x
    60   | str_of (Arg (Keyword, (x, _))) = x
    61   | str_of (Arg (EOF, _)) = "end-of-text";
    62 
    63 fun arg kind x_pos = Arg (kind, x_pos);
    64 val ident = arg Ident;
    65 val string = arg String;
    66 val keyword = arg Keyword;
    67 
    68 
    69 (* eof *)
    70 
    71 val eof = arg EOF ("", Position.none);
    72 
    73 fun is_eof (Arg (EOF, _)) = true
    74   | is_eof _ = false;
    75 
    76 val stopper = (eof, is_eof);
    77 val not_eof = not o is_eof;
    78 
    79 
    80 
    81 (** scanners **)
    82 
    83 (* position *)
    84 
    85 fun position scan = (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
    86 
    87 
    88 (* cut *)
    89 
    90 fun !!! scan =
    91   let
    92     fun get_pos [] = " (past end-of-text!)"
    93       | get_pos (Arg (_, (_, pos)) :: _) = Position.str_of pos;
    94 
    95     fun err (args, None) = "Argument syntax error" ^ get_pos args
    96       | err (args, Some msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
    97   in Scan.!! err scan end;
    98 
    99 
   100 (* basic *)
   101 
   102 fun $$$$ x = Scan.one (fn Arg (k, (y, _)) => (k = Ident orelse k = Keyword) andalso x = y);
   103 fun $$$ x = $$$$ x >> val_of;
   104 
   105 val name = Scan.one (fn Arg (k, (x, _)) => k = Ident orelse k = String) >> val_of;
   106 
   107 val keyword_symid =
   108   Scan.one (fn Arg (k, (x, _)) => k = Keyword andalso OuterLex.is_sid x) >> val_of;
   109 
   110 fun kind f = Scan.one (K true) :--
   111   (fn Arg (Ident, (x, _)) =>
   112     (case f x of Some y => Scan.succeed y | _ => Scan.fail)
   113   | _ => Scan.fail) >> #2;
   114 
   115 val nat = kind Syntax.read_nat;
   116 val var = kind (apsome #1 o try Term.dest_Var o Syntax.read_var);
   117 
   118 
   119 (* enumerations *)
   120 
   121 fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::;
   122 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   123 
   124 fun and_list1 scan = enum1 "and" scan;
   125 fun and_list scan = enum "and" scan;
   126 
   127 
   128 (* terms and types *)
   129 
   130 fun gen_item read = Scan.depend (fn st => name >> (pair st o read st));
   131 
   132 val global_typ = gen_item (ProofContext.read_typ o ProofContext.init);
   133 val global_term = gen_item (ProofContext.read_term o ProofContext.init);
   134 val global_prop = gen_item (ProofContext.read_prop o ProofContext.init);
   135 
   136 val local_typ = gen_item ProofContext.read_typ;
   137 val local_term = gen_item ProofContext.read_term;
   138 val local_prop = gen_item ProofContext.read_prop;
   139 
   140 
   141 (* bang facts *)
   142 
   143 val bang_facts = Scan.depend (fn ctxt =>
   144   ($$$ "!" >> K (ProofContext.prems_of ctxt) || Scan.succeed []) >> pair ctxt);
   145 
   146 
   147 (* goal specification *)
   148 
   149 (* range *)
   150 
   151 val from_to =
   152   nat -- ($$$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
   153   nat --| $$$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
   154   nat >> (fn i => fn tac => tac i) ||
   155   $$$$ "!" >> K ALLGOALS;
   156 
   157 val goal = $$$$ "[" |-- !!! (from_to --| $$$$ "]");
   158 fun goal_spec def = Scan.lift (Scan.optional goal def);
   159 
   160 
   161 (* args *)
   162 
   163 val exclude = explode "(){}[],";
   164 
   165 fun atom_arg blk = Scan.one (fn Arg (k, (x, _)) =>
   166   k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ",");
   167 
   168 fun paren_args l r scan = $$$$ l -- !!! (scan true -- $$$$ r)
   169   >> (fn (x, (ys, z)) => x :: ys @ [z]);
   170 
   171 fun args blk x = Scan.optional (args1 blk) [] x
   172 and args1 blk x =
   173   ((Scan.repeat1
   174     (Scan.repeat1 (atom_arg blk) ||
   175       paren_args "(" ")" args ||
   176       paren_args "{" "}" args ||
   177       paren_args "[" "]" args)) >> flat) x;
   178 
   179 
   180 
   181 (** type src **)
   182 
   183 datatype src = Src of (string * T list) * Position.T;
   184 
   185 val src = Src;
   186 fun dest_src (Src src) = src;
   187 
   188 fun err_in_src kind msg (Src ((s, args), pos)) =
   189   error (kind ^ " " ^ s ^ Position.str_of pos ^ ": " ^ msg ^ "\n  " ^
   190     space_implode " " (map str_of args));
   191 
   192 
   193 (* argument syntax *)
   194 
   195 fun syntax kind scan (src as Src ((s, args), pos)) st =
   196   (case handle_error (Scan.error (Scan.finite' stopper (Scan.option scan))) (st, args) of
   197     OK (Some x, (st', [])) => (st', x)
   198   | OK (_, (_, args')) => err_in_src kind "bad arguments" (Src ((s, args'), pos))
   199   | Error msg => err_in_src kind ("\n" ^ msg) src);
   200 
   201 
   202 (* attribs *)
   203 
   204 fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::;
   205 fun list scan = list1 scan || Scan.succeed [];
   206 
   207 val attrib = position ((keyword_symid || name) -- !!! (args false)) >> src;
   208 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   209 val opt_attribs = Scan.optional attribs [];
   210 
   211 
   212 end;