Tom
Tom

Reputation: 6332

How many categories are involved in my Functor definition

I have a following definition of Functor:

trait Functor[F[_]] { 
    def map[A, B](fa: F[A])(f: A => B): F[B]
}
object ListFunctor extends Functor[List] { //
    def map[A, B](f: A => B)(data: List[A]): List[B] = data map f
}

Assume that A is Int,B is String ,and F is List,then f is a function of type: Int=>String, and we get a function List[Int]=>List[String] with ListFunctor.map(f) _

Since functor is closely related with Category, and I am now trying to understand category, I would ask how many categories are involved in the ListFunctor if A is Int,B is String ,and F is List

I have two possible answers in mind but don't know which one is correct:

1.There are four categories:

All possible values of Int forms an Int category

All possible values of String forms a String category

All possible values of List[Int] forms a List[Int] category

All possible values of List[String] forms a List[String] category
  1. There are two categories

    2.1 Int and String form a category, that is,this category contains all possible values of Int and String

    2.2 List[Int] and List[String] form the other category, that is,this category contains all possible values of List[Int]

I would ask which of the above answers is correct? Thanks!

Upvotes: 1

Views: 98

Answers (2)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

When we talk about categories in relation to a programming language, we define a category "Scala" where objects are types and morphisms are functions. Following that definition, every functor defined in such as category is an endo-functor.

This means that the map function on Functor is a morphism from object A => B, and the functor definition is a functor from the category of Scala onto itself.

Upvotes: 0

Ziyang Liu
Ziyang Liu

Reputation: 810

There are two categories here:

  • The one whose objects are all types in Scala, and whose morphisms are functions A => B for all A and B.
  • The one whose objects are types List[A] for all A, and whose morphisms are functions List[A] => List[B] for all A and B.

A Functor is a mapping between categories, and the ListFunctor is a functor that maps from the first category to the second.

Upvotes: 1

Related Questions