Vitaly Bragilevsky Foreword by Simon Peyton Jones M A N N I N G Selected examples inside the book Name Main features Section Extracting a Designing a small functional program 1.2–1.4 vocabulary Using OverloadedStrings GHC extension Working with Text from the text package Formatting texts with fmt Manipulating a Using type classes with derived instances 2.1 radar antenna Defining your own type classes Implementing type class instances Exploiting randomness for testing Arithmetic Converting recursive datatype values to Text 2.2, 5.2, expressions Implementing stateful imperative algorithms with the State monad 6.1, 7.2, 11.4 Exploiting complex monad transformer stacks with mtl Handling exceptions in monad stacks Controlling domain types with GADTs (generalized algebraic data types) Stock quotes Structuring a program with modules 3.1–3.3 Plotting graphs with Chart and Chart-diagrams Generating HTML with blaze-html Processing command-line arguments with optparse-applicative Reading CSV files with cassava Generating SQL Processing potentially incorrect data with logging errors in the Writer monad 5.1, 12.2 Using the traverse function Applying GHC.Generics to generating SQL queries from data type declarations Disk usage Manipulating files and directories 6.2 Working in IO-based monad transformer stacks Sunrise and Dealing with times and time zones 7.4 sunset Parsing JSON with aeson Making HTTP requests with req Designing an exception-handling strategy (Continues on inside back cover) Haskell in Depth VITALY BRAGILEVSKY FOREWORD BY SIMON PEYTON JONES MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2021 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Jenny Stout 20 Baldwin Road Technical development editor: Marcello Seri PO Box 761 Review editor: Aleksandar Dragosavljevic´ Shelter Island, NY 11964 Production editor: Lori Weidert Copy editor: Pam Hunt Proofreader: Katie Tennant Technical proofreader: Alexander Vershilov Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617295409 Printed in the United States of America To my mother brief contents P 1 C H ................................................................1 ART ORE ASKELL 1 ■ Functions and types 3 2 ■ Type classes 18 3 ■ Developing an application: Stock quotes 60 P 2 I ............................97 ART NTRODUCTION TO APPLICATION DESIGN 4 ■ Haskell development with modules, packages, and projects 99 5 ■ Monads as practical functionality providers 132 6 ■ Structuring programs with monad transformers 170 P 3 Q .....................................................203 ART UALITY ASSURANCE 7 ■ Error handling and logging 205 8 ■ Writing tests 246 9 ■ Haskell data and code at run time 281 10 ■ Benchmarking and profiling 310 v vi BRIEF CONTENTS P 4 A H ......................................................341 ART DVANCED ASKELL 11 ■ Type system advances 343 12 ■ Metaprogramming in Haskell 387 13 ■ More about types 435 P 5 H ........................................................477 ART ASKELL TOOLKIT 14 ■ Data-processing pipelines 479 15 ■ Working with relational databases 530 16 ■ Concurrency 567 contents foreword xv preface xvii acknowledgments xxiv about this book xxvi about the author xxxii about the cover illustration xxxiii P 1 C H ......................................................1 ART ORE ASKELL 1 Functions and types 3 1.1 Solving problems in the GHCi REPL with functions 4 1.2 From GHCi and String to GHC and Text 6 1.3 Functional programs as sets of IO actions 7 1.4 Embracing pure functions 10 Separating I/O from pure functions 10 ■ Computing the most frequent words by sorting them 13 ■ Formatting reports 14 Rule them all with IO actions 17 vii viii CONTENTS 2 Type classes 18 2.1 Manipulating a radar antenna with type classes 19 The problem at hand 19 ■ Rotating a radar antenna with Eq, Enum, and Bounded 21 ■ Combining turns with Semigroup and Monoid 26 ■ Printing and reading data with Show and Read 30 ■ Testing functions with Ord and Random 33 2.2 Issues with numbers and text 38 Numeric types and type classes 38 ■ Numeric conversions 40 Computing with fixed precision 41 ■ More about Show and Read 43 ■ Converting recursive types to strings 47 2.3 Abstracting computations with type classes 51 An idea of a computational context and a common behavior 51 Exploring different contexts in parallel 53 ■ The do notation 54 Folding and traversing 56 3 Developing an application: Stock quotes 60 3.1 Setting the scene 61 Inputs 62 ■ Outputs 62 ■ Project structure 64 3.2 Exploring design space 68 Designing the user interface 69 ■ Dealing with input data 70 Formatting reports 72 ■ Plotting charts 73 Project dependencies overview 74 3.3 Implementation details 75 Describing data 76 ■ Plotting charts 80 ■ Preparing reports 85 Implementing the user interface 92 ■ Connecting parts 94 P 2 I ...................97 ART NTRODUCTION TO APPLICATION DESIGN 4 Haskell development with modules, packages, and projects 99 4.1 Organizing Haskell code with modules 100 Module structure, imports and exports, and module hierarchy 100 Custom Preludes 105 ■ Example: containers-mini 107 4.2 Understanding Haskell packages 111 Packages at the GHC level 111 ■ Cabal packages and Hackage 114 4.3 Tools for project development 121 Dependency management 122 ■ Haskell projects as a collection of packages 127 ■ Common project management activities and tools 129