src/Pure/Isar/spec_parse.ML
changeset 36950 75b8f26f2f07
parent 33287 0f99569d23e1
equal deleted inserted replaced
36949:080e85d46108 36950:75b8f26f2f07
    33 end;
    33 end;
    34 
    34 
    35 structure SpecParse: SPEC_PARSE =
    35 structure SpecParse: SPEC_PARSE =
    36 struct
    36 struct
    37 
    37 
    38 structure P = OuterParse;
       
    39 
       
    40 
       
    41 (* theorem specifications *)
    38 (* theorem specifications *)
    42 
    39 
    43 val attrib = P.position ((P.keyword_ident_or_symbolic || P.xname) -- P.!!! Args.parse) >> Args.src;
    40 val attrib =
    44 val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
    41   Parse.position ((Parse.keyword_ident_or_symbolic || Parse.xname) -- Parse.!!! Args.parse)
       
    42   >> Args.src;
       
    43 
       
    44 val attribs = Parse.$$$ "[" |-- Parse.list attrib --| Parse.$$$ "]";
    45 val opt_attribs = Scan.optional attribs [];
    45 val opt_attribs = Scan.optional attribs [];
    46 
    46 
    47 fun thm_name s = P.binding -- opt_attribs --| P.$$$ s;
    47 fun thm_name s = Parse.binding -- opt_attribs --| Parse.$$$ s;
    48 
    48 
    49 fun opt_thm_name s =
    49 fun opt_thm_name s =
    50   Scan.optional ((P.binding -- opt_attribs || attribs >> pair Binding.empty) --| P.$$$ s)
    50   Scan.optional ((Parse.binding -- opt_attribs || attribs >> pair Binding.empty) --| Parse.$$$ s)
    51     Attrib.empty_binding;
    51     Attrib.empty_binding;
    52 
    52 
    53 val spec = opt_thm_name ":" -- P.prop;
    53 val spec = opt_thm_name ":" -- Parse.prop;
    54 val specs = opt_thm_name ":" -- Scan.repeat1 P.prop;
    54 val specs = opt_thm_name ":" -- Scan.repeat1 Parse.prop;
    55 
    55 
    56 val alt_specs =
    56 val alt_specs =
    57   P.enum1 "|" (spec --| Scan.option (Scan.ahead (P.name || P.$$$ "[") -- P.!!! (P.$$$ "|")));
    57   Parse.enum1 "|"
       
    58     (spec --| Scan.option (Scan.ahead (Parse.name || Parse.$$$ "[") -- Parse.!!! (Parse.$$$ "|")));
    58 
    59 
    59 val where_alt_specs = P.where_ |-- P.!!! alt_specs;
    60 val where_alt_specs = Parse.where_ |-- Parse.!!! alt_specs;
    60 
    61 
    61 val xthm =
    62 val xthm =
    62   P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
    63   Parse.$$$ "[" |-- attribs --| Parse.$$$ "]" >> pair (Facts.named "") ||
    63   (P.alt_string >> Facts.Fact ||
    64   (Parse.alt_string >> Facts.Fact ||
    64     P.position P.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
    65     Parse.position Parse.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
    65 
    66 
    66 val xthms1 = Scan.repeat1 xthm;
    67 val xthms1 = Scan.repeat1 xthm;
    67 
    68 
    68 val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
    69 val name_facts = Parse.and_list1 (opt_thm_name "=" -- xthms1);
    69 
    70 
    70 
    71 
    71 (* basic constant specifications *)
    72 (* basic constant specifications *)
    72 
    73 
    73 val constdecl =
    74 val constdecl =
    74   P.binding --
    75   Parse.binding --
    75     (P.where_ >> K (NONE, NoSyn) ||
    76     (Parse.where_ >> K (NONE, NoSyn) ||
    76       P.$$$ "::" |-- P.!!! ((P.typ >> SOME) -- P.opt_mixfix' --| P.where_) ||
    77       Parse.$$$ "::" |-- Parse.!!! ((Parse.typ >> SOME) -- Parse.opt_mixfix' --| Parse.where_) ||
    77       Scan.ahead (P.$$$ "(") |-- P.!!! (P.mixfix' --| P.where_ >> pair NONE))
    78       Scan.ahead (Parse.$$$ "(") |-- Parse.!!! (Parse.mixfix' --| Parse.where_ >> pair NONE))
    78   >> P.triple2;
    79   >> Parse.triple2;
    79 
    80 
    80 val constdef = Scan.option constdecl -- (opt_thm_name ":" -- P.prop);
    81 val constdef = Scan.option constdecl -- (opt_thm_name ":" -- Parse.prop);
    81 
    82 
    82 
    83 
    83 (* locale and context elements *)
    84 (* locale and context elements *)
    84 
    85 
    85 val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
    86 val locale_mixfix =
       
    87   Parse.$$$ "(" -- Parse.$$$ "structure" -- Parse.!!! (Parse.$$$ ")") >> K Structure ||
       
    88   Parse.mixfix;
    86 
    89 
    87 val locale_fixes =
    90 val locale_fixes =
    88   P.and_list1 (P.binding -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
    91   Parse.and_list1 (Parse.binding -- Scan.option (Parse.$$$ "::" |-- Parse.typ) -- locale_mixfix
    89     >> (single o P.triple1) ||
    92     >> (single o Parse.triple1) ||
    90   P.params >> map Syntax.no_syn) >> flat;
    93   Parse.params >> map Syntax.no_syn) >> flat;
    91 
    94 
    92 val locale_insts =
    95 val locale_insts =
    93   Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
    96   Scan.optional
    94   -- Scan.optional (P.where_ |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
    97     (Parse.$$$ "[" |-- Parse.!!! (Scan.repeat1 (Parse.maybe Parse.term) --| Parse.$$$ "]")) [] --
       
    98   Scan.optional (Parse.where_ |-- Parse.and_list1 (opt_thm_name ":" -- Parse.prop)) [];
    95 
    99 
    96 local
   100 local
    97 
   101 
    98 val loc_element =
   102 val loc_element =
    99   P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
   103   Parse.$$$ "fixes" |-- Parse.!!! locale_fixes >> Element.Fixes ||
   100   P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
   104   Parse.$$$ "constrains" |--
       
   105     Parse.!!! (Parse.and_list1 (Parse.name -- (Parse.$$$ "::" |-- Parse.typ)))
   101     >> Element.Constrains ||
   106     >> Element.Constrains ||
   102   P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
   107   Parse.$$$ "assumes" |-- Parse.!!! (Parse.and_list1 (opt_thm_name ":" -- Scan.repeat1 Parse.propp))
   103     >> Element.Assumes ||
   108     >> Element.Assumes ||
   104   P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
   109   Parse.$$$ "defines" |-- Parse.!!! (Parse.and_list1 (opt_thm_name ":" -- Parse.propp))
   105     >> Element.Defines ||
   110     >> Element.Defines ||
   106   P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
   111   Parse.$$$ "notes" |-- Parse.!!! (Parse.and_list1 (opt_thm_name "=" -- xthms1))
   107     >> (curry Element.Notes "");
   112     >> (curry Element.Notes "");
   108 
   113 
   109 fun plus1_unless test scan =
   114 fun plus1_unless test scan =
   110   scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
   115   scan ::: Scan.repeat (Parse.$$$ "+" |-- Scan.unless test (Parse.!!! scan));
   111 
   116 
   112 fun prefix mandatory =
   117 fun prefix mandatory =
   113   P.name -- (P.$$$ "!" >> K true || P.$$$ "?" >> K false || Scan.succeed mandatory) --| P.$$$ ":";
   118   Parse.name --
       
   119     (Parse.$$$ "!" >> K true || Parse.$$$ "?" >> K false || Scan.succeed mandatory) --|
       
   120     Parse.$$$ ":";
   114 
   121 
   115 val instance = P.where_ |--
   122 val instance = Parse.where_ |--
   116   P.and_list1 (P.name -- (P.$$$ "=" |-- P.term)) >> Expression.Named ||
   123   Parse.and_list1 (Parse.name -- (Parse.$$$ "=" |-- Parse.term)) >> Expression.Named ||
   117   Scan.repeat1 (P.maybe P.term) >> Expression.Positional;
   124   Scan.repeat1 (Parse.maybe Parse.term) >> Expression.Positional;
   118 
   125 
   119 in
   126 in
   120 
   127 
   121 val locale_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
   128 val locale_keyword =
   122    P.$$$ "defines" || P.$$$ "notes";
   129   Parse.$$$ "fixes" || Parse.$$$ "constrains" || Parse.$$$ "assumes" ||
       
   130   Parse.$$$ "defines" || Parse.$$$ "notes";
   123 
   131 
   124 val class_expr = plus1_unless locale_keyword P.xname;
   132 val class_expr = plus1_unless locale_keyword Parse.xname;
   125 
   133 
   126 fun locale_expression mandatory =
   134 fun locale_expression mandatory =
   127   let
   135   let
   128     val expr2 = P.xname;
   136     val expr2 = Parse.xname;
   129     val expr1 = Scan.optional (prefix mandatory) ("", false) -- expr2 --
   137     val expr1 = Scan.optional (prefix mandatory) ("", false) -- expr2 --
   130       Scan.optional instance (Expression.Named []) >> (fn ((p, l), i) => (l, (p, i)));
   138       Scan.optional instance (Expression.Named []) >> (fn ((p, l), i) => (l, (p, i)));
   131     val expr0 = plus1_unless locale_keyword expr1;
   139     val expr0 = plus1_unless locale_keyword expr1;
   132   in expr0 -- Scan.optional (P.$$$ "for" |-- P.!!! locale_fixes) [] end;
   140   in expr0 -- Scan.optional (Parse.$$$ "for" |-- Parse.!!! locale_fixes) [] end;
   133 
   141 
   134 val context_element = P.group "context element" loc_element;
   142 val context_element = Parse.group "context element" loc_element;
   135 
   143 
   136 end;
   144 end;
   137 
   145 
   138 
   146 
   139 (* statements *)
   147 (* statements *)
   140 
   148 
   141 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   149 val statement = Parse.and_list1 (opt_thm_name ":" -- Scan.repeat1 Parse.propp);
   142 
   150 
   143 val obtain_case =
   151 val obtain_case =
   144   P.parbinding -- (Scan.optional (P.simple_fixes --| P.where_) [] --
   152   Parse.parbinding -- (Scan.optional (Parse.simple_fixes --| Parse.where_) [] --
   145     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   153     (Parse.and_list1 (Scan.repeat1 Parse.prop) >> flat));
   146 
   154 
   147 val general_statement =
   155 val general_statement =
   148   statement >> (fn x => ([], Element.Shows x)) ||
   156   statement >> (fn x => ([], Element.Shows x)) ||
   149   Scan.repeat context_element --
   157   Scan.repeat context_element --
   150    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   158    (Parse.$$$ "obtains" |-- Parse.!!! (Parse.enum1 "|" obtain_case) >> Element.Obtains ||
   151     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   159     Parse.$$$ "shows" |-- Parse.!!! statement >> Element.Shows);
   152 
   160 
   153 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   161 val statement_keyword = Parse.$$$ "obtains" || Parse.$$$ "shows";
   154 
   162 
   155 end;
   163 end;