Can I have a fully polymorphic function?

Is it possible to have a completely two-digit type function? Will the function have a type signature, for example:

Poly :: a -> a 

where a is a type variable, such as the syntax used with a type constructor declaration or a class requirement?

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

Is it possible to create an ubiquitous id function that always returns its own value without knowing which value constructors are used?

 id :: a -> a 
+6
source share
1 answer

Like others, Haskell functions are automatically polymorphic by default if they do not use any specific properties of the base type. If you open ghci and type:

 >>> let fx = x 

... then ask its type f , it will automatically infer that f completely polymorphic:

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

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

 fx = x 

... and the compiler concludes that f is of type a -> a . You can also explicitly annotate f too:

 f :: a -> a fx = x 
+9
source

Source: https://habr.com/ru/post/947348/


All Articles