src/Pure/Syntax/mixfix.ML
author wenzelm
Wed Mar 30 14:59:12 2016 +0200 (2016-03-30)
changeset 62760 aabcc727aa2d
parent 62759 d16b2ec535ba
child 62761 5c672b22dcc2
permissions -rw-r--r--
tuned;
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 range_of: mixfix -> Position.range
wenzelm@62752
    26
  val pos_of: mixfix -> Position.T
wenzelm@62759
    27
  val reset_pos: mixfix -> mixfix
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 range_of NoSyn = Position.no_range
wenzelm@62752
    68
  | range_of (Mixfix (_, _, _, range)) = range
wenzelm@62752
    69
  | range_of (Delimfix (_, range)) = range
wenzelm@62752
    70
  | range_of (Infix (_, _, range)) = range
wenzelm@62752
    71
  | range_of (Infixl (_, _, range)) = range
wenzelm@62752
    72
  | range_of (Infixr (_, _, range)) = range
wenzelm@62752
    73
  | range_of (Binder (_, _, _, range)) = range
wenzelm@62752
    74
  | range_of (Structure range) = range;
wenzelm@62752
    75
wenzelm@62752
    76
val pos_of = Position.set_range o range_of;
wenzelm@384
    77
wenzelm@62759
    78
fun reset_pos NoSyn = NoSyn
wenzelm@62759
    79
  | reset_pos (Mixfix (sy, ps, p, _)) = Mixfix (Input.reset_pos sy, ps, p, Position.no_range)
wenzelm@62759
    80
  | reset_pos (Delimfix (sy, _)) = Delimfix (Input.reset_pos sy, Position.no_range)
wenzelm@62759
    81
  | reset_pos (Infix (sy, p, _)) = Infix (Input.reset_pos sy, p, Position.no_range)
wenzelm@62759
    82
  | reset_pos (Infixl (sy, p, _)) = Infixl (Input.reset_pos sy, p, Position.no_range)
wenzelm@62759
    83
  | reset_pos (Infixr (sy, p, _)) = Infixr (Input.reset_pos sy, p, Position.no_range)
wenzelm@62759
    84
  | reset_pos (Binder (sy, p, q, _)) = Binder (Input.reset_pos sy, p, q, Position.no_range)
wenzelm@62759
    85
  | reset_pos (Structure _) = Structure Position.no_range;
wenzelm@62759
    86
wenzelm@384
    87
wenzelm@19271
    88
(* pretty_mixfix *)
wenzelm@19271
    89
wenzelm@19271
    90
local
wenzelm@11920
    91
wenzelm@62752
    92
val quoted = Pretty.quote o Pretty.str o Input.source_content;
wenzelm@55763
    93
val keyword = Pretty.keyword2;
wenzelm@19271
    94
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    95
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    96
val int = Pretty.str o string_of_int;
wenzelm@19271
    97
wenzelm@19271
    98
in
wenzelm@11920
    99
wenzelm@19271
   100
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@62752
   101
  | pretty_mixfix (Mixfix (s, ps, p, _)) =
wenzelm@19271
   102
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@62752
   103
  | pretty_mixfix (Delimfix (s, _)) = parens [quoted s]
wenzelm@62752
   104
  | pretty_mixfix (Infix (s, p, _)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@62752
   105
  | pretty_mixfix (Infixl (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@62752
   106
  | pretty_mixfix (Infixr (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@62752
   107
  | pretty_mixfix (Binder (s, p1, p2, _)) =
wenzelm@19271
   108
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@62752
   109
  | pretty_mixfix (Structure _) = parens [keyword "structure"];
wenzelm@19271
   110
wenzelm@19271
   111
end;
wenzelm@11920
   112
wenzelm@11920
   113
wenzelm@12531
   114
(* syntax specifications *)
wenzelm@384
   115
wenzelm@4053
   116
fun mixfix_args NoSyn = 0
wenzelm@62752
   117
  | mixfix_args (Mixfix (sy, _, _, _)) = Syntax_Ext.mixfix_args sy
wenzelm@62752
   118
  | mixfix_args (Delimfix (sy, _)) = Syntax_Ext.mixfix_args sy
wenzelm@62752
   119
  | mixfix_args (Infix (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
wenzelm@62752
   120
  | mixfix_args (Infixl (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
wenzelm@62752
   121
  | mixfix_args (Infixr (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy
wenzelm@18673
   122
  | mixfix_args (Binder _) = 1
wenzelm@62752
   123
  | mixfix_args (Structure _) = 0;
wenzelm@4053
   124
wenzelm@22709
   125
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
wenzelm@22709
   126
  | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
wenzelm@22702
   127
wenzelm@4053
   128
wenzelm@384
   129
(* syn_ext_types *)
wenzelm@384
   130
wenzelm@42293
   131
val typeT = Type ("type", []);
wenzelm@42293
   132
fun make_type n = replicate n typeT ---> typeT;
wenzelm@35412
   133
wenzelm@14903
   134
fun syn_ext_types type_decls =
wenzelm@384
   135
  let
wenzelm@62753
   136
    fun mk_infix sy ty t p1 p2 p3 range =
wenzelm@62752
   137
      Syntax_Ext.Mfix
wenzelm@62752
   138
        (Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)",
wenzelm@62753
   139
          ty, t, [p1, p2], p3, Position.set_range range);
wenzelm@384
   140
wenzelm@35129
   141
    fun mfix_of (_, _, NoSyn) = NONE
wenzelm@62753
   142
      | mfix_of (t, ty, Mixfix (sy, ps, p, range)) =
wenzelm@62753
   143
          SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, ps, p, Position.set_range range))
wenzelm@62753
   144
      | mfix_of (t, ty, Delimfix (sy, range)) =
wenzelm@62753
   145
          SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, [], 1000, Position.set_range range))
wenzelm@62753
   146
      | mfix_of (t, ty, Infix (sy, p, range)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p range)
wenzelm@62753
   147
      | mfix_of (t, ty, Infixl (sy, p, range)) = SOME (mk_infix sy ty t p (p + 1) p range)
wenzelm@62753
   148
      | mfix_of (t, ty, Infixr (sy, p, range)) = SOME (mk_infix sy ty t (p + 1) p p range)
wenzelm@42130
   149
      | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);
wenzelm@384
   150
wenzelm@62753
   151
    fun check_args (_, ty, mx) (SOME (Syntax_Ext.Mfix (sy, _, _, _, _, _))) =
wenzelm@42288
   152
          if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then ()
wenzelm@62752
   153
          else
wenzelm@62752
   154
            error ("Bad number of type constructor arguments in mixfix annotation" ^
wenzelm@62752
   155
              Position.here (pos_of mx))
wenzelm@35351
   156
      | check_args _ NONE = ();
wenzelm@35351
   157
wenzelm@35351
   158
    val mfix = map mfix_of type_decls;
wenzelm@35351
   159
    val _ = map2 check_args type_decls mfix;
wenzelm@42298
   160
    val consts = map (fn (t, _, _) => (t, "")) type_decls;
wenzelm@42298
   161
  in Syntax_Ext.syn_ext (map_filter I mfix) consts ([], [], [], []) ([], []) end;
wenzelm@384
   162
wenzelm@384
   163
wenzelm@384
   164
(* syn_ext_consts *)
wenzelm@384
   165
wenzelm@15751
   166
val binder_stamp = stamp ();
wenzelm@21534
   167
val binder_name = suffix "_binder";
wenzelm@15751
   168
wenzelm@59841
   169
fun syn_ext_consts logical_types const_decls =
wenzelm@384
   170
  let
wenzelm@62753
   171
    fun mk_infix sy ty c p1 p2 p3 range =
wenzelm@62752
   172
      [Syntax_Ext.Mfix
wenzelm@62753
   173
        (Symbol_Pos.explode0 "op " @ Input.source_explode sy,
wenzelm@62753
   174
          ty, c, [], 1000, Position.set_range range),
wenzelm@62752
   175
       Syntax_Ext.Mfix
wenzelm@62752
   176
        (Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)",
wenzelm@62753
   177
          ty, c, [p1, p2], p3, Position.set_range range)];
wenzelm@384
   178
wenzelm@384
   179
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   180
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   181
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   182
wenzelm@35129
   183
    fun mfix_of (_, _, NoSyn) = []
wenzelm@62753
   184
      | mfix_of (c, ty, Mixfix (sy, ps, p, range)) =
wenzelm@62753
   185
          [Syntax_Ext.Mfix (Input.source_explode sy, ty, c, ps, p, Position.set_range range)]
wenzelm@62753
   186
      | mfix_of (c, ty, Delimfix (sy, range)) =
wenzelm@62753
   187
          [Syntax_Ext.Mfix (Input.source_explode sy, ty, c, [], 1000, Position.set_range range)]
wenzelm@62753
   188
      | mfix_of (c, ty, Infix (sy, p, range)) = mk_infix sy ty c (p + 1) (p + 1) p range
wenzelm@62753
   189
      | mfix_of (c, ty, Infixl (sy, p, range)) = mk_infix sy ty c p (p + 1) p range
wenzelm@62753
   190
      | mfix_of (c, ty, Infixr (sy, p, range)) = mk_infix sy ty c (p + 1) p p range
wenzelm@62753
   191
      | mfix_of (c, ty, Binder (sy, p, q, range)) =
wenzelm@62752
   192
          [Syntax_Ext.Mfix
wenzelm@62752
   193
            (Symbol_Pos.explode0 "(3" @ Input.source_explode sy @ Symbol_Pos.explode0 "_./ _)",
wenzelm@62753
   194
              binder_typ c ty, (binder_name c), [0, p], q, Position.set_range range)]
wenzelm@62752
   195
      | mfix_of (c, _, mx) =
wenzelm@62752
   196
          error ("Bad mixfix declaration for " ^ quote c ^ Position.here (pos_of mx));
wenzelm@384
   197
wenzelm@21534
   198
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
skalberg@15531
   199
      | binder _ = NONE;
wenzelm@384
   200
wenzelm@19482
   201
    val mfix = maps mfix_of const_decls;
wenzelm@19482
   202
    val binders = map_filter binder const_decls;
wenzelm@35129
   203
    val binder_trs = binders
wenzelm@52143
   204
      |> map (Syntax_Ext.stamp_trfun binder_stamp o Syntax_Trans.mk_binder_tr);
wenzelm@35129
   205
    val binder_trs' = binders
wenzelm@42288
   206
      |> map (Syntax_Ext.stamp_trfun binder_stamp o
wenzelm@52143
   207
          apsnd Syntax_Trans.non_typed_tr' o Syntax_Trans.mk_binder_tr' o swap);
wenzelm@42298
   208
wenzelm@42298
   209
    val consts = binders @ map (fn (c, _, _) => (c, "")) const_decls;
wenzelm@14903
   210
  in
wenzelm@59841
   211
    Syntax_Ext.syn_ext' logical_types mfix consts ([], binder_trs, binder_trs', []) ([], [])
wenzelm@14903
   212
  end;
wenzelm@384
   213
wenzelm@384
   214
end;
wenzelm@42287
   215
wenzelm@42287
   216
structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
wenzelm@42287
   217
open Basic_Mixfix;