C

EDITPS: After this lecture, you should solve the problems/riddles on slides 16, 31, 38 in the deck. Then, read the Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire paper by our very own Erik Meijer and friends.

posted by Charles

]]>posted by user42

]]>C

posted by Charles

]]>posted by Justin Bailey

]]>

But hey yes, I also want to encourage Erik to tell us about category theory.

posted by user42

]]>

Watching...

posted by exoteric

]]>

An entire lecture dedicated to Monads will be so welcome, Ralf. Thank you!

C

posted by Charles

]]>posted by head.in.the.box

]]>C

posted by Charles

]]>

Talking of the Fixed point operator/combinator it reminded me of Bart De Smet's post on implementing the trampoline for safe recursion in c#:

where he uses a fixed point combinator. I'm not sure I'd want to implement that in production code as none of my collogues would understand how it worked! I'd have to bow to simplicity and introduce local mutation and use an explicit stack rather than the function stack. It's a very interesting article though.

posted by Parmenio

]]>

I would like to add a few bits.

Bart mentions this Y combinator:

static Func<T, R> Fix<T, R>(Func<Func<T, R>, Func<T, R>> f) { FuncRec<T, R> fRec = r => t => f(r(r))(t); return fRec(fRec); } delegate Func<T, R> FuncRec<T, R>(FuncRec<T, R> f);

It is not so easy to see why it works. (It works because Bart leverages recursive types, c.f., FuncRec, and this is one way of getting a fixed point combinator; see here.) Anyway, let's try something else. Our Haskell-based Y-combinator, as mentioned in my talk, would be rendered in C# as follows:

static Func<T, R> Fix<T, R>(Func<Func<T, R>, Func<T, R>> f) { return f(Fix(f)); }

You have to admit that this definition looks much simpler. We do not rely on recursive types; instead Fix is recursively defined. It is, in fact,
**the definitional property of a fixed point**. That is, the fixed point of a function must be such that if we apply the function to the fixed point, then we get back the fixed point. The reason that it works in Haskell (or a lazy language in general)
is that it essentially also captures** the computational notion of recursive unfolding**. That is, to compute the fixed point of a function, what we do is, we apply the function, and as we hit its recursive reference we just compute the fixed
point, which means that we apply the function, and as we hit its recursive reference, but only then, we compute the fixed point, which means that we apply the function, etc.

Now, the bad news is that C# is not lazy and hence the above transcription of our really easy to understand Y combinator does not work. We get a stack overflow (not just for greater arguments but for any arguments). However, there is simple trick that gives
us **a computationally useful Y combinator that is still easy to understand**; we need to defer recursive evaluation until it is really needed, i.e., we need to make the application of the Y combinator lazy.

Hence, this works:

static Func<T, R> Fix<T, R>(Func<Func<T, R>, Func<T, R>> f) { return f(t => Fix(f)(t)); } static void Main(string[] args) { var factorial = Fix<int,int>(fac => n => n == 0 ? 1 : n * fac(n-1)); Debug.WriteLine(factorial(5)); }

posted by user42

]]>

while watching this new lecture, at 8:00 after adding Pred into the simple language interpreter, you say there's no change in its semantic domain. I'd expect there is a change, from type Value = Nat to type Value = Maybe Nat, and the definition for Nat staying the same, as type Nat = Int. Does this make sense?

And, thanks a lot for the lectures! Very interesting stuff, and a clear presentation. Can't wait for the next ones, monads especially. Interpreters are of course the essence of Monads (?) (and vice versa ). In that light, when you have { interpret (Succ x) = (Just.(+1)) $$ interpret x }, it could've been redefined as an optimizing monad to push the (+1) inside, hoping to catch the rogue Pred early on, making { interpret (Succ (Pred x)) } always equivalent to { interpret (Pred (Succ x)) }, transforming it on the fly into just { interpret x }. Making { Succ(Pred Zero) } an invalid expression is too operational-minded IMO. I mean it in general, not here in this lecture of course where you have to keep things simple. And partial application would still be needed of course.

posted by Will48

]]>

We had first:

interpret :: Term -> Value where Value = Nat

Then we had (option 1):

interpret :: Term -> Maybe Value where Value = Nat

As you mention, we could also have done instead (option 2):

interpret :: Term -> Value where Value = Maybe Nat

The reason for favoring option 1 on my side is that I view partiality (i.e., the application of Maybe) really as "effect" involved in computing values. Here, I use the terms "values vs. computations" in the sense of monadic style already. (Interestingly, Wadler, in his essence paper, makes partiality part of his Value domain, see his constructor "Wrong", but it is only needed for as long as he doesn't replace it by the use of an error monad.) Another reason for my preference is that the used style for Value makes it also accessible to extension (as needed for Boolean values and functions later).

Now, when I said that no semantic domain changed, I should really have emphasized that no *named* domain changed, but the result type of the meaning-giving function, i.e., the denotation type for terms, has changed. Precision would be gained by assigning an explicit name to the result type as in:

interpret :: Term -> Meaning where Meaning = Maybe Term Value = Nat

Thanks for catching this.

re: "Admitting Succ(Pred Zero)"

re: "interpret (Succ (Pred x)) \equiv interpret (Pred (Succ x))"

Not sure. If you can suggest some patched code, perhaps, I can say whether I like it or not

Do you agree with the following views?

If someone really consumes the result of (Pred Zero) as in Console.WriteLn, then we have to throw.

If someone's code is not strict in the result of (Pred Zero), then we can succeed (in a lazy language).

Thanks for the comments.

Regards,

Ralf

posted by user42

]]>

Your "views" at the end of the post make total sense to me. About the code, I was thinking along the lines of

interpret Zero = Just 0

interpret (Succ x) = interpret' 1 x

interpret (Pred x) = interpret' (-1) x

interpret' n (Succ x) = interpret' (n+1) x

interpret' n (Pred x) = interpret' (n-1) x

interpret' n Zero = if n>=0 then Just n else Nothing

This really goes to the separation of *timelines *(*combination *
time vs *execution/run *time) as I see it as the essential feature of monads. To do something (here, (1+) )
*after* the processing is done, or *while* processing - *after *
combining all the monadic actions, or *while *combining them.

But it *is *an embellishement.

posted by Will48

]]>

There is nothing essentially monadic about this code.

In fact, I don't know how to usefully classify this approach, but is very systematic of course.

It smells a bit like CPS because of the way you pass around the current result of interpreted constructors so far.

At a general level, I think what you are proposing is to try to remain defined in cases where the partiality of a surface domain (such as Nat) can be accommodated for with a more liberal, internal domain (such as Int). I can see that this overall idea of being more restricted at the interface level than in the internal design of a system with intermediate results of computation makes very much sense.

This is an unusual technique in interpretation

Would you say that your code is sufficiently equivalent (and in intention and behavior) to the following perhaps more direct approach?

data Term = Zero | Succ Term | Pred Term type Nat = Int -- We won't use negative numbers. interpret :: Term -> Maybe Nat interpret x = case interpret' x of n | n >= 0 -> Just n otherwise -> Nothing where interpret' :: Term -> Int interpret' Zero = 0 interpret' (Succ x) = interpret' x + 1 interpret' (Pred x) = interpret' x - 1 main = do print $ interpret $ Succ $ Succ $ Succ $ Pred $ Pred $ Zero

This version makes interpretation operate on Int but it projects to Nat at the very end.

Cool, this made me thinking!

posted by user42

]]>

interpret Zero = Just 0 interpret (Succ x) = interpretPos 1 x interpret (Pred x) = interpretNeg 1 x interpretPos n Zero = Just n interpretPos n (Succ x) = interpretPos (n+1) x interpretPos n (Pred x) = if n>0 then interpretPos (n-1) x else interpretNeg 1 x interpretNeg n Zero = if n>0 then Nothing else Just n interpretNeg n (Pred x) = interpretNeg (n+1) x interpretNeg n (Succ x) = if n>0 then interpretNeg (n-1) x else interpretPos 1 x

This way we don't demand the existence of extended domain prior to our defining it (we
*didn't* define negatives above, but we could, now - just using Either instead of Maybe).

What I mean by my reference to monads is that I see it as essential to monads the separation of monad composition timeline and monad execution timeline, which makes optimization (pre-processing) while composing, possible - and that's what my version it doing. Also "monadic" is that my interpretPos/Neg pair encode and carry along the additional data. Or something like that.

posted by Will48

]]>

data PosNat = One | Succ PosNat data Int = Zero | PosInt PosNat | NegInt PosNat

... and yes, you could be using the reader (environment) monad for passing around the data except that all your equations are non-oblivious to the extra argument (excerpt perhaps the interpretPos case for Zero). In such a setting, the reader monad gives you little leverage.

posted by user42

]]>Regards,

Kashyap

posted by Kashyap

]]>