Skip to content
This repository has been archived by the owner on Feb 2, 2025. It is now read-only.

Commit

Permalink
re-adding composeModify laws
Browse files Browse the repository at this point in the history
  • Loading branch information
Kantis committed Dec 7, 2024
1 parent 3226a91 commit c3a3c3f
Showing 7 changed files with 59 additions and 56 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-c
arrow-fx-coroutines = { module = "io.arrow-kt:arrow-fx-coroutines", version.ref = "arrow" }
arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
arrow-optics = { module = "io.arrow-kt:arrow-optics", version.ref = "arrow" }
arrow-functions = { module = "io.arrow-kt:arrow-functions", version.ref = "arrow" }

# Gradle plugins used in buildSrc
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1 change: 1 addition & 0 deletions kotest-property-arrow-optics/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ kotlin {
commonMain {
dependencies {
compileOnly(libs.arrow.optics)
implementation(libs.arrow.functions)
implementation(libs.kotest.assertions.core)
implementation(libs.kotest.framework.api)
implementation(libs.kotest.property)
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

package io.kotest.property.arrow.optics

//import arrow.core.compose
import arrow.core.compose
import arrow.core.identity
import arrow.optics.Iso
import io.kotest.property.Arb
@@ -25,7 +25,7 @@ public object IsoLaws {
Law("Iso Law: round trip one way") { iso.roundTripOneWay(aGen, eqa) },
Law("Iso Law: round trip other way") { iso.roundTripOtherWay(bGen, eqb) },
Law("Iso Law: modify identity is identity") { iso.modifyIdentity(aGen, eqa) },
// Law("Iso Law: compose modify") { iso.composeModify(aGen, funcGen, eqa) },
Law("Iso Law: compose modify") { iso.composeModify(aGen, funcGen, eqa) },
Law("Iso Law: consitent set with modify") { iso.consistentSetModify(aGen, bGen, eqa) }
)

@@ -44,11 +44,11 @@ public object IsoLaws {
modify(a, ::identity).equalUnderTheLaw(a, eq)
}

// public suspend fun <A, B> Iso<A, B>.composeModify(aGen: Arb<A>, funcGen: Arb<(B) -> B>, eq: (A, A) -> Boolean): PropertyContext =
// checkAll(aGen, funcGen, funcGen) { a, f, g ->
// modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
// }
//
public suspend fun <A, B> Iso<A, B>.composeModify(aGen: Arb<A>, funcGen: Arb<(B) -> B>, eq: (A, A) -> Boolean): PropertyContext =
checkAll(aGen, funcGen, funcGen) { a, f, g ->
modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
}

public suspend fun <A, B> Iso<A, B>.consistentSetModify(aGen: Arb<A>, bGen: Arb<B>, eq: (A, A) -> Boolean): PropertyContext =
checkAll(aGen, bGen) { a, b ->
set(b).equalUnderTheLaw(modify(a) { b }, eq)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

package io.kotest.property.arrow.optics

import arrow.core.compose
import arrow.core.identity
import arrow.optics.Lens
import io.kotest.property.Arb
@@ -26,7 +27,7 @@ public object LensLaws {
Law("Lens law: set get") { lensSetGet(lensGen, aGen, bGen, eqb) },
Law("Lens law: is set idempotent") { lensSetIdempotent(lensGen, aGen, bGen, eqa) },
Law("Lens law: modify identity") { lensModifyIdentity(lensGen, aGen, eqa) },
// Law("Lens law: compose modify") { lensComposeModify(lensGen, aGen, funcGen, eqa) },
Law("Lens law: compose modify") { lensComposeModify(lensGen, aGen, funcGen, eqa) },
Law("Lens law: consistent set modify") { lensConsistentSetModify(lensGen, aGen, bGen, eqa) }
)

@@ -84,18 +85,18 @@ public object LensLaws {
}
}

// public suspend fun <A, B> lensComposeModify(
// lensGen: Arb<Lens<A, B>>,
// aGen: Arb<A>,
// funcGen: Arb<(B) -> B>,
// eq: (A, A) -> Boolean
// ): PropertyContext =
// checkAll(lensGen, aGen, funcGen, funcGen) { lens, a, f, g ->
// lens.run {
// modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
// }
// }
//
public suspend fun <A, B> lensComposeModify(
lensGen: Arb<Lens<A, B>>,
aGen: Arb<A>,
funcGen: Arb<(B) -> B>,
eq: (A, A) -> Boolean
): PropertyContext =
checkAll(lensGen, aGen, funcGen, funcGen) { lens, a, f, g ->
lens.run {
modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
}
}

public suspend fun <A, B> lensConsistentSetModify(
lensGen: Arb<Lens<A, B>>,
aGen: Arb<A>,
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

package io.kotest.property.arrow.optics

import arrow.core.compose
import arrow.core.identity
import arrow.optics.Optional
import io.kotest.property.Arb
@@ -25,7 +26,7 @@ public object OptionalLaws {
Law("Optional Law: set what you get") { setGetOption(optionalGen, aGen, bGen, eqb) },
Law("Optional Law: set is idempotent") { setIdempotent(optionalGen, aGen, bGen, eqa) },
Law("Optional Law: modify identity = identity") { modifyIdentity(optionalGen, aGen, eqa) },
// Law("Optional Law: compose modify") { composeModify(optionalGen, aGen, funcGen, eqa) },
Law("Optional Law: compose modify") { composeModify(optionalGen, aGen, funcGen, eqa) },
Law("Optional Law: consistent set with modify") { consistentSetModify(optionalGen, aGen, bGen, eqa) }
)

@@ -88,18 +89,18 @@ public object OptionalLaws {
}
}

// public suspend fun <A, B> composeModify(
// optionalGen: Arb<Optional<A, B>>,
// aGen: Arb<A>,
// funcGen: Arb<(B) -> B>,
// eq: (A, A) -> Boolean
// ): PropertyContext =
// checkAll(optionalGen, aGen, funcGen, funcGen) { optional, a, f, g ->
// optional.run {
// modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
// }
// }
//
public suspend fun <A, B> composeModify(
optionalGen: Arb<Optional<A, B>>,
aGen: Arb<A>,
funcGen: Arb<(B) -> B>,
eq: (A, A) -> Boolean
): PropertyContext =
checkAll(optionalGen, aGen, funcGen, funcGen) { optional, a, f, g ->
optional.run {
modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
}
}

public suspend fun <A, B> consistentSetModify(
optionalGen: Arb<Optional<A, B>>,
aGen: Arb<A>,
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

package io.kotest.property.arrow.optics

import arrow.core.compose
import arrow.core.identity
import arrow.optics.Prism
import io.kotest.property.Arb
@@ -23,7 +24,7 @@ public object PrismLaws {
Law("Prism law: partial round trip one way") { prism.partialRoundTripOneWay(aGen, eqa) },
Law("Prism law: round trip other way") { prism.roundTripOtherWay(bGen, eqb) },
Law("Prism law: modify identity") { prism.modifyIdentity(aGen, eqa) },
// Law("Prism law: compose modify") { prism.composeModify(aGen, funcGen, eqa) },
Law("Prism law: compose modify") { prism.composeModify(aGen, funcGen, eqa) },
Law("Prism law: consistent set modify") { prism.consistentSetModify(aGen, bGen, eqa) }
)

@@ -44,10 +45,10 @@ public object PrismLaws {
modify(a, ::identity).equalUnderTheLaw(a, eq)
}

// public suspend fun <A, B> Prism<A, B>.composeModify(aGen: Arb<A>, funcGen: Arb<(B) -> B>, eq: (A, A) -> Boolean): PropertyContext =
// checkAll(aGen, funcGen, funcGen) { a, f, g ->
// modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
// }
public suspend fun <A, B> Prism<A, B>.composeModify(aGen: Arb<A>, funcGen: Arb<(B) -> B>, eq: (A, A) -> Boolean): PropertyContext =
checkAll(aGen, funcGen, funcGen) { a, f, g ->
modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), eq)
}

public suspend fun <A, B> Prism<A, B>.consistentSetModify(aGen: Arb<A>, bGen: Arb<B>, eq: (A, A) -> Boolean): PropertyContext =
checkAll(aGen, bGen) { a, b ->
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

package io.kotest.property.arrow.optics

//import arrow.core.compose
import arrow.core.compose
import arrow.core.identity
import arrow.optics.Traversal
import io.kotest.property.Arb
@@ -23,8 +23,7 @@ public object TraversalLaws {
): List<Law> = listOf(
Law("Traversal law: set is idempotent") { traversal.setIdempotent(aGen, bGen, eq) },
Law("Traversal law: modify identity") { traversal.modifyIdentity(aGen, eq) },
// TODO: Was there any replacement for `compose` in Arrow 2?
// Law("Traversal law: compose modify") { traversal.composeModify(aGen, funcGen, eq) }
Law("Traversal law: compose modify") { traversal.composeModify(aGen, funcGen, eq) }
)

public suspend fun <A, B> Traversal<A, B>.setIdempotent(aGen: Arb<A>, bGen: Arb<B>, eq: (A, A) -> Boolean): PropertyContext =
@@ -38,19 +37,18 @@ public object TraversalLaws {
modify(a, ::identity).equalUnderTheLaw(a, eq)
}

// TODO: Was there any replacement for `compose` in Arrow 2?
// public suspend fun <A, B> Traversal<A, B>.composeModify(
// aGen: Arb<A>,
// funcGen: Arb<(B) -> B>,
// eq: (A, A) -> Boolean
// ): PropertyContext =
// checkAll(
// max(max(aGen.minIterations(), funcGen.minIterations()), funcGen.minIterations()),
// aGen,
// funcGen,
// funcGen
// ) { a, f, g ->
// modify(modify(a, f), g)
// .equalUnderTheLaw(modify(a, g compose f), eq)
// }
public suspend fun <A, B> Traversal<A, B>.composeModify(
aGen: Arb<A>,
funcGen: Arb<(B) -> B>,
eq: (A, A) -> Boolean
): PropertyContext =
checkAll(
max(max(aGen.minIterations(), funcGen.minIterations()), funcGen.minIterations()),
aGen,
funcGen,
funcGen
) { a, f, g ->
modify(modify(a, f), g)
.equalUnderTheLaw(modify(a, g compose f), eq)
}
}

0 comments on commit c3a3c3f

Please sign in to comment.