Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fancy rules for traversing with PrimMonad #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions Data/Primitive/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ import Text.ParserCombinators.ReadP
#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),Read1(..))
#endif
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Identity
import Control.Monad.Trans.State.Strict

-- | Boxed arrays
data Array a = Array
Expand Down Expand Up @@ -519,19 +522,33 @@ traverseArray f = \ !ary ->
else runSTA len <$> go 0
{-# INLINE [1] traverseArray #-}

newtype WonkP m f = WonkP
{ runWonkP :: forall a b. PrimMonad m => (a -> f b) -> Array a -> f (Array b) }

{-# RULES
"traverse/ST" forall (f :: a -> ST s b). traverseArray f =
traverseArrayP f
"traverse/IO" forall (f :: a -> IO b). traverseArray f =
traverseArrayP f
#-}
#if MIN_VERSION_base(4,8,0)
{-# RULES
"traverse/Id" forall (f :: a -> Identity b). traverseArray f =
(coerce :: (Array a -> Array (Identity b))
-> Array a -> Identity (Array b)) (fmap f)
"toWonk" [~1] traverseArray = traverseArrayWonk (WonkP traverseArrayP :: WonkP f f)

"wonkIO" forall (w :: WonkP IO f).
traverseArrayWonk w = runWonkP w
"wonkST" forall (w :: WonkP (ST s) f).
traverseArrayWonk w = runWonkP w

"wonkMaybeT" forall (w :: WonkP (MaybeT m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (runWonkP w) :: WonkP m f)
"wonkStateT" forall (w :: WonkP (StateT s m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (runWonkP w) :: WonkP m f)
"wonkIdentityT" forall (w :: WonkP (IdentityT m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (runWonkP w) :: WonkP m f)
#-}
#endif

traverseArrayWonk
:: Applicative f
=> WonkP m f
-> (a -> f b)
-> Array a
-> f (Array b)
traverseArrayWonk _ f = traverseArray f
{-# INLINE [0] traverseArrayWonk #-}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if it would help, we could add another wonky parameter to this function to handle Maybe, Either, Identity, and the like. I just haven't yet been able to figure out how that part should work at all.


-- | This is the fastest, most straightforward way to traverse
-- an array, but it only works correctly with a sufficiently
Expand Down