src/Pure/Syntax/mixfix.ML
author wenzelm
Mon Feb 15 18:03:42 2010 +0100 (2010-02-15)
changeset 35130 0991c84e8dcf
parent 35129 ed24ba6f69aa
child 35351 7425aece4ee3
permissions -rw-r--r--
renamed InfixName to Infix etc.;
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@384
     7
signature MIXFIX0 =
wenzelm@2199
     8
sig
wenzelm@384
     9
  datatype mixfix =
wenzelm@384
    10
    NoSyn |
wenzelm@384
    11
    Mixfix of string * int list * int |
wenzelm@384
    12
    Delimfix of string |
wenzelm@35130
    13
    Infix of string * int |
wenzelm@35130
    14
    Infixl of string * int |
wenzelm@35130
    15
    Infixr of string * int |
wenzelm@18673
    16
    Binder of string * int * int |
wenzelm@18673
    17
    Structure
wenzelm@21534
    18
  val binder_name: string -> string
wenzelm@2199
    19
end;
wenzelm@384
    20
wenzelm@384
    21
signature MIXFIX1 =
wenzelm@2199
    22
sig
wenzelm@384
    23
  include MIXFIX0
wenzelm@12864
    24
  val literal: string -> mixfix
wenzelm@4823
    25
  val no_syn: 'a * 'b -> 'a * 'b * mixfix
wenzelm@19271
    26
  val pretty_mixfix: mixfix -> Pretty.T
wenzelm@4823
    27
  val mixfix_args: mixfix -> int
wenzelm@22702
    28
  val mixfixT: mixfix -> typ
wenzelm@2199
    29
end;
wenzelm@384
    30
wenzelm@384
    31
signature MIXFIX =
wenzelm@2199
    32
sig
wenzelm@384
    33
  include MIXFIX1
wenzelm@14903
    34
  val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext
wenzelm@14903
    35
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> SynExt.syn_ext
wenzelm@2199
    36
end;
wenzelm@2199
    37
wenzelm@15751
    38
structure Mixfix: MIXFIX =
wenzelm@384
    39
struct
wenzelm@384
    40
wenzelm@2199
    41
wenzelm@384
    42
(** mixfix declarations **)
wenzelm@384
    43
wenzelm@384
    44
datatype mixfix =
wenzelm@384
    45
  NoSyn |
wenzelm@384
    46
  Mixfix of string * int list * int |
wenzelm@384
    47
  Delimfix of string |
wenzelm@35130
    48
  Infix of string * int |
wenzelm@35130
    49
  Infixl of string * int |
wenzelm@35130
    50
  Infixr of string * int |
wenzelm@18673
    51
  Binder of string * int * int |
wenzelm@18673
    52
  Structure;
wenzelm@384
    53
wenzelm@12864
    54
val literal = Delimfix o SynExt.escape_mfix;
wenzelm@12864
    55
wenzelm@4823
    56
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    57
wenzelm@384
    58
wenzelm@19271
    59
(* pretty_mixfix *)
wenzelm@19271
    60
wenzelm@19271
    61
local
wenzelm@11920
    62
wenzelm@19271
    63
val quoted = Pretty.quote o Pretty.str;
wenzelm@19271
    64
val keyword = Pretty.keyword;
wenzelm@19271
    65
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    66
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    67
val int = Pretty.str o string_of_int;
wenzelm@19271
    68
wenzelm@19271
    69
in
wenzelm@11920
    70
wenzelm@19271
    71
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@19271
    72
  | pretty_mixfix (Mixfix (s, ps, p)) =
wenzelm@19271
    73
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@19271
    74
  | pretty_mixfix (Delimfix s) = parens [quoted s]
wenzelm@35130
    75
  | pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@35130
    76
  | pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@35130
    77
  | pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    78
  | pretty_mixfix (Binder (s, p1, p2)) =
wenzelm@19271
    79
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@19271
    80
  | pretty_mixfix Structure = parens [keyword "structure"];
wenzelm@19271
    81
wenzelm@19271
    82
end;
wenzelm@11920
    83
wenzelm@11920
    84
wenzelm@12531
    85
(* syntax specifications *)
wenzelm@384
    86
wenzelm@4053
    87
fun mixfix_args NoSyn = 0
wenzelm@5690
    88
  | mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy
wenzelm@5690
    89
  | mixfix_args (Delimfix sy) = SynExt.mfix_args sy
wenzelm@35130
    90
  | mixfix_args (Infix (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@35130
    91
  | mixfix_args (Infixl (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@35130
    92
  | mixfix_args (Infixr (sy, _)) = 2 + SynExt.mfix_args sy
wenzelm@18673
    93
  | mixfix_args (Binder _) = 1
wenzelm@18673
    94
  | mixfix_args Structure = 0;
wenzelm@4053
    95
wenzelm@22709
    96
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
wenzelm@22709
    97
  | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
wenzelm@22702
    98
wenzelm@4053
    99
wenzelm@384
   100
(* syn_ext_types *)
wenzelm@384
   101
wenzelm@14903
   102
fun syn_ext_types type_decls =
wenzelm@384
   103
  let
wenzelm@2199
   104
    fun mk_infix sy t p1 p2 p3 =
wenzelm@5690
   105
      SynExt.Mfix ("(_ " ^ sy ^ "/ _)",
wenzelm@12512
   106
        [SynExt.typeT, SynExt.typeT] ---> SynExt.typeT, t, [p1, p2], p3);
wenzelm@384
   107
wenzelm@35129
   108
    fun mfix_of (_, _, NoSyn) = NONE
wenzelm@35130
   109
      | mfix_of (t, 2, Infix (sy, p)) = SOME (mk_infix sy t (p + 1) (p + 1) p)
wenzelm@35130
   110
      | mfix_of (t, 2, Infixl (sy, p)) = SOME (mk_infix sy t p (p + 1) p)
wenzelm@35130
   111
      | mfix_of (t, 2, Infixr (sy, p)) = SOME (mk_infix sy t (p + 1) p p)
wenzelm@35129
   112
      | mfix_of (t, _, _) =
wenzelm@35129
   113
          error ("Bad mixfix declaration for type: " ^ quote t);  (* FIXME printable!? *)
wenzelm@384
   114
wenzelm@19482
   115
    val mfix = map_filter mfix_of type_decls;
wenzelm@35129
   116
    val xconsts = map #1 type_decls;
wenzelm@14903
   117
  in SynExt.syn_ext mfix xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   118
wenzelm@384
   119
wenzelm@384
   120
(* syn_ext_consts *)
wenzelm@384
   121
wenzelm@15751
   122
val binder_stamp = stamp ();
wenzelm@21534
   123
val binder_name = suffix "_binder";
wenzelm@15751
   124
wenzelm@14903
   125
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   126
  let
wenzelm@384
   127
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@5690
   128
      [SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri),
wenzelm@12512
   129
       SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   130
wenzelm@384
   131
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   132
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   133
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   134
wenzelm@35129
   135
    fun mfix_of (_, _, NoSyn) = []
wenzelm@35129
   136
      | mfix_of (c, ty, Mixfix (sy, ps, p)) = [SynExt.Mfix (sy, ty, c, ps, p)]
wenzelm@35129
   137
      | mfix_of (c, ty, Delimfix sy) = [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)]
wenzelm@35130
   138
      | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@35130
   139
      | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
wenzelm@35130
   140
      | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
wenzelm@35129
   141
      | mfix_of (c, ty, Binder (sy, p, q)) =
wenzelm@35129
   142
          [SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
wenzelm@35129
   143
      | mfix_of (c, _, _) = error ("Bad mixfix declaration for const: " ^ quote c);
wenzelm@384
   144
wenzelm@21534
   145
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
skalberg@15531
   146
      | binder _ = NONE;
wenzelm@384
   147
wenzelm@19482
   148
    val mfix = maps mfix_of const_decls;
wenzelm@35129
   149
    val xconsts = map #1 const_decls;
wenzelm@19482
   150
    val binders = map_filter binder const_decls;
wenzelm@35129
   151
    val binder_trs = binders
wenzelm@35129
   152
      |> map (SynExt.stamp_trfun binder_stamp o apsnd K o SynTrans.mk_binder_tr);
wenzelm@35129
   153
    val binder_trs' = binders
wenzelm@35129
   154
      |> map (SynExt.stamp_trfun binder_stamp o
wenzelm@35129
   155
          apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap);
wenzelm@14903
   156
  in
wenzelm@14903
   157
    SynExt.syn_ext' true is_logtype
wenzelm@14903
   158
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   159
  end;
wenzelm@384
   160
wenzelm@384
   161
end;