The function instance for ArrowLoop contains
loop :: ((b,d) -> (c,d)) -> (b -> c)
loop f b = let (c,d) = f (b,d) in c
First I have a problem with the signature: How can we possibly get b -> c from (b,d) -> (c,d)? I mean, the c in the resulting tuple may depend on both elements of the input, how is it possible to “cut off” the influence of d?
Second I don’t get how the let works here. Doesn’t contain (c,d) = f (b,d) a cyclic definition for d? Where does d come from? To be honest, I’m surprised this is valid syntax, as it looks like we would kind of redefine d.
I mean in mathematics this would make kind of sense, e.g. f could be a complex function, but I would provide only the real part b, and I would need to chose the imaginary part d in a way that it doesn’t change when I evaluate f (b,d), which would make it some kind of fixed point. But if this analogy holds, the let expression must somehow “search” for that fixed point for d (and there could be more than one). Which looks close to magic to me. Or do I think too complicated?
This works the same way the standard definition of
fixworks:i.e., it’s finding a fixed point in the exact same way
fixdoes: recursively.For instance, as a trivial example, consider
loop (\((),xs) -> (xs, 1:xs)) (). This is just likefix (\xs -> 1:xs); we ignore our input, and use thedoutput (herexs) as our main output. The extra element in the tuple thatloophas is just to contain the input parameter and output value, since arrows can’t do currying. Consider how you’d define a factorial function withfix— you’d end up using currying, but when using arrows you’d use the extra parameter and output thatloopgives you.Basically,
loopties a knot, giving a arrow access to an auxiliary output of itself, just likefixties a knot, giving a function access to its own output as an input.