higher-rank-types

Why are higher rank types so fragile in Haskell

邮差的信 提交于 2020-01-12 07:28:47
问题 I was messing around with the runST function. Which has type (forall s. ST s a) -> a and it seems like trying to use it in any way that isn't directly applying without any indirection breaks it in pretty nasty ways. runST :: (forall s. ST s a) -> a const :: a -> b -> a so by substituting a in const for forall s. ST s a you should get the type of const runST const runST :: b -> (forall s. ST s a) -> a but instead GHC says that it can't match a with (forall s. ST s a) -> a but since a literally

Juggling existentials without unsafeCoerce

别说谁变了你拦得住时间么 提交于 2020-01-01 04:47:08
问题 Lately I have been playing with this type, which I understand to be an encoding of the free distributive functor (for tangential background on that, see this answer): data Ev g a where Ev :: ((g x -> x) -> a) -> Ev g a deriving instance Functor (Ev g) The existential constructor ensures I can only consume an Ev g by supplying a polymorphic extractor forall x. g x -> x , and that the lift and lower functions of the free construction can be given compatible types: runEv :: Ev g a -> (forall x.

How should the general type of a “lemma” function be understood?

烈酒焚心 提交于 2020-01-01 02:39:10
问题 Perhaps this is a stupid question. Here's a quote from the Hasochism paper: One approach to resolving this issue is to encode lemmas, given by parameterised equations, as Haskell functions. In general, such lemmas may be encoded as functions of type: ∀ x1 ... xn. Natty x1 → ... → Natty xn → ((l ~ r) ⇒ t) → t I thought I understood RankNTypes , but I can't make sense of the last part of this proposition. I'm reading it informally as "given a term which requires l ~ r , return that term". I'm

What is “n” in RankNTypes

守給你的承諾、 提交于 2019-12-29 05:19:24
问题 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

What is the purpose of Rank2Types?

依然范特西╮ 提交于 2019-12-27 12:07:37
问题 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? 回答1: 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.

The case of the disappearing constraint: Oddities of a higher-rank type

十年热恋 提交于 2019-12-23 07:25:38
问题 All the experiments described below were done with GHC 8.0.1. This question is a follow-up to RankNTypes with type aliases confusion. The issue there boiled down to the types of functions like this one... {-# LANGUAGE RankNTypes #-} sleight1 :: a -> (Num a => [a]) -> a sleight1 x (y:_) = x + y ... which are rejected by the type checker... ThinAir.hs:4:13: error: * No instance for (Num a) arising from a pattern Possible fix: add (Num a) to the context of the type signature for: sleight1 :: a -

Kind vs Rank in type theory

跟風遠走 提交于 2019-12-20 12:36:11
问题 I'm having a hard time understanding Higher Kind vs Higher Rank types. Kind is pretty simple (thanks Haskell literature for that) and I used to think rank is like kind when talking about types but apparently not! I read the Wikipedia article to no avail. So can someone please explain what is a Rank? and what is meant by Higher Rank? Higher Rank Polymorphism? how that comes to Kinds (if any) ? Comparing Scala and Haskell would be awesome too. 回答1: The concept of rank is not really related to

Why class constraint in type synonym needs RankNTypes

烈酒焚心 提交于 2019-12-18 13:58:07
问题 This compiles fine: type List a = [a] But when I introduce a class constraint, the compiler asks for RankNTypes to be included: type List2 a = Num a => [a] After including that extension, it compiles fine. Why is that extension required for compiling the code ? Edit: Why do I need the constraint in the first place ? I was inspecting this Lens type ( type RefF a b = Functor f => (b -> f b) -> (a -> f a) ) from this post and found out that it actually needed RankNTypes because of the Functor

Understanding a rank 2 type alias with a class constraint

試著忘記壹切 提交于 2019-12-18 02:43:10
问题 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

Understanding a rank 2 type alias with a class constraint

痴心易碎 提交于 2019-12-18 02:42:30
问题 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