Haskell 在本地绑定中查找表达式的类型
有没有办法通过重写下面的实例,在下面的代码中找到Haskell 在本地绑定中查找表达式的类型,haskell,Haskell,有没有办法通过重写下面的实例,在下面的代码中找到shift的类型?是因为它是本地绑定(在where构造中),所以在下面尝试调用它时找不到它的类型的原因吗 class CoMonad m其中 摘录::m a->a (硕士->硕士 实例(幺半群s)=>CoMonad((->)s),其中 摘录=($mempty) f rr(s t) :t移位 :1:1:错误:变量不在范围内:shift 一种可能性是使用通配符给shift一个不完整的签名: class CoMonad m where extract
shift
的类型?是因为它是本地绑定(在where构造中),所以在下面尝试调用它时找不到它的类型的原因吗
class CoMonad m其中
摘录::m a->a
(硕士->硕士
实例(幺半群s)=>CoMonad((->)s),其中
摘录=($mempty)
f rr(s t)
:t移位
:1:1:错误:变量不在范围内: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)
如果您还没有实际添加本地绑定,那么键入的孔(在下面的示例中为shift)将以类似的方式为您提供缺少的任何内容的推断类型
\ t -> (f . _shift) r
您还可以内联写入通配符(
\t->(f.(shift::)))r
)或将术语替换为键入的孔(\t->(f.\u shift)r
)
<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)
(\ t -> (f . (shift :: _)) r
\ t -> (f . _shift) r