src/Pure/Syntax/mixfix.ML
author wenzelm
Wed May 17 01:23:46 2006 +0200 (2006-05-17)
changeset 19673 853f5a3cc06e
parent 19482 9f11af8f7ef9
child 20786 96077403f619
permissions -rw-r--r--
always preserve authentic consts -- removed Syntax.mixfix_const;
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@19271
    29
  val pretty_mixfix: mixfix -> Pretty.T
wenzelm@384
    30
  val type_name: string -> mixfix -> string
wenzelm@384
    31
  val const_name: string -> mixfix -> string
wenzelm@19373
    32
  val const_mixfix: string -> mixfix -> string * mixfix
wenzelm@18565
    33
  val unlocalize_mixfix: mixfix -> mixfix
wenzelm@4823
    34
  val mixfix_args: mixfix -> int
wenzelm@19020
    35
  val mixfix_content: mixfix -> string list list
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@19271
    69
(* pretty_mixfix *)
wenzelm@19271
    70
wenzelm@19271
    71
local
wenzelm@11920
    72
wenzelm@19271
    73
val quoted = Pretty.quote o Pretty.str;
wenzelm@19271
    74
val keyword = Pretty.keyword;
wenzelm@19271
    75
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    76
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    77
val int = Pretty.str o string_of_int;
wenzelm@19271
    78
wenzelm@19271
    79
in
wenzelm@11920
    80
wenzelm@19271
    81
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@19271
    82
  | pretty_mixfix (Mixfix (s, ps, p)) =
wenzelm@19271
    83
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@19271
    84
  | pretty_mixfix (Delimfix s) = parens [quoted s]
wenzelm@19271
    85
  | pretty_mixfix (InfixName (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@19271
    86
  | pretty_mixfix (InfixlName (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    87
  | pretty_mixfix (InfixrName (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    88
  | pretty_mixfix (Infix p) = parens (Pretty.breaks [keyword "infix", int p])
wenzelm@19271
    89
  | pretty_mixfix (Infixl p) = parens (Pretty.breaks [keyword "infixl", int p])
wenzelm@19271
    90
  | pretty_mixfix (Infixr p) = parens (Pretty.breaks [keyword "infixr", int p])
wenzelm@19271
    91
  | pretty_mixfix (Binder (s, p1, p2)) =
wenzelm@19271
    92
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@19271
    93
  | pretty_mixfix Structure = parens [keyword "structure"];
wenzelm@19271
    94
wenzelm@19271
    95
end;
wenzelm@11920
    96
wenzelm@11920
    97
wenzelm@12531
    98
(* syntax specifications *)
wenzelm@384
    99
wenzelm@384
   100
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
   101
  | strip ["'"] = []
wenzelm@384
   102
  | strip (c :: cs) = c :: strip cs
wenzelm@384
   103
  | strip [] = [];
wenzelm@384
   104
wenzelm@4697
   105
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
   106
wenzelm@17284
   107
fun deprecated c = (warning ("Unnamed infix operator " ^ quote c ^ " -- deprecated"); c);
wenzelm@17284
   108
wenzelm@11651
   109
fun type_name t (InfixName _) = t
wenzelm@11651
   110
  | type_name t (InfixlName _) = t
wenzelm@2199
   111
  | type_name t (InfixrName _) = t
wenzelm@17284
   112
  | type_name t (Infix _) = deprecated (strip_esc t)
wenzelm@17284
   113
  | type_name t (Infixl _) = deprecated (strip_esc t)
wenzelm@17284
   114
  | type_name t (Infixr _) = deprecated (strip_esc t)
wenzelm@384
   115
  | type_name t _ = t;
wenzelm@384
   116
wenzelm@11651
   117
fun const_name c (InfixName _) = c
wenzelm@11651
   118
  | const_name c (InfixlName _) = c
wenzelm@2199
   119
  | const_name c (InfixrName _) = c
wenzelm@17284
   120
  | const_name c (Infix _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   121
  | const_name c (Infixl _) = "op " ^ deprecated (strip_esc c)
wenzelm@17284
   122
  | const_name c (Infixr _) = "op " ^ deprecated (strip_esc c)
wenzelm@384
   123
  | const_name c _ = c;
wenzelm@384
   124
wenzelm@19373
   125
fun fix_mixfix c (Infix p) = InfixName (c, p)
wenzelm@19373
   126
  | fix_mixfix c (Infixl p) = InfixlName (c, p)
wenzelm@19373
   127
  | fix_mixfix c (Infixr p) = InfixrName (c, p)
wenzelm@5056
   128
  | fix_mixfix _ mx = mx;
wenzelm@5056
   129
wenzelm@19373
   130
fun const_mixfix c mx = (const_name c mx, fix_mixfix c mx);
wenzelm@19373
   131
wenzelm@18565
   132
fun map_mixfix _ NoSyn = NoSyn
wenzelm@18565
   133
  | map_mixfix f (Mixfix (sy, ps, p)) = Mixfix (f sy, ps, p)
wenzelm@18565
   134
  | map_mixfix f (Delimfix sy) = Delimfix (f sy)
wenzelm@18565
   135
  | map_mixfix f (InfixName (sy, p)) = InfixName (f sy, p)
wenzelm@18565
   136
  | map_mixfix f (InfixlName (sy, p)) = InfixlName (f sy, p)
wenzelm@18565
   137
  | map_mixfix f (InfixrName (sy, p)) = InfixrName (f sy, p)
wenzelm@18565
   138
  | map_mixfix f (Binder (sy, p, q)) = Binder (f sy, p, q)
wenzelm@18673
   139
  | map_mixfix _ Structure = Structure
wenzelm@18565
   140
  | map_mixfix _ _ = raise Fail ("map_mixfix: illegal occurrence of unnamed infix");
wenzelm@18565
   141
wenzelm@19467
   142
fun unlocalize_mixfix mx =
wenzelm@19467
   143
  let val mx' = map_mixfix SynExt.unlocalize_mfix mx
wenzelm@19467
   144
  in if mx = mx' then NoSyn else mx' end;
wenzelm@18565
   145
wenzelm@4053
   146
fun mixfix_args NoSyn = 0
wenzelm@5690
   147
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   148
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@12531
   149
  | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   150
  | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   151
  | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@18341
   152
  | mixfix_args (Infix _) = 2
wenzelm@18341
   153
  | mixfix_args (Infixl _) = 2
wenzelm@18341
   154
  | mixfix_args (Infixr _) = 2
wenzelm@18673
   155
  | mixfix_args (Binder _) = 1
wenzelm@18673
   156
  | mixfix_args Structure = 0;
wenzelm@4053
   157
wenzelm@4053
   158
wenzelm@384
   159
(* syn_ext_types *)
wenzelm@384
   160
wenzelm@14903
   161
fun syn_ext_types type_decls =
wenzelm@384
   162
  let
wenzelm@384
   163
    fun name_of (t, _, mx) = type_name t mx;
wenzelm@384
   164
wenzelm@2199
   165
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   166
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   167
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   168
wenzelm@2199
   169
    fun mfix_of decl =
wenzelm@2199
   170
      let val t = name_of decl in
wenzelm@2199
   171
        (case decl of
skalberg@15531
   172
          (_, _, NoSyn) => NONE
skalberg@15531
   173
        | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   174
        | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   175
        | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
skalberg@15531
   176
        | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   177
        | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   178
        | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
wenzelm@14903
   179
        | _ => error ("Bad mixfix declaration for type: " ^ quote t))
wenzelm@2199
   180
      end;
wenzelm@384
   181
wenzelm@19482
   182
    val mfix = map_filter mfix_of type_decls;
wenzelm@384
   183
    val xconsts = map name_of type_decls;
wenzelm@14903
   184
  in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   185
wenzelm@384
   186
wenzelm@384
   187
(* syn_ext_consts *)
wenzelm@384
   188
wenzelm@15751
   189
val binder_stamp = stamp ();
wenzelm@15751
   190
wenzelm@14903
   191
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   192
  let
wenzelm@384
   193
    fun name_of (c, _, mx) = const_name c mx;
wenzelm@384
   194
wenzelm@384
   195
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   196
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   197
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   198
wenzelm@384
   199
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   200
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   201
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   202
wenzelm@2199
   203
    fun mfix_of decl =
wenzelm@2199
   204
      let val c = name_of decl in
wenzelm@2199
   205
        (case decl of
wenzelm@2199
   206
          (_, _, NoSyn) => []
wenzelm@5690
   207
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   208
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   209
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   210
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   211
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   212
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   213
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   214
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   215
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@18673
   216
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)]
wenzelm@18673
   217
        | _ => error ("Bad mixfix declaration for const: " ^ quote c))
wenzelm@2199
   218
    end;
wenzelm@384
   219
skalberg@15531
   220
    fun binder (c, _, Binder (sy, _, _)) = SOME (sy, c)
skalberg@15531
   221
      | binder _ = NONE;
wenzelm@384
   222
wenzelm@19482
   223
    val mfix = maps mfix_of const_decls;
wenzelm@384
   224
    val xconsts = map name_of const_decls;
wenzelm@19482
   225
    val binders = map_filter binder const_decls;
wenzelm@16610
   226
    val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   227
        apsnd K o SynTrans.mk_binder_tr);
wenzelm@15751
   228
    val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   229
        apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
wenzelm@14903
   230
  in
wenzelm@14903
   231
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   232
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   233
  end;
wenzelm@384
   234
wenzelm@19020
   235
fun mixfix_content mx =
wenzelm@19020
   236
  let
wenzelm@19020
   237
    fun delims sy = SynExt.mfix_delims sy;
wenzelm@19020
   238
    fun op_delims sy = let val ds = delims sy in ["op" :: ds, ds] end;
wenzelm@19020
   239
  in
wenzelm@19020
   240
    case mx of
wenzelm@19020
   241
      Mixfix (sy, _, _) => [delims sy]
wenzelm@19020
   242
    | Delimfix sy => [delims sy]
wenzelm@19020
   243
    | InfixName (sy, _) => op_delims sy
wenzelm@19020
   244
    | InfixlName (sy, _) => op_delims sy
wenzelm@19020
   245
    | InfixrName (sy, _) => op_delims sy
wenzelm@19020
   246
    | Binder (sy, _, _) => [delims sy @ ["."]]
wenzelm@19020
   247
    | _ => []
wenzelm@19020
   248
  end;
wenzelm@19020
   249
wenzelm@384
   250
end;