Skip to content

Instantly share code, notes, and snippets.

@chowells79
Last active July 8, 2020 20:28
Show Gist options
  • Save chowells79/1f2b74cd1b9a655d9f97db4d78fa1762 to your computer and use it in GitHub Desktop.
Save chowells79/1f2b74cd1b9a655d9f97db4d78fa1762 to your computer and use it in GitHub Desktop.
{-# Language PolyKinds, GADTs, ScopedTypeVariables, TypeApplications #-}
module HasochistIntersperse (intersperse) where
import Data.Singletons
data Between a v = Empty | Has v deriving (Eq, Ord, Show)
instance forall k (a :: k) v.
(SingI a, SingKind k, v ~ Demote k, Semigroup v) =>
Semigroup (Between a v) where
Empty <> x = x
x <> Empty = x
(Has x) <> (Has y) = Has (x <> demote @a <> y)
instance forall k (a :: k) v.
(SingI a, SingKind k, v ~ Demote k, Semigroup v) =>
Monoid (Between a v) where
mempty = Empty
unBetween :: Monoid v => Between a v -> v
unBetween Empty = mempty
unBetween (Has x) = x
intersperse :: forall f v k.
(SingKind k, v ~ Demote k, Foldable f, Monoid v) =>
v -> f v -> v
intersperse x xs = withSomeSing x $
\(sx :: Sing a) -> withSingI sx (unBetween $ foldMap (Has @a) xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment