Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: feuerbach/traverse-with-class
base: 465c051307
...
head fork: feuerbach/traverse-with-class
compare: 73b059b2ab
  • 8 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
23 Data/Generics/Traversable.hs
@@ -38,30 +38,19 @@ module Data.Generics.Traversable
)
where
-import GHC.Exts (Constraint)
import Control.Applicative
import Control.Monad
import Data.Monoid
import Data.Functor.Identity
import Data.Functor.Constant
+import Data.Generics.Traversable.Core
+import Data.Generics.Traversable.Instances ()
+
-- for documentation only
import Data.Foldable
import Data.Traversable
-class GTraversable (c :: * -> Constraint) a where
- -- | Applicative traversal over (a subset of) immediate subterms. This is
- -- a generic version of 'traverse' from "Data.Traversable".
- --
- -- The supplied function is applied only to the «interesting» subterms.
- --
- -- Other subterms are lifted using 'pure', and the whole structure is
- -- folded back using '<*>'.
- gtraverse
- :: (Applicative f, ?c :: p c)
- => (forall d . (GTraversable c d, c d, ?c :: p c) => d -> f d)
- -> a -> f a
-
-- | Generic map over the immediate subterms
gmap
:: (GTraversable c a, ?c :: p c)
@@ -102,21 +91,21 @@ gfoldl' f z0 xs = gfoldr f' id xs z0
-- | Apply a transformation everywhere in bottom-up manner
everywhere
:: (GTraversable c a, c a, ?c :: p c)
- => (forall a. (GTraversable c a, c a) => a -> a)
+ => (forall d. (GTraversable c d, c d) => d -> d)
-> a -> a
everywhere f = f . gmap (everywhere f)
-- | Apply a transformation everywhere in top-down manner
everywhere'
:: (GTraversable c a, c a, ?c :: p c)
- => (forall a. (GTraversable c a, c a) => a -> a)
+ => (forall d. (GTraversable c d, c d) => d -> d)
-> a -> a
everywhere' f = gmap (everywhere' f) . f
-- | Monadic variation on everywhere
everywhereM
:: (Monad m, GTraversable c a, c a, ?c :: p c)
- => (forall a. (GTraversable c a, c a) => a -> m a)
+ => (forall d. (GTraversable c d, c d) => d -> m d)
-> a -> m a
everywhereM f = f <=< gmapM (everywhereM f)
View
23 Data/Generics/Traversable/Core.hs
@@ -0,0 +1,23 @@
+{-# LANGUAGE ConstraintKinds, KindSignatures, MultiParamTypeClasses, RankNTypes, UndecidableInstances, ImplicitParams #-}
+module Data.Generics.Traversable.Core where
+
+import GHC.Exts (Constraint)
+import Control.Applicative
+
+class GTraversable (c :: * -> Constraint) a where
+ -- | Applicative traversal over (a subset of) immediate subterms. This is
+ -- a generic version of 'traverse' from "Data.Traversable".
+ --
+ -- The supplied function is applied only to the «interesting» subterms.
+ --
+ -- Other subterms are lifted using 'pure', and the whole structure is
+ -- folded back using '<*>'.
+ --
+ -- 'gtraverse' has a default implementation @const pure@, which works for
+ -- types without interesting subterms (in particular, atomic types).
+ gtraverse
+ :: (Applicative f, ?c :: p c)
+ => (forall d . (GTraversable c d, c d, ?c :: p c) => d -> f d)
+ -> a -> f a
+ gtraverse = const pure
+
View
29 Data/Generics/Traversable/Instances.hs
@@ -0,0 +1,29 @@
+{-# LANGUAGE TemplateHaskell, MultiParamTypeClasses, FlexibleInstances, ConstraintKinds, UndecidableInstances #-}
+-- | This module defines 'GTraversable' instances for standard types
+-- exported by "Prelude"
+module Data.Generics.Traversable.Instances () where
+
+import Data.Generics.Traversable.Core
+import Data.Generics.Traversable.TH
+import Control.Applicative
+
+instance GTraversable c ()
+instance GTraversable c Bool
+instance GTraversable c Int
+instance GTraversable c Integer
+instance GTraversable c Float
+instance GTraversable c Double
+instance GTraversable c Char
+instance GTraversable c Ordering
+
+deriveGTraversable ''Maybe
+deriveGTraversable ''Either
+deriveGTraversable ''(,)
+deriveGTraversable ''(,,)
+
+-- Uniform instance for lists
+instance (GTraversable c a, c a) => GTraversable c [a] where
+ gtraverse f = go where
+ go [] = pure []
+ go (x:xs) = (:) <$> f x <*> go xs
+
View
2  Data/Generics/Traversable/TH.hs
@@ -10,7 +10,7 @@ module Data.Generics.Traversable.TH
import Language.Haskell.TH
import Control.Monad
-import Data.Generics.Traversable
+import Data.Generics.Traversable.Core
import Control.Applicative
err s = error $ "Data.Generics.Traversable.TH: " ++ s
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Roman Cheplyaka
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
4 traverse-with-class.cabal
@@ -17,6 +17,8 @@ cabal-version: >=1.10
library
exposed-modules: Data.Generics.Traversable
Data.Generics.Traversable.TH
- -- other-modules:
+ other-modules:
+ Data.Generics.Traversable.Core
+ Data.Generics.Traversable.Instances
build-depends: base ==4.6.*, transformers ==0.3.*, template-haskell
default-language: Haskell2010

No commit comments for this range

Something went wrong with that request. Please try again.