Skip to content
This repository
Browse code

Kleisli.mapK and remove Alternative(Empty)

  • Loading branch information...
commit 42ee5456a2ed065562ad40a3401698765843670a 1 parent 37d7d9d
Richard Wallace authored February 08, 2012
33  core/src/main/scala/scalaz/Alternative.scala
... ...
@@ -1,33 +0,0 @@
1  
-package scalaz
2  
-
3  
-////
4  
-/**
5  
- *
6  
- */
7  
-////
8  
-trait Alternative[F[_]] extends Applicative[F] { self =>
9  
-  ////
10  
-  def orElse[A](a: F[A], b: => F[A]): F[A]
11  
-  
12  
-  // derived functions
13  
-  def oneOrMore[A](fa: F[A]): F[List[A]] = apF(map(fa)(a => (as: List[A]) => a :: as))(zeroOrMore(fa))
14  
-  
15  
-  def zeroOrMore[A](fa: F[A]): F[List[A]] = orElse(oneOrMore(fa), point(List()))
16  
-
17  
-  trait AlternativeLaw {
18  
-    def associative[A](f1: F[A], f2: F[A], f3: F[A])(implicit FA: Equal[F[A]]): Boolean =
19  
-      FA.equal(orElse(f1, orElse(f2, f3)), orElse(orElse(f1, f2), f3))
20  
-  }
21  
-  def alternativeLaw = new AlternativeLaw {}
22  
-  ////
23  
-  val alternativeSyntax = new scalaz.syntax.AlternativeSyntax[F] {}
24  
-}
25  
-
26  
-object Alternative {
27  
-  @inline def apply[F[_]](implicit F: Alternative[F]): Alternative[F] = F
28  
-
29  
-  ////
30  
-
31  
-  ////
32  
-}
33  
-
33  core/src/main/scala/scalaz/AlternativeEmpty.scala
... ...
@@ -1,33 +0,0 @@
1  
-package scalaz
2  
-
3  
-////
4  
-/**
5  
- *
6  
- */
7  
-////
8  
-trait AlternativeEmpty[F[_]] extends Alternative[F] { self =>
9  
-  ////
10  
-  def empty[A]: F[A]
11  
-  // derived functions
12  
-
13  
-  trait EmptyLaw extends AlternativeLaw {
14  
-    def rightOrElseIdentity[A](f1: F[A])(implicit FA: Equal[F[A]]): Boolean =
15  
-      FA.equal(orElse(f1, empty[A]), f1)
16  
-
17  
-    def leftOrElseIdentity[A](f1: F[A])(implicit FA: Equal[F[A]]): Boolean =
18  
-      FA.equal(orElse(empty[A], f1), f1)
19  
-  }
20  
-
21  
-  def alternativeEmptyLaw = new EmptyLaw {}
22  
-  ////
23  
-  val alternativeEmptySyntax = new scalaz.syntax.AlternativeEmptySyntax[F] {}
24  
-}
25  
-
26  
-object AlternativeEmpty {
27  
-  @inline def apply[F[_]](implicit F: AlternativeEmpty[F]): AlternativeEmpty[F] = F
28  
-
29  
-  ////
30  
-
31  
-  ////
32  
-}
33  
-
30  core/src/main/scala/scalaz/Kleisli.scala
@@ -25,8 +25,11 @@ sealed trait Kleisli[M[_], A, B] { self =>
25 25
 
26 26
   def =<<[AA <: A](a: M[AA])(implicit m: Bind[M]): M[B] = m.bind(a)(run _)
27 27
 
28  
-  def map[C](f: B => C)(implicit m: Functor[M]): Kleisli[M, A, C] =
29  
-    kleisli(a => m.map(run(a))(f))
  28
+  def map[C](f: B => C)(implicit M: Functor[M]): Kleisli[M, A, C] =
  29
+    kleisli(a => M.map(run(a))(f))
  30
+    
  31
+  def mapK[N[_], C](f: M[B] => N[C]): Kleisli[N, A, C] = 
  32
+    kleisli(a => f(run(a)))
30 33
 
31 34
   def flatMapK[C](f: B => M[C])(implicit M: Bind[M]): Kleisli[M, A, C] =
32 35
     kleisli(a => M.bind(run(a))(f))
@@ -69,11 +72,14 @@ trait KleisliInstances2 extends KleisliInstances3 {
69 72
     implicit def F: Applicative[F] = F0
70 73
   }
71 74
   implicit def kleisliIdApplicative[R]: Applicative[({type λ[α] = Kleisli[Id, R, α]})#λ] = kleisliApplicative[Id, R]
  75
+  implicit def kleislPlus[F[_], A](implicit F0: Plus[F]) = new KleisliPlus[F, A] {
  76
+    implicit def F = F0
  77
+  }
72 78
 }
73 79
 
74 80
 trait KleisliInstances1 extends KleisliInstances2 {
75  
-  implicit def kleisliAlternative[F[_], R](implicit F0: Alternative[F]): Alternative[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliAlternative[F, R] {
76  
-    implicit def F: Alternative[F] = F0
  81
+  implicit def kleisliApplicativePlus[F[_], R](implicit F0: ApplicativePlus[F]): ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] = new ApplicativePlus[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] with KleisliPlusEmpty[F, R] {
  82
+    implicit def F: ApplicativePlus[F] = F0
77 83
   }
78 84
   implicit def kleisliArrId[F[_]](implicit F0: Pointed[F]) = new KleisliArrIdArr[F] {
79 85
     implicit def F: Pointed[F] = F0
@@ -81,9 +87,6 @@ trait KleisliInstances1 extends KleisliInstances2 {
81 87
   implicit def kleisliSemigroup[F[_], A, B](implicit FB0: Semigroup[F[B]]) = new KleisliSemigroup[F, A, B] {
82 88
     implicit def FB = FB0
83 89
   }
84  
-  implicit def kleislPlus[F[_], A](implicit F0: Plus[F]) = new KleisliPlus[F, A] {
85  
-    implicit def F = F0
86  
-  }
87 90
 }
88 91
 
89 92
 trait KleisliInstances0 extends KleisliInstances1 {
@@ -109,9 +112,6 @@ trait KleisliInstances extends KleisliInstances0 {
109 112
   implicit def kleisliPlusEmpty[F[_], A](implicit F0: PlusEmpty[F]) = new KleisliPlusEmpty[F, A] {
110 113
     implicit def F = F0
111 114
   }
112  
-  implicit def kleisliAlternativeEmpty[F[_], R](implicit F0: AlternativeEmpty[F]): AlternativeEmpty[({type λ[α] = Kleisli[F, R, α]})#λ] = new KleisliAlternativeEmpty[F, R] {
113  
-    implicit def F: AlternativeEmpty[F] = F0
114  
-  }
115 115
   implicit def kleisliMonadTrans[R]: Hoist[({type λ[α[_], β] = Kleisli[α, R, β]})#λ] = new KleisliHoist[R] {}
116 116
 }
117 117
 
@@ -163,16 +163,6 @@ private[scalaz] trait KleisliApplicative[F[_], R] extends Applicative[({type λ[
163 163
   implicit def F: Applicative[F]
164 164
 }
165 165
 
166  
-private[scalaz] trait KleisliAlternative[F[_], R] extends Alternative[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R]{
167  
-  implicit def F: Alternative[F]
168  
-  def orElse[A](a: Kleisli[F, R, A], b: => Kleisli[F, R, A]): Kleisli[F, R, A] = Kleisli[F, R, A](r => F.orElse(a(r), b apply r))
169  
-}
170  
-
171  
-private[scalaz] trait KleisliAlternativeEmpty[F[_], R] extends AlternativeEmpty[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliAlternative[F, R] {
172  
-  implicit def F: AlternativeEmpty[F]
173  
-  def empty[A]: Kleisli[F, R, A] = Kleisli[F, R, A](r => F.empty)
174  
-}
175  
-
176 166
 private[scalaz] trait KleisliMonad[F[_], R] extends Monad[({type λ[α] = Kleisli[F, R, α]})#λ] with KleisliApplicative[F, R] {
177 167
   implicit def F: Monad[F]
178 168
   def bind[A, B](fa: Kleisli[F, R, A])(f: A => Kleisli[F, R, B]): Kleisli[F, R, B] = fa flatMap f
15  core/src/main/scala/scalaz/OptionT.scala
@@ -84,9 +84,6 @@ trait OptionTInstances1 extends OptionTInstances2 {
84 84
 }
85 85
 
86 86
 trait OptionTInstances0 extends OptionTInstances1 {
87  
-  implicit def optionTAlternativeEmpty[F[_]](implicit F0: Monad[F]): AlternativeEmpty[({type λ[α] = OptionT[F, α]})#λ] = new OptionTAlternativeEmpty[F] {
88  
-    implicit def F: Monad[F] = F0
89  
-  }
90 87
   implicit def optionTFoldable[F[_]](implicit F0: Foldable[F]): Foldable[({type λ[α] = OptionT[F, α]})#λ] = new OptionTFoldable[F] {
91 88
     implicit def F: Foldable[F] = F0
92 89
   }
@@ -154,18 +151,6 @@ private[scalaz] trait OptionTTraverse[F[_]] extends Traverse[({type λ[α] = Opt
154 151
   def traverseImpl[G[_] : Applicative, A, B](fa: OptionT[F, A])(f: (A) => G[B]): G[OptionT[F, B]] = fa traverse f
155 152
 }
156 153
 
157  
-trait OptionTAlternative[F[_]] extends Alternative[({type λ[α] = OptionT[F, α]})#λ] with OptionTApply[F] with OptionTPointed[F] {
158  
-  implicit def F: Monad[F]
159  
-  
160  
-  def orElse[A](a: OptionT[F, A], b: => OptionT[F, A]): OptionT[F, A] = a orElse b
161  
-}
162  
-
163  
-trait OptionTAlternativeEmpty[F[_]] extends AlternativeEmpty[({type λ[α] = OptionT[F, α]})#λ] with OptionTAlternative[F] {
164  
-  implicit def F: Monad[F]
165  
-  
166  
-  def empty[A]: OptionT[F, A] = OptionT(F.point(None))
167  
-}
168  
-
169 154
 private[scalaz] trait OptionTHoist extends Hoist[OptionT] {
170 155
   def liftM[G[_], A](a: G[A])(implicit G: Monad[G]): OptionT[G, A] =
171 156
     OptionT[G, A](G.map[A, Option[A]](a)((a: A) => Some(a)))
4  core/src/main/scala/scalaz/std/List.scala
@@ -4,7 +4,7 @@ package std
4 4
 import annotation.tailrec
5 5
 
6 6
 trait ListInstances {
7  
-  implicit val listInstance = new Traverse[List] with MonadPlus[List] with Each[List] with Index[List] with Length[List] with Alternative[List] {
  7
+  implicit val listInstance = new Traverse[List] with MonadPlus[List] with Each[List] with Index[List] with Length[List] with ApplicativePlus[List] {
8 8
     def each[A](fa: List[A])(f: (A) => Unit) = fa foreach f
9 9
     def index[A](fa: List[A], i: Int) = {
10 10
       var n = 0
@@ -43,8 +43,6 @@ trait ListInstances {
43 43
       }
44 44
     }
45 45
     
46  
-    def orElse[A](a: List[A], b: => List[A]) = a ++ b
47  
-
48 46
     override def traverseS[S,A,B](l: List[A])(f: A => State[S,B]): State[S,List[B]] = {
49 47
       State((s: S) => {
50 48
         val buf = new collection.mutable.ListBuffer[B]
3  core/src/main/scala/scalaz/std/Option.scala
@@ -8,7 +8,7 @@ trait OptionInstances0 {
8 8
 }
9 9
 
10 10
 trait OptionInstances extends OptionInstances0 {
11  
-  implicit val optionInstance = new Traverse[Option] with MonadPlus[Option] with Each[Option] with Index[Option] with Length[Option] with AlternativeEmpty[Option] {
  11
+  implicit val optionInstance = new Traverse[Option] with MonadPlus[Option] with Each[Option] with Index[Option] with Length[Option] with ApplicativePlus[Option] {
12 12
     def point[A](a: => A) = Some(a)
13 13
     def each[A](fa: Option[A])(f: (A) => Unit) = fa foreach f
14 14
     def index[A](fa: Option[A], n: Int) = if (n == 0) fa else None
@@ -30,7 +30,6 @@ trait OptionInstances extends OptionInstances0 {
30 30
       case Some(a) => f(a, z)
31 31
       case None    => z
32 32
     }
33  
-    def orElse[A](a: Option[A], b: => Option[A]) = a orElse b
34 33
   }
35 34
 
36 35
   implicit def optionMonoid[A: Semigroup]: Monoid[Option[A]] = new Monoid[Option[A]] {
33  core/src/main/scala/scalaz/syntax/AlternativeEmptySyntax.scala
... ...
@@ -1,33 +0,0 @@
1  
-package scalaz
2  
-package syntax
3  
-
4  
-/** Wraps a value `self` and provides methods related to `AlternativeEmpty` */
5  
-trait AlternativeEmptyV[F[_],A] extends SyntaxV[F[A]] {
6  
-  implicit def F: AlternativeEmpty[F]
7  
-  ////
8  
-
9  
-  ////
10  
-}
11  
-
12  
-trait ToAlternativeEmptyV0 {
13  
-  implicit def ToAlternativeEmptyVUnapply[FA](v: FA)(implicit F0: Unapply[AlternativeEmpty, FA]) =
14  
-    new AlternativeEmptyV[F0.M,F0.A] { def self = F0(v); implicit def F: AlternativeEmpty[F0.M] = F0.TC }
15  
-
16  
-}
17  
-
18  
-trait ToAlternativeEmptyV extends ToAlternativeEmptyV0 with ToAlternativeV {
19  
-  implicit def ToAlternativeEmptyV[F[_],A](v: F[A])(implicit F0: AlternativeEmpty[F]) =
20  
-    new AlternativeEmptyV[F,A] { def self = v; implicit def F: AlternativeEmpty[F] = F0 }
21  
-
22  
-  ////
23  
-
24  
-  ////
25  
-}
26  
-
27  
-trait AlternativeEmptySyntax[F[_]] extends AlternativeSyntax[F] {
28  
-  implicit def ToAlternativeEmptyV[A](v: F[A])(implicit F0: AlternativeEmpty[F]): AlternativeEmptyV[F, A] = new AlternativeEmptyV[F,A] { def self = v; implicit def F: AlternativeEmpty[F] = F0 }
29  
-
30  
-  ////
31  
-
32  
-  ////
33  
-}
39  core/src/main/scala/scalaz/syntax/AlternativeSyntax.scala
... ...
@@ -1,39 +0,0 @@
1  
-package scalaz
2  
-package syntax
3  
-
4  
-/** Wraps a value `self` and provides methods related to `Alternative` */
5  
-trait AlternativeV[F[_],A] extends SyntaxV[F[A]] {
6  
-  implicit def F: Alternative[F]
7  
-  ////
8  
-  final def orElse(a: => F[A]): F[A] = F.orElse(self, a)
9  
-
10  
-  final def <|>(a: => F[A]): F[A] = F.orElse(self, a)
11  
-  
12  
-  final def oneOrMore: F[List[A]] = F.oneOrMore(self)
13  
-  
14  
-  final def zeroOrMore: F[List[A]] = F.zeroOrMore(self)
15  
-  ////
16  
-}
17  
-
18  
-trait ToAlternativeV0 {
19  
-  implicit def ToAlternativeVUnapply[FA](v: FA)(implicit F0: Unapply[Alternative, FA]) =
20  
-    new AlternativeV[F0.M,F0.A] { def self = F0(v); implicit def F: Alternative[F0.M] = F0.TC }
21  
-
22  
-}
23  
-
24  
-trait ToAlternativeV extends ToAlternativeV0 with ToApplicativeV {
25  
-  implicit def ToAlternativeV[F[_],A](v: F[A])(implicit F0: Alternative[F]) =
26  
-    new AlternativeV[F,A] { def self = v; implicit def F: Alternative[F] = F0 }
27  
-
28  
-  ////
29  
-
30  
-  ////
31  
-}
32  
-
33  
-trait AlternativeSyntax[F[_]] extends ApplicativeSyntax[F] {
34  
-  implicit def ToAlternativeV[A](v: F[A])(implicit F0: Alternative[F]): AlternativeV[F, A] = new AlternativeV[F,A] { def self = v; implicit def F: Alternative[F] = F0 }
35  
-
36  
-  ////
37  
-
38  
-  ////
39  
-}
6  core/src/main/scala/scalaz/syntax/Syntax.scala
@@ -37,10 +37,6 @@ trait Syntaxes {
37 37
 
38 38
   object applicative extends ToApplicativeV
39 39
   
40  
-  object alternative extends ToAlternativeV
41  
-
42  
-  object alternativeEmpty extends ToAlternativeEmptyV
43  
-
44 40
   object bind extends ToBindV
45 41
 
46 42
   object monad extends ToMonadV
@@ -95,7 +91,7 @@ trait ToAllTypeClassV
95 91
   with ToApplicativeV with ToBindV with ToMonadV with ToCoJoinV with ToCoMonadV
96 92
   with ToPlusV with ToApplicativePlusV with ToMonadPlusV with ToTraverseV with ToBiFunctorV
97 93
   with ToBiTraverseV with ToArrIdV with ToComposeV with ToCategoryV
98  
-  with ToArrowV with ToFoldableV with ToAlternativeV with ToAlternativeEmptyV
  94
+  with ToArrowV with ToFoldableV
99 95
 
100 96
 
101 97
 trait SyntaxV[A] {
23  scalacheck-binding/src/main/scala/scalaz/scalacheck/ScalazProperties.scala
@@ -218,29 +218,6 @@ object ScalazProperties {
218 218
     }
219 219
   }
220 220
 
221  
-  object alternative {
222  
-    def associative[F[_], X](implicit f: Alternative[F], afx: Arbitrary[F[X]], ef: Equal[F[X]]) =
223  
-      forAll(f.alternativeLaw.associative[X] _)
224  
-
225  
-    def laws[F[_]](implicit F: Alternative[F], afx: Arbitrary[F[Int]], ef: Equal[F[Int]]) = new Properties("alternative") {
226  
-      property("associative") = associative[F, Int]
227  
-    }
228  
-  }
229  
-
230  
-  object alternativeEmpty {
231  
-    def leftAlternativeIdentity[F[_], X](implicit f: AlternativeEmpty[F], afx: Arbitrary[F[X]], ef: Equal[F[X]]) =
232  
-      forAll(f.alternativeEmptyLaw.leftOrElseIdentity[X] _)
233  
-
234  
-    def rightAlternativeIdentity[F[_], X](implicit f: AlternativeEmpty[F], afx: Arbitrary[F[X]], ef: Equal[F[X]]) =
235  
-      forAll(f.alternativeEmptyLaw.rightOrElseIdentity[X] _)
236  
-
237  
-    def laws[F[_]](implicit F: AlternativeEmpty[F], afx: Arbitrary[F[Int]], af: Arbitrary[Int => Int], ef: Equal[F[Int]]) = new Properties("alternativeEmpty") {
238  
-      include(alternative.laws[F])
239  
-      property("left orElse identity") = leftAlternativeIdentity[F, Int]
240  
-      property("right orElse identity") = rightAlternativeIdentity[F, Int]
241  
-    }
242  
-  }
243  
-
244 221
   object monadPlus {
245 222
     def emptyMap[F[_], X](implicit f: MonadPlus[F], afx: Arbitrary[X => X], ef: Equal[F[X]]) =
246 223
       forAll(f.monadPlusLaw.emptyMap[X] _)
8  tests/src/test/scala/scalaz/KleisliTest.scala
@@ -24,11 +24,15 @@ class KleisliTest extends Spec {
24 24
       M.equal(mb1, mb2)
25 25
     }
26 26
   }
  27
+  
  28
+  "mapK" ! check {
  29
+    (f: Int => Option[Int], a: Int) => 
  30
+      Kleisli(f).mapK(_.toList.map(_.toString)).run(a)  must be_===(f(a).toList.map(_.toString))
  31
+  }
27 32
 
28 33
   checkAll(monoid.laws[KleisliOptInt[Int]])
29 34
   checkAll(monadPlus.laws[KleisliOptInt])
30 35
   checkAll(category.laws[KleisliOpt])
31  
-  checkAll(alternativeEmpty.laws[KleisliOptInt])
32 36
 
33 37
   object instances {
34 38
     def semigroup[F[_], A, B](implicit FB: Semigroup[F[B]]) = Semigroup[Kleisli[F, A, B]]
@@ -61,4 +65,4 @@ class KleisliTest extends Spec {
61 65
     def plus[F[_] : PlusEmpty, A] = Plus[({type f[a] = Kleisli[F, A, a]})#f]
62 66
     def empty[F[_] : MonadPlus, A] = PlusEmpty[({type f[a] = Kleisli[F, A, a]})#f]
63 67
   }
64  
-}
  68
+}
1  tests/src/test/scala/scalaz/OptionTTest.scala
@@ -12,7 +12,6 @@ class OptionTTest extends Spec {
12 12
   checkAll(equal.laws[OptionTList[Int]])
13 13
   checkAll(monad.laws[OptionTList])
14 14
   checkAll(traverse.laws[OptionTList])
15  
-  checkAll(alternativeEmpty.laws[OptionTOption])
16 15
 
17 16
   object instances {
18 17
     def functor[F[_] : Functor] = Functor[({type λ[α] = OptionT[F, α]})#λ]
6  tests/src/test/scala/scalaz/std/ListTest.scala
@@ -9,7 +9,6 @@ class ListTest extends Spec {
9 9
   checkAll(monoid.laws[List[Int]])
10 10
   checkAll(monadPlus.laws[List])
11 11
   checkAll(traverse.laws[List])
12  
-  checkAll(alternative.laws[List])
13 12
   
14 13
   import std.list.listSyntax._
15 14
   import syntax.monad._
@@ -61,9 +60,4 @@ class ListTest extends Spec {
61 60
     val actual = takeWhileN("/abc/def/hij/klm".toList, 4)(_ != '/').mkString
62 61
     actual must be_===("/abc/def/hij")
63 62
   }
64  
-
65  
-  "alternative" in {
66  
-    Alternative[List].orElse(List(), List(0)) must be_===(List(0))
67  
-    Alternative[List].orElse(List(0), List(1)) must be_===(List(0, 1))
68  
-  }
69 63
 }
1  tests/src/test/scala/scalaz/std/OptionTest.scala
@@ -15,7 +15,6 @@ class OptionTest extends Spec {
15 15
   checkAll("Option", monoid.laws[Option[Int]])
16 16
   checkAll("Option", monadPlus.laws[Option])
17 17
   checkAll("Option", traverse.laws[Option])
18  
-  checkAll("Option", alternativeEmpty.laws[Option])
19 18
   
20 19
   checkAll("Option @@ First", monoid.laws[Option[Int] @@ First])
21 20
   checkAll("Option @@ Last", monoid.laws[Option[Int] @@ Last])

1 note on commit 42ee545

Richard Wallace
Collaborator

Need to add instances for ApplicativePlus.

I also seem to be seeing a failure in the ListTTest. Seems that ListT.fromList[Option, Int](None).flatMap(_ => MonadPlus[({type l[a] = ListT[Option, a]})#l].empty) is coming up as ListT.fromList[Option, Int](None) which is not equal to empty - which is defined as M point Done. The empty definition for MonadPlus doesn't seem right to me. It seems like it should use MonadPlus[M].empty. I don't see how else the MonadPlus laws can be satisfied.

Please sign in to comment.
Something went wrong with that request. Please try again.