Applicative functors other than monads and ZipList?

前端 未结 7 833
小鲜肉
小鲜肉 2020-12-31 07:36

Two well-known examples of applicatives are monads and ziplists. Are there any other examples?

7条回答
  •  Happy的楠姐
    2020-12-31 08:40

    From Time flies like an applicative functor by Conor McBride:

    Structure cops will note that De is another example of an applicative functor which is not a monad — join would bring things from the far future to the near future, and that had better not be possible. However, where applicative functors in general only pull through traversable functors (containers with finitely many elements), De pulls through all containers. So it’s a bit special. I wonder what it is.

    and

    The De functor represents a fixed delay, rather than an arbitrary one. I’m dividing time into discrete slices. De x is the type of an x due at the next slice. De (De x) is thus the type of an x due in two slices’ time, and you can’t make it turn up any sooner!

    Read the whole post. To answer the immediate question, the author’s conclusion is

    Don’t Look!

    OK, here’s the implementation. It’s a con.

    newtype De x = De x deriving Show -- ssh, don't tell!
    
    instance Functor De where
      fmap f (De x) = De (f x)
    
    instance Applicative De where
      pure = De
      De f <*> De s = De (f s)
    
    fix :: (De x -> x) -> x
    fix f = f (De (fix f))
    

提交回复
热议问题