What I Wish I Knew When Learning Haskell
Total Page:16
File Type:pdf, Size:1020Kb
What I Wish I Knew When Learning Haskell Stephen Diehl 2 Version This is the fifth major draft of this document since 2009. All versions of this text are freely available onmywebsite: 1. HTML Version http://dev.stephendiehl.com/hask/index.html 2. PDF Version http://dev.stephendiehl.com/hask/tutorial.pdf 3. EPUB Version http://dev.stephendiehl.com/hask/tutorial.epub 4. Kindle Version http://dev.stephendiehl.com/hask/tutorial.mobi Pull requests are always accepted for fixes and additional content. The only way this document will stayupto date and accurate through the kindness of readers like you and community patches and pull requests on Github. https://github.com/sdiehl/wiwinwlh Publish Date: March 3, 2020 Git Commit: 77482103ff953a8f189a050c4271919846a56612 Author This text is authored by Stephen Diehl. 1. Web: www.stephendiehl.com 2. Twitter: https://twitter.com/smdiehl 3. Github: https://github.com/sdiehl Special thanks to Erik Aker for copyediting assistance. Copyright © 20092020 Stephen Diehl This code included in the text is dedicated to the public domain. You can copy, modify, distribute and perform thecode, even for commercial purposes, all without asking permission. You may distribute this text in its full form freely, but may not reauthor or sublicense this work. Any reproductions of major portions of the text must include attribution. The software is provided ”as is”, without warranty of any kind, express or implied, including But not limitedtothe warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authorsor copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, Arising from, out of or in connection with the software or the use or other dealings in the software. Contents 1 Basics 13 What is Haskell? ................................................ 13 How to Read .................................................. 14 GHC ...................................................... 14 ghcup ..................................................... 15 Package Managers ............................................... 16 Project Structure ................................................ 16 Cabal ..................................................... 17 Cabal NewBuild ............................................... 20 Local Packages ................................................. 22 Version Bounds ................................................ 22 Stack ...................................................... 23 Hpack ..................................................... 24 Base ...................................................... 25 Prelude ..................................................... 26 Modern Haskell ................................................ 26 Flags ...................................................... 26 Hackage .................................................... 27 Stackage .................................................... 28 GHCi ..................................................... 28 .ghci.conf ................................................... 30 Editor Integration ............................................... 32 Linux Packages ................................................. 32 Names ..................................................... 33 Modules .................................................... 33 Functions ................................................... 34 Types ...................................................... 35 Type Signatures ................................................ 36 Currying .................................................... 37 Algebraic Datatypes .............................................. 38 Lists ...................................................... 40 Pattern Matching ............................................... 41 Guards ..................................................... 43 Operators and Sections ............................................. 43 Tuples ..................................................... 44 Where & Let Clauses ............................................. 44 Conditionals .................................................. 45 Function Composition ............................................. 45 List Comprehensions .............................................. 47 Comments ................................................... 49 Typeclasses ................................................... 49 Side Effects ................................................... 52 3 CONTENTS 4 Records .................................................... 52 Pragmas .................................................... 53 Newtypes ................................................... 53 Bottoms .................................................... 54 Exhaustiveness ................................................. 56 Debugger ................................................... 57 Stack Traces .................................................. 58 Printf Tracing ................................................. 58 Type Inference ................................................. 59 Type Holes ................................................... 61 Deferred Type Errors .............................................. 62 Name Conventions .............................................. 63 ghcid ...................................................... 65 HLint ..................................................... 65 Docker Images ................................................. 65 Continuous Integration ............................................ 66 Ormolu .................................................... 66 Haddock .................................................... 67 Unsafe Functions ................................................ 69 2 Monads 71 Eightfold Path to Monad Satori ........................................ 71 Monad Myths ................................................. 71 Monad Methods ................................................ 72 Monad Laws .................................................. 72 Do Notation .................................................. 73 Maybe Monad ................................................. 75 List Monad .................................................. 77 IO Monad ................................................... 78 What’s the point? ............................................... 79 Reader Monad ................................................. 81 Writer Monad ................................................. 82 State Monad .................................................. 83 Why are monads confusing? .......................................... 84 3 Monad Transformers 87 mtl / transformers ............................................... 87 Transformers .................................................. 88 Basics ..................................................... 88 mtl ....................................................... 89 ReaderT .................................................... 90 Newtype Deriving ............................................... 90 Efficiency ................................................... 93 Monad Morphisms ............................................... 93 Effect Systems ................................................. 94 Polysemy .................................................... 95 Fused Effects .................................................. 98 4 Language Extensions 101 Philosophy ................................................... 101 Classes ..................................................... 101 Extension Dependencies ............................................ 103 The Benign ................................................... 104 The Advanced ................................................. 104 The Lowlevel .................................................. 105 5 CONTENTS The Dangerous ................................................. 105 NoMonomorphismRestriction ......................................... 105 ExtendedDefaultRules ............................................. 106 Safe Haskell .................................................. 107 PartialTypeSignatures ............................................. 107 RecursiveDo .................................................. 107 ApplicativeDo ................................................. 108 PatternGuards ................................................. 109 ViewPatterns .................................................. 109 TupleSections ................................................. 109 Postfix Operators ................................................ 110 MultiWayIf .................................................. 110 EmptyCase ................................................... 110 LambdaCase .................................................. 111 NumDecimals ................................................. 111 PackageImports ................................................ 111 RecordWildCards ............................................... 112 NamedFieldPuns ................................................ 112 PatternSynonyms ............................................... 113 DeriveFunctor ................................................. 114 DeriveFoldable ................................................. 114 DeriveTraversable ............................................... 115 DeriveGeneric ................................................. 115 DeriveAnyClass ................................................ 116 DuplicateRecordFields ............................................