src/Pure/Isar/spec_parse.ML
author ballarin
Wed Dec 10 10:11:18 2008 +0100 (2008-12-10)
changeset 29033 721529248e31
parent 28965 1de908189869
child 29214 76c7fc5ba849
permissions -rw-r--r--
Enable keyword 'structure' in for clause of locale expression.
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@28084
    14
  val thm_name: string -> token list -> Attrib.binding * token list
wenzelm@28084
    15
  val opt_thm_name: string -> token list -> Attrib.binding * token list
wenzelm@28084
    16
  val spec: token list -> (Attrib.binding * string list) * token list
wenzelm@28084
    17
  val named_spec: token list -> (Attrib.binding * string list) * token list
haftmann@28965
    18
  val spec_name: token list -> ((Binding.T * string) * Attrib.src list) * token list
haftmann@28965
    19
  val spec_opt_name: token list -> ((Binding.T * 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@28084
    23
    (Attrib.binding * (Facts.ref * Attrib.src list) list) list * token list
wenzelm@22104
    24
  val locale_mixfix: token list -> mixfix * token list
haftmann@28965
    25
  val locale_fixes: token list -> (Binding.T * string option * mixfix) list * token list
wenzelm@28084
    26
  val locale_insts: token list -> (string option list * (Attrib.binding * string) list) * token list
wenzelm@24869
    27
  val class_expr: token list -> string list * token list
wenzelm@22104
    28
  val locale_expr: token list -> Locale.expr * token list
ballarin@29033
    29
  val locale_expression: OuterParse.token list -> Expression.expression * OuterParse.token list
wenzelm@22104
    30
  val locale_keyword: token list -> string * token list
wenzelm@22104
    31
  val context_element: token list -> Element.context * token list
wenzelm@28084
    32
  val statement: token list -> (Attrib.binding * (string * string list) list) list * token list
wenzelm@22104
    33
  val general_statement: token list ->
ballarin@28710
    34
    (Element.context list * Element.statement) * OuterLex.token list
wenzelm@22104
    35
  val statement_keyword: token list -> string * token list
wenzelm@22104
    36
  val specification: token list ->
haftmann@28965
    37
    (Binding.T *
haftmann@28965
    38
      ((Attrib.binding * string list) list * (Binding.T * string option) list)) list * token list
wenzelm@22104
    39
end;
wenzelm@22104
    40
wenzelm@22104
    41
structure SpecParse: SPEC_PARSE =
wenzelm@22104
    42
struct
wenzelm@22104
    43
wenzelm@22104
    44
structure P = OuterParse;
wenzelm@22104
    45
type token = P.token;
wenzelm@22104
    46
wenzelm@22104
    47
wenzelm@22104
    48
(* theorem specifications *)
wenzelm@22104
    49
wenzelm@27816
    50
val attrib = P.position ((P.keyword_ident_or_symbolic || P.xname) -- P.!!! Args.parse) >> Args.src;
wenzelm@24014
    51
val attribs = P.$$$ "[" |-- P.list attrib --| P.$$$ "]";
wenzelm@22104
    52
val opt_attribs = Scan.optional attribs [];
wenzelm@22104
    53
wenzelm@28083
    54
fun thm_name s = P.binding -- opt_attribs --| P.$$$ s;
wenzelm@28083
    55
wenzelm@22104
    56
fun opt_thm_name s =
haftmann@28965
    57
  Scan.optional ((P.binding -- opt_attribs || attribs >> pair Binding.empty) --| P.$$$ s)
haftmann@28965
    58
    Attrib.empty_binding;
wenzelm@22104
    59
wenzelm@22104
    60
val spec = opt_thm_name ":" -- Scan.repeat1 P.prop;
wenzelm@22104
    61
val named_spec = thm_name ":" -- Scan.repeat1 P.prop;
wenzelm@22104
    62
wenzelm@22104
    63
val spec_name = thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@22104
    64
val spec_opt_name = opt_thm_name ":" -- P.prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@22104
    65
wenzelm@24013
    66
val xthm =
wenzelm@26361
    67
  P.$$$ "[" |-- attribs --| P.$$$ "]" >> pair (Facts.named "") ||
wenzelm@26361
    68
  (P.alt_string >> Facts.Fact ||
wenzelm@27816
    69
    P.position P.xname -- Scan.option Attrib.thm_sel >> Facts.Named) -- opt_attribs;
wenzelm@24013
    70
wenzelm@22104
    71
val xthms1 = Scan.repeat1 xthm;
wenzelm@22104
    72
wenzelm@22104
    73
val name_facts = P.and_list1 (opt_thm_name "=" -- xthms1);
wenzelm@22104
    74
wenzelm@22104
    75
wenzelm@22104
    76
(* locale and context elements *)
wenzelm@22104
    77
wenzelm@22104
    78
val locale_mixfix = P.$$$ "(" -- P.$$$ "structure" -- P.!!! (P.$$$ ")") >> K Structure || P.mixfix;
wenzelm@22104
    79
wenzelm@22104
    80
val locale_fixes =
wenzelm@28083
    81
  P.and_list1 (P.binding -- Scan.option (P.$$$ "::" |-- P.typ) -- locale_mixfix
wenzelm@22104
    82
    >> (single o P.triple1) ||
wenzelm@22104
    83
  P.params >> map Syntax.no_syn) >> flat;
wenzelm@22104
    84
wenzelm@22104
    85
val locale_insts =
ballarin@22658
    86
  Scan.optional (P.$$$ "[" |-- P.!!! (Scan.repeat1 (P.maybe P.term) --| P.$$$ "]")) []
ballarin@25094
    87
  -- Scan.optional (P.$$$ "where" |-- P.and_list1 (opt_thm_name ":" -- P.prop)) [];
wenzelm@22104
    88
wenzelm@22104
    89
local
wenzelm@22104
    90
wenzelm@22104
    91
val loc_element =
wenzelm@22104
    92
  P.$$$ "fixes" |-- P.!!! locale_fixes >> Element.Fixes ||
wenzelm@22104
    93
  P.$$$ "constrains" |-- P.!!! (P.and_list1 (P.name -- (P.$$$ "::" |-- P.typ)))
wenzelm@22104
    94
    >> Element.Constrains ||
wenzelm@22104
    95
  P.$$$ "assumes" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp))
wenzelm@22104
    96
    >> Element.Assumes ||
wenzelm@22104
    97
  P.$$$ "defines" |-- P.!!! (P.and_list1 (opt_thm_name ":" -- P.propp))
wenzelm@22104
    98
    >> Element.Defines ||
wenzelm@22104
    99
  P.$$$ "notes" |-- P.!!! (P.and_list1 (opt_thm_name "=" -- xthms1))
wenzelm@22104
   100
    >> (curry Element.Notes "");
wenzelm@22104
   101
wenzelm@24869
   102
fun plus1_unless test scan =
wenzelm@25999
   103
  scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
wenzelm@22104
   104
wenzelm@22104
   105
val rename = P.name -- Scan.option P.mixfix;
wenzelm@22104
   106
ballarin@29033
   107
val prefix = P.name --| P.$$$ ":";
ballarin@29033
   108
val named = P.name -- (P.$$$ "=" |-- P.term);
ballarin@29033
   109
val position = P.maybe P.term;
ballarin@29033
   110
val instance = P.$$$ "where" |-- P.and_list1 named >> Expression.Named ||
ballarin@29033
   111
  Scan.repeat1 position >> Expression.Positional;
ballarin@29033
   112
wenzelm@24869
   113
in
wenzelm@24869
   114
ballarin@28722
   115
val locale_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
ballarin@28722
   116
   P.$$$ "defines" || P.$$$ "notes";
ballarin@28722
   117
ballarin@28722
   118
val class_expr = plus1_unless locale_keyword P.xname;
wenzelm@24869
   119
wenzelm@24869
   120
val locale_expr =
wenzelm@22104
   121
  let
wenzelm@22104
   122
    fun expr2 x = (P.xname >> Locale.Locale || P.$$$ "(" |-- P.!!! (expr0 --| P.$$$ ")")) x
wenzelm@22104
   123
    and expr1 x = (expr2 -- Scan.repeat1 (P.maybe rename) >> Locale.Rename || expr2) x
ballarin@28722
   124
    and expr0 x = (plus1_unless locale_keyword expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
wenzelm@22104
   125
  in expr0 end;
wenzelm@22104
   126
ballarin@29033
   127
val locale_expression =
ballarin@29033
   128
  let
ballarin@29033
   129
    fun expr2 x = P.xname x;
ballarin@29033
   130
    fun expr1 x = (Scan.optional prefix "" -- expr2 --
ballarin@29033
   131
      Scan.optional instance (Expression.Named []) >> (fn ((p, l), i) => (l, (p, i)))) x;
ballarin@29033
   132
    fun expr0 x = (plus1_unless locale_keyword expr1) x;
ballarin@29033
   133
  in expr0 -- Scan.optional (P.$$$ "for" |-- P.!!! locale_fixes) [] end;
ballarin@29033
   134
wenzelm@22104
   135
val context_element = P.group "context element" loc_element;
wenzelm@22104
   136
wenzelm@22104
   137
end;
wenzelm@22104
   138
wenzelm@22104
   139
wenzelm@22104
   140
(* statements *)
wenzelm@22104
   141
wenzelm@22104
   142
val statement = P.and_list1 (opt_thm_name ":" -- Scan.repeat1 P.propp);
wenzelm@22104
   143
wenzelm@22104
   144
val obtain_case =
wenzelm@28083
   145
  P.parbinding -- (Scan.optional (P.simple_fixes --| P.$$$ "where") [] --
wenzelm@22104
   146
    (P.and_list1 (Scan.repeat1 P.prop) >> flat));
wenzelm@22104
   147
wenzelm@22104
   148
val general_statement =
wenzelm@22104
   149
  statement >> (fn x => ([], Element.Shows x)) ||
ballarin@28722
   150
  Scan.repeat context_element --
wenzelm@22104
   151
   (P.$$$ "obtains" |-- P.!!! (P.enum1 "|" obtain_case) >> Element.Obtains ||
wenzelm@22104
   152
    P.$$$ "shows" |-- P.!!! statement >> Element.Shows);
wenzelm@22104
   153
wenzelm@22104
   154
val statement_keyword = P.$$$ "obtains" || P.$$$ "shows";
wenzelm@22104
   155
wenzelm@22104
   156
wenzelm@22104
   157
(* specifications *)
wenzelm@22104
   158
wenzelm@28083
   159
val specification = P.enum1 "|" (P.parbinding -- (P.and_list1 spec -- P.for_simple_fixes));
wenzelm@22104
   160
wenzelm@22104
   161
end;