gpt4 book ai didi

javascript - 外部 monad 没有 monad 约束的 MaybeT 应用实例

转载 作者:行者123 更新时间:2023-11-29 10:57:03 25 4
gpt4 key购买 nike

我正在实现 Maybe 的 monad 转换器(又名 Option )输入 Javascript(请注意,我使用类型字典传递):

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)

虽然这段代码有效,但我想知道我是否可以实现 optApT没有外部 monad 的 monad 约束。我偶然发现了这个 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 =>
...?

感谢任何提示。

最佳答案

希望这有助于...

以下是与各种类型类函数关联的类型:

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 - 外部 monad 没有 monad 约束的 MaybeT 应用实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com