src/Pure/Thy/syntax.ML
author nipkow
Tue, 21 Dec 1993 16:27:36 +0100
changeset 201 9e41c6cec27c
parent 123 0a2f744e008a
child 257 b36874cf3b0b
permissions -rw-r--r--
Added []-field to extend_theory to accomodate type abbreviations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Thy/syntax.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
     3
    Author:     Sonia Mahjoub and Tobias Nipkow and Markus Wenzel, TU Muenchen
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
Definition of theory syntax together with translation to ML code.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
signature THYSYN =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
 sig
123
0a2f744e008a moved call of store_theory to end of use t.thy; use t.ML;
clasohm
parents: 81
diff changeset
    10
  datatype basetype = Thy  of string
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
    11
                    | File of string
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
    12
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
   val read: string list -> string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
 end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
functor ThySynFUN (Parse: PARSE): THYSYN =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
(*-------------- OBJECT TO STRING TRANSLATION ---------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
fun parent s = "(" ^ s ^ ")";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    26
fun pair(a, b) = parent(a ^ ", " ^ b);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    28
fun pair_quote(a, b) = pair(quote a, quote b);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    30
fun pair_quote2(a, b) = pair(a, quote b);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
fun bracket s = "[" ^ s ^ "]";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
val comma = space_implode ", ";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
val bracket_comma = bracket o comma;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
val big_bracket_comma = bracket o space_implode ",\n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
fun big_bracket_comma_ind ind strs = bracket (space_implode (",\n" ^ ind) strs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    42
val bracket_comma_quote = bracket_comma o (map quote);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
(*------------------- ABSTRACT SYNTAX FUNCTIONS ---------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
datatype mixfix = Mixfix of string * string * string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
                | Delimfix of string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
                | Infixl of string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
                | Infixr of string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
                | Binder of string * string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
                | TInfixl of string
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
                | TInfixr of string;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
datatype pfix_or_mfix = Pref of string | Mixf of string;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
fun pm_proj(Pref s) = s
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  | pm_proj(Mixf s) = s;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
fun split_decls l =
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    62
    let val (p, m) = partition (fn Pref _ => true | _ => false) l;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
    in (big_bracket_comma_ind "   " (map pm_proj p), map pm_proj m) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
fun delim_mix (s, None) = Delimfix(s)
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    66
  | delim_mix (s, Some(l, n)) = Mixfix(s, l, n);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    68
fun mixfix (sy, c, ty, l, n) =  "Mixfix(" ^ comma[quote sy, c, ty, l, n] ^ ")";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    70
fun infixrl(ty, c, n) = parent(comma[ty, c, n]);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    72
fun binder(sy, c, ty, n) = "Binder(" ^ comma[quote sy, c, ty, "0", n] ^ ")";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    74
fun delimfix (sy, c, ty) = "Delimfix(" ^ comma[quote sy, c, ty] ^ ")";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
fun tinfixrl (ty, n) = "(" ^ comma[ty, ty, n] ^ ")";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    78
fun mk_mfix((c, ty), mfix) =
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    79
      let val cs = quote c and tys = quote ty
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
      in case mfix of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
           Mixfix(sy, l, n) => mixfix (sy, tys, cs, l, n)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
         | Infixr(n) => "Infixr" ^ infixrl(cs, tys, n)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
         | Infixl(n) => "Infixl" ^ infixrl(cs, tys, n)
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    84
         | Binder(sy, n) => binder(sy, tys, cs, n)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
         | TInfixl(n) => "TInfixl" ^ tinfixrl(cs, n)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
         | TInfixr(n) => "TInfixr" ^ tinfixrl(cs, n)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
         | Delimfix(sy) => delimfix(sy, tys, cs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
      end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    91
fun mk_mixfix((cs, ty), None) =
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    92
      [Pref(pair(bracket_comma_quote cs, quote ty))]
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    93
  | mk_mixfix((c::cs, ty), Some(mfix)) =
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    94
      Mixf(mk_mfix((c, ty), mfix)) :: mk_mixfix((cs, ty), Some(mfix))
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    95
  | mk_mixfix(([], _), _) = [];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    97
fun mk_type_decl((ts, n), None) = [Pref(pair(bracket_comma_quote ts, n))]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
  | mk_type_decl((t::ts, n), Some(tinfix)) =
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
    99
      [Pref(pair(bracket(quote t), n)), Mixf(mk_mfix((t, n), tinfix))] @
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
      mk_type_decl((ts, n), Some(tinfix))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
  | mk_type_decl(([], n), Some(tinfix)) = [];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
fun mk_extension (((((((cl, def), (ty, tinfix)), ar), (co, mfix)), tr), ax), ml) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
  ((cl, def, ty, ar, co, ax), big_bracket_comma_ind "    " tinfix,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
    big_bracket_comma_ind "     " mfix, big_bracket_comma_ind "     " tr, ml);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   108
fun add_val((id, _), s) = "val " ^ id ^ " = get_axiom thy " ^ quote id ^ "\n" ^ s;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
fun mk_rules ps =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
  let
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   112
    val axs = big_bracket_comma_ind "  " (map pair_quote ps);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
    val vals = foldr add_val (ps, "")
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
  in
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   115
    axs ^ "\n\n" ^ vals
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
123
0a2f744e008a moved call of store_theory to end of use t.thy; use t.ML;
clasohm
parents: 81
diff changeset
   118
fun mk_struct (id, s) = "structure " ^ id ^ " =\nstruct\n" ^ s ^ "\nend;\n";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
fun mk_sext mfix trans =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
  "Some (NewSext {\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
\   mixfix =\n    " ^ mfix ^ ",\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
\   xrules =\n    " ^ trans ^ ",\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
\   parse_ast_translation = parse_ast_translation,\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
\   parse_translation = parse_translation,\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
\   print_translation = print_translation,\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
\   print_ast_translation = print_ast_translation})";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
fun mk_simple_sext mfix =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
  "Some (Syntax.simple_sext\n   " ^ mfix ^ ")";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
fun mk_ext ((cl, def, ty, ar, co, ax), sext) =
201
9e41c6cec27c Added []-field to extend_theory to accomodate type abbreviations.
nipkow
parents: 123
diff changeset
   134
  " (" ^ space_implode ",\n  " [cl, def, ty, "[]",ar, co, sext] ^ ")\n " ^ ax ^ "\n";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
fun mk_ext_thy (base, name, ext, sext) =
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   137
  "extend_theory (" ^ base ^ ")\n " ^ quote name ^ "\n" ^ mk_ext (ext, sext);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
val preamble =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
  "\nlocal\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
  \ val parse_ast_translation = []\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
  \ val parse_translation = []\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
  \ val print_translation = []\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
  \ val print_ast_translation = []\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
  \in\n\n\
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
  \(**** begin of user section ****)\n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
val postamble = "\n(**** end of user section ****)\n";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
fun mk_structure ((name, base), Some (ext, tinfix, mfix, trans, ml)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
      let
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
        val noext = ("[]", "[]", "[]", "[]", "[]", "[]");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
        val basethy =
74
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   154
          if tinfix = "[]" then base ^ (quote name)
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   155
          else mk_ext_thy (base ^ (quote name), name ^ "(type infix)", noext, mk_simple_sext tinfix);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
        val sext =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
          if mfix = "[]" andalso trans = "[]" andalso ml = "" then "None"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
          else mk_sext mfix trans;
74
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   159
        val thy = "val thy = " ^ mk_ext_thy (basethy, name, ext, sext);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
      in
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
        mk_struct (name, preamble ^ ml ^ postamble ^ thy ^ "\nend")
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
      end
74
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   163
  | mk_structure ((name, base), None) =
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   164
      mk_struct (name, "\nval thy = " ^ base ^ (quote name));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
123
0a2f744e008a moved call of store_theory to end of use t.thy; use t.ML;
clasohm
parents: 81
diff changeset
   166
datatype basetype = Thy  of string
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   167
                  | File of string;
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   168
74
208ab8773a73 changes in Readthy:
clasohm
parents: 20
diff changeset
   169
fun merge thys =
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   170
  let fun make_list (Thy t :: ts) =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   171
            ("Thy \"" ^ t ^ "\"") :: make_list ts
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   172
        | make_list (File t :: ts) =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   173
            ("File \"" ^ t ^ "\"") :: make_list ts
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   174
        | make_list [] = []
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   175
  in "base_on " ^ (bracket (space_implode "," (make_list thys))) ^ " " end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
(*------------------------ PARSERS -------------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
open Parse
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
(*------------------- VARIOUS PARSERS ----------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   186
val emptyl = empty >> K "[]";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   188
val ids  =  list_of1 id >> bracket_comma_quote;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
(* -> "[id1, id2, ..., idn]" *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
val stgorids =  list_of1 (stg || id);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   193
val sort =    id >> (bracket o quote)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
           || "{" $$-- (ids || emptyl) --$$ "}";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
(* -> "[id]"
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   196
   -> "[id1, ..., idn]"  *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
val infxl = "infixl" $$-- !! nat
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
and infxr = "infixr" $$-- !! nat
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
(*------------------- CLASSES PARSER ----------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   207
val class  =  (id >> quote) -- ( "<" $$-- (!! ids)  ||  emptyl)   >> pair;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
(* -> "(id, [id1, ..., idn])"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
   ||
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
   -> "(id, [])"  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
val classes =  "classes" $$-- !!(repeat1 class) >> bracket_comma
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
            || emptyl;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   218
(* "[(id, [..]), ..., (id, [...])]" *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
(*------------------- DEFAULT PARSER ---------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
val default =  "default" $$-- !!sort
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
           ||  emptyl;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
(* -> "[]"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
   -> "[id]"
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   230
   -> "[id1, ..., idn]"  *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
(*-------------------- TYPES  PARSER  ----------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
val type_decl =  stgorids -- nat;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
val tyinfix =  infxl  >> (Some o TInfixl)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
            || infxr  >> (Some o TInfixr);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
val type_infix =   "(" $$-- !! (tyinfix --$$ ")")
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
               || empty                           >> K None;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
val types =  "types" $$--
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
                !! (repeat1 (type_decl -- type_infix >> mk_type_decl))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
                >> (split_decls o flat)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
          || empty >> (K ("[]", []));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  (* ==> ("[(id, nat), ... ]", [strg, ...]) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
(*-------------------- ARITIES PARSER ----------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
val sorts =  list_of sort >> bracket_comma;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
(* -> "[[id1, ...], ..., [id, ...]]" *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   261
val arity =  id                           >> (fn s => pair("[]", quote s))
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   262
          || "(" $$-- sorts --$$")" -- id >> (fn (l, s) => pair(l, quote s));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   264
(* -> "([], id)"
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   265
   -> "([[id, ..], ..., [id, ..]], id)" *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   267
val tys = stgorids >> bracket_comma_quote;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
val arities =  "arities" $$-- !! (repeat1 (tys --$$ "::" -- arity >> pair))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
               >> bracket_comma
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
            || emptyl;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   273
(* -> "[([id, ..], ([[id, ...], ...], id))]" *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
(*--------------------- CONSTS PARSER ---------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
val natlist = "[" $$--  !!(list_of nat --$$ "]") >> bracket_comma
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   279
            || empty                             >> K "[]";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
  (* "[nat, ...]"  || "[]" *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
val prio_opt =  natlist -- nat  >> Some
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
             || empty           >> K None;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
val mfix =  stg -- !! prio_opt            >> delim_mix
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
         || infxl                         >> Infixl
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
         || infxr                         >> Infixr
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
         || "binder" $$-- !!(stg -- nat)  >> Binder
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
val const_decl = stgorids -- !! ("::" $$-- stg);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   295
(*("[exid, ...]", stg)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
val mixfix =  "(" $$-- !! (mfix --$$ ")")  >> Some
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
           || empty                        >> K None;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
(* (s, e, l, n) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
val consts = "consts" $$--
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
                 !! (repeat1 (const_decl -- mixfix >> mk_mixfix))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
                 >> (split_decls o flat)
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   307
           || empty >> K ("[]", []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
(* ("[([exid, ...], stg), ....]", [strg, ..])  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
(*---------------- TRANSLATIONS PARSER --------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   314
val xpat = "(" $$-- id --$$ ")" -- stg >> pair_quote
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   315
         || stg >> (fn s => pair_quote ("logic", s));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
val arrow = $$ "=>" >> K " |-> "
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
         || $$ "<=" >> K " <-| "
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
         || $$ "==" >> K " <-> ";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   320
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
val xrule = xpat -- !! (arrow -- xpat) >> (fn (xp1, (a, xp2)) => xp1 ^ a ^ xp2);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
val translations = "translations" $$-- !! (repeat1 xrule)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
                 || empty;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
(*------------------- RULES PARSER -----------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
val rules  = "rules" $$-- !! (repeat1 (id -- !! stg) >> mk_rules)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
           || emptyl;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
(* "[(id, stg), ...]" *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   334
(*----------------------- BASE PARSER -------------------------*)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   335
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   336
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   337
fun base toks =
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   338
  let fun make_thy (b, toks) = (Thy b, toks);
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   339
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   340
      fun make_file (b, toks) = (File b, toks);
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   341
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   342
      val (b, toks) = make_thy (id toks)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   343
                      handle _ => make_file (stg toks)
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   344
  in (b, toks) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   345
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   346
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   347
(*----------------------- ML_TEXT -------------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   348
20
e6fb60365db9 Pure/Thy/syntax.ML
wenzelm
parents: 0
diff changeset
   349
val mltxt =  txt || empty >> K "";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   350
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   352
(*---------------------------------------------------------*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   353
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
val extension = "+" $$-- !! (classes -- default -- types -- arities
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   355
                             -- consts -- translations -- rules --$$ "end" -- mltxt)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   356
                       >> (Some o mk_extension)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   357
              || empty >> K None;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   358
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   359
81
4cc5a34292a9 corrected some spelling mistakes;
clasohm
parents: 74
diff changeset
   360
val bases = base -- repeat("+" $$-- base) >> op:: ;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   361
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   362
val theoryDef = !!(id --$$ "=" -- (bases >> merge) -- extension)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   363
                >> mk_structure;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   365
val read = reader theoryDef
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   366
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   367
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   368