The Haskell ecosystem consists of various tools, libraries, and resources that support Haskell development. Understanding this ecosystem is valuable for working on larger projects.

Core Components

GHC: The Glasgow Haskell Compiler

GHC is the most widely used Haskell compiler, providing:

  • A compiler (ghc) that produces native code
  • An interpreter (ghci) for interactive development
  • The Haskell Language Server for IDE integration
  • Support for many language extensions
  • Optimizations for high-performance code

Hackage

Hackage is the central package repository for Haskell libraries, containing:

  • Thousands of open-source packages
  • Extensive documentation and API references
  • Version history and dependency information
  • Package metadata and compatibility information

Cabal

Cabal (Common Architecture for Building Applications and Libraries) is a system for:

  • Building and packaging Haskell libraries and programs
  • Managing dependencies between packages
  • Installing packages from Hackage
  • Configuring build options

Stack

Stack is a cross-platform build tool that addresses some of Cabal’s limitations:

  • Reproducible builds with pinned dependency versions
  • Isolated project environments
  • Automatic installation of appropriate GHC versions
  • Project templates for faster startup
  • Integration with Stackage (curated consistent package sets)

Development Tools

Hoogle

Hoogle is a search engine for Haskell APIs that allows:

  • Searching for functions by name
  • Searching for functions by type signature
  • Finding implementations across multiple libraries
  • Quick access to documentation

IDEs and Editor Support

Common choices for Haskell development:

  • Visual Studio Code with Haskell extension
  • IntelliJ IDEA with HaskForce plugin
  • Emacs with haskell-mode
  • Vim with haskell-vim
  • Atom with ide-haskell

These typically provide:

  • Syntax highlighting
  • Type information on hover
  • Auto-completion
  • Error checking
  • Jump to definition
  • Refactoring tools

Data Structures and Algorithms

  • containers: Efficient container types (Map, Set, Sequence, etc.)
  • vector: Efficient arrays
  • unordered-containers: Hash-based containers
  • array: Native arrays

Parsing

  • parsec: Parser combinator library
  • megaparsec: Modern fork of parsec with better error messages
  • attoparsec: Fast parser combinator library for ByteStrings

Web Development

  • scotty: Lightweight web framework
  • servant: Type-level web APIs
  • yesod: Full-featured web framework with strong type safety
  • wai/warp: Web application interface and server

Concurrency and Parallelism

  • async: Asynchronous operations
  • stm: Software transactional memory
  • parallel: Parallel programming utilities

Testing

  • QuickCheck: Property-based testing
  • hspec: BDD-style testing
  • HUnit: Unit testing
  • tasty: Test framework aggregator

Database Access

  • postgresql-simple: PostgreSQL client
  • sqlite-simple: SQLite client
  • persistent: Database access with type safety
  • esqueleto: SQL DSL for type-safe queries

Building Projects

Project Structure

A typical Stack project has the following structure:

my-project/
├── app/                  # Executable code
│   └── Main.hs
├── src/                  # Library code
│   ├── MyLib.hs
│   └── ...
├── test/                 # Test code
│   └── Spec.hs
├── README.md
├── CHANGELOG.md
├── package.yaml         # Stack configuration (higher-level)
├── my-project.cabal     # Generated from package.yaml
├── stack.yaml           # Stack project configuration
└── .gitignore

Configuration Files

package.yaml (for Stack)

name: my-project
version: 0.1.0.0
github: "username/my-project"
license: BSD3
author: "Author Name"
maintainer: "email@example.com"
copyright: "2023 Author Name"
 
dependencies:
  - base >= 4.7 && < 5
  - text
  - containers
 
library:
  source-dirs: src
 
executables:
  my-project-exe:
    main: Main.hs
    source-dirs: app
    ghc-options:
      - -threaded
      - -rtsopts
      - -with-rtsopts=-N
    dependencies:
      - my-project
 
tests:
  my-project-test:
    main: Spec.hs
    source-dirs: test
    ghc-options:
      - -threaded
      - -rtsopts
      - -with-rtsopts=-N
    dependencies:
      - my-project
      - hspec
      - QuickCheck

.cabal file (for Cabal)

cabal-version: 1.12
name: my-project
version: 0.1.0.0
license: BSD3
license-file: LICENSE
author: Author Name
maintainer: email@example.com
copyright: (c) 2023 Author Name
build-type: Simple

library
  exposed-modules:
      MyLib
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
    , containers
    , text
  default-language: Haskell2010

executable my-project-exe
  main-is: Main.hs
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , my-project
  default-language: Haskell2010

test-suite my-project-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      QuickCheck
    , base >=4.7 && <5
    , hspec
    , my-project
  default-language: Haskell2010

Common Commands

Stack Commands

# Create a new project
stack new project-name
 
# Build project
stack build
 
# Run executable
stack exec project-name-exe
 
# Run GHCi with project modules loaded
stack ghci
 
# Run tests
stack test
 
# Install a package
stack install package-name
 
# Clean build artifacts
stack clean

Cabal Commands

# Initialize a project
cabal init
 
# Configure the project
cabal configure
 
# Build project
cabal build
 
# Run executable
cabal run executable-name
 
# Run GHCi with project modules loaded
cabal repl
 
# Run tests
cabal test
 
# Install a package
cabal install package-name
 
# Clean build artifacts
cabal clean

Advanced GHC Features

Language Extensions

GHC supports many language extensions that add features beyond Haskell 2010:

{-# LANGUAGE OverloadedStrings #-}  -- String literals for other string-like types
{-# LANGUAGE GeneralizedNewtypeDeriving #-}  -- Automatic deriving for newtypes
{-# LANGUAGE TypeFamilies #-}  -- Type-level functions
{-# LANGUAGE GADTs #-}  -- Generalized algebraic data types
{-# LANGUAGE RankNTypes #-}  -- Higher-rank polymorphism

Optimization Flags

# Compile with optimization
ghc -O2 Main.hs
 
# Enable specific optimizations
ghc -fspecialise-aggressively Main.hs
 
# Generate profiling information
ghc -prof -fprof-auto Main.hs

Key Points to Remember

  1. GHC is the primary Haskell compiler with an extensive feature set
  2. Stack provides reproducible builds with project isolation
  3. Hackage hosts thousands of libraries with documentation
  4. Hoogle helps find functions by name or type signature
  5. The ecosystem includes libraries for parsing, web development, testing, and more
  6. Language extensions provide powerful features beyond standard Haskell