haskell - 在 Haskell 中输入签名打高尔夫球(或不打高尔夫球)
问题描述
GHC 可以打出这个额外的类型注释还是带来一些真实的东西?
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module SOQuestionInstanceQuantification2 where
class IsomorphismFromTo a b where -- :~:
isofromto :: forall p. p a -> p b
to :: a -> b
--to = (isofromto) @((->) a) id -- Bad without (redundant ?) annotation
to = (isofromto :: (forall p. p a -> p b)) @((->) a) id -- Good with (redundant ?) annotation
编辑:删除多余的DefaultSignatures
解决方案
的真正签名isofromto
包括来自类的类型变量:
isofromto :: forall a b. IsomorphismFromTo a b => forall p. p a -> p b
因此,如果我们想明确p
,我们需要写
to = isofromto @a @b @((->) a) id
也传递这些论点。或者,
to = isofromto @_ @_ @((->) a) id
使这些论点被推断出来。
在更长的选择中
to = (isofromto :: (forall p. p a -> p b)) @((->) a) id
签名不是多余的,因为它修复了a
和b
类型参数。
请注意,在一般情况下,需要传递这些额外的参数,因为我们可能希望在 内调用来自另一个实例to @a @b
的实现,isofromto
例如isofromto @(a,b) @(b,a)
。
当然,Haskell 可以有一个符号来表示“isofromto
从当前实例调用”,没有额外的类型参数,但没有。我猜在常规编程中类型推断已经涵盖了大多数情况,当它还不够时,类型应用程序已经提供了一种选择我们需要的实例(包括当前实例)的方法,所以我想没有强烈需要这种特殊的符号.
推荐阅读
- wso2-am - 用于生产的 WSO2 3.0.0 数据库
- javascript - 对服务器的 HTTP 请求不更新标签
- testing - 如何在 Flutter 中测试渲染对象的内在大小
- sqlite - SQLite SUM 问题,它没有正确求和?
- asp.net - 如何针对跨站点脚本清理查询字符串输入 - 反映 ASP .Net(Web 窗体)应用程序中的问题?
- git - 将 git repo 与自己的 fork 合并,但保留所有移动/重命名文件的两个版本
- java - Spring boot:在类路径文件夹(类)之外的自定义文件夹中添加静态网页(Angular)
- java - 我怎样才能被包容的Letters成?
- php - 如何在 PHP 或 Laravel 中获取完整的 URL 路径“http://127.0.0.1:8000/admin/2#document”
- hadoop - Hadoop 2.8 NodeManager 在 Windows 上关闭