Skip to content

Commit ed386f3

Browse files
committed
WIP ValidationT
1 parent 0da49d3 commit ed386f3

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

selective.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ library
3535
Control.Selective.Multi,
3636
Control.Selective.Rigid.Free,
3737
Control.Selective.Rigid.Freer,
38-
Control.Selective.Trans.Except
38+
Control.Selective.Trans.Except,
39+
Control.Selective.Trans.Validation
3940
build-depends: base >= 4.9 && < 5,
4041
containers >= 0.5.5.1 && < 0.7,
4142
transformers >= 0.4.2.0 && < 0.7

src/Control/Selective.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,16 @@ instance Selective ZipList where select = selectA
430430

431431
-- | Selective instance for the standard applicative functor Validation. This is
432432
-- a good example of a non-trivial selective functor which is not a monad.
433-
data Validation e a = Failure e | Success a deriving (Eq, Functor, Ord, Show)
433+
data Validation e a = Failure e | Success a
434+
deriving (Eq, Functor, Ord, Show, Foldable, Traversable)
435+
436+
-- instance Foldable (Validation e) where
437+
-- foldMap _ (Failure _) = mempty
438+
-- foldMap f (Success a) = f a
439+
440+
-- instance Traversable (Validation e) where
441+
-- traverse _ (Failure e) = pure $ Failure e
442+
-- traverse f (Success a) = Success <$> f a
434443

435444
instance Semigroup e => Applicative (Validation e) where
436445
pure = Success
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveTraversable #-}
3+
{-# LANGUAGE DerivingVia #-}
4+
{-# LANGUAGE StandaloneDeriving #-}
5+
{- |
6+
FIXME some inspiration/guidance needed here
7+
-}
8+
module Control.Selective.Trans.Validation where
9+
10+
import Data.Functor.Compose
11+
#if MIN_VERSION_base(4,12,0)
12+
#endif
13+
14+
import Control.Monad.Trans.Class
15+
16+
import Control.Selective
17+
18+
-- | A newtype around @transformers@' 'Transformers.ValidationT'.
19+
newtype ValidationT e m a = ValidationT
20+
{ runValidationT :: m (Validation e a) }
21+
deriving
22+
( Functor, Foldable, Traversable
23+
)
24+
25+
26+
-- TODO
27+
#if MIN_VERSION_base(4,12,0)
28+
--, Contravariant
29+
#endif
30+
31+
-- Eq1, Ord1, Read1, Show1,
32+
--, Eq, Ord, Read, Show
33+
34+
#if MIN_VERSION_base(4,9,0)
35+
-- , MonadFail
36+
#endif
37+
-- MonadZip, MonadIO, MonadPlus, -- alternative versions?
38+
-- Monad, MonadTrans, MonadFix,
39+
40+
instance MonadTrans (ValidationT e) where
41+
lift = ValidationT . fmap Success
42+
43+
deriving via Compose f (Validation e) instance (Applicative f, Semigroup e) => Applicative (ValidationT e f)
44+
45+
-- TODO want:
46+
-- deriving via ComposeTraversable
47+
-- See https://github.com/snowleopard/selective/pull/52/
48+
instance (Selective f, Semigroup e) => Selective (ValidationT e f) where
49+
select eab fab = ValidationT $ select (sequenceA <$> runValidationT eab) (sequenceA <$> runValidationT fab)
50+
51+
-- TODO reproduce API of Validation

0 commit comments

Comments
 (0)