src/Pure/Thy/thy_parse.ML
author wenzelm
Mon May 17 21:36:34 1999 +0200 (1999-05-17)
changeset 6667 58b9785f8534
parent 6642 732af87c0650
child 8420 f37fd19476ca
permissions -rw-r--r--
cleaned comments;
     1 (*  Title:      Pure/Thy/thy_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The parser for theory files.
     6 *)
     7 
     8 infix 5 -- --$$ $$-- ^^;
     9 infix 3 >>;
    10 infix 0 ||;
    11 
    12 signature THY_PARSE =
    13 sig
    14   type token
    15   val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c
    16   val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
    17   val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
    18   val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
    19   val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
    20   val $$ : string -> token list -> string * token list
    21   val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c
    22   val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list
    23   val ident: token list -> string * token list
    24   val long_ident: token list -> string * token list
    25   val long_id: token list -> string * token list
    26   val type_var: token list -> string * token list
    27   val type_args: token list -> string list * token list
    28   val nat: token list -> string * token list
    29   val string: token list -> string * token list
    30   val verbatim: token list -> string * token list
    31   val empty: 'a -> 'b list * 'a
    32   val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
    33   val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    34   val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    35   val enum: string -> (token list -> 'a * token list)
    36     -> token list -> 'a list * token list
    37   val enum1: string -> (token list -> 'a * token list)
    38     -> token list -> 'a list * token list
    39   val list: (token list -> 'a * token list)
    40     -> token list -> 'a list * token list
    41   val list1: (token list -> 'a * token list)
    42     -> token list -> 'a list * token list
    43   val name: token list -> string * token list
    44   val sort: token list -> string * token list
    45   val typ: token list -> string * token list
    46   val opt_infix: token list -> string * token list
    47   val opt_mixfix: token list -> string * token list
    48   val opt_witness: token list -> string * token list
    49   val const_decls: token list -> string * token list
    50   type syntax
    51   val get_lexicon: syntax -> Scan.lexicon;
    52   val make_syntax: string list ->
    53     (string * (token list -> (string * string) * token list)) list -> syntax
    54   val parse_thy: syntax -> string list -> string
    55   val section: string -> string -> (token list -> string * token list)
    56     -> (string * (token list -> (string * string) * token list))
    57   val axm_section: string -> string
    58     -> (token list -> (string * string list) * token list)
    59     -> (string * (token list -> (string * string) * token list))
    60   val pure_keywords: string list
    61   val pure_sections:
    62     (string * (token list -> (string * string) * token list)) list
    63   (*items for building strings*)
    64   val cat: string -> string -> string
    65   val parens: string -> string
    66   val brackets: string -> string
    67   val mk_list: string list -> string
    68   val mk_big_list: string list -> string
    69   val mk_pair: string * string -> string
    70   val mk_triple: string * string * string -> string
    71   val mk_triple1: (string * string) * string -> string
    72   val mk_triple2: string * (string * string) -> string
    73 end;
    74 
    75 
    76 structure ThyParse : THY_PARSE=
    77 struct
    78 
    79 open ThyScan;
    80 
    81 
    82 (** parser toolbox **)
    83 
    84 type token = token_kind * string * int;
    85 
    86 
    87 (* errors *)
    88 
    89 exception SYNTAX_ERROR of string * string * int;
    90 
    91 fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n);
    92 
    93 fun eof_err () = error "Unexpected end-of-file";
    94 
    95 (*Similar to Prolog's cut: reports any syntax error instead of backtracking
    96   through a superior || *)
    97 fun !! parse toks = parse toks
    98   handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^
    99     string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found");
   100 
   101 
   102 (* parser combinators *)
   103 
   104 fun (parse >> f) toks = apfst f (parse toks);
   105 
   106 fun (parse1 || parse2) toks =
   107   parse1 toks handle SYNTAX_ERROR _ => parse2 toks;
   108 
   109 fun (parse1 -- parse2) toks =
   110   let
   111     val (x, toks') = parse1 toks;
   112     val (y, toks'') = parse2 toks';
   113   in
   114     ((x, y), toks'')
   115   end;
   116 
   117 fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^;
   118 
   119 
   120 (* generic parsers *)
   121 
   122 fun $$ a ((k, b, n) :: toks) =
   123       if k = Keyword andalso a = b then (a, toks)
   124       else syn_err (quote a) (quote b) n
   125   | $$ _ [] = eof_err ();
   126 
   127 fun (a $$-- parse) = $$ a -- parse >> #2;
   128 
   129 fun (parse --$$ a) = parse -- $$ a >> #1;
   130 
   131 
   132 fun kind k1 ((k2, s, n) :: toks) =
   133       if k1 = k2 then (s, toks)
   134       else syn_err (name_of_kind k1) (quote s) n
   135   | kind _ [] = eof_err ();
   136 
   137 val ident = kind Ident;
   138 val long_ident = kind LongIdent;
   139 val long_id = ident || long_ident;
   140 val type_var = kind TypeVar >> quote;
   141 val nat = kind Nat;
   142 val string = kind String;
   143 val verbatim = kind Verbatim;
   144 val eof = kind EOF;
   145 
   146 fun empty toks = ([], toks);
   147 
   148 fun optional parse def = parse || empty >> K def;
   149 
   150 fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks;
   151 fun repeat1 parse = parse -- repeat parse >> op ::;
   152 
   153 fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::;
   154 fun enum sep parse = enum1 sep parse || empty;
   155 
   156 fun list1 parse = enum1 "," parse;
   157 fun list parse = enum "," parse;
   158 
   159 
   160 
   161 (** theory parsers **)
   162 
   163 (* misc utilities *)
   164 
   165 fun cat s1 s2 = s1 ^ " " ^ s2;
   166 
   167 val parens = enclose "(" ")";
   168 val brackets = enclose "[" "]";
   169 
   170 val mk_list = brackets o commas;
   171 val mk_big_list = brackets o space_implode ",\n ";
   172 
   173 fun mk_pair (x, y) = parens (commas [x, y]);
   174 fun mk_triple (x, y, z) = parens (commas [x, y, z]);
   175 fun mk_triple1 ((x, y), z) = mk_triple (x, y, z);
   176 fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z);
   177 
   178 fun split_decls l = flat (map (fn (xs, y) => map (rpair y) xs) l);
   179 
   180 
   181 (* names *)
   182 
   183 val name = ident >> quote || string;
   184 val names = list name;
   185 val names1 = list1 name;
   186 val name_list = names >> mk_list;
   187 val name_list1 = names1 >> mk_list;
   188 
   189 
   190 (* empty *)
   191 
   192 fun empty_decl toks = (empty >> K "") toks;
   193 
   194 
   195 (* classes *)
   196 
   197 val subclass = name -- optional ("<" $$-- !! name_list1) "[]";
   198 
   199 val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list;
   200 
   201 
   202 (* arities *)
   203 
   204 val sort =
   205   name >> brackets ||
   206   "{" $$-- name_list --$$ "}";
   207 
   208 val sort_list1 = list1 sort >> mk_list;
   209 
   210 
   211 val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort;
   212 
   213 val arity_decls = repeat1 (names1 --$$ "::" -- !! arity)
   214   >> (mk_big_list o map mk_triple2 o split_decls);
   215 
   216 
   217 (* mixfix annotations *)
   218 
   219 val infxl =
   220   "infixl" $$-- !! (nat >> cat "Infixl" || string -- nat >> (cat "InfixlName" o mk_pair));
   221 val infxr =
   222   "infixr" $$-- !! (nat >> cat "Infixr" || string -- nat >> (cat "InfixrName" o mk_pair));
   223 
   224 val binder = "binder" $$--
   225   !! (string -- (("[" $$-- nat --$$ "]") -- nat || nat >> (fn n => (n, n))))
   226     >> (cat "Binder" o mk_triple2);
   227 
   228 val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list;
   229 
   230 val mixfix = string -- !! (opt_pris -- optional nat "Syntax.max_pri")
   231   >> (cat "Mixfix" o mk_triple2);
   232 
   233 fun opt_syn fx = optional ("(" $$-- fx --$$ ")") "NoSyn";
   234 
   235 val opt_infix = opt_syn (infxl || infxr);
   236 val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder);
   237 
   238 
   239 (* types *)
   240 
   241 (*Parse an identifier, but only if it is not followed by "::", "=" or ",";
   242   the exclusion of a postfix comma can be controlled to allow expressions
   243   like "(id, id)" but disallow ones like "'a => id id,id :: ..."*)
   244 fun ident_no_colon _ [] = eof_err()
   245   | ident_no_colon allow_comma ((Ident, s, n) :: (rest as (Keyword, s2, n2) ::
   246                                 toks)) =
   247       if s2 = "::" orelse s2 = "=" orelse (not allow_comma andalso s2 = ",")
   248       then syn_err (name_of_kind Ident) (quote s2) n2
   249       else (s, rest)
   250   | ident_no_colon _ ((Ident, s, n) :: toks) = (s, toks)
   251   | ident_no_colon _ ((k, s, n) :: _) =
   252       syn_err (name_of_kind Ident) (quote s) n;
   253 
   254 (*type used in types, consts and syntax sections*)
   255 fun const_type allow_comma toks =
   256   let
   257     val simple_type =
   258       (ident || kind TypeVar ^^ optional ($$ "::" ^^ ident) "") --
   259           repeat (ident_no_colon allow_comma)
   260           >> (fn (args, ts) => cat args (space_implode " " ts)) ||
   261         ("(" $$-- (list1 (const_type true)) --$$ ")" >> (parens o commas)) --
   262           repeat1 (ident_no_colon allow_comma)
   263           >> (fn (args, ts) => cat args (space_implode " " ts));
   264 
   265       val appl_param =
   266         simple_type || "(" $$-- const_type true --$$ ")" >> parens || 
   267         "[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" --
   268           const_type allow_comma >>
   269           (fn (src, dest) => mk_list src ^ " => " ^ dest);
   270   in ("[" $$-- (list1 (const_type true)) --$$ "]" --$$ "=>" --
   271         const_type allow_comma >>
   272         (fn (src, dest) => mk_list src ^ " => " ^ dest) ||
   273       repeat1 (appl_param --$$ "=>") -- const_type allow_comma >>
   274         (fn (src, dest) => space_implode " => " (src@[dest])) ||
   275       simple_type ||
   276       "(" $$-- const_type true --$$ ")" >> parens) toks
   277   end;
   278 
   279 val typ = string || (const_type false >> quote);
   280 
   281 
   282 fun mk_old_type_decl ((ts, n), syn) =
   283   map (fn t => (mk_triple (t, n, syn), false)) ts;
   284 
   285 fun mk_type_decl (((xs, t), None), syn) =
   286       [(mk_triple (t, string_of_int (length xs), syn), false)]
   287   | mk_type_decl (((xs, t), Some rhs), syn) =
   288       [(parens (commas [t, mk_list xs, rhs, syn]), true)];
   289 
   290 fun mk_type_decls tys =
   291   "|> Theory.add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
   292   \|> Theory.add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
   293 
   294 
   295 val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl;
   296 
   297 val type_args =
   298   type_var >> (fn x => [x]) ||
   299   "(" $$-- !! (list1 type_var --$$ ")") ||
   300   empty >> K [];
   301 
   302 val type_decl = type_args -- name --
   303   optional ("=" $$-- typ >> Some) None -- opt_infix >> mk_type_decl;
   304 
   305 val type_decls =
   306   repeat1 (old_type_decl || type_decl) >> (mk_type_decls o flat);
   307 
   308 
   309 (* consts *)
   310 
   311 val const_decls =
   312   repeat1 (names1 --$$ "::" -- !! (typ -- opt_mixfix))
   313   >> (mk_big_list o map mk_triple2 o split_decls);
   314 
   315 val opt_mode =
   316   optional
   317     ("(" $$-- !! (name -- optional ($$ "output" >> K "false") "true" --$$ ")"))
   318     ("\"\"", "true")
   319   >> mk_pair;
   320 
   321 val syntax_decls = opt_mode -- const_decls >> (fn (mode, txt) => mode ^ "\n" ^ txt);
   322 
   323 
   324 (* translations *)
   325 
   326 val trans_pat =
   327   optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair;
   328 
   329 val trans_arrow =
   330   $$ "=>" >> K "Syntax.ParseRule " ||
   331   $$ "<=" >> K "Syntax.PrintRule " ||
   332   $$ "==" >> K "Syntax.ParsePrintRule ";
   333 
   334 val trans_line =
   335   trans_pat -- !! (trans_arrow -- trans_pat)
   336     >> (fn (left, (arr, right)) => arr ^ mk_pair (left, right));
   337 
   338 val trans_decls = repeat1 trans_line >> mk_big_list;
   339 
   340 
   341 (* ML translations *)
   342 
   343 val local_defs =
   344   " val parse_ast_translation = [];\n\
   345   \ val parse_translation = [];\n\
   346   \ val print_translation = [];\n\
   347   \ val typed_print_translation = [];\n\
   348   \ val print_ast_translation = [];\n\
   349   \ val token_translation = [];";
   350 
   351 val trfun_args =
   352   "(parse_ast_translation, parse_translation, \
   353   \print_translation, print_ast_translation)";
   354 
   355 
   356 (* axioms *)
   357 
   358 val mk_axms = mk_big_list o map (mk_pair o apfst quote);
   359 val mk_axms' = mk_big_list o map (mk_pair o rpair "[]" o mk_pair o apfst quote);
   360 
   361 fun mk_axiom_decls axms = (mk_axms axms, map fst axms);
   362 
   363 val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls;
   364 
   365 
   366 (* oracle *)
   367 
   368 val oracle_decl = (name --$$ "=" -- long_id) >> mk_pair;
   369 
   370 
   371 (* combined consts and axioms *)
   372 
   373 fun mk_constaxiom_decls x =
   374   let
   375     val (axms_defs, axms_names) =
   376       mk_axiom_decls (map (fn ((id, _), def) => (id ^ "_def", def)) x);
   377   in ((mk_big_list o map mk_triple2 o map (apfst quote o fst)) x ^
   378        "\n\n|> (PureThy.add_defs o map Thm.no_attributes)\n" ^ axms_defs, axms_names)
   379   end;
   380 
   381 val constaxiom_decls =
   382   repeat1 (ident --$$ "::" -- !! (typ -- opt_mixfix) -- !! string)
   383   >> mk_constaxiom_decls;
   384 
   385 
   386 (* axclass *)
   387 
   388 fun mk_axclass_decl ((c, cs), axms) =
   389   (mk_pair (c, cs) ^ "\n" ^ mk_axms' axms, unenclose c ^ "I" :: map fst axms);
   390 
   391 val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl;
   392 
   393 
   394 (* instance *)
   395 
   396 fun mk_witness (axths, opt_tac) =
   397   mk_list (keyfilter axths false) ^ "\n" ^
   398   mk_list (keyfilter axths true) ^ "\n" ^
   399   opt_tac;
   400 
   401 val axm_or_thm =
   402   string >> rpair false ||
   403   long_id >> rpair true;
   404 
   405 
   406 val opt_witness =
   407   optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
   408   optional (verbatim >> (parens o cat "Some" o parens)) "None"
   409   >> mk_witness;
   410 
   411 val instance_decl =
   412   (name --$$ "<" -- name >> (pair "|> AxClass.add_inst_subclass" o mk_pair) ||
   413     name --$$ "::" -- arity >> (pair "|> AxClass.add_inst_arity" o mk_triple2))
   414   -- opt_witness
   415   >> (fn ((x, y), z) => (cat_lines [x, y, z]));
   416 
   417 
   418 (* locale *)
   419 
   420 val locale_decl =
   421   (name --$$ "=") -- 
   422     (optional ((ident >> (fn x => parens ("Some" ^ quote x))) --$$ "+") ("None")) --
   423     ("fixes" $$--
   424       (repeat (name --$$ "::" -- !! (typ -- opt_mixfix)) 
   425        >> (mk_big_list o map mk_triple2))) --
   426     (optional 
   427      ("assumes" $$-- (repeat ((ident >> quote) -- !! string) 
   428 		     >> (mk_list o map mk_pair)))
   429      "[]") --
   430     (optional 
   431      ("defines" $$-- (repeat ((ident >> quote) -- !! string) 
   432 		      >> (mk_list o map mk_pair)))
   433      "[]")
   434   >> (fn ((((nm, ext), cs), asms), defs) => cat_lines [nm, ext, cs, asms, defs]);
   435 
   436 
   437 
   438 (** theory syntax **)
   439 
   440 type syntax =
   441   Scan.lexicon * (token list -> (string * string) * token list) Symtab.table;
   442 
   443 fun get_lexicon (lex, _) = lex;
   444 
   445 fun make_syntax keywords sects =
   446   let
   447     val dups = duplicates (map fst sects);
   448     val sects' = gen_distinct eq_fst sects;
   449     val keys = map Symbol.explode (map fst sects' @ keywords);
   450   in
   451     if null dups then ()
   452     else warning ("Duplicate declaration of theory file sections:\n" ^ commas_quote dups);
   453     (Scan.make_lexicon keys, Symtab.make sects')
   454   end;
   455 
   456 
   457 (* header *)
   458 
   459 fun mk_header (thy_name, parents) =
   460   (thy_name, "IsarThy.begin_theory " ^ cat (quote thy_name) (mk_list parents) ^ " []");
   461 
   462 val header = ident --$$ "=" -- enum1 "+" name >> mk_header;
   463 
   464 
   465 (* extension *)
   466 
   467 fun mk_extension (txts, mltxt) =
   468   let
   469     val cat_sects = space_implode "\n\n" o filter_out (equal "");
   470     val (extxts, postxts) = split_list txts;
   471   in
   472     (cat_sects extxts, cat_sects postxts, mltxt)
   473   end;
   474 
   475 fun sect tab ((Keyword, s, n) :: toks) =
   476       (case Symtab.lookup (tab, s) of
   477         Some parse => !! parse toks
   478       | None => syn_err "section" s n)
   479   | sect _ ((_, s, n) :: _) = syn_err "section" s n
   480   | sect _ [] = eof_err ();
   481 
   482 fun extension sectab = "+" $$-- !!
   483   (repeat (sect sectab) --$$ "end" -- optional verbatim "")
   484     >> mk_extension;
   485 
   486 fun opt_extension sectab = optional (extension sectab) ("", "", "");
   487 
   488 
   489 (* theory definition *)
   490 
   491 fun mk_structure ((thy_name, bg_theory), (extxt, postxt, mltxt)) =
   492   "structure " ^ thy_name ^ " =\n\
   493   \struct\n\
   494   \\n\
   495   \local\n"
   496   ^ local_defs ^ "\n\
   497   \in\n\
   498   \\n"
   499   ^ mltxt ^ "\n\
   500   \\n\
   501   \val thy = " ^ bg_theory ^ "\n\
   502   \\n\
   503   \|> Theory.add_trfuns\n"
   504   ^ trfun_args ^ "\n\
   505   \|> Theory.add_trfunsT typed_print_translation\n\
   506   \|> Theory.add_tokentrfuns token_translation\n\
   507   \\n"
   508   ^ extxt ^ "\n\
   509   \\n\
   510   \|> IsarThy.end_theory;\n\
   511   \\n\
   512   \val _ = context thy;\n\
   513   \\n\
   514   \\n"
   515   ^ postxt ^ "\n\
   516   \\n\
   517   \end;\n\
   518   \end;\n\
   519   \\n\
   520   \open " ^ thy_name ^ ";\n";
   521 
   522 fun theory_defn sectab =
   523   header -- opt_extension sectab -- eof >> (mk_structure o #1);
   524 
   525 fun parse_thy (lex, sectab) chs = #1 (!! (theory_defn sectab) (tokenize lex chs));
   526 
   527 
   528 (* standard sections *)
   529 
   530 fun mk_val ax = "val " ^ ax ^ " = PureThy.get_thm thy " ^ quote ax ^ ";";
   531 val mk_vals = cat_lines o map mk_val;
   532 
   533 fun mk_axm_sect "" (txt, axs) = (txt, mk_vals axs)
   534   | mk_axm_sect pretxt (txt, axs) = (pretxt ^ "\n" ^ txt, mk_vals axs);
   535 
   536 fun axm_section name pretxt parse =
   537   (name, parse >> mk_axm_sect pretxt);
   538 
   539 fun section name pretxt parse =
   540   axm_section name pretxt (parse >> rpair []);
   541 
   542 
   543 val pure_keywords =
   544  ["end", "ML", "mixfix", "infixr", "infixl", "binder", "output", "=",
   545   "+", ",", "<", "{", "}", "(", ")", "[", "]", "::", "==", "=>",
   546   "<=", "fixes", "assumes", "defines"];
   547 
   548 val pure_sections =
   549  [section "classes" "|> Theory.add_classes" class_decls,
   550   section "default" "|> Theory.add_defsort" sort,
   551   section "types" "" type_decls,
   552   section "nonterminals" "|> Theory.add_nonterminals" (repeat1 name >> mk_list),
   553   section "arities" "|> Theory.add_arities" arity_decls,
   554   section "consts" "|> Theory.add_consts" const_decls,
   555   section "syntax" "|> Theory.add_modesyntax" syntax_decls,
   556   section "translations" "|> Theory.add_trrules" trans_decls,
   557   axm_section "rules" "|> (PureThy.add_axioms o map Thm.no_attributes)" axiom_decls,
   558   axm_section "defs" "|> (PureThy.add_defs o map Thm.no_attributes)" axiom_decls,
   559   section "oracle" "|> Theory.add_oracle" oracle_decl,
   560   axm_section "constdefs" "|> Theory.add_consts" constaxiom_decls,
   561   axm_section "axclass" "|> (#1 ooo AxClass.add_axclass)" axclass_decl,
   562   section "instance" "" instance_decl,
   563   section "path" "|> Theory.add_path" name,
   564   section "global" "|> PureThy.global_path" empty_decl,
   565   section "local" "|> PureThy.local_path" empty_decl,
   566   section "setup" "|> Library.apply" long_id,
   567   section "MLtext" "" verbatim,
   568   section "locale" "|> Locale.add_locale" locale_decl];
   569 
   570 
   571 end;