如何用类约束手动计算功能的类型?

发布于 2025-02-09 01:58:12 字数 270 浏览 2 评论 0原文

以下功能应用程序的键入规则

    f :: A -> B
    e :: A
    -----------
    f e :: B

没有考虑到类约束f。我如何手动计算类型,例如:

(+) :: Num a => a -> a -> a
3 :: Int
---------------------------
(+) 3 :: ?

The following typing rule for function application

    f :: A -> B
    e :: A
    -----------
    f e :: B

doesn't take into account an f with class constraints. How can I manually calculate the type of, for example:

(+) :: Num a => a -> a -> a
3 :: Int
---------------------------
(+) 3 :: ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2025-02-16 01:58:12

对于:

(+) :: Num a =>  a -> a -> a
3   ::          Int

我们知道a(第一个参数)与int3的类型)相同,因此这意味着<代码> a〜int (aint是相同的类型),因此这意味着:

(+)   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
(+) 3 :: Num Int =>     Int -> Int

由于int是成员在num typeclass中,我们可以删除num Int =&gt;,从而获得:

(+)   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
(+) 3 ::                Int -> Int

For:

(+) :: Num a =>  a -> a -> a
3   ::          Int

We know that a (the first parameter) is the same type as Int (the type of 3), so that means that a ~ Int (a and Int are the same type), so that means that:

(+)   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
(+) 3 :: Num Int =>     Int -> Int

Since Int is a member of the Num typeclass, we can remove Num Int =>, and thus obtain:

(+)   :: Num a =>  a ->  a  ->  a
3     ::          Int
------------------------------------
(+) 3 ::                Int -> Int
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文