CS 214: Programming Languages
Spring 2009

Home|Syllabus|Schedule

Haskell Templates

There are templates for Haskell files and a Rakefile for processing Haskell files.

When you're finished, you'll have these rake commands:

Haskell Files

You may find these templates handy when creating new Haskell files. Follow the instructions in the TODO comments, and then delete the comments.

This is for a new computation module.

Module.hs

module Module where

{- TODO: write the computation functions here -}

Test the computation functions in a test module.

ModuleTest.hs

module Main where

import Test.HUnit
import System
{- TODO: change the module name -}
import Module

{- TODO: Change the variable name.  Add more variables. -}
moduleTests = TestList [
  TestCase (assertEqual "write real test cases" 1 0)
  {- TODO: add more test cases -}
  ]

checkTests counts = case counts of
        Counts _ _ errors failures ->
                if (errors + failures > 0)
                then exitFailure
                else (exitWith ExitSuccess)

main = runTestTT
        (TestList [
          {- TODO: use test variables you've defined above -}
          TestLabel "templated variable of tests" moduleTests
          ])
       >>= checkTests

This is for a command-line driver. (These are very rare in my world.)

ModuleMain.hs

module Main where

import Data.String
import System.Environment
import System.Exit

{- TODO: import the appropriate modules -}
import Factorial

{- TODO: change "computation" to the computation you want to run on the
  command-line arguments -}
main = getArgs >>= putAnswers . (map computation) . parse

parse args     = map read args

putAnswers l   = putStr (unlines (map show l))

Rakefile

Each Haskell subfolder gets its own Rakefile:

Rakefile

def test_files
  # TODO: list all executable tests, without .hs!
  ["FactorialTest"]
end

def driver_files
  # TODO: list all executable drivers, without .hs!
  ["FactorialMain"]
end

load '../haskell.rake'

But it only works if you put this file in the _languages/haskell folder:

haskell.rake

require 'rake/clean'

SRC = FileList['*.hs']
CLEAN.include('*~')
CLOBBER.include('*.hi', '*.o')
CLOBBER.include(test_files)
CLOBBER.include(driver_files)

task :default => :compile

task :compile do
  (test_files + driver_files).each do |f|
    system "ghc -O2 --make #{f}.hs"
  end
end

task :test => :compile do
  test_files.each do |f|
    system f
  end
end