src/Pure/Isar/spec_parse.ML
author wenzelm
Sat Jun 28 15:17:26 2008 +0200 (2008-06-28)
changeset 27378 0968c0d0b969
parent 26361 7946f459c6c8
child 27816 0dfed2f2822a
permissions -rw-r--r--
tuned;
     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 -> (bstring * Attrib.src list) * token list
    15   val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    16   val spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    17   val named_spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    18   val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
    19   val spec_opt_name: token list -> ((bstring * 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     ((bstring * 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 -> (string * string option * mixfix) list * token list
    26   val locale_insts: token list ->
    27     (string option list * ((bstring * 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     ((bstring * 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     (string *
    40       (((bstring * Attrib.src list) * string list) list * (string * string option) list)) list *
    41     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_sid || P.xname) -- P.!!! P.arguments) >> Args.src;
    54 val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
    55 val opt_attribs = Scan.optional attribs [];
    56 
    57 fun thm_name s = P.name -- opt_attribs --| P.$$$ s;
    58 fun opt_thm_name s =
    59   Scan.optional ((P.name -- opt_attribs || attribs >> pair "") --| P.$$$ s) ("", []);
    60 
    61 val spec = opt_thm_name ":" -- Scan.repeat1 P.prop;
    62 val named_spec = thm_name ":" -- Scan.repeat1 P.prop;
    63 
    64 val spec_name = thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
    65 val spec_opt_name = opt_thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
    66 
    67 val thm_sel = P.$$$ "(" |-- P.list1
    68  (P.nat --| P.minus -- P.nat >> Facts.FromTo ||
    69   P.nat --| P.minus >> Facts.From ||
    70   P.nat >> Facts.Single) --| P.$$$ ")";
    71 
    72 val xthm =
    73   P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
    74   (P.alt_string >> Facts.Fact ||
    75     P.position P.xname -- Scan.option thm_sel >> Facts.Named) -- opt_attribs;
    76 
    77 val xthms1 = Scan.repeat1 xthm;
    78 
    79 val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
    80 
    81 
    82 (* locale and context elements *)
    83 
    84 val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
    85 
    86 val locale_fixes =
    87   P.and_list1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
    88     >> (single o P.triple1) ||
    89   P.params >> map Syntax.no_syn) >> flat;
    90 
    91 val locale_insts =
    92   Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
    93   -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
    94 
    95 local
    96 
    97 val loc_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
    98    P.$$$ "defines" || P.$$$ "notes" || P.$$$ "includes";
    99 
   100 val loc_element =
   101   P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
   102   P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
   103     >> Element.Constrains ||
   104   P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
   105     >> Element.Assumes ||
   106   P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
   107     >> Element.Defines ||
   108   P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
   109     >> (curry Element.Notes "");
   110 
   111 fun plus1_unless test scan =
   112   scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
   113 
   114 val rename = P.name -- Scan.option P.mixfix;
   115 
   116 in
   117 
   118 val class_expr = plus1_unless loc_keyword P.xname;
   119 
   120 val locale_expr =
   121   let
   122     fun expr2 x = (P.xname >> Locale.Locale || P.$$$ "(" |-- P.!!! (expr0 --| P.$$$ ")")) x
   123     and expr1 x = (expr2 -- Scan.repeat1 (P.maybe rename) >> Locale.Rename || expr2) x
   124     and expr0 x = (plus1_unless loc_keyword expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   125   in expr0 end;
   126 
   127 val locale_keyword = loc_keyword;
   128 
   129 val locale_element = P.group "locale element"
   130   (loc_element >> Locale.Elem || P.$$$ "includes" |-- P.!!! locale_expr >> Locale.Expr);
   131 
   132 val context_element = P.group "context element" loc_element;
   133 
   134 end;
   135 
   136 
   137 (* statements *)
   138 
   139 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   140 
   141 val obtain_case =
   142   P.parname -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
   143     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   144 
   145 val general_statement =
   146   statement >> (fn x => ([], Element.Shows x)) ||
   147   Scan.repeat locale_element --
   148    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   149     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   150 
   151 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   152 
   153 
   154 (* specifications *)
   155 
   156 val specification = P.enum1 "|" (P.parname -- (P.and_list1 spec -- P.for_simple_fixes));
   157 
   158 end;