src/Pure/Syntax/mixfix.ML
author wenzelm
Fri Apr 08 15:02:11 2011 +0200 (2011-04-08)
changeset 42288 2074b31650e6
parent 42287 d98eb048a2e4
child 42293 6cca0343ea48
permissions -rw-r--r--
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
clarified Syntax.root;
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@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@2199
    18
end;
wenzelm@384
    19
wenzelm@42287
    20
signature MIXFIX =
wenzelm@2199
    21
sig
wenzelm@42287
    22
  include BASIC_MIXFIX
wenzelm@19271
    23
  val pretty_mixfix: mixfix -> Pretty.T
wenzelm@4823
    24
  val mixfix_args: mixfix -> int
wenzelm@22702
    25
  val mixfixT: mixfix -> typ
wenzelm@35412
    26
  val make_type: int -> typ
wenzelm@42287
    27
  val binder_name: string -> string
wenzelm@42288
    28
  val syn_ext_types: (string * typ * mixfix) list -> Syntax_Ext.syn_ext
wenzelm@42288
    29
  val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext
wenzelm@2199
    30
end;
wenzelm@2199
    31
wenzelm@15751
    32
structure Mixfix: MIXFIX =
wenzelm@384
    33
struct
wenzelm@384
    34
wenzelm@384
    35
(** mixfix declarations **)
wenzelm@384
    36
wenzelm@384
    37
datatype mixfix =
wenzelm@384
    38
  NoSyn |
wenzelm@384
    39
  Mixfix of string * int list * int |
wenzelm@384
    40
  Delimfix of string |
wenzelm@35130
    41
  Infix of string * int |
wenzelm@35130
    42
  Infixl of string * int |
wenzelm@35130
    43
  Infixr of string * int |
wenzelm@18673
    44
  Binder of string * int * int |
wenzelm@18673
    45
  Structure;
wenzelm@384
    46
wenzelm@384
    47
wenzelm@19271
    48
(* pretty_mixfix *)
wenzelm@19271
    49
wenzelm@19271
    50
local
wenzelm@11920
    51
wenzelm@19271
    52
val quoted = Pretty.quote o Pretty.str;
wenzelm@19271
    53
val keyword = Pretty.keyword;
wenzelm@19271
    54
val parens = Pretty.enclose "(" ")";
wenzelm@19271
    55
val brackets = Pretty.enclose "[" "]";
wenzelm@19271
    56
val int = Pretty.str o string_of_int;
wenzelm@19271
    57
wenzelm@19271
    58
in
wenzelm@11920
    59
wenzelm@19271
    60
fun pretty_mixfix NoSyn = Pretty.str ""
wenzelm@19271
    61
  | pretty_mixfix (Mixfix (s, ps, p)) =
wenzelm@19271
    62
      parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p])
wenzelm@19271
    63
  | pretty_mixfix (Delimfix s) = parens [quoted s]
wenzelm@35130
    64
  | pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p])
wenzelm@35130
    65
  | pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@35130
    66
  | pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p])
wenzelm@19271
    67
  | pretty_mixfix (Binder (s, p1, p2)) =
wenzelm@19271
    68
      parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2])
wenzelm@19271
    69
  | pretty_mixfix Structure = parens [keyword "structure"];
wenzelm@19271
    70
wenzelm@19271
    71
end;
wenzelm@11920
    72
wenzelm@11920
    73
wenzelm@12531
    74
(* syntax specifications *)
wenzelm@384
    75
wenzelm@4053
    76
fun mixfix_args NoSyn = 0
wenzelm@42288
    77
  | mixfix_args (Mixfix (sy, _, _)) = Syntax_Ext.mfix_args sy
wenzelm@42288
    78
  | mixfix_args (Delimfix sy) = Syntax_Ext.mfix_args sy
wenzelm@42288
    79
  | mixfix_args (Infix (sy, _)) = 2 + Syntax_Ext.mfix_args sy
wenzelm@42288
    80
  | mixfix_args (Infixl (sy, _)) = 2 + Syntax_Ext.mfix_args sy
wenzelm@42288
    81
  | mixfix_args (Infixr (sy, _)) = 2 + Syntax_Ext.mfix_args sy
wenzelm@18673
    82
  | mixfix_args (Binder _) = 1
wenzelm@18673
    83
  | mixfix_args Structure = 0;
wenzelm@4053
    84
wenzelm@22709
    85
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT
wenzelm@22709
    86
  | mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT;
wenzelm@22702
    87
wenzelm@4053
    88
wenzelm@384
    89
(* syn_ext_types *)
wenzelm@384
    90
wenzelm@42288
    91
fun make_type n = replicate n Syntax_Ext.typeT ---> Syntax_Ext.typeT;
wenzelm@35412
    92
wenzelm@14903
    93
fun syn_ext_types type_decls =
wenzelm@384
    94
  let
wenzelm@42288
    95
    fun mk_infix sy ty t p1 p2 p3 = Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, t, [p1, p2], p3);
wenzelm@384
    96
wenzelm@35129
    97
    fun mfix_of (_, _, NoSyn) = NONE
wenzelm@42288
    98
      | mfix_of (t, ty, Mixfix (sy, ps, p)) = SOME (Syntax_Ext.Mfix (sy, ty, t, ps, p))
wenzelm@42288
    99
      | mfix_of (t, ty, Delimfix sy) = SOME (Syntax_Ext.Mfix (sy, ty, t, [], Syntax_Ext.max_pri))
wenzelm@35412
   100
      | mfix_of (t, ty, Infix (sy, p)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p)
wenzelm@35412
   101
      | mfix_of (t, ty, Infixl (sy, p)) = SOME (mk_infix sy ty t p (p + 1) p)
wenzelm@35412
   102
      | mfix_of (t, ty, Infixr (sy, p)) = SOME (mk_infix sy ty t (p + 1) p p)
wenzelm@42130
   103
      | mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t);
wenzelm@384
   104
wenzelm@42288
   105
    fun check_args (_, ty, _) (SOME (mfix as Syntax_Ext.Mfix (sy, _, _, _, _))) =
wenzelm@42288
   106
          if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then ()
wenzelm@42288
   107
          else Syntax_Ext.err_in_mfix "Bad number of type constructor arguments" mfix
wenzelm@35351
   108
      | check_args _ NONE = ();
wenzelm@35351
   109
wenzelm@35351
   110
    val mfix = map mfix_of type_decls;
wenzelm@35351
   111
    val _ = map2 check_args type_decls mfix;
wenzelm@35129
   112
    val xconsts = map #1 type_decls;
wenzelm@42288
   113
  in Syntax_Ext.syn_ext (map_filter I mfix) xconsts ([], [], [], []) ([], []) end;
wenzelm@384
   114
wenzelm@384
   115
wenzelm@384
   116
(* syn_ext_consts *)
wenzelm@384
   117
wenzelm@15751
   118
val binder_stamp = stamp ();
wenzelm@21534
   119
val binder_name = suffix "_binder";
wenzelm@15751
   120
wenzelm@14903
   121
fun syn_ext_consts is_logtype const_decls =
wenzelm@384
   122
  let
wenzelm@384
   123
    fun mk_infix sy ty c p1 p2 p3 =
wenzelm@42288
   124
      [Syntax_Ext.Mfix ("op " ^ sy, ty, c, [], Syntax_Ext.max_pri),
wenzelm@42288
   125
       Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)];
wenzelm@384
   126
wenzelm@384
   127
    fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) =
wenzelm@384
   128
          [Type ("idts", []), ty2] ---> ty3
wenzelm@14903
   129
      | binder_typ c _ = error ("Bad type of binder: " ^ quote c);
wenzelm@384
   130
wenzelm@35129
   131
    fun mfix_of (_, _, NoSyn) = []
wenzelm@42288
   132
      | mfix_of (c, ty, Mixfix (sy, ps, p)) = [Syntax_Ext.Mfix (sy, ty, c, ps, p)]
wenzelm@42288
   133
      | mfix_of (c, ty, Delimfix sy) = [Syntax_Ext.Mfix (sy, ty, c, [], Syntax_Ext.max_pri)]
wenzelm@35130
   134
      | mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p
wenzelm@35130
   135
      | mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p
wenzelm@35130
   136
      | mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p
wenzelm@35129
   137
      | mfix_of (c, ty, Binder (sy, p, q)) =
wenzelm@42288
   138
          [Syntax_Ext.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)]
wenzelm@42130
   139
      | mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c);
wenzelm@384
   140
wenzelm@21534
   141
    fun binder (c, _, Binder _) = SOME (binder_name c, c)
skalberg@15531
   142
      | binder _ = NONE;
wenzelm@384
   143
wenzelm@19482
   144
    val mfix = maps mfix_of const_decls;
wenzelm@35129
   145
    val xconsts = map #1 const_decls;
wenzelm@19482
   146
    val binders = map_filter binder const_decls;
wenzelm@35129
   147
    val binder_trs = binders
wenzelm@42288
   148
      |> map (Syntax_Ext.stamp_trfun binder_stamp o apsnd K o Syntax_Trans.mk_binder_tr);
wenzelm@35129
   149
    val binder_trs' = binders
wenzelm@42288
   150
      |> map (Syntax_Ext.stamp_trfun binder_stamp o
wenzelm@42284
   151
          apsnd (K o Syntax_Trans.non_typed_tr') o Syntax_Trans.mk_binder_tr' o swap);
wenzelm@14903
   152
  in
wenzelm@42288
   153
    Syntax_Ext.syn_ext' true is_logtype
wenzelm@42268
   154
      mfix xconsts ([], binder_trs, binder_trs', []) ([], [])
wenzelm@14903
   155
  end;
wenzelm@384
   156
wenzelm@384
   157
end;
wenzelm@42287
   158
wenzelm@42287
   159
structure Basic_Mixfix: BASIC_MIXFIX = Mixfix;
wenzelm@42287
   160
open Basic_Mixfix;
wenzelm@42287
   161