monomorphism-restriction

Transforming a function to point-free style changes its type

吃可爱长大的小学妹 提交于 2019-12-05 11:43:52
问题 I'm beginning Haskell... I tried to write the following trivial function in two different ways, letting Haskell decide the types, and the type system does something different in each case. What is the explanation for that behavior? Prelude> let f x = 2 * x Prelude> let g = (2*) Prelude> :info f f :: Num a => a -> a -- Defined at <interactive>:1:5 Prelude> :info g g :: Integer -> Integer -- Defined at <interactive>:1:5 Thanks! 回答1: This is known as the monomorphism restriction. Basically, it

Haskell's type inference strangeness

十年热恋 提交于 2019-12-05 09:35:43
Look at this output from ghci: Prelude> :t Data.Map.lookup Data.Map.lookup :: Ord k => k -> Data.Map.Map k a -> Maybe a Prelude> :t flip Data.Map.lookup flip Data.Map.lookup :: Ord a => Data.Map.Map a a1 -> a -> Maybe a1 Prelude> let look = flip Data.Map.lookup Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Prelude> :t look look :: Data.Map.Map () a -> () -> Maybe a Why look 's inferred type differs from type of flip Data.Map.lookup ? To give you some context. Initially I had small program and was trying to figure out why it

Transforming a function to point-free style changes its type

空扰寡人 提交于 2019-12-03 23:28:50
I'm beginning Haskell... I tried to write the following trivial function in two different ways, letting Haskell decide the types, and the type system does something different in each case. What is the explanation for that behavior? Prelude> let f x = 2 * x Prelude> let g = (2*) Prelude> :info f f :: Num a => a -> a -- Defined at <interactive>:1:5 Prelude> :info g g :: Integer -> Integer -- Defined at <interactive>:1:5 Thanks! This is known as the monomorphism restriction . Basically, it means that top-level bindings that look like x = are forced to be non-polymorphic, unless you specify a type

Effects of monomorphism restriction on type class constraints

穿精又带淫゛_ 提交于 2019-12-03 22:54:47
This code breaks when a type declaration for baz is added: baz (x:y:_) = x == y baz [_] = baz [] baz [] = False A common explanation (see Why can't I declare the inferred type? for an example) is that it's because of polymorphic recursion. But that explanation doesn't explain why the effect disappears with another polymorphically recursive example: foo f (x:y:_) = f x y foo f [_] = foo f [] foo f [] = False It also doesn't explain why GHC thinks the recursion is monomorphic without type declaration. Can the explanation of the example with reads in http://www.haskell.org/onlinereport/decls.html

ghci - defaulting confusion

痴心易碎 提交于 2019-12-01 17:27:47
问题 I happened to see some strange behaviour during checking the size ( minBound , maxBound ) and "length in decimal representation" of different integral types. Using GHCi: Prelude> :{ Prelude| let mi = minBound Prelude| ma = maxBound Prelude| le = fromIntegral $ length $ show ma Prelude| in [mi,ma,le] :: [Int] Prelude| :} [-9223372036854775808,922372036854775807,2] ^ in the last place I would expect 19 . My first guess is that maxBound defaults to () and thus yields 2 , but I don't understand

Haskell multiply Int and real number

試著忘記壹切 提交于 2019-11-30 22:43:12
I have coefficient :: ??????? coefficient = 1.0 and val :: Int and I would like to do result :: ??????? result val coefficient = val * coefficient What type signatures and conversion functions do I need to do to make this work? What must I do on top of that if I want to have ability to generalize val to any kind of Num? This: coefficient = 1.0 val :: Int val = 3 result :: Num a => a result = coefficient * (fromIntegral val) gives me this compiler warning: Could not deduce (a ~ Double) from the context (Num a) bound by the type signature for result :: Num a => a at Move.hs:17:1-41 `a' is a

Why do 3 and x (which was assigned 3) have different inferred types in Haskell? [duplicate]

元气小坏坏 提交于 2019-11-26 19:00:59
This question already has an answer here: What is the monomorphism restriction? 1 answer Type inference in Haskell has a bit of a learning curve (to say the least!). A good way to start learning it is with simple examples. So, the following is a bit of a "hello world" for type inference. Consider the following example: Prelude> :t 3 3 :: (Num t) => t Prelude> let x = 3 Prelude> :t x x :: Integer The question is thus: Why do 3 and x have different types? Link Summary: Read the answers below for the full story; here's just a link summary: GHC type defaulting: Haskell Report section 4.3.4 GHCi's

Why are polymorphic values not inferred in Haskell?

可紊 提交于 2019-11-26 18:55:31
Numeric literals have a polymorphic type: *Main> :t 3 3 :: (Num t) => t But if I bind a variable to such a literal, the polymorphism is lost: x = 3 ... *Main> :t x x :: Integer If I define a function, on the other hand, it is of course polymorphic: f x = 3 ... *Main> :t f f :: (Num t1) => t -> t1 I could provide a type signature to ensure the x remains polymorphic: x :: Num a => a x = 3 ... *Main> :t x x :: (Num a) => a But why is this necessary? Why isn't the polymorphic type inferred? sepp2k It's the monomorphism restriction which says that all values, which are defined without parameters

:sprint for polymorphic values?

≡放荡痞女 提交于 2019-11-26 16:43:24
I am wondering why :sprint reports xs = _ in this case: Prelude> xs = map (+1) [1..10] Prelude> length xs 10 Prelude> :sprint xs xs = _ but not in this case: Prelude> xs = map (+1) [1..10] :: [Int] Prelude> length xs 10 Prelude> :sprint xs xs = [_,_,_,_,_,_,_,_,_,_] Note: I am running ghci with -XNoMonomorphismRestriction . Does it have to do with the fact that the type of xs is polymorphic in the first case but not in the second? I'd like to know what's going on internally. jozefg The gist is that the with the polymorphic xs it has a type of the form xs :: Num a => [a] typeclasses under the

Why do 3 and x (which was assigned 3) have different inferred types in Haskell? [duplicate]

六眼飞鱼酱① 提交于 2019-11-26 06:44:52
问题 This question already has an answer here : What is the monomorphism restriction? (1 answer) Closed 4 years ago . Type inference in Haskell has a bit of a learning curve (to say the least!). A good way to start learning it is with simple examples. So, the following is a bit of a \"hello world\" for type inference. Consider the following example: Prelude> :t 3 3 :: (Num t) => t Prelude> let x = 3 Prelude> :t x x :: Integer The question is thus: Why do 3 and x have different types? Link Summary: