liveBook · Manning

Type alias

As explained in Learn Haskell by Example

A type synonym gives an existing type an alternate name without creating a distinct representation or type identity.

A Haskell type alias, or type synonym, gives an existing type a clearer name without creating a distinct representation or type identity. Use the type keyword when the name explains how values are used. section 2.3.1 section 2.6

Naming an alphabet

An alphabet is represented as a list of characters, just like a String, but the alias communicates its role in a substitution cipher: type Alphabet = [Char] lowerAlphabet :: Alphabet lowerAlphabet = ['a' .. 'z'] upperAlphabet :: Alphabet upperAlphabet = ['A' .. 'Z'] digits :: Alphabet digits = ['0' .. '9'] Alphabet values remain lists of Char. The alias improves readability; it does not prevent ordinary String and Alphabet values from being used interchangeably. section 2.3.1

Aliases become especially useful when a general representation has a more specific purpose:

  • Use String for human-readable text and Alphabet for characters used by a cipher. section 2.3.1
  • Use FilePath when a String represents a filename; the two types remain interchangeable. section 3.3.1
  • Inspect aliases and values with :type and :info, and refresh changed definitions with :reload in GHCi. section 2.3.1 section 2.6
type Alphabet = [Char]

Inspect the compact alias declaration that gives a list of characters a domain-specific name. section 2.3.1

How Learn Haskell by Example covers it

How deep it goes
Defines aliases, demonstrates interchangeability, and stops short of creating a distinct type identity.
Examples
Uses Alphabet for cipher alphabets, FilePath for command-line filenames, and GHCi commands for inspection.
What it assumes
Assumes basic familiarity with Haskell types and lists, then teaches domain-specific naming through concrete code.
What you can do after
Apply type synonyms in Haskell code and inspect their resulting values and signatures through GHCi.
How it connects
Connects type synonyms with String, FilePath, lists, pattern matching, and GHCi inspection commands.

Also explained in

Compare how 6 books explain type alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.