1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/// Creates a [`Type`][] (convenience for common patterns).
///
/// Specifically, a `Type<&'static str>`, where all names are static strings.
///
/// ```rust,ignore
/// // Equivalent to:
/// Type::Constructed(ident, vec![
///     tp1,
///     tp2,
///     ...
/// ])
/// // or
/// Type::Variable(n)
/// // or
/// Type::arrow(
///     tp0,
///     Type::arrow(
///         tp1,
///         Type::arrow(
///             tp2,
///             ...,
///         )
///     )
/// )
/// ```
///
/// # Examples
///
/// Make a primitive type:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::Type;
/// # fn main() {
/// let t = tp!(int);
/// assert_eq!(format!("{}", t), "int");
/// // Equivalent to:
/// let t_eq = Type::Constructed("int", vec![]);
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// Make a variable type:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::Type;
/// # fn main() {
/// let t = tp!(0);
/// assert_eq!(format!("{}", t), "t0");
/// // Equivalent to:
/// let t_eq = Type::Variable(0);
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// Make a composite type:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::Type;
/// # fn main() {
/// let tint = tp!(int);
/// let tstr = tp!(str);
/// let t = tp!(dict(tstr, tint));
/// assert_eq!(format!("{}", t), "dict(str,int)");
/// // Equivalent to:
/// let t_eq = Type::Constructed("dict", vec![
///     Type::Constructed("str", vec![]),
///     Type::Constructed("int", vec![]),
/// ]);
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// Make an arrow:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::Type;
/// # fn main() {
/// let t = tp!(@arrow[Type::Variable(0), Type::Variable(1), Type::Variable(2)]);
/// assert_eq!(format!("{}", t), "t0 → t1 → t2");
/// // Equivalent to:
/// let t_eq = Type::arrow(
///     Type::Variable(0),
///     Type::arrow(
///         Type::Variable(1),
///         Type::Variable(2),
///     )
/// );
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// Nest them for more complex types:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::Type;
/// # fn main() {
/// // mapi: (int → α → β) → [α] → [β]
/// let t = tp!(@arrow[
///     tp!(@arrow[tp!(int), tp!(0), tp!(1)]),
///     tp!(list(tp!(0))),
///     tp!(list(tp!(1))),
/// ]);
/// assert_eq!(format!("{}", t), "(int → t0 → t1) → list(t0) → list(t1)");
/// # }
/// ```
///
/// [`Type`]: enum.Type.html
#[macro_export]
macro_rules! tp {
    ($n:ident) => ($crate::Type::Constructed(stringify!($n), Vec::new()));
    ($n:ident($($x:expr),*)) => {
        $crate::Type::Constructed(stringify!($n), vec![$($x),*])
    };
    ($n:ident($($x:expr,)*)) => (tp!($n($($x),*)));
    ($n:expr) => ($crate::Type::Variable($n) as $crate::Type<&'static str>);
    (@arrow[$x:expr]) => ($x as $crate::Type<&'static str>);
    (@arrow[$x:expr, $($xs:expr),*]) => (
        match ($x, tp!(@arrow[$($xs),+])) {
            (arg, ret) => $crate::Type::arrow(arg, ret)
        }
    );
    (@arrow[$x:expr, $($xs:expr,)*]) => (tp!(@arrow[$x, $($xs),*]))
}

/// Creates a [`TypeSchema`][] (convenience for common patterns).
///
/// Specifically, a `TypeSchema<&'static str>`, where all names are static strings.
///
/// ```rust,ignore
/// // Equivalent to:
/// TypeSchema::Monotype(tp)
/// // Or
/// TypeSchema::Polytype {
///     variable1,
///     body: Box::new(TypeSchema::Polytype {
///         variable2,
///         body: ...
///     })
/// }
/// ```
///
/// This behaves much like [`tp!`], but this gives a [`TypeSchema`] and you can
/// express quantified type variables in a prefixed comma-delimited list. There
/// are three usage patterns, shown in the examples below.
///
/// # Examples
///
/// If you don't want to do any quantification, using `ptp!` on its own is just
/// like wrapping [`tp!`] with a [`TypeSchema::Monotype`]:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::{Type, TypeSchema};
/// # fn main() {
/// let t = ptp!(dict(tp!(str), tp!(int)));
/// assert_eq!(format!("{}", t), "dict(str,int)");
/// // Equivalent to:
/// let t_eq = TypeSchema::Monotype(
///     Type::Constructed("dict", vec![
///         Type::Constructed("str", vec![]),
///         Type::Constructed("int", vec![]),
///     ])
/// );
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// If you want to do quantification over a known monotype, precede the type
/// with quantified variables follows by a semicolon `;` (note that the
/// subsequent monotype is treated like the [`tp!`] macro):
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::{Type, TypeSchema};
/// # fn main() {
/// let t = ptp!(0, 1; @arrow[tp!(0), tp!(1), tp!(0)]);
/// assert_eq!(format!("{}", t), "∀t0. ∀t1. t0 → t1 → t0");
/// // Equivalent to:
/// let t_eq = TypeSchema::Polytype {
///     variable: 0,
///     body: Box::new(TypeSchema::Polytype {
///         variable: 1,
///         body: Box::new(TypeSchema::Monotype(
///             Type::arrow(
///                 Type::Variable(0),
///                 Type::arrow(
///                     Type::Variable(1),
///                     Type::Variable(0),
///                 )
///             )
///         ))
///     })
/// };
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// If you want want do quantification over an existing [`TypeSchema`], use a
/// comma after the quantified variables:
///
/// ```
/// # #[macro_use] extern crate polytype;
/// # use polytype::{Type, TypeSchema};
/// # fn main() {
/// let inner = tp!(@arrow[tp!(0), tp!(1), tp!(0)]);
/// let t = ptp!(0, 1, TypeSchema::Monotype(inner.clone()));
/// assert_eq!(format!("{}", t), "∀t0. ∀t1. t0 → t1 → t0");
/// // Equivalent to:
/// let t_eq = TypeSchema::Polytype {
///     variable: 0,
///     body: Box::new(TypeSchema::Polytype {
///         variable: 1,
///         body: Box::new(TypeSchema::Monotype(inner))
///     })
/// };
/// assert_eq!(t, t_eq);
/// # }
/// ```
///
/// [`TypeSchema::Polytype`]: enum.TypeSchema.html#variant.Polytype
/// [`TypeSchema::Monotype`]: enum.TypeSchema.html#variant.Monotype
/// [`TypeSchema`]: enum.TypeSchema.html
/// [`Type`]: enum.Type.html
/// [`tp!`]: macro.tp.html
#[macro_export]
macro_rules! ptp {
    ($n:expr; $($t:tt)+) => {
        $crate::TypeSchema::Polytype {
            variable: $n,
            body: Box::new($crate::TypeSchema::Monotype(tp!($($t)+))),
        }
    };
    ($n:expr, $body:expr) => {
        $crate::TypeSchema::Polytype {
            variable: $n,
            body: Box::new($body),
        }
    };
    ($n:expr, $($t:tt)+) => {
        $crate::TypeSchema::Polytype {
            variable: $n,
            body: Box::new(ptp!($($t)+)),
        }
    };
    ($($t:tt)+) => {
        $crate::TypeSchema::Monotype(tp!($($t)+))
    };
}