tba
tba

Reputation: 6571

Lazy forward reference in Haskell

I'm new to Haskell and reading the Arrow tutorial. I can't understand how Haskell evaluates this loop function:

newtype Circuit a b = Circuit { unCircuit :: a -> (Circuit a b, b) }
instance ArrowLoop Circuit where
    loop (Circuit cir) = Circuit $ \b ->
        let (cir', (c,d)) = cir (b,d) -- line (A)
        in  (loop cir', c)

At line (A), variable d is used before initialization. How is this possible? Does GHC perform a fixed-point computation?

Upvotes: 3

Views: 199

Answers (1)

J. Abrahamson
J. Abrahamson

Reputation: 74334

Haskell's let and where bindings are self recursive. You could model that elimination exactly by using a fixed point computation. Since ArrowLoop models exactly a function which folds in on itself you would expect a need to use this self recursion.

As always, self-recursion like this might be non-terminating unless it can be evaluated sufficiently lazily to be productive.

Upvotes: 9

Related Questions