gpt4 book ai didi

haskell - 'try' 可以决定程序何时停止

转载 作者:行者123 更新时间:2023-12-02 06:56:14 24 4
gpt4 key购买 nike

我有这个功能:

isUndefined :: () -> Bool    
isUndefined x = case unsafePerformIO $ (try (return $! x) :: IO (Either SomeException ())) of
Left _ -> True
Right _ -> False

然后:

isUndefined () = False
isUndefined undefined = True

解决停机问题。当然,这也可以扩展到其他类型。

我的问题:这怎么可能? Control.Exception.try 真的会破坏这里的东西吗?

最佳答案

Is Control.Exception.try really breaking things here?

unsafePerformIO 破坏了这里的东西。在 GHC 中,undefined 只是引发异常而不是永远循环(这将无济于事)。异常并不意味着要在纯(非 IO)代码中捕获——事实上,类型系统确实会阻止您尝试那么多。

通过使用 unsafe* 函数,您告诉 GHC“忽略一切,我知道我在做什么”,现在所有的安全带都已关闭。帮自己一个忙,假装 unsafe* 东西不存在。

关于haskell - 'try' 可以决定程序何时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959054/

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