packages feed

syb-extras 0.1 → 0.2.0

raw patch · 2 files changed

+110/−9 lines, 2 filesdep +eq

Dependencies added: eq

Files

Data/Data/Extras.hs view
@@ -1,18 +1,32 @@-{-# LANGUAGE Rank2Types #-}+{-# LANGUAGE Rank2Types, GeneralizedNewtypeDeriving, MultiParamTypeClasses, FlexibleInstances, TypeOperators #-} module Data.Data.Extras -  ( Data1(..)+  ( +  -- * Data.Data+  -- ** Kind: *+    module Data.Data+  -- ** Kind: * -> *+  , Data1(..)   , fromConstr1   , fromConstrB1   , fromConstrM1+  -- ** Kind: * -> * -> *+  , Data2(..)   , fromConstr2   , fromConstrB2   , fromConstrM2-  , module Data.Data+  -- * Combinators+  -- * Lifting @k@ in @'gfoldl'{n} k@+  , liftK+  , liftK2+  -- * Lifting @f@ in @'gunfold'{n} f@+  , liftF+  , liftF2   ) where  import Data.Data import Data.Maybe import Control.Monad+import Data.Eq.Type  newtype ID x = ID { unID :: x } newtype CONST c a = CONST { unCONST :: c }@@ -115,6 +129,7 @@   dataTypeOf1 = dataTypeOf   dataCast1_1 f = gcast1 f + class Typeable2 f => Data2 f where   gfoldl2 :: (Data a, Data x) => (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> f a x -> c (f a x)   gfoldl2 _ z = z@@ -196,3 +211,84 @@   gunfold2 = gunfold   dataTypeOf2 = dataTypeOf   dataCast2_2 f = gcast2 f+++-- horrible hackery+newtype WrappedData1 f a = WrapData1 { _unwrapData1 :: f a } deriving (Iso (f a))+class Iso a b where+  iso :: f a -> f b+  osi :: f b -> f a++instance Iso a a where+  iso = id+  osi = id++iso1 :: f a := WrappedData1 f a+iso1 = Refl iso++liftK :: (Data1 d1, Data a) => (forall d b. Data d => c (d -> b) -> d -> c b) -> c (d1 a -> b') -> d1 a -> c b'+liftK k cf d = k (subst (lift2 iso1) cf) (WrapData1 d)++liftF :: (Data1 b1, Data a) => (forall b r. Data b => c (b -> r) -> c r) -> c (b1 a -> r') -> c r'+liftF f cf = f (subst (lift2 iso1) cf)++data1 :: c (f a) -> c (WrappedData1 f a)+data1 = iso ++undata1 :: c (WrappedData1 f a) -> c (f a)+undata1 = osi++instance Typeable1 f => Typeable1 (WrappedData1 f) where+  typeOf1 (WrapData1 a) = typeOf1 a++instance Data1 f => Data1 (WrappedData1 f) where+  gfoldl1 k z (WrapData1 a) = data1 $ gfoldl1 k z a+  gunfold1 k z c = undata1 $ gunfold1 k z c+  toConstr1 (WrapData1 a) = toConstr1 a+  dataTypeOf1 (WrapData1 a) = dataTypeOf1 a+  +-- these lie and let us convert instances+instance (Data1 f, Data a) => Data (WrappedData1 f a) where+  gfoldl k z (WrapData1 a) = data1 $ gfoldl1 k z a+  gunfold k z c = undata1 $ gunfold1 k z c+  toConstr (WrapData1 a) = toConstr1 a+  dataTypeOf (WrapData1 a) = dataTypeOf1 a++newtype WrappedData2 f a b = WrapData2 { _unwrapData2 :: f a b } deriving (Iso (f a b))++iso2 :: f a b := WrappedData2 f a b+iso2 = Refl iso++liftK2 :: (Data2 d2, Data a, Data x) => (forall d b. Data d => c (d -> b) -> d -> c b) -> c (d2 a x -> b') -> d2 a x -> c b'+liftK2 k cf d = k (subst (lift2 iso2) cf) (WrapData2 d) ++liftF2 :: (Data2 b2, Data a, Data x) => (forall b r. Data b => c (b -> r) -> c r) -> c (b2 a x -> r') -> c r'+liftF2 f cf = f (subst (lift2 iso2) cf)++data2 :: c (f a b) -> c (WrappedData2 f a b)+data2 = iso ++undata2 :: c (WrappedData2 f a b) -> c (f a b)+undata2 = osi++instance Typeable2 f => Typeable2 (WrappedData2 f) where+  typeOf2 (WrapData2 a) = typeOf2 a++instance Data2 f => Data2 (WrappedData2 f) where+  gfoldl2 k z (WrapData2 a) = data2 $ gfoldl2 k z a+  gunfold2 k z c = undata2 $ gunfold1 k z c+  toConstr2 (WrapData2 a) = toConstr2 a+  dataTypeOf2 (WrapData2 a) = dataTypeOf2 a++instance (Data2 f, Data a) => Data1 (WrappedData2 f a) where+  gfoldl1 k z (WrapData2 a) = data2 $ gfoldl2 k z a+  gunfold1 k z c = undata2 $ gunfold2 k z c+  toConstr1 (WrapData2 a) = toConstr2 a+  dataTypeOf1 (WrapData2 a) = dataTypeOf2 a+  +-- these lie and let us convert instances+instance (Data2 f, Data a, Data b) => Data (WrappedData2 f a b) where+  gfoldl k z (WrapData2 a) = data2 $ gfoldl2 k z a+  gunfold k z c = undata2 $ gunfold2 k z c+  toConstr (WrapData2 a) = toConstr2 a+  dataTypeOf (WrapData2 a) = dataTypeOf2 a
syb-extras.cabal view
@@ -1,22 +1,27 @@ name:          syb-extras category:      Polymorphism, Combinators, Generic-version:       0.1+version:       0.2.0 license:       BSD3-cabal-version: >= 1.2+cabal-version: >= 1.6 license-file:  LICENSE author:        Edward A. Kmett maintainer:    Edward A. Kmett <ekmett@gmail.com> stability:     provisional-homepage:      http://comonad.com/reader/+homepage:      http://github.com/ekmett/syb-extras/ copyright:     Copyright (C) 2011 Edward A. Kmett-synopsis:      higher order versions of the Scrap Your Boilerplate classes-description:   higher order versions of Scrap Your Boilerplate classes to ease programming with polymorphic recursion and reduce UndecidableInstances+synopsis:      Higher order versions of the Scrap Your Boilerplate classes+description:   Higher order versions of the Scrap Your Boilerplate classes to ease programming with polymorphic recursion and reduce UndecidableInstances build-type:    Simple +source-repository head+  type: git+  location: git://github.com/ekmett/syb-extras.git+ library   build-depends:      base >= 4 && < 4.4,-    prelude-extras >= 0.1 && < 0.3+    prelude-extras >= 0.1 && < 0.3,+    eq >= 0.1.1 && <= 3    extensions: CPP