src/Pure/Isar/spec_parse.ML
author ballarin
Fri Oct 19 12:21:32 2007 +0200 (2007-10-19)
changeset 25094 ba43514068fd
parent 24869 bad2b2be1f24
child 25999 f8bcd311d501
permissions -rw-r--r--
Interpretation equations may have name and/or attribute.
     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 -> (thmref * Attrib.src list) * token list
    21   val xthms1: token list -> (thmref * Attrib.src list) list * token list
    22   val name_facts: token list ->
    23     ((bstring * Attrib.src list) * (thmref * 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 >> PureThy.FromTo ||
    69   P.nat --| P.minus >> PureThy.From ||
    70   P.nat >> PureThy.Single) --| P.$$$ ")";
    71 
    72 val xthm =
    73   P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Name "") ||
    74   (P.alt_string >> Fact || P.xname -- thm_sel >> NameSelection || P.xname >> Name) -- opt_attribs;
    75 
    76 val xthms1 = Scan.repeat1 xthm;
    77 
    78 val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
    79 
    80 
    81 (* locale and context elements *)
    82 
    83 val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
    84 
    85 val locale_fixes =
    86   P.and_list1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
    87     >> (single o P.triple1) ||
    88   P.params >> map Syntax.no_syn) >> flat;
    89 
    90 val locale_insts =
    91   Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
    92   -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
    93 
    94 local
    95 
    96 val loc_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
    97    P.$$$ "defines" || P.$$$ "notes" || P.$$$ "includes";
    98 
    99 val loc_element =
   100   P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
   101   P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
   102     >> Element.Constrains ||
   103   P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
   104     >> Element.Assumes ||
   105   P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
   106     >> Element.Defines ||
   107   P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
   108     >> (curry Element.Notes "");
   109 
   110 fun plus1_unless test scan =
   111   scan -- Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan)) >> op ::;
   112 
   113 val rename = P.name -- Scan.option P.mixfix;
   114 
   115 in
   116 
   117 val class_expr = plus1_unless loc_keyword P.xname;
   118 
   119 val locale_expr =
   120   let
   121     fun expr2 x = (P.xname >> Locale.Locale || P.$$$ "(" |-- P.!!! (expr0 --| P.$$$ ")")) x
   122     and expr1 x = (expr2 -- Scan.repeat1 (P.maybe rename) >> Locale.Rename || expr2) x
   123     and expr0 x = (plus1_unless loc_keyword expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   124   in expr0 end;
   125 
   126 val locale_keyword = loc_keyword;
   127 
   128 val locale_element = P.group "locale element"
   129   (loc_element >> Locale.Elem || P.$$$ "includes" |-- P.!!! locale_expr >> Locale.Expr);
   130 
   131 val context_element = P.group "context element" loc_element;
   132 
   133 end;
   134 
   135 
   136 (* statements *)
   137 
   138 val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
   139 
   140 val obtain_case =
   141   P.parname -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
   142     (P.and_list1 (Scan.repeat1 P.prop) >> flat));
   143 
   144 val general_statement =
   145   statement >> (fn x => ([], Element.Shows x)) ||
   146   Scan.repeat locale_element --
   147    (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
   148     P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
   149 
   150 val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
   151 
   152 
   153 (* specifications *)
   154 
   155 val specification = P.enum1 "|" (P.parname -- (P.and_list1 spec -- P.for_simple_fixes));
   156 
   157 end;