src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Apr 08 15:02:11 2011 +0200 (2011-04-08)
changeset 42288 2074b31650e6
parent 42287 d98eb048a2e4
child 42293 6cca0343ea48
permissions -rw-r--r--
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
clarified Syntax.root;
     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 fun make_type n = replicate n Syntax_Ext.typeT ---> Syntax_Ext.typeT;
    92 
    93 fun syn_ext_types type_decls =
    94   let
    95     fun mk_infix sy ty t p1 p2 p3 = Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, t, [p1, p2], p3);
    96 
    97     fun mfix_of (_, _, NoSyn) = NONE
    98       | mfix_of (t, ty, Mixfix (sy, ps, p)) = SOME (Syntax_Ext.Mfix (sy, ty, t, ps, p))
    99       | mfix_of (t, ty, Delimfix sy) = SOME (Syntax_Ext.Mfix (sy, ty, t, [], Syntax_Ext.max_pri))
   100       | mfix_of (t, ty, Infix (sy, p)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p)
   101       | mfix_of (t, ty, Infixl (sy, p)) = SOME (mk_infix sy ty t p (p + 1) p)
   102       | mfix_of (t, ty, Infixr (sy, p)) = SOME (mk_infix sy ty t (p + 1) p p)
   103       | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);
   104 
   105     fun check_args (_, ty, _) (SOME (mfix as Syntax_Ext.Mfix (sy, _, _, _, _))) =
   106           if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then ()
   107           else Syntax_Ext.err_in_mfix "Bad number of type constructor arguments" mfix
   108       | check_args _ NONE = ();
   109 
   110     val mfix = map mfix_of type_decls;
   111     val _ = map2 check_args type_decls mfix;
   112     val xconsts = map #1 type_decls;
   113   in Syntax_Ext.syn_ext (map_filter I mfix) xconsts ([], [], [], []) ([], []) end;
   114 
   115 
   116 (* syn_ext_consts *)
   117 
   118 val binder_stamp = stamp ();
   119 val binder_name = suffix "_binder";
   120 
   121 fun syn_ext_consts is_logtype const_decls =
   122   let
   123     fun mk_infix sy ty c p1 p2 p3 =
   124       [Syntax_Ext.Mfix ("op " ^ sy, ty, c, [], Syntax_Ext.max_pri),
   125        Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
   126 
   127     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   128           [Type ("idts", []), ty2] ---> ty3
   129       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   130 
   131     fun mfix_of (_, _, NoSyn) = []
   132       | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Syntax_Ext.Mfix (sy, ty, c, ps, p)]
   133       | mfix_of (c, ty, Delimfix sy) = [Syntax_Ext.Mfix (sy, ty, c, [], Syntax_Ext.max_pri)]
   134       | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
   135       | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
   136       | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
   137       | mfix_of (c, ty, Binder (sy, p, q)) =
   138           [Syntax_Ext.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
   139       | mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c);
   140 
   141     fun binder (c, _, Binder _) = SOME (binder_name c, c)
   142       | binder _ = NONE;
   143 
   144     val mfix = maps mfix_of const_decls;
   145     val xconsts = map #1 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   in
   153     Syntax_Ext.syn_ext' true is_logtype
   154       mfix xconsts ([], binder_trs, binder_trs', []) ([], [])
   155   end;
   156 
   157 end;
   158 
   159 structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
   160 open Basic_Mixfix;
   161