Skip to content

Instantly share code, notes, and snippets.

@glaebhoerl
Forked from rwbarton/A.hs
Last active July 4, 2017 23:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaebhoerl/f606757a91c66bccc0ea to your computer and use it in GitHub Desktop.
Save glaebhoerl/f606757a91c66bccc0ea to your computer and use it in GitHub Desktop.
-- Code taken from http://stackoverflow.com/questions/12735274/breaking-data-set-integrity-without-generalizednewtypederiving/12744568#12744568
-- Discussion on haskell-cafe: http://thread.gmane.org/gmane.comp.lang.haskell.cafe/100870
-- http://www.haskell.org/pipermail/haskell-cafe/2012-October/103984.html
-- Modified to remove orphan instances by rwbarton
-- Simplified by glaebhoerl
module A (A(..), Set, empty, insert, on) where
import Data.Set
import Data.Function
data A x y = B | C deriving (Eq, Show)
{-# LANGUAGE FlexibleInstances #-}
module B where
import A
data B = MkB
instance Ord (A B c) where
compare = compare `on` (== B)
insertB :: A B c -> Set (A B c) -> Set (A B c)
insertB = insert
{-# LANGUAGE FlexibleInstances #-}
module C where
import A
data C = MkC
instance Ord (A b C) where
compare = compare `on` (== C)
insertC :: A b C -> Set (A b C) -> Set (A b C)
insertC = insert
module Main where
import A
import B
import C
test :: Set (A B C)
test = insertC B $ insertB B $ insertB C $ empty
main :: IO ()
main = print test
!glaebhoerl@e135:~/test/f606757a91c66bccc0ea$ ghc -Wall -XSafe -fforce-recomp --make D.hs
[1 of 4] Compiling A ( A.hs, A.o )
[2 of 4] Compiling B ( B.hs, B.o )
[3 of 4] Compiling C ( C.hs, C.o )
[4 of 4] Compiling Main ( D.hs, D.o )
Linking D ...
!glaebhoerl@e135:~/test/f606757a91c66bccc0ea$ ./D
fromList [B,C,B]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment