Mzk*_*evi 9 haskell scala category-theory
所以我一直试图通过定义一个更高阶的函子来将我的函子的直觉推到极限,即a,将一阶类型作为类型参数的F,以及函数和提升第一阶类型的函数到scala中的这个更高的上下文喜欢
trait Functor1[F[_[_]] {
def hmap[X[_], Y[_]] : (X ~> Y) => F[X] => F[Y]
}
Run Code Online (Sandbox Code Playgroud)
我一直试图定义普通仿函数的一些地图可导函数,例如
trait Functor[F[_]] {
def map[A, B] : (A => B) => F[A] => F[B]
// there's a few more besides this that are map derivable
def distribute[A,B](fab: F[(A, B)]): (F[A], F[B])
}
Run Code Online (Sandbox Code Playgroud)
但我不能写任何类型的支票...我只是在玩,但我想知道是否还有其他人在这条路上走得比我聪明
可以在scala中定义更高阶的仿函数吗?如果不是那么在哈斯克尔?
/** Higher order functor */\ntrait HFunctor[F[_]] {\n def ffmap[G[_]: Functor, A, B](f: A => B): F[G[A]] => F[G[B]]\n def hfmap[G[_], H[_]](t: G ~> H): ({type \xce\xbb[\xce\xb1] = F[G[\xce\xb1]]})#\xce\xbb ~> ({type \xce\xbb[\xce\xb1] = F[H[\xce\xb1]]})#\xce\xbb\n}\n\ntrait Functor[F[_]] { self =>\n\n def fmap[A, B](f: A => B): F[A] => F[B]\n\n // derived\n\n def map[A, B](x: F[A])(f: A => B): F[B] = fmap(f)(x)\n def strengthL[A, B]: A => F[B] => F[(A, B)] = a => f => fmap((x: B) => (a, x))(f)\n def strengthR[A, B]: F[A] => B => F[(A, B)] = f => b => fmap((x: A) => (x, b))(f)\n def compose[G[_]](implicit e: Functor[G]): Functor[({ type \xce\xbb[\xce\xb1] = F[G[\xce\xb1]]})#\xce\xbb] =\n new Functor[({ type \xce\xbb[\xce\xb1] = F[G[\xce\xb1]]})#\xce\xbb] {\n def F = self;\n def G = e\n def fmap[A, B](f: A => B) = F.fmap(G.fmap(f))\n }\n\n}\n\nobject Functor {\n @inline def apply[F[_]: Functor]: Functor[F] = iev\n}\n\ntrait Coyoneda[F[_], A] { co =>\n\n type I\n\n def fi: F[I]\n\n def k: I => A\n\n final def run(implicit F: Functor[F]): F[A] = F.fmap(k)(fi)\n\n final def map[B](f: A => B): Coyoneda.Aux[F, B, I] =\n Coyoneda(fi)(f compose k)\n\n final def trans[G[_]](phi: F ~> G): Coyoneda[G, A] =\n Coyoneda(phi(fi))(k)\n}\n\nobject Coyoneda {\n\n type Aux[F[_], A, B] = Coyoneda[F, A] { type I = B }\n\n def apply[F[_], B, A](x: F[B])(f: B => A): Aux[F, A, B] =\n new Coyoneda[F, A] {\n type I = B\n val fi = x\n val k = f\n }\n\nimplicit def coyonedaFunctor[F[_]]: Functor[({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb] =\n new Functor[({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb] {\n def fmap[A, B](f: A => B): Coyoneda[F, A] => Coyoneda[F, B] =\n x => apply(x.fi)(f compose x.k)\n }\n\nimplicit def coyonedaHFunctor: HFunctor[({ type \xce\xbb[F[_]] = ({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb })#\xce\xbb] =\n new HFunctor[({ type \xce\xbb[F[_]] = ({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb })#\xce\xbb] {\n def ffmap[G[_]: Functor, A, B](f: A => B): Coyoneda[G, A] => Coyoneda[G, B] = _.map(f)\n def hfmap[F[_], G[_]](t: F ~> G): (({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb ~> ({ type \xce\xbb[\xce\xb1] = Coyoneda[G, \xce\xb1] })#\xce\xbb) = \n new (({ type \xce\xbb[\xce\xb1] = Coyoneda[F, \xce\xb1] })#\xce\xbb ~> ({ type \xce\xbb[\xce\xb1] = Coyoneda[G, \xce\xb1] })#\xce\xbb) {\n def apply[A](x: Coyoneda[F, A]) = x.trans(t)\n }\n}\n\ndef liftCoyoneda[F[_], A](fa: F[A]): Coyoneda[F, A] = apply(fa)(x => x)\n\ndef lowerCoyoneda[F[_]: Functor, A](c: Coyoneda[F, A]): F[A] = c.run\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
525 次 |
| 最近记录: |