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:
```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,12 +1,17 @@
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module Coerce where
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.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
-- | This module provides explicit coercion.
@ -23,13 +28,16 @@ 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
unwrap mnx = (unwrap (pfst mnx), unwrap(psnd mnx)) where
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
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