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