src/Pure/Syntax/mixfix.ML
author wenzelm
Thu Jan 19 21:22:24 2006 +0100 (2006-01-19)
changeset 18719 dca3ae4f6dd6
parent 18673 fad60fe1565c
child 19003 64ad6c520464
permissions -rw-r--r--
moved pure syntax to Syntax/syntax.ML and pure_thy.ML;
wenzelm@384
     1
(*  Title:      Pure/Syntax/mixfix.ML
wenzelm@384
     2
    ID:         $Id$
wenzelm@551
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@384
     4
wenzelm@18719
     5
Mixfix declarations, infixes, binders.
wenzelm@384
     6
*)
wenzelm@384
     7
wenzelm@384
     8
signature MIXFIX0 =
wenzelm@2199
     9
sig
wenzelm@384
    10
  datatype mixfix =
wenzelm@384
    11
    NoSyn |
wenzelm@384
    12
    Mixfix of string * int list * int |
wenzelm@384
    13
    Delimfix of string |
wenzelm@11651
    14
    InfixName of string * int |
wenzelm@2199
    15
    InfixlName of string * int |
wenzelm@2199
    16
    InfixrName of string * int |
wenzelm@18565
    17
    Infix of int |    (*obsolete*)
wenzelm@18565
    18
    Infixl of int |   (*obsolete*)
wenzelm@18565
    19
    Infixr of int |   (*obsolete*)
wenzelm@18673
    20
    Binder of string * int * int |
wenzelm@18673
    21
    Structure
wenzelm@2199
    22
end;
wenzelm@384
    23
wenzelm@384
    24
signature MIXFIX1 =
wenzelm@2199
    25
sig
wenzelm@384
    26
  include MIXFIX0
wenzelm@12864
    27
  val literal: string -> mixfix
wenzelm@4823
    28
  val no_syn: 'a * 'b -> 'a * 'b * mixfix
wenzelm@11920
    29
  val string_of_mixfix: mixfix -> string
wenzelm@384
    30
  val type_name: string -> mixfix -> string
wenzelm@384
    31
  val const_name: string -> mixfix -> string
wenzelm@5056
    32
  val fix_mixfix: string -> mixfix -> mixfix
wenzelm@18565
    33
  val unlocalize_mixfix: mixfix -> mixfix
wenzelm@4823
    34
  val mixfix_args: mixfix -> int
wenzelm@2199
    35
end;
wenzelm@384
    36
wenzelm@384
    37
signature MIXFIX =
wenzelm@2199
    38
sig
wenzelm@384
    39
  include MIXFIX1
wenzelm@14903
    40
  val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext
wenzelm@14903
    41
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> SynExt.syn_ext
wenzelm@2199
    42
end;
wenzelm@2199
    43
wenzelm@15751
    44
structure Mixfix: MIXFIX =
wenzelm@384
    45
struct
wenzelm@384
    46
wenzelm@2199
    47
wenzelm@384
    48
(** mixfix declarations **)
wenzelm@384
    49
wenzelm@384
    50
datatype mixfix =
wenzelm@384
    51
  NoSyn |
wenzelm@384
    52
  Mixfix of string * int list * int |
wenzelm@384
    53
  Delimfix of string |
wenzelm@11651
    54
  InfixName of string * int |
wenzelm@2199
    55
  InfixlName of string * int |
wenzelm@2199
    56
  InfixrName of string * int |
wenzelm@18565
    57
  Infix of int |      (*obsolete*)
wenzelm@18565
    58
  Infixl of int |     (*obsolete*)
wenzelm@18565
    59
  Infixr of int |     (*obsolete*)
wenzelm@18673
    60
  Binder of string * int * int |
wenzelm@18673
    61
  Structure;
wenzelm@384
    62
wenzelm@12864
    63
val literal = Delimfix o SynExt.escape_mfix;
wenzelm@12864
    64
wenzelm@4823
    65
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    66
wenzelm@384
    67
wenzelm@11920
    68
(* string_of_mixfix *)
wenzelm@11920
    69
wenzelm@11920
    70
val parens = enclose "(" ")";
wenzelm@11920
    71
val brackets = enclose "[" "]";
wenzelm@11920
    72
val spaces = space_implode " ";
wenzelm@11920
    73
wenzelm@11920
    74
fun string_of_mixfix NoSyn = ""
wenzelm@11920
    75
  | string_of_mixfix (Mixfix (sy, ps, p)) =
wenzelm@11920
    76
      parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
wenzelm@11920
    77
  | string_of_mixfix (Delimfix sy) = parens (quote sy)
wenzelm@11920
    78
  | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
wenzelm@11920
    79
  | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    80
  | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
wenzelm@11920
    81
  | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
wenzelm@11920
    82
  | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
wenzelm@11978
    83
  | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
wenzelm@11978
    84
  | string_of_mixfix (Binder (sy, p1, p2)) =
wenzelm@18673
    85
      parens (spaces ["binder", quote sy, brackets (string_of_int p1), string_of_int p2])
wenzelm@18673
    86
  | string_of_mixfix Structure = "(structure)";
wenzelm@11920
    87
wenzelm@11920
    88
wenzelm@12531
    89
(* syntax specifications *)
wenzelm@384
    90
wenzelm@384
    91
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
    92
  | strip ["'"] = []
wenzelm@384
    93
  | strip (c :: cs) = c :: strip cs
wenzelm@384
    94
  | strip [] = [];
wenzelm@384
    95
wenzelm@4697
    96
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
    97
wenzelm@17284
    98
fun deprecated c = (warning ("Unnamed infix operator " ^ quote c ^ " -- deprecated"); c);
wenzelm@17284
    99
wenzelm@11651
   100
fun type_name t (InfixName _) = t
wenzelm@11651
   101
  | type_name t (InfixlName _) = t
wenzelm@2199
   102
  | type_name t (InfixrName _) = t
wenzelm@17284
   103
  | type_name t (Infix _) = deprecated (strip_esc t)
wenzelm@17284
   104
  | type_name t (Infixl _) = deprecated (strip_esc t)
wenzelm@17284
   105
  | type_name t (Infixr _) = deprecated (strip_esc t)
wenzelm@384
   106
  | type_name t _ = t;
wenzelm@384
   107
wenzelm@11651
   108
fun const_name c (InfixName _) = c
wenzelm@11651
   109
  | const_name c (InfixlName _) = c
wenzelm@2199
   110
  | const_name c (InfixrName _) = c
wenzelm@17284
   111
  | const_name c (Infix _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   112
  | const_name c (Infixl _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   113
  | const_name c (Infixr _) = "op " ^ deprecated (strip_esc c)
wenzelm@384
   114
  | const_name c _ = c;
wenzelm@384
   115
wenzelm@17284
   116
fun fix_mixfix c (Infix p) = (deprecated (strip_esc c); InfixName (c, p))
wenzelm@17284
   117
  | fix_mixfix c (Infixl p) = (deprecated (strip_esc c); InfixlName (c, p))
wenzelm@17284
   118
  | fix_mixfix c (Infixr p) = (deprecated (strip_esc c); InfixrName (c, p))
wenzelm@5056
   119
  | fix_mixfix _ mx = mx;
wenzelm@5056
   120
wenzelm@18565
   121
fun map_mixfix _ NoSyn = NoSyn
wenzelm@18565
   122
  | map_mixfix f (Mixfix (sy, ps, p)) = Mixfix (f sy, ps, p)
wenzelm@18565
   123
  | map_mixfix f (Delimfix sy) = Delimfix (f sy)
wenzelm@18565
   124
  | map_mixfix f (InfixName (sy, p)) = InfixName (f sy, p)
wenzelm@18565
   125
  | map_mixfix f (InfixlName (sy, p)) = InfixlName (f sy, p)
wenzelm@18565
   126
  | map_mixfix f (InfixrName (sy, p)) = InfixrName (f sy, p)
wenzelm@18565
   127
  | map_mixfix f (Binder (sy, p, q)) = Binder (f sy, p, q)
wenzelm@18673
   128
  | map_mixfix _ Structure = Structure
wenzelm@18565
   129
  | map_mixfix _ _ = raise Fail ("map_mixfix: illegal occurrence of unnamed infix");
wenzelm@18565
   130
wenzelm@18565
   131
val unlocalize_mixfix = map_mixfix SynExt.unlocalize_mfix;
wenzelm@18565
   132
wenzelm@4053
   133
fun mixfix_args NoSyn = 0
wenzelm@5690
   134
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   135
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@12531
   136
  | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   137
  | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   138
  | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@18341
   139
  | mixfix_args (Infix _) = 2
wenzelm@18341
   140
  | mixfix_args (Infixl _) = 2
wenzelm@18341
   141
  | mixfix_args (Infixr _) = 2
wenzelm@18673
   142
  | mixfix_args (Binder _) = 1
wenzelm@18673
   143
  | mixfix_args Structure = 0;
wenzelm@4053
   144
wenzelm@4053
   145
wenzelm@384
   146
(* syn_ext_types *)
wenzelm@384
   147
wenzelm@14903
   148
fun syn_ext_types type_decls =
wenzelm@384
   149
  let
wenzelm@384
   150
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   151
wenzelm@2199
   152
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   153
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   154
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   155
wenzelm@2199
   156
    fun mfix_of decl =
wenzelm@2199
   157
      let val t = name_of decl in
wenzelm@2199
   158
        (case decl of
skalberg@15531
   159
          (_, _, NoSyn) => NONE
skalberg@15531
   160
        | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   161
        | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   162
        | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
skalberg@15531
   163
        | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   164
        | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   165
        | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
wenzelm@14903
   166
        | _ => error ("Bad mixfix declaration for type: " ^ quote t))
wenzelm@2199
   167
      end;
wenzelm@384
   168
skalberg@15570
   169
    val mfix = List.mapPartial mfix_of type_decls;
wenzelm@384
   170
    val xconsts = map name_of type_decls;
wenzelm@14903
   171
  in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   172
wenzelm@384
   173
wenzelm@384
   174
(* syn_ext_consts *)
wenzelm@384
   175
wenzelm@15751
   176
val binder_stamp = stamp ();
wenzelm@15751
   177
wenzelm@14903
   178
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   179
  let
wenzelm@384
   180
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   181
wenzelm@384
   182
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   183
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   184
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   185
wenzelm@384
   186
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   187
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   188
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   189
wenzelm@2199
   190
    fun mfix_of decl =
wenzelm@2199
   191
      let val c = name_of decl in
wenzelm@2199
   192
        (case decl of
wenzelm@2199
   193
          (_, _, NoSyn) => []
wenzelm@5690
   194
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   195
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   196
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   197
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   198
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   199
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   200
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   201
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   202
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@18673
   203
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)]
wenzelm@18673
   204
        | _ => error ("Bad mixfix declaration for const: " ^ quote c))
wenzelm@2199
   205
    end;
wenzelm@384
   206
skalberg@15531
   207
    fun binder (c, _, Binder (sy, _, _)) = SOME (sy, c)
skalberg@15531
   208
      | binder _ = NONE;
wenzelm@384
   209
skalberg@15570
   210
    val mfix = List.concat (map mfix_of const_decls);
wenzelm@384
   211
    val xconsts = map name_of const_decls;
skalberg@15570
   212
    val binders = List.mapPartial binder const_decls;
wenzelm@16610
   213
    val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   214
        apsnd K o SynTrans.mk_binder_tr);
wenzelm@15751
   215
    val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   216
        apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
wenzelm@14903
   217
  in
wenzelm@14903
   218
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   219
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   220
  end;
wenzelm@384
   221
wenzelm@384
   222
end;