문제

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 t => t -> t

so I remove the x's and get

>let sq = dup (*)
>:t sq
sq :: Integer -> Integer

what am I missing?

도움이 되었습니까?

해결책

You have run into the monomorphism restriction. Haskell will not infer polymorphic types for functions unless they are given in "function" style (not point free). This would mean that let sq = dup (*) would not type check, but Haskell has so called "default rules" for standard numeric classes that mean it defaults to the monomorphic type `Integer->Integer"

Prelude> :set -XNoMonomorphismRestriction
Prelude> let dup f x = f x x
Prelude> let sq = dup (*)
Prelude> :t sq
sq :: Num t => t -> t
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top