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