Skip to content

Haskell Types Guide — Algebraic Data Types and Type Inference

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Haskell Types Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Haskell's type system is one of the most expressive in any language -- combining algebraic data types (sums and products), type inference, parametric polymorphism, and type classes for compile-time correctness guarantees.

Basic Types

x :: Int        -- machine integer
y :: Integer    -- arbitrary precision integer
z :: Float      -- single precision
w :: Double     -- double precision
c :: Char       -- character
s :: String     -- [Char] (list of chars)
b :: Bool       -- True or False

Type Inference

-- Haskell infers types automatically
x = 42     -- x :: Num a => a
y = 3.14   -- y :: Fractional a => a
z = "hi"   -- z :: String

-- But explicit signatures are good practice
add :: Int -> Int -> Int
add x y = x + y

Algebraic Data Types

-- Product type (like struct)
data Person = Person
  { name :: String
  , age  :: Int
  } deriving (Show)

-- Sum type (like enum)
data Color = Red | Green | Blue
  deriving (Show, Eq)

-- Recursive sum type
data MyList a = Empty | Cons a (MyList a)
  deriving (Show)

Type Aliases

type Name = String
type Age = Int

person :: Name -> Age -> Person
person n a = Person n a

Parametric Polymorphism

-- Works with any type
identity :: a -> a
identity x = x

-- Works with any type in a list
first :: [a] -> a
first (x:_) = x

Common Mistakes

1. Type mismatch errors

Trying to add Int and String: Haskell catches these at compile time.

2. Forgetting deriving Show

print requires Show. Use deriving (Show) on custom types.

3. Confusing data and type

data creates new types, type creates aliases. data needs constructors.

Practice Questions

1. What is the difference between sum and product types? Sum types are "or" (Int or String). Product types are "and" (Int and String). Sum = |, Product = struct.

2. What does deriving (Show) do? Automatically generates a toString-like function for the type.

3. What is a type variable? Lowercase letters in type signatures (a, b) representing any type -- parametric polymorphism.

FAQ

{{< faq question="Can Haskell infer all types?" >}} Almost always. But explicit type signatures are recommended for top-level functions as documentation and to produce better error messages. {{< /faq >}}

{{< faq question="What is the Maybe type?" >}} data Maybe a = Nothing | Just a. It represents an optional value. Similar to Optional in Java or Option in Rust. {{< /faq >}}

{{< faq question="What is the difference between Int and Integer?" >}} Int is fixed-size (64-bit). Integer is arbitrary precision (unbounded). Integer is slower but never overflows. {{< /faq >}}

What's Next

Now learn about pattern matching in Haskell.

Topic Description Link
Pattern Matching Destructuring and case {{< ref "05-pattern-matching" >}}
Lists Working with lists {{< ref "06-lists" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro