liveBook · Manning

Property conjunction

As explained in Functional Programming in Scala, Second Edition

The property conjunction operator combines two Prop values so that the combined property represents their conjunction.

Property conjunction combines two Prop values into one Prop. The && operator lets independently stated properties participate in a single composable testing expression. section 8.1.2 section 8.1.3

Combining list properties

val intList = Gen.listOf(Gen.choose(0, 100)) val prop = Prop.forAll(intList)(ns => ns.reverse.reverse == ns) && Prop.forAll(intList)(ns => ns.headOption == ns.reverse.lastOption) The expression states that both generated-list properties must hold. section 8.1.2

A naive implementation cannot combine properties if check only prints a report and returns Unit: running both checks produces independent output and discards the information needed for composition. check must return meaningful success or failure data. section 8.1.3

A useful design progression is:

  • Represent check results with at least a success/failure distinction so conjunction can decide its result. section 8.1.3
  • Consider richer failure information, such as successful-test counts and the arguments that produced failure. section 8.1.3
val intList = Gen.listOf(Gen.choose(0, 100))
val prop =
  Prop.forAll(intList)(ns => ns.reverse.reverse == ns) &&
  Prop.forAll(intList)(ns => ns.headOption == ns.reverse.lastOption)

Inspect a concrete conjunction of two properties over generated integer lists. section 8.1.2

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Descends from an && signature to meaningful check results, but defers a complete Prop representation.
Examples
Uses reversal and head/last agreement properties over generated integer lists.
What it assumes
Assumes generators, predicates, and matching Scala function types while motivating Prop from those types.
What you can do after
Build it from scratch by defining && over a Prop representation that preserves check results.
How it connects
Connects property-based testing with Prop, forAll, check, Gen, Boolean, and Either.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.