6d76eaf2bb74ee0107948d053b8f48da5232518b
[packages/containers.git] / Utils / Containers / Internal / Coercions.hs
1 {-# LANGUAGE CPP #-}
2 {-# OPTIONS_HADDOCK hide #-}
3
4 #include "containers.h"
5
6 module Utils.Containers.Internal.Coercions where
7
8 #if __GLASGOW_HASKELL__ >= 708
9 import Data.Coerce
10 #endif
11
12 infixl 8 .#
13 #if __GLASGOW_HASKELL__ >= 708
14 (.#) :: Coercible b a => (b -> c) -> (a -> b) -> a -> c
15 (.#) f _ = coerce f
16 #else
17 (.#) :: (b -> c) -> (a -> b) -> a -> c
18 (.#) = (.)
19 #endif
20 {-# INLINE (.#) #-}
21
22 infix 9 .^#
23
24 -- | Coerce the second argument of a function. Conceptually,
25 -- can be thought of as:
26 --
27 -- @
28 -- (f .^# g) x y = f x (g y)
29 -- @
30 --
31 -- However it is most useful when coercing the arguments to
32 -- 'foldl':
33 --
34 -- @
35 -- foldl f b . fmap g = foldl (f .^# g) b
36 -- @
37 #if __GLASGOW_HASKELL__ >= 708
38 (.^#) :: Coercible c b => (a -> c -> d) -> (b -> c) -> (a -> b -> d)
39 (.^#) f _ = coerce f
40 #else
41 (.^#) :: (a -> c -> d) -> (b -> c) -> (a -> b -> d)
42 (f .^# g) x y = f x (g y)
43 #endif
44 {-# INLINE (.^#) #-}