-
-
Save qnikst/a6568ed6598e9760f2cfa31c1b971d3a to your computer and use it in GitHub Desktop.
TF (juick : http://juick.com/segfault/2863032#5)
This file contains hidden or 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 TypeFamilies #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
import Data.Time | |
type family TimeDiff a where | |
TimeDiff UTCTime = NominalDiffTime | |
TimeDiff a = a | |
class Diffable a {-| a -> Diff a-} where | |
diff :: a -> a -> TimeDiff a | |
addDiff :: TimeDiff a -> a -> a | |
instance {-# OVERLAPPABLE #-} (Num a, TimeDiff a ~ a) => Diffable a where | |
diff = (-) | |
addDiff = (+) | |
instance {-# OVERLAPPING #-} Diffable UTCTime where | |
diff = diffUTCTime | |
addDiff = addUTCTime | |
-- Compiles. | |
{- | |
import Data.Time | |
class Diffable a where | |
type Diff a :: * | |
diff :: a -> a -> Diff a | |
addDiff :: Diff a -> a -> a | |
instance {-# OVERLAPPABLE #-} (Num a) => Diffable a where | |
type Diff a = a | |
diff = (-) | |
addDiff = (+) | |
instance Diffable UTCTime where | |
type Diff UTCTime = NominalDiffTime | |
diff = diffUTCTime | |
addDiff = addUTCTime | |
-- Conflicting family instance declarations: | |
-- Diff a -- Defined at src/Peridot/Diffable.hs:11:8 | |
-- Diff UTCTime -- Defined at src/Peridot/Diffable.hs:16:8 | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment