src/Pure/Isar/spec_parse.ML
author wenzelm
Sat Aug 09 22:43:58 2008 +0200 (2008-08-09)
changeset 27816 0dfed2f2822a
parent 27378 0968c0d0b969
child 28083 103d9282a946
permissions -rw-r--r--
unified Args.T with OuterLex.token, renamed some operations;
moved thm_sel to attrib.ML;
wenzelm@22104
     1
(*  Title:      Pure/Isar/spec_parse.ML
wenzelm@22104
     2
    ID:         $Id$
wenzelm@22104
     3
    Author:     Makarius
wenzelm@22104
     4
wenzelm@22104
     5
Parsers for complex specifications.
wenzelm@22104
     6
*)
wenzelm@22104
     7
wenzelm@22104
     8
signature SPEC_PARSE =
wenzelm@22104
     9
sig
wenzelm@22104
    10
  type token
wenzelm@22104
    11
  val attrib: OuterLex.token list -> Attrib.src * token list
wenzelm@22104
    12
  val attribs: token list -> Attrib.src list * token list
wenzelm@22104
    13
  val opt_attribs: token list -> Attrib.src list * token list
wenzelm@22104
    14
  val thm_name: string -> token list -> (bstring * Attrib.src list) * token list
wenzelm@22104
    15
  val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
wenzelm@22104
    16
  val spec: token list -> ((bstring * Attrib.src list) * string list) * token list
wenzelm@22104
    17
  val named_spec: token list -> ((bstring * Attrib.src list) * string list) * token list
wenzelm@22104
    18
  val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
wenzelm@22104
    19
  val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list
wenzelm@26336
    20
  val xthm: token list -> (Facts.ref * Attrib.src list) * token list
wenzelm@26336
    21
  val xthms1: token list -> (Facts.ref * Attrib.src list) list * token list
wenzelm@22104
    22
  val name_facts: token list ->
wenzelm@26336
    23
    ((bstring * Attrib.src list) * (Facts.ref * Attrib.src list) list) list * token list
wenzelm@22104
    24
  val locale_mixfix: token list -> mixfix * token list
wenzelm@22104
    25
  val locale_fixes: token list -> (string * string option * mixfix) list * token list
ballarin@25094
    26
  val locale_insts: token list ->
ballarin@25094
    27
    (string option list * ((bstring * Attrib.src list) * string) list) * token list
wenzelm@24869
    28
  val class_expr: token list -> string list * token list
wenzelm@22104
    29
  val locale_expr: token list -> Locale.expr * token list
wenzelm@22104
    30
  val locale_keyword: token list -> string * token list
wenzelm@22104
    31
  val locale_element: token list -> Element.context Locale.element * token list
wenzelm@22104
    32
  val context_element: token list -> Element.context * token list
wenzelm@22104
    33
  val statement: token list ->
wenzelm@22104
    34
    ((bstring * Attrib.src list) * (string * string list) list) list * token list
wenzelm@22104
    35
  val general_statement: token list ->
wenzelm@22104
    36
    (Element.context Locale.element list * Element.statement) * OuterLex.token list
wenzelm@22104
    37
  val statement_keyword: token list -> string * token list
wenzelm@22104
    38
  val specification: token list ->
wenzelm@22104
    39
    (string *
wenzelm@22104
    40
      (((bstring * Attrib.src list) * string list) list * (string * string option) list)) list *
wenzelm@22104
    41
    token list
wenzelm@22104
    42
end;
wenzelm@22104
    43
wenzelm@22104
    44
structure SpecParse: SPEC_PARSE =
wenzelm@22104
    45
struct
wenzelm@22104
    46
wenzelm@22104
    47
structure P = OuterParse;
wenzelm@22104
    48
type token = P.token;
wenzelm@22104
    49
wenzelm@22104
    50
wenzelm@22104
    51
(* theorem specifications *)
wenzelm@22104
    52
wenzelm@27816
    53
val attrib = P.position ((P.keyword_ident_or_symbolic || P.xname) -- P.!!! Args.parse) >> Args.src;
wenzelm@24014
    54
val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
wenzelm@22104
    55
val opt_attribs = Scan.optional attribs [];
wenzelm@22104
    56
wenzelm@22104
    57
fun thm_name s = P.name -- opt_attribs --| P.$$$ s;
wenzelm@22104
    58
fun opt_thm_name s =
wenzelm@27378
    59
  Scan.optional ((P.name -- opt_attribs || attribs >> pair "") --| P.$$$ s) ("", []);
wenzelm@22104
    60
wenzelm@22104
    61
val spec = opt_thm_name ":" -- Scan.repeat1 P.prop;
wenzelm@22104
    62
val named_spec = thm_name ":" -- Scan.repeat1 P.prop;
wenzelm@22104
    63
wenzelm@22104
    64
val spec_name = thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@22104
    65
val spec_opt_name = opt_thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@22104
    66
wenzelm@24013
    67
val xthm =
wenzelm@26361
    68
  P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
wenzelm@26361
    69
  (P.alt_string >> Facts.Fact ||
wenzelm@27816
    70
    P.position P.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
wenzelm@24013
    71
wenzelm@22104
    72
val xthms1 = Scan.repeat1 xthm;
wenzelm@22104
    73
wenzelm@22104
    74
val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
wenzelm@22104
    75
wenzelm@22104
    76
wenzelm@22104
    77
(* locale and context elements *)
wenzelm@22104
    78
wenzelm@22104
    79
val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
wenzelm@22104
    80
wenzelm@22104
    81
val locale_fixes =
wenzelm@22104
    82
  P.and_list1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
wenzelm@22104
    83
    >> (single o P.triple1) ||
wenzelm@22104
    84
  P.params >> map Syntax.no_syn) >> flat;
wenzelm@22104
    85
wenzelm@22104
    86
val locale_insts =
ballarin@22658
    87
  Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
ballarin@25094
    88
  -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
wenzelm@22104
    89
wenzelm@22104
    90
local
wenzelm@22104
    91
wenzelm@22104
    92
val loc_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
wenzelm@22104
    93
   P.$$$ "defines" || P.$$$ "notes" || P.$$$ "includes";
wenzelm@22104
    94
wenzelm@22104
    95
val loc_element =
wenzelm@22104
    96
  P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
wenzelm@22104
    97
  P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
wenzelm@22104
    98
    >> Element.Constrains ||
wenzelm@22104
    99
  P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
wenzelm@22104
   100
    >> Element.Assumes ||
wenzelm@22104
   101
  P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
wenzelm@22104
   102
    >> Element.Defines ||
wenzelm@22104
   103
  P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
wenzelm@22104
   104
    >> (curry Element.Notes "");
wenzelm@22104
   105
wenzelm@24869
   106
fun plus1_unless test scan =
wenzelm@25999
   107
  scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
wenzelm@22104
   108
wenzelm@22104
   109
val rename = P.name -- Scan.option P.mixfix;
wenzelm@22104
   110
wenzelm@24869
   111
in
wenzelm@24869
   112
wenzelm@24869
   113
val class_expr = plus1_unless loc_keyword P.xname;
wenzelm@24869
   114
wenzelm@24869
   115
val locale_expr =
wenzelm@22104
   116
  let
wenzelm@22104
   117
    fun expr2 x = (P.xname >> Locale.Locale || P.$$$ "(" |-- P.!!! (expr0 --| P.$$$ ")")) x
wenzelm@22104
   118
    and expr1 x = (expr2 -- Scan.repeat1 (P.maybe rename) >> Locale.Rename || expr2) x
wenzelm@24869
   119
    and expr0 x = (plus1_unless loc_keyword expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
wenzelm@22104
   120
  in expr0 end;
wenzelm@22104
   121
wenzelm@22104
   122
val locale_keyword = loc_keyword;
wenzelm@22104
   123
wenzelm@22104
   124
val locale_element = P.group "locale element"
wenzelm@22104
   125
  (loc_element >> Locale.Elem || P.$$$ "includes" |-- P.!!! locale_expr >> Locale.Expr);
wenzelm@22104
   126
wenzelm@22104
   127
val context_element = P.group "context element" loc_element;
wenzelm@22104
   128
wenzelm@22104
   129
end;
wenzelm@22104
   130
wenzelm@22104
   131
wenzelm@22104
   132
(* statements *)
wenzelm@22104
   133
wenzelm@22104
   134
val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
wenzelm@22104
   135
wenzelm@22104
   136
val obtain_case =
wenzelm@22104
   137
  P.parname -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
wenzelm@22104
   138
    (P.and_list1 (Scan.repeat1 P.prop) >> flat));
wenzelm@22104
   139
wenzelm@22104
   140
val general_statement =
wenzelm@22104
   141
  statement >> (fn x => ([], Element.Shows x)) ||
wenzelm@22104
   142
  Scan.repeat locale_element --
wenzelm@22104
   143
   (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
wenzelm@22104
   144
    P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
wenzelm@22104
   145
wenzelm@22104
   146
val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
wenzelm@22104
   147
wenzelm@22104
   148
wenzelm@22104
   149
(* specifications *)
wenzelm@22104
   150
wenzelm@22104
   151
val specification = P.enum1 "|" (P.parname -- (P.and_list1 spec -- P.for_simple_fixes));
wenzelm@22104
   152
wenzelm@22104
   153
end;