Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile-time assertions with GHC Haskell?

Coming from C++, I'm used to be able to build simple forms of compile-time assertions, where I could emit warnings or errors during compilation if some simple conditions (e.g. over simple algebraic expressions) weren't met via use of template meta-programming and/or cpp(1)

For instance, if I wanted to make sure my program compiles only when Int has at least a certain minBound/maxBound range or alternatively, if a loss-free (as in reversible) conversion from Int64 to Int is possible with the current compilation target. Is this possible with some GHC Haskell extension? My first guess would have been to use TH. Are there other GHC facilities that could be exploited to this end?

like image 998
hvr Avatar asked Jul 11 '11 10:07

hvr


2 Answers

Here's a generalized and slightly simplified version of Anthony's example:

{-# LANGUAGE TemplateHaskell #-}
module StaticAssert (staticAssert) where

import Control.Monad (unless)
import Language.Haskell.TH (report)

staticAssert cond mesg = do
    unless cond $ report True $ "Compile time assertion failed: " ++ mesg
    return [] -- No need to make a dummy declaration

Usage:

{-# LANGUAGE TemplateHaskell #-}
import StaticAssert

$(staticAssert False "Not enough waffles")
like image 176
hammar Avatar answered Nov 14 '22 13:11

hammar


Using TH for this isn't too bad. Here is a module that defines the desired assertion as part of a vestigial declaration:

{-# LANGUAGE TemplateHaskell #-}
module CompileTimeWarning where
import Control.Monad (unless)
import Data.Int (Int64)
import Language.Haskell.TH

assertInt = let test = fromIntegral (maxBound::Int) == (maxBound::Int64)
            in do unless test $ report True "Int is not safe!"
                  n <- newName "assertion"
                  e <- fmap NormalB [|()|]
                  return $ [FunD n [Clause [] e []]]

Using the assertion involves a top-level declaration that isn't used for anything other than the assertion:

{-# LANGUAGE TemplateHaskell #-}
import CompileTimeWarning
$(assertInt)
like image 6
Anthony Avatar answered Nov 14 '22 14:11

Anthony