Bowl Full of Lentils


Sep 10, 2018

In this blog post I'm going to take a break from Haskell and spread the good word of Dhall. The good news is that it won't be a break from functional programming. We still have all the good stuff in Dhall like: lambdas, products, sums and types! We'll take a look through some of the basics of Dhall and work our way up to defining Either. This will be a two part series and in the second part we'll take the definition of Either and work with it to see some more functional concepts in Dhall.

Introduction to Dhall

One can almost view Dhall as JSON with functions and types, but really it's so much better. There is an excellent tutorial that lives in the library. It goes through the simple usage of Dhall and how you can utilise Dhall files in your Haskell code. I will try summarise here the main points to give you a taste. To really get a feel we can grab the Dhall executable. An easy way to get it is if you have stack, we can run stack install dhall --resolver=lts-12.0; grabbing the dhall package (version 1.16.1) at LTS-12.0.

The native types to Dhall can be enumerated:

Basic Types

So let's take a look at them via the dhall command. Running the following we can see what we can do with Dhall.

Booleans

Naturals

Integers and Doubles

Note:

There are no built-in operations on Integers or Doubles. For all practical purposes they are opaque values within the Dhall language

Text

Lists

Optionals

Unit

The Unit type looks like an empty record, which segues us onto our next topic nicely!

Records

On top of all these types we can make records that have named fields. For example let's define a user with a name, age, and email.

Defining Records Types and Values

Notice that we didn't have to bind the record type to a name such as User. Due to the nature of working with directories and files, our file path will be our name.

For these small examples, we will use let and in to bind the type and assert that the value we are constructing is the correct type.

Just to prove to ourselves that Dhall is type checking correctly, let's leave off the email value and see what happens.

Accessing Record Values

We can access one or more record fields use the . accessor.

Unions

As well as records we can define union types. For example we can enumerate the days of the week.

Defining Union Types and Construcing Values

And construct values of union types using the constructors keyword:

Consuming Unions

When we want to collapse union data we use the merge keyword:

Either

Ok, so that was a whirlwind tour of Dhall and I'm sure we missed some things along the way but it should be enough to get us writing a self defined Either data type. If we were to go off of the knowledge we covered above, our first attempt at Either would be:

< Left : a | Right : b >

That is to say, we have a Left union entry of type a, and a Right union entry of type b. The question is, where do a and b come from? Well, this is where type functions come in. In Dhall, types are passed along to say what types of things we are working with. Let's see this in action with the full definition of Either:

Notice how the output uses , λ, and . We can format our files to use these symbols, and I strongly recommend you do so. If we put the above definition in a file Either/type and run dhall format --inplace ./Either/type, it will convert all the symbols for us. So pretty 😍.

Let's see our Either in action! Assuming you're following along and have defined the above in Either/type we can try the following:

Just the Starters

We have taken a whirlwind tour of Dhall and filled ourselves with some starters. Going through the types that Dhall supports, defining and creating records and unions, and defining our good ol' friend Either. I'm not sure about you but I'm still hungry, so tune in next time for exploring some more Dhall visiting our familiar friend Functor, and its lesser known yokefellow Yoneda.