PageRenderTime 64ms CodeModel.GetById 14ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/scalacheck-binding/src/main/scala/scalaz/scalacheck/ScalazArbitrary.scala

http://github.com/scalaz/scalaz
Scala | 615 lines | 438 code | 167 blank | 10 comment | 11 complexity | c0fc798718e6616ac343a5d39ffff264 MD5 | raw file
  1package scalaz
  2package scalacheck
  3
  4import java.math.BigInteger
  5import org.scalacheck.{Cogen, Gen, Arbitrary}
  6import reflect.ClassTag
  7
  8/**
  9 * Instances of {@link scalacheck.Arbitrary} for many types in Scalaz.
 10 */
 11object ScalazArbitrary extends ScalazArbitraryPlatform {
 12  import Scalaz._
 13  import Tags._
 14  import Arbitrary._
 15  import Gen._
 16  import ScalaCheckBinding._
 17
 18  private def arb[A: Arbitrary]: Arbitrary[A] = implicitly[Arbitrary[A]]
 19
 20  implicit def cogenIList[A: Cogen]: Cogen[IList[A]] =
 21    Cogen[List[A]].contramap(_.toList)
 22
 23  implicit def cogenMaybe[A: Cogen]: Cogen[Maybe[A]] =
 24    Cogen[Option[A]].contramap(_.toOption)
 25
 26  implicit def cogenLazyOption[A: Cogen]: Cogen[LazyOption[A]] =
 27    Cogen[Option[A]].contramap(_.toOption)
 28
 29  implicit def cogenThese[A, B](implicit A: Cogen[A], B: Cogen[B]): Cogen[A \&/ B] =
 30    Cogen[(A \/ B) \/ (A, B)].contramap{
 31      case x @ \&/.Both(a, b) =>
 32        \/-((a, b))
 33      case \&/.This(a) =>
 34        -\/(-\/(a))
 35      case \&/.That(b) =>
 36        -\/(\/-(b))
 37    }
 38
 39  implicit def cogenLazyEither[A: Cogen, B: Cogen]: Cogen[LazyEither[A, B]] =
 40    Cogen[Either[A, B]].contramap(_.toEither)
 41
 42  implicit def cogenDisjunction[A, B](implicit A: Cogen[A], B: Cogen[B]): Cogen[A \/ B] =
 43    Cogen[Either[A, B]].contramap(_.toEither)
 44
 45  implicit def cogenValidation[A: Cogen, B: Cogen]: Cogen[Validation[A, B]] =
 46    Cogen[Either[A, B]].contramap(_.toEither)
 47
 48  implicit val cogenOrdering: Cogen[Ordering] =
 49    Cogen[Int].contramap {
 50      case Ordering.GT => 0
 51      case Ordering.EQ => 1
 52      case Ordering.LT => 2
 53    }
 54
 55  implicit def cogenOneOr[F[_], A: Cogen](implicit F: Cogen[F[A]]): Cogen[OneOr[F, A]] =
 56    Cogen[F[A] \/ A].contramap(_.run)
 57
 58  implicit def cogenOneAnd[F[_], A: Cogen](implicit F: Cogen[F[A]]): Cogen[OneAnd[F, A]] =
 59    Cogen[(A, F[A])].contramap(a => (a.head, a.tail))
 60
 61  implicit def cogenISet[A: Cogen]: Cogen[ISet[A]] =
 62    Cogen[List[A]].contramap(_.toList)
 63
 64  implicit def cogenIMap[A: Cogen, B: Cogen]: Cogen[A ==>> B] =
 65    Cogen[List[(A, B)]].contramap(_.toList)
 66
 67  implicit def cogenDList[A: Cogen]: Cogen[DList[A]] =
 68    Cogen[List[A]].contramap(_.toList)
 69
 70  implicit def cogenEphemeralStream[A: Cogen]: Cogen[EphemeralStream[A]] =
 71    Cogen[List[A]].contramap(_.toList)
 72
 73  implicit def cogenHeap[A: Cogen]: Cogen[Heap[A]] =
 74    Cogen[EphemeralStream[A]].contramap(_.toUnsortedStream)
 75
 76  implicit def cogenDequeue[A: Cogen]: Cogen[Dequeue[A]] =
 77    Cogen[List[A]].contramap(_.toList)
 78
 79  implicit def cogenNonEmptyList[A: Cogen]: Cogen[NonEmptyList[A]] =
 80    Cogen[(A, IList[A])].contramap(nel => (nel.head, nel.tail))
 81
 82  implicit def cogenIndSeq[A: Cogen]: Cogen[IndSeq[A]] =
 83    Cogen[List[A]].contramap(Foldable[IndSeq].toList)
 84
 85  implicit def cogenDiev[A: Cogen]: Cogen[Diev[A]] =
 86    Cogen[Vector[(A, A)]].contramap(_.intervals)
 87
 88  implicit def cogenCoproduct[F[_], G[_], A](implicit A: Cogen[F[A] \/ G[A]]): Cogen[Coproduct[F, G, A]] =
 89    Cogen[F[A] \/ G[A]].contramap(_.run)
 90
 91  implicit def cogenConst[A, B](implicit A: Cogen[A]): Cogen[Const[A, B]] =
 92    A.contramap(_.getConst)
 93
 94  implicit def cogenZipper[A](implicit A: Cogen[A]): Cogen[Zipper[A]] =
 95    Cogen[(Stream[A], A, Stream[A])].contramap(z => (z.lefts, z.focus, z.rights))
 96
 97  implicit def cogenTracedT[W[_], A, B](implicit W: Cogen[W[A => B]]): Cogen[TracedT[W, A, B]] =
 98    W.contramap(_.run)
 99
100  implicit def cogenIndexedStoreT[F[_], I: Cogen, A, B](implicit F: Cogen[F[A => B]]): Cogen[IndexedStoreT[F, I, A, B]] =
101    Cogen[(F[A => B], I)].contramap(_.run)
102
103  implicit def cogenIndexedContsT[W[_], R, O, M[_], A](implicit F: Cogen[W[A => M[O]] => M[R]]): Cogen[IndexedContsT[W, R, O, M, A]] =
104    F.contramap(_.run)
105
106  implicit def cogenEndo[A](implicit A: Cogen[A => A]): Cogen[Endo[A]] =
107    A.contramap(_.run)
108
109  implicit def cogenEndomorphic[F[_, _], A](implicit F: Cogen[F[A, A]]): Cogen[Endomorphic[F, A]] =
110    F.contramap(_.run)
111
112  implicit def cogenKleisli[F[_], A, B](implicit F: Cogen[A => F[B]]): Cogen[Kleisli[F, A, B]] =
113    F.contramap(_.run)
114
115  implicit def cogenCokleisli[F[_], A, B](implicit F: Cogen[F[A] => B]): Cogen[Cokleisli[F, A, B]] =
116    F.contramap(_.run)
117
118  implicit def cogenNullResult[A, B](implicit F: Cogen[A => Option[B]]): Cogen[NullResult[A, B]] =
119    F.contramap(_.apply)
120
121  implicit def cogenNullArgument[A, B](implicit F: Cogen[Option[A] => B]): Cogen[NullArgument[A, B]] =
122    F.contramap(_.apply)
123
124  implicit def cogenContravariantCoyoneda[F[_]: Contravariant, A](implicit F: Cogen[F[A]]): Cogen[ContravariantCoyoneda[F, A]] =
125    Cogen[F[A]].contramap(_.run)
126
127  implicit def cogenEitherT[A, F[_], B](implicit F: Cogen[F[A \/ B]]): Cogen[EitherT[A, F, B]] =
128    F.contramap(_.run)
129
130  implicit def cogenLazyEitherT[F[_], A, B](implicit F: Cogen[F[LazyEither[A, B]]]): Cogen[LazyEitherT[F, A, B]] =
131    F.contramap(_.run)
132
133  implicit def cogenTheseT[F[_], A, B](implicit F: Cogen[F[A \&/ B]]): Cogen[TheseT[F, A, B]] =
134    F.contramap(_.run)
135
136  implicit def cogenMaybeT[F[_], A](implicit F: Cogen[F[Maybe[A]]]): Cogen[MaybeT[F, A]] =
137    F.contramap(_.run)
138
139  implicit def cogenStreamT[F[_]: Monad, A](implicit F: Cogen[F[LazyList[A]]]): Cogen[StreamT[F, A]] =
140    F.contramap(_.toLazyList)
141
142  implicit def cogenOptionT[F[_], A](implicit F: Cogen[F[Option[A]]]): Cogen[OptionT[F, A]] =
143    F.contramap(_.run)
144
145  implicit def cogenLazyOptionT[F[_], A](implicit F: Cogen[F[LazyOption[A]]]): Cogen[LazyOptionT[F, A]] =
146    F.contramap(_.run)
147
148  implicit def cogenIdT[F[_], A](implicit F: Cogen[F[A]]): Cogen[IdT[F, A]] =
149    F.contramap(_.run)
150
151  implicit def cogenIndexedReaderWriterStateT[R, W, S1, S2, F[_]: Monad, A](implicit F: Cogen[(R, S1) => F[(W, A, S2)]]): Cogen[IndexedReaderWriterStateT[R, W, S1, S2, F, A]] =
152    F.contramap(_.run)
153
154  implicit def cogenIndexedStateT[F[_]: Monad, S1, S2, A](implicit F: Cogen[S1 => F[(S2, A)]]): Cogen[IndexedStateT[S1, S2, F, A]] =
155    F.contramap(s => s.apply(_))
156
157  implicit def cogenWriterT[A, F[_], B](implicit F: Cogen[F[(A, B)]]): Cogen[WriterT[A, F, B]] =
158    F.contramap(_.run)
159
160  implicit def cogenUnwriterT[F[_], A, B](implicit F: Cogen[F[(A, B)]]): Cogen[UnwriterT[F, A, B]] =
161    F.contramap(_.run)
162
163  implicit def cogenTree[A: Cogen]: Cogen[Tree[A]] =
164    Cogen[List[A]].contramap(_.toList)
165
166  implicit def cogenTreeLoc[A: Cogen]: Cogen[TreeLoc[A]] =
167    Divide[Cogen].dividing4(Function.unlift(TreeLoc.unapply))
168
169  implicit def cogenStrictTree[A: Cogen]: Cogen[StrictTree[A]] =
170    Cogen[List[A]].contramap(_.toList)
171
172  implicit def cogenCoyoneda[F[_]: Functor, A](implicit F: Cogen[F[A]]): Cogen[Coyoneda[F, A]] =
173    F.contramap(_.run)
174
175  private[this] def nameToValue[A]: Name[A] => A = _.value
176
177  implicit def cogenNeed[A](implicit A: Cogen[A]): Cogen[Need[A]] =
178    A.contramap(nameToValue)
179
180  implicit def cogenValue[A](implicit A: Cogen[A]): Cogen[Value[A]] =
181    A.contramap(nameToValue)
182
183  implicit def cogenName[A](implicit A: Cogen[A]): Cogen[Name[A]] =
184    A.contramap(nameToValue)
185
186  implicit def cogenImmutableArray[A: Cogen]: Cogen[ImmutableArray[A]] =
187    Cogen[List[A]].contramap(_.toList)
188
189  implicit def cogenEither3[A1: Cogen, A2: Cogen, A3: Cogen]: Cogen[Either3[A1, A2, A3]] =
190    Cogen[A1 \/ A2 \/ A3].contramap{
191      case Left3(a) => -\/(-\/(a))
192      case Middle3(a) => -\/(\/-(a))
193      case Right3(a) => \/-(a)
194    }
195
196  implicit def cogenLazyTuple2[A1: Cogen, A2: Cogen]: Cogen[LazyTuple2[A1, A2]] =
197    Cogen[(A1, A2)].contramap(t => (t._1, t._2))
198
199  implicit def cogenLazyTuple3[A1: Cogen, A2: Cogen, A3: Cogen]: Cogen[LazyTuple3[A1, A2, A3]] =
200    Cogen[(A1, A2, A3)].contramap(t => (t._1, t._2, t._3))
201
202  implicit def cogenLazyTuple4[A1: Cogen, A2: Cogen, A3: Cogen, A4: Cogen]: Cogen[LazyTuple4[A1, A2, A3, A4]] =
203    Cogen[(A1, A2, A3, A4)].contramap(t => (t._1, t._2, t._3, t._4))
204
205  implicit def cogenAp[F[_], A](implicit F: Cogen[F[A]]): Cogen[Ap[F, A]] =
206    F.contramap(_.f)
207
208  implicit def cogenTannen[F[_], G[_, _], A, B](implicit F: Cogen[F[G[A, B]]]): Cogen[Tannen[F, G, A, B]] =
209    F.contramap(_.f)
210
211  implicit def tannenArbitrary[F[_], G[_, _], A, B](implicit F: Arbitrary[F[G[A, B]]]): Arbitrary[Tannen[F, G, A, B]] =
212    Functor[Arbitrary].map(F)(Tannen[F, G, A, B](_))
213
214  implicit def apArbitrary[F[_], A](implicit F: Arbitrary[F[A]]): Arbitrary[Ap[F, A]] =
215    Functor[Arbitrary].map(F)(Ap[F, A](_))
216
217  implicit def cogenAlter[F[_], A](implicit F: Cogen[F[A]]): Cogen[Alter[F, A]] =
218    F.contramap(_.f)
219
220  implicit def alterArbitrary[F[_], A](implicit F: Arbitrary[F[A]]): Arbitrary[Alter[F, A]] =
221    Functor[Arbitrary].map(F)(Alter[F, A](_))
222
223  implicit def monoidCoproductArbitrary[M: Arbitrary, N: Arbitrary]: Arbitrary[M :+: N] =
224    Functor[Arbitrary].map(arb[Vector[M \/ N]])(new :+:(_))
225
226  /** @since 7.0.3 */
227  implicit def theseArb[A: Arbitrary, B: Arbitrary]: Arbitrary[A \&/ B] =
228    Arbitrary(Gen.oneOf(
229      ^(arbitrary[A], arbitrary[B])(\&/.Both(_, _)),
230      arbitrary[A].map(\&/.This[A, B](_)),
231      arbitrary[B].map(\&/.That[A, B](_))
232    ))
233
234  implicit def endoArb[A](implicit A: Arbitrary[A => A]): Arbitrary[Endo[A]] =
235    Functor[Arbitrary].map(A)(Endo.endo)
236
237  implicit def endoByNameArb[A](implicit A: Arbitrary[A => A]): Arbitrary[EndoByName[A]] =
238    Functor[Arbitrary].map(A)(f => Endo.endoByName(f(_)))
239
240  implicit def endomorphicArbitrary[F[_, _], A](implicit F: Arbitrary[F[A, A]]): Arbitrary[Endomorphic[F, A]] =
241    Functor[Arbitrary].map(F)(Endomorphic[F, A](_))
242
243  implicit def EphemeralStreamArbitrary[A : Arbitrary]: Arbitrary[EphemeralStream[A]] =
244    Functor[Arbitrary].map(arb[Stream[A]])(EphemeralStream.fromStream[A](_))
245
246  implicit def IStreamArbitrary[A : Arbitrary]: Arbitrary[IStream[A]] =
247    Functor[Arbitrary].map(arb[Stream[A]])(IStream.fromStream[A](_))
248
249  implicit def CorecursiveListArbitrary[A : Arbitrary]: Arbitrary[CorecursiveList[A]] =
250    Functor[Arbitrary].map(arb[Stream[A]])(CorecursiveList.fromStream)
251
252  implicit def ImmutableArrayArbitrary[A : Arbitrary : ClassTag]: Arbitrary[ImmutableArray[A]] =
253    Functor[Arbitrary].map(arb[Array[A]])(ImmutableArray.fromArray[A](_))
254
255  implicit def ValueArbitrary[A: Arbitrary]: Arbitrary[Value[A]] = Functor[Arbitrary].map(arb[A])(a => Value(a))
256  implicit def NameArbitrary[A: Arbitrary]: Arbitrary[Name[A]] = Functor[Arbitrary].map(arb[A])(a => Name(a))
257  implicit def NeedArbitrary[A: Arbitrary]: Arbitrary[Need[A]] = Functor[Arbitrary].map(arb[A])(a => Need(a))
258
259  implicit val UnitArbitrary: Arbitrary[Unit] = Arbitrary(const(()))
260
261  implicit val AlphaArbitrary: Arbitrary[Alpha] = {
262    val alphaList = Alpha.alphas.toList
263    Arbitrary(oneOf(alphaList))
264  }
265
266  implicit val BooleanConjunctionArbitrary: Arbitrary[Boolean @@ Conjunction] = Functor[Arbitrary].map(arb[Boolean])(_.conjunction)
267
268  implicit val arbBigInt: Arbitrary[BigInt] = Apply[Arbitrary].apply2[Int, Int, BigInt](arb[Int], arb[Int])(_ + _)
269
270  implicit val arbBigInteger: Arbitrary[BigInteger] = Functor[Arbitrary].map(arb[BigInt])(_.bigInteger)
271
272  implicit val BigIntegerMultiplicationArbitrary: Arbitrary[BigInteger @@ Multiplication] =
273    Tag.subst[BigInteger, Arbitrary, Multiplication](arb[BigInteger])
274
275  implicit val BigIntMultiplicationArbitrary: Arbitrary[BigInt @@ Multiplication] = Tag.subst(arb[BigInt])
276
277  implicit val ByteMultiplicationArbitrary: Arbitrary[Byte @@ Multiplication] = Tag.subst(arb[Byte])
278
279  implicit val CharMultiplicationArbitrary: Arbitrary[Char @@ Multiplication] = Tag.subst(arb[Char])
280
281  implicit val ShortMultiplicationArbitrary: Arbitrary[Short @@ Multiplication] = Tag.subst(arb[Short])
282
283  implicit val IntMultiplicationArbitrary: Arbitrary[Int @@ Multiplication] = Tag.subst(arb[Int])
284
285  implicit val LongMultiplicationArbitrary: Arbitrary[Long @@ Multiplication] = Tag.subst(arb[Long])
286
287  implicit val FloatMultiplicationArbitrary: Arbitrary[Float @@ Multiplication] = Tag.subst(arb[Float])
288
289  implicit val DoubleMultiplicationArbitrary: Arbitrary[Double @@ Multiplication] = Tag.subst(arb[Double])
290
291  implicit val IntDualArbitrary: Arbitrary[Int @@ Dual] = Tag.subst(arb[Int])
292
293  implicit val DigitArbitrary: Arbitrary[Digit] = {
294    val digitList = Digit.digits.toList
295    Arbitrary(oneOf(digitList))
296  }
297
298  import NonEmptyList._
299  implicit def NonEmptyListArbitrary[A: Arbitrary]: Arbitrary[NonEmptyList[A]] = Apply[Arbitrary].apply2[A, IList[A], NonEmptyList[A]](arb[A], ilistArbitrary)(nel(_, _))
300
301
302  /** @since 7.0.3 */
303  implicit def OneAndArbitrary[F[_], A](implicit A: Arbitrary[A], FA: Arbitrary[F[A]]
304                                        ): Arbitrary[OneAnd[F, A]] =
305    Apply[Arbitrary].apply2(arb[A], arb[F[A]])(OneAnd.apply)
306
307  /** @since 7.0.3 */
308  implicit def OneOrArbitrary[F[_], A](implicit A: Arbitrary[A], FA: Arbitrary[F[A]]): Arbitrary[OneOr[F, A]] =
309    Functor[Arbitrary].map(arb[F[A] \/ A])(OneOr(_))
310
311  /** @since 7.1.0 */
312  implicit def Arbitrary_==>>[A, B](implicit o: Order[A], A: Arbitrary[A], B: Arbitrary[B]): Arbitrary[A ==>> B] =
313    Functor[Arbitrary].map(arb[List[(A, B)]])(as => ==>>.fromList(as))
314
315  implicit def Arbitrary_ISet[A](implicit o: Order[A], A: Arbitrary[A]): Arbitrary[ISet[A]] =
316    Functor[Arbitrary].map(arb[List[A]])(as => ISet.fromList(as))
317
318  implicit def Arbitrary_Maybe[A: Arbitrary]: Arbitrary[Maybe[A]] =
319    Functor[Arbitrary].map(arb[Option[A]])(Maybe.fromOption)
320
321  import scalaz.Ordering._
322  implicit val OrderingArbitrary: Arbitrary[Ordering] = Arbitrary(oneOf(LT, EQ, GT))
323
324  private[this] def withSize[A](size: Int)(f: Int => Gen[A]): Gen[EphemeralStream[A]] = {
325    Applicative[Gen].sequence(
326      (IList.fill(size)(Gen.choose(1, size max 1))).toEphemeralStream
327    ).flatMap { s =>
328      val ns = Traverse[EphemeralStream].traverseS(s) { n =>
329        for {
330          sum <- State.get[Int]
331          r <- if (sum >= size) {
332            State.state[Int, Option[Int]](None)
333          } else if ((sum + n) > size) {
334            State((s: Int) => (s + n) -> Option(size - sum))
335          } else {
336            State((s: Int) => (s + n) -> Option(n))
337          }
338        } yield r
339      }.eval(0).flatMap(o => o.cata[EphemeralStream[Int]](EphemeralStream(_), EphemeralStream.emptyEphemeralStream))
340
341      Applicative[Gen].sequence(ns.map(f))
342    }
343  }
344
345  private[scalaz] def treeGenSized[A: NotNothing](size: Int)(implicit A: Arbitrary[A]): Gen[Tree[A]] =
346    size match {
347      case n if n <= 1 =>
348        A.arbitrary.map(a => Tree.Leaf(a))
349      case 2 =>
350        arb[(A, A)].arbitrary.map{ case (a1, a2) =>
351          Tree.Node(a1, EphemeralStream(Tree.Leaf(a2)))
352        }
353      case 3 =>
354        arb[(A, A, A)].arbitrary.flatMap{ case (a1, a2, a3) =>
355          Gen.oneOf(
356            Tree.Node(a1, EphemeralStream(Tree.Leaf(a2), Tree.Leaf(a3))),
357            Tree.Node(a1, EphemeralStream(Tree.Node(a2, EphemeralStream(Tree.Leaf(a3)))))
358          )
359        }
360      case _ =>
361        withSize(size - 1)(treeGenSized[A]).flatMap{ as =>
362          A.arbitrary.map(a => Tree.Node(a, as))
363        }
364    }
365
366  implicit def TreeArbitrary[A: Arbitrary]: Arbitrary[Tree[A]] =
367    Arbitrary(Gen.sized(n =>
368      if (n < 1) treeGenSized[A](0)
369      else Gen.choose(1, n).flatMap(treeGenSized[A])
370    ))
371
372  private[scalaz] def strictTreeGenSized[A: NotNothing](size: Int)(implicit A: Arbitrary[A]): Gen[StrictTree[A]] =
373    size match {
374      case n if n <= 1 =>
375        A.arbitrary.map(a => StrictTree.Leaf(a))
376      case 2 =>
377        arb[(A, A)].arbitrary.map{ case (a1, a2) =>
378          StrictTree(a1, Vector(StrictTree.Leaf(a2)))
379        }
380      case 3 =>
381        arb[(A, A, A)].arbitrary.flatMap{ case (a1, a2, a3) =>
382          Gen.oneOf(
383            StrictTree(a1, Vector(StrictTree.Leaf(a2), StrictTree.Leaf(a3))),
384            StrictTree(a1, Vector(StrictTree(a2, Vector(StrictTree.Leaf(a3)))))
385          )
386        }
387      case _ =>
388        withSize(size - 1)(strictTreeGenSized[A]).flatMap{ as =>
389          A.arbitrary.map(a => StrictTree(a, as.toVector))
390        }
391    }
392
393  implicit def StrictTreeArbitrary[A: Arbitrary]: Arbitrary[StrictTree[A]] =
394    Arbitrary(Gen.sized(n =>
395      if (n < 1) strictTreeGenSized[A](0)
396      else Gen.choose(1, n).flatMap(strictTreeGenSized[A])
397    ))
398
399  private[scalaz] def treeLocGenSized[A: NotNothing](size: Int)(implicit A: Arbitrary[A]): Gen[TreeLoc[A]] = {
400    def forest(n: Int): Gen[TreeLoc.TreeForest[A]] =
401      withSize(n)(treeGenSized[A])
402
403    val parent: Int => Gen[TreeLoc.Parent[A]] = { n =>
404      Gen.choose(0, (n - 1) max 0).flatMap { x1 =>
405        Apply[Gen].tuple3(
406          forest(x1), A.arbitrary, forest(n - x1 - 1)
407        )
408      }
409    }
410
411    for{
412      a <- Gen.choose(1, size max 1)
413      b = size - a
414      aa <- Gen.choose(1, a max 1)
415      ba <- Gen.choose(0, b max 0)
416      t <- Apply[Gen].apply4(
417        treeGenSized[A](aa),
418        forest(a - aa),
419        forest(ba),
420        withSize(b - ba)(parent)
421      )(TreeLoc.apply[A])
422    } yield t
423  }
424
425  implicit def IterableArbitrary[A: Arbitrary]: Arbitrary[Iterable[A]] =
426      Apply[Arbitrary].apply2[A, List[A], Iterable[A]](arb[A], arb[List[A]])((a, list) => a :: list)
427
428  implicit def TreeLocArbitrary[A: Arbitrary]: Arbitrary[TreeLoc[A]] =
429    Arbitrary(Gen.sized(n =>
430      Gen.choose(0, n).flatMap(treeLocGenSized[A])
431    ))
432
433  implicit def DisjunctionArbitrary[A: Arbitrary, B: Arbitrary]: Arbitrary[A \/ B] =
434    Functor[Arbitrary].map(arb[Either[A, B]]) {
435      case Left(a) => -\/(a)
436      case Right(b) => \/-(b)
437    }
438
439  implicit def ValidationArbitrary[A: Arbitrary, B: Arbitrary]: Arbitrary[Validation[A, B]] =
440    Functor[Arbitrary].map(arb[A \/ B])(_.toValidation)
441
442//  implicit def ZipStreamArbitrary[A](implicit a: Arbitrary[A]): Arbitrary[ZipStream[A]] = arb[Stream[A]] ∘ ((s: Stream[A]) => s.ʐ)
443
444  implicit def Tuple1Arbitrary[A: Arbitrary]: Arbitrary[Tuple1[A]] = Functor[Arbitrary].map(arb[A])(Tuple1(_))
445
446  implicit def Function0Arbitrary[A: Arbitrary]: Arbitrary[() => A] = Functor[Arbitrary].map(arb[A])(() => _)
447
448  implicit def FirstOptionArbitrary[A: Arbitrary]: Arbitrary[Option[A] @@ First] = Functor[Arbitrary].map(arb[Option[A]])(_.first)
449
450  implicit def LastOptionArbitrary[A: Arbitrary]: Arbitrary[Option[A] @@ Last] = Functor[Arbitrary].map(arb[Option[A]])(_.last)
451
452  implicit def MinOptionArbitrary[A: Arbitrary]: Arbitrary[MinOption[A]] = Tag.subst(arb[Option[A]])
453
454  implicit def MaxOptionArbitrary[A: Arbitrary]: Arbitrary[MaxOption[A]] = Tag.subst(arb[Option[A]])
455
456  implicit def FirstMaybeArbitrary[A: Arbitrary]: Arbitrary[Maybe[A] @@ First] = Functor[Arbitrary].map(arb[Maybe[A]])(_.first)
457
458  implicit def LastMaybeArbitrary[A: Arbitrary]: Arbitrary[Maybe[A] @@ Last] = Functor[Arbitrary].map(arb[Maybe[A]])(_.last)
459
460  implicit def MinMaybeArbitrary[A: Arbitrary]: Arbitrary[MinMaybe[A]] = Tag.subst(arb[Maybe[A]])
461
462  implicit def MaxMaybeArbitrary[A: Arbitrary]: Arbitrary[MaxMaybe[A]] = Tag.subst(arb[Maybe[A]])
463
464  import FingerTree._
465
466  implicit def FingerArbitrary[V: Monoid, A](implicit a: Arbitrary[A], measure: Reducer[A, V]): Arbitrary[Finger[V, A]] = Arbitrary(oneOf(
467    arbitrary[A].map(one(_): Finger[V, A]),
468    ^(arbitrary[A], arbitrary[A])(two(_, _): Finger[V, A]),
469    ^^(arbitrary[A], arbitrary[A], arbitrary[A])(three(_, _, _): Finger[V, A]),
470    ^^^(arbitrary[A], arbitrary[A], arbitrary[A], arbitrary[A])(four(_, _, _, _): Finger[V, A])
471  ))
472
473  implicit def NodeArbitrary[V: Monoid, A](implicit a: Arbitrary[A], measure: Reducer[A, V]): Arbitrary[Node[V, A]] = Arbitrary(oneOf(
474    ^(arbitrary[A], arbitrary[A])(node2[V, A](_, _)),
475    ^^(arbitrary[A], arbitrary[A], arbitrary[A])(node3[V, A](_, _, _))
476  ))
477
478  implicit def FingerTreeArbitrary[V: Monoid, A](implicit a: Arbitrary[A], measure: Reducer[A, V]): Arbitrary[FingerTree[V, A]] = Arbitrary {
479    def fingerTree[A](n: Int)(implicit a1: Arbitrary[A], measure1: Reducer[A, V]): Gen[FingerTree[V, A]] = n match {
480      case 0 => empty[V, A]
481      case 1 => arbitrary[A].map(single[V, A](_))
482      case n => {
483        val nextSize = n.abs / 2
484        ^^(FingerArbitrary[V, A].arbitrary,
485          fingerTree[Node[V, A]](nextSize)(NodeArbitrary[V, A], implicitly),
486          FingerArbitrary[V, A].arbitrary
487        )(deep[V, A](_, _, _))
488      }
489    }
490    Gen.sized(fingerTree[A] _)
491  }
492
493  implicit def IndSeqArbibrary[A: Arbitrary]: Arbitrary[IndSeq[A]] = Functor[Arbitrary].map(arb[List[A]])(IndSeq.fromSeq)
494
495  import java.util.concurrent.Callable
496
497  implicit def CallableArbitrary[A: Arbitrary]: Arbitrary[Callable[A]] = Functor[Arbitrary].map(arb[A])((x: A) => Applicative[Callable].point(x))
498
499  import Zipper._
500  implicit def ZipperArbitrary[A: Arbitrary]: Arbitrary[Zipper[A]] =
501    Apply[Arbitrary].apply3[Stream[A], A, Stream[A], Zipper[A]](arb[Stream[A]], arb[A], arb[Stream[A]])(zipper[A](_, _, _))
502
503  implicit def KleisliArbitrary[M[_], A, B](implicit a: Arbitrary[A => M[B]]): Arbitrary[Kleisli[M, A, B]] =
504    Functor[Arbitrary].map(a)(Kleisli[M, A, B](_))
505
506  implicit def CokleisliArbitrary[M[_], A, B](implicit a: Arbitrary[M[A] => B]): Arbitrary[Cokleisli[M, A, B]] =
507    Functor[Arbitrary].map(a)(Cokleisli[M, A, B](_))
508
509  implicit def CoproductArbitrary[F[_], G[_], A](implicit a: Arbitrary[F[A] \/ G[A]]): Arbitrary[Coproduct[F, G, A]] =
510    Functor[Arbitrary].map(a)(Coproduct(_))
511
512  implicit def writerTArb[W, F[_], A](implicit A: Arbitrary[F[(W, A)]]): Arbitrary[WriterT[W, F, A]] =
513    Functor[Arbitrary].map(A)(WriterT[W, F, A](_))
514
515  implicit def unwriterTArb[F[_], U, A](implicit A: Arbitrary[F[(U, A)]]): Arbitrary[UnwriterT[F, U, A]] =
516    Functor[Arbitrary].map(A)(UnwriterT[F, U, A](_))
517
518  implicit def optionTArb[F[_], A](implicit A: Arbitrary[F[Option[A]]]): Arbitrary[OptionT[F, A]] =
519    Functor[Arbitrary].map(A)(OptionT[F, A](_))
520
521  implicit def maybeTArb[F[_], A](implicit A: Arbitrary[F[Maybe[A]]]): Arbitrary[MaybeT[F, A]] =
522    Functor[Arbitrary].map(A)(MaybeT[F, A](_))
523
524  implicit def lazyOptionArb[F[_], A](implicit A: Arbitrary[Option[A]]): Arbitrary[LazyOption[A]] =
525    Functor[Arbitrary].map(A)(LazyOption.fromOption[A](_))
526
527  implicit def lazyOptionTArb[F[_], A](implicit A: Arbitrary[F[LazyOption[A]]]): Arbitrary[LazyOptionT[F, A]] =
528    Functor[Arbitrary].map(A)(LazyOptionT[F, A](_))
529
530  implicit def lazyEitherArb[F[_], A: Arbitrary, B: Arbitrary]: Arbitrary[LazyEither[A, B]] =
531    Functor[Arbitrary].map(arb[Either[A, B]]) {
532      case Left(a)  => LazyEither.lazyLeft(a)
533      case Right(b) => LazyEither.lazyRight(b)
534    }
535
536  implicit def lazyEitherTArb[F[_], A, B](implicit A: Arbitrary[F[LazyEither[A, B]]]): Arbitrary[LazyEitherT[F, A, B]] =
537    Functor[Arbitrary].map(A)(LazyEitherT[F, A, B](_))
538
539  implicit def idTArbitrary[A, F[_]](implicit A: Arbitrary[F[A]]): Arbitrary[IdT[F, A]] =
540    Functor[Arbitrary].map(A)(IdT[F, A])
541
542  // backwards compatibility
543  def stateTArb[S, F[+_], A](implicit A: Arbitrary[S => F[(S, A)]]): Arbitrary[StateT[S, F, A]] =
544    indexedStateTArb[S, S, F, A](A)
545
546  implicit def indexedReaderWriterStateTArb[R, W, S1, S2, F[_], A](implicit A: Arbitrary[(R, S1) => F[(W, A, S2)]]): Arbitrary[IndexedReaderWriterStateT[R, W, S1, S2, F, A]] =
547    Functor[Arbitrary].map(A)(IndexedReaderWriterStateT[R, W, S1, S2, F, A](_))
548
549  implicit def tracedTArb[W[_], A, B](implicit A: Arbitrary[W[A => B]]): Arbitrary[TracedT[W, A, B]] =
550    Functor[Arbitrary].map(A)(TracedT(_))
551
552  implicit def indexedContsTArb[W[_], R, O, M[_], A](implicit A: Arbitrary[W[A => M[O]] => M[R]]): Arbitrary[IndexedContsT[W, R, O, M, A]] =
553    Functor[Arbitrary].map(A)(IndexedContsT(_))
554
555  implicit def indexedStateTArb[S1, S2, F[_], A](implicit A: Arbitrary[S1 => F[(S2, A)]]): Arbitrary[IndexedStateT[S1, S2, F, A]] =
556    Functor[Arbitrary].map(A)(IndexedStateT[S1, S2, F, A](_))
557
558  implicit def eitherTArb[A, F[_], B](implicit A: Arbitrary[F[A \/ B]]): Arbitrary[EitherT[A, F, B]] =
559      Functor[Arbitrary].map(A)(EitherT[A, F, B](_))
560
561  implicit def theseTArb[F[_], A, B](implicit A: Arbitrary[F[A \&/ B]]): Arbitrary[TheseT[F, A, B]] =
562    Functor[Arbitrary].map(A)(TheseT[F, A, B](_))
563
564  implicit def constArbitrary[A: Arbitrary, B]: Arbitrary[Const[A, B]] =
565    Functor[Arbitrary].map(arb[A])(Const(_))
566
567  implicit def dlistArbitrary[A](implicit A: Arbitrary[List[A]]): Arbitrary[DList[A]] = Functor[Arbitrary].map(A)(as => DList(as : _*))
568
569  implicit def ilistArbitrary[A](implicit A: Arbitrary[List[A]]): Arbitrary[IList[A]] = Functor[Arbitrary].map(A)(IList.fromList)
570
571  implicit def dequeueArbitrary[A](implicit A: Arbitrary[List[A]]): Arbitrary[Dequeue[A]] = Functor[Arbitrary].map(A)(Dequeue.apply)
572
573  implicit def lazyTuple2Arbitrary[A: Arbitrary, B: Arbitrary]: Arbitrary[LazyTuple2[A, B]] =
574    Applicative[Arbitrary].apply2(arb[A], arb[B])(LazyTuple2(_, _))
575
576  implicit def lazyTuple3Arbitrary[A: Arbitrary, B: Arbitrary, C: Arbitrary]: Arbitrary[LazyTuple3[A, B, C]] =
577    Applicative[Arbitrary].apply3(arb[A], arb[B], arb[C])(LazyTuple3(_, _, _))
578
579  implicit def lazyTuple4Arbitrary[A: Arbitrary, B: Arbitrary, C: Arbitrary, D: Arbitrary]: Arbitrary[LazyTuple4[A, B, C, D]] =
580    Applicative[Arbitrary].apply4(arb[A], arb[B], arb[C], arb[D])(LazyTuple4(_, _, _, _))
581
582  implicit def heapArbitrary[A](implicit O: Order[A], A: Arbitrary[List[A]]): Arbitrary[Heap[A]] = {
583    Functor[Arbitrary].map(A)(as => Heap.fromData(as))
584  }
585
586  // backwards compatibility
587  def storeTArb[F[+_], A, B](implicit A: Arbitrary[(F[A => B], A)]): Arbitrary[StoreT[F, A, B]] = indexedStoreTArb[F, A, A, B](A)
588
589  implicit def indexedStoreTArb[F[_], I, A, B](implicit A: Arbitrary[(F[A => B], I)]): Arbitrary[IndexedStoreT[F, I, A, B]] = Functor[Arbitrary].map(A)(IndexedStoreT[F, I, A, B](_))
590
591  implicit def listTArb[F[_]: Applicative, A](implicit FA: Arbitrary[F[IList[A]]]): Arbitrary[ListT[F, A]] = Functor[Arbitrary].map(FA)(ListT.fromIList(_))
592
593  implicit def streamTArb[F[_], A](implicit FA: Arbitrary[F[LazyList[A]]], F: Applicative[F]): Arbitrary[StreamT[F, A]] = Functor[Arbitrary].map(FA)(StreamT.fromLazyList(_))
594
595  implicit def CaseInsensitiveArbitrary[A](implicit A0: Arbitrary[A], A1: FoldCase[A]): Arbitrary[CaseInsensitive[A]] =
596    Functor[Arbitrary].map(A0)(CaseInsensitive(_))
597
598  implicit def dievArbitrary[A](implicit A: Arbitrary[List[A]], E: Enum[A]): Arbitrary[Diev[A]] = Functor[Arbitrary].map(A)(_.grouped(2).foldLeft(Diev.empty[A]){(working, possiblePair) =>
599    possiblePair match {
600      case first :: second :: Nil => working + ((first, second))
601      case value :: Nil => working
602      case _ => sys.error("Unexpected amount of items in paired list.")
603    }
604  })
605
606  implicit def iterateeInputArbitrary[A: Arbitrary]: Arbitrary[scalaz.iteratee.Input[A]] = {
607    import scalaz.iteratee.Input._
608    Arbitrary(Gen.oneOf(
609      Gen.const(emptyInput[A]),
610      Gen.const(eofInput[A]),
611      arbitrary[A].map(e => elInput(e))
612    ))
613  }
614
615}