From d215e2ef5236dd2f72c5d8b2b731ca31325ade1a Mon Sep 17 00:00:00 2001 From: mahlerth Date: Mon, 10 Dec 2018 13:55:49 +0100 Subject: [PATCH] clean up --- README.md | 2 -- src/Coerce.hs | 66 +++++++++++++++++++++++++++---------------------- src/Strategy.hs | 8 +++--- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 9221e0a..87db622 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Coerce.hs b/src/Coerce.hs index 1b90612..b337531 100644 --- a/src/Coerce.hs +++ b/src/Coerce.hs @@ -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 diff --git a/src/Strategy.hs b/src/Strategy.hs index d91ba25..275ae05 100644 --- a/src/Strategy.hs +++ b/src/Strategy.hs @@ -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]