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