Macro polytype::ptp
[−]
[src]
macro_rules! ptp { ($n:expr; $($t:tt)+) => { ... }; ($n:expr, $body:expr) => { ... }; ($n:expr, $($t:tt)+) => { ... }; ($($t:tt)+) => { ... }; }
Creates a TypeSchema
(convenience for common patterns).
ⓘThis example is not tested
// 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
:
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):
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:
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);