Forgive my ignorance, but what can you do with higher-kinded types? Why introduce the [_] syntax? What would it help me do that I can’t already do in Rust? I really don’t understand the rationale or excitement here. At the moment it just seems like needless complexity. Please explain.
Without higher-kinded polymorphism, we would be able to write this for lists:
map :: (a -> b) -> [a] -> [b]
But higher-kind polymorphism allows abstraction, thus less code repetition. We could call a single “mapping” function when we want to map, instead of a new one per data type. In Haskell it’s called fmap which works for all functors:
fmap :: Functor f => (a -> b) -> f a -> f b
Where f is any type-constructor which implements Functor - not tied to list, like the first example.
So we could write another function, like so:
x :: Functor f => f Int -> f Bool
x = fmap (== 42)
And we can run the above on lists, maps, trees, functions, state, configuration, loggers, etc.
This would be especially useful for Rust’s traits (i.e. type classes) because then we could write a “functor” trait and just use the fmap function which would work for any type which implements the trait.
Out of curiosity, how much influence is there from Haskell (& friends) on the development of Rust as a language? I haven’t had time to do any nontrivial programming with Rust yet, but the sense that I get is that it has a lot of the good ideas from languages like Haskell, (e.g., powerful types, ‘free theorems’ from the compiler) but aimed at programming at a more ‘system’ level, where you want to have control over resources.
Is that sort of accurate (or is there a better way to express it?)?
I haven’t done much Rust but I’d say that’s only a little true. Rust’s type system is a little bit impressive, but the lack of higher kinded polymorphism, existential types, etc means it’s quite a bit less powerful than Haskell. Free theorems aren’t as powerful either, since Rust doesn’t feature effect tracking.
You’d be able to do the above in a language designed for systems development but the combination with Rust features would probably take time to figure out.
The biggest I think which relates Rust to Haskell would be type-classes.
Rust has existential types. They are called “trait objects.”
Rust’s type system is a little bit impressive
Its linear types make it very impressive IMO.
Thanks, those are both good points!
You see a lot of features traditionally found in functional programming languages like type inferencing, type classes, option types, pattern matching, map / filter / reduce, etc.
At the same time, Rust’s priority is a systems language so it has dropped / ignored certain features (e.g. tail call optimization, higher kinded polymorphism).