monomorphism-restriction

How to work around issue with ambiguity when monomorphic restriction turned *on*?

被刻印的时光 ゝ 提交于 2019-12-24 03:24:42
问题 So, learning Haskell, I came across the dreaded monomorphic restriction, soon enough, with the following (in ghci): Prelude> let f = print.show Prelude> f 5 <interactive>:3:3: No instance for (Num ()) arising from the literal `5' Possible fix: add an instance declaration for (Num ()) In the first argument of `f', namely `5' In the expression: f 5 In an equation for `it': it = f 5 So there's a bunch of material about this, e.g. here, and it is not so hard to workaround. I can either add an

Inferred generic function typechecks as a return type but not an argument type

*爱你&永不变心* 提交于 2019-12-24 01:59:30
问题 I'm learning about SYB and rank n types, and came across a confusing case of what seems like the monomorphism restriction. I wrote a function to find the shallowest entry that matches a predicate. Instead of a reducing function, I wanted to accept a more predicate-like function using Alternative , and transform it into a generic function myself. I decided to omit the type annotation in the let block to see how the monomorphism reduction would affect the type in this implementation: shallowest

Effects of monomorphism restriction on type class constraints

匆匆过客 提交于 2019-12-21 06:59:33
问题 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

Haskell multiply Int and real number

丶灬走出姿态 提交于 2019-12-19 03:44:28
问题 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

Why does changing sq to point-free change the type [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-11 02:23:23
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: What is going on with the types in this ghci session? To try and practice a bit of haskell and learn about point free I was playing around with a function to square a number so I started by defining >let dup f x = f x x so I could rewrite sq in terms of dup (without worrying about making dup point free for now) >let sq x = dup (*) x and checking the type of sq I see what I'm expecting to see >:t sq >sq :: Num

Haskell's type inference strangeness

扶醉桌前 提交于 2019-12-10 04:35:47
问题 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

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

When can I bind a function to another name?

有些话、适合烂在心里 提交于 2019-12-08 16:02:39
问题 When working in the interpreter, it's often convenient to bind a function to a name, for example: ghci> let f = (+1) ghci> f 1 2 This aliases the name f to the function (+1) . Simple. However, this doesn't always work. One example I've found which causes an error is trying to alias nub from the Data.List module. For example, ghci> :m Data.List ghci> nub [1,2,2,3,3,3] [1,2,3] ghci> let f = nub ghci> f [1,2,2,3,3,3] <interactive>:1:14: No instance for (Num ()) arising from the literal `3'

Specific type inference using uncurry function

大憨熊 提交于 2019-12-07 03:21:02
问题 I've been playing with the uncurry function in GHCi and I've found something I couldn't quite get at all. When I apply uncurry to the (+) function and bind that to some variable like in the code below, the compiler infers its type to be specific to Integer : Prelude> let add = uncurry (+) Prelude> :t add add :: (Integer, Integer) -> Integer However, when ask for the type of the following expression I get (what I expect to be) the correct result: Prelude> :t uncurry (+) uncurry (+) :: (Num a)

What is XNoMonomorphismRestriction?

纵然是瞬间 提交于 2019-12-06 17:38:54
问题 This page usages $ ghci -XNoMonomorphismRestriction to start the haskell interpreter. What does XNoMonomorphismRestriction switch mean? 回答1: It turns off the Monomorphism restriction, which restricts values which are not defined using "function notation"¹ to have a non-polymorphic type. ¹ By "not using function notation" I mean that they're defined as foo = something and not foo bar = something , i.e. the definition does not contain explicit arguments. 来源: https://stackoverflow.com/questions