Questions tagged [impredicativetypes]

A rarely-used GHC type system extension that allows quantifiers to appear anywhere in a type.

20 questions
59
votes
2 answers

Impredicative types vs. plain old subtyping

A friend of mine posed a seemingly innocuous Scala language question last week that I didn't have a good answer to: whether there's an easy way to declare a collection of things belonging to some common typeclass. Of course there's no first-class…
29
votes
3 answers

Why can't the type of id be specialised to (forall a. a -> a) -> (forall b. b -> b)?

Take the humble identity function in Haskell, id :: forall a. a -> a Given that Haskell supposedly supports impredicative polymorphism, it seems reasonable that I should be able to "restrict" id to the type (forall a. a -> a) -> (forall b. b -> b)…
Tom Crockett
  • 28,680
  • 6
  • 68
  • 86
12
votes
1 answer

Why is impredicative polymorphism allowed only for functions in Haskell?

In Haskell I can't write f :: [forall a. a -> a] f = [id] because • Illegal polymorphic type: forall a. a -> a GHC doesn't yet support impredicative polymorphism But I can happily do f :: (forall a. a -> a) -> (a, b) -> (a, b) f i (x, y) = (i x,…
radrow
  • 4,248
  • 1
  • 19
  • 36
12
votes
1 answer

Impredicative polymorphism in F#

OCaml's Hindley-Milner type system does not allow for impredicative polymorphism (à la System-F), except through a somewhat recent extension for record types. The same applies to F#. It however is sometimes desirable to translate programs written…
David Monniaux
  • 1,564
  • 10
  • 21
12
votes
3 answers

Simple example for ImpredicativeTypes

The GHC user's guide describes the impredicative polymorphism extension with reference to the following example: f :: Maybe (forall a. [a] -> [a]) -> Maybe ([Int], [Char]) f (Just g) = Just (g [3], g "hello") f Nothing = Nothing However, when I…
Ben Millwood
  • 6,450
  • 21
  • 44
9
votes
2 answers

Row polymorphism in Haskell: trouble writing Forth DSL with "transformations"

I've been inspired by the recent Haskell blog activity1 to try my hand at writing a Forth-like DSL in Haskell. The approach I have taken is simultaneously straightforward and confusing: {-# LANGUAGE TypeOperators, RankNTypes, ImpredicativeTypes…
9
votes
2 answers

Why are higher rank types so fragile in Haskell

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) ->…
8
votes
2 answers

Why does this equivalent program not compile?

This program: {-# LANGUAGE RankNTypes, ImpredicativeTypes #-} import qualified Data.Vector.Mutable as MV import qualified Data.Vector as V import Control.Monad.ST import Control.Monad.Primitive unsafeModify :: [(forall s . MV.MVector s Int -> ST s…
MaiaVictor
  • 45,122
  • 42
  • 127
  • 254
8
votes
1 answer

How does let interact with higher rank types in Haskell?

I ran in to a puzzling situation with a higher rank type. I figured out how to make it work, but I don't understand the difference between the working and non-working versions. With these background definitions: {-# LANGUAGE RankNTypes #-} data…
Doug McClean
  • 13,739
  • 4
  • 43
  • 68
7
votes
1 answer

Practical Implications of runST vs unsafePerformIO

I want something like f :: [forall m. (Mutable v) (PrimState m) r -> m ()] -> v r -> v r -- illegal signature f gs x = runST $ do y <- thaw x foldM_ (\_ g -> g y) undefined gs -- you get the idea unsafeFreeze y I'm essentially in the same…
crockeea
  • 21,467
  • 10
  • 44
  • 93
6
votes
1 answer

How to non-impredicatively return `Maybe` a lens?

A type like Maybe (Lens' a b) doesn't work because Lens' is under the hood a Rank-2 type, that can't be wrapped in a type constructor without the -XImpredicativeTypes extension (which is not really supported in GHC). What is thus the best type to…
leftaroundabout
  • 101,764
  • 3
  • 156
  • 291
5
votes
1 answer

Why doesn't this typecheck?

This is toy-example.hs: {-# LANGUAGE ImpredicativeTypes #-} import Control.Arrow data From = From (forall a. Arrow a => a Int Char -> a [Int] String) data Fine = Fine (forall a. Arrow a => a Int Char -> a () String) data Broken = Broken (Maybe…
dave4420
  • 44,728
  • 6
  • 108
  • 146
4
votes
1 answer

"case" operator for System-F natural numbers coded with RankNTypes fails to typecheck

In Haskell, if one enables the RankNTypes extension {-# Language RankNTypes #-} then one can define the natural numbers as they are encoded in System-F: type Nat = forall a. a -> ((a -> a) -> a) zero :: Nat zero = \z s -> z succ ::…
4
votes
1 answer

What is the relationship between polymorphism's rank and (im)predicativity?

What is the relationship between polymorphism's rank and (im)predicativity? Can rank-1 polymorphism be either predicative or impredicative? Can rank-k polymorphism with k > 1 be either predicative or impredicative? My confusions come from: Why does…
2
votes
1 answer

Hold a lens in a record

I have a record MyRecord for which I create lenses with makeLenses. I would like to have a field in that record which holds itself a lens but that is also accessible with a lens like the other fields. This is my code to achieve this: {-# LANGUAGE…
F. Böller
  • 3,904
  • 2
  • 16
  • 30
1
2