Skip to content

Instantly share code, notes, and snippets.

@jfischoff
Created December 13, 2014 23:51
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 jfischoff/b2f8c60883f2cfd0b187 to your computer and use it in GitHub Desktop.
Save jfischoff/b2f8c60883f2cfd0b187 to your computer and use it in GitHub Desktop.
Use case for reflection non law abiding Eqs
{-# 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