[Haskell-cafe] is there a way to run tests at compile time using TH

Chris Mears chris at cmears.id.au
Sun Aug 26 09:13:27 EDT 2007

Alex Jacobson <alex at alexjacobson.com> writes:

> I'd like to have code not compile if it doesn't pass the tests.
> Is there a way to use TH to generate compiler errors if the tests
> don't pass?

What about something like this?

    import Language.Haskell.TH

    import Tests         -- this should export "testsPass :: Bool"

    -- don't bother trying to call this function.
    check = $( if testsPass then [| () |]
               else error "test failed" )

    main = print "hi"

If testsPass returns False, the compiler will throw an exception during
compilation, like this:

    Exception when trying to run compile-time code:
      test failed

More information about the Haskell-Cafe mailing list