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;
     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 mixfix_args NoSyn = 0
   132   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
   133   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
   134   | mixfix_args (InfixName (sy, _)) = 2 + SynExt.mfix_args sy
   135   | mixfix_args (InfixlName (sy, _)) = 2 + SynExt.mfix_args sy
   136   | mixfix_args (InfixrName (sy, _)) = 2 + SynExt.mfix_args sy
   137   | mixfix_args (Infix _) = 2
   138   | mixfix_args (Infixl _) = 2
   139   | mixfix_args (Infixr _) = 2
   140   | mixfix_args (Binder _) = 1
   141   | mixfix_args Structure = 0;
   142 
   143 fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
   144   | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
   145 
   146 
   147 (* syn_ext_types *)
   148 
   149 fun syn_ext_types type_decls =
   150   let
   151     fun name_of (t, _, mx) = type_name mx t;
   152 
   153     fun mk_infix sy t p1 p2 p3 =
   154       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   155         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   156 
   157     fun mfix_of decl =
   158       let val t = name_of decl in
   159         (case decl of
   160           (_, _, NoSyn) => NONE
   161         | (_, 2, InfixName (sy, p)) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   162         | (_, 2, InfixlName (sy, p)) => SOME (mk_infix sy t p (p + 1) p)
   163         | (_, 2, InfixrName (sy, p)) => SOME (mk_infix sy t (p + 1) p p)
   164         | (sy, 2, Infix p) => SOME (mk_infix sy t (p + 1) (p + 1) p)
   165         | (sy, 2, Infixl p) => SOME (mk_infix sy t p (p + 1) p)
   166         | (sy, 2, Infixr p) => SOME (mk_infix sy t (p + 1) p p)
   167         | _ => error ("Bad mixfix declaration for type: " ^ quote t))
   168       end;
   169 
   170     val mfix = map_filter mfix_of type_decls;
   171     val xconsts = map name_of type_decls;
   172   in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
   173 
   174 
   175 (* syn_ext_consts *)
   176 
   177 val binder_stamp = stamp ();
   178 val binder_name = suffix "_binder";
   179 
   180 fun syn_ext_consts is_logtype const_decls =
   181   let
   182     fun name_of (c, _, mx) = const_name mx c;
   183 
   184     fun mk_infix sy ty c p1 p2 p3 =
   185       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   186        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   187 
   188     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   189           [Type ("idts", []), ty2] ---> ty3
   190       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   191 
   192     fun mfix_of decl =
   193       let val c = name_of decl in
   194         (case decl of
   195           (_, _, NoSyn) => []
   196         | (_, ty, Mixfix (sy, ps, p)) => [SynExt.Mfix (sy, ty, c, ps, p)]
   197         | (_, ty, Delimfix sy) => [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   198         | (_, ty, InfixName (sy, p)) => mk_infix sy ty c (p + 1) (p + 1) p
   199         | (_, ty, InfixlName (sy, p)) => mk_infix sy ty c p (p + 1) p
   200         | (_, ty, InfixrName (sy, p)) => mk_infix sy ty c (p + 1) p p
   201         | (sy, ty, Infix p) => mk_infix sy ty c (p + 1) (p + 1) p
   202         | (sy, ty, Infixl p) => mk_infix sy ty c p (p + 1) p
   203         | (sy, ty, Infixr p) => mk_infix sy ty c (p + 1) p p
   204         | (_, ty, Binder (sy, p, q)) =>
   205             [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
   206         | _ => error ("Bad mixfix declaration for const: " ^ quote c))
   207     end;
   208 
   209     fun binder (c, _, Binder _) = SOME (binder_name c, c)
   210       | binder _ = NONE;
   211 
   212     val mfix = maps mfix_of const_decls;
   213     val xconsts = map name_of const_decls;
   214     val binders = map_filter binder const_decls;
   215     val binder_trs = binders |> map (SynExt.stamp_trfun binder_stamp o
   216         apsnd K o SynTrans.mk_binder_tr);
   217     val binder_trs' = binders |> map (SynExt.stamp_trfun binder_stamp o
   218         apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
   219   in
   220     SynExt.syn_ext' true is_logtype
   221       mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
   222   end;
   223 
   224 end;