Haskell:从类型中推导函数 [英] Haskell: Deducing function from type

查看:123
本文介绍了Haskell:从类型中推导函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,函数的定义

  twoply ::(a  - > b,a  - > c) - > a  - > (b,c)

对于我来说显而易见(如果我不使用 undefined :: a )。



然后我想出了以下内容:

 ¢:: a  - > (a  - > b) - > b 
¢= flip($)

其中有趣的属性

 (¢)¢($):: a  - > (a  - > b) - > b 

这引出了我的问题。给定 = :: = 为与...具有相同类型的关系,那么语句 x = :: = xx($)唯一定义 x 的类型?必须 x = :: =¢,还是存在其他可能的类型 x



我尝试从 x = :: = xx($)反推以推导出 x :: a - > (a - > b) - >

对于 x = const ,xx($)也是如此,它的类型为 a - > b - >一个。所以它不能唯一标识这个类型。


So I was playing around with Haskell today, thinking about autogeneration of function definitions given a type.

For example, the definition of the function

twoply :: (a -> b, a -> c) -> a -> (b, c)

is obvious to me given the type (if I rule out use of undefined :: a).

So then I came up with the following:

¢ :: a -> (a ->b) -> b
¢ = flip ($)

Which has the interesting property that

(¢) ¢ ($) :: a -> (a -> b) -> b

Which brings me to my question. Given the relation =::= for "has the same type as", does the statement x =::= x x ($) uniquely define the type of x? Must x =::= ¢, or does there exist another possible type for x?

I've tried to work backward from x =::= x x ($) to deduce x :: a -> (a -> b) -> b, but gotten bogged down.

解决方案

x =::= x x ($) is also true for x = const, which has the type a -> b -> a. So it does not uniquely identify the type.

这篇关于Haskell:从类型中推导函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆