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