 Well class happy Wednesday the second to last class of the semester Like almost a half party of defeated Before the next before the fight Question Whatever it says on the page Yeah How to even begin implementing the structure for parsing a switch statement, what should you do other than read the document again? Draw that you should draw that so you should this is what I do What I suggest to the people who came in to talk to me about it on office hours is you should start with the switch statement code And then the very first thing you realize is oh the back end intermediate representation does not have switch Just like see on see you have wild statements. You have switch statements, right? But that compiles to x86 code and that does not have any switch statements or wild statements Right you have move instructions, and you have go-to instructions So part of your job on this task is Understanding not only the input, but also the back-end language. So what does it have that I confuse? Right and so there are some tricks in there, so they're not tricks But it doesn't map one to one right you are doing a translation and you are compiling from one thing to another and so The way I do this is I would start with The switch statement code then I write the corresponding code in using only the privilege we have in the other language Right so in the intermediate representation Right do it just like that and then draw the diagrams okay How should my when I see a switch statement to the node what types of nodes do I want like what is my whole thing look like? With I mean obviously you only have if statements in our intermediate representation So your switch statement is going to translate those series of this statements How do they all look compared to the code that you wrote there? I think for me doing it all by hand Makes a lot more sense and then walking through so do it by hand and then go look at me that execute method And step through it see does it as it actually going to do what you think it's going to do The question was could you even trouble with posting sharing a diagram? No, you get a problem posting code Like this is how you implement switch statements. That was the You have a diagram that's absolutely fine to share At the war everyone should be warned. You may be wrong That's just his what he says right just a diagram It's like I heard in some law schools. They'll rent the books from the library and get out pages from the book so that that way because there it's all Very much curve. So only like two a's are gonna be given out of class or Three a's and so it's in your best interest to torpedo and one else is great But we don't have that you we have but you should still be skeptical of anything you read or hear online Even if it's from me the ta's or your fellow classmates So we then finally we've been studying land of calculus we finally come to the craziest part of land of calculus So we've built up Boolean operations so we can do and or knots We have true and false values. We have branching so we can do it branches Now as we saw at the end class on Monday, so we have Boolean logic We have arithmetic so we can do mathematical operations on things that represent numbers Right, we don't have actual numbers to work with when you think about it. That's fine right the computer the CPU does not have actual numbers to work with either all the CPU has is 32 ones and zeros that we interpreted a certain number to mean a certain number Right, so very this is very similar to how we've done arithmetic, right? We create a land expression a certain way and we're saying this represents the number one of the number two So we have arithmetic so we've talked about what we need, but we're missing loops We have no way to loop Right, and we've seen this in the factorial function So we have those who don't remember the factorial function is the factorial zero is one the vectorial n is n times the vectorial Then minus one right. This is pretty much this and to the nachi or kind of the standard things we use to teach recursive functions, which is kind of silly. We don't really There's very few times you have to write a factorial function in your career But you often have to iterate over a tree data structure like you're doing in your parsing So that's more of the type of recursion So we can code this up and see and we can see that we have a factorial function that takes in an integer value n Checks if n is zero return one. This is the base case of our recursion Otherwise return n times the factorial of n minus one standard factorial. We've all know so if we assume so we define actually everything in Like a calculus that we need for this except for is zero So we have a function that takes in a number if it's zero returns true if it's not zero returns false We could write that if you wanted to We also assume we have a predecessor function so an n minus one function So it gives you n and it returns n minus one so assuming we have that we can just easily translate that factorial function into a lambda expression So we have factorial is equal to lambda n if Is zero n return one Otherwise return multiply n times factorial at the predecessor of that right with predecessors n minus one So this is a direct translation of times return n times n minus one Questions on this? Yes, it is illegal. So this is not actually a lambda expression Right we have definitions so we have a lambda we have a lambda so here we have an abstraction We have lambda n and we have if we've already defined it so we can replace this fully with the body of if We are saying we have an is zero function so we can expand that we know one here is not represent the number one it is a placeholder for the church numeral lambda expression that represents one The multiply we know we have a multiply function But now we get to this factorial function How do we expand that out right and the problem is fundamentally we can't because we're trying to use factorial in defining itself So this is a place where we'd like to use recursion and this is how we do recursion when we have function calls in When we have recursive calls in our C code right inside that and this relates to scoping rules right when a function is declared It's name is available to be used in the body of its own function So you can call factorial from factorial and that's how we get awesome recursion There's some early languages that didn't have that where you only had procedures So you could only call them call a procedure and then that procedure could not call any other procedures It could just do stuff and then return So you didn't have any recursion in there. Okay Cool So this function even though this seems so easy to write and this is really a straightforward translation of what we had in the C code to here to the lambda code right and so this to me brings up a lot of interesting questions of Do we need names of functions in order to be able to write recursive lambda functions? Right. That was kind of the point of where we're starting with lambda calculus all anonymous functions completely 100% anonymous and so I Kind of tease this. I've been teasing this really throughout the whole time. We're talking about lambda calculus So commentator. What's a commentator? Yeah, so a land expression with no three variables Cool. So there are Well, maybe you can tell because this is called the Y Combinator There are a lot of different commentators. They all have different names. Usually they're just one letter Combinators, they each do different things to be honest. I don't know all of them One of them is that ID Combinator we've seen of lambda x dot x which just returns its parameter Has anybody ever heard this term in other usage that is not lambda calculus related in this class? Yeah, so what would be here for me? Yeah, so it's a startup incubator who co-founded He is a partner of that so it was Paul Graham and Trevor Blackwell, I want to say is one of the other ones and I think Robert Tadamoros was the third person there Along with Jessica. I cannot remember her last name. Anyways, they all started originally started this company in the 90s called Violet where they were making It easy for people to have online stores and so their startup eventually got bought by Yahoo and Became Yahoo stores. It's really big platform. So they got a bunch of money and so they decided hey We want to become a start. We want to make it easier for other people to make startups, right? And so when they were Looking around for names they actually looked to lambda calculus and thought back on their base and actually it ties in very well Because Violet was written in Lisp. So Lisp derives a lot of its notations from lambda calculus So they named their startup incubator Y Combinator So I'm gonna leave that there for now We're gonna look at it and then we'll look back and just think why they named it that and how that kind of ties in So if any of you are thinking about doing a startup and you end up applying for Y Combinator's Program, you can tell them, you know exactly why they named this Y Combinator And then you actually understand the Y Combinator, that would be reasonable. And then when you make a lot of money You can come back to ASU and donate it to ASU So we need more Okay, so we're gonna look at the Y Combinator and then we're gonna kind of take it apart to try to see what it does I'm just gonna kind of present it here at first because it's something that is very much non-obvious It is again one of these things where You know you have to take that kind of intuition leap to come up with this and then you can see that it has the properties that you want Okay, so the Y Combinator Lambda x dot lambda y y x x y space Lambda x dot lambda y dot y x x y So what is it composed of at a high level? Yeah, so a high level is actually an application right, so each of these expressions here How many branders are they taking it? Two, so they first taking an x and then taking a y So let's step through to see what happens when we just say Why food? So we'll just try to see what this does We'll try to stay a step by step. We have this whole huge thing Right So what's the first thing that happens? So what's the first thing what what's the first thing that we do baby? Yes, what do you produce? you What was that? So how do you parse this at the very highest level we have an expression We have an expression what type is it the very top Application how many do we have well? We're parsing it. There's only to when we look at in here. How many do we have? Three so this thing different than x y food So how do we do those? The left most things happen first Right, so implicitly These things are grouped together and you may notice when you look at the y-combinator definition Right, but it's not It's not in it's not fully beta reduced Right, even just looking at it. You can see that there's a Beta reduction that you could do Right the way it is now you can replace in here the x in this body with the thing on the right But we'll see that it actually kind of makes a lot of sense for you show it in this form Even though we use it here when we say why food Right the very first thing we're gonna do is on the left that has nothing to do with food Every on board with that. So inside this body here, so we're going to replace x with this expression Do we have to worry about changing Meta variable names on the left expression No, because it's a combinator, right? There are no free variables in this expression So we are going to inside this body replace both of these free x's With this expression cool. So we have lambda y dot y Landa x the lambda y y x x y Landa x dot lambda y dot y x x y y But look When we just replace these two x's in here with this, what do we put here? Another why is that a guess? He looks surprised So we can actually even So what we have in here is another why but we'll look at that in a second Now let's substitute in food for why Right, so we have the outermost we have the application on the left we have this giant thing and on the right we have food So inside this body here, we're going to replace every free Why with food? So if we look just in this body is this why free? By removing this part Yes, this why will be free. What about this why? No, it's gonna be down here this why Down here this why Down here this why Down here this why yes, so we were placing food in two different places So we will have food This thing this thing food But we just saw what is this part? So we have white food. So we have food applied To why food? So then what would happen if we expanded this one again? We get the same thing We get food food why food and then we expand that why food again We get food food food why food and we keep doing this forever. So we've essentially created a loop here Where we can execute food an arbitrary number of times because we can just keep doing this loop If statements just like a normal recursion. We're gonna have an if statement So if it's true, we'll see we'll return something. Otherwise, we'll do Exactly, we'll do the why food again So why does this work? What is it about the structure of this y-combinator that allows this to work? Yeah, so the key thing here I think is this xx y Right. So this xx is replicating itself. It's replicating the y-combinator into this other call here and then Also passing into that whatever you originally passed into it this y and then it's also applying that result to whatever y was And then if you have to call us again, this will keep going and it will keep expanding out And it's each of these beta reductions. So it's cool here. We have basically a bit of Landocadulous a landocadulous expression that not only propagates itself Right because we've seen if we have like ID, but we have x dot xx And we apply that to itself. It's just going to keep going forever. I Think that one actually keeps expanding forever And this one will keep replicating itself and calling food Essentially applying to how many times you want Yes, the y represents this whole statement, so that's the key here is that this y can always be replaced with this Right because we're not and we don't have we will not have any circular definitions. This sounds weird This is how we're going to use it or use it like why food well We'll see exactly how we use it we'll write a function that doesn't just take it a number It takes in a number and a y-combinator and we'll see how that propagates This is just to show you what happens when we see a y-combinator with any expression on the right This is what it's going to do right this could be An abstraction and so it's going to keep applying that result So let's step through and kind of see What our factorial function now would look like so this is the function we'd want to write But we can't write this ever an agree We cannot write this we're defining factorial in terms of itself so Now in this function, we're taking in n we're taking in And is the number right which makes sense We're copying the factorial number taking one number We're even going to return and we will return you know a number in our land to calculus in our church new rules So but this new version is going to be slightly different. So what we will return is why? So we have the y-combinator that we've already seen applied to Now a function that takes in two parameters the first parameter is F Which as we'll see is going to be the y-combinator and The end is the number. So let's step through this a little bit to see kind of what happens So we define factorial like this And we have factorial of one What's going to happen? So our factorial so we have Lambda f dot lambda n dot if size zero and return one always multiply n by f predecessor of n one. Can you guys see that in the back? So And what we're going to use we're going to use basically The what the y-combinator is going to do right the y-combinator is Going to I have to make sure I actually did this right Okay, I see it is right. Thank you Okay, so we're first going to Reduce the thing on the left right and we know that it's left most here and we're going to skip what the white We're going to skip all of the y-combinator steps Right, so we're going to take this as just like before we had why foo So we're going to say this returns foo Why foo? So here we have foo. Let's see. That's right Lambda f dot lambda n dot if size zero n one multiply n f predicate of n So this is foo in this case. So here we have foo applied to why foo all that applied to one So just doing that y-combinator operation once on the right here. What do we have? So if we think sorry, there's not really right and left In this lambda expression here We have y lambda f dot lambda n dot if size zero n one multiply n f predicate of n. What is that? Factorial that is our definition of factorial right and so When we are going to substitute that in for f here We're essentially going to put f where we wanted factorial to be we're essentially putting the definition of factorial back in there So we're letting the y-combinator generate us basically a copy of our own code And we're going to kind of insert it where we wanted that call to be so let's do that So let's reduce this substitute that in for f and I'm lambda n dot if is zero n one multiply n and this part in here Is factorial factorial predecessor of n But specifically we're not using the main factorial But it is the factorial right? I mean it's exactly as we've defined it right here y lambda f dot lambda n dot lambda n if size zero n one multiply n f predicate of n. Now it's starting to get a little hairy right? We have lots of predicates and n's and f's But we can still walk through this very carefully right and see exactly what's going to happen So now it's finally start to do some calculation and let's substitute one for n right we have that as our body So now we say okay all the so let's remove this lambda n here all of the three n's in here We're going to replace them with one So that should be just this end and this end Correct because inside here this lambda n blocks all those n's All right, let's see if that's correct. Oh except for this n here. This is one stuff gets tricky, right? so now we have if is zero one one multiply one times all this Time multiply this which is food which is factorial multiply factorial of predecessor of one So we have a couple things so it's if stated so what is zero one going to return? False and now I have if and we have false so which of the two arguments is it going to return? The second one So it's going to go away, and it's going to turn multiply one by this whole thing Times the predecessor of one so we'll save you calculate. Let's see. We can get a predecessor I will do that later so multiply one times the factorial the predecessor of one So then what happens when we do this? Well, we should yeah, I don't know that's going to be more or less confusing But let's go Are we If we do the recursive first I think so well, we're still getting to the same place Yes, but without the name exactly once we get to where that statement will evaluate to true It will return one and it will get rid of the y factorial Right you'll get rid of that and that will return one and we'll finally kind of essentially you think of it as coming back up the Call chain because we've reached our base case So let's see it step through this one more time So we have why foo here again, so just like before we'll end up having foo why foo So I multiply one foo here y of foo Oh I remember why we have to do this Okay, and once again we replace this thing which y f of this is factorial So we're gonna replace f in here with factorial Which gives us the ability to call it again if we need to and now we can reduce the predecessor of one is zero Now inside here, we're gonna replace n with zero So we're saying if is zero zero return one otherwise multiply this by this and So is zero zero will return true and So of these two branches Right of these two choices. We're gonna return one Because that's the way you define true. It will always return to true branch And so we multiply one by one and we get One yes, so be n minus one is what it returns it's tricky is zero we can probably do as a class maybe but Predecessor would require a lot more Kind of death, but really you saw we did addition you saw your multiplication I don't think it's that they were stretched to believe that we can go backwards and do subtraction So I think this is the more important part is really Seeing that this crazy way that we wrote we've written this y-combinator actually allows us to have recursion So the fact that it replicates itself and the function that it wants to go to is really cool Because then we can use that ability Where we would want to make a recursive call We would have that as a parameter being passed to us and what will be passed to us is the y-combinator with ourselves So we'll end up calling ourselves questions on this Got any questions Wait, no Let's put it this way. I want you to know the behavior you should understand the behavior of the y-combinator and how it's used If I ever want you to know specifically this thing, I would give you the definition Questions on the exam pertaining to this could be multiple choice I'm a big fan of multiple questions Basic instructions is just wherever it sees that factorial replace it with So in a language so in a language like this It really it uses the syntax of land expressions. So when you look at it, it's all parentheses like this So maybe I don't think it's sensitive in there. I can maybe pull up my My e-max configuration really quickly It's in a really weird version of Yeah, that's fine. So like here. So this is an elist file So it's an elist file so you can see all the parentheses. So the syntax is very similar. So this is says like When the computer is Matt is a Mac set the Mac command key modifier to meta so it's very similar in that inside every expression so inside all the parentheses the thing on the left is Most times a function Macro Is a function and the other ones are parameters. So the differences are a so the driving desks So these are so this was the that command that I just ran so D fun is to define a function So here you're defining a function name set frame for hacking group The thing in the next list is the parameters to the function and then the body here So in most lists, you have the ability to call functions again. So there you don't usually need You don't usually need the Y Combinator. It's more like It it shows you that you don't actually need when you think about what is at the base of computations, right? What actually is necessary? It turns out in a language with only functions You don't need name functions to do recursion, right? So that in itself is a very powerful idea You would still just like you never want to program a Turing machine like write code for a Turing machine It's very important because it has that idea that you can reduce any Computation any crazy language that you write to something that can work on a Turing machine The ideas here are the really important really cool things here So now we've seen this why do you think a startup incubator of all things besides being huge nerds? Why they used what the name why they made their startup incubator Y Combinator? Yeah, because they take in startups and they can like make more startups right there a startup themselves They're trying to take any startups and make more startups, right? So it's kind of very similar vibes So I think it was a pretty cool decision on them to do that questions Okay So now we've seen we actually are we've had Boolean logic. We have arithmetic. We have loops Now we have recursion Now I will make the claim that we're turning complete So how would you show that? Or prove it? I mean, how would you prove that? Yeah, so one way to do that would be to show that you can simulate a Turing machine using lambda calculus That would show that you can go one way, right? What about the other way if you're trying to say that they're the same Yeah, you want to go backwards, right? You want to write a Turing machine that can simulate lambda calculus, and then you're showing that they're both at the same expressivity level Right, so there's no program fundamentally, there's nothing you can do on a Turing machine you can't do in lambda calculus and Because we know that the language that we program in our turn complete That means there's nothing that you can do in C or Java that you can't do on the turn on a Turing machine or using lambda calculus So that's the church I think it's the church Turing hypothesis. I should have looked at that thesis. There you go. Yeah I think that was wrong Okay, well then you guys know all these So do you guys talk about lambda calculus in 3.55? Use words like function loops Cool Yeah, so any questions on this? Okay, so what we'll do so this is actually the end of lambda calculus I'm just as surprised as you all are Actually got here very quickly. I think last semester I was like rushing over this last part on the Persian but I think that we actually went through it at enough level that Give you some of the intuition because I think those are the really important things So class wise what we'll do is on Friday. I'll use class like office hours random So come very questions. I will go over things. I would do things but it will be entirely driven by the people in the room So come with your questions. You can have major questions homework questions project by questions, whatever Happy to answer. I'll also since I mean we still have 10 minutes if you want to leave leave I will hold I can hold like a post-class office hour session for 10 minutes if you guys have questions Well, let me start Let's see tension the final it'll definitely be cumulative That's something I'm not My perspective right the point of an exam is not there's not to test you on specific things but to Improve your knowledge of the course right so if I tell you it's only going to be this that's all you study I want you to say everything and I'm gonna pick certain things if You were looking at the class closely think huh midterm three included some content from midterm two Specifically because people didn't do so good on that part So that would be something interesting to think about is I may look at the things and say people didn't do really well in this You have to look inside for that Nothing outrageous that you haven't seen the other thing I'm thinking about is I haven't There's stuff that we covered after midterm three