src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Sep 30 22:25:50 2009 +0200 (2009-09-30)
changeset 32786 f1ac4b515af9
parent 30340 60b2c50420d2
child 35129 ed24ba6f69aa
permissions -rw-r--r--
eliminated dead code;
wenzelm@384
     1
(*  Title:      Pure/Syntax/mixfix.ML
wenzelm@551
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@384
     3
wenzelm@18719
     4
Mixfix declarations, infixes, binders.
wenzelm@384
     5
*)
wenzelm@384
     6
wenzelm@384
     7
signature MIXFIX0 =
wenzelm@2199
     8
sig
wenzelm@384
     9
  datatype mixfix =
wenzelm@384
    10
    NoSyn |
wenzelm@384
    11
    Mixfix of string * int list * int |
wenzelm@384
    12
    Delimfix of string |
wenzelm@11651
    13
    InfixName of string * int |
wenzelm@2199
    14
    InfixlName of string * int |
wenzelm@2199
    15
    InfixrName of string * int |
wenzelm@18565
    16
    Infix of int |    (*obsolete*)
wenzelm@18565
    17
    Infixl of int |   (*obsolete*)
wenzelm@18565
    18
    Infixr of int |   (*obsolete*)
wenzelm@18673
    19
    Binder of string * int * int |
wenzelm@18673
    20
    Structure
wenzelm@21534
    21
  val binder_name: string -> string
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@30340
    30
  val type_name: mixfix -> string -> string
wenzelm@30340
    31
  val const_name: mixfix -> string -> string
wenzelm@19373
    32
  val const_mixfix: string -> mixfix -> string * mixfix
wenzelm@4823
    33
  val mixfix_args: mixfix -> int
wenzelm@22702
    34
  val mixfixT: mixfix -> typ
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@19271
    68
(* pretty_mixfix *)
wenzelm@19271
    69
wenzelm@19271
    70
local
wenzelm@11920
    71
wenzelm@19271
    72
val quoted = Pretty.quote o Pretty.str;
wenzelm@19271
    73
val keyword = Pretty.keyword;
wenzelm@19271
    74
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    75
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    76
val int = Pretty.str o string_of_int;
wenzelm@19271
    77
wenzelm@19271
    78
in
wenzelm@11920
    79
wenzelm@19271
    80
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@19271
    81
  | pretty_mixfix (Mixfix (s, ps, p)) =
wenzelm@19271
    82
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@19271
    83
  | pretty_mixfix (Delimfix s) = parens [quoted s]
wenzelm@19271
    84
  | pretty_mixfix (InfixName (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@19271
    85
  | pretty_mixfix (InfixlName (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    86
  | pretty_mixfix (InfixrName (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    87
  | pretty_mixfix (Infix p) = parens (Pretty.breaks [keyword "infix", int p])
wenzelm@19271
    88
  | pretty_mixfix (Infixl p) = parens (Pretty.breaks [keyword "infixl", int p])
wenzelm@19271
    89
  | pretty_mixfix (Infixr p) = parens (Pretty.breaks [keyword "infixr", int p])
wenzelm@19271
    90
  | pretty_mixfix (Binder (s, p1, p2)) =
wenzelm@19271
    91
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@19271
    92
  | pretty_mixfix Structure = parens [keyword "structure"];
wenzelm@19271
    93
wenzelm@19271
    94
end;
wenzelm@11920
    95
wenzelm@11920
    96
wenzelm@12531
    97
(* syntax specifications *)
wenzelm@384
    98
wenzelm@384
    99
fun strip ("'" :: c :: cs) = c :: strip cs
wenzelm@384
   100
  | strip ["'"] = []
wenzelm@384
   101
  | strip (c :: cs) = c :: strip cs
wenzelm@384
   102
  | strip [] = [];
wenzelm@384
   103
wenzelm@4697
   104
val strip_esc = implode o strip o Symbol.explode;
wenzelm@384
   105
wenzelm@26291
   106
fun deprecated c = (legacy_feature ("Unnamed infix operator " ^ quote c); c);
wenzelm@17284
   107
wenzelm@30340
   108
fun type_name (InfixName _) = I
wenzelm@30340
   109
  | type_name (InfixlName _) = I
wenzelm@30340
   110
  | type_name (InfixrName _) = I
wenzelm@30340
   111
  | type_name (Infix _) = deprecated o strip_esc
wenzelm@30340
   112
  | type_name (Infixl _) = deprecated o strip_esc
wenzelm@30340
   113
  | type_name (Infixr _) = deprecated o strip_esc
wenzelm@30340
   114
  | type_name _ = I;
wenzelm@384
   115
wenzelm@30340
   116
fun const_name (InfixName _) = I
wenzelm@30340
   117
  | const_name (InfixlName _) = I
wenzelm@30340
   118
  | const_name (InfixrName _) = I
wenzelm@30340
   119
  | const_name (Infix _) = prefix "op " o deprecated o strip_esc
wenzelm@30340
   120
  | const_name (Infixl _) = prefix "op " o deprecated o strip_esc
wenzelm@30340
   121
  | const_name (Infixr _) = prefix "op " o deprecated o strip_esc
wenzelm@30340
   122
  | const_name _ = I;
wenzelm@384
   123
wenzelm@19373
   124
fun fix_mixfix c (Infix p) = InfixName (c, p)
wenzelm@19373
   125
  | fix_mixfix c (Infixl p) = InfixlName (c, p)
wenzelm@19373
   126
  | fix_mixfix c (Infixr p) = InfixrName (c, p)
wenzelm@5056
   127
  | fix_mixfix _ mx = mx;
wenzelm@5056
   128
wenzelm@30340
   129
fun const_mixfix c mx = (const_name mx c, fix_mixfix c mx);
wenzelm@19373
   130
wenzelm@4053
   131
fun mixfix_args NoSyn = 0
wenzelm@5690
   132
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
   133
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@12531
   134
  | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   135
  | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@12531
   136
  | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@18341
   137
  | mixfix_args (Infix _) = 2
wenzelm@18341
   138
  | mixfix_args (Infixl _) = 2
wenzelm@18341
   139
  | mixfix_args (Infixr _) = 2
wenzelm@18673
   140
  | mixfix_args (Binder _) = 1
wenzelm@18673
   141
  | mixfix_args Structure = 0;
wenzelm@4053
   142
wenzelm@22709
   143
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
wenzelm@22709
   144
  | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
wenzelm@22702
   145
wenzelm@4053
   146
wenzelm@384
   147
(* syn_ext_types *)
wenzelm@384
   148
wenzelm@14903
   149
fun syn_ext_types type_decls =
wenzelm@384
   150
  let
wenzelm@30340
   151
    fun name_of (t, _, mx) = type_name mx t;
wenzelm@384
   152
wenzelm@2199
   153
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   154
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   155
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   156
wenzelm@2199
   157
    fun mfix_of decl =
wenzelm@2199
   158
      let val t = name_of decl in
wenzelm@2199
   159
        (case decl of
skalberg@15531
   160
          (_, _, NoSyn) => NONE
skalberg@15531
   161
        | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   162
        | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   163
        | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
skalberg@15531
   164
        | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
skalberg@15531
   165
        | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
skalberg@15531
   166
        | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
wenzelm@14903
   167
        | _ => error ("Bad mixfix declaration for type: " ^ quote t))
wenzelm@2199
   168
      end;
wenzelm@384
   169
wenzelm@19482
   170
    val mfix = map_filter mfix_of type_decls;
wenzelm@384
   171
    val xconsts = map name_of type_decls;
wenzelm@14903
   172
  in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   173
wenzelm@384
   174
wenzelm@384
   175
(* syn_ext_consts *)
wenzelm@384
   176
wenzelm@15751
   177
val binder_stamp = stamp ();
wenzelm@21534
   178
val binder_name = suffix "_binder";
wenzelm@15751
   179
wenzelm@14903
   180
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   181
  let
wenzelm@30340
   182
    fun name_of (c, _, mx) = const_name mx c;
wenzelm@384
   183
wenzelm@384
   184
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   185
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   186
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   187
wenzelm@384
   188
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   189
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   190
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   191
wenzelm@2199
   192
    fun mfix_of decl =
wenzelm@2199
   193
      let val c = name_of decl in
wenzelm@2199
   194
        (case decl of
wenzelm@2199
   195
          (_, _, NoSyn) => []
wenzelm@5690
   196
        | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@5690
   197
        | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@11651
   198
        | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   199
        | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   200
        | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
wenzelm@11651
   201
        | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@2199
   202
        | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
wenzelm@2199
   203
        | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
wenzelm@2199
   204
        | (_, ty, Binder (sy, p, q)) =>
wenzelm@21534
   205
            [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
wenzelm@18673
   206
        | _ => error ("Bad mixfix declaration for const: " ^ quote c))
wenzelm@2199
   207
    end;
wenzelm@384
   208
wenzelm@21534
   209
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
skalberg@15531
   210
      | binder _ = NONE;
wenzelm@384
   211
wenzelm@19482
   212
    val mfix = maps mfix_of const_decls;
wenzelm@384
   213
    val xconsts = map name_of const_decls;
wenzelm@19482
   214
    val binders = map_filter binder const_decls;
wenzelm@16610
   215
    val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   216
        apsnd K o SynTrans.mk_binder_tr);
wenzelm@15751
   217
    val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@16610
   218
        apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
wenzelm@14903
   219
  in
wenzelm@14903
   220
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   221
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   222
  end;
wenzelm@384
   223
wenzelm@384
   224
end;