src/Pure/Isar/spec_parse.ML
author wenzelm
Thu Mar 26 18:59:25 2009 +0100 (2009-03-26)
changeset 30726 67388cc4ccb4
parent 30720 6d8dcfb264dc
child 33287 0f99569d23e1
permissions -rw-r--r--
locale_expression: mandatory as parameter;
misc tuning and simplifications of parsers;
     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: bool -> 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 fun prefix mandatory =
    99   P.name -- (P.$$$ "!" >> K true || P.$$$ "?" >> K false || Scan.succeed mandatory) --| P.$$$ ":";
   100 
   101 val instance = P.where_ |--
   102   P.and_list1 (P.name -- (P.$$$ "=" |-- P.term)) >> Expression.Named ||
   103   Scan.repeat1 (P.maybe P.term) >> Expression.Positional;
   104 
   105 in
   106 
   107 val locale_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
   108    P.$$$ "defines" || P.$$$ "notes";
   109 
   110 val class_expr = plus1_unless locale_keyword P.xname;
   111 
   112 fun locale_expression mandatory =
   113   let
   114     val expr2 = P.xname;
   115     val expr1 = Scan.optional (prefix mandatory) ("", false) -- expr2 --
   116       Scan.optional instance (Expression.Named []) >> (fn ((p, l), i) => (l, (p, i)));
   117     val expr0 = plus1_unless locale_keyword expr1;
   118   in expr0 -- Scan.optional (P.$$$ "for" |-- P.!!! locale_fixes) [] end;
   119 
   120 val context_element = P.group "context element" loc_element;
   121 
   122 end;
   123 
   124 
   125 (* statements *)
   126 
   127 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   128 
   129 val obtain_case =
   130   P.parbinding -- (Scan.optional (P.simple_fixes --| P.where_) [] --
   131     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   132 
   133 val general_statement =
   134   statement >> (fn x => ([], Element.Shows x)) ||
   135   Scan.repeat context_element --
   136    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   137     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   138 
   139 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   140 
   141 end;