8

在阅读此博客上对 Functor 的描述时:

https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/

Functor 有一个通用定义和一个更具体的定义:

trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] {
  def fmap[A, B](f: A ->> B): F[A] ->>> F[B]
}
trait Functor[F[_]] extends GenericFunctor[Function, Function, F] {
  final def fmap[A, B](as: F[A])(f: A => B): F[B] =
    fmap(f)(as)
}

显然,这意味着 Functor 可以与 Function 对象之外的其他更高种类的类型一起使用。有人可以举个例子或解释如何或为什么或在什么情况下会做?也就是说,在 Scala 中 GenericFunctor 的另一个实现会是什么——它使用与 Function 不同的类型构造函数?谢谢!

编辑:

只是为了澄清:

object Functor {

  def fmap[A, B, F[_]](as: F[A])(f: A => B)(implicit functor: Functor[F]): F[B] =
    functor.fmap(as)(f)

  implicit object ListFunctor extends Functor[List] {
    def fmap[A, B](f: A => B): List[A] => List[B] =
      as => as map f
  }
}
scala> fmap(List(1, 2, 3))(x => x + 1)
res0: List[Int] = List(2, 3, 4)

澄清一下,根据我的理解,ListFunctor 在 GenericFunctor 中实现 1-arg fmap,而 repl 脚本中的代码调用 Trait Functor 中的 fmap,而 Trait Functor 又调用 fmap 实现(例如在 ListFunctor 中)。

这不会改变整体问题,只是认为它会帮助人们试图提供答案。提供的任何见解将不胜感激。

4

3 回答 3

7

在您的示例Functor中,是带有Function1箭头的 Scala 类型类别中的 endofunctor。

还有其他类别。例如,想象一个类别,其中对象是 Scala 类型,A >~> B如果B是 的子类型,则有一个箭头AScalaz中的这个类别称为Liskov. Liskov从范畴到范畴有一个“健忘”函子Function1

import scalaz._
import Scalaz._
trait Forget[F[-_]] extends GenericFunctor[>~>, Function1, F] {
  def fmap[A, B](f: A >~> B): F[A] => F[B] = fa => f.subst(fa)
}

请注意,您可以通过将一个或多个参数固定为GenericFunctor. 例如...

常量函子将一个类别中的每个对象映射到另一个类别中的单个对象:

type ConstantFunctor[->>[_, _], ->>>[_, _], C] =
  GenericFunctor[->>,->>>,({type F[x] = C})#F]
// def fmap[A, B](f: A ->> B): C ->>> C

endofunctor将类别映射到自身:

type EndoFunctor[->>[_, _], F[_]] = GenericFunctor[->>, ->>, F]
// def fmap[A, B](f: A ->> B): F[A] ->> F[B]

恒等函子将每个对象和箭头映射到自身:

type IdentityFunctor[->>[_, _]] = EndoFunctor[->>, ({type F[x] = x})#F]
// def fmap[A, B](f: A ->> B): A ->> B

当然,你的Functor特质只是类别EndoFunctor中的一个。Function1

type Functor[F[_]] = EndoFunctor[Function1, F]
// def fmap[A, B](f: A => B): F[A] => F[B]
于 2011-09-27T23:56:11.410 回答
6

您可以想象一个函子,它将 的实例提升Either[A,B]到可以是、等的Either[F[A],F[B]]地方。FListOption

编辑实现示例:

trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] {
  def fmap[A, B](f: A ->> B): F[A] ->>> F[B]
}

trait EitherFunctor[F[_]] extends GenericFunctor[Either,Either,F]

object ListFunctor extends EitherFunctor[List] {
  def fmap[A,B]( f: Either[A,B] ): Either[List[A],List[B]] = 
    f match {
      case Left(a) => Left( List(a) )
      case Right(b) => Right( List(b) )
    }
}

EDIT2另一个(可能有用)的例子是一个从PartialFunction(type ->>) 到Function(type ->>>) 的函子:

trait PartialFunctor[F[_]] 
extends GenericFunctor[PartialFunction,Function,F] {
  final def fmap[A, B](as: F[A])(f: PartialFunction[A,B]): F[B] =
    fmap(f)(as)
}

object OptionFunctor extends PartialFunctor[Option] {
  def fmap[A,B]( f: PartialFunction[A,B] ): Option[A] => Option[B] =
    (opt:Option[A]) => opt match {
      case Some(a) => f.lift(a)
      case None => None
    }
}

object ListFunctor extends PartialFunctor[List] {
  private def mapPartial[A,B]( f: PartialFunction[A,B], as: List[A] ): List[B] =
    as match {
      case Nil => Nil
      case h :: t => if( f isDefinedAt h ) f(h) :: mapPartial( f, t )
                     else mapPartial( f, t )
    }

  def fmap[A,B]( f: PartialFunction[A,B] ): List[A] => List[B] =
    (lst:List[A]) => mapPartial(f, lst)

}

第二个示例允许实现collectScala 集合中定义的操作:

def collect[A,B,F[_]]( as: F[A] )
                     ( pf: PartialFunction[A,B] )
                     ( implicit functor: PartialFunctor[F] ) = 
  functor.fmap( as )( pf )
于 2011-09-27T16:45:46.370 回答
4

Data.Category是一个很好的例子(在 Haskell 中)。部分翻译来自http://hackage.haskell.org/packages/archive/data-category/0.4.1/doc/html/src/Data-Category-Functor.html的实例之一:

type OpFun[A, B] = B => A

case class OpFunctor[F[_]](functor: Functor[F[_]]) extends GenericFunctor[OpFun, OpFun, F] {
  def fmap[A, B](f: B => A): F[B] => F[A] = fb => functor.fmap(fb)(f)
}
于 2011-09-27T17:38:44.863 回答