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