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