Book Image

Haskell Design Patterns

By : Tikhon Jelvis, Ryan Lemmer
Book Image

Haskell Design Patterns

By: Tikhon Jelvis, Ryan Lemmer

Overview of this book

Design patterns and idioms can widen our perspective by showing us where to look, what to look at, and ultimately how to see what we are looking at. At their best, patterns are a shorthand method of communicating better ways to code (writing less, more maintainable, and more efficient code) This book starts with Haskell 98 and through the lens of patterns and idioms investigates the key advances and programming styles that together make "modern Haskell". Your journey begins with the three pillars of Haskell. Then you'll experience the problem with Lazy I/O, together with a solution. You'll also trace the hierarchy formed by Functor, Applicative, Arrow, and Monad. Next you'll explore how Fold and Map are generalized by Foldable and Traversable, which in turn is unified in a broader context by functional Lenses. You'll delve more deeply into the Type system, which will prepare you for an overview of Generic programming. In conclusion you go to the edge of Haskell by investigating the Kind system and how this relates to Dependently-typed programming
Table of Contents (14 chapters)

Preface

This book is not a blow-by-blow translation of the Gang of Four design patterns (distilled out of the object-oriented programming paradigm). Having said that, wherever there is an intersection with Gang of Four patterns, we explore it more deeply.

This book is also not intended as a definitive taxonomy of patterns in functional programming or Haskell. Instead, this book is the story of modern Haskell, one pattern at a time, one line of code at a time. By following the historical arc of development, we can place the elements of modern Haskell in a conceptual framework more easily.

What this book covers

Chapter 1, Functional Patterns – the Building Blocks, explores the three pillars of Haskell, that is, first-class functions, lazy evaluation, and the Haskell type system, through the lens of patterns and idioms. We will cover some Gang of Four OOP design patterns along the way.

Chapter 2, Patterns for I/O, explores three ways of streaming I/O, that is, imperative, lazy, and iteratee based. While you're at it, learn about the problem with lazy I/O, together with a solution.

Chapter 3, Patterns for Composition, traces the hierarchy formed by functor, applicative, arrow, and monad, with a focus on how these types compose. Synthesize functor, applicative, arrow, and monad in a single conceptual framework.

Chapter 4, Patterns of Folding and Traversing, demonstrates how fold and map are generalized by Foldable and Traversable, which in turn are unified in a broader context by functional lenses.

Chapter 5, Patterns of Type Abstraction, retraces the evolution of the Haskell type system, one key language extension at a time. We'll explore RankNtypes, existensial types, phantom types, GADTs, the type-case pattern, dynamic types, heterogeneous lists, multiparameter typeclasses, and functional dependencies.

Chapter 6, Patterns of Generic Programming, delves into patterns of generic programming, with a focus on datatype generic programming. We will taste three flavors of generic programming: sum of products generic programming, origami programming, and scrap your boilerplate.

Chapter 7, Patterns of Kind Abstraction, delves into the Haskell kind system and related language extensions: associated types, type families, kind polymorphism, and type promotion. We'll get a sense of type-level programming and then conclude by going to the edge of Haskell: diving into dependently-typed programming.

What you need for this book

This book is written against the GHC 7.10 compiler.

However, the first four chapters of this book are less dependent on any particular compiler. Since remaining chapters rely more heavily on the GHC language extensions, the GHC compiler matters more for those chapters.

Who this book is for

If you're a Haskell programmer with a firm grasp of the basics and ready to move more deeply into modern idiomatic Haskell programming, this book is for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text are shown as follows: "We refactor evalTry function by using the bind operator (>>=)"

A block of code is set as follows:

   data Choice a b = L a | R b   
     deriving (Show)

   data Combo a b = Combo a b       
     deriving (Show)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

   data Choice a b = L a | R b   
     deriving (Show)

   data Combo a b = Combo a b       
     deriving (Show)

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.