File: Iso.hs

package info (click to toggle)
darcs 2.12.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,748 kB
  • sloc: haskell: 42,936; sh: 11,086; ansic: 837; perl: 129; makefile: 8
file content (29 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Darcs.UI.Options.Iso where

import Prelude ()
import Darcs.Prelude

-- * Isomorphisms

-- | Lightweight type ismomorphisms (a.k.a. invertible functions). If
-- 
-- > Iso fw bw :: Iso a b
--
-- then @fw@ and @bw@ are supposed to satisfy
--
-- prop> fw . bw = id = bw . fw
data Iso a b = Iso (a -> b) (b -> a)

-- | Lift an isomorphism between @a@ and @b@ to one between @f a@ and @f b@.
-- Like 'Functor', except we can only map invertible functions (i.e. an
-- Isomorphisms).
class IsoFunctor f where
  imap :: Iso a b -> f a -> f b

-- | Apply an iso under a functor.
under :: Functor f => Iso a b -> Iso (f a) (f b)
under (Iso fw bw) = Iso (fmap fw) (fmap bw)

-- | Apply an iso under cps (which is a cofunctor).
cps :: Iso a b -> Iso (a -> c) (b -> c)
cps (Iso fw bw) = Iso (\k -> k . bw) (\k -> k . fw)