As you know, this week is about designing data.
And as part of that, we're going to introduce a new kind of design element
called the data definition. What I'm going to do in this video
though, is just talk about the problem that data definition solve.
And the way I'm going to do it is first show you how to design functions recipe
solution, a complete function design. Look at a problem we have in
understanding that, and then show you a complete data definition and talk about
how it solves the problem. Then in the next video we'll look at
designing data definitions from scratch. So now suppose you're working on a
program that somebody else wrote, and the program has to do with simulating
traffic. So it's reasonable to expect in a program
like that, that there's going to be code that has to do with traffic light and
cars and trucks and things like that. Now you're reading that program and you
come to this function definition, and you ask yourself, what does it do?
Well, you know, the name is kind of a hint.
It seems to produce the next color. So maybe if you give it a color, it gives
you another color. But it's just not really very clear from
this. Now at this point in the course, you
might say, well, look, the problem is that this person didn't follow the how to
design functions recipe. Let's imagine they did follow how to
design functions recipe. So here's the same function with the how
to define functions recipe. There's the original function definition.
And there we see that the function consumes the natural and produces the
natural and produces the next color of traffic light.
But here is a question. The signature says the function consumes
natural, so can I call it with 3? 3 is a natural number, but I don't see
any examples other than 0, 1, or 2. And what do these examples mean anyways?
I mean the examples match the functions, so they kind of tell me the function's
probably right. But when I call next-color with 0 and it
gives me back a 2, what does that mean? Well in order to understand this problem,
we have to understand something very important about data in programs.
In any program we have what we call the problem domain.
In this case the problem domain Has to do with traffic and simulation of traffic,
and traffic lights, and cars, and roads, and things like that.
And in the problem domain we have information in the problem domain.
In this case we might have a piece of information like a certain light is red.
But inside the program we don't have red lights, we don't green lights, we don't
have lights at all inside the program. What we have inside the program is data,
in this case the natural number 0. And what's going on, when we design a
program like this, is that we are playing a game, where we represent information in
the problem domain using data in the program.
In this case we represent the fact that some light might be read using the
natural number 0, and the inverse of that relationship is that we can interpret the
natural number 0 if we are told this natural number is supposed to represent
some information about a light, we can interpret it as meaning the light is red.
And now if I tell you this, if I tell you hey, to represent a red light, you use 0,
and if 0 is representing a light, it means it's red.
Then you can start to understand this program a bit more.
You can look at it and you can say, well if 0 is red, then I guess maybe 1 is
yellow. But instead of giving this information in
a piecemeal way, what we're going to do is we're going to use something called a
data definition, and a data definition is really going to tell us everything that
we need to know about how we represent information as data.
So now lets look at a new version of this program, the third version of it.
That uses the data definition. In this third version of the traffic
program we divided the file into two parts, the first part is called data
definitions and the second part is called functions.
And in the data definitions part we have one data definition.
I want to read this quickly now and in future videos this week we'll talk about
it in more detail. But the data definition starts out with a
type comment that says there's a new type, TLColor, and the way you form
something of TLColor, is that it's either 0, 1, or 2.
We'll have more elaborate type comments in the next video.
And it says, that if you have a piece of this data, you interpret it as follows,
something that's a TL color is the color of a traffic light, and 0 means red, 1
means yellow, and 2 means green. And this last part is a template for
operating on TL color. And for now just read it as saying, if
you're designing a function that operates on TL color.
Then there is really three cases, there is one case where the color is 0, there
is one case where the color is 1 and there is one case where the color is 2
and you know all about TLColor. Now, if you scroll down a little bit
there will be a functions part, here is a function and the signature now says hey,
this function consumes the TLColor and it produces the TLColor.
And notice we've already answered one of our questions from before, this function
can't consume a 4 or a 3 because it can only consume a TLColor.
And the only thing a TLColor can be, I can see by looking up here at the type
comment, a TLColor can only be a 0, a 1, or a 2.
And this function can only produce a TLcolor, a zero a one or a two and now
its producing the next color of a traffic light and now this function gets to be
easier to understand. If I say next color of a zero which means
red then that produces a two which means green and that's right.
After a light is red its green. That's color number 1, which is means
yellow, is a 0, which means red. And a light also gos from green to
yellow. And now the template, well what's the
template for this function. Well the template came from TLColor.
Because this function consumes a tl color and what we're going to see for the next
several weeks is that the template of a function is determined by what type data
it consumes and this just says well if you consume a zero, in other words a red
light, produce a zero. You can see in the one or in other words
the yellow light produce a red light and a green light goes to a yellow light.
There is a couple of important things going on here.
One is that this data definition tells me how to represent information as data and
how to interpret data as information. This data definition has made this
program much more meaningful. And this data definition has also greatly
simplified the design of this function. Because the signature now says TLColor to
TLColor, I know all sorts of things this function can't consume.
It can't consume the number 3, and I also know how to read this function and
understand what it's doing. So that's what data definitions are
going to do for us. They're going to help us really
understand the relationship between information in data and how data
represents information, and how data can be interpreted as information.
But I didn't talk about how to design data definitions, and I didn't talk about
how to design a function that consumes data defined by a data definition.
I just showed you examples of the completed product.
So now what we're going to do starting next time is look at how to design data definitions.