Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance Category (->) where id = GHC.Base.id (.) = (GHC.Base..) (<<<) :: Category cat => cat b c -> cat a b -> cat a c (<<<) = (.) (&…
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Free f a)) instance Functor f => Functor (Free f) where fmap f = go where go (Pure a) = Pure (f a) go (Free fa) = Free (go <$> fa) instance Functor f…
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHaskell is needed for makeLenses; RankNTypes is needed for -- a few type signatures later on. {-# LANGUAGE TemplateHaskell, RankNTypes #-} import Control…
Comprehension Extensions 关于解析式的相关语言扩展. List and Comprehension Extensions 24 Days of GHC Extensions: List Comprehensions ParallelListComp Prelude> [(w,x,y,z) | ((w,x),(y,z)) <- zip [(w,x) | w <- [1..3], x <- [2..4]] [(y,z) | y <- [3..5], z &…
fix 函数 fix 是一个在 Data.Function 模块中定义的函数,它是对于递归的封装,可以用于定义不动点函数. fix :: (a -> a) -> a fix f = let x = f x in x fix 函数的定义使用了递归绑定,比较难以理解: fix f = let x = f x in x = let x = f x in f x = let x = f x in f (f x) = let x = f x in f (f (f x)) = let x = f x in…
Existentials(存在类型) Existentially quantified types(Existentially types,Existentials)是一种将一组类型归为一个类型的方式. 通常在使用 type, newtype, data 定义新类型的时候,出现在右边的类型参数必须出现在左边. 存在类型可以突破此限制. 实例 {-# LANGUAGE ExistentialQuantification #-} data ShowBox = forall s. Show s =>…
Comonad class Functor w => Comonad w where extract :: w a -> a duplicate :: w a -> w (w a) duplicate = extend id extend :: (w a -> b) -> w a -> w b extend f = fmap f . duplicate Comonad 是个类型类. 比较 Monad 和 Comonad class Functor m => Mon…
Arrow class Category a => Arrow a where arr :: (b -> c) -> a b c first :: a b c -> a (b,d) (c,d) first = (*** id) second :: a b c -> a (d,b) (d,c) second = (id ***) (***) :: a b c -> a b' c' -> a (b,b') (c,c') f *** g = first f >&g…
MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a instance MonadCont (ContT r m) where callCC = ContT.callCC class Monad m => MonadCont m where MonadState 是个类型类,它为 ContT 等封装了CPS函数的 Monad 定义了通用接口. MonadSta…
Endo Monoid newtype Endo a = Endo { appEndo :: a -> a } instance Monoid (Endo a) where mempty = Endo id Endo f `mappend` Endo g = Endo (f . g) Endo 是个 newtype,也就是对现有类型的封装. Endo a 封装的是一个自反射的函数,即 a->a.通过 appEndo 字段可以取出这个函数. Endo a 在结合时结合两个函数,因此它本质上是对函…
MonadState 类型类 class Monad m => MonadState s m | m -> s where get :: m s get = state (\s -> (s, s)) put :: s -> m () put s = state (\_ -> ((), s)) state :: (s -> (a, s)) -> m a state f = do s <- get let ~(a, s') = f s put s' return…
MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -> m a writer ~(a, w) = do tell w return a tell :: w -> m () tell w = writer ((),w) listen :: m a -> m (a, w) pass :: m (a, w -> w) -> m a lis…
MonadReader 类型类 class Monad m => MonadReader r m | m -> r where ask :: m r ask = reader id local :: (r -> r) -> m a -> m a reader :: (r -> a) -> m a reader f = do r <- ask return (f r) instance Monad m => MonadReader r (ReaderT…
Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text.Read> decimal "123" Right (123,"") Prelude Data.Text.Read> decimal "abc" Left "input does not start with a digit&…