安全地推广ST返回值 [英] Safely generalize ST return value

查看:99
本文介绍了安全地推广ST返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ST提取器 foo ::(forall s。ST s a) - >对于某些具体 a b ,b 。我可以安全地做另一个函数 fooC ::(forall s。ST s(a,c)) - > (b,c),这将允许我从ST计算中返回一些附加值?是否可以存在一个通用转换 addC ::((forall s。ST s a) - > b) - > (s1.S1 s1(a,c)) - > (b,c)可以安全使用吗?

I have an ST extractor foo :: (forall s . ST s a) -> b for some concrete a and b. Could I safely make another function fooC :: (forall s . ST s (a, c)) -> (b, c), which would allow me to return some additional value out of the ST computation? Can there be a generic transformation addC :: ((forall s . ST s a) -> b) -> (forall s1 . ST s1 (a,c)) -> (b,c) which would be safe to use?

推荐答案

我不确定我明白你想要完成什么,但是它有正确的类型:

I'm not really sure I understand what you're trying to accomplish, but this has the right type:

addC :: ((forall s . ST s a) -> b)
     -> (forall s1 . ST s1 (a,c)) -> (b,c)
addC f m = runST $ (\(a,c) -> (f (pure a), c)) <$> m

请注意(所有s。sa sa)同构于 a (如 pure runST ),所以也许你只需要一个函数 a - > b 和一个值(a,c)

Note that (forall s. ST s a) is isomorphic to a (as witnessed by pure and runST), so maybe you should just take a function a -> b and a value (a, c).

这篇关于安全地推广ST返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆