mirror of
https://github.com/thma/LtuPatternFactory.git
synced 2024-12-03 03:55:08 +03:00
on the way to a poor man's coerce...
This commit is contained in:
parent
2badd11048
commit
1f33d594f9
@ -1,37 +1,43 @@
|
|||||||
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
|
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
|
||||||
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
|
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
|
||||||
module Coerce where
|
module Coerce where
|
||||||
|
import Data.Monoid (Sum (..), getSum)
|
||||||
import Data.Functor.Product -- Product of Functors
|
import Data.Functor.Product -- Product of Functors
|
||||||
import Data.Functor.Compose -- Composition of Functors
|
import Data.Functor.Compose -- Composition of Functors
|
||||||
import Data.Functor.Const -- Const Functor
|
import Data.Functor.Const -- Const Functor
|
||||||
import Control.Applicative -- WrappedMonad
|
import Control.Applicative -- WrappedMonad
|
||||||
import Control.Monad.State.Lazy -- State Monad
|
import Control.Monad.State.Lazy -- State Monad
|
||||||
|
import Data.Typeable
|
||||||
|
|
||||||
-- | This module provides explicit coercion.
|
-- | This module provides explicit coercion.
|
||||||
-- Just in case you want to know what's behind the "magic" Data.Coerce.coerce
|
-- Just in case you want to know what's behind the "magic" Data.Coerce.coerce
|
||||||
class Coerce a b | a -> b where
|
class Coerce a b | a -> b where
|
||||||
down :: a -> b
|
unwrap :: a -> b
|
||||||
up :: b -> a
|
wrap :: b -> a
|
||||||
|
|
||||||
instance Coerce (Const a b) a where
|
instance Coerce (Const a b) a where
|
||||||
down = getConst
|
unwrap = getConst
|
||||||
up = Const
|
wrap = Const
|
||||||
|
|
||||||
|
instance Coerce (Sum a) a where
|
||||||
|
unwrap = getSum
|
||||||
|
wrap = Sum
|
||||||
|
|
||||||
instance (Coerce(m a) b, Coerce(n a) c) => Coerce((Product m n) a) (b,c) where
|
instance (Coerce(m a) b, Coerce(n a) c) => Coerce((Product m n) a) (b,c) where
|
||||||
down mnx = (down (pfst mnx), down(psnd mnx)) where
|
unwrap mnx = (unwrap (pfst mnx), unwrap(psnd mnx)) where
|
||||||
pfst (Pair fst _) = fst
|
pfst (Pair fst _) = fst
|
||||||
psnd (Pair _ snd) = snd
|
psnd (Pair _ snd) = snd
|
||||||
up (x,y) = Pair (up x) (up y)
|
wrap (x,y) = Pair (wrap x) (wrap y)
|
||||||
|
|
||||||
instance (Functor m, Functor n, Coerce(m b)c, Coerce(n a)b) => Coerce((Compose m n) a) c where
|
instance (Functor m, Functor n, Coerce(m b)c, Coerce(n a)b) => Coerce((Compose m n) a) c where
|
||||||
down = down . fmap down . getCompose
|
unwrap = unwrap . fmap unwrap . getCompose
|
||||||
up = Compose . fmap up . up
|
wrap = Compose . fmap wrap . wrap
|
||||||
|
|
||||||
instance Coerce (m a) c => Coerce (WrappedMonad m a) c where
|
instance Coerce (m a) c => Coerce (WrappedMonad m a) c where
|
||||||
down = down . unwrapMonad
|
unwrap = unwrap . unwrapMonad
|
||||||
up = WrapMonad . up
|
wrap = WrapMonad . wrap
|
||||||
|
|
||||||
instance Coerce (State s a) (s -> (a,s)) where
|
instance Coerce (State s a) (s -> (a,s)) where
|
||||||
down = runState
|
unwrap = runState
|
||||||
up = state
|
wrap = state
|
||||||
|
|
Loading…
Reference in New Issue
Block a user