monomorphism-restriction

Why are polymorphic values not inferred in Haskell?

百般思念 提交于 2019-11-26 06:43:03
问题 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? 回答1:

:sprint for polymorphic values?

纵然是瞬间 提交于 2019-11-26 04:55:27
问题 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. 回答1: The gist

What is the monomorphism restriction?

喜夏-厌秋 提交于 2019-11-25 21:48:06
问题 I\'m puzzled by how the haskell compiler sometimes infers types that are less polymorphic than what I\'d expect, for example when using point-free definitions. It seems like the issue is the \"monomorphism restriction\", which is on by default on older versions of the compiler. Consider the following haskell program: {-# LANGUAGE MonomorphismRestriction #-} import Data.List(sortBy) plus = (+) plus\' x = (+ x) sort = sortBy compare main = do print $ plus\' 1.0 2.0 print $ plus 1.0 2.0 print $