src/Pure/Isar/spec_parse.ML
author wenzelm
Tue Sep 02 14:10:45 2008 +0200 (2008-09-02)
changeset 28083 103d9282a946
parent 27816 0dfed2f2822a
child 28084 a05ca48ef263
permissions -rw-r--r--
explicit type Name.binding for higher-specification elements;
     1 (*  Title:      Pure/Isar/spec_parse.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Parsers for complex specifications.
     6 *)
     7 
     8 signature SPEC_PARSE =
     9 sig
    10   type token
    11   val attrib: OuterLex.token list -> Attrib.src * token list
    12   val attribs: token list -> Attrib.src list * token list
    13   val opt_attribs: token list -> Attrib.src list * token list
    14   val thm_name: string -> token list -> (Name.binding * Attrib.src list) * token list
    15   val opt_thm_name: string -> token list -> (Name.binding * Attrib.src list) * token list
    16   val spec: token list -> ((Name.binding * Attrib.src list) * string list) * token list
    17   val named_spec: token list -> ((Name.binding * Attrib.src list) * string list) * token list
    18   val spec_name: token list -> ((Name.binding * string) * Attrib.src list) * token list
    19   val spec_opt_name: token list -> ((Name.binding * string) * Attrib.src list) * token list
    20   val xthm: token list -> (Facts.ref * Attrib.src list) * token list
    21   val xthms1: token list -> (Facts.ref * Attrib.src list) list * token list
    22   val name_facts: token list ->
    23     ((Name.binding * Attrib.src list) * (Facts.ref * Attrib.src list) list) list * token list
    24   val locale_mixfix: token list -> mixfix * token list
    25   val locale_fixes: token list -> (Name.binding * string option * mixfix) list * token list
    26   val locale_insts: token list ->
    27     (string option list * ((Name.binding * Attrib.src list) * string) list) * token list
    28   val class_expr: token list -> string list * token list
    29   val locale_expr: token list -> Locale.expr * token list
    30   val locale_keyword: token list -> string * token list
    31   val locale_element: token list -> Element.context Locale.element * token list
    32   val context_element: token list -> Element.context * token list
    33   val statement: token list ->
    34     ((Name.binding * Attrib.src list) * (string * string list) list) list * token list
    35   val general_statement: token list ->
    36     (Element.context Locale.element list * Element.statement) * OuterLex.token list
    37   val statement_keyword: token list -> string * token list
    38   val specification: token list ->
    39     (Name.binding *
    40       (((Name.binding * Attrib.src list) * string list) list *
    41         (Name.binding * string option) list)) list * token list
    42 end;
    43 
    44 structure SpecParse: SPEC_PARSE =
    45 struct
    46 
    47 structure P = OuterParse;
    48 type token = P.token;
    49 
    50 
    51 (* theorem specifications *)
    52 
    53 val attrib = P.position ((P.keyword_ident_or_symbolic || P.xname) -- P.!!! Args.parse) >> Args.src;
    54 val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
    55 val opt_attribs = Scan.optional attribs [];
    56 
    57 fun thm_name s = P.binding -- opt_attribs --| P.$$$ s;
    58 
    59 fun opt_thm_name s =
    60   Scan.optional ((P.binding -- opt_attribs || attribs >> pair Name.no_binding) --| P.$$$ s)
    61     (Name.no_binding, []);
    62 
    63 val spec = opt_thm_name ":" -- Scan.repeat1 P.prop;
    64 val named_spec = thm_name ":" -- Scan.repeat1 P.prop;
    65 
    66 val spec_name = thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
    67 val spec_opt_name = opt_thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
    68 
    69 val xthm =
    70   P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
    71   (P.alt_string >> Facts.Fact ||
    72     P.position P.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
    73 
    74 val xthms1 = Scan.repeat1 xthm;
    75 
    76 val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
    77 
    78 
    79 (* locale and context elements *)
    80 
    81 val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
    82 
    83 val locale_fixes =
    84   P.and_list1 (P.binding -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
    85     >> (single o P.triple1) ||
    86   P.params >> map Syntax.no_syn) >> flat;
    87 
    88 val locale_insts =
    89   Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
    90   -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
    91 
    92 local
    93 
    94 val loc_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
    95    P.$$$ "defines" || P.$$$ "notes" || P.$$$ "includes";
    96 
    97 val loc_element =
    98   P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
    99   P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
   100     >> Element.Constrains ||
   101   P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
   102     >> Element.Assumes ||
   103   P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
   104     >> Element.Defines ||
   105   P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
   106     >> (curry Element.Notes "");
   107 
   108 fun plus1_unless test scan =
   109   scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
   110 
   111 val rename = P.name -- Scan.option P.mixfix;
   112 
   113 in
   114 
   115 val class_expr = plus1_unless loc_keyword P.xname;
   116 
   117 val locale_expr =
   118   let
   119     fun expr2 x = (P.xname >> Locale.Locale || P.$$$ "(" |-- P.!!! (expr0 --| P.$$$ ")")) x
   120     and expr1 x = (expr2 -- Scan.repeat1 (P.maybe rename) >> Locale.Rename || expr2) x
   121     and expr0 x = (plus1_unless loc_keyword expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   122   in expr0 end;
   123 
   124 val locale_keyword = loc_keyword;
   125 
   126 val locale_element = P.group "locale element"
   127   (loc_element >> Locale.Elem || P.$$$ "includes" |-- P.!!! locale_expr >> Locale.Expr);
   128 
   129 val context_element = P.group "context element" loc_element;
   130 
   131 end;
   132 
   133 
   134 (* statements *)
   135 
   136 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   137 
   138 val obtain_case =
   139   P.parbinding -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
   140     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   141 
   142 val general_statement =
   143   statement >> (fn x => ([], Element.Shows x)) ||
   144   Scan.repeat locale_element --
   145    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   146     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   147 
   148 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   149 
   150 
   151 (* specifications *)
   152 
   153 val specification = P.enum1 "|" (P.parbinding -- (P.and_list1 spec -- P.for_simple_fixes));
   154 
   155 end;