Skip to content

Commit

Permalink
Add IsKeyValues for lists of hetrogeneous key-values
Browse files Browse the repository at this point in the history
  • Loading branch information
benl23x5 committed Mar 20, 2016
1 parent 6800bad commit fefbf89
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions repa-scalar/Data/Repa/Scalar/Product.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ module Data.Repa.Scalar.Product
( -- * Product type
(:*:) (..)
, IsProdList (..)
, IsKeyValues (..)

-- * Selecting
, Select (..)
, Select (..)

-- * Discarding
, Discard (..), Keep(..), Drop(..)
, Discard (..)
, Keep (..)
, Drop (..)

-- * Masking
, Mask (..))
, Mask (..))
where
import Data.Repa.Scalar.Singleton.Nat
import qualified Data.Vector.Unboxed as U
Expand All @@ -28,9 +31,9 @@ data a :*: b
infixr :*:


-- | Sequences of products that form a valid list,
-- using () for the nil value.
class IsProdList p where
-- | Check if a sequence of products forms a valid list,
-- using () for the nil value.
--
-- @
-- isProdList (1 :*: 4 :*: 5) ... no instance
Expand All @@ -44,12 +47,43 @@ instance IsProdList () where
isProdList _ = True
{-# INLINE isProdList #-}


instance IsProdList fs => IsProdList (f :*: fs) where
isProdList (_ :*: xs) = isProdList xs
{-# INLINE isProdList #-}


-- Key-value pairs---------------------------------------------------------------------------------
-- | Sequences of products and tuples that form hetrogeneous key-value pairs.
class IsKeyValues p where
type Keys p
type Values p
keys :: p -> [Keys p]
values :: p -> Values p

instance IsKeyValues (k, v) where
type Keys (k, v) = k
type Values (k, v) = v

keys (k, _) = [k]
{-# INLINE keys #-}

values (_, v) = v
{-# INLINE values #-}


instance (IsKeyValues p, IsKeyValues ps, Keys p ~ Keys ps)
=> IsKeyValues (p :*: ps) where

type Keys (p :*: ps) = Keys p
type Values (p :*: ps) = Values p :*: Values ps

keys (p :*: ps) = keys p ++ keys ps
{-# INLINE keys #-}

values (p :*: ps) = values p :*: values ps
{-# INLINE values #-}


---------------------------------------------------------------------------------------------------
class IsProdList t
=> Select (n :: N) t where
Expand Down

0 comments on commit fefbf89

Please sign in to comment.