1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2025-01-07 11:57:50 +03:00
This commit is contained in:
mahlerth 2018-12-10 13:55:49 +01:00
parent 4899cb6e42
commit d215e2ef52
3 changed files with 41 additions and 35 deletions

View File

@ -66,8 +66,6 @@ For each of the Typeclassopedia type classes (at least up to Traversable) I try
We are starting with a simplified example working on Numbers: We are starting with a simplified example working on Numbers:
```haskell ```haskell
-- first we define two simple strategies that map numbers to numbers:
-- first we define two simple strategies that work on numbers:
strategyDouble :: Num a => a -> a strategyDouble :: Num a => a -> a
strategyDouble n = 2*n strategyDouble n = 2*n

View File

@ -1,42 +1,50 @@
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module Coerce where module Coerce where
import Data.Monoid (Sum (..), getSum)
import Data.Functor.Product -- Product of Functors import Control.Applicative
import Data.Functor.Compose -- Composition of Functors import Control.Monad.State.Lazy
import Data.Functor.Const -- Const Functor import Data.Functor.Compose
import Control.Applicative -- WrappedMonad import Data.Functor.Const
import Control.Monad.State.Lazy -- State Monad import Data.Functor.Product
import Data.Typeable import Data.Monoid (Sum (..), getSum)
import Data.Typeable
-- | This module provides explicit coercion. -- | This module provides explicit coercion.
-- Instead of the "magic" Data.Coerce.coerce you could use wrap and unwrap to explicitly write the coercions. -- Instead of the "magic" Data.Coerce.coerce you could use wrap and unwrap to explicitly write the coercions.
class Coerce a b | a -> b where class Coerce a b | a -> b where
unwrap :: a -> b unwrap :: a -> b
wrap :: b -> a wrap :: b -> a
instance Coerce (Const a b) a where instance Coerce (Const a b) a where
unwrap = getConst unwrap = getConst
wrap = Const wrap = Const
instance Coerce (Sum a) a where instance Coerce (Sum a) a where
unwrap = getSum unwrap = getSum
wrap = Sum 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) =>
unwrap mnx = (unwrap (pfst mnx), unwrap(psnd mnx)) where Coerce ((Product m n) a) (b, c) where
pfst (Pair fst _) = fst unwrap mnx = (unwrap (pfst mnx), unwrap (psnd mnx))
psnd (Pair _ snd) = snd where
wrap (x,y) = Pair (wrap x) (wrap y) pfst (Pair fst _) = fst
psnd (Pair _ snd) = snd
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) =>
unwrap = unwrap . fmap unwrap . getCompose Coerce ((Compose m n) a) c where
wrap = Compose . fmap wrap . wrap unwrap = unwrap . fmap unwrap . getCompose
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
unwrap = unwrap . unwrapMonad unwrap = unwrap . unwrapMonad
wrap = WrapMonad . wrap wrap = WrapMonad . wrap
instance Coerce (State s a) (s -> (a,s)) where instance Coerce (State s a) (s -> (a, s)) where
unwrap = runState unwrap = runState
wrap = state wrap = state

View File

@ -16,7 +16,7 @@ applyInContext :: Num a => (a -> b) -> Context a -> Context b
applyInContext f (Context a) = Context (f a) applyInContext f (Context a) = Context (f a)
instance Functor Context where instance Functor Context where
fmap f (Context a) = Context (f a) fmap f (Context a) = Context (f a)
-- | applyInListContext applies a function of type Num a => a -> a to a list of a's: -- | applyInListContext applies a function of type Num a => a -> a to a list of a's:
applyInListContext :: Num a => (a -> b) -> [a] -> [b] applyInListContext :: Num a => (a -> b) -> [a] -> [b]
@ -31,11 +31,11 @@ applyInListContext = map
strategyDemo = do strategyDemo = do
putStrLn "Strategy Pattern -> Functor (and Higher Order Functions in general)" putStrLn "Strategy Pattern -> Functor (and Higher Order Functions in general)"
print $ strategySquare 16 print $ strategySquare 16
print $ (strategyToString . strategySquare . strategyDouble) 4 print $ (strategyToString . strategySquare . strategyDouble) 4
print $ applyInContext (strategySquare . strategyDouble) (Context 7) print $ applyInContext (strategySquare . strategyDouble) (Context 7)
print $ applyInListContext strategyDouble [1..10] print $ applyInListContext strategyDouble [1..10]