Skip to content

Instantly share code, notes, and snippets.

@wandernauta
Last active November 30, 2016 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wandernauta/151fca420531e3427bbb to your computer and use it in GitHub Desktop.
Save wandernauta/151fca420531e3427bbb to your computer and use it in GitHub Desktop.
Floating-point Haskell type for use in VW diesel engines
import VWFloat
testResult :: VWFloat
testResult = 214.0
limit :: VWFloat
limit = 200.0
main = do
putStrLn "Emissions within limit?"
putStrLn $ show $ testResult < limit
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module VWFloat (VWFloat) where
import System.Environment
import System.IO.Unsafe
import Data.Maybe
defeatDevice :: Bool
defeatDevice = isJust $ unsafePerformIO $ lookupEnv "EMISSIONS_TEST"
instance Ord VWFloat where
compare (VWFloat a) (VWFloat b) = if defeatDevice then LT else (compare a b)
newtype VWFloat = VWFloat Float
deriving (Eq, Show, Read, Floating, RealFrac,
Fractional, Real, Num, RealFloat)
@wandernauta
Copy link
Author

Usage:

$ ./Main
Emissions within limit?
False
$ EMISSIONS_TEST=1 ./Main
Emissions within limit?
True

Beautiful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment