Created
October 18, 2025 01:52
-
-
Save LSLeary/4a3d918ac1409baba8a5999e18efdcfb to your computer and use it in GitHub Desktop.
Parallel Monoid/Applicative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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