src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Apr 08 22:40:29 2011 +0200 (2011-04-08)
changeset 42298 d622145603ee
parent 42297 140f283266b7
child 52143 36ffe23b25f8
permissions -rw-r--r--
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
tuned;
     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 BASIC_MIXFIX =
     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 end;
    19 
    20 signature MIXFIX =
    21 sig
    22   include BASIC_MIXFIX
    23   val pretty_mixfix: mixfix -> Pretty.T
    24   val mixfix_args: mixfix -> int
    25   val mixfixT: mixfix -> typ
    26   val make_type: int -> typ
    27   val binder_name: string -> string
    28   val syn_ext_types: (string * typ * mixfix) list -> Syntax_Ext.syn_ext
    29   val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext
    30 end;
    31 
    32 structure Mixfix: MIXFIX =
    33 struct
    34 
    35 (** mixfix declarations **)
    36 
    37 datatype mixfix =
    38   NoSyn |
    39   Mixfix of string * int list * int |
    40   Delimfix of string |
    41   Infix of string * int |
    42   Infixl of string * int |
    43   Infixr of string * int |
    44   Binder of string * int * int |
    45   Structure;
    46 
    47 
    48 (* pretty_mixfix *)
    49 
    50 local
    51 
    52 val quoted = Pretty.quote o Pretty.str;
    53 val keyword = Pretty.keyword;
    54 val parens = Pretty.enclose "(" ")";
    55 val brackets = Pretty.enclose "[" "]";
    56 val int = Pretty.str o string_of_int;
    57 
    58 in
    59 
    60 fun pretty_mixfix NoSyn = Pretty.str ""
    61   | pretty_mixfix (Mixfix (s, ps, p)) =
    62       parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
    63   | pretty_mixfix (Delimfix s) = parens [quoted s]
    64   | pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
    65   | pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
    66   | pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
    67   | pretty_mixfix (Binder (s, p1, p2)) =
    68       parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
    69   | pretty_mixfix Structure = parens [keyword "structure"];
    70 
    71 end;
    72 
    73 
    74 (* syntax specifications *)
    75 
    76 fun mixfix_args NoSyn = 0
    77   | mixfix_args (Mixfix (sy, _, _)) = Syntax_Ext.mfix_args sy
    78   | mixfix_args (Delimfix sy) = Syntax_Ext.mfix_args sy
    79   | mixfix_args (Infix (sy, _)) = 2 + Syntax_Ext.mfix_args sy
    80   | mixfix_args (Infixl (sy, _)) = 2 + Syntax_Ext.mfix_args sy
    81   | mixfix_args (Infixr (sy, _)) = 2 + Syntax_Ext.mfix_args sy
    82   | mixfix_args (Binder _) = 1
    83   | mixfix_args Structure = 0;
    84 
    85 fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
    86   | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
    87 
    88 
    89 (* syn_ext_types *)
    90 
    91 val typeT = Type ("type", []);
    92 fun make_type n = replicate n typeT ---> typeT;
    93 
    94 fun syn_ext_types type_decls =
    95   let
    96     fun mk_infix sy ty t p1 p2 p3 = Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, t, [p1, p2], p3);
    97 
    98     fun mfix_of (_, _, NoSyn) = NONE
    99       | mfix_of (t, ty, Mixfix (sy, ps, p)) = SOME (Syntax_Ext.Mfix (sy, ty, t, ps, p))
   100       | mfix_of (t, ty, Delimfix sy) = SOME (Syntax_Ext.Mfix (sy, ty, t, [], 1000))
   101       | mfix_of (t, ty, Infix (sy, p)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p)
   102       | mfix_of (t, ty, Infixl (sy, p)) = SOME (mk_infix sy ty t p (p + 1) p)
   103       | mfix_of (t, ty, Infixr (sy, p)) = SOME (mk_infix sy ty t (p + 1) p p)
   104       | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);
   105 
   106     fun check_args (_, ty, _) (SOME (mfix as Syntax_Ext.Mfix (sy, _, _, _, _))) =
   107           if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then ()
   108           else Syntax_Ext.err_in_mfix "Bad number of type constructor arguments" mfix
   109       | check_args _ NONE = ();
   110 
   111     val mfix = map mfix_of type_decls;
   112     val _ = map2 check_args type_decls mfix;
   113     val consts = map (fn (t, _, _) => (t, "")) type_decls;
   114   in Syntax_Ext.syn_ext (map_filter I mfix) consts ([], [], [], []) ([], []) end;
   115 
   116 
   117 (* syn_ext_consts *)
   118 
   119 val binder_stamp = stamp ();
   120 val binder_name = suffix "_binder";
   121 
   122 fun syn_ext_consts is_logtype const_decls =
   123   let
   124     fun mk_infix sy ty c p1 p2 p3 =
   125       [Syntax_Ext.Mfix ("op " ^ sy, ty, c, [], 1000),
   126        Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   127 
   128     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   129           [Type ("idts", []), ty2] ---> ty3
   130       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   131 
   132     fun mfix_of (_, _, NoSyn) = []
   133       | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Syntax_Ext.Mfix (sy, ty, c, ps, p)]
   134       | mfix_of (c, ty, Delimfix sy) = [Syntax_Ext.Mfix (sy, ty, c, [], 1000)]
   135       | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
   136       | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
   137       | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
   138       | mfix_of (c, ty, Binder (sy, p, q)) =
   139           [Syntax_Ext.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
   140       | mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c);
   141 
   142     fun binder (c, _, Binder _) = SOME (binder_name c, c)
   143       | binder _ = NONE;
   144 
   145     val mfix = maps mfix_of const_decls;
   146     val binders = map_filter binder const_decls;
   147     val binder_trs = binders
   148       |> map (Syntax_Ext.stamp_trfun binder_stamp o apsnd K o Syntax_Trans.mk_binder_tr);
   149     val binder_trs' = binders
   150       |> map (Syntax_Ext.stamp_trfun binder_stamp o
   151           apsnd (K o Syntax_Trans.non_typed_tr') o Syntax_Trans.mk_binder_tr' o swap);
   152 
   153     val consts = binders @ map (fn (c, _, _) => (c, "")) const_decls;
   154   in
   155     Syntax_Ext.syn_ext' is_logtype mfix consts ([], binder_trs, binder_trs', []) ([], [])
   156   end;
   157 
   158 end;
   159 
   160 structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
   161 open Basic_Mixfix;
   162