module Darcs.Patch.Commute
( Commute(..)
, commuteFL
, commuteRL
, commuteRLFL
, selfCommuter
) where
import Darcs.Prelude
import Darcs.Patch.CommuteFn
( CommuteFn
, commuterIdFL
, commuterRLId
, commuterRLFL
)
import Darcs.Patch.Witnesses.Ordered
( FL(..), RL(..), reverseFL, reverseRL,
(:>)(..) )
class Commute p where
commute :: (p :> p) wX wY -> Maybe ((p :> p) wX wY)
instance Commute p => Commute (FL p) where
{-# INLINE commute #-}
commute (NilFL :> x) = Just (x :> NilFL)
commute (x :> NilFL) = Just (NilFL :> x)
commute (xs :> ys) = do
ys' :> rxs' <- commuteRLFL (reverseFL xs :> ys)
return $ ys' :> reverseRL rxs'
{-# INLINE commuteRLFL #-}
commuteRLFL :: Commute p => (RL p :> FL p) wX wY
-> Maybe ((FL p :> RL p) wX wY)
commuteRLFL = commuterRLFL commute
instance Commute p => Commute (RL p) where
{-# INLINE commute #-}
commute (xs :> ys) = do
fys' :> xs' <- commuteRLFL (xs :> reverseRL ys)
return (reverseFL fys' :> xs')
{-# INLINE commuteRL #-}
commuteRL :: Commute p => (RL p :> p) wX wY -> Maybe ((p :> RL p) wX wY)
commuteRL = commuterRLId commute
{-# INLINE commuteFL #-}
commuteFL :: Commute p => (p :> FL p) wX wY -> Maybe ((FL p :> p) wX wY)
commuteFL = commuterIdFL commute
selfCommuter :: Commute p => CommuteFn p p
selfCommuter = commute