Magical Haskell: A Friendly Approach to Modern Functional Programming, Type Theory, and Artificial Intelligence
Book information
Description
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. What You Will Learn • 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. Who This Book Is For Haskell programmers of all levels and those interested in Type Theory. Table of Contents About the Author About the Technical Reviewer Acknowledgments Introduction How This Book Came to Be: Instead of an Introduction Chapter 1: Wizards, Types, and Functions Solving Problems As Wizards Do Let’s Fall in Love with Types! A Char and an Int Walk into a Bar and Make a Function How FunTypical! Curry and Recurse! Conclusion Chapter 2: Type Construction House of Cards with a Little Help from Algebra Algebraic Data Types Records Type Functions Are Functions, Maybe? Advanced: On Data Constructors and Types List, Recursion on Types and Patterns Length of a List Map a Function Over a List of Values Conclusion Chapter 3: Very Gentle Type Theory and Category Theory Intro Types and Functions Maybe and Advanced Generalized Functions Dependent Function Types (Pi-Types) Sum, Product, and Dependent Pair (Sigma) Types Very Gentle Category Theory Introduction Typeclasses Conclusion Chapter 4: Basic Typeclasses or “Show Me a Monoid” Show Typeclass Algebra Is Cool Typeclass Hierarchy in Haskell Lift Me Up! Conclusion Chapter 5: Functor, Bifunctor, and Applicative Functor Enter an Elevator … Functor Typeclass Definition Three-Dimensional Vector Example Tracking Players in Our Game You Are Either Functor or a Bifunctor We Need a Bigger Lift Action! Apply! Cut! Applicative Typeclass Conclusion Chapter 6: O, Monad, Help Me Compose! Can We Play Cards in This State? Monad Typeclass and Basic Monads Reader–Writer–State Triple The State Monad The Reader Monad The Writer Monad Conclusion Chapter 7: Input, Transformer Stack, Output Do Notation Monad Transformer Stacks Conclusion Chapter 8: Blackjack: Full Haskell Program Preparation Initial “Pure” Design Building the First Floor: IO Building the Top Floor: StateT IO Final Game and Recap Conclusion Chapter 9: Let’s AI Agentic AI and Large Language Models LLMs in a Nutshell Three Ways to Make LLMs Better Vector RAG Additional Fine-Tuning Better Original Training Data Haskell and LLMs Big Picture of the Haskell AI Framework Terminal Chatbot High-Level Design Initial RWS Monad Designing the OpenAI Interface Converting Between Haskell Types and JSON The Message Type ChatOptions: Configuring the Chat Interaction How ToJSON and FromJSON Work Handling Optional Fields aeson in Action: API Requests and Responses Conclusion on aeson Sending API Requests: The chatCompletion Function Handling the Streaming Response Conclusion Conclusion Chapter 10: Terminal AI Chat Agent Terminal UI Skeleton Adding a Nicer Response Function Initialization Building the Monad Transformer Stack Main Program Building Muscles Logging Usage Stats in the Writer Monad Chat History Conclusion Chapter 11: Web-Enabled AI Framework and GHC “Guts” Terminal UI Improvements Example Jarvis Scenarios Format Conversions Code Explanation Error Fixing/Working with the Compiler “M” Subchapter: Under the Hood of GHC Adding a Web API Mutable Variables IORef: Simple Mutable Variables in the IO Monad Use Cases for IORef When to Avoid IORef MVar: Mutable Variables with Concurrency Control Use Cases for MVar When to Avoid MVar TVar: The Power of Software Transactional Memory (STM) Use Cases for TVar When to Avoid TVar The Hierarchy Visualized Conclusion Chapter 12: Down the Rabbit Hole MongoDB as a Persistence Layer MongoDB Initialization Adding a System Prompts Collection Redesigning Architecture: MRWST Monad Transformer Vectors and Type Families Type Synonym Families Data Families Associated Type and Data Families Vectors As Arrays in Haskell Understanding Vectors: Boxed vs. Unboxed Internal Implementation and Generic Interface Mutable Vectors and the ST Monad: For When Performance Is Key Rank-N Types Vectors in Practice More AI: Vector RAG In-Memory Vector Engine Mongo Storage Part In-Memory Store Part Finishing RAG Conclusion Chapter 13: AI Multi-agents, Arrows, and the Future Arrows: The Last Abstraction Arrows Definition and Basic Examples Arrows Interface AI Multi-agents and Arrows AI Multi-agents Examples Arrows to Support Quick AI Multi-agent Creation Final Improvements for the AI Framework Web Authentication with JWTs Error Handling: Embracing the Try–Catch Approach Catching Exceptions: The Basics Going the Extra Mile with Catch Exception Handling with Style: The Handle Function Multiple LLMs and Agents Support Conclusion Index
Similar books
MySQL® Notes for Professionals book
2018 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
MrExcel 2022: Boosting Excel
2022 · PDF
Session C11: Ancient Cultural Landscapes in South Europe – their Ecological Setting and Evolution, Session C22: Gardeners from South America, Session S04: Agro-Pastoralism and Early Metallurgy Sessions, Session WS29: The Idea of Enclosure in Recent Iberian Prehistory, Session C88: Rhytmes et causalites des dynamiques de l'anthropisation en Europe entre 6500 ET 500 BC: Hypotheses socio-culturelles et/ou climatiques: Proceedings of the XV UISPP World Congress (Lisbon 4-9 September 2006) / Actes du XV Congrès Mondial (Lisbonne 4-9 Septembre 2006) Vol.36
2010 · PDF
THE BRITISH ARMY IN INDIA: ITS PRESERVATION BY AN APPROPRIATE CLOTHING, HOUSING, LOCATING, RECREATIVE EMPLOYMENT, AND HOPEFUL ENCOURAGEMENT OF THE TROOPS. with AN APPENDIX ON INDIA : THE CLIMATE OP ITS HILLS ; THE DEVELOPMENT OF ITS RESODRCBS, INDUSTRY, AND ARTS ; THE ADMINISTRATION OF JUSTICE ; THE BLACK ACT ; THE PROGRESS OF CHRISTIANITY ; THE TRAFFIC IN OPIUM ; THE VALUE OF INDIA ; PERMANENT CAUSES OF DISAFFECTION, AND OF THE RECENT REBELLION ; THE TRADITIONARY POLICY; MISGOVERNMENT BY NATIVE RULERS ; ANNEXATIONS OF THEIR TERRITORY, ETC.
1858 · PDF
Idries Shah 27 Books Collection : A Perfumed Scorpion, A Veiled Gazelle, Caravan of Dreams, Darkest England, Destination Mecca, Evenings with Idries Shah, Knowing How to Know, Learning How to Learn, Letters and Lectures of Idries Shah, Neglected aspects of Sufi study, Observations, Oriental Magic, Reflections, Seeker after Truth, Special Illumination, Special Problems in the study of Sufi ideas, Sufi thought and action, Tales of the Dervishes, The Dermis Probe, The Elephant in the Dark, The Englishman Handbook, Idries Shah Antology, The Magic Monastery, The natives are restless, wisdom of the Idiots PDF.
2022 · PDF
The travels of Capts. Lewis and Clarke from St. Louis, by way of the Missouri and Columbia rivers, to the Pacific ocean; performed in the years 1804, 1805 & 1806, by order of the government of the United States. Containing delineations of the manners, customs, religion, &c. of the Indians, comp. from various authentic sources, and original documents, and a summary of the Statistical view of the Indian nations, from the official communication of Meriwether Lewis. Illustrated with a map of the country, inhabited by the western tribes of Indians
1809 · PDF
Professional Linux kernel architecture ''Wrox programmer to programmer''--Cover. - ''What you are reading right now is the result of an evolution over more than seven years: After two years of writing, the first edition was published in German by Carl Hanser Verlag in 2003. It then described kernel 2.6.0. The test was used as a basis for the low-level design documentation for the EAL4+ security evaluation of Red Hat Enterprise Linux 5, requiring to update it to kernel 2.6.18 (if the EAL acronym does not mean anything to you, then Wikipedia is once more your friend). Hewlett-Packard sponsored the translation into English and has, thankfully, granted the rights to publish the result. Updates to kernel 2.6.24 were then performed specifically for this book''--P. ix
2008 · PDF