javascript-MaybeT外部monad不受monad约束的应用实例
作者:互联网
我正在用Javascript实现Maybe(aka选项)类型的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〜< *,链〜=<< 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
这似乎正是我想要的,但是我无法识别纯(< *)< *>的评估顺序. abf< *> abs和哪个< *>运算符属于哪个应用层:
const optApT = (ap, of) => ttf => ttx =>
...?
任何提示表示赞赏.
解决方法:
希望这会有所帮助…
以下是与各种类型类函数关联的类型:
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
标签:monad-transformers,applicative,javascript,haskell 来源: https://codeday.me/bug/20191211/2105816.html