对于一个单子(monad)M
,是否可以将A => M[B]
变成M[A => B]
?
我尝试过遵循这些类型,但没有成功,这让我认为这是不可能的,但我想我还是会问。另外,在 Hoogle 中搜索 a -> m b -> m (a -> b)
没有返回任何内容,因此我运气不佳。
最佳答案
实践
不,这是不可能完成的,至少不能以有意义的方式完成。
考虑这个 Haskell 代码
action :: Int -> IO String
action n = print n >> getLine
这需要 n
首先,打印它(此处执行 IO),然后从用户读取一行。
假设我们有一个假设的 transform :: (a -> IO b) -> IO (a -> b)
。然后作为心理实验,考虑:
action' :: IO (Int -> String)
action' = transform action
上面得提前做好所有的IO,才知道n
,然后返回一个纯函数。这不能等同于上面的代码。
为了强调这一点,请考虑下面这段无意义的代码:
test :: IO ()
test = do f <- action'
putStr "enter n"
n <- readLn
putStrLn (f n)
神奇的是,action'
应该提前知道用户接下来要输入什么! session 看起来像
42 (printed by action')
hello (typed by the user when getLine runs)
enter n
42 (typed by the user when readLn runs)
hello (printed by test)
这需要时间机器,所以无法完成。
理论上
不,这是不可能的。该参数类似于 the one I gave to a similar question .
矛盾假设transform :: forall m a b. Monad m => (a -> m b) -> m (a -> b)
存在。
专业m
到延续单子(monad) ((_ -> r) -> r)
(我省略了 newtype 包装器)。
transform :: forall a b r. (a -> (b -> r) -> r) -> ((a -> b) -> r) -> r
专业r=a
:
transform :: forall a b. (a -> (b -> a) -> a) -> ((a -> b) -> a) -> a
申请:
transform const :: forall a b. ((a -> b) -> a) -> a
根据 Curry-Howard 同构,以下是直觉同义反复
((A -> B) -> A) -> A
但这就是 PIL 士定律,在直觉逻辑中无法证明。矛盾。
关于scala - 将 A => M[B] 转换为 M[A => B],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27267848/