外部 monad 没有 monad 约束的 MaybeT 应用实例
MaybeT applicative instance without monad constraint for the outer monad
我正在 Javascript 中实现 Maybe
(又名 Option
)类型的 monad 转换器(请注意我使用类型字典传递):
const optOfT = of => x =>
of(optOf(x));
const optMapT = map => f => ttx =>
map(optMap(f)) (ttx);
const optApT = chain => ttf => ttx =>
chain(tf =>
chain(tx =>
optAp(tf) (tx)) (ttx)) (ttf);
const optChainT = chain => fm => mmx =>
chain(mx =>
optChain(fm) (mx)) (mmx);
(map ~ <$>
, ap ~ <*>
, chain ~ =<<
, of = pure/return)
虽然这段代码有效,但我想知道我是否可以在没有外部 monad 的 monad 约束的情况下实现 optApT
。我偶然发现了这个 Haskell 示例:
(<<**>>) :: (Applicative a, Applicative b) => a (b (s -> t)) -> a (b s) -> a (b t)
abf <<**>> abs = pure (<*>) <*> abf <*> abs
这似乎正是我想要的,但我无法识别 pure (<*>) <*> abf <*> abs
的评估顺序以及哪个 <*>
运算符属于哪个应用层:
const optApT = (ap, of) => ttf => ttx =>
...?
如有任何提示,我们将不胜感激。
希望这对您有所帮助...
以下是与各种类型-class 函数关联的类型:
abf <<**>> abs = pure (<*>) <*> abf <*> abs
(4) (3) (2) (1)
(1), (2): the `ap` for type a
(3): the `ap` for type b
(4): the `pure` for type a
评价顺序为:
(pure (<*>)) <*> abf <*> abs
我正在 Javascript 中实现 Maybe
(又名 Option
)类型的 monad 转换器(请注意我使用类型字典传递):
const optOfT = of => x =>
of(optOf(x));
const optMapT = map => f => ttx =>
map(optMap(f)) (ttx);
const optApT = chain => ttf => ttx =>
chain(tf =>
chain(tx =>
optAp(tf) (tx)) (ttx)) (ttf);
const optChainT = chain => fm => mmx =>
chain(mx =>
optChain(fm) (mx)) (mmx);
(map ~ <$>
, ap ~ <*>
, chain ~ =<<
, of = pure/return)
虽然这段代码有效,但我想知道我是否可以在没有外部 monad 的 monad 约束的情况下实现 optApT
。我偶然发现了这个 Haskell 示例:
(<<**>>) :: (Applicative a, Applicative b) => a (b (s -> t)) -> a (b s) -> a (b t)
abf <<**>> abs = pure (<*>) <*> abf <*> abs
这似乎正是我想要的,但我无法识别 pure (<*>) <*> abf <*> abs
的评估顺序以及哪个 <*>
运算符属于哪个应用层:
const optApT = (ap, of) => ttf => ttx =>
...?
如有任何提示,我们将不胜感激。
希望这对您有所帮助...
以下是与各种类型-class 函数关联的类型:
abf <<**>> abs = pure (<*>) <*> abf <*> abs
(4) (3) (2) (1)
(1), (2): the `ap` for type a
(3): the `ap` for type b
(4): the `pure` for type a
评价顺序为:
(pure (<*>)) <*> abf <*> abs