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 1 commit
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
42 changes: 31 additions & 11 deletions Data/Primitive/Array.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE CPP, MagicHash, UnboxedTuples, DeriveDataTypeable, BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs, ConstraintKinds #-}

-- |
-- Module : Data.Primitive.Array
Expand Down Expand Up @@ -73,6 +74,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 @@ -520,18 +524,34 @@ traverseArray f = \ !ary ->
{-# INLINE [1] traverseArray #-}

{-# RULES
"traverse/ST" forall (f :: a -> ST s b). traverseArray f =
traverseArrayP f
"traverse/IO" forall (f :: a -> IO b). traverseArray f =
traverseArrayP f
"toWonk" [~1] traverseArray
= traverseArrayWonk (WonkP (\(Dict :: Dict (PrimMonad f)) (g :: a -> f b) -> traverseArrayP g))
"wonkIO" forall (w :: WonkP IO f).
traverseArrayWonk w = runWonkP w Dict
"wonkST" forall (w :: WonkP (ST s) f).
traverseArrayWonk w = runWonkP w Dict
"wonkMaybeT" forall (w :: WonkP (MaybeT m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (\(Dict :: Dict (PrimMonad m)) -> runWonkP w Dict))
"wonkStateT" forall (w :: WonkP (StateT s m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (\(Dict :: Dict (PrimMonad m)) -> runWonkP w Dict))
"wonkIdentityT" forall (w :: WonkP (IdentityT m) f).
traverseArrayWonk w = traverseArrayWonk (WonkP (\(Dict :: Dict (PrimMonad m)) -> runWonkP w Dict))
#-}
#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)
#-}
#endif

data Dict c where
Dict :: c => Dict c

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

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