Skip to content

Commit

Permalink
avoid kind-projector 'Polymorphic lambda' syntax
Browse files Browse the repository at this point in the history
prepare dotty
  • Loading branch information
xuwei-k committed May 16, 2020
1 parent bf4710d commit b877c1e
Show file tree
Hide file tree
Showing 31 changed files with 166 additions and 82 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/scalaz/Const.scala
Expand Up @@ -125,5 +125,7 @@ object Const extends ConstInstances {

/** A properly universally quantified constant function. */
def const[A](a: A): Function0 ~> λ[α => A] =
λ[Function0 ~> λ[α => A]](_ => a)
new (Function0 ~> λ[α => A]) {
override def apply[B](fa: Function0[B]): A = a
}
}
4 changes: 3 additions & 1 deletion core/src/main/scala/scalaz/Coyoneda.scala
Expand Up @@ -105,7 +105,9 @@ object Coyoneda extends CoyonedaInstances {

/** Turns a natural transformation F ~> G into CF ~> CG */
def liftT[F[_], G[_]](fg: F ~> G): Coyoneda[F, *] ~> Coyoneda[G, *] =
λ[Coyoneda[F, *] ~> Coyoneda[G, *]](_.trans(fg))
new (Coyoneda[F, *] ~> Coyoneda[G, *]) {
def apply[A](c: Coyoneda[F, A]) = c.trans(fg)
}

}

Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala/scalaz/Day.scala
Expand Up @@ -94,7 +94,9 @@ sealed abstract class DayInstances extends DayInstances1 {
G.map(a.gy)(a.xya(F.copoint(a.fx), _))

override def cohoist[M[_], N[_]: Comonad](f: M ~> N) =
Lambda[Day[F, M, *] ~> Day[F, N, *]](_ trans2 f)
new (Day[F, M, *] ~> Day[F, N, *]){
def apply[A](a: Day[F, M, A]) = a trans2 f
}
}

implicit def comonadDay[F[_], G[_]](implicit CF0: Comonad[F], CG0: Comonad[G]): Comonad[Day[F, G, *]] = new DayComonad[F, G] {
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/scalaz/FingerTree.scala
Expand Up @@ -1104,7 +1104,10 @@ sealed abstract class IndSeqInstances {
implicit val indSeqInstance: MonadPlus[IndSeq] with Alt[IndSeq] with Traverse[IndSeq] with IsEmpty[IndSeq] =
new MonadPlus[IndSeq] with Alt[IndSeq] with Traverse[IndSeq] with IsEmpty[IndSeq] with IsomorphismFoldable[IndSeq, FingerTree[Int, *]]{
def G = implicitly
override val naturalTrans = λ[IndSeq ~> FingerTree[Int, *]](_.self)
override val naturalTrans = new (IndSeq ~> FingerTree[Int, *]) {
def apply[A](a: IndSeq[A]) =
a.self
}
def traverseImpl[G[_], A, B](fa: IndSeq[A])(f: A => G[B])(implicit G: Applicative[G]) = {
import std.anyVal._
implicit val r: Reducer[B, Int] = UnitReducer((_: B) => 1)
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/scala/scalaz/FreeAp.scala
Expand Up @@ -39,9 +39,9 @@ sealed abstract class FreeAp[F[_],A] {
* }}}
*/
def analyze[M:Monoid](f: F ~> λ[α => M]): M =
foldMap[Const[M, *]](
λ[F ~> Const[M, *]](x => Const(f(x)))
).getConst
foldMap[Const[M, *]](new (F ~> Const[M, *]) {
def apply[X](x: F[X]): Const[M,X] = Const(f(x))
}).getConst

/**
* The natural transformation from `FreeAp[F,_]` to `FreeAp[G,_]`
Expand All @@ -64,7 +64,9 @@ sealed abstract class FreeAp[F[_],A] {
* Embeds this program in the free monad on `F`.
*/
def monadic: Free[F,A] =
foldMap[Free[F,*]](λ[F ~> Free[F,*]](Free.liftF(_)))
foldMap[Free[F, *]](new (F ~> Free[F, *]) {
def apply[B](fb: F[B]) = Free.liftF(fb)
})

/** Idiomatic function application */
def ap[B](f: FreeAp[F, A => B]): FreeAp[F,B] = f match {
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/scala/scalaz/FreeT.scala
Expand Up @@ -191,7 +191,9 @@ sealed abstract class FreeTInstances5 extends FreeTInstances6 {
override def ask =
FreeT.liftM(M1.ask)
override def local[A](f: E => E)(fa: FreeT[S, M, A]) =
fa.hoist(λ[M ~> M](M1.local(f)(_)))
fa.hoist(new (M ~> M){
def apply[A](a: M[A]) = M1.local(f)(a)
})
}
}

Expand Down Expand Up @@ -226,7 +228,9 @@ sealed abstract class FreeTInstances2 extends FreeTInstances3 {
implicit def freeTHoist[S[_]]: Hoist[FreeT[S, *[_], *]] =
new Hoist[FreeT[S, *[_], *]] {
def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[FreeT[S, M, *] ~> FreeT[S, N, *]](_ hoist f)
new (FreeT[S, M, *] ~> FreeT[S, N, *]) {
def apply[A](fa: FreeT[S, M, A]) = fa.hoist(f)
}
def liftM[G[_]: Monad, A](a: G[A]) =
FreeT.liftM(a)
def apply[G[_]: Monad] =
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/scala/scalaz/IdT.scala
Expand Up @@ -73,7 +73,9 @@ sealed abstract class IdTInstances extends IdTInstances0 {
implicit val idTCohoist: Cohoist[IdT] =
new Cohoist[IdT] {
override def cohoist[M[_], N[_]: Comonad](f: M ~> N) =
Lambda[IdT[M, *] ~> IdT[N, *]](x => IdT(f(x.run)))
new (IdT[M, *] ~> IdT[N, *]){
def apply[A](x: IdT[M, A]) = IdT(f(x.run))
}

override def lower[G[_]: Cobind, A](a: IdT[G, A]) =
a.run
Expand All @@ -93,7 +95,10 @@ private object IdTHoist extends Hoist[IdT] {
new IdT[G, A](a)

def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[IdT[M, *] ~> IdT[N, *]](fa => new IdT(f(fa.run)))
new (IdT[M, *] ~> IdT[N, *]) {
def apply[A](fa: IdT[M, A]): IdT[N, A] =
new IdT[N, A](f(fa.run))
}

implicit def apply[G[_] : Monad]: Monad[IdT[G, *]] =
IdT.idTMonad[G]
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/scala/scalaz/IndexedContsT.scala
Expand Up @@ -70,14 +70,14 @@ trait IndexedContsTFunctions {
ContsT { k => M.bind(a)(W.copoint(k)) }

def xhoist[W[_], R, O, M[_], N[_]](f: M ~> N, g: N ~> M)(implicit W: Functor[W]): IndexedContsT[W, R, O, M, *] ~> IndexedContsT[W, R, O, N, *] =
λ[IndexedContsT[W, R, O, M, *] ~> IndexedContsT[W, R, O, N, *]](
fa => IndexedContsT { wk => f(fa.run(W.map(wk) { k => { x => g(k(x)) } })) }
)
new (IndexedContsT[W, R, O, M, *] ~> IndexedContsT[W, R, O, N, *]) {
def apply[A](fa: IndexedContsT[W, R, O, M, A]): IndexedContsT[W, R, O, N, A] = IndexedContsT { wk => f(fa.run(W.map(wk) { k => { x => g(k(x)) } })) }
}

def contracohoist[W[_], V[_], R, O, M[_]](f: V ~> W): (IndexedContsT[W, R, O, M, *] ~> IndexedContsT[V, R, O, M, *]) =
λ[IndexedContsT[W, R, O, M, *] ~> IndexedContsT[V, R, O, M, *]](
fa => IndexedContsT { k => fa.run(f(k)) }
)
new (IndexedContsT[W, R, O, M, *] ~> IndexedContsT[V, R, O, M, *]) {
def apply[A](fa: IndexedContsT[W, R, O, M, A]): IndexedContsT[V, R, O, M, A] = IndexedContsT { k => fa.run(f(k)) }
}

def shift[W[_], I, R, J, O, M[_], A](f: (A => IndexedContsT[W, I, I, M, O]) => IndexedContsT[W, R, J, M, J])(implicit W: Comonad[W], WA: Applicative[W], M: Monad[M]): IndexedContsT[W, R, O, M, A] =
IndexedContsT { k0 =>
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/scalaz/Isomorphism.scala
Expand Up @@ -37,9 +37,9 @@ sealed abstract class Isomorphisms {
}

def %~(f: G ~> G)(implicit FG: Arr[F, G] <~< (F ~> G), GF: Arr[G, F] <~< (G ~> F)): F ~> F =
λ[F ~> F](
a => GF(self.from)(f(FG(self.to)(a)))
)
new (F ~> F) {
def apply[A](a: F[A]): F[A] = GF(self.from)(f(FG(self.to)(a)))
}
}

/**Isomorphism for arrows of kind (* -> * -> *) -> (* -> * -> *) -> * */
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/scalaz/Kleisli.scala
Expand Up @@ -380,7 +380,10 @@ private trait KleisliMonadReader[F[_], R] extends MonadReader[Kleisli[F, R, *],

private trait KleisliHoist[R] extends Hoist[Kleisli[*[_], R, *]] {
def hoist[M[_]: Monad, N[_]](f: M ~> N): Kleisli[M, R, *] ~> Kleisli[N, R, *] =
λ[Kleisli[M, R, *] ~> Kleisli[N, R, *]](_ mapT f.apply)
new (Kleisli[M, R, *] ~> Kleisli[N, R, *]) {
def apply[A](m: Kleisli[M, R, A]): Kleisli[N, R, A] =
m.mapT(f.apply)
}

def liftM[G[_] : Monad, A](a: G[A]): Kleisli[G, R, A] =
Kleisli(_ => a)
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/scalaz/LazyEitherT.scala
Expand Up @@ -296,9 +296,9 @@ sealed abstract class LazyEitherTInstances extends LazyEitherTInstances0 {
implicit def lazyEitherTHoist[A]: Hoist[λ[(a[_], b) => LazyEitherT[a, A, b]]] =
new Hoist[λ[(a[_], b) => LazyEitherT[a, A, b]]] {
override def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[LazyEitherT[M, A, *] ~> LazyEitherT[N, A, *]](
mb => LazyEitherT(f(mb.run))
)
new (LazyEitherT[M, A, *] ~> LazyEitherT[N, A, *]) {
def apply[B](mb: LazyEitherT[M, A, B]) = LazyEitherT(f(mb.run))
}
override def liftM[M[_], B](mb: M[B])(implicit M: Monad[M]) =
LazyEitherT(M.map(mb)(LazyEither.lazyRight[A].apply(_)))
override def apply[M[_]: Monad] =
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/scalaz/LazyOptionT.scala
Expand Up @@ -165,9 +165,9 @@ private trait LazyOptionTHoist extends Hoist[LazyOptionT] {
LazyOptionT[G, A](G.map[A, LazyOption[A]](a)((a: A) => LazyOption.lazySome(a)))

def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[LazyOptionT[M, *] ~> LazyOptionT[N, *]](
fa => LazyOptionT(f.apply(fa.run))
)
new (LazyOptionT[M, *] ~> LazyOptionT[N, *]) {
def apply[A](fa: LazyOptionT[M, A]): LazyOptionT[N, A] = LazyOptionT(f.apply(fa.run))
}

implicit def apply[G[_] : Monad]: Monad[LazyOptionT[G, *]] =
LazyOptionT.lazyOptionTMonadPlusAlt[G]
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/scala/scalaz/Lens.scala
Expand Up @@ -157,9 +157,10 @@ sealed abstract class LensFamily[A1, A2, B1, B2] {

/** Contravariantly mapping the state of a state monad through a lens is a natural transformation */
def liftsNT: IndexedState[B1, B2, *] ~> IndexedState[A1, A2, *] =
λ[IndexedState[B1, B2, *] ~> IndexedState[A1, A2, *]](
s => IndexedState(a => modp(s(_), a))
)
new (IndexedState[B1, B2, *] ~> IndexedState[A1, A2, *]) {
def apply[C](s : IndexedState[B1, B2, C]): IndexedState[A1, A2, C] =
IndexedState[A1, A2, C](a => modp(s(_), a))
}

/** Lenses can be composed */
def compose[C1, C2](that: LensFamily[C1, C2, A1, A2]): LensFamily[C1, C2, B1, B2] =
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/scala/scalaz/ListT.scala
Expand Up @@ -127,9 +127,9 @@ sealed abstract class ListTInstances extends ListTInstances1 {

object ListT extends ListTInstances {
def listT[M[_]]: (λ[α => M[IList[α]]] ~> ListT[M, *]) =
λ[λ[α => M[IList[α]]] ~> ListT[M, *]](
new ListT(_)
)
new (λ[α => M[IList[α]]] ~> ListT[M, *]) {
def apply[A](a: M[IList[A]]) = new ListT[M, A](a)
}

def empty[M[_], A](implicit M: Applicative[M]): ListT[M, A] =
new ListT[M, A](M.point(INil()))
Expand Down Expand Up @@ -201,5 +201,8 @@ private trait ListTHoist extends Hoist[ListT] {
fromIList(G.map(a)(entry => entry :: INil()))

def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]): ListT[M, *] ~> ListT[N, *] =
λ[ListT[M, *] ~> ListT[N, *]](_ mapT f.apply)
new (ListT[M, *] ~> ListT[N, *]) {
def apply[A](a: ListT[M, A]): ListT[N, A] =
a.mapT(f.apply)
}
}
11 changes: 7 additions & 4 deletions core/src/main/scala/scalaz/MaybeT.scala
Expand Up @@ -150,9 +150,9 @@ sealed abstract class MaybeTInstances extends MaybeTInstances0 {

object MaybeT extends MaybeTInstances {
def maybeT[M[_]]: λ[α => M[Maybe[α]]] ~> MaybeT[M, *] =
λ[λ[α => M[Maybe[α]]] ~> MaybeT[M, *]](
new MaybeT(_)
)
new (λ[α => M[Maybe[α]]] ~> MaybeT[M, *]) {
def apply[A](a: M[Maybe[A]]) = new MaybeT[M, A](a)
}

def just[M[_], A](v: => A)(implicit M: Applicative[M]): MaybeT[M, A] =
MaybeT.maybeT[M].apply[A](M.point(Maybe.just(v)))
Expand Down Expand Up @@ -234,7 +234,10 @@ private trait MaybeTHoist extends Hoist[MaybeT] {
MaybeT[G, A](G.map[A, Maybe[A]](a)((a: A) => Maybe.just(a)))

def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[MaybeT[M, *] ~> MaybeT[N, *]](_ mapT f.apply)
new (MaybeT[M, *] ~> MaybeT[N, *]) {
def apply[A](fa: MaybeT[M, A]): MaybeT[N, A] =
fa.mapT(f.apply)
}

implicit def apply[G[_] : Monad]: Monad[MaybeT[G, *]] =
MaybeT.maybeTMonadPlus[G]
Expand Down
27 changes: 19 additions & 8 deletions core/src/main/scala/scalaz/NaturalTransformation.scala
Expand Up @@ -15,9 +15,9 @@ trait NaturalTransformation[F[_], G[_]] {
self =>
def apply[A](fa: F[A]): G[A]

def compose[E[_]](f: E ~> F): E ~> G = λ[E ~> G](
ea => self(f(ea))
)
def compose[E[_]](f: E ~> F): E ~> G = new (E ~> G) {
def apply[A](ea: E[A]) = self(f(ea))
}

def andThen[H[_]](f: G ~> H): F ~> H =
f compose self
Expand All @@ -30,7 +30,9 @@ trait NaturalTransformation[F[_], G[_]] {
* the [[scalaz.Coproduct]] while the other one will be used to transform the Right (`H`) value.
*/
def or[H[_]](hg: H ~> G): Coproduct[F, H, *] ~> G =
λ[Coproduct[F, H, *] ~> G](_.fold(self, hg))
new (Coproduct[F, H, *] ~> G) {
def apply[A](a: Coproduct[F, H, A]) = a.fold(self, hg)
}

import LiskovF._

Expand All @@ -48,11 +50,15 @@ trait NaturalTransformations {

/** `refl` specialized to [[scalaz.Id.Id]]. */
def id: Id ~> Id =
λ[Id ~> Id](a => a)
new (Id ~> Id) {
def apply[A](a: A) = a
}

/** A universally quantified identity function */
def refl[F[_]]: F ~> F =
λ[F ~> F](fa => fa)
new (F ~> F) {
def apply[A](fa: F[A]) = fa
}
}

object NaturalTransformation extends NaturalTransformations {
Expand All @@ -61,13 +67,18 @@ object NaturalTransformation extends NaturalTransformations {
* Useful for combining Free interpreters.
*/
def or[F[_], G[_], H[_]](fg: F ~> G, hg: H ~> G): Coproduct[F, H, *] ~> G =
λ[Coproduct[F, H, *] ~> G](_.fold(fg, hg))
new (Coproduct[F, H, *] ~> G) {
def apply[A](a: Coproduct[F, H, A]) = a.fold(fg, hg)
}

/**
* Like Hoist, for Functors, when we already know how to transform `F ~> G`.
*/
def liftMap[F[_], G[_], H[_]: Functor](in: F ~> G): λ[α => H[F[α]]] ~> λ[α => H[G[α]]] =
λ[λ[α => H[F[α]]] ~> λ[α => H[G[α]]]](fa => Functor[H].map(fa)(in.apply))
new ~>[λ[α => H[F[α]]], λ[α => H[G[α]]]] {
def apply[A](fa: H[F[A]]) =
Functor[H].map(fa)(in.apply)
}
}

/** A function universally quantified over two parameters. */
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/scala/scalaz/OptionT.scala
Expand Up @@ -176,9 +176,9 @@ sealed abstract class OptionTInstances extends OptionTInstances0 {

object OptionT extends OptionTInstances {
def optionT[M[_]]: λ[α => M[Option[α]]] ~> OptionT[M, *] =
λ[λ[α => M[Option[α]]] ~> OptionT[M, *]](
new OptionT(_)
)
new (λ[α => M[Option[α]]] ~> OptionT[M, *]) {
def apply[A](a: M[Option[A]]) = new OptionT[M, A](a)
}

def some[M[_], A](v: => A)(implicit M: Applicative[M]): OptionT[M, A] =
OptionT.optionT[M].apply[A](M.point(Some(v)))
Expand Down Expand Up @@ -285,7 +285,10 @@ private trait OptionTHoist extends Hoist[OptionT] {
OptionT[G, A](G.map[A, Option[A]](a)((a: A) => some(a)))

def hoist[M[_]: Monad, N[_]](f: M ~> N) =
λ[OptionT[M, *] ~> OptionT[N, *]](_ mapT f.apply)
new (OptionT[M, *] ~> OptionT[N, *]) {
def apply[A](fa: OptionT[M, A]): OptionT[N, A] =
fa.mapT(f.apply)
}

implicit def apply[G[_] : Monad]: Monad[OptionT[G, *]] = OptionT.optionTMonadPlus[G]
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/scalaz/ReaderWriterStateT.scala
Expand Up @@ -247,7 +247,10 @@ private trait ReaderWriterStateTHoist[R, W, S] extends Hoist[λ[(α[_], β) => R
implicit def W: Monoid[W]

def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]) =
λ[ReaderWriterStateT[R, W, S, M, *] ~> ReaderWriterStateT[R, W, S, N, *]](_ mapT f.apply)
new (ReaderWriterStateT[R, W, S, M, *] ~> ReaderWriterStateT[R, W, S, N, *]) {
def apply[A](ma: ReaderWriterStateT[R, W, S, M, A]): ReaderWriterStateT[R, W, S, N, A] =
ma mapT f.apply
}

def liftM[M[_], A](ma: M[A])(implicit M: Monad[M]): ReaderWriterStateT[R, W, S, M, A] =
ReaderWriterStateT( (r,s) => M.map(ma)((W.zero, _, s)))
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/scalaz/StateT.scala
Expand Up @@ -391,7 +391,10 @@ private trait StateTHoist[S] extends Hoist[StateT[S, *[_], *]] {
def liftM[G[_], A](ga: G[A])(implicit G: Monad[G]): StateT[S, G, A] =
StateT(s => G.map(ga)(a => (s, a)))

def hoist[M[_]: Monad, N[_]](f: M ~> N) = λ[StateT[S, M, *] ~> StateT[S, N, *]](_ mapT f.apply)
def hoist[M[_]: Monad, N[_]](f: M ~> N) = new (StateT[S, M, *] ~> StateT[S, N, *]) {
def apply[A](action: StateT[S, M, A]) =
action.mapT(f.apply)
}

implicit def apply[G[_] : Monad]: Monad[StateT[S, G, *]] = StateT.stateTMonadState[S, G]
}
Expand Down
8 changes: 5 additions & 3 deletions core/src/main/scala/scalaz/StoreT.scala
Expand Up @@ -204,8 +204,10 @@ private trait StoreTCohoist[S] extends Cohoist[λ[(ƒ[_], α) => StoreT[ƒ, S,
Cobind[G].map(a.run._1)((z: S => A) => z(a.run._2))

def cohoist[M[_], N[_]: Comonad](f: M ~> N) =
λ[StoreT[M, S, *] ~> StoreT[N, S, *]]{ c =>
val (m, a) = c.run
StoreT((f(m), a))
new (StoreT[M, S, *] ~> StoreT[N, S, *]) {
def apply[A](c: StoreT[M, S, A]) = {
val (m, a) = c.run
StoreT((f(m), a))
}
}
}
16 changes: 9 additions & 7 deletions core/src/main/scala/scalaz/StreamT.scala
Expand Up @@ -361,11 +361,13 @@ private trait StreamTHoist extends Hoist[StreamT] {
override def wrapEffect[G[_]: Monad, A](a: G[StreamT[G, A]]): StreamT[G, A] = StreamT.wrapEffect(a)

def hoist[M[_], N[_]](f: M ~> N)(implicit M: Monad[M]): StreamT[M, *] ~> StreamT[N, *] =
λ[StreamT[M, *] ~> StreamT[N, *]](a =>
StreamT(f(M.map(a.step) {
case Yield(a, s) => Yield(a, hoist(f).apply(s()))
case Skip(s) => Skip(hoist(f).apply(s()))
case Done() => Done()
}
)))
new (StreamT[M, *] ~> StreamT[N, *]) {
def apply[A](a: StreamT[M, A]) = StreamT[N, A](
f(M.map(a.step) {
case Yield(a, s) => Yield(a, hoist(f).apply(s()))
case Skip(s) => Skip(hoist(f).apply(s()))
case Done() => Done()
}
))
}
}

0 comments on commit b877c1e

Please sign in to comment.