git.iko.soy/semi-iso-optics
on master help sign in
Add WrappedCovariant.
4cc50cdPaweł Nowak
Data/SemiIsoFunctor.hs+2−3
@@ -11,9 +11,8 @@ Stability : experimental
11
11
12Defines a functor from the category of semi-isomoprihsms to Hask.
12Defines a functor from the category of semi-isomoprihsms to Hask.
13
13
14−The most interesting property of that class is that it can be
15−instantiated by both covariant (like Parser) and contravariant (like Printer)
14+It can be instantiated by both covariant (like Parser) and contravariant
16−functors. Therefore it can be used as a common interface to unify
15+(like Printer) functors. Therefore it can be used as a common interface to unify
17parsing and pretty printing.
16parsing and pretty printing.
18-}
17-}
19module Data.SemiIsoFunctor where
18module Data.SemiIsoFunctor where
Data/SemiIsoFunctor/Wrapped.hsnew file+37
@@ -0,0 +1,37 @@
1+{-# LANGUAGE FlexibleInstances #-}
2+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3+{- |
4+Module : Data.SemiIsoFunctor.Wrapped
5+Description : SemiIso instances for wrapped monads.
6+Copyright : (c) Paweł Nowak
7+License : MIT
8+
9+Maintainer : Paweł Nowak <pawel834@gmail.com>
10+Stability : experimental
11+
12+Every monad (with fail) is a SemiIsoMonad.
13+-}
14+module Data.SemiIsoFunctor.Wrapped where
15+
16+import Control.Applicative
17+import Control.Lens.SemiIso
18+import Control.Monad
19+import Data.SemiIsoFunctor
20+
21+-- | A wrapped covariant functor.
22+newtype WrappedCovariant m a = WrappedCovariant { runCovariant :: m a }
23+ deriving (Functor, Applicative, Alternative, Monad)
24+
25+instance Monad m => SemiIsoFunctor (WrappedCovariant m) where
26+ simapCo ai m = m >>= either fail return . apply ai
27+
28+instance Monad m => SemiIsoApply (WrappedCovariant m) where
29+ sipure ai = either fail return (unapply ai ())
30+ f /*/ g = liftM2 (,) f g
31+
32+instance (Monad m, Alternative m) => SemiIsoAlternative (WrappedCovariant m) where
33+ siempty = empty
34+ f /|/ g = f <|> g
35+
36+instance Monad m => SemiIsoMonad (WrappedCovariant m) where
37+ f //= g = f >>= (\x -> g x >>= \y -> return (x, y))
semi-iso.cabal+1
@@ -23,6 +23,7 @@ library
23 exposed-modules: Control.Lens.SemiIso
23 exposed-modules: Control.Lens.SemiIso
24 Control.Lens.Internal.SemiIso
24 Control.Lens.Internal.SemiIso
25 Data.SemiIsoFunctor
25 Data.SemiIsoFunctor
26+ Data.SemiIsoFunctor.Wrapped
26 Data.Profunctor.Exposed
27 Data.Profunctor.Exposed
27 build-depends: base >= 4 && < 5, profunctors, transformers, lens, tuple-morph
28 build-depends: base >= 4 && < 5, profunctors, transformers, lens, tuple-morph
28 default-language: Haskell2010
29 default-language: Haskell2010