haskell - 在本地绑定中查找表达式的类型
问题描述
有没有办法shift
通过重写下面的实例来找到下面代码中的类型?当我尝试在下面调用它时找不到它的类型是因为它是本地绑定(在 where 构造中)吗?
class CoMonad m where
extract :: m a -> a
(<<=) :: (m a -> b) -> m a -> m b
instance (Monoid s) => CoMonad ((->) s) where
extract = ($ mempty)
f <<= r = \ t -> (f . shift t) r
where shift t rr = \ s -> rr (s <> t)
:t shift
<interactive>:1:1: error: Variable not in scope: shift
解决方案
一种可能性是shift
使用通配符给出不完整的签名:
class CoMonad m where
extract :: m a -> a
(<<=) :: (m a -> b) -> m a -> m b
instance (Monoid s) => CoMonad ((->) s) where
extract = ($ mempty)
f <<= r = \ t -> (f . shift t) r
where
shift :: _
shift t rr = \ s -> rr (s <> t)
这将无法通过有用的错误消息进行类型检查:
<interactive>:9:15: error:
• Found type wildcard ‘_’ standing for ‘s -> (s -> t) -> s -> t’
Where: ‘t’ is a rigid type variable bound by
the inferred type of shift :: s -> (s -> t) -> s -> t
at <interactive>:10:6-36
‘s’ is a rigid type variable bound by
the instance declaration
at <interactive>:5:10-39
To use the inferred type, enable PartialTypeSignatures
• In the type signature: shift :: _
In an equation for ‘<<=’:
f <<= r
= ...
where
...
In the instance declaration for ‘CoMonad ((->) s)’
• Relevant bindings include
r :: s -> a (bound at <interactive>:7:9)
f :: (s -> a) -> b (bound at <interactive>:7:3)
(<<=) :: ((s -> a) -> b) -> (s -> a) -> s -> b
(bound at <interactive>:7:5)
正如 Jon Purdy提醒我们的那样,部分签名也可以内联添加,作为类型注释:
(\ t -> (f . (shift :: _)) r
如果您实际上还没有添加本地绑定,则类型化的孔(_shift
在下面的示例中)将以类似的方式为您提供所缺少的任何内容的推断类型。
\ t -> (f . _shift) r
推荐阅读
- angular - 在 Azure App 服务上部署多个 Angular 单页应用程序
- javascript - 地图折线,停止绘图
- java - 如何将静态库链接到在 NDK Android Build 中用作本机库的源
- python - 根据其他列的值创建新列
- robotframework - 在机器人框架中保存终端的结果
- java - 如何使用适用于 Android 的广播接收器保存查询?
- php - 如何从数组中获取第一个键(0或关联)并在新数组中输出
- c# - 第 3 方同步方法在 .NET 4.7.2 WPF 中冻结 UI
- command-line-interface - 在 fzf 中显示结果之前和之后的行
- vue.js - 无法在主机组件中的组件上获取 DOM 事件