summaryrefslogtreecommitdiff
path: root/src/Control/Arrow/AppArrow.hs
blob: fe88236fb8d9f08b98d6cb80ac2742f6471ac078 (plain)
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
30
31
32
33
34
35
36
37
38
39
-- | This modules defines the composition of an applicative functor and an
-- arrow, which is always an arrow.

module Control.Arrow.AppArrow
  ( AppArrow(..)
  , appArrow
  ) where

import Control.Category
import Control.Arrow
import Prelude          hiding (id, (.))

newtype AppArrow app arr a b = AppArrow { unAppArrow :: app (arr a b) }

instance (Applicative app, Category cat) => Category (AppArrow app cat) where
  id = appArrow id
  AppArrow a1 . AppArrow a2 = AppArrow $ (.) <$> a1 <*> a2

instance (Applicative app, Arrow arr) => Arrow (AppArrow app arr) where
  arr = appArrow . arr
  first (AppArrow a) = AppArrow $ first <$> a
  second (AppArrow a) = AppArrow $ second <$> a
  AppArrow a1 *** AppArrow a2 = AppArrow $ (***) <$> a1 <*> a2

instance (Applicative app, ArrowChoice arr) => ArrowChoice (AppArrow app arr) where
  left (AppArrow a) = AppArrow $ left <$> a
  right (AppArrow a) = AppArrow $ right <$> a
  AppArrow a1 +++ AppArrow a2 = AppArrow $ (+++) <$> a1 <*> a2
  AppArrow a1 ||| AppArrow a2 = AppArrow $ (|||) <$> a1 <*> a2

instance (Applicative f, Arrow arr) => Functor (AppArrow f arr t) where
  fmap f = (>>> arr f)

instance (Applicative app, Arrow arr) => Applicative (AppArrow app arr t) where
  pure = arr . const
  a <*> b = a &&& b >>> arr (uncurry ($))

appArrow :: (Applicative app) => arr a b -> AppArrow app arr a b
appArrow = AppArrow . pure