Crates.io | lifted |
lib.rs | lifted |
version | 0.1.0 |
source | src |
created_at | 2020-12-03 06:23:03.647429 |
updated_at | 2020-12-03 06:23:03.647429 |
description | Higher-kinded types in Rust. |
homepage | |
repository | https://git.sr.ht/~couch/lifted |
max_upload_size | |
id | 319230 |
size | 44,816 |
Higher-kinded types for Rust. Implementation based on modifications to Liebow-Feeser's method, winding up at the same scheme described by Yallop & White.
Say you'd like to write a higher-kinded trait such as Functor
. We can
imagine it might someday look a bit like this:
trait Functor<A> {
fn fmap<B, F: Fn(A) -> B>(me: Self<A>, f: F) -> Self<B>;
}
This isn't valid today, but with some minor changes we can write it:
use lifted::K1;
trait Functor {
fn fmap<A, B, F: Fn(A) -> B>(me: K1<Self, A>, f: F) -> K1<Self, B>;
}
There are two key visible changes, and one hidden one:
Functor
trait, we've
moved the type parameter to the method itself,Self<B>
, we use a K1<Self, B>
wrapper struct, andVec
), we implement it for the HKT form VecC
.(Note that, while the term HKT form is borrowed from generic_std
,
the use of them here was discovered independently. On further review,
the concept closely matches the brand from Yallop & White.)
The first two are more or less self-explanatory, but the third could use an illustration to clarify. Let's implement it!
use lifted::Kind1;
// Define the higher-kinded form of `Vec`
pub struct VecC;
// Specify how to construct a type instance
impl<T> Kind1<T> for VecC {
type Inner = Vec<T>;
}
impl Functor for VecC {
fn fmap<A, B, F: Fn(A) -> B>(me: K1<Self, A>, f: F) -> K1<Self, B> {
let me = me.into_inner(); // unwrap the Vec<A>
let you = me.map(f); // map the Vec<A> to a Vec<B>
K1::new(you) // rewrap the Vec<B>
}
}
For more details, please consult the API Documentation.
API docs are hosted on docs.rs:
This crate makes use of no exotic language features, and so can support even very old versions of stable Rust. It has been tested successfully with Rust 1.35. (Once we get around to setting up CI, we will verify this MSRV with CI testing).
generic_std
std
typesfp-core
HKT
trait constructs a type instance from another instance, macro to implementrats
higher
Lift
trait constructs a type instance from another instance, derive macro to implementkinder
Higher
trait constructs a type instance from another instance, macro to implementarchery
I'm happy to see any and all contributions, including bug reports, usability suggestions, patches, or angry yet well-intentioned rants. You are encouraged to report issues to the official issue tracker and send any questions or patches to the mailing list. Pull requests to the GitHub mirror are also acceptable.