packages feed

vinyl-utils 0.0.0.0 → 0.1.0.0

raw patch · 6 files changed

+42/−105 lines, 6 filesdep ~vinyl

Dependency ranges changed: vinyl

Files

+ changelog.md view
@@ -0,0 +1,11 @@+0.1.0.0+-------++* Updated for Vinyl 0.5.*+* Removed slicing lens and overwriting (already in Vinyl proper).++0.0.0.0+------++* Initial commit.+
− src/Data/Vinyl/Upcast.hs
@@ -1,71 +0,0 @@-{-# LANGUAGE-      DataKinds-    , MultiParamTypeClasses-    , FlexibleContexts-    , FlexibleInstances-    , PolyKinds-    , ScopedTypeVariables-    , TypeFamilies-    , TypeOperators-    , UndecidableInstances-    #-}--{-# OPTIONS_HADDOCK show-extensions #-}--{-|-Module      : Data.Vinyl.Upcast-Description : Upward cast and slicing.-Copyright   : (c) Marcin Mrotek, 2014-License     : BSD3-Maintainer  : marcin.jan.mrotek@gmail.com--}--module Data.Vinyl.Upcast (-      Overwrite(..)-    , slice-    , (:>)(..)-    , AltRec(..)-) where--import Control.Applicative-import Data.Monoid-import Data.Vinyl-import Data.Vinyl.TyFun---- |Overwrite a wider record with a narrower record.-class Overwrite (xs :: [k]) (ys :: [k]) where-    overwrite :: Rec el f xs -> Rec el f ys -> Rec el f xs--instance Overwrite xs '[] where-    overwrite xs _ = xs--instance (IElem y xs, Overwrite xs ys) => Overwrite xs (y ': ys) where-    overwrite xs (y :& ys) = overwrite (ith (implicitly :: Elem y xs) y xs) ys-        where -            ith :: Elem x rs ->  f (el $ x) -> Rec el f rs -> Rec el f rs-            ith Here      y (_ :& xs) = y :& xs -            ith (There p) y (x :& xs) = x :& ith p y xs--slice :: (Functor f, Overwrite xs ys, xs <: ys) -      => (Rec el g ys -> f (Rec el g ys))-      ->  Rec el g xs -> f (Rec el g xs)--- ^A lens from a record to a portion of it.-slice k x = overwrite x <$> k (cast x)---- |Wrapper for Rec with a different Monoid instance. Instead of lifting mappend, it acts on (f (el $ r)) directly, to support temporarily turning records into monoids by changing functors.-newtype AltRec el f rs = AltRec {getRec :: Rec el f rs}--instance Monoid (AltRec el f '[]) where-    mempty = AltRec RNil-    _ `mappend` _ = AltRec RNil--instance (Monoid (f (el $ r)), Monoid (AltRec el f rs)) => Monoid (AltRec el f (r ': rs)) where-    mempty = AltRec $ mempty :& getRec mempty-    (AltRec (x :& xs)) `mappend` (AltRec (y :& ys)) = AltRec $ (x <> y) :& getRec (AltRec xs `mappend` AltRec ys)---- |Upward record casting.-class (Overwrite ys xs, ys <: xs) => (xs :: [k]) :> (ys :: [k]) where-    upcast :: (Monoid (AltRec el f ys)) => Rec el f xs -> Rec el f ys--instance (Overwrite ys xs, ys <: xs) => xs :> ys where-    upcast xs = overwrite (getRec mempty) xs
src/Data/Vinyl/Utils/Compose.hs view
@@ -17,9 +17,9 @@     rtraverse1 ) where import Control.Applicative-import Data.Functor.Compose+import Data.Vinyl.Functor import Data.Vinyl -rtraverse1 :: Applicative f => Rec el (Compose f g) rs -> f (Rec el g rs)+rtraverse1 :: Applicative f => Rec (Compose f g) rs -> f (Rec g rs) -- ^Traverse a record parametrized with a composition of functors, leaving the inner functor. rtraverse1 = rtraverse getCompose
src/Data/Vinyl/Utils/Const.hs view
@@ -19,24 +19,23 @@       ConstApplicative(..)     , rconst     , constCommute-    , rconstdist+    , rtraverseconst ) where  import Data.Vinyl.Utils.Compose -import Control.Applicative-import Data.Functor.Compose+import Control.Applicative hiding (Const)+import Data.Vinyl.Functor import Data.Vinyl-import Data.Vinyl.TyFun -cfmap :: (a -> b) -> Rec el (Const a) rs -> Rec el (Const b) rs+cfmap :: (a -> b) -> Rec (Const a) rs -> Rec (Const b) rs -- ^Map a function over a constant type record. cfmap _ RNil = RNil cfmap f (Const r :& rs) = Const (f r) :& cfmap f rs  -- |Extension of 'pure' to constant type records. class ConstApplicative (rs :: [k]) where-    cpure :: a -> Rec el (Const a) rs+    cpure :: a -> Rec (Const a) rs   instance ConstApplicative '[] where     cpure = const RNil@@ -44,12 +43,12 @@ instance ConstApplicative rs => ConstApplicative (r ': rs) where     cpure x = Const x :& cpure x -capp  :: (Rec el (Const (a -> b)) rs) -> Rec el (Const a) rs -> Rec el (Const b) rs+capp  :: (Rec (Const (a -> b)) rs) -> Rec (Const a) rs -> Rec (Const b) rs -- ^Extension of (<*>) to constant type records. capp RNil RNil = RNil capp (Const f :& fs) (Const x :& xs) = Const (f x) :& capp fs xs -rconst :: (Applicative f, RecApplicative rs) => f a -> f (Rec el (Const a) rs)+rconst :: (Applicative f, RecApplicative rs) => f a -> f (Rec (Const a) rs) rconst = rtraverse1 . rcpure     where rcpure a = rpure (Compose $ Const <$> a) @@ -57,6 +56,6 @@ -- ^Commute a constant functor with another functor. constCommute (Const a) = Compose $ Const <$> a -rconstdist :: Applicative f => Rec el (Const (f a)) rs -> f (Rec el (Const a) rs)+rtraverseconst :: Applicative f => Rec (Const (f a)) rs -> f (Rec (Const a) rs) -- ^Distribute a functor over a constant type record.-rconstdist rec = rtraverse1 $ constCommute <<$>> rec+rtraverseconst rec = rtraverse1 $ constCommute <<$>> rec
src/Data/Vinyl/Utils/Operator.hs view
@@ -27,55 +27,53 @@     , p ) where -import Control.Applicative-import Data.Functor.Compose+import Control.Applicative hiding (Const) import Data.Functor.Contravariant import Data.Monoid import Data.Vinyl-import Data.Vinyl.Constraint-import Data.Vinyl.TyFun+import Data.Vinyl.Functor -operator :: (forall t. f t -> g t -> h t) -> Rec el f rs -> Rec el g rs -> Rec el h rs--- ^Create an operator between records sharing their universes and fields but differing in functors.+operator :: (forall t. f t -> g t -> h t) -> Rec f rs -> Rec g rs -> Rec h rs+-- ^Create an operator between records sharing their fields but differing in functors. operator _ RNil RNil = RNil operator f (a :& as) (b :& bs) = (f a b) :& operator f as bs -(/$/) :: Functor f => Rec el ((->) a) rs -> Rec el (Const (f a)) rs -> Rec el f rs+(/$/) :: Functor f => Rec ((->) a) rs -> Rec (Const (f a)) rs -> Rec f rs -- ^Apply a record of (a -> x) functions to a constant type record to obtain a plain f-record. (/$/) = operator (\f (Const x) -> f <$> x) -(/$$/) :: Rec el (Compose ((->) a) f) rs -> Rec el (Const a) rs -> Rec el f rs--- ^Apply a record of (f a -> f x) functions to a constant type record to obtain a plain f-record.+(/$$/) :: Rec (Compose ((->) a) f) rs -> Rec (Const a) rs -> Rec f rs+-- ^Apply a record of (a -> f x) functions to a constant type record to obtain a plain f-record. (/$$/) = operator (\(Compose f) (Const x) -> f x) -(\$\) :: Functor f => Rec el (Op a) rs -> Rec el f rs -> Rec el (Const (f a)) rs+(\$\) :: Functor f => Rec (Op a) rs -> Rec f rs -> Rec (Const (f a)) rs -- ^Apply a record of (x -> a) functions to a plain f-record to obtain a constant type record. (\$\) = operator (\(Op f) x -> Const $ f <$> x) -(\$$\) :: Rec el (Compose (Op a) f) rs -> Rec el f rs -> Rec el (Const a) rs--- ^Apply a record of (f x -> f a) functions to a plain f-record to obtain a constant type record.+(\$$\) :: Rec (Compose (Op a) f) rs -> Rec f rs -> Rec (Const a) rs+-- ^Apply a record of (f x -> a) functions to a plain f-record to obtain a constant type record. (\$$\) = operator (\(Compose (Op f)) x -> Const $ f x)  predicate (Predicate p) x = Const $ p <$> x -(\&&\) :: forall el f rs. Applicative f => Rec el Predicate rs -> Rec el f rs -> f Bool+(\&&\) :: forall f rs. Applicative f => Rec Predicate rs -> Rec f rs -> f Bool -- ^Apply a predicate record to a plain f-record to obtain a boolean product inside the f functor. (\&&\) p r = go result $ pure True     where-        go :: Rec el (Const (f Bool)) xs -> f Bool -> f Bool+        go :: Rec (Const (f Bool)) xs -> f Bool -> f Bool         go RNil b = b         go (Const a :& as) b = go as $ (&&) <$> a <*> b-        result :: Rec el (Const (f Bool)) rs+        result :: Rec (Const (f Bool)) rs         result = operator predicate p r     -(\||\) :: forall el f rs. Applicative f => Rec el Predicate rs -> Rec el f rs -> f Bool+(\||\) :: forall f rs. Applicative f => Rec Predicate rs -> Rec f rs -> f Bool -- ^Apply a predicate record to a plain f-record to obtain a boolean sum inside the f functor. (\||\) p r = go result $ pure False     where -        go :: Rec el (Const (f Bool)) xs -> f Bool -> f Bool+        go :: Rec (Const (f Bool)) xs -> f Bool -> f Bool         go RNil b = b         go (Const a :& as) b = go as $ (||) <$> a <*> b-        result :: Rec el (Const (f Bool)) rs+        result :: Rec (Const (f Bool)) rs         result = operator predicate p r  p :: (a -> Bool) -> Predicate a
vinyl-utils.cabal view
@@ -2,9 +2,9 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                vinyl-utils-version:             0.0.0.0+version:             0.1.0.0 synopsis:            Utilities for vinyl-description:         Upcast operator, slicing lens, and operations on records parametrized with various kinds of functors.+description:         Operations on records parametrized with various kinds of functors. license:             BSD3 license-file:        LICENSE author:              Marcin Mrotek@@ -15,6 +15,7 @@ -- extra-source-files:   cabal-version:       >=1.10 homepage:            http://hub.darcs.net/mjm/vinyl-utils+extra-source-files:  changelog.md source-repository    head     type:    darcs     location: mjm@hub.darcs.net:mjm/vinyl-utils@@ -23,12 +24,11 @@   exposed-modules:     Data.Vinyl.Utils.Const                       ,Data.Vinyl.Utils.Compose                       ,Data.Vinyl.Utils.Operator-                      ,Data.Vinyl.Upcast   -- other-modules:          -- other-extensions:       build-depends:       base >=4.7 && <4.8                       ,contravariant                       ,transformers-                      ,vinyl+                      ,vinyl >= 0.5   hs-source-dirs:      src   default-language:    Haskell2010