Now that we have created a way to hold information about a specific file, and to serialize it, we should turn our eyes toward representing a collection of files. The minimal implementation here is pretty easy thanks to the work that we’ve done with supporting encoding lists. We’ll make a newtype wrapper around a list of values, and while we’re at it, we’ll go ahead and give it an Encode instance. We’re manually specifying the instance here because if we use newtype deriving we’ll once again run into an overlapping instances problem.

​  ​ instance ​ ​ Encode ​ a => ​ Encode ​ (​ FilePack ​ a) ​ where ​
​  encode (​ FilePack ​ a) = encode a

We can use this to create files pretty easily:

​  ​ λ ​ filePack = ​ FilePack ​ [ ​ FileData ​ ​ "file1" ​ 0 0 ​ "data1" ​
​  , ​ FileData ​ ​ "file2" ​ 0 0 ​ "data2" ​
​  , ​ FileData ​ ​ "file3" ​ 0 0 ​ "data3" ​ ]
​  ​ λ ​ :​ type ​ filePack
​  filePack :: ​ FilePack ​ (​ FileData ​ ​ String ​)

This works great so long as we only ever want to pack a single kind of file into an archive, but what if our user has a file that contains text data stored as a Text and a file that contains some image data stored as a ByteString ?

​  ​ λ ​ textFile = ​ FileData ​ @​ Text ​ ​ "textfile.txt" ​ 0 0 ​ "some text" ​
​  ​ λ ​ imageData <- ​ BS ​.readFile ​ "image.jpg" ​
​  ​ λ ​ imageFile = ​ FileData ​ ​ "image.jpg" ​ 0 0 imageData
​  ​ λ ​ filepack = ​ FilePack ​ [textFile, imageFile]
​  <interactive>:75:32: error:
​  ​ • ​ ​ Couldn't ​ match ​ type ​ ​ ‘ ​​ ByteString ​​ ’ ​ with ​ ‘ ​​ Text ​​ ’ ​
​  ​ Expected ​ ​ type ​: ​ FileData ​ ​ Text ​
​  ​ Actual ​ ​ type ​: ​ FileData ​ ​ ByteString ​
​  ​ • ​ ​ In ​ the expression: imageFile
​  ​ In ​ the first argument ​ of ​ ​ ‘ ​​ FilePack ​​ ’ ​, namely ​ ‘ ​[textFile, imageFile]​ ’ ​
​  ​ In ​ the expression: ​ FilePack ​ [textFile, imageFile]

As you might have expected, this failed because we’re trying to put values with two different types into a list. Up until now, when we ran into this situation we’d throw our hands up in the air and think of a different way to represent our code, but in this example being able to represent files with different types of content is really fundamental to what we’re trying to build. Surely Haskell can’t be stymied by a requirement like this.

Supporting Multiple File Types with Sum Types

One way that we could solve this problem is by creating a new sum type that would let us hold different sorts of files. There are a couple of ways we could do this. We might try a fairly high-level and user-friendly approach, like enumerating different sorts of files a user might be dealing with:

​  ​ data ​ ​ SomeFile ​
​  = ​ SomeTextFile ​ (​ FileData ​ ​ Text ​)
​  | ​ SomeImageFile ​ (​ FileData ​ ​ ByteString ​)
​  | ​ SomeAudioFile ​ (​ FileData ​ ​ ByteString ​)

As you can imagine, that would start to become quite a long list, and it would be impossible to predict all of the different sorts of files that a user might want to store. Alternatively, we could use a lower-level representation and just wrap the file data type directly:

​  ​ data ​ ​ SomeFile ​
​  = ​ SomeStringFile ​ (​ FileData ​ ​ String ​)
​  | ​ SomeTextFile ​ (​ FileData ​ ​ Text ​)
​  | ​ SomeByteStringFile ​ (​ FileData ​ ​ ByteString ​)

This approach is more general, but even now we’re limited to just a handful of file types, and we’ve completely defeated the work that we did earlier to allow users to create their own file content types.

Some Failed Attempts

If you stare at the problem for a while, the next thing that might come to your mind as a way to get around this problem is to use forall .

We’ll start by looking at FilePack . We have a type parameter that we’re using to keep track of what kind of data is in our list. This is a problem because it’s limiting us to a single type of value. Let’s remove it:

​  λ newtype FilePack = Filepack [a]
​  
​  <interactive>:7:30: error: Not in scope: type variable ‘a’

This doesn’t work because GHC doesn’t have any idea what a is or where it came from. If a isn’t a type parameter we need to introduce it somehow. We can use the forall keyword here to introduce a type variable. Let’s try it:

​  ​ newtype ​ ​ FilePack ​ = ​ FilePack ​ (forall a. ​ Encode ​ a => [a])

Hey, that works! We can create a FilePack that can hold any sort of data that has an Encode instance. Let’s create a value:

​  λ filepack = FilePack [FileData "file" 0 0 "file"]
​  
​  <interactive>:22:22: error:
​  • Couldn't match expected type ‘a’
​  with actual type ‘FileData [Char]’
​  ‘a’ is a rigid type variable bound by
​  a type expected by the context:
​  forall a. Encode a => [a]
​  at <interactive>:22:21-48
​  • In the expression: FileData "file" 0 0 "file"
​  In the first argument of ‘FilePack’, namely
​  ‘[FileData "file" 0 0 "file"]’
​  In the expression: FilePack [FileData "file" 0 0 "file"]

Foiled again. This time we’re telling our newtype wrapper that it needs to hold some polymorphic value that can be any Encode value, but then we’re trying to give it a value with a specific type. Since that specific type isn’t polymorphic, it fails to typecheck.

Impredicative Polymorphism

One of the problems that we’re going to run into with all of these attempts is that even if they would otherwise typecheck, we’re using forall outside of our list. No matter how flexible we might be with picking a value, we’re still going to come up with a list of values of the same type. What we’d really like to have is a list of anything, so long as it can be encoded. Let’s try another approach:

​  λ data FilePack = FilePack [forall a. Encode a => a]
​  
​  <interactive>:29:17: error:
​  • Illegal polymorphic type: forall a. Encode a => a
​  GHC doesn't yet support impredicative polymorphism
​  • In the definition of data constructor ‘FilePack’
​  In the data type declaration for ‘FilePack’

This still doesn’t work…yet. As you might note from the error message here, impredicative polymorphism is on the roadmap for GHC and may even be available in newer releases of the compiler. If this works for you, congratulations! You can move on to the next section of this chapter. If, however, you are working with an older GHC version, or working in a code base that was written before impredicative types were added, we have a different approach we can use to work around this problem.

Existential Types

Existential types in Haskell give you a way to “weaken” a type into a representation that is more general, but about which you have less information. In our case, we’d like to take all of the different FileData types that we might have, like FileData String or FileData Text , and forget everything about them except that they are something we can encode. When we do that, what we end up with is an existential type that we can use in places where we have to use a single type—for example, in a list. To make use of them we have to add the ExistentialQuantification extension.

ExistentialQuantification

| images/aside-icons/info.png | The ExistentialQuantification extension has been available since GHC 6.8.1. It’s enabled by default in GHC2021 but you’ll need to enable it manually in Haskell2010 . This extension implies the ExplicitForAll feature, so you don’t need to enable it if you’ve already enabled ExistentialQuantification . This is generally a safe extension that shouldn’t cause problems with any existing code. |

Let’s look at an example to start with. We’ll create a new type, Packable , that will hold values that we can put into a FilePack :

​  ​ {-# LANGUAGE ExistentialQuantification #-} ​
​  
​  ​ data ​ ​ Packable ​ = forall a. ​ Encode ​ a => ​ Packable ​ { getPackable :: ​ FileData ​ a }

You’ll notice in this example that although we’re using a type variable, a , we’re not actually referring to that type variable anywhere in the type declaration. This means that our existential type, Packable , doesn’t carry around any information about the type of the variable we used to construct it. Erasing this information is what lets us use Packable to do things like create a list that holds different sorts of file data.

Instead of holding the type information as a parameter to Packable , we’ve introduced it in our statement:

​  forall a. ​ Encode ​ a => ​ Packable ​

This allows us to not only introduce the type variable, but also add in any constraints that we might need. Adding constraints at this point is important, because once we’ve constructed a value of our existential type we no longer have access to any information about the type of the value.

To understand this limitation a little bit better, let’s try to write something that will get our FileData out of a Packable value:

​  getPackedFileData (​ Packable ​ fileData) = fileData

Let’s load this up in ghci and see what happens:

​  FilePack.hs:125:41: error:
​  • Couldn't match expected type ‘p’ with actual type ‘FileData a’
​  because type variable ‘a’ would escape its scope
​  This (rigid, skolem) type variable is bound by
​  a pattern with constructor:
​  Packable :: forall a. Encode a => FileData a -> Packable,
​  in an equation for ‘getPackedFileData’
​  at FilePack.hs:125:20-36
​  • In the expression: fileData
​  In an equation for ‘getPackedFileData’:
​  getPackedFileData (Packable fileData) = fileData
​  • Relevant bindings include
​  fileData :: FileData a (bound at FilePack.hs:125:29)
​  getPackedFileData :: Packable -> p (bound at FilePack.hs:125:1)
​  |
​  125 | getPackedFileData (Packable fileData) = fileData
​  | ^^^^^^^^
​  Failed, no modules loaded.

That’s quite an error message. The internet is full of questions about “rigid skolem” variables from intrepid Haskell developers who have run into problems trying to work with existential types.

The problem here is that, as you’ve learned, when we create a new Packable value we erase any information we had about the type that we created it with, except that it is encodable.

When we try to pattern match with (Packable fileData) we’re bringing into existence some variable, fileData . When we go to return that value, it needs to have a type, but there’s no type that we can actually assign to it because we’ve already lost that information. Not only that, the limited information that we do have about our value, forall a. Encode a , is captured by our Packable value. The type can’t exist outside of Packable , so we don’t have any way of extracting that value.

The idea that the erased type information might somehow leak out of an existential type is sometimes referred to by saying that the variable might “escape its context,” and you’ll sometimes get error messages from GHC that refer to the error this way. For example, if we rewrite getPackedFileData to be pointfree we’ll get a different error:

​  λ getPackedFileData = getPackable
​  
​  <interactive>:39:21: error:
​  • Cannot use record selector ‘getPackable’ as a function due to escaped
​  type variables
​  Probable fix: use pattern-matching syntax instead
​  • In the expression: getPackable
​  In an equation for ‘getPackedFileData’:
​  getPackedFileData = getPackable

Although the error message in this case is different, the fundamental problem is the same. We can’t leak any type information out of an existential type.

Just because we can’t leak type information out of our existential type doesn’t mean that we can’t do anything useful with existential data. One way you can make use of existential types is to use a record that contains some functions you might want to call. As an example, let’s create a new existential type that can hold a value and some functions:

​  ​ {-# LANGUAGE ExistentialQuantification #-} ​
​  ​ {-# LANGUAGE RecordWildCards #-} ​
​  ​ module ​ ​ ExistentialDemo ​ ​ where ​
​  
​  ​ data ​ ​ SomeExistential ​ b = forall a. ​ SomeExistential ​
​  { someValue :: a
​  , modifyValue :: a -> a
​  , combineValues :: a -> a -> a
​  , consumeValue :: a -> b
​  }

Although we have a type parameter here, b , we’re still creating an existential type because we’re erasing the existence of a . Inside of our record, we have several functions that reference both of our type variables, a and b . All of the normal typing rules apply here within our record, so someValue and the types of all of our functions are referring to the same type when they use the type variable a . Similarly, when we refer to b in consumeValue we’re referring to the b type parameter that we’ve added to our type constructor, so if we have a value with the type SomeExistential Int then we know that consumeValue must take as input whatever type someValue happens to be, and it must return an Int .

Since the type of a gets erased when we create an existential, we can’t ever get at it directly, but we are allowed to use the values and functions inside of our record as long as we never end up with a value whose type would leak out from the existential. Let’s look at a concrete example.

We’ll start by writing a function that will let us create one of our existential values that holds some numeric value:

​  addAndMultiplyInt :: ​ Integral ​ a => a -> ​ SomeExistential ​ ​ Int ​
​  addAndMultiplyInt n = ​ SomeExistential ​
​  { someValue = n
​  , modifyValue = (+n)
​  , combineValues = (*)
​  , consumeValue = fromIntegral
​  }

We can’t do much with this directly, but we can write a function to do some work with our existential value and give us back something that we can work with. Let’s write one:

​  runExistential :: ​ SomeExistential ​ a -> a
​  runExistential ​ SomeExistential ​{..} =
​  consumeValue $
​  combineValues (modifyValue someValue) someValue

In this function, we’re referring to values that we don’t, and can’t, know the type of. We can do that, because we don’t know what type someValue is, but we do know that whatever type it has, modifyValue can accept one and returns one. Similarly, combineValues takes two of them and returns one. At the end of our computation we’re calling consumeValue . Once again, we don’t know what type is going into consumeValue , but we do know the type that it will return. For example, if we pass runExistential , a value with the type SomeExistential Int , we don’t know what the values are inside of the existential, but we do know that whatever we get out must be an Int , and if we look up the type information in ghci it will tell us as much:

​  ​ λ ​ :​ type ​ runExistential . addAndMultiplyInt
​  runExistential . addAndMultiplyInt :: ​ Integral ​ a => a -> ​ Int ​
​  ​ λ ​ runExistential $ addAndMultiplyInt 4
​  32

In this example, we’re passing in a number and getting a number back out, but of course with our existential type we might be wrapping up anything. Let’s create a version that holds a string:

​  reverseAndUnwordsString :: ​ String ​ -> ​ SomeExistential ​ ​ String ​
​  reverseAndUnwordsString s = ​ SomeExistential ​
​  { someValue = s
​  , modifyValue = reverse
​  , combineValues = ​ \ ​a b -> unwords [a,b]
​  , consumeValue = id
​  }

And if we apply one of these to runExistential , we’ll get back a string:

​  ​ λ ​ runExistential $ reverseAndUnwordsString ​ "Hello, World" ​
​  ​ "dlroW ,olleH Hello, World" ​

Since consumeValue has a known return type, we can also use that as an entry point to be able to modify our existential record. For example, we could start by saying:

​  modifyExistential :: (a -> b) -> ​ SomeExistential ​ a -> ​ SomeExistential ​ b
​  modifyExistential f ​ SomeExistential ​{..} = ​ SomeExistential ​
​  { someValue = someValue
​  , modifyValue = modifyValue
​  , combineValues = combineValues
​  , consumeValue = f . consumeValue
​  }

If you look at the type of this function, you might recognize it from earlier in this book. It’s the type of fmap , so we can define a Functor instance for our existential type as well:

​  ​ instance ​ ​ Functor ​ ​ SomeExistential ​ ​ where ​
​  fmap = modifyExistential

This makes it easy for us to start making some changes to the eventual value returned by runExistential , as you can see in the demo:

​  ​ λ ​ runExistential $ show <$> addAndMultiplyInt 7
​  ​ "98" ​
​  ​ λ ​ runExistential $ length <$> reverseAndUnwordsString ​ "hello" ​
​  11

Of course, we’re not limited to a single function to evaluate an existential, we could write others as well. For example, this one returns the result of consumeValue directly:

​  ​ λ ​ runSimple ​ SomeExistential ​{..} = consumeValue someValue
​  ​ λ ​ runSimple $ length <$> reverseAndUnwordsString ​ "hello" ​
​  5
​  ​ λ ​ runSimple $ reverseAndUnwordsString ​ "hello" ​
​  ​ "hello" ​

You might have noticed at this point that what we’ve been doing is similar to another Haskell feature you’ve already used: type classes. In fact, much of what we have done so far could have been done with type classes instead of using an existential record, but with a little bit less flexibility.

For example, let’s consider the type class equivalent to SomeExistential and addAndMultiplyInt :

First of all, we’ll need to add a couple of additional language extensions:

​  ​ {-# LANGUAGE MultiParamTypeClasses #-} ​
​  ​ {-# LANGUAGE AllowAmbiguousTypes #-} ​
​  ​ {-# LANGUAGE FlexibleInstances #-} ​
AllowAmbiguousTypes

| images/aside-icons/info.png | The AllowAmbiguousTypes extension has been available since GHC 7.8.1. It isn’t enabled by default in either Haskell2010 or GHC2021 . Although this extension is generally safe to use, in some cases it can make it more difficult to track down errors in your program. As a general rule, it’s best to enable this extension on a module-by-module basis when you need it rather than enabling it project-wide. |

MultiParamTypeClasses

| images/aside-icons/info.png | The MultiParamTypeClasses extension has been available since GHC 6.8.1. This extension is enabled by default in GHC2021 , but you’ll need to enable it explicitly if you are using Haskell2010 . This is generally a safe extension that shouldn’t cause problems with any existing code. |

Our type class definition doesn’t look much different than the definition of our existential, except that we don’t need to actually store a value at all. Instead, we’ll just add the functions:

​  ​ class ​ ​ SomeClass ​ a b ​ where ​
​  modifyClassValue :: a -> a
​  combineClassValues :: a -> a -> a
​  consumeClassValue :: a -> b

This type class has two parameters, a and b . This mirrors both the type parameter, b , and the quantified type, a in our existential type. We also need to add AllowAmbiguousTypes , since neither modifyClassValue nor combineClassValues ever refer to b ; they are ambiguous—if we call them the compiler doesn’t know which instance it should use, and so without this extension it will not try at all and will give us an error.

Next we can create an instance. Let’s make one for integral types that output an Int, just like addAndMultiplyInt :

We’ll need to enable FlexibleInstances so that we can tell GHC what type we want to use for our second type parameter. Otherwise, this instance looks quite similar to the existential version that we created earlier:

​  ​ instance ​ ​ Integral ​ a => ​ SomeClass ​ a ​ Int ​ ​ where ​
​  modifyClassValue a = a + a
​  combineClassValues = (*)
​  consumeClassValue = fromIntegral

Finally, let’s write runSomeClass , a function that will be the type class equivalent to runExistential . We’ll need to add another couple of extensions here as well:

​  ​ {-# LANGUAGE TypeApplications #-} ​
​  ​ {-# LANGUAGE ScopedTypeVariables #-} ​
​  
​  runSomeClass :: forall a b. ​ SomeClass ​ a b => a -> b
​  runSomeClass val =
​  ​ let ​
​  modified = modifyClassValue @a @b val
​  combined = combineClassValues @a @b modified val
​  ​ in ​ consumeClassValue combined

We’ve had to enable TypeApplications and ScopedTypeVariables here so that we can tell GHC exactly what types we want to use when we call modifyClassValue and combineClassValues . We have to do this for the same reason that we had to enable AllowAmbiguousTypes earlier when defining our class: since these types never refer to b explicitly, without a type annotation GHC can’t figure out what versions of the function to call.

Other than our new type applications, runSomeClass again looks pretty similar to our existential version, and if we run it we’ll see that it does indeed return the same values:

​  ​ λ ​ runSomeClass 5 :: ​ Int ​
​  50
​  ​ λ ​ runExistential $ addAndMultiplyInt 5
​  50
​  ​ λ ​ runSomeClass 3 :: ​ Int ​
​  18
​  ​ λ ​ runExistential $ addAndMultiplyInt 3
​  18

There are some cases where we can’t trivially create a type class that’s equivalent to an existential record. Let’s imagine a different existential record that simply returns its input no matter what:

​  constExistential :: ​ Int ​ -> ​ SomeExistential ​ ​ Int ​
​  constExistential n = ​ SomeExistential ​
​  { someValue = n
​  , modifyValue = const n
​  , combineValues = const $ const n
​  , consumeValue = const n
​  }

If we re-use our existing runExistential function here, you’ll see that we do in fact always get out what we put in:

​  ​ λ ​ runExistential $ constExistential 7
​  7
​  ​ λ ​ runExistential $ constExistential 2
​  2
​  ​ λ ​ runExistential $ constExistential 0
​  0

On the other hand, if we try to redefine our SomeClass instance to do the same thing, you’ll find that we can’t really directly port our code. Ultimately, we can’t create a consumeClassValue function that always returns our original input, because that input is part of runSomeClass and not the instance itself. Since we can only have one instance of a type class per type, or in the case of our SomeClass , per pair of types a and b , we’d need to either create a newtype wrapper and instance for every integer constant we wanted to use, or create a new type class. In fact, that we needed to try to redefine our existing type class at all shows one of the advantages of existential records. We can arbitrarily have many different existential records that we can pass around, whereas with type classes we are limited to a single instance per type.

From these examples, you can see that existential records can be useful in cases where you want something like a lightweight and ad hoc type class, or where you need some additional constant values associated with your functions without the overhead of defining multiple type classes. Even so, in most cases you can restructure your code to avoid using an existential record. When it’s possible, you are usually going to be better served using a type class than an existential record because type classes are more idiomatic Haskell. Type classes give you nice things like constraints. They don’t force you to lose track of type information, and the fact that they can only have a single implementation is often a feature for readers of your code who can better predict what is going to happen on any given function call, because they can understand the behavior of a type class once and re-use that knowledge.

Type Classes and Existential Constraints

You’ve just learned how you can use existential record types to recreate some of the flexibility of type classes, and why you usually shouldn’t, but these two features aren’t often in opposition. In fact, the most common way to use existential types in practice is in combination with type class constraints.

When you define an existential type, you can include type class constraints on the quantified variable. For example, we can create an existential type whose value must be showable:

​  ​ data ​ ​ CanBeShown ​ = forall a. ​ Show ​ a => ​ CanBeShown ​ a

Just like in our previous examples, we can’t get a value out of CanBeShown directly, but because we know a must have an instance of Show we are free to use that to get information back out of our existential value. We could, for example, write a show function:

​  showWhatCanBeShown :: ​ CanBeShown ​ -> ​ String ​
​  showWhatCanBeShown (​ CanBeShown ​ value) = show value

And we can create type class instances for our existential type, so long as we don’t rely on knowing the type of the value that is inside of it:

​  ​ instance ​ ​ Show ​ ​ CanBeShown ​ ​ where ​
​  show (​ CanBeShown ​ a) = show a

Existential types like this are particularly useful because they give us a way of creating collections of things that share some common behaviors, like being able to be printed. Since we’re erasing the type information of the values inside our existential, we can create collections of existential values that were created with different types:

​  print [​ CanBeShown ​ ​ "hello" ​, ​ CanBeShown ​ 12, ​ CanBeShown ​ ​ True ​]
​  [​ "hello" ​,12,​ True ​]

Let’s take a moment to appreciate this. We’ve just created a list of a string, a number, and a boolean value and printed them out. Existential types helped us work around the “single type of element in a list” restriction by erasing the type information and only keeping around the relevant type class constraints that we needed.

Existential File Packing

Now that you understand existential types and how we can use them, you might already see how we can create a pack of files containing different types of underlying values. We will create a new existential type that represents any data that we can encode:

​  ​ data ​ ​ Packable ​ = forall a. ​ Encode ​ a => ​ Packable ​ { getPackable :: ​ FileData ​ a }
​  
​  ​ instance ​ ​ Encode ​ ​ Packable ​ ​ where ​
​  encode (​ Packable ​ p) = encode p

And now we can trivially define a FilePack as a list of Packable values:

​  ​ newtype ​ ​ FilePack ​ = ​ FilePack ​ [​ Packable ​]
​  
​  ​ instance ​ ​ Encode ​ ​ FilePack ​ ​ where ​
​  encode (​ FilePack ​ p) = encode p

One of the problems with using existential types this way is that it can be inconvenient for our users to have to manually wrap all of their values inside of our existential type. We can make this more ergonomic by giving users a way to easily add any ‘FileData‘ to a file pack without having to wrap it themselves:

​  addFileDataToPack :: ​ Encode ​ a => ​ FileData ​ a -> ​ FilePack ​ -> ​ FilePack ​
​  addFileDataToPack a (​ FilePack ​ as) = ​ FilePack ​ $ (​ Packable ​ a) : as

This is also a good opportunity to use an infix operator to recreate the convenience of creating lists:

​  ​ infixr ​ 6 .:
​  (.:) :: (​ Encode ​ a) => ​ FileData ​ a -> ​ FilePack ​ -> ​ FilePack ​
​  (.:) = addFileDataToPack
​  
​  emptyFilePack :: ​ FilePack ​
​  emptyFilePack = ​ FilePack ​ ​ [] ​

With that done, we’ve successfully built a system that will let us encode heterogenous file data into a single packed file. Let’s look at a demo:

​  testEncodeValue :: ​ ByteString ​
​  testEncodeValue =
​  ​ let ​
​  a = ​ FileData ​
​  { fileName = ​ "a" ​
​  , fileSize = 3
​  , filePermissions = 0755
​  , fileData = ​ "foo" ​ :: ​ String ​
​  }
​  b = ​ FileData ​
​  { fileName = ​ "b" ​
​  , fileSize = 10
​  , filePermissions = 0644
​  , fileData = [​ "hello" ​,​ "world" ​] :: [​ Text ​]
​  }
​  c = ​ FileData ​
​  { fileName = ​ "c" ​
​  , fileSize = 8
​  , filePermissions = 0644
​  , fileData = (0,​ "zero" ​) :: (​ Word32 ​,​ String ​)
​  }
​  ​ in ​ encode $ a .: b .: c .: emptyFilePack

We’ll skip showing the output here since it’s just a blob of encoded binary data, but if you run this in ghci you may be able to pick out some of the key parts of these files in the encoded data now that you are familiar with exactly how everything is being encoded.