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