src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Feb 10 02:22:52 2006 +0100 (2006-02-10)
changeset 19003 64ad6c520464
parent 18719 dca3ae4f6dd6
child 19020 e1867198df48
permissions -rw-r--r--
added mixfix_conflict;
     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 string_of_mixfix: mixfix -> string
    30   val type_name: string -> mixfix -> string
    31   val const_name: string -> mixfix -> string
    32   val fix_mixfix: string -> mixfix -> mixfix
    33   val unlocalize_mixfix: mixfix -> mixfix
    34   val mixfix_args: mixfix -> int
    35   val mixfix_conflict: mixfix * mixfix -> bool
    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 (* string_of_mixfix *)
    70 
    71 val parens = enclose "(" ")";
    72 val brackets = enclose "[" "]";
    73 val spaces = space_implode " ";
    74 
    75 fun string_of_mixfix NoSyn = ""
    76   | string_of_mixfix (Mixfix (sy, ps, p)) =
    77       parens (spaces [quote sy, brackets (commas (map string_of_int ps)), string_of_int p])
    78   | string_of_mixfix (Delimfix sy) = parens (quote sy)
    79   | string_of_mixfix (InfixName (sy, p)) = parens (spaces ["infix", quote sy, string_of_int p])
    80   | string_of_mixfix (InfixlName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    81   | string_of_mixfix (InfixrName (sy, p)) = parens (spaces ["infixl", quote sy, string_of_int p])
    82   | string_of_mixfix (Infix p) = parens (spaces ["infix", string_of_int p])
    83   | string_of_mixfix (Infixl p) = parens (spaces ["infixl", string_of_int p])
    84   | string_of_mixfix (Infixr p) = parens (spaces ["infixr", string_of_int p])
    85   | string_of_mixfix (Binder (sy, p1, p2)) =
    86       parens (spaces ["binder", quote sy, brackets (string_of_int p1), string_of_int p2])
    87   | string_of_mixfix Structure = "(structure)";
    88 
    89 
    90 (* syntax specifications *)
    91 
    92 fun strip ("'" :: c :: cs) = c :: strip cs
    93   | strip ["'"] = []
    94   | strip (c :: cs) = c :: strip cs
    95   | strip [] = [];
    96 
    97 val strip_esc = implode o strip o Symbol.explode;
    98 
    99 fun deprecated c = (warning ("Unnamed infix operator " ^ quote c ^ " -- deprecated"); c);
   100 
   101 fun type_name t (InfixName _) = t
   102   | type_name t (InfixlName _) = t
   103   | type_name t (InfixrName _) = t
   104   | type_name t (Infix _) = deprecated (strip_esc t)
   105   | type_name t (Infixl _) = deprecated (strip_esc t)
   106   | type_name t (Infixr _) = deprecated (strip_esc t)
   107   | type_name t _ = t;
   108 
   109 fun const_name c (InfixName _) = c
   110   | const_name c (InfixlName _) = c
   111   | const_name c (InfixrName _) = c
   112   | const_name c (Infix _) = "op " ^ deprecated (strip_esc c)
   113   | const_name c (Infixl _) = "op " ^ deprecated (strip_esc c)
   114   | const_name c (Infixr _) = "op " ^ deprecated (strip_esc c)
   115   | const_name c _ = c;
   116 
   117 fun fix_mixfix c (Infix p) = (deprecated (strip_esc c); InfixName (c, p))
   118   | fix_mixfix c (Infixl p) = (deprecated (strip_esc c); InfixlName (c, p))
   119   | fix_mixfix c (Infixr p) = (deprecated (strip_esc c); InfixrName (c, p))
   120   | fix_mixfix _ mx = mx;
   121 
   122 fun map_mixfix _ NoSyn = NoSyn
   123   | map_mixfix f (Mixfix (sy, ps, p)) = Mixfix (f sy, ps, p)
   124   | map_mixfix f (Delimfix sy) = Delimfix (f sy)
   125   | map_mixfix f (InfixName (sy, p)) = InfixName (f sy, p)
   126   | map_mixfix f (InfixlName (sy, p)) = InfixlName (f sy, p)
   127   | map_mixfix f (InfixrName (sy, p)) = InfixrName (f sy, p)
   128   | map_mixfix f (Binder (sy, p, q)) = Binder (f sy, p, q)
   129   | map_mixfix _ Structure = Structure
   130   | map_mixfix _ _ = raise Fail ("map_mixfix: illegal occurrence of unnamed infix");
   131 
   132 val unlocalize_mixfix = map_mixfix SynExt.unlocalize_mfix;
   133 
   134 fun mixfix_args NoSyn = 0
   135   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
   136   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
   137   | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
   138   | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
   139   | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
   140   | mixfix_args (Infix _) = 2
   141   | mixfix_args (Infixl _) = 2
   142   | mixfix_args (Infixr _) = 2
   143   | mixfix_args (Binder _) = 1
   144   | mixfix_args Structure = 0;
   145 
   146 fun mixfix_conflict (mx1, mx2) =
   147   let
   148     fun content kind sy = SOME (kind, SynExt.mfix_delims sy);
   149     fun content_of (Mixfix (sy, _, _)) = content 0 sy
   150       | content_of (Delimfix sy) = content 0 sy
   151       | content_of (InfixName (sy, _)) = content 1 sy
   152       | content_of (InfixlName (sy, _)) = content 1 sy
   153       | content_of (InfixrName (sy, _)) = content 1 sy
   154       | content_of (Binder (sy, _, _)) = content 2 sy
   155       | content_of _ = NONE;
   156   in
   157     (case (content_of mx1, content_of mx2) of
   158       (SOME (kind1, delims1), SOME (kind2, delims2)) =>
   159         (kind1 = 0 orelse kind2 = 0 orelse kind1 = kind2) andalso delims1 = delims2
   160     | _ => false)
   161   end;
   162 
   163 
   164 (* syn_ext_types *)
   165 
   166 fun syn_ext_types type_decls =
   167   let
   168     fun name_of (t, _, mx) = type_name t mx;
   169 
   170     fun mk_infix sy t p1 p2 p3 =
   171       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   172         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   173 
   174     fun mfix_of decl =
   175       let val t = name_of decl in
   176         (case decl of
   177           (_, _, NoSyn) => NONE
   178         | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   179         | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
   180         | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
   181         | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   182         | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
   183         | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
   184         | _ => error ("Bad mixfix declaration for type: " ^ quote t))
   185       end;
   186 
   187     val mfix = List.mapPartial mfix_of type_decls;
   188     val xconsts = map name_of type_decls;
   189   in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
   190 
   191 
   192 (* syn_ext_consts *)
   193 
   194 val binder_stamp = stamp ();
   195 
   196 fun syn_ext_consts is_logtype const_decls =
   197   let
   198     fun name_of (c, _, mx) = const_name c mx;
   199 
   200     fun mk_infix sy ty c p1 p2 p3 =
   201       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   202        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   203 
   204     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   205           [Type ("idts", []), ty2] ---> ty3
   206       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   207 
   208     fun mfix_of decl =
   209       let val c = name_of decl in
   210         (case decl of
   211           (_, _, NoSyn) => []
   212         | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
   213         | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   214         | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
   215         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   216         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   217         | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
   218         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   219         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   220         | (_, ty, Binder (sy, p, q)) =>
   221             [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, sy, [0, p], q)]
   222         | _ => error ("Bad mixfix declaration for const: " ^ quote c))
   223     end;
   224 
   225     fun binder (c, _, Binder (sy, _, _)) = SOME (sy, c)
   226       | binder _ = NONE;
   227 
   228     val mfix = List.concat (map mfix_of const_decls);
   229     val xconsts = map name_of const_decls;
   230     val binders = List.mapPartial binder const_decls;
   231     val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
   232         apsnd K o SynTrans.mk_binder_tr);
   233     val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
   234         apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
   235   in
   236     SynExt.syn_ext' true is_logtype
   237       mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
   238   end;
   239 
   240 end;