 Their type inference we're gonna go over. I believe it's homework three question. That right? Okay, cool. Okay, so we know We have this fun. We have a definition of a function f takes in three parameters a b and c And we say if b of a then return c of a otherwise return c of b Okay, so the first thing we know so the question asks us for the type of f And so we know the type of f is a function that takes in a type of a a type of b a type of c and Return some function type t1. We don't know what it is. We'll call it t1 Okay, so if we draw our abstract syntax tree right here. We have a function definition We're just defining a function a b see and it has an if statement and the if condition is a call and The other thing is be the right one is a and then if this succeeds Then we have a call where the left is c Right is a and then else. We have a call left is c right is b That correct Yeah, if you verify it on the homework, just make sure we don't start solving the wrong problem. Okay Great. Yeah. Okay, so we've looked at this right. We've written it out here So we know this function f is definitely a function that takes in a t a t b Tc and returns a t1. So then what do we know about this node? Has to be t1 exactly Right, okay So we have an if statement So what do we know about the the constraints of the parent and the children of an if statement? Exactly the left one is a Boolean, right? This is the condition But the other ones have to be t1 exactly, right? So that's what we talked about today in class, right? So here so we're checking a condition and if it's true, we're gonna go down this path It's false. We're gonna go in this path. So those whatever path we take us to return the same value. Otherwise, it doesn't make sense Okay question so far pretty easy okay Here we have a function invocation so What do we know from this function invocation about the constraints of its parents and its children? The left is the function and the right is the parameters exactly so this we can see has the type t a and So this means B must be a function that takes in how many parameters And what's the type of that parameter? And what is this function return? Right, which makes sense when we look at it here So we know that B because it's used in as a function invocation in an if statement that Whatever it takes in as type ta that has to return a boolean Right, so B is a function that takes in a type ta and returns a boolean perfect Okay, then we go down this first branch. So in here we have another call Instruction Right, so this does exactly the same thing right so we just apply exactly we did over here over here So we know that this note so we know that this is a type ta so we know that this node of C Takes in one parameter of type ta and returns what? Which makes sense so right still we don't know what t1 is but we know that hey Whatever c is it better return a t1 so it better return. Whatever this thing returns So otherwise this thing's not gonna type check and that thing that it returns is going to be the return value of f Okay final call So we know here. This is TB and here we have What does this call say? Okay, so we haven't been giving track We probably should but we couldn't do this out of the fact. It's not a big deal. So we have ta TB TC and we have t1 Okay, so we know here that CB is a Function that takes in ta and returns a bully, right? Here we have ta is a ta here we have TC is a function that takes in a ta and Returns a t1 It's fine, right? Traits here ta is a ta great Here we now see that TC is Is a function that takes in a TB and returns a type t1 so Now we have to satisfy Now you have to satisfy these constraints, right? So type C So we need to say okay. How can we how can we make these the same? Right, so they're the same they're both functions which is good because otherwise We don't have a type error right if this was an array and this was a function then we'd say okay This is definitely an error So we say okay for this to be the same well the return values have to be the same t1 fine We already have that so now we have the additional constraint that TB is equal to ta right Okay, so then TB. Ah, yeah, so that's to take in a B and B is here. Okay, good. Okay, so type of B Takes in a type of a okay, so we have TB is equal to ta Which basically says ta is Function that takes in a ta and returns a Boolean And that does not type check because you'd have to infinitely replace this and you could never Well, we have an infinite rule if we do it more than once I mean, why would it be any different if you had a TB basically and you had a circular reference in TV Why would ta ta be different than a ta to TD were TD refer back to ta did that make sense? So you mean I'm going yeah, I'm thinking of Structural equivalence Right like a that's what I I see so wise. Yeah. Yes, I'm like that and a right TV why On the simple level why is that different than what we're running into over here with the circular reference? Ah because the type system can't I the type system can't express this So it could be that they're structurally equivalent But that the Hindi Milner type inference can't infer that these types are the same so there's There's a difference there because these are recursively defined Hindley Milner Will say I can't create a type for this which makes sense because this just like so It wouldn't treat it just like that. Why wouldn't I mean why wouldn't I guess treat it just like that? Because it doesn't have any additional information like the compiler would so here's the difference So you can actually do this Yeah, so here's the difference you can the problem is you can do it in a real language with pointers But not with actual structures because you can't build this array Right, you can't ever build this finite array of arrays of arrays of arrays of arrays of arrays of right It's an infinite array, but if you had a T letters E and F pointer to TF had to support at least the TD TA That way and that was supposed to be implementing your program. Yeah It was implementing and it supported it Oh Because I think this is a limitation of the actual language you're using and I yes on that when the hook when that Yes, so the language the project three language specifically doesn't allow Like I don't think you can create types like this. I did no no no because all you all you have is You have basic types, but there's no arrays or arrays for today. Okay, so change it to instead of array of TD is equal to TA and TA is equal to TA. Yes. Yes. Yes, so it will support that So, yeah, so that's actually the thing right so even in Hindley-Millner that does support this Because this is a constraint So that's the thing here. It's a constraint the constraint is TD is equal to TA. Okay, great I know that they're the same and here you're saying TA is constrained to be the same as TD Great, that doesn't conflict with anything. I know previously. I still know they're the same type The problem is trying to say TA is the same type as an array of TA's Right here. You can never satisfy this constraint So you couldn't say TA is equal to TA You could also say that you can say TA is equal to TA, right? We do that all the time. We say here This is TA. This is TA I mean depending on how it did so this is a constraint you can satisfy. Okay, right? You can say okay TA is equal to TA Done how does that never from TA is equal to the function call with TA So these are the same whereas You're defining an infinite type here like this type you're constructing it recursively So if you were to expand that out, you can't ever expand that out But here there's no expansion, right? You're not creating a new type You're saying that hey type TD is another name for type A and vice versa I think I just mentioned like in the textbook too if you run into that situation It's supposed to be an error or a type of smash or the other ways you define it that way Yes, that was the thing for this right it was a type error I was getting there and I was like, oh my gosh, did I mess everything up? I can remember which all was which so I Yeah, because if you can never create arrays or Pointers or structures then all you have is basic types. So then it's Type equivalence is a lot easier exactly So you never are going to get into this situation because you're can't you can't really construct new types in that language You can just give the new types different names and you can specify constraints on those types So you can say that like hey types DNA are the same and types and D are the same But you can only ever say that those are one of the basic types at some point Exactly right dig deeper to see if there's a Deeper If you had something on the program like x is an a right x is type a yeah, then you have y is type a Right Because you're saying well and then later on in the program you have x is equal to y So you're basically saying the constraint that type a is equal to type a that's the constraint that you're satisfied But you're just looking up by comparing the types This is the type construction exactly because you need to deconstruct these functions in order So say okay for this type to be equal to something else that means whatever's inside of it The parameters have to be structurally equivalent and the return value has to be structurally equivalent And because you can never satisfy that property here because it keeps Going forever the type there would we Mean do something similar Should we like for example for a question such as this We consider mostly stuff to study for is like if else. Ah, yeah, you should study the stuff that we talked about I wouldn't throw you to look like a ball. Okay to throw you a brand new thing here and define the semantics I didn't know if you would you throw anything like a while? No, if we didn't talk about walloops, then no We'll get into a lot of stuff the lambda calculus is actually very similar to that Or midterm questions, right wait, I'll stop this here. Let's see if there's more stuff