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