1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2025-01-06 03:23:19 +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:
```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 n = 2*n

View File

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

View File

@ -16,7 +16,7 @@ applyInContext :: Num a => (a -> b) -> Context a -> Context b
applyInContext f (Context a) = Context (f a)
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 :: Num a => (a -> b) -> [a] -> [b]
@ -31,11 +31,11 @@ applyInListContext = map
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 $ (strategyToString . strategySquare . strategyDouble) 4
print $ applyInContext (strategySquare . strategyDouble) (Context 7)
print $ applyInListContext strategyDouble [1..10]