zio / zio-prelude

A lightweight, distinctly Scala take on functional abstractions, with tight ZIO integration
https://zio.dev/zio-prelude
Apache License 2.0
441 stars 112 forks source link

Laws and tests for Divariant #290

Open lemastero opened 3 years ago

lemastero commented 3 years ago

I am able to add laws using predicates like in IdentityCompose

  def identityCompose[A, B](
    ab: A :=> B
  )(implicit eq: Equal[A :=> B]): Boolean = {
    val ab1 = compose(identity[B], ab)
    val ab2 = compose(ab, identity[A])

    eq.equal(ab1, ab) && eq.equal(ab2, ab)
  }

and instances, as I have done a decent amount of work around profunctors (Divariant).

But to handle laws using Lawful/Laws/.... I would need some hints on how to approach this.

sideeffffect commented 3 years ago

I actually tried that, but I found the Lawful/Laws/... from ZIO Test insufficient :disappointed: We would need in ZIO Test something like this

package object laws {
  object LawfulF {
    type Divariant[-CapsF[_[-_, +_]], -Caps[_]] = ZLawfulF.Divariant[CapsF, Caps, Any]
  }
  object LawsF {
    type Divariant[-CapsF[_[-_, +_]], -Caps[_]] = ZLawsF.Divariant[CapsF, Caps, Any]

Given that, then we would need to write Derive2 and Derive2Equal and coherent.DivariantDerive2Equal but that should already be simple enough.

sideeffffect commented 3 years ago

ticket in ZIO to implement this: https://github.com/zio/zio/issues/4211

adamgfraser commented 3 years ago

Yes, the downside of the laws framework is that you need a different instance for each kind since we don't really have a way to abstract over kindedness in Scala 2, but that is definitely something we can add!