Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Solonarv/913b0dd293f076a996cea4e9ce3a2224 to your computer and use it in GitHub Desktop.
Save Solonarv/913b0dd293f076a996cea4e9ce3a2224 to your computer and use it in GitHub Desktop.
data FooF = Foo { alpha :: HKD f Int, beta :: HKD f Int, gamma :: HKD f Int
, delta :: HKD f Int, eta :: HKD f Int } deriving (Eq, Ord, Show)
type Foo = FooF Identity
newtype Combine a = C { applyC :: a -> a -> a }
combineFoos :: FooF Combine -> Foo -> Foo -> Foo
combineFoos (Foo fa fb fg fd fe) (Foo a0 b0 g0 d0 e0) (Foo a1 b1 g1 d1 e1)
= Foo (applyC fa a0 a1) (applyC fb b0 b1) (applyC fg g0 g1) (applyC fd d0 d1) (applyC fe e0 e1)
newtype AddFoo = AddFoo { getAddFoo :: Foo }
instance Semigroup AddFoo where
AddFoo x <> AddFoo y = coerce $ combineFoos $ Foo (C (+)) (C max) (C (+)) (C max) (C (+))
instance Monoid AddFoo where
mempty = AddFoo (Foo 0 0 0 0 0)
mappend = (<>)
newtype MaxFoo = MaxFoo { getMaxFoo :: Foo }
instance Semigroup MaxFoo where
(<>) = coerce $ combineFoos $ Foo (C max) (C max) (C max) (C max) (C max)
instance Monoid MaxFoo where
mempty = MaxFoo (Foo 0 0 0 0 0)
mappend = (<>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment