higher-rank-types

Heterogeneous map

你。 提交于 2019-12-12 10:38:43
问题 I need a map which can contain arbitrary values as long as their types are of the same typeclass. My first naive approach was something like this: type HMap = forall a . MyClass a => M.Map Int a but it does not seem to work: the following code gives a compile error: testFunction :: (forall a . MyClass a => M.Map Int a) -> Int -> IO () testFunction m i = do case M.lookup i m of Nothing -> return () Just v -> someActionFromMyClass v >> putStrLn "OK" Ambiguous type variable `a0' in the

Using a monadic rank-2 type

走远了吗. 提交于 2019-12-10 14:22:16
问题 Here's the code: {-# LANGUAGE RankNTypes, FlexibleContexts, ScopedTypeVariables #-} module Foo where import Data.Vector.Generic.Mutable as M import Data.Vector.Generic as V import Control.Monad.ST import Control.Monad.Primitive import Control.Monad data DimFun v s r = DimFun {dim::Int, func :: v (PrimState s) r -> s ()} runFun :: (Vector v r) => (forall s . (PrimMonad s) => DimFun (Mutable v) s r) -> v r -> v r runFun t x = runST $ do y <- thaw x evalFun t y unsafeFreeze y evalFun ::

RankNTypes doesn't match return type

梦想与她 提交于 2019-12-10 11:16:28
问题 Using RankNTypes , I define a type that doesn't depend on a type variable. Is this the right way to go around the case below? I need to define a few functions to be used inside ST s , which, of course, doesn't depend on s . Yet, this causes a problem that an expression of Exp with two Int s applied to it doesn't result in a Block . Why? Here is a reproducer: import Control.Monad.ST import Data.Vector.Unboxed (Vector) import qualified Data.Vector.Unboxed as U import Data.Vector.Unboxed.Mutable

RankNTypes and scope of `forall'

依然范特西╮ 提交于 2019-12-09 09:23:48
问题 What is the difference between these? {-# LANGUAGE RankNTypes #-} f :: forall a. a -> Int f _ = 1 g :: (forall a. a) -> Int g _ = 1 In particular, why do I get an error with g () ? ghci> f () 1 ghci> g () <interactive>:133:3: Couldn't match expected type `a' with actual type `()' `a' is a rigid type variable bound by a type expected by the context: a at <interactive>:133:1 In the first argument of `g', namely `()' In the expression: g () In an equation for `it': it = g () ghci> f undefined 1

Transducers in Haskell and the monomorphism restriction

别说谁变了你拦得住时间么 提交于 2019-12-08 20:36:18
问题 I implemented transducers in Haskell as follows: {-# LANGUAGE RankNTypes #-} import Prelude hiding (foldr) import Data.Foldable type Reducer b a = a -> b -> b type Transducer a b = forall t. Reducer t b -> Reducer t a class Foldable c => Collection c where insert :: a -> c a -> c a empty :: c a reduce :: Collection c => Transducer a b -> c a -> c b reduce f = foldr (f insert) empty mapping :: (a -> b) -> Transducer a b mapping f g x = g (f x) Now I want to define a generic map function. Hence

RankNTypes with type aliases confusion [duplicate]

荒凉一梦 提交于 2019-12-07 18:06:33
问题 This question already has answers here : Understanding a rank 2 type alias with a class constraint (2 answers) Closed 3 years ago . I'm trying to understand how type constraints work with type aliases. First, let's assume I have next type alias: type NumList a = Num a => [a] And I have next function: addFirst :: a -> NumList a -> NumList addFirst x (y:_) = x + y This function fails with next error: Type.hs:9:13: error: • No instance for (Num a) arising from a pattern Possible fix: add (Num a)

List of existentially quantified values in Haskell

一个人想着一个人 提交于 2019-12-07 11:14:29
问题 I'm wondering why this piece of code doesn't type-check: {-# LANGUAGE ScopedTypeVariables, Rank2Types, RankNTypes #-} {-# OPTIONS -fglasgow-exts #-} module Main where foo :: [forall a. a] foo = [1] ghc complains: Could not deduce (Num a) from the context () arising from the literal `1' at exist5.hs:7:7 Given that: Prelude> :t 1 1 :: (Num t) => t Prelude> it seems that the (Num t) context can't match the () context of arg. The point I can't understand is that since () is more general than (Num

Are there any advantages of using Rank2Types in favor of RankNTypes?

核能气质少年 提交于 2019-12-06 18:18:11
问题 As far as I know, a decidable type checking algorithm exists (only) for rank-2 types. Does GHC use somehow this fact, and does it have any practical implications? Is there also a notion of principal types for rank-2 types, and a type inference algorithm? If yes, does GHC use it? Are there any other advantages of rank-2 types over rank- n types? 回答1: Rank2Types is a synonym for RankNTypes . So right now there are no advantages of rank-2 over rank-n. 回答2: In principle type checking is decidable

Referential transparency with polymorphism in Haskell

我的未来我决定 提交于 2019-12-06 17:12:21
问题 Say I have a function: f :: Int -> (Rational, Integer) f b = ((toRational b)+1,(toInteger b)+1) I want to abstract away the (+1) like so: f :: Int -> (Rational, Integer) f b = (h (toRational b) ,h (toInteger b)) where h = (+1) This wont work obviously, but if I specify the type signature it will work: f :: Int -> (Rational, Integer) f b = (h (toRational b) ,h (toInteger b)) where h :: Num a => a -> a h = (+1) Say I now want to further abstract the function by passing h as a parameter: f ::

RankNTypes doesn't match return type

☆樱花仙子☆ 提交于 2019-12-06 07:50:36
Using RankNTypes , I define a type that doesn't depend on a type variable. Is this the right way to go around the case below? I need to define a few functions to be used inside ST s , which, of course, doesn't depend on s . Yet, this causes a problem that an expression of Exp with two Int s applied to it doesn't result in a Block . Why? Here is a reproducer: import Control.Monad.ST import Data.Vector.Unboxed (Vector) import qualified Data.Vector.Unboxed as U import Data.Vector.Unboxed.Mutable (STVector) import qualified Data.Vector.Unboxed.Mutable as UM type Exp = Int -> Int -> Block type