src/Pure/Thy/thy_parse.ML
author wenzelm
Fri Aug 19 15:38:50 1994 +0200 (1994-08-19)
changeset 558 c4092ae47210
parent 476 836cad329311
child 570 6333c181a3f7
permissions -rw-r--r--
renamed 'defns' to 'defs';
removed 'sigclass';
replaced parents by enclose;
exported parens, brackets, mk_list, mk_big_list, mk_pair, mk_triple;
various minor internal changes;
     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 nat: token list -> string * token list
    28   val string: token list -> string * token list
    29   val verbatim: token list -> string * token list
    30   val empty: 'a -> 'b list * 'a
    31   val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
    32   val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    33   val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    34   val enum: string -> (token list -> 'a * token list)
    35     -> token list -> 'a list * token list
    36   val enum1: string -> (token list -> 'a * token list)
    37     -> token list -> 'a list * token list
    38   val list: (token list -> 'a * token list)
    39     -> token list -> 'a list * token list
    40   val list1: (token list -> 'a * token list)
    41     -> token list -> 'a list * token list
    42   val name: token list -> string * token list
    43   val sort: token list -> string * token list
    44   val opt_infix: token list -> string * token list
    45   val opt_mixfix: token list -> string * token list
    46   val parens: string -> string
    47   val brackets: string -> string
    48   val mk_list: string list -> string
    49   val mk_big_list: string list -> string
    50   val mk_pair: string * string -> string
    51   val mk_triple: string * string * string -> string
    52   type syntax
    53   val make_syntax: string list ->
    54     (string * (token list -> (string * string) * token list)) list -> syntax
    55   val parse_thy: syntax -> string -> string -> string
    56   val section: string -> string -> (token list -> string * token list)
    57     -> (string * (token list -> (string * string) * token list))
    58   val axm_section: string -> string
    59     -> (token list -> (string * string list) * token list)
    60     -> (string * (token list -> (string * string) * token list))
    61   val pure_keywords: string list
    62   val pure_sections:
    63     (string * (token list -> (string * string) * token list)) list
    64 end;
    65 
    66 functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN): THY_PARSE =
    67 struct
    68 
    69 open ThyScan;
    70 
    71 
    72 (** parser toolbox **)
    73 
    74 type token = token_kind * string * int;
    75 
    76 
    77 (* errors *)
    78 
    79 exception SYNTAX_ERROR of string * string * int;
    80 
    81 fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n);
    82 
    83 fun eof_err () = error "Unexpected end-of-file";
    84 
    85 (*similar to Prolog's cut: reports any syntax error instead of
    86   backtracking through a superior ||*)
    87 fun !! parse toks = parse toks
    88   handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^
    89     string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found");
    90 
    91 
    92 (* parser combinators *)
    93 
    94 fun (parse >> f) toks = apfst f (parse toks);
    95 
    96 fun (parse1 || parse2) toks =
    97   parse1 toks handle SYNTAX_ERROR _ => parse2 toks;
    98 
    99 fun (parse1 -- parse2) toks =
   100   let
   101     val (x, toks') = parse1 toks;
   102     val (y, toks'') = parse2 toks';
   103   in
   104     ((x, y), toks'')
   105   end;
   106 
   107 fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^;
   108 
   109 
   110 (* generic parsers *)
   111 
   112 fun $$ a ((k, b, n) :: toks) =
   113       if k = Keyword andalso a = b then (a, toks)
   114       else syn_err (quote a) (quote b) n
   115   | $$ _ [] = eof_err ();
   116 
   117 fun (a $$-- parse) = $$ a -- parse >> #2;
   118 
   119 fun (parse --$$ a) = parse -- $$ a >> #1;
   120 
   121 
   122 fun kind k1 ((k2, s, n) :: toks) =
   123       if k1 = k2 then (s, toks)
   124       else syn_err (name_of_kind k1) (quote s) n
   125   | kind _ [] = eof_err ();
   126 
   127 val ident = kind Ident;
   128 val long_ident = kind LongIdent;
   129 val long_id = ident || long_ident;
   130 val type_var = kind TypeVar >> quote;
   131 val nat = kind Nat;
   132 val string = kind String;
   133 val verbatim = kind Verbatim;
   134 val eof = kind EOF;
   135 
   136 fun empty toks = ([], toks);
   137 
   138 fun optional parse def = parse || empty >> K def;
   139 
   140 fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks;
   141 fun repeat1 parse = parse -- repeat parse >> op ::;
   142 
   143 fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::;
   144 fun enum sep parse = enum1 sep parse || empty;
   145 
   146 val list = enum ",";
   147 val list1 = enum1 ",";
   148 
   149 
   150 
   151 (** theory parsers **)
   152 
   153 (* misc utilities *)
   154 
   155 fun cat s1 s2 = s1 ^ " " ^ s2;
   156 
   157 val parens = enclose "(" ")";
   158 val brackets = enclose "[" "]";
   159 
   160 val mk_list = brackets o commas;
   161 val mk_big_list = brackets o space_implode ",\n ";
   162 
   163 fun mk_pair (x, y) = parens (commas [x, y]);
   164 fun mk_triple (x, y, z) = parens (commas [x, y, z]);
   165 fun mk_triple1 ((x, y), z) = mk_triple (x, y, z);
   166 fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z);
   167 
   168 val split_decls = flat o map (fn (xs, y) => map (rpair y) xs);
   169 
   170 fun strip_quotes str =
   171   implode (tl (take (size str - 1, explode str)));
   172 
   173 
   174 (* names *)
   175 
   176 val name = ident >> quote || string;
   177 val names = list name;
   178 val names1 = list1 name;
   179 val name_list = names >> mk_list;
   180 val name_list1 = names1 >> mk_list;
   181 
   182 
   183 (* classes *)
   184 
   185 val subclass = name -- optional ("<" $$-- !! name_list1) "[]";
   186 
   187 val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list;
   188 
   189 
   190 (* arities *)
   191 
   192 val sort =
   193   name >> brackets ||
   194   "{" $$-- name_list --$$ "}";
   195 
   196 val sort_list1 = list1 sort >> mk_list;
   197 
   198 
   199 val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort;
   200 
   201 val arity_decls = repeat1 (names1 --$$ "::" -- !! arity)
   202   >> (mk_big_list o map mk_triple2 o split_decls);
   203 
   204 
   205 (* mixfix annotations *)
   206 
   207 val infxl = "infixl" $$-- !! nat >> cat "Infixl";
   208 val infxr = "infixr" $$-- !! nat >> cat "Infixr";
   209 
   210 val binder = "binder" $$-- !! (string -- nat) >> (cat "Binder" o mk_pair);
   211 
   212 val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list;
   213 
   214 val mixfix = string -- !! (opt_pris -- optional nat "max_pri")
   215   >> (cat "Mixfix" o mk_triple2);
   216 
   217 fun opt_syn fx = optional ("(" $$-- fx --$$ ")") "NoSyn";
   218 
   219 val opt_infix = opt_syn (infxl || infxr);
   220 val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder);
   221 
   222 
   223 (* types *)
   224 
   225 fun mk_old_type_decl ((ts, n), syn) =
   226   map (fn t => (mk_triple (t, n, syn), false)) ts;
   227 
   228 fun mk_type_decl (((xs, t), None), syn) =
   229       [(mk_triple (t, string_of_int (length xs), syn), false)]
   230   | mk_type_decl (((xs, t), Some rhs), syn) =
   231       [(parens (commas [t, mk_list xs, rhs, syn]), true)];
   232 
   233 fun mk_type_decls tys =
   234   "|> add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
   235   \|> add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
   236 
   237 
   238 val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl;
   239 
   240 val type_args =
   241   type_var >> (fn x => [x]) ||
   242   "(" $$-- !! (list1 type_var --$$ ")") ||
   243   empty >> K [];
   244 
   245 val type_decl = type_args -- name -- optional ("=" $$-- !! string >> Some) None
   246   -- opt_infix >> mk_type_decl;
   247 
   248 val type_decls = repeat1 (old_type_decl || type_decl)
   249   >> (rpair "" o mk_type_decls o flat);
   250 
   251 
   252 (* consts *)
   253 
   254 val const_decls = repeat1 (names1 --$$ "::" -- !! (string -- opt_mixfix))
   255   >> (mk_big_list o map mk_triple2 o split_decls);
   256 
   257 
   258 (* translations *)
   259 
   260 val trans_pat =
   261   optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair;
   262 
   263 val trans_arrow =
   264   $$ "=>" >> K " |-> " ||
   265   $$ "<=" >> K " <-| " ||
   266   $$ "==" >> K " <-> ";
   267 
   268 val trans_decls = repeat1 (trans_pat ^^ !! (trans_arrow ^^ trans_pat))
   269   >> mk_big_list;
   270 
   271 
   272 (* ML translations *)
   273 
   274 val trfun_defs =
   275   " val parse_ast_translation = [];\n\
   276   \ val parse_translation = [];\n\
   277   \ val print_translation = [];\n\
   278   \ val print_ast_translation = [];";
   279 
   280 val trfun_args =
   281   "(parse_ast_translation, parse_translation, \
   282   \print_translation, print_ast_translation)";
   283 
   284 fun mk_mltrans txt =
   285   "let\n"
   286   ^ trfun_defs ^ "\n"
   287   ^ txt ^ "\n\
   288   \in\n\
   289   \ " ^ trfun_args ^ "\n\
   290   \end";
   291 
   292 val mltrans = verbatim >> mk_mltrans;
   293 
   294 
   295 (* axioms *)
   296 
   297 val mk_axms = mk_big_list o map (mk_pair o apfst quote);
   298 
   299 fun mk_axiom_decls axms = (mk_axms axms, map fst axms);
   300 
   301 val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls;
   302 
   303 
   304 (* axclass *)
   305 
   306 fun mk_axclass_decl ((c, cs), axms) =
   307   (mk_pair (c, cs) ^ "\n" ^ mk_axms axms,
   308     (strip_quotes c ^ "I") :: map fst axms);
   309 
   310 val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl;
   311 
   312 
   313 (* instance *)
   314 
   315 fun mk_witness (axths, opt_tac) =
   316   mk_list (keyfilter axths false) ^ "\n" ^
   317   mk_list (keyfilter axths true) ^ "\n" ^
   318   opt_tac;
   319 
   320 val axm_or_thm =
   321   string >> rpair false ||
   322   long_id >> rpair true;
   323 
   324 
   325 val opt_witness =
   326   optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
   327   optional (verbatim >> (parens o cat "Some" o parens)) "None"
   328   >> mk_witness;
   329 
   330 val instance_decl =
   331   (name --$$ "<" -- name >> (pair "|> add_inst_subclass" o mk_pair) ||
   332     name --$$ "::" -- arity >> (pair "|> add_inst_arity" o mk_triple2))
   333   -- opt_witness
   334   >> (fn ((x, y), z) => (cat_lines [x, y, z], ""));
   335 
   336 
   337 
   338 (** theory syntax **)
   339 
   340 type syntax =
   341   lexicon * (token list -> (string * string) * token list) Symtab.table;
   342 
   343 fun make_syntax keywords sects =
   344   (make_lexicon (map fst sects @ keywords),
   345     Symtab.make sects handle Symtab.DUPS dups =>
   346       error ("Duplicate sections in theory file syntax: " ^ commas_quote dups));
   347 
   348 
   349 (* header *)
   350 
   351 fun mk_header (thy_name, bases) =
   352   (thy_name, "base_on " ^ mk_list bases ^ " " ^ quote thy_name);
   353 
   354 val base =
   355   ident >> (cat "Thy" o quote) ||
   356   string >> cat "File";
   357 
   358 val header = ident --$$ "=" -- enum1 "+" base >> mk_header;
   359 
   360 
   361 (* extension *)
   362 
   363 fun mk_extension (txts, mltxt) =
   364   let
   365     val cat_sects = space_implode "\n\n" o filter_out (equal "");
   366     val (extxts, postxts) = split_list txts;
   367   in
   368     (cat_sects extxts, cat_sects postxts, mltxt)
   369   end;
   370 
   371 fun sect tab ((Keyword, s, n) :: toks) =
   372       (case Symtab.lookup (tab, s) of
   373         Some parse => !! parse toks
   374       | None => syn_err "section" s n)
   375   | sect _ ((_, s, n) :: _) = syn_err "section" s n
   376   | sect _ [] = eof_err ();
   377 
   378 fun extension sectab = "+" $$-- !! (repeat (sect sectab) --$$ "end") --
   379   optional ("ML" $$-- verbatim) "" >> mk_extension;
   380 
   381 
   382 (* theory definition *)
   383 
   384 fun mk_structure tname ((thy_name, old_thys), opt_txts) =
   385   if thy_name <> tname then
   386     error ("Filename \"" ^ tname ^ ".thy\" and theory name "
   387       ^ quote thy_name ^ " are different")
   388   else
   389     (case opt_txts of
   390       Some (extxt, postxt, mltxt) =>
   391         "structure " ^ thy_name ^ " =\n\
   392         \struct\n\
   393         \\n\
   394         \local\n"
   395         ^ trfun_defs ^ "\n\
   396         \in\n\
   397         \\n"
   398         ^ mltxt ^ "\n\
   399         \\n\
   400         \val thy = (" ^ old_thys ^ " true)\n\n\
   401         \|> add_trfuns\n"
   402         ^ trfun_args ^ "\n\
   403         \\n"
   404         ^ extxt ^ "\n\
   405         \\n\
   406         \|> add_thyname " ^ quote thy_name ^ ";\n\
   407         \\n\
   408         \\n"
   409         ^ postxt ^ "\n\
   410         \\n\
   411         \end;\n\
   412         \end;\n"
   413     | None =>
   414         "structure " ^ thy_name ^ " =\n\
   415         \struct\n\
   416         \\n\
   417         \val thy = (" ^ old_thys ^ " false);\n\
   418         \\n\
   419         \end;\n");
   420 
   421 fun theory_defn sectab tname =
   422   header -- optional (extension sectab >> Some) None -- eof
   423   >> (mk_structure tname o #1);
   424 
   425 fun parse_thy (lex, sectab) tname str =
   426   #1 (!! (theory_defn sectab tname) (tokenize lex str));
   427 
   428 
   429 (* standard sections *)
   430 
   431 fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";";
   432 
   433 fun mk_axm_sect pretxt (txt, axs) =
   434   (pretxt ^ "\n" ^ txt, cat_lines (map mk_val axs));
   435 
   436 fun axm_section name pretxt parse =
   437   (name, parse >> mk_axm_sect pretxt);
   438 
   439 fun section name pretxt parse =
   440   axm_section name pretxt (parse >> rpair []);
   441 
   442 
   443 val pure_keywords =
   444  ["end", "ML", "mixfix", "infixr", "infixl", "binder", "=", "+", ",", "<",
   445    "{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="];
   446 
   447 val pure_sections =
   448  [section "classes" "|> add_classes" class_decls,
   449   section "default" "|> add_defsort" sort,
   450   ("types", type_decls),
   451   section "arities" "|> add_arities" arity_decls,
   452   section "consts" "|> add_consts" const_decls,
   453   section "syntax" "|> add_syntax" const_decls,
   454   section "translations" "|> add_trrules" trans_decls,
   455   section "MLtrans" "|> add_trfuns" mltrans,
   456   ("MLtext", verbatim >> rpair ""),
   457   axm_section "rules" "|> add_axioms" axiom_decls,
   458   axm_section "defs" "|> add_defs" axiom_decls,
   459   axm_section "axclass" "|> add_axclass" axclass_decl,
   460   ("instance", instance_decl)];
   461 
   462 
   463 end;
   464