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