Друзья, мы тут исследуем проблемы, с которыми сталкиваются пользователи электронных книг.
Будем вам благодарны за ваши ответы. Опрос займёт не более 5 минут.
Друзья, мы тут исследуем проблемы, с которыми сталкиваются пользователи электронных книг.
Будем вам благодарны за ваши ответы. Опрос займёт не более 5 минут.
About the Author ................................................................................................xi
About the Technical Reviewer ...............................................................................xiii
Acknowledgments ...............................................................................................xv
Introduction .......................................................................................................xvii
How This Book Came to Be: Instead of an Introduction ............................................xix
Chapter 1: Wizards, Types, and Functions ..............................................................1
Solving Problems As Wizards Do ...........................................................................3
Let’s Fall in Love with Types! ................................................................................7
A Char and an Int Walk into a Bar and Make a Function ...........................................8
How FunTypical! ..................................................................................................12
Curry and Recurse! .............................................................................................15
Conclusion ..........................................................................................................19
Chapter 2: Type Construction ................................................................................21
House of Cards with a Little Help from Algebra ........................................................22
Algebraic Data Types ...........................................................................................25
Records .............................................................................................................27
Type Functions Are Functions, Maybe? ...............................................................30
Advanced: On Data Constructors and Types ........................................................37
List, Recursion on Types and Patterns ................................................................39
Length of a List ..............................................................................................42
Map a Function Over a List of Values .................................................................44
Conclusion ........................................................................................................50
Chapter 3: Very Gentle Type Theory and Category Theory Intro ...............................51
Types and Functions ...........................................................................................52
Maybe and Advanced Generalized Functions .......................................................54
Dependent Function Types (Pi-Types) .................................................................59
Sum, Product, and Dependent Pair (Sigma) Types ..............................................61
Very Gentle Category Theory Introduction ..........................................................63
Typeclasses .........................................................................................................66
Conclusion ..........................................................................................................70
Chapter 4: Basic Typeclasses or “Show Me a Monoid” ...............................................71
Show Typeclass ...................................................................................................72
Algebra Is Cool ....................................................................................................73
Typeclass Hierarchy in Haskell ................................................................................76
Lift Me Up! ..........................................................................................................79
Conclusion ..........................................................................................................83
Chapter 5: Functor, Bifunctor, and Applicative Functor Enter
an Elevator … ...................................................................................................85
Functor Typeclass Definition ...............................................................................86
Three-Dimensional Vector Example ....................................................................89
Tracking Players in Our Game .............................................................................91
You Are Either Functor or a Bifunctor ..................................................................92
We Need a Bigger Lift ....................................................................................98
Action! Apply! Cut! .......................................................................................104
Applicative Typeclass .....................................................................................107
Conclusion ...................................................................................................111
Chapter 6: O, Monad, Help Me Compose! ..........................................................113
Can We Play Cards in This State? .....................................................................117
Monad Typeclass and Basic Monads ..................................................................128
Reader–Writer–State Triple ...............................................................................132
The State Monad ...............................................................................................132
The Reader Monad ............................................................................................135
The Writer Monad ..............................................................................................140
Conclusion ........................................................................................................145
Chapter 7: Input, Transformer Stack, Output .........................................................147
Do Notation .......................................................................................................153
Monad Transformer Stacks ..................................................................................158
Conclusion ........................................................................................................169
Chapter 8: Blackjack: Full Haskell Program ............................................................171
Preparation .......................................................................................................174
Initial “Pure” Design ..........................................................................................177
Building the First Floor: IO .................................................................................183
Building the Top Floor: StateT IO .......................................................................188
Final Game and Recap ......................................................................................197
Conclusion ........................................................................................................201
Chapter 9: Let’s AI ........................................................................................203
Agentic AI and Large Language Models ............................................................203
LLMs in a Nutshell .......................................................................................205
Haskell and LLMs ..............................................................................................210
Big Picture of the Haskell AI Framework .....................................................211
Terminal Chatbot .........................................................................................212
Conclusion ........................................................................................................231
Conclusion ........................................................................................................234
Chapter 10: Terminal AI Chat Agent ............................................................237
Terminal UI Skeleton .........................................................................................238
Adding a Nicer Response Function ..............................................................239
Initialization .................................................................................................241
Building the Monad Transformer Stack .......................................................242
Main Program ..............................................................................................245
Building Muscles ...............................................................................................248
Logging ........................................................................................................248
Usage Stats in the Writer Monad .................................................................253
Chat History .................................................................................................255
Conclusion ........................................................................................................263
Chapter 11: Web-Enabled AI Framework and GHC “Guts” ...............................265
Terminal UI Improvements ................................................................................265
Example Jarvis Scenarios ...........................................................................270
“M” Subchapter: Under the Hood of GHC ..........................................................278
Adding a Web API ..............................................................................................283
Mutable Variables .............................................................................................294
IORef: Simple Mutable Variables in the IO Monad .......................................295
MVar: Mutable Variables with Concurrency Control .....................................296
TVar: The Power of Software Transactional Memory (STM) .........................298
Conclusion ........................................................................................................300
Chapter 12: Down the Rabbit Hole ..................................................................303
MongoDB as a Persistence Layer .....................................................................304
MongoDB Initialization .................................................................................308
Adding a System Prompts Collection ..........................................................310
Redesigning Architecture: MRWST Monad Transformer ...................................313
Vectors and Type Families ................................................................................321
Type Synonym Families ...............................................................................322
Data Families ...............................................................................................323
Associated Type and Data Families .............................................................325
Vectors As Arrays in Haskell ........................................................................330
More AI: Vector RAG ..........................................................................................340
In-Memory Vector Engine ............................................................................344
Conclusion ........................................................................................................358
Chapter 13: AI Multi-agents, Arrows, and the Future .............................................359
Arrows: The Last Abstraction ............................................................................361
Arrows Definition and Basic Examples ........................................................364
Arrows Interface ..........................................................................................367
AI Multi-agents and Arrows ..............................................................................373
AI Multi-agents Examples ............................................................................377
Arrows to Support Quick AI Multi-agent Creation ........................................379
Final Improvements for the AI Framework ........................................................390
Web Authentication with JWTs ....................................................................390
Error Handling: Embracing the Try–Catch Approach ....................................396
Multiple LLMs and Agents Support ..............................................................399
Conclusion ..............................................................................................404
Index .....................................................................................................407
Discover a unique and fun approach to adopting modern typed functions programming patterns. This book uses playful metaphors and examples to help you learn Haskell through imagination, building on math without relying on imperative crutches or technical complexity. You’ll use math to build completely different Typed Functional patterns from the ground up and understand the link between building Mathematics through Types and constructing Haskell as a programming language.
Intended for working with various applications, especially AI-powered apps, the book gently builds up to what are normally considered complex and difficult concepts all without needing a PhD to understand them. Illustrative explanations will guide you to tackle monads, using monad transformer stacks to structure real programs, foldable and traversable structures, as well as other Type classes. This book will also help you structure programs efficiently and apply your own abstractions to real-life problem domains. Next, you’ll explore exciting advancements in AI, including building with OpenAI APIs, creating a terminal chatbot, adding web functionality, and enhancing with retrieval-augmented generation. Finally, you’ll delve into AI multi-agents and future directions using Arrows abstraction, reinforcing Haskell’s design.
Magical Haskell is a solution for programmers who feel limited by imperative programming languages but are also put off by excessively mathematical approaches.
Grasp a solid math foundation without complex technicalities for Types and Typeclasses.
Solve problems via a typed functional approach and understand why it’s superior to what’s available in the imperative language world (“if it compiles, it runs”).
Build your own abstractions to efficiently resolve problems in any given domain.
Develop AI frameworks in Haskell, including chatbots, web functionality, and retrieval-augmented generation.
Haskell programmers of all levels and those interested in Type Theory.