src/Pure/Isar/outer_parse.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 20983 d4500b9b220e
child 21314 6d709b9bea1a
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Generic parsers for Isabelle/Isar outer syntax.
     6 *)
     7 
     8 signature OUTER_PARSE =
     9 sig
    10   type token
    11   val group: string -> (token list -> 'a) -> token list -> 'a
    12   val !!! : (token list -> 'a) -> token list -> 'a
    13   val !!!! : (token list -> 'a) -> token list -> 'a
    14   val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    15   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    16   val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    17   val not_eof: token list -> token * token list
    18   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    19   val command: token list -> string * token list
    20   val keyword: token list -> string * token list
    21   val short_ident: token list -> string * token list
    22   val long_ident: token list -> string * token list
    23   val sym_ident: token list -> string * token list
    24   val minus: token list -> string * token list
    25   val term_var: token list -> string * token list
    26   val type_ident: token list -> string * token list
    27   val type_var: token list -> string * token list
    28   val number: token list -> string * token list
    29   val string: token list -> string * token list
    30   val verbatim: token list -> string * token list
    31   val sync: token list -> string * token list
    32   val eof: token list -> string * token list
    33   val $$$ : string -> token list -> string * token list
    34   val reserved : string -> token list -> string * token list
    35   val semicolon: token list -> string * token list
    36   val underscore: token list -> string * token list
    37   val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list
    38   val tag_name: token list -> string * token list
    39   val tags: token list -> string list * token list
    40   val opt_unit: token list -> unit * token list
    41   val opt_keyword: string -> token list -> bool * token list
    42   val begin: token list -> string * token list
    43   val opt_begin: token list -> bool * token list
    44   val nat: token list -> int * token list
    45   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    46   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    47   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    48   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    49   val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    50   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    51   val name: token list -> bstring * token list
    52   val xname: token list -> xstring * token list
    53   val text: token list -> string * token list
    54   val path: token list -> Path.T * token list
    55   val parname: token list -> string * token list
    56   val sort: token list -> string * token list
    57   val arity: token list -> (string list * string) * token list
    58   val type_args: token list -> string list * token list
    59   val typ: token list -> string * token list
    60   val mixfix: token list -> mixfix * token list
    61   val opt_infix: token list -> mixfix * token list
    62   val opt_mixfix: token list -> mixfix * token list
    63   val opt_infix': token list -> mixfix * token list
    64   val opt_mixfix': token list -> mixfix * token list
    65   val const: token list -> (string * string * mixfix) * token list
    66   val simple_fixes: token list -> (string * string option) list * token list
    67   val fixes: token list -> (string * string option * mixfix) list * token list
    68   val for_fixes: token list -> (string * string option * mixfix) list * token list
    69   val term: token list -> string * token list
    70   val prop: token list -> string * token list
    71   val propp: token list -> (string * string list) * token list
    72   val termp: token list -> (string * string list) * token list
    73   val arguments: token list -> Args.T list * token list
    74   val attrib: token list -> Attrib.src * token list
    75   val attribs: token list -> Attrib.src list * token list
    76   val opt_attribs: token list -> Attrib.src list * token list
    77   val thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    78   val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    79   val spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    80   val named_spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    81   val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
    82   val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list
    83   val xthm: token list -> (thmref * Attrib.src list) * token list
    84   val xthms1: token list -> (thmref * Attrib.src list) list * token list
    85   val name_facts: token list ->
    86     ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list * token list
    87   val locale_mixfix: token list -> mixfix * token list
    88   val locale_fixes: token list -> (string * string option * mixfix) list * token list
    89   val locale_insts: token list -> string option list * token list
    90   val locale_target: token list -> xstring * token list
    91   val opt_locale_target: token list -> xstring option * token list
    92   val locale_expr: token list -> Locale.expr * token list
    93   val locale_expr_unless: (token list -> 'a * token list) -> 
    94          token list -> Locale.expr * token list 
    95   val locale_keyword: token list -> string * token list
    96   val locale_element: token list -> Element.context Locale.element * token list
    97   val context_element: token list -> Element.context * token list
    98   val statement: token list ->
    99     ((bstring * Attrib.src list) * (string * string list) list) list * token list
   100   val general_statement: token list ->
   101     (Element.context Locale.element list * Element.statement) * OuterLex.token list
   102   val statement_keyword: token list -> string * token list
   103   val method: token list -> Method.text * token list
   104 end;
   105 
   106 structure OuterParse: OUTER_PARSE =
   107 struct
   108 
   109 structure T = OuterLex;
   110 type token = T.token;
   111 
   112 
   113 (** error handling **)
   114 
   115 (* group atomic parsers (no cuts!) *)
   116 
   117 fun fail_with s = Scan.fail_with
   118   (fn [] => s ^ " expected (past end-of-file!)"
   119     | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
   120 
   121 fun group s scan = scan || fail_with s;
   122 
   123 
   124 (* cut *)
   125 
   126 fun cut kind scan =
   127   let
   128     fun get_pos [] = " (past end-of-file!)"
   129       | get_pos (tok :: _) = T.pos_of tok;
   130 
   131     fun err (toks, NONE) = kind ^ get_pos toks
   132       | err (toks, SOME msg) = kind ^ get_pos toks ^ ": " ^ msg;
   133   in Scan.!! err scan end;
   134 
   135 fun !!! scan = cut "Outer syntax error" scan;
   136 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   137 
   138 
   139 
   140 (** basic parsers **)
   141 
   142 (* utils *)
   143 
   144 fun triple1 ((x, y), z) = (x, y, z);
   145 fun triple2 (x, (y, z)) = (x, y, z);
   146 fun triple_swap ((x, y), z) = ((x, z), y);
   147 
   148 
   149 (* tokens *)
   150 
   151 val not_eof = Scan.one T.not_eof;
   152 
   153 fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   154 
   155 fun kind k =
   156   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   157 
   158 val command = kind T.Command;
   159 val keyword = kind T.Keyword;
   160 val short_ident = kind T.Ident;
   161 val long_ident = kind T.LongIdent;
   162 val sym_ident = kind T.SymIdent;
   163 val term_var = kind T.Var;
   164 val type_ident = kind T.TypeIdent;
   165 val type_var = kind T.TypeVar;
   166 val number = kind T.Nat;
   167 val string = kind T.String;
   168 val alt_string = kind T.AltString;
   169 val verbatim = kind T.Verbatim;
   170 val sync = kind T.Sync;
   171 val eof = kind T.EOF;
   172 
   173 fun $$$ x =
   174   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   175     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
   176 
   177 fun reserved x =
   178   group ("Reserved identifier " ^ quote x)
   179     (Scan.one (T.ident_with (equal x)) >> T.val_of);
   180 
   181 val semicolon = $$$ ";";
   182 
   183 val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
   184 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   185 fun maybe scan = underscore >> K NONE || scan >> SOME;
   186 
   187 val nat = number >> (#1 o Library.read_int o Symbol.explode);
   188 
   189 val tag_name = group "tag name" (short_ident || string);
   190 val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
   191 
   192 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   193 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   194 
   195 val begin = $$$ "begin";
   196 val opt_begin = Scan.optional (begin >> K true) false;
   197 
   198 
   199 (* enumerations *)
   200 
   201 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   202 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   203 
   204 fun list1 scan = enum1 "," scan;
   205 fun list scan = enum "," scan;
   206 
   207 fun and_list1 scan = enum1 "and" scan;
   208 fun and_list scan = enum "and" scan;
   209 
   210 
   211 (* names and text *)
   212 
   213 val name = group "name declaration" (short_ident || sym_ident || string || number);
   214 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   215 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   216 val path = group "file name/path specification" name >> Path.unpack;
   217 
   218 val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
   219 
   220 
   221 (* sorts and arities *)
   222 
   223 val sort = group "sort" xname;
   224 
   225 fun gen_arity cod =
   226   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   227 
   228 val arity = gen_arity sort;
   229 
   230 
   231 (* types *)
   232 
   233 val typ = group "type"
   234   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   235 
   236 val type_args =
   237   type_ident >> single ||
   238   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   239   Scan.succeed [];
   240 
   241 
   242 (* mixfix annotations *)
   243 
   244 val mfix = string --
   245   !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
   246     Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
   247 
   248 val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName);
   249 val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName);
   250 val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName);
   251 
   252 val binder = $$$ "binder" |--
   253   !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   254   >> (Binder o triple2);
   255 
   256 fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   257 fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
   258 
   259 val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
   260 val opt_infix = opt_annotation !!! (infxl || infxr || infx);
   261 val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
   262 val opt_infix' = opt_annotation I (infxl || infxr || infx);
   263 val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
   264 
   265 
   266 (* fixes *)
   267 
   268 val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   269 
   270 val params = Scan.repeat1 name -- Scan.option ($$$ "::" |-- !!! typ)
   271   >> (fn (xs, T) => map (rpair T) xs);
   272 
   273 val simple_fixes = and_list1 params >> flat;
   274 
   275 val fixes =
   276   and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
   277     params >> map Syntax.no_syn) >> flat;
   278 
   279 val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
   280 
   281 
   282 (* terms *)
   283 
   284 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   285 
   286 val term = group "term" trm;
   287 val prop = group "proposition" trm;
   288 
   289 
   290 (* patterns *)
   291 
   292 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   293 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   294 
   295 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   296 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   297 
   298 
   299 (* arguments *)
   300 
   301 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   302 val keyword_sid = keyword_symid T.is_sid;
   303 
   304 fun atom_arg is_symid blk =
   305   group "argument"
   306     (position (short_ident || long_ident || sym_ident || term_var ||
   307         type_ident || type_var || number) >> Args.mk_ident ||
   308       position (keyword_symid is_symid) >> Args.mk_keyword ||
   309       position (string || verbatim) >> Args.mk_string ||
   310       position alt_string >> Args.mk_alt_string ||
   311       position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword);
   312 
   313 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   314   >> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]);
   315 
   316 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   317 and args1 is_symid blk x =
   318   ((Scan.repeat1
   319     (Scan.repeat1 (atom_arg is_symid blk) ||
   320       paren_args "(" ")" (args is_symid) ||
   321       paren_args "[" "]" (args is_symid))) >> flat) x;
   322 
   323 val arguments = args T.is_sid false;
   324 
   325 
   326 (* theorem specifications *)
   327 
   328 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   329 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   330 val opt_attribs = Scan.optional attribs [];
   331 
   332 fun thm_name s = name -- opt_attribs --| $$$ s;
   333 fun opt_thm_name s =
   334   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);
   335 
   336 val spec = opt_thm_name ":" -- Scan.repeat1 prop;
   337 val named_spec = thm_name ":" -- Scan.repeat1 prop;
   338 
   339 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   340 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   341 
   342 val thm_sel = $$$ "(" |-- list1
   343  (nat --| minus -- nat >> PureThy.FromTo ||
   344   nat --| minus >> PureThy.From ||
   345   nat >> PureThy.Single) --| $$$ ")";
   346 
   347 val xthm = (alt_string >> Fact || xname -- thm_sel >> NameSelection || xname >> Name)
   348   -- opt_attribs;
   349 val xthms1 = Scan.repeat1 xthm;
   350 
   351 val name_facts = and_list1 (opt_thm_name "=" -- xthms1);
   352 
   353 
   354 (* locale and context elements *)
   355 
   356 val locale_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K Structure || mixfix;
   357 
   358 val locale_fixes =
   359   and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- locale_mixfix >> (single o triple1) ||
   360     params >> map Syntax.no_syn) >> flat;
   361 
   362 val locale_insts =
   363   Scan.optional ($$$ "[" |-- !!! (Scan.repeat1 (maybe term) --| $$$ "]")) [];
   364 
   365 local
   366 
   367 val loc_keyword = $$$ "fixes" || $$$ "constrains" || $$$ "assumes" ||
   368    $$$ "defines" || $$$ "notes" || $$$ "includes";
   369 
   370 val loc_element =
   371   $$$ "fixes" |-- !!! locale_fixes >> Element.Fixes ||
   372   $$$ "constrains" |-- !!! (and_list1 (name -- ($$$ "::" |-- typ)))
   373     >> Element.Constrains ||
   374   $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
   375     >> Element.Assumes ||
   376   $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp))
   377     >> Element.Defines ||
   378   $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
   379     >> Element.Notes;
   380 
   381 fun plus1 test scan =
   382   scan -- Scan.repeat ($$$ "+" |-- Scan.unless test (!!! scan)) >> op ::;
   383 
   384 val rename = name -- Scan.option mixfix;
   385 
   386 fun expr test =
   387   let 
   388     fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
   389     and expr1 x = (expr2 -- Scan.repeat1 (maybe rename) >> Locale.Rename || expr2) x
   390     and expr0 x = (plus1 test expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   391   in expr0 end;
   392 in
   393 
   394 val locale_target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
   395 val opt_locale_target = Scan.option locale_target;
   396 val locale_expr_unless = expr
   397 val locale_expr = expr loc_keyword;
   398 val locale_keyword = loc_keyword;
   399 
   400 val locale_element = group "locale element"
   401   (loc_element >> Locale.Elem || $$$ "includes" |-- !!! locale_expr >> Locale.Expr);
   402 
   403 val context_element = group "context element" loc_element;
   404 
   405 end;
   406 
   407 
   408 (* statements *)
   409 
   410 val statement = and_list1 (opt_thm_name ":" -- Scan.repeat1 propp);
   411 
   412 val obtain_case =
   413   parname -- (Scan.optional (simple_fixes --| $$$ "where") [] --
   414     (and_list1 (Scan.repeat1 prop) >> flat));
   415 
   416 val general_statement =
   417   statement >> (fn x => ([], Element.Shows x)) ||
   418   Scan.repeat locale_element --
   419    ($$$ "obtains" |-- !!! (enum1 "|" obtain_case) >> Element.Obtains ||
   420     $$$ "shows" |-- !!! statement >> Element.Shows);
   421 
   422 val statement_keyword =  $$$ "obtains" || $$$ "shows";
   423 
   424 
   425 (* proof methods *)
   426 
   427 fun is_symid_meth s =
   428   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   429 
   430 fun meth4 x =
   431  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   432   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   433 and meth3 x =
   434  (meth4 --| $$$ "?" >> Method.Try ||
   435   meth4 --| $$$ "+" >> Method.Repeat1 ||
   436   meth4 -- ($$$ "[" |-- Scan.optional nat 1 --| $$$ "]") >> (Method.SelectGoals o swap) ||
   437   meth4) x
   438 and meth2 x =
   439  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   440   meth3) x
   441 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   442 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   443 
   444 val method = meth3;
   445 
   446 
   447 end;