Skip to content

Commit

Permalink
Merge pull request #324 from tweag/consumable-dupable-v
Browse files Browse the repository at this point in the history
Add Consumable and Dupable instances to V
  • Loading branch information
Divesh Otwani committed Apr 13, 2021
2 parents ecc4e3c + f2a5c6e commit 28fcd2c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Data/Unrestricted/Internal/Instances.hs
Expand Up @@ -31,8 +31,12 @@ import GHC.Types hiding (Any)
import Data.Monoid.Linear
import Data.List.NonEmpty
import qualified Prelude
import Prelude.Linear.Internal
import qualified Unsafe.Linear as Unsafe
import Data.V.Linear ()
import Data.V.Linear.Internal.V (V(..), theLength)
import qualified Data.Vector as Vector
import GHC.TypeLits

instance Consumable () where
consume () = ()
Expand Down Expand Up @@ -142,6 +146,18 @@ instance (Dupable a, Dupable b, Dupable c) => Dupable (a, b, c) where
instance (Movable a, Movable b, Movable c) => Movable (a, b, c) where
move (a, b, c) = (,,) Data.<$> move a Data.<*> move b Data.<*> move c

-- XXX: The Consumable and Dupable instances for V will be easier to define (in
-- fact direct, we may consider adding a deriving-via combinator) when we have a
-- traversable-by-a-data-applicative class see #190.

instance (KnownNat n, Consumable a) => Consumable (V n a) where
consume (V xs) = consume (Unsafe.toLinear Vector.toList xs)

instance (KnownNat n, Dupable a) => Dupable (V n a) where
dupV (V xs) =
V . Unsafe.toLinear (Vector.fromListN (theLength @n)) Data.<$>
dupV (Unsafe.toLinear Vector.toList xs)

instance Consumable a => Consumable (Prelude.Maybe a) where
consume Prelude.Nothing = ()
consume (Prelude.Just x) = consume x
Expand Down

0 comments on commit 28fcd2c

Please sign in to comment.