on master help sign in
Add WrappedCovariant.
commit4cc50cd51212bd7e9b923c7d36c8fc61c666b43a authorPaweł Nowak <pawel834@gmail.com> dateSun 30 Nov 2014, 15:10 (12 years ago) parent0e68b49
3 files changed+40−3
Data/SemiIsoFunctor.hs+2−3 Data/SemiIsoFunctor/Wrapped.hs+37 semi-iso.cabal+1
Data/SemiIsoFunctor.hs+2−3
@@ -11,9 +11,8 @@ Stability : experimental
1111
1212Defines a functor from the category of semi-isomoprihsms to Hask.
1313
14−The most interesting property of that class is that it can be
15−instantiated by both covariant (like Parser) and contravariant (like Printer)
16−functors. Therefore it can be used as a common interface to unify
14+It can be instantiated by both covariant (like Parser) and contravariant
15+(like Printer) functors. Therefore it can be used as a common interface to unify
1716parsing and pretty printing.
1817-}
1918module 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
2323 exposed-modules: Control.Lens.SemiIso
2424 Control.Lens.Internal.SemiIso
2525 Data.SemiIsoFunctor
26+ Data.SemiIsoFunctor.Wrapped
2627 Data.Profunctor.Exposed
2728 build-depends: base >= 4 && < 5, profunctors, transformers, lens, tuple-morph
2829 default-language: Haskell2010