I've recently begun an attempt to understand lens. I've found nothing gets you into the code like implementing it yourself and subsequently using it. And there's nothing better that helps with the process like Haskell does.
Our quest begins in modest surroundings. We have a staff surrounded by an aura of artificial light, kitted out for ergonomic handling and some stuffy robes that just about manage to conceal boxer shorts. As a humble warlock we begin by practicing our type magic before tackling the big bosses.
s the type of an equality.
The strange points about it are:
- Equality is usually a relation defined on two variables, we're confronted with four over here.
- At first glance the above does not really seem to constrain the variables to be equal to each other.
- We have two absolutely arbitrary contexts that will allow us to do absolutely nothing with these variables.
- Is it even possible to create any equalities?
Well, we can answer the last one quite easily with
The equality in that case is the identity function. This function says, for absolutely any context 'p' and 'f', given a we can get the same back.
Thus an obvious equality exists when the type variables are the same, so lets create a data type that reifies the idea of identical type variables.
The ONLY inhabitant of this type has the type
Thus an inhabitant of Identical s t a b must mean that s is equal to a (represented by the type constructor ~ for the constraint kind) and b is equal to t.
The existence of an inhabitant of this type is thus a 'proof of equality' between s and a, b and t.
In order to fit this into our general model of equality we make the observation that
Thus our proof is of type
What can we do with our proof?
Glad you asked..
This function says that given a proof of equality between s and a, t and b, i.e. (s ~ a, t ~ b) and an r that can only exist when the aforementioned constrains hold, create the r.
This function is thus modus ponens at the type level.
This function gives us the ability to construct r's under the assumption that (s ~ a, t ~ b) which allows us to perform substitutions of s and t with a and b respectively and vice versa.
Some examples of these substitutions are mentioned below:
Substitution of a variable in a random context.
Equality is symmetric:
Although, we've used AnEquality in our proof they will work with an Equality as Equality is strictly more general.
To address the questions raised in the beginning
- I'm still not sure why we have to represent an equality between two pairs of type variables.
- It is the function and the contexts that constrains the type variables.
Our warlock has pushed 'forward' through the undergrowth into a splendorous wide open clarity.
He must now dive 'backward' back into it to explore Iso's ;)