From Haskell to Hardware

Star Build Status Hackage

Fork me on GitHub


FIR filter

fir hs x = dotp hs (window x)
    dotp as bs = foldr1 (+) (zipWith (*) as bs)
  Parametric in the number of filter taps
  Parametric in the data type
  Strongly-typed, yet free from type annotations

Why use CλaSH

CλaSH (pronounced ‘clash’) is a functional hardware description language that borrows both its syntax and semantics from the functional programming language Haskell. The merits of using a functional language to describe hardware comes from the fact that combinational circuits can be directly modeled as mathematical functions and that functional languages lend themselves very well at describing and (de-)composing mathematical functions. The CλaSH compiler transforms these high-level descriptions to low-level synthesizable VHDL, Verilog, or SystemVerilog.

setup details

How do I install CλaSH?

Please file a bug report if the following installation instructions do not work for you.

The CλaSH compiler is written in Haskell and is compiled from source using the the GHC Haskell compiler version 7.10.* or higher:

  1. Install GHC (version 7.10.* or higher)

    • Download GHC for your platform. Unix users can use ./configure prefix=<LOCATION> to set the installation location
    • Make sure the bin directory of GHC is in your PATH

    Alternatives, if you cannot find what you are looking for on http://www.haskell.org/ghc/download

    • Ubuntu:
      • sudo add-apt-repository -y ppa:hvr/ghc
      • sudo apt-get update
      • sudo apt-get install cabal-install-1.22 ghc-7.10.1 libtinfo-dev
      • update your PATH with: /opt/ghc/7.10.1/bin, /opt/cabal/1.22/bin and $HOME/.cabal/bin
      • Run cabal update
      • Skip step 2.
    • OS X:
    • Windows:
      • Follow the instructions on: MinGHC
      • Run cabal update
      • Skip step 2.
  2. Install Cabal (version 1.22.* or higher)

    • Binary, when available:
      • Download the binary for cabal-install
      • Put the binary in a location mentioned in your PATH
      • Add cabal’s bin directory to your PATH:
        • Windows: %appdata%\cabal\bin
        • Unix: $HOME/.cabal/bin
    • Source:
      • Download the sources for cabal-install
      • Unpack (tar xf) the archive and cd to the directory
      • Run sh bootstrap.sh
      • Follow the instructions to add cabal to your PATH
    • Run cabal update
  3. Install CλaSH

    • Run:
      • i386 Linux: cabal install clash-ghc --enable-documentation --enable-executable-dynamic
      • Other: cabal install clash-ghc --enable-documentation
    • This is going to take awhile, so have a refreshment xkcd

Alright, you’ve got the compiler installed, now it’s time for some testing:

  • Download the FIR.hs example
  • Run clash --interactive FIR.hs
  • Execute, in the interpreter, the :vhdl command.
  • Execute, in the interpreter, the :verilog command.
  • Execute, in the interpreter, the :systemverilog command.
  • Exit the interpreter using :q
  • Examine the VHDL code in the vhdl directory
  • Examine the Verilog code in the verilog directory
  • Examine the SystemVerilog code in the systemverilog directory