我可以拥有完全多态的功能吗?

Ath*_*ark 6 haskell types

是否有可能具有完全类型模糊的功能?该函数是否具有类型签名:

Poly :: a -> a
Run Code Online (Sandbox Code Playgroud)

其中a是类型变量,就像类型构造函数声明或类型类要求一样使用的语法?

data TypeConstructor a = One | Two a
Func :: Num a => a -> a
Run Code Online (Sandbox Code Playgroud)

是否有可能创建一个无处不在的id函数,始终返回它自己的值,而不必知道正在使用的构造函数是什么?

id :: a -> a
Run Code Online (Sandbox Code Playgroud)

Gab*_*lez 9

像其他人所说的那样,如果Haskell函数不使用底层类型的任何具体特性,则它们默认是自动多态的.如果打开ghci并输入:

>>> let f x = x
Run Code Online (Sandbox Code Playgroud)

...然后询问它的类型f,它会自动推断出它f是完全多态的:

>>> :type f
f :: t -> t
Run Code Online (Sandbox Code Playgroud)

如果您使用文件也一样.你可以定义:

f x = x
Run Code Online (Sandbox Code Playgroud)

...并且编译器将推断它f具有类型a -> a.您也可以明确注释f:

f :: a -> a
f x = x
Run Code Online (Sandbox Code Playgroud)