> higher-rank type
Im not a type person. So here is my question, how many languages other then haskell have this?
Im not a type person. So here is my question, how many languages other then haskell have this?
For example, the following code needs Higher Ranked Types to typecheck:
id :: forall t. t -> t
id x = x
-- mk_pair's type is a rank-2 type, because its
-- f parameter is a polymorphic function that gets applied
-- to different types inside mk_pair.
mk_pair :: (forall t. t -> t) -> (Int, String)
mk_pair f = (f 10, f "str")
a_pair :: (Int, String)
a_pair = mk_pair id
But it can obviously be written in an untyped language if you wanted to. Just erase all those type annotations: function id(x){ return x}
function mk_pair(f){ return [f(10), f("hello")] }
var a_pair = mk_pair(id);
The deal with higher-ranked types is basically a tradeoff between the flexibility of the type system and the power of type inference. If you restrict yourself to rank-1 types (that is, none of your function arguments are polymorphic) then the compiler can infer all all types in the program without you having to write any type signatures. If you want to use higher ranked types then the compiler can't infer everything anymore so you might need to add some type annotations yourself.That said, I'm pretty sure you know this :)
type Transducer a b = forall r . (r -> b -> r) -> (r -> a -> r)
This may seems unusual or complex but is available in mainstream languages. In java: interface Transducer<A,B> {
<R> Reducer<B,R> transduce(Reducer<A,R> reducer);
}
See https://gist.github.com/didier-wenzek/07faa3990ff03fdea372 for a more complete java implementation of transducers. Transducer r a b = (r -> b -> r) -> (r -> a -> r)
but if you even accidentally specialize the `r` while constructing Transducers then you will have reduced their ability to generalize over input and output containers. In other words, it'll be up to you to maintain the universality of `r`. interface Transducer<A,B> {
Func<R,B,R> Apply<R>(Func<R,A,R> f);
}