src/Pure/Syntax/mixfix.ML
author wenzelm
Mon May 31 21:06:57 2010 +0200 (2010-05-31)
changeset 37216 3165bc303f66
parent 35412 b8dead547d9e
child 42130 e10e2cce85c8
permissions -rw-r--r--
modernized some structure names, keeping a few legacy aliases;
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@4823
    24
  val no_syn: 'a * 'b -> 'a * 'b * mixfix
wenzelm@19271
    25
  val pretty_mixfix: mixfix -> Pretty.T
wenzelm@4823
    26
  val mixfix_args: mixfix -> int
wenzelm@22702
    27
  val mixfixT: mixfix -> typ
wenzelm@35412
    28
  val make_type: int -> typ
wenzelm@2199
    29
end;
wenzelm@384
    30
wenzelm@384
    31
signature MIXFIX =
wenzelm@2199
    32
sig
wenzelm@384
    33
  include MIXFIX1
wenzelm@37216
    34
  val syn_ext_types: (string * typ * mixfix) list -> Syn_Ext.syn_ext
wenzelm@37216
    35
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> Syn_Ext.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@4823
    54
fun no_syn (x, y) = (x, y, NoSyn);
wenzelm@4823
    55
wenzelm@384
    56
wenzelm@19271
    57
(* pretty_mixfix *)
wenzelm@19271
    58
wenzelm@19271
    59
local
wenzelm@11920
    60
wenzelm@19271
    61
val quoted = Pretty.quote o Pretty.str;
wenzelm@19271
    62
val keyword = Pretty.keyword;
wenzelm@19271
    63
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    64
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    65
val int = Pretty.str o string_of_int;
wenzelm@19271
    66
wenzelm@19271
    67
in
wenzelm@11920
    68
wenzelm@19271
    69
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@19271
    70
  | pretty_mixfix (Mixfix (s, ps, p)) =
wenzelm@19271
    71
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@19271
    72
  | pretty_mixfix (Delimfix s) = parens [quoted s]
wenzelm@35130
    73
  | pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@35130
    74
  | pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@35130
    75
  | pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    76
  | pretty_mixfix (Binder (s, p1, p2)) =
wenzelm@19271
    77
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@19271
    78
  | pretty_mixfix Structure = parens [keyword "structure"];
wenzelm@19271
    79
wenzelm@19271
    80
end;
wenzelm@11920
    81
wenzelm@11920
    82
wenzelm@12531
    83
(* syntax specifications *)
wenzelm@384
    84
wenzelm@4053
    85
fun mixfix_args NoSyn = 0
wenzelm@37216
    86
  | mixfix_args (Mixfix (sy, _, _)) = Syn_Ext.mfix_args sy
wenzelm@37216
    87
  | mixfix_args (Delimfix sy) = Syn_Ext.mfix_args sy
wenzelm@37216
    88
  | mixfix_args (Infix (sy, _)) = 2 + Syn_Ext.mfix_args sy
wenzelm@37216
    89
  | mixfix_args (Infixl (sy, _)) = 2 + Syn_Ext.mfix_args sy
wenzelm@37216
    90
  | mixfix_args (Infixr (sy, _)) = 2 + Syn_Ext.mfix_args sy
wenzelm@18673
    91
  | mixfix_args (Binder _) = 1
wenzelm@18673
    92
  | mixfix_args Structure = 0;
wenzelm@4053
    93
wenzelm@22709
    94
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
wenzelm@22709
    95
  | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
wenzelm@22702
    96
wenzelm@4053
    97
wenzelm@384
    98
(* syn_ext_types *)
wenzelm@384
    99
wenzelm@37216
   100
fun make_type n = replicate n Syn_Ext.typeT ---> Syn_Ext.typeT;
wenzelm@35412
   101
wenzelm@14903
   102
fun syn_ext_types type_decls =
wenzelm@384
   103
  let
wenzelm@37216
   104
    fun mk_infix sy ty t p1 p2 p3 = Syn_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, t, [p1, p2], p3);
wenzelm@384
   105
wenzelm@35129
   106
    fun mfix_of (_, _, NoSyn) = NONE
wenzelm@37216
   107
      | mfix_of (t, ty, Mixfix (sy, ps, p)) = SOME (Syn_Ext.Mfix (sy, ty, t, ps, p))
wenzelm@37216
   108
      | mfix_of (t, ty, Delimfix sy) = SOME (Syn_Ext.Mfix (sy, ty, t, [], Syn_Ext.max_pri))
wenzelm@35412
   109
      | mfix_of (t, ty, Infix (sy, p)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p)
wenzelm@35412
   110
      | mfix_of (t, ty, Infixl (sy, p)) = SOME (mk_infix sy ty t p (p + 1) p)
wenzelm@35412
   111
      | mfix_of (t, ty, Infixr (sy, p)) = SOME (mk_infix sy ty t (p + 1) p p)
wenzelm@35351
   112
      | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);  (* FIXME printable t (!?) *)
wenzelm@384
   113
wenzelm@37216
   114
    fun check_args (_, ty, _) (SOME (mfix as Syn_Ext.Mfix (sy, _, _, _, _))) =
wenzelm@37216
   115
          if length (Term.binder_types ty) = Syn_Ext.mfix_args sy then ()
wenzelm@37216
   116
          else Syn_Ext.err_in_mfix "Bad number of type constructor arguments" mfix
wenzelm@35351
   117
      | check_args _ NONE = ();
wenzelm@35351
   118
wenzelm@35351
   119
    val mfix = map mfix_of type_decls;
wenzelm@35351
   120
    val _ = map2 check_args type_decls mfix;
wenzelm@35129
   121
    val xconsts = map #1 type_decls;
wenzelm@37216
   122
  in Syn_Ext.syn_ext (map_filter I mfix) xconsts ([], [], [], []) [] ([], []) end;
wenzelm@384
   123
wenzelm@384
   124
wenzelm@384
   125
(* syn_ext_consts *)
wenzelm@384
   126
wenzelm@15751
   127
val binder_stamp = stamp ();
wenzelm@21534
   128
val binder_name = suffix "_binder";
wenzelm@15751
   129
wenzelm@14903
   130
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   131
  let
wenzelm@384
   132
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@37216
   133
      [Syn_Ext.Mfix ("op " ^ sy, ty, c, [], Syn_Ext.max_pri),
wenzelm@37216
   134
       Syn_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   135
wenzelm@384
   136
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   137
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   138
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   139
wenzelm@35129
   140
    fun mfix_of (_, _, NoSyn) = []
wenzelm@37216
   141
      | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Syn_Ext.Mfix (sy, ty, c, ps, p)]
wenzelm@37216
   142
      | mfix_of (c, ty, Delimfix sy) = [Syn_Ext.Mfix (sy, ty, c, [], Syn_Ext.max_pri)]
wenzelm@35130
   143
      | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@35130
   144
      | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
wenzelm@35130
   145
      | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
wenzelm@35129
   146
      | mfix_of (c, ty, Binder (sy, p, q)) =
wenzelm@37216
   147
          [Syn_Ext.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
wenzelm@35351
   148
      | mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c);   (* FIXME printable c (!?) *)
wenzelm@384
   149
wenzelm@21534
   150
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
skalberg@15531
   151
      | binder _ = NONE;
wenzelm@384
   152
wenzelm@19482
   153
    val mfix = maps mfix_of const_decls;
wenzelm@35129
   154
    val xconsts = map #1 const_decls;
wenzelm@19482
   155
    val binders = map_filter binder const_decls;
wenzelm@35129
   156
    val binder_trs = binders
wenzelm@37216
   157
      |> map (Syn_Ext.stamp_trfun binder_stamp o apsnd K o Syn_Trans.mk_binder_tr);
wenzelm@35129
   158
    val binder_trs' = binders
wenzelm@37216
   159
      |> map (Syn_Ext.stamp_trfun binder_stamp o
wenzelm@37216
   160
          apsnd (K o Syn_Trans.non_typed_tr') o Syn_Trans.mk_binder_tr' o swap);
wenzelm@14903
   161
  in
wenzelm@37216
   162
    Syn_Ext.syn_ext' true is_logtype
wenzelm@14903
   163
      mfix xconsts ([], binder_trs, binder_trs', []) [] ([], [])
wenzelm@14903
   164
  end;
wenzelm@384
   165
wenzelm@384
   166
end;