Macro polytype::ptp [] [src]

macro_rules! ptp {
    ($n:expr; $body:expr) => { ... };
    ($n:expr, $body:expr) => { ... };
    ($n:expr, $($tail:tt)+) => { ... };
    ($t:expr) => { ... };
}

Creates a TypeSchema::Polytype or TypeSchema::Monotype (convenience for common pattern).

This example is not tested
// Equivalent to:
TypeSchema::Polytype(ident, tp)
// Or
TypeSchema::Monotype(tp)

Note: The syntax follows ptp!(0, 1,...n, typeschema) or ptp!(0, 1,...n; type) where n can be any number of quantified variables to be bound. See below for more details.

Examples

Make a monotype:

let t = ptp!(tp!(int));
assert_eq!(format!("{}", t), "int");
// Equivalent to:
let t_eq = TypeSchema::Monotype(Type::Constructed("int", vec![]));
assert_eq!(t, t_eq);

Make a bound type (from a TypeSchema):

let inner_polytype = ptp!(1; arrow![tp!(0), tp!(1), tp!(0)]);
let t = ptp!(0, inner_polytype);
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::Constructed(
                        "→",
                        vec![Type::Variable(0),
                             Type::Constructed(
                                 "→",
                                 vec![Type::Variable(1),
                                      Type::Variable(0)
                                 ]
                             )
                        ]
                    )
                )
            )
        }
    )
};
assert_eq!(t, t_eq);

Make a bound type (from a Type):

let t = ptp!(0; arrow![tp!(0), tp!(0)]);
assert_eq!(format!("{}", t), "∀t0. t0 → t0");
// Equivalent to:
let t_eq = TypeSchema::Polytype{
    variable: 0,
    body: Box::new(
        TypeSchema::Monotype(
            Type::Constructed(
                "→",
                vec![Type::Variable(0),
                     Type::Variable(0)]
            )
        )
    )
};
assert_eq!(t, t_eq);