Created
December 13, 2014 23:51
-
-
Save jfischoff/b2f8c60883f2cfd0b187 to your computer and use it in GitHub Desktop.
Use case for reflection non law abiding Eqs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE StandaloneDeriving #-} | |
module Approx where | |
import Data.Reflection | |
import Data.Proxy | |
import Foreign.Storable | |
newtype Approx s = Approx { unApprox :: Double } | |
deriving (Num, Show, Read, Storable) | |
deriving instance Reifies s Double => Ord (Approx s) | |
deriving instance Reifies s Double => Fractional (Approx s) | |
deriving instance Reifies s Double => Real (Approx s) | |
deriving instance Reifies s Double => RealFrac (Approx s) | |
deriving instance Reifies s Double => Floating (Approx s) | |
deriving instance Reifies s Double => RealFloat (Approx s) | |
deriving instance Reifies s Double => Enum (Approx s) | |
mkApprox :: p s -> Double -> Approx s | |
mkApprox _ = Approx | |
-- replace with something like one of the anwsers in | |
-- http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison | |
instance Reifies s Double => Eq (Approx s) where | |
Approx x == Approx y = abs (y - x) < reflect (Proxy :: Proxy s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment