src/Pure/Syntax/mixfix.ML
author wenzelm
Mon Feb 15 18:03:42 2010 +0100 (2010-02-15)
changeset 35130 0991c84e8dcf
parent 35129 ed24ba6f69aa
child 35351 7425aece4ee3
permissions -rw-r--r--
renamed InfixName to Infix etc.;
     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     Infix of string * int |
    14     Infixl of string * int |
    15     Infixr of string * int |
    16     Binder of string * int * int |
    17     Structure
    18   val binder_name: string -> string
    19 end;
    20 
    21 signature MIXFIX1 =
    22 sig
    23   include MIXFIX0
    24   val literal: string -> mixfix
    25   val no_syn: 'a * 'b -> 'a * 'b * mixfix
    26   val pretty_mixfix: mixfix -> Pretty.T
    27   val mixfix_args: mixfix -> int
    28   val mixfixT: mixfix -> typ
    29 end;
    30 
    31 signature MIXFIX =
    32 sig
    33   include MIXFIX1
    34   val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext
    35   val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> SynExt.syn_ext
    36 end;
    37 
    38 structure Mixfix: MIXFIX =
    39 struct
    40 
    41 
    42 (** mixfix declarations **)
    43 
    44 datatype mixfix =
    45   NoSyn |
    46   Mixfix of string * int list * int |
    47   Delimfix of string |
    48   Infix of string * int |
    49   Infixl of string * int |
    50   Infixr of string * int |
    51   Binder of string * int * int |
    52   Structure;
    53 
    54 val literal = Delimfix o SynExt.escape_mfix;
    55 
    56 fun no_syn (x, y) = (x, y, NoSyn);
    57 
    58 
    59 (* pretty_mixfix *)
    60 
    61 local
    62 
    63 val quoted = Pretty.quote o Pretty.str;
    64 val keyword = Pretty.keyword;
    65 val parens = Pretty.enclose "(" ")";
    66 val brackets = Pretty.enclose "[" "]";
    67 val int = Pretty.str o string_of_int;
    68 
    69 in
    70 
    71 fun pretty_mixfix NoSyn = Pretty.str ""
    72   | pretty_mixfix (Mixfix (s, ps, p)) =
    73       parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
    74   | pretty_mixfix (Delimfix s) = parens [quoted s]
    75   | pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
    76   | pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
    77   | pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
    78   | pretty_mixfix (Binder (s, p1, p2)) =
    79       parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
    80   | pretty_mixfix Structure = parens [keyword "structure"];
    81 
    82 end;
    83 
    84 
    85 (* syntax specifications *)
    86 
    87 fun mixfix_args NoSyn = 0
    88   | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
    89   | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
    90   | mixfix_args (Infix (sy, _)) = 2 + SynExt.mfix_args sy
    91   | mixfix_args (Infixl (sy, _)) = 2 + SynExt.mfix_args sy
    92   | mixfix_args (Infixr (sy, _)) = 2 + SynExt.mfix_args sy
    93   | mixfix_args (Binder _) = 1
    94   | mixfix_args Structure = 0;
    95 
    96 fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
    97   | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
    98 
    99 
   100 (* syn_ext_types *)
   101 
   102 fun syn_ext_types type_decls =
   103   let
   104     fun mk_infix sy t p1 p2 p3 =
   105       SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
   106         [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
   107 
   108     fun mfix_of (_, _, NoSyn) = NONE
   109       | mfix_of (t, 2, Infix (sy, p)) = SOME (mk_infix sy t (p + 1) (p + 1) p)
   110       | mfix_of (t, 2, Infixl (sy, p)) = SOME (mk_infix sy t p (p + 1) p)
   111       | mfix_of (t, 2, Infixr (sy, p)) = SOME (mk_infix sy t (p + 1) p p)
   112       | mfix_of (t, _, _) =
   113           error ("Bad mixfix declaration for type: " ^ quote t);  (* FIXME printable!? *)
   114 
   115     val mfix = map_filter mfix_of type_decls;
   116     val xconsts = map #1 type_decls;
   117   in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
   118 
   119 
   120 (* syn_ext_consts *)
   121 
   122 val binder_stamp = stamp ();
   123 val binder_name = suffix "_binder";
   124 
   125 fun syn_ext_consts is_logtype const_decls =
   126   let
   127     fun mk_infix sy ty c p1 p2 p3 =
   128       [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
   129        SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   130 
   131     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   132           [Type ("idts", []), ty2] ---> ty3
   133       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   134 
   135     fun mfix_of (_, _, NoSyn) = []
   136       | mfix_of (c, ty, Mixfix (sy, ps, p)) = [SynExt.Mfix (sy, ty, c, ps, p)]
   137       | mfix_of (c, ty, Delimfix sy) = [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
   138       | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
   139       | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
   140       | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
   141       | mfix_of (c, ty, Binder (sy, p, q)) =
   142           [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
   143       | mfix_of (c, _, _) = error ("Bad mixfix declaration for const: " ^ quote c);
   144 
   145     fun binder (c, _, Binder _) = SOME (binder_name c, c)
   146       | binder _ = NONE;
   147 
   148     val mfix = maps mfix_of const_decls;
   149     val xconsts = map #1 const_decls;
   150     val binders = map_filter binder const_decls;
   151     val binder_trs = binders
   152       |> map (SynExt.stamp_trfun binder_stamp o apsnd K o SynTrans.mk_binder_tr);
   153     val binder_trs' = binders
   154       |> map (SynExt.stamp_trfun binder_stamp o
   155           apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
   156   in
   157     SynExt.syn_ext' true is_logtype
   158       mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
   159   end;
   160 
   161 end;