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