src/Pure/Isar/spec_parse.ML
author wenzelm
Fri Mar 13 21:25:15 2009 +0100 (2009-03-13)
changeset 30513 1796b8ea88aa
parent 30481 de003023c302
child 30720 6d8dcfb264dc
permissions -rw-r--r--
eliminated type Args.T;
pervasive types 'a parser and 'a context_parser;
     1 (*  Title:      Pure/Isar/spec_parse.ML
     2     Author:     Makarius
     3 
     4 Parsers for complex specifications.
     5 *)
     6 
     7 signature SPEC_PARSE =
     8 sig
     9   val attrib: Attrib.src parser
    10   val attribs: Attrib.src list parser
    11   val opt_attribs: Attrib.src list parser
    12   val thm_name: string -> Attrib.binding parser
    13   val opt_thm_name: string -> Attrib.binding parser
    14   val spec: (Attrib.binding * string) parser
    15   val specs: (Attrib.binding * string list) parser
    16   val alt_specs: (Attrib.binding * string) list parser
    17   val where_alt_specs: (Attrib.binding * string) list parser
    18   val xthm: (Facts.ref * Attrib.src list) parser
    19   val xthms1: (Facts.ref * Attrib.src list) list parser
    20   val name_facts: (Attrib.binding * (Facts.ref * Attrib.src list) list) list parser
    21   val locale_mixfix: mixfix parser
    22   val locale_fixes: (binding * string option * mixfix) list parser
    23   val locale_insts: (string option list * (Attrib.binding * string) list) parser
    24   val class_expr: string list parser
    25   val locale_expression: Expression.expression parser
    26   val locale_keyword: string parser
    27   val context_element: Element.context parser
    28   val statement: (Attrib.binding * (string * string list) list) list parser
    29   val general_statement: (Element.context list * Element.statement) parser
    30   val statement_keyword: string parser
    31 end;
    32 
    33 structure SpecParse: SPEC_PARSE =
    34 struct
    35 
    36 structure P = OuterParse;
    37 
    38 
    39 (* theorem specifications *)
    40 
    41 val attrib = P.position ((P.keyword_ident_or_symbolic || P.xname) -- P.!!! Args.parse) >> Args.src;
    42 val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
    43 val opt_attribs = Scan.optional attribs [];
    44 
    45 fun thm_name s = P.binding -- opt_attribs --| P.$$$ s;
    46 
    47 fun opt_thm_name s =
    48   Scan.optional ((P.binding -- opt_attribs || attribs >> pair Binding.empty) --| P.$$$ s)
    49     Attrib.empty_binding;
    50 
    51 val spec = opt_thm_name ":" -- P.prop;
    52 val specs = opt_thm_name ":" -- Scan.repeat1 P.prop;
    53 
    54 val alt_specs =
    55   P.enum1 "|" (spec --| Scan.option (Scan.ahead (P.name || P.$$$ "[") -- P.!!! (P.$$$ "|")));
    56 
    57 val where_alt_specs = P.where_ |-- P.!!! alt_specs;
    58 
    59 val xthm =
    60   P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
    61   (P.alt_string >> Facts.Fact ||
    62     P.position P.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
    63 
    64 val xthms1 = Scan.repeat1 xthm;
    65 
    66 val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
    67 
    68 
    69 (* locale and context elements *)
    70 
    71 val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
    72 
    73 val locale_fixes =
    74   P.and_list1 (P.binding -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
    75     >> (single o P.triple1) ||
    76   P.params >> map Syntax.no_syn) >> flat;
    77 
    78 val locale_insts =
    79   Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
    80   -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
    81 
    82 local
    83 
    84 val loc_element =
    85   P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
    86   P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
    87     >> Element.Constrains ||
    88   P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
    89     >> Element.Assumes ||
    90   P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
    91     >> Element.Defines ||
    92   P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
    93     >> (curry Element.Notes "");
    94 
    95 fun plus1_unless test scan =
    96   scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
    97 
    98 val rename = P.name -- Scan.option P.mixfix;
    99 
   100 val prefix = P.name -- Scan.optional (P.$$$ "!" >> K true) false --| P.$$$ ":";
   101 val named = P.name -- (P.$$$ "=" |-- P.term);
   102 val position = P.maybe P.term;
   103 val instance = P.$$$ "where" |-- P.and_list1 named >> Expression.Named ||
   104   Scan.repeat1 position >> Expression.Positional;
   105 
   106 in
   107 
   108 val locale_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
   109    P.$$$ "defines" || P.$$$ "notes";
   110 
   111 val class_expr = plus1_unless locale_keyword P.xname;
   112 
   113 val locale_expression =
   114   let
   115     fun expr2 x = P.xname x;
   116     fun expr1 x = (Scan.optional prefix ("", false) -- expr2 --
   117       Scan.optional instance (Expression.Named []) >> (fn ((p, l), i) => (l, (p, i)))) x;
   118     fun expr0 x = (plus1_unless locale_keyword expr1) x;
   119   in expr0 -- Scan.optional (P.$$$ "for" |-- P.!!! locale_fixes) [] end;
   120 
   121 val context_element = P.group "context element" loc_element;
   122 
   123 end;
   124 
   125 
   126 (* statements *)
   127 
   128 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   129 
   130 val obtain_case =
   131   P.parbinding -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
   132     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   133 
   134 val general_statement =
   135   statement >> (fn x => ([], Element.Shows x)) ||
   136   Scan.repeat context_element --
   137    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   138     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   139 
   140 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   141 
   142 end;