Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a fully polymorphic function?

Tags:

types

haskell

Would it be possible to have a completely type ambiguous function? Would the function have a type signature like:

Poly :: a -> a

where a is a type variable, like the syntax used with a type constructor declaration or typeclass requirement?

data TypeConstructor a = One | Two a
Func :: Num a => a -> a

Would it be possible to make a ubiquitous id function that always returns it's own value without having to know what value constructors are in use?

id :: a -> a
like image 203
Athan Clark Avatar asked Jun 15 '13 01:06

Athan Clark


1 Answers

Like others have said, Haskell functions are automatically polymorphic by default if they don't use any concrete features of the underlying type. If you open up ghci and type:

>>> let f x = x

... then ask it the type of f, it will automatically infer that f is completely polymorphic:

>>> :type f
f :: t -> t

Same thing if you use a file. You can just define:

f x = x

... and the compiler will infer that f has type a -> a. You can also explicitly annotate f, too:

f :: a -> a
f x = x
like image 111
Gabriella Gonzalez Avatar answered Sep 19 '22 23:09

Gabriella Gonzalez