Skip to content

Instantly share code, notes, and snippets.

@qnikst
Forked from s9gf4ult/lol.hs
Last active February 24, 2017 07:13
Show Gist options
  • Save qnikst/a6568ed6598e9760f2cfa31c1b971d3a to your computer and use it in GitHub Desktop.
Save qnikst/a6568ed6598e9760f2cfa31c1b971d3a to your computer and use it in GitHub Desktop.
{-# 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