src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Mar 30 15:15:12 2016 +0200 (2016-03-30)
changeset 62761 5c672b22dcc2
parent 62760 aabcc727aa2d
child 62766 70b73465f636
permissions -rw-r--r--
clarified simple mixfix;
     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 Input.source * int list * int * Position.range |
    12     Infix of Input.source * int * Position.range |
    13     Infixl of Input.source * int * Position.range |
    14     Infixr of Input.source * int * Position.range |
    15     Binder of Input.source * int * int * Position.range |
    16     Structure of Position.range
    17 end;
    18 
    19 signature MIXFIX =
    20 sig
    21   include BASIC_MIXFIX
    22   val mixfix: string -> mixfix
    23   val is_empty: mixfix -> bool
    24   val equal: mixfix * mixfix -> bool
    25   val range_of: mixfix -> Position.range
    26   val pos_of: mixfix -> Position.T
    27   val reset_pos: mixfix -> mixfix
    28   val pretty_mixfix: mixfix -> Pretty.T
    29   val mixfix_args: mixfix -> int
    30   val mixfixT: mixfix -> typ
    31   val make_type: int -> typ
    32   val binder_name: string -> string
    33   val syn_ext_types: (string * typ * mixfix) list -> Syntax_Ext.syn_ext
    34   val syn_ext_consts: string list -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext
    35 end;
    36 
    37 structure Mixfix: MIXFIX =
    38 struct
    39 
    40 (** mixfix declarations **)
    41 
    42 datatype mixfix =
    43   NoSyn |
    44   Mixfix of Input.source * int list * int * Position.range |
    45   Infix of Input.source * int * Position.range |
    46   Infixl of Input.source * int * Position.range |
    47   Infixr of Input.source * int * Position.range |
    48   Binder of Input.source * int * int * Position.range |
    49   Structure of Position.range;
    50 
    51 fun mixfix s = Mixfix (Input.string s, [], 1000, Position.no_range);
    52 
    53 fun is_empty NoSyn = true
    54   | is_empty _ = false;
    55 
    56 fun equal (NoSyn, NoSyn) = true
    57   | equal (Mixfix (sy, ps, p, _), Mixfix (sy', ps', p', _)) =
    58       Input.equal_content (sy, sy') andalso ps = ps' andalso p = p'
    59   | equal (Infix (sy, p, _), Infix (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
    60   | equal (Infixl (sy, p, _), Infixl (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
    61   | equal (Infixr (sy, p, _), Infixr (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p'
    62   | equal (Binder (sy, p, q, _), Binder (sy', p', q', _)) =
    63       Input.equal_content (sy, sy') andalso p = p' andalso q = q'
    64   | equal (Structure _, Structure _) = true
    65   | equal _ = false;
    66 
    67 fun range_of NoSyn = Position.no_range
    68   | range_of (Mixfix (_, _, _, range)) = range
    69   | range_of (Infix (_, _, range)) = range
    70   | range_of (Infixl (_, _, range)) = range
    71   | range_of (Infixr (_, _, range)) = range
    72   | range_of (Binder (_, _, _, range)) = range
    73   | range_of (Structure range) = range;
    74 
    75 val pos_of = Position.set_range o range_of;
    76 
    77 fun reset_pos NoSyn = NoSyn
    78   | reset_pos (Mixfix (sy, ps, p, _)) = Mixfix (Input.reset_pos sy, ps, p, Position.no_range)
    79   | reset_pos (Infix (sy, p, _)) = Infix (Input.reset_pos sy, p, Position.no_range)
    80   | reset_pos (Infixl (sy, p, _)) = Infixl (Input.reset_pos sy, p, Position.no_range)
    81   | reset_pos (Infixr (sy, p, _)) = Infixr (Input.reset_pos sy, p, Position.no_range)
    82   | reset_pos (Binder (sy, p, q, _)) = Binder (Input.reset_pos sy, p, q, Position.no_range)
    83   | reset_pos (Structure _) = Structure Position.no_range;
    84 
    85 
    86 (* pretty_mixfix *)
    87 
    88 local
    89 
    90 val quoted = Pretty.quote o Pretty.str o Input.source_content;
    91 val keyword = Pretty.keyword2;
    92 val parens = Pretty.enclose "(" ")";
    93 val brackets = Pretty.enclose "[" "]";
    94 val int = Pretty.str o string_of_int;
    95 
    96 in
    97 
    98 fun pretty_mixfix NoSyn = Pretty.str ""
    99   | pretty_mixfix (Mixfix (s, ps, p, _)) =
   100       parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
   101   | pretty_mixfix (Infix (s, p, _)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
   102   | pretty_mixfix (Infixl (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
   103   | pretty_mixfix (Infixr (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
   104   | pretty_mixfix (Binder (s, p1, p2, _)) =
   105       parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
   106   | pretty_mixfix (Structure _) = parens [keyword "structure"];
   107 
   108 end;
   109 
   110 
   111 (* syntax specifications *)
   112 
   113 fun mixfix_args NoSyn = 0
   114   | mixfix_args (Mixfix (sy, _, _, _)) = Syntax_Ext.mixfix_args sy
   115   | mixfix_args (Infix (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
   116   | mixfix_args (Infixl (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
   117   | mixfix_args (Infixr (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
   118   | mixfix_args (Binder _) = 1
   119   | mixfix_args (Structure _) = 0;
   120 
   121 fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
   122   | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
   123 
   124 
   125 (* syn_ext_types *)
   126 
   127 val typeT = Type ("type", []);
   128 fun make_type n = replicate n typeT ---> typeT;
   129 
   130 fun syn_ext_types type_decls =
   131   let
   132     fun mk_infix sy ty t p1 p2 p3 range =
   133       Syntax_Ext.Mfix
   134         (Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)",
   135           ty, t, [p1, p2], p3, Position.set_range range);
   136 
   137     fun mfix_of (_, _, NoSyn) = NONE
   138       | mfix_of (t, ty, Mixfix (sy, ps, p, range)) =
   139           SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, ps, p, Position.set_range range))
   140       | mfix_of (t, ty, Infix (sy, p, range)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p range)
   141       | mfix_of (t, ty, Infixl (sy, p, range)) = SOME (mk_infix sy ty t p (p + 1) p range)
   142       | mfix_of (t, ty, Infixr (sy, p, range)) = SOME (mk_infix sy ty t (p + 1) p p range)
   143       | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);
   144 
   145     fun check_args (_, ty, mx) (SOME (Syntax_Ext.Mfix (sy, _, _, _, _, _))) =
   146           if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then ()
   147           else
   148             error ("Bad number of type constructor arguments in mixfix annotation" ^
   149               Position.here (pos_of mx))
   150       | check_args _ NONE = ();
   151 
   152     val mfix = map mfix_of type_decls;
   153     val _ = map2 check_args type_decls mfix;
   154     val consts = map (fn (t, _, _) => (t, "")) type_decls;
   155   in Syntax_Ext.syn_ext (map_filter I mfix) consts ([], [], [], []) ([], []) end;
   156 
   157 
   158 (* syn_ext_consts *)
   159 
   160 val binder_stamp = stamp ();
   161 val binder_name = suffix "_binder";
   162 
   163 fun syn_ext_consts logical_types const_decls =
   164   let
   165     fun mk_infix sy ty c p1 p2 p3 range =
   166       [Syntax_Ext.Mfix
   167         (Symbol_Pos.explode0 "op " @ Input.source_explode sy,
   168           ty, c, [], 1000, Position.set_range range),
   169        Syntax_Ext.Mfix
   170         (Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)",
   171           ty, c, [p1, p2], p3, Position.set_range range)];
   172 
   173     fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
   174           [Type ("idts", []), ty2] ---> ty3
   175       | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
   176 
   177     fun mfix_of (_, _, NoSyn) = []
   178       | mfix_of (c, ty, Mixfix (sy, ps, p, range)) =
   179           [Syntax_Ext.Mfix (Input.source_explode sy, ty, c, ps, p, Position.set_range range)]
   180       | mfix_of (c, ty, Infix (sy, p, range)) = mk_infix sy ty c (p + 1) (p + 1) p range
   181       | mfix_of (c, ty, Infixl (sy, p, range)) = mk_infix sy ty c p (p + 1) p range
   182       | mfix_of (c, ty, Infixr (sy, p, range)) = mk_infix sy ty c (p + 1) p p range
   183       | mfix_of (c, ty, Binder (sy, p, q, range)) =
   184           [Syntax_Ext.Mfix
   185             (Symbol_Pos.explode0 "(3" @ Input.source_explode sy @ Symbol_Pos.explode0 "_./ _)",
   186               binder_typ c ty, (binder_name c), [0, p], q, Position.set_range range)]
   187       | mfix_of (c, _, mx) =
   188           error ("Bad mixfix declaration for " ^ quote c ^ Position.here (pos_of mx));
   189 
   190     fun binder (c, _, Binder _) = SOME (binder_name c, c)
   191       | binder _ = NONE;
   192 
   193     val mfix = maps mfix_of const_decls;
   194     val binders = map_filter binder const_decls;
   195     val binder_trs = binders
   196       |> map (Syntax_Ext.stamp_trfun binder_stamp o Syntax_Trans.mk_binder_tr);
   197     val binder_trs' = binders
   198       |> map (Syntax_Ext.stamp_trfun binder_stamp o
   199           apsnd Syntax_Trans.non_typed_tr' o Syntax_Trans.mk_binder_tr' o swap);
   200 
   201     val consts = binders @ map (fn (c, _, _) => (c, "")) const_decls;
   202   in
   203     Syntax_Ext.syn_ext' logical_types mfix consts ([], binder_trs, binder_trs', []) ([], [])
   204   end;
   205 
   206 end;
   207 
   208 structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
   209 open Basic_Mixfix;