Maths - Functor - Code

Implementing functors in computer code.

Haskell Code

Code from here.

The 'Functor' class is used for types that can be mapped over. Instances of 'Functor' should satisfy the following laws:

class  Functor f  where
    fmap        :: (a -> b) -> f a -> f b

    -- Replace all locations in the input with the same value.
    -- The default definition is @'fmap' . 'const'@, but this may be
    -- overridden with a more efficient version.
    (<$)        :: a -> f b -> f a
    (<$)        =  fmap . const

Some Instances

The instances of 'Functor' satisfy these laws.

List functor

instance Functor [] where
    fmap = map

IO functor

instance Functor ((->) r) where
    fmap = (.)

Maybe functor

instance Functor ((,) a) where
    fmap f (x,y) = (x, f y)


Scala Code

Code from here. package scalaz

Covariant function application in an environment. i.e. a covariant Functor.

All functor instances must satisfy 2 laws:

trait Functor[F[_]] extends InvariantFunctor[F] {
  def fmap[A, B](r: F[A], f: A => B): F[B]

  final def xmap[A, B](ma: F[A], f: A => B, g: B => A) = fmap(ma, f)
}

object Functor {
  import Scalaz._

  implicit def IdentityFunctor: Functor[Identity] = new Functor[Identity] {
    def fmap[A, B](r: Identity[A], f: A => B) = f(r.value)
  }

  implicit def TraversableFunctor[CC[X] <: collection.TraversableLike[X, CC[X]] :
                                      CanBuildAnySelf]: Functor[CC] = new Functor[CC] {
    def fmap[A, B](r: CC[A], f: A => B) = {
      implicit val cbf = implicitly[CanBuildAnySelf[CC]].builder[A, B]
      r map f
    }
  }

  implicit def NonEmptyListFunctor = new Functor[NonEmptyList] {
    def fmap[A, B](r: NonEmptyList[A], f: A => B) = r map f
  }

  implicit def ConstFunctor[BB: Monoid] = new Functor[({type λ[α]=Const[BB, α]})#λ] {
    def fmap[A, B](r: Const[BB, A], f: (A) => B) = Const(r.value)
  }

  implicit def StateFunctor[S] = new Functor[({type λ[α]=State[S, α]})#λ] {
    def fmap[A, B](r: State[S, A], f: A => B) = r map f
  }

Extending these examples to Natural Transformations

We can extend these examples to represent natural transformations as shown on page here.


metadata block
see also:

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

flag flag flag flag flag flag The Princeton Companion to Mathematics - This is a big book that attempts to give a wide overview of the whole of mathematics, inevitably there are many things missing, but it gives a good insight into the history, concepts, branches, theorems and wider perspective of mathematics. It is well written and, if you are interested in maths, this is the type of book where you can open a page at random and find something interesting to read. To some extent it can be used as a reference book, although it doesn't have tables of formula for trig functions and so on, but where it is most useful is when you want to read about various topics to find out which topics are interesting and relevant to you.

 

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2024 Martin John Baker - All rights reserved - privacy policy.