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