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