higher-rank-types

What uses have you found for higher-rank types in Haskell?

ⅰ亾dé卋堺 提交于 2019-12-03 02:46:35
问题 Higher rank types look like great fun. From the Haskell wikibook comes this example: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) Now we can evaluate foo id without the compiler exploding. This example is quickly followed in the book by the real-world example I have seen in a few other places: the ST monad and runST . That's pretty cool. But I have yet to come across a situation where I solve a problem by writing my own function with an argument of higher-rank. Have you?

What uses have you found for higher-rank types in Haskell?

别说谁变了你拦得住时间么 提交于 2019-12-02 16:21:26
Higher rank types look like great fun. From the Haskell wikibook comes this example: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) Now we can evaluate foo id without the compiler exploding. This example is quickly followed in the book by the real-world example I have seen in a few other places: the ST monad and runST . That's pretty cool. But I have yet to come across a situation where I solve a problem by writing my own function with an argument of higher-rank. Have you? What examples do you have of rank-2 or rank-n polymorphism in the wild? Take a look at functions like

How to asign a value from the IO monad to a RankNType qualified constructor

ε祈祈猫儿з 提交于 2019-12-02 13:30:17
问题 (UPDATED) I have made an interface using a Free Monad to a generic data store. I want to place the specific interpreter (:: DataStore a -> IO a) chosen by the user at run time into a state monad along with some other information. I cannot seem to put anything into this field in the data structure. How do I put a value into a field defined as a higher rank type? Below is a minimum example: {-# LANGUAGE RankNTypes, DeriveFunctor #-} data ProgramState = PS { -- line 3 [...] , storageInterface ::

STArray documentation for newbies and State/ST related questions

泪湿孤枕 提交于 2019-11-29 20:26:41
I have a hard time to understand STArray from the documentation and other howtos/discussion I've found through Google. I've got some more related questions below. According to the documentation, STArray s are Mutable boxed and unboxed arrays in the ST monad. This gave me the impression, that STArray is meant to be used as a state being passed around between functions (imagine you have a vector that has to be updated often). Apparently this is used differently though: ST s (STArray s a e) What is the state s here? If it is used internally, then why is this not hidden from the user? This also

What is “n” in RankNTypes

左心房为你撑大大i 提交于 2019-11-29 01:53:45
I understand how forall enables us to write polymorphic function. According to this chapter , the normal function which we generally write are Rank 1 types. And this function is of Rank 2 type: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) It explains like this: In general, a rank-n type is a function that has at least one rank-(n-1) argument but no arguments of even higher rank. What does it actually mean by rank argument ? Can somebody give an example of Rank 3 type which is similar to the above foo function. Rank is defined inductively on the structure of types: rank

Understanding a rank 2 type alias with a class constraint

不问归期 提交于 2019-11-28 23:36:07
I have code that frequently uses functions that look like foo :: (MyMonad m) => MyType a -> MyOtherType a -> ListT m a To try to shorten this, I wrote the following type alias: type FooT m a = (MyMonad m) => ListT m a GHC asked me to turn on Rank2Types (or RankNTypes), but didn't complain when I used the alias to shorten my code to foo :: MyType a -> MyOtherType a -> FooT m a By contrast, when I wrote another type alias type Bar a b = (Something a, SomethingElse b) => NotAsBar a b and used it in a negative position bar :: Bar a b -> InsertTypeHere GHC loudly yelled at me for being wrong. I

How to express existential types using higher rank (rank-N) type polymorphism?

南楼画角 提交于 2019-11-28 17:56:42
We're used to having universally quantified types for polymorphic functions. Existentially quantified types are used much less often. How can we express existentially quantified types using universal type quantifiers? It turns out that existential types are just a special case of Σ-types (sigma types). What are they? Sigma types Just as Π-types (pi types) generalise our ordinary function types, allowing the resulting type to depend on the value of its argument, Σ-types generalise pairs, allowing the type of second component to depend on the value of the first one. In a made-up Haskell-like

STArray documentation for newbies and State/ST related questions

荒凉一梦 提交于 2019-11-28 15:57:56
问题 I have a hard time to understand STArray from the documentation and other howtos/discussion I've found through Google. I've got some more related questions below. According to the documentation, STArray s are Mutable boxed and unboxed arrays in the ST monad. This gave me the impression, that STArray is meant to be used as a state being passed around between functions (imagine you have a vector that has to be updated often). Apparently this is used differently though: ST s (STArray s a e) What

How to express existential types using higher rank (rank-N) type polymorphism?

本小妞迷上赌 提交于 2019-11-27 10:55:59
问题 We're used to having universally quantified types for polymorphic functions. Existentially quantified types are used much less often. How can we express existentially quantified types using universal type quantifiers? 回答1: It turns out that existential types are just a special case of Σ-types (sigma types). What are they? Sigma types Just as Π-types (pi types) generalise our ordinary function types, allowing the resulting type to depend on the value of its argument, Σ-types generalise pairs,

What is the purpose of Rank2Types?

馋奶兔 提交于 2019-11-26 18:04:53
I am not really proficient in Haskell, so this might be a very easy question. What language limitation do Rank2Types solve? Don't functions in Haskell already support polymorphic arguments? Do not functions in Haskell already support polymorphic arguments? They do, but only of rank 1. This means that while you can write a function that takes different types of arguments without this extension, you can't write a function that uses its argument as different types in the same invocation. For example the following function can't be typed without this extension because g is used with different