gpt4 book ai didi

haskell - 如何评估 `fix f = let {x = f x} in x`?

转载 作者:行者123 更新时间:2023-12-03 16:54:45 26 4
gpt4 key购买 nike

fix f = let {x = f x} in xlet , 我以为 let P = Q in R将评估 Q -> Q' 然后 P 被 R 中的 Q' 替换,或者:R[P -> Q'] .
但是在 fix定义Q取决于R,那么如何评估呢?
我想这是关于惰性评估的。 Q'变成了一个重击,但我无法在我的脑海中推理。
作为上下文,我正在查看 组合器,它应该找到一个函数的不动点,所以如果我有这个函数,one x = 1 ,然后 fix one == 1必须坚持,对吧?
所以fix one = let {x = one x} in x ,但我看不到 1会从中出现。

最佳答案

Talking about let, I thought that let P = Q in R would evaluate Q -> Q' then P is replaced by Q' in R, or: R[P -> Q'].


在道德上,是的,但是 P不会立即评估,而是在需要时评估。

But in fix definition the Q depends on R, how to evaluate then?

Q不依赖于 R , 这取决于 P .这使得 P递归地依赖于自身。这可能导致几种不同的结果。粗略地说:
  • 如果 Q在评估 P 之前不能返回其结果的任何部分,然后 P表示无限递归的计算,它不会终止。例如,
    let x = x + 1 in x     -- loops forever with no result
    -- (GHC is able to catch this specific case and raise an exception instead,
    -- but it's an irrelevant detail)
  • 如果 Q可以在需要评估 P 之前返回其结果的一部分,它确实如此。
    let x = 2 : x in x
    -- x = 2 : .... can be generated immediately
    -- This results in the infinite list 2:2:2:2:2:.....

    let x = (32, 10 + fst x) in x
    -- x = (32, ...) can be generated immediately
    -- hence x = (32, 10 + fst (32, ...)) = (32, 10+32) = (32, 42)

  • I imagine that this is about lazy evaluation. Q' becomes a thunk, but I can't reason this in my head.

    P与 thunk 相关联。重要的是这个 thunk 在返回输出的某些部分之前是否调用自己。

    As a matter of context, I'm looking at Y combinator, it should find a fixed point of a function so if I have this function. one x = 1, then fix one == 1 must hold, right?


    是的。

    So fix one = let x = one x in x, but I can't see why 1 would emerge from that


    我们可以这样计算:
    fix one
    = {- definition of fix -}
    let x = one x in x
    = {- definition of x -}
    let x = one x in one x
    = {- definition of one -}
    let x = one x in 1
    = {- x is now irrelevant -}
    1
    只需扩展定义。保留递归定义,以防您再次需要它们。

    关于haskell - 如何评估 `fix f = let {x = f x} in x`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65123956/

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