Skip to content

Instantly share code, notes, and snippets.

@LSLeary
Created October 18, 2025 01:52
Show Gist options
  • Select an option

  • Save LSLeary/4a3d918ac1409baba8a5999e18efdcfb to your computer and use it in GitHub Desktop.

Select an option

Save LSLeary/4a3d918ac1409baba8a5999e18efdcfb to your computer and use it in GitHub Desktop.
Parallel Monoid/Applicative
{-# LANGUAGE DerivingVia #-}
module Par where
-- GHC/base
import GHC.Conc (par, pseq)
-- base
import Data.Monoid (Ap(..))
newtype Par a = Par{ runPar :: a }
deriving (Show, Read, Functor)
deriving (Semigroup, Monoid)
via Ap Par a
instance Applicative Par where
pure = Par
liftA2 f (Par x) (Par y) = Par (x `par` y `pseq` f x y)
instance Eq a => Eq (Par a) where
p1 == p2 = runPar (liftA2 (==) p1 p2)
instance Ord a => Ord (Par a) where
compare p1 p2 = runPar (liftA2 compare p1 p2)
foldPar :: (Foldable f, Monoid a) => f a -> a
foldPar = runPar . foldMap Par
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment