Skip to content

Commit

Permalink
migrate to typelevel discipline
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrh committed Dec 2, 2013
1 parent a8a2224 commit 86098f3
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 200 deletions.
6 changes: 5 additions & 1 deletion project/Build.scala
Expand Up @@ -198,7 +198,11 @@ object MyBuild extends Build {

lazy val scalacheckSettings = Seq(
name := "spire-scalacheck-binding",
libraryDependencies ++= Seq(scalaTest, scalaCheck)
libraryDependencies ++= Seq(
"org.typelevel" %% "discipline" % "0.1",
scalaTest % "test",
scalaCheck
)
)


Expand Down
6 changes: 4 additions & 2 deletions scalacheck-binding/src/main/scala/spire/laws/BaseLaws.scala
Expand Up @@ -3,6 +3,8 @@ package spire.laws
import spire.algebra._
import spire.implicits._

import org.typelevel.discipline.Laws

import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Prop._

Expand All @@ -19,14 +21,14 @@ trait BaseLaws[A] extends Laws {
implicit def Arb: Arbitrary[A]


def signed(implicit A: Signed[A]) = new SimpleProperties(
def signed(implicit A: Signed[A]) = new SimpleRuleSet(
name = "signed",
"abs non-negative" forAll((x: A) =>
x.abs.sign != Sign.Negative
)
)

def metricSpace[R](implicit MSA: MetricSpace[A, R], SR: Signed[R], OR: Order[R], ASR: AdditiveSemigroup[R]) = new SimpleProperties(
def metricSpace[R](implicit MSA: MetricSpace[A, R], SR: Signed[R], OR: Order[R], ASR: AdditiveSemigroup[R]) = new SimpleRuleSet(
name = "metricSpace",
"non-negative" forAll((a1: A, a2: A) =>
MSA.distance(a1, a2).sign != Sign.Negative
Expand Down
6 changes: 4 additions & 2 deletions scalacheck-binding/src/main/scala/spire/laws/GroupLaws.scala
Expand Up @@ -3,6 +3,8 @@ package spire.laws
import spire.algebra._
import spire.implicits._

import org.typelevel.discipline.Laws

import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Prop._

Expand Down Expand Up @@ -92,13 +94,13 @@ trait GroupLaws[A] extends Laws {
name: String,
parent: Option[GroupProperties],
props: (String, Prop)*
) extends DefaultProperties(name, parent, props: _*)
) extends DefaultRuleSet(name, parent, props: _*)

class AdditiveProperties(
val base: GroupProperties,
val parent: Option[AdditiveProperties],
val props: (String, Prop)*
) extends SpireProperties with HasOneParent {
) extends RuleSet with HasOneParent {
val name = base.name
val bases = Seq("base" base)
}
Expand Down
19 changes: 0 additions & 19 deletions scalacheck-binding/src/main/scala/spire/laws/LawChecker.scala

This file was deleted.

138 changes: 0 additions & 138 deletions scalacheck-binding/src/main/scala/spire/laws/Laws.scala

This file was deleted.

27 changes: 0 additions & 27 deletions scalacheck-binding/src/main/scala/spire/laws/Predicate.scala

This file was deleted.

8 changes: 5 additions & 3 deletions scalacheck-binding/src/main/scala/spire/laws/RingLaws.scala
Expand Up @@ -3,6 +3,8 @@ package spire.laws
import spire.algebra._
import spire.implicits._

import org.typelevel.discipline.{Laws, Predicate}

import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop._
Expand Down Expand Up @@ -126,7 +128,7 @@ trait RingLaws[A] extends GroupLaws[A] {
val base: GroupLaws[A] => GroupLaws[A]#GroupProperties,
val parent: Option[MultiplicativeProperties],
val props: (String, Prop)*
) extends SpireProperties with HasOneParent {
) extends RuleSet with HasOneParent {
private val _base = base(RingLaws.this)

val name = _base.name
Expand All @@ -144,12 +146,12 @@ trait RingLaws[A] extends GroupLaws[A] {
val ml: MultiplicativeProperties,
val parents: Seq[RingProperties],
val props: (String, Prop)*
) extends SpireProperties {
) extends RuleSet {
def nonZero: Boolean = false

def _ml =
if (nonZero)
new SpireProperties with HasOneParent {
new RuleSet with HasOneParent {
val name = ml.name
val bases = Seq("base-nonzero" ml.base(nonZeroLaws))
val parent = ml.parent
Expand Down
17 changes: 10 additions & 7 deletions scalacheck-binding/src/main/scala/spire/laws/VectorSpaceLaws.scala
Expand Up @@ -3,6 +3,8 @@ package spire.laws
import spire.algebra._
import spire.implicits._

import org.typelevel.discipline.{Laws, Predicate}

import org.scalacheck.{Arbitrary, Prop}
import org.scalacheck.Prop._

Expand Down Expand Up @@ -55,8 +57,8 @@ trait VectorSpaceLaws[V, A] extends Laws {

def metricSpace(implicit V: MetricSpace[V, A], o: Order[A], A: Rng[A]) = new SpaceProperties(
name = "metric space",
sl = _.emptyProperties,
vl = _.emptyProperties,
sl = _.emptyRuleSet,
vl = _.emptyRuleSet,
parents = Seq.empty,
"identity" forAll((x: V, y: V) =>
if (x === y) V.distance(x, y) === Rng[A].zero
Expand Down Expand Up @@ -87,7 +89,7 @@ trait VectorSpaceLaws[V, A] extends Laws {
)
)

def linearity(f: V => A)(implicit V: Module[V, A]) = new SimpleProperties(
def linearity(f: V => A)(implicit V: Module[V, A]) = new SimpleRuleSet(
name = "linearity",

"homogeneity" forAll((r: A, v: V) =>
Expand All @@ -106,7 +108,8 @@ trait VectorSpaceLaws[V, A] extends Laws {
(v ⋅ w).abs === (w ⋅ v).abs
),
"linearity of partial inner product" forAll((w: V) =>
linearity(_ ⋅ w)
// TODO this probably requires some thought -- should `linearity` be a full `RuleSet`?
linearity(_ ⋅ w).all
)
)

Expand All @@ -117,11 +120,11 @@ trait VectorSpaceLaws[V, A] extends Laws {

class SpaceProperties(
val name: String,
val sl: scalarLaws.type => scalarLaws.SpireProperties,
val vl: vectorLaws.type => vectorLaws.SpireProperties,
val sl: scalarLaws.type => scalarLaws.RuleSet,
val vl: vectorLaws.type => vectorLaws.RuleSet,
val parents: Seq[SpaceProperties],
val props: (String, Prop)*
) extends SpireProperties {
) extends RuleSet {
val bases = Seq("scalar" sl(scalarLaws), "vector" vl(vectorLaws))
}

Expand Down
16 changes: 16 additions & 0 deletions scalacheck-binding/src/main/scala/spire/laws/package.scala
@@ -0,0 +1,16 @@
package spire

import spire.algebra._
import spire.implicits._

import org.typelevel.discipline.Predicate

package object laws {

implicit def PredicateFromMonoid[A: Eq](implicit A: AdditiveMonoid[A]): Predicate[A] = new Predicate[A] {
def apply(a: A) = a =!= A.zero
}

}

// vim: expandtab:ts=2:sw=2
6 changes: 5 additions & 1 deletion scalacheck-binding/src/test/scala/spire/laws/LawTests.scala
Expand Up @@ -10,7 +10,11 @@ import spire.implicits.{

import scala.{ specialized => spec }

class LawTests extends LawChecker {
import org.typelevel.discipline.scalatest.Discipline

import org.scalatest.FunSuite

class LawTests extends FunSuite with Discipline {

import SpireArbitrary._

Expand Down

0 comments on commit 86098f3

Please sign in to comment.