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