-
Notifications
You must be signed in to change notification settings - Fork 4
0003: Email Tutorials Haskell For Beginners ‐ Using deriving Show in Haskell as a Beginner
- 3.1. Why This Topic Matters for Beginners
- 3.2. Custom Types: Day, Month, and Year
- 3.3. The Problem in GHCi: “No instance for Show …”
- 3.4. The Quick Fix: Adding
deriving Show - 3.5. A Partial Explanation: What
deriving ShowTells the Compiler - 3.6. How to Use
deriving ShowFor Now - 3.7. Looking Ahead: Type Classes in a Future Module
When you are just getting started with Haskell, one of the most useful tools is GHCi, the interactive environment where you can load your code, run expressions, and see results immediately. However, beginners often run into a confusing error as soon as they start defining their own types and trying to inspect values in GHCi. This can be discouraging because everything appears to compile correctly, yet asking GHCi to “show” a value suddenly fails. This tutorial focuses on that exact situation and introduces the deriving Show technique as a practical solution you can use right away, even before you fully understand all the underlying theory.
Earlier in the course, three new types were defined: Day, Month, and Year. These types were introduced to give more structure and clarity to date-related values, rather than representing everything as raw integers. Once these types are defined, it is natural to want to experiment with them in GHCi. For example, you might have a value like d1 of type Day and want to see what it looks like. The expectation is that GHCi will print the value back to you, allowing you to interact with your new types just as you would with built-in ones.
After loading the code that defines Day, Month, and Year, everything may compile successfully, which gives the impression that all is well. The surprise comes when you attempt to inspect a value (such as d1) in GHCi. Instead of printing the value, GHCi reports an error that says there is “no instance for Show Day”. For a beginner, this message can be quite mysterious. It suggests that something is missing, but it does not immediately explain what needs to be added or why. At this point in the course, the full concept of type classes has not yet been explained, which puts you in a situation where the error refers to an idea you have not properly encountered. This is the “chicken-and-egg” problem: understanding the error requires knowledge about type classes, but understanding type classes properly depends on being comfortable with types, which are the current focus.
To keep you moving forward and allow you to keep using GHCi effectively, the instructor suggests a practical, temporary solution: add deriving Show to the end of your type declarations for Day, Month, and Year. After modifying your type definitions in this way, you reload your GHCi session. Now, when you ask GHCi to display d1 or any other value of these types, the value appears as expected instead of causing an error. This means you can continue experimenting interactively with your custom types, which is an important part of learning and building intuition about how Haskell behaves.
Even though the course has not yet fully explained type classes, it is useful to have a partial sense of what is happening when you write deriving Show. For each of the types Day, Month, and Year, this clause tells the compiler to automatically generate some default code related to the Show type class. That generated code is responsible for converting values of these types into human-readable strings. When GHCi tries to print a value, it uses this generated code under the hood. Without such an instruction, Haskell does not know how to turn your custom types into strings for display, which is why the error appears. With deriving Show in place, that gap is filled, and your values can now be converted and printed on the screen.
At this stage, you are not expected to fully understand the machinery behind type classes or exactly how the generated code looks. The important practical takeaway is simple: whenever you define your own types and want to inspect values of those types in GHCi, add deriving Show to the type declarations. This gives you the ability to see your values and experiment interactively, which is essential for learning. You can think of it as flipping a switch that enables printing for your custom types, even if the wiring behind that switch is still to be explained later in the course.
The instructor makes it clear that a complete understanding of what deriving Show does must wait until the concept of type classes is introduced properly in a later module. At that point, you will learn exactly what a type class is, how instances are defined, and why Show is just one example among many. For now, the goal is to keep your learning flow smooth: by accepting this temporary, partial explanation and using deriving Show as instructed, you gain the practical ability to explore your code in GHCi while deferring the deeper theory to a more appropriate time in your learning journey.
- GHCi — GHC’s interactive REPL where you load code, run expressions, and see results immediately.
- REPL — “Read–Eval–Print Loop”; an interactive environment that reads input, evaluates it, and prints results.
-
Custom type (user-defined type) — A new type you define yourself (not built-in), like
Day,Month,Year. - Day / Month / Year — Example custom types used to represent date parts with clearer meaning than raw integers.
-
Value (term) — A concrete piece of data of some type (e.g.,
d1 :: Day). - Inspect (in GHCi) — Asking GHCi to print a value so you can see it.
- Compile / Compilation — The process of checking and translating Haskell source code (often via GHC).
-
Error message — Output indicating something went wrong (e.g., missing
Showinstance). -
“No instance for Show …” — A compiler/GHCi error meaning there is no
Showimplementation for that type. - Type class — A Haskell feature that defines a set of functions (capabilities) a type can support.
- Show (type class) — A type class for converting values into a printable string representation.
-
Show instance — The implementation of
Showfor a specific type (e.g.,Show Day). - deriving — A keyword that asks the compiler to automatically generate instances for certain type classes.
-
deriving Show— A clause that tells the compiler to auto-generate aShowinstance for your type. - Auto-generated code — Code produced by the compiler for you (instead of you writing it manually).
- Human-readable string — Text form meant for display (what GHCi prints).
- String — A sequence of characters (text) used for output/display.
- Reload (GHCi) — Loading the file again after changes so new definitions/instances take effect.
- Beginner “chicken-and-egg” problem — An early-learning issue where an error references concepts (type classes) not taught yet.
Bernard Sibanda is a global Technology Entrepreneur, Web3 and Software Consultant with a deep focus on Cardano Blockchain, Midnight and Community building.
Key Positions:
- Founder, CTO, Developer Advocate cohort #1, Fullstake Developer, Cardano Ambassador, Catalyst Project Manager, DREP-WIMS:
- Co-founder of ABL Tech and Cardano Africa Live
- EBU-certified Plutus Pioneer (Plutus/Haskell)
- Cohort #1 Plutus Pioneer Developer
- Catalyst Community Reviewer & Funded Projects Manager
-
DRep for WIMS-Cardano (ID:
drep1yguj8zu48n99pv70yl6ckzt9hdgjy8yjnlqs2uyzcpafnjgu4vkul) - Intersect Developer Advocate
- Intersect Committe Member 2025-2026
- Cardano Marketer,Promoter and blogger
- Cardano Open Source Contributor
- Cardano communities and events organizer and builder
- Cardano Ambassador for South Africa
Official links:
- Stablecoins Dex
- Coxygen Global Universities
- WIMS Cardano Global
- Cardano Africa Live
- WIMS Cardano Videos
- Cardano Smart Contract Videos
- Fullstack IT Consulting
Social links: