 Recording office hours. We're talking about Hindley-Milner type inference and specifically in the case here Where we have two apply statements and we have the same function on the left E and has some parameter on the right here So let's just say Let's give these abstract names. We've never seen before. This is C and this is D, right? So we know from each we know let's say not even looking at this one looking at this apply statement We know E has the type Where it's a function, so it's definitely a function, right? And it takes in some type So it takes in some One parameter, so it takes in a type of C and it returns Let's just say this is T1 whatever that type of that apply statement is so it takes in a TC and returns a T1 Then we go over here to this apply statement, right? We see the same thing. So we see okay, so first thing right So the constraints all so the constraints are always valid have to always be satisfied So this is always going to happen. This is going to be an equality that holds throughout the entire tree Or else we have a type error For instance, so if we had something else like let's say We had something else over here. It was a bracket operator and that had something F and E so we're applying the bracket operator on the Array on the F, which would mean F would have to be an array, but what was E right here? Yeah, it has to be an integer to be used here, right? So that's a constraint here So the question is is an integer a function that takes in a Function that takes in the type TC And returns a type T1 If what You mean that Oh E E is a function that takes in a type TC and returns a type T1 But does that does that work or does it have can it not be a function? So what's the constraint here? So let's just put arbitrary types here. So we have type Let's say T1 T2 and T3 Right arbitrary types not even looking the only node. We're looking at is this node here Right, so what do we know about the types just looking at this statement? So I'm gonna erase these I don't know I don't know any of their types But I know I know something this node to the constraints about this node and the semantics here the types Tell me something about how these types are all related T2 is an array T2 is an array Of T1 yep, right T3 is an integer And these have to hold right if the type of T3 is not an integer This doesn't type check so just to double check So let's get in there's like so Okay So then we look at what it is right What I have F and E here right and so now I have the constraints when I look at each of these nodes I say oh, I have the constraint T3 is equal to the type of E and Let's see T2 is equal to the type of F Right these constraints hold everywhere So if earlier I Had said the type of E is something that it's a function that takes in so let's let's think about like this Let's get rid of functions. Let's say earlier. We said we we have the constraint that T E is an array the type of E is an array of It's so Question is with this T E is an array of it's can T E be equal to an integer or those types the same No, it doesn't make sense right one is an array of integers Yeah, the other one's just an in single integer right those are completely different types Right, so therefore that would not type check Your team does that do anything? I think it's still working. I guess I can ruin the flow of everything in tests, but yeah Allegedly, okay So so yeah, so this is kind of comes down to remember when we talked about like structural equality of types Right, so like it's really easy to do is an integer a Boolean Definitely not is an integer an Array of integers No, can't be right. It doesn't make sense You can't use an integer when you have an array because the whole point is an array is multiple type Multiple elements all of the same type and you use the array Array index operator to select one of them so you think of the This operator gets rid of this part as opposed to the types, right? Okay Okay, so then we have kind of the question so let's say we have a function that takes in type t1 Returns some type t2 and We want to say well is that equal to a function that takes in type t1 comma t2 and returns a t3 So what does this mean? It's a function how many parameters does function have And what is it typed as a return? Okay, this function what is this? Yeah, this function returns two parameters takes in two parameters one of type t1 one of type t2 and returns a parameter of type t return something of type t3 so Are these equal? No, you can't any places the thing about is it equals any place you could use this could you use this instead? Well, actually, okay, so that's a good question No, so definitely Definitely not yet Right because this function takes two parameters and this one takes one So you definitely can never use those in different instances, right? It doesn't make sense So this is definitely not equal Right so then So if we go back to your question here Or if t3 we see that instead of it being an array of it So Well based off we said it can't look a function exactly. Yeah, so if I have an integer And I say well, is it the same thing as something that takes in t1 and returns an integer a function that takes in t1 returns an integer No, you can't use a function in Place of an integer, right? It doesn't make sense You have to call this function in order to get the return type, right? That's how you use that if you'd use the apply operator pass in something of type t1 and you get an integer that you could then use But just using that function by itself Man that definitely doesn't type check So even though the return type of that function is integer you can't say it's equal to integer, right? I still have to operate on that function. Exactly that function Exactly, they're not structurally equivalent, right? So on one side you have a base type of an it and on the right side you have a function that takes in t1 returns an integer Okay, some other wrinkles here. Let's say we have like t1 returns type t2 and then we have I don't know t3 returns type t4, right? so we have this constraint so So what does this mean? It could be yeah, exactly, but this gives us additional constraints, right? So we know they're both functions and we know they both have the same types Right, so this means that in order for this equality to hold Was t1 have to equal t3 and was t2 have to equal t4 exactly Just like if you get in a situation where you have let's say an array of Type t5 and you have the constraint that that's equal to an array of Booleans. That's our favorite word Right, so is this equal? Maybe but it also gives us another constraint, right? Because we know okay structurally on the outside of the same they're both arrays so that matches so they definitely don't it's not Not equal right away, but then I have the additional constraint that t5 is a Boolean And that constraint must hold so for this to hold that means this holds And that's got to hold everywhere throughout the program. So if you ever see t5 not be a Boolean Then that's a problem Doesn't matter honestly Yeah, I mean I like to just do it top-down because that's more natural to me But that's like the nice really nice thing about this is you can do it in any order And as you're going through as long as you apply these constraints just like we did here, right? That's t3 is an integer and t2 is an array of type t1 as long as you do that everywhere It'll work out the same so in a homework problem if it asks for the type of a function, but the parse tree doesn't type check You type error Have any other homework three questions ABC and B is a function, but it's also a use later by itself Right, so it's just basically just like so It's got to be have the same type everywhere so if it's So for instance like here here's actually a really good example Like this example Scoot in a little bit. I am recording by the way So here you have function application. We're applying e We have e as a function that's being called So this would be some somewhere we have ec like this and then this here we have something like f bracket e Right and the dot dots dots here. It doesn't matter where this other piece is it could be anywhere, right? Doesn't matter so when we see so let's give each of let's give this a type t1 and So when we look at this apply this apply has constraints right function calls have constraints. So what What do we know about the types? Let's say t e tc and t1 here Return an integer why just did this Well, what does e have to be in order to for this to work Yeah, it's got to be a function and How many parameters does that function have? What what's the type of that parameter? Exactly we have tc and what does that function return? Something but we've given it a name here, right? Yeah, so whatever because this node is going to have some type and if the type so if I said x is equal to E bracket c right or e calling function e passing a parameter c assign that to parameter x So whatever the type of this is right if we're using this to check or whatever if this is an if statement Right, so we have the constraint that e better be a function that takes in one parameter Whose type is tc and that returns something of type t1? That's what you know, so we know right here e Has got to be a function. It's got to be a function that takes in one type c It takes in one parameter that's type is tc and that returns some other type t1 Now we get to this So let's give this a name t2 Just this node and then we have tf and te so we have t2 Okay, so looking at this node what constraints do we have here and what about tf of what? Yeah, because that's what we're gonna return here So the same thing right this is going to return something right because when you access when you access an array element, right? you want to you're essentially Peeling off this array of layer and returning one of those elements, and so if you have an array of Booleans When you index into one you're gonna return a Boolean right, but we don't have any constraints on what that type is We don't know these may be defined t2. Maybe Have a constraint somewhere else Okay, so the question is We have this So can an integer ever be a function? No if we have a similar situation Well, what kind of error is it? Yeah, a type check error exactly. So yeah, you have a type error So you say you say here well you could explain a little bit you say here e is being used as an integer So we have the constraint that e has to be an integer, but here e is being used as a function Therefore, there's no possible way that these things type check this this program does not type check It doesn't matter whatever else happens in any of this tree if we get one case like that we say boom type error Yeah, can't possibly be valid Yeah Let's have project questions. Yes First I think I'm gonna stop recording now