Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
Forked from kevinmeredith/JoinList.hs
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelXavier/e96f07562a50ddb38253 to your computer and use it in GitHub Desktop.
Save MichaelXavier/e96f07562a50ddb38253 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Applicative
import Data.Monoid
import Test.QuickCheck
data JoinList m a = Empty
| Single m a
| Append m (JoinList m a) (JoinList m a)
deriving (Eq, Show)
-- where `m` is a Monoid
newtype Size = Size Int
deriving (Eq, Ord, Show, Num)
getSize :: Size -> Int
getSize (Size i) = i
class Sized a where
size :: a -> Size
instance Sized Size where
size = id
-- This instance means that things like
-- (Foo, Size)
-- (Foo, (Bar, Size))
-- ...
-- are all instances of Sized.
instance Sized b => Sized (a,b) where
size = size . snd
instance Monoid Size where
mempty = Size 0
mappend = (+)
instance (Arbitrary m, Arbitrary a) => Arbitrary (JoinList m a) where
arbitrary = oneof [ pure Empty
, Single <$> arbitrary <*> arbitrary
, Append <$> arbitrary <*> arbitrary <*> arbitrary
]
main :: IO ()
main = quickCheck \(jl :: JoinList [Int] Int) (error "your property here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment