Algebraic data type in PureScript and Show instance

Vincent Orr
InstructorVincent Orr

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 4 months ago

In this lesson, you’ll learn how PureScripts compiler will enforce pattern matching when we create data constructors with an Algebraic Data Type.

You’ll then learn how to create show functions for these data constructors that we just instantiated.

Instructor: [00:00] We'll make our first one by using a keyword data. We'll call it vehicle, and that'll equal a car, and it has wheels. Now we have an error of unknown type, wheels. Let's make this, so we call it data wheels = wheels, and we'll give it a type of int.

[00:15] Next, let's add more attributes. To do this, we'll separate with a pipe, and we'll do | motorcycle wheels | skateboard wheels | bicycle wheels. Let's demonstrate how this would be used. We'd do myVehicle, and its type is vehicle. On the next line myVehicle = car [wheels 4] .

[00:36] Let's change car to pony and see what happens. It has an unknown data constructor of pony, because we didn't create one. Let's now change it to skateboard, and then we'll give the wheels a string of "poop." Oh, another error. It couldn't match type string with type int. That's because, on line 13, you'll see that wheels are int. Let's change that back to 4, and that type checks.

[01:05] What we'll attempt now is to log myVehicle. Let's remove the string, add myVehicle. Oh, there's a error, "No type class instance was found for data show, show vehicle." This means that we need to add an instance. Right now, we can't actually show anything from our new data constructor. Let's do that.

[01:21] First of all, we'll create a function called showWheels, and that'll have a type of wheels -> string. On the next line, show wheels [wheels a] , and that will equal a string of wheels colon. To that, we'll append show a, and the a is the int that we use with wheels. Int already has a show instance, so that'll work here.

[01:44] Let's test this out. We'll do show wheels, remove this, and we'll bracket wheels 4. Voila! We've created a function that can show our wheels and its value.

[01:55] Next, let's make one for showing our vehicle. We'll call it showVehicle, and we'll give it a type of vehicle -> string, and on a next line, showVehicle [:car a] . That'll equal the string of "vehicle:car," and then we'll give it a little space, and we'll append show a.

[02:16] We've got an error, "An expression cannot be determined to cover all inputs." Basically, we're pattern matching, and so far we've only pattern-matched against car. We're missing on motorcycle, skateboard, and bicycle. This is a really handy thing about ADTs. If you've not covered all your cases, then you're compiler will let you know.

[02:33] Let's cover these cases. We'll copy line 19, paste it three times. Let's grab car. We'll change that to motorcycle, then we'll add skateboard, and finally we'll get bicycle and add that. The compiler's still returning something's wrong.

[02:52] There's no instance for data show, show of wheels. We created a function that does that, but we need to actually make it an instance. We'll do that by using the keyword instance. We'll call it showWheels tick, and that'll have a type of show wheels, and then we'll do where. On the next line, show = showWheels.

[03:14] The next time that we use a type show with our type wheels, we'll use our function showWheels to display the output. Let's make a show instance for our vehicle. That'll be instance showVehicles tick, and that's got the type of showVehicle, where, on the next line, show equals showVehicle.

[03:29] This will work exactly in the same way as our instance of showWheels. Let's copy myVehicle from line 21, and we'll replace it on line 34. There you go. You have the output. It's showing vehicle. There's a skateboard, and its wheels are four.

[03:45] Let's change myVehicle to a type of car, and its wheels will have a string of "poop." It couldn't match a type string with a type int. It needs to be an integer. We'll give it a 6, and it'll display. My vehicle is a car, and it's got wheels of 6. Pretty strange car, but that's what it's got.

[04:04] There you go, an introduction to algebraic data types and show instances.

Hamdan Khan
Hamdan Khan
~ 4 years ago

image is broken where it says unkown data constructor in transcript