typelevel / algebra

Experimental project to lay out basic algebra type classes
https://typelevel.org/algebra/
Other
378 stars 69 forks source link

Implicit Wildcard Import? #145

Closed kevinmeredith closed 8 years ago

kevinmeredith commented 8 years ago

Given:

$cat build.sbt
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies += "org.spire-math" %% "algebra" % "0.3.1"
scalaVersion := "2.11.8"

I tried to compare two Int's via Eq.eqv(Int, Int):

$sbt console
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51).
Type in expressions for evaluation. Or try :help.

scala> import algebra._
import algebra._

scala> Eq.eqv(1, 1)
<console>:15: error: could not find implicit value for parameter ev: algebra.Eq[Int]
       Eq.eqv(1, 1)
             ^

So, I git clone'd this project. Then I attempted to find implicit Eq instances for Int via grep:

$pwd
/Users/kevin/Workspace/Personal/algebra
$git branch
* master
$grep -r implicit . | grep -v test | grep Eq | grep -i int
./project/Boilerplate.scala:        -  implicit def tuple${arity}Eq[${`A..N`}](implicit ${constraints("Eq")}): Eq[${`(A..N)`}] =
./ring/src/main/scala/algebra/ring/Additive.scala:  def isZero[@sp(Int, Long, Float, Double) A](a: A)(implicit ev0: M[A], ev1: Eq[A]): Boolean =
./ring/src/main/scala/algebra/ring/Multiplicative.scala:  def isOne[@sp(Int, Long, Float, Double) A](a: A)(implicit ev0: M[A], ev1: Eq[A]): Boolean =

Could you please tell me how an implicit Eq[Int] is resolved? Perhaps there's a import algebra..x.y._ wildcard that's recommended?

Thank you

adelbertc commented 8 years ago

This should work:

scala> import algebra._
import algebra._

scala> import algebra.std.int._
import algebra.std.int._

scala> Eq[Int]
res0: algebra.Eq[Int] = algebra.std.IntAlgebra@38a93034

scala> Eq.eqv(1, 1)
res1: Boolean = true

scala> Eq[Int].eqv(1, 1)
res2: Boolean = true
kevinmeredith commented 8 years ago

Thank you, @adelbertc.

kevinmeredith commented 8 years ago

Actually, I re-ran this example, with your help, @adelbertc, using the project's source:

$pwd
/Users/Kevin/Workspace/Personal/algebra
$git branch
* master
$git log | head
commit 23bf1748dfc5d5986aafa368f49597eb264b6cef
Merge: 3028114 e8f5c14
Author: Tom Switzer <thomas.switzer@gmail.com>
Date:   Wed Dec 9 17:37:47 2015 -0500

    Merge pull request #132 from rklaehn/feature/array-instances

    Add array instances

commit 30281148e40a33322c2e2326baabcd77546a8e95
$sbt console

scala> import algebra._
import algebra._

scala> import algebra.std._
import algebra.std._

However, with the following set-up, i.e. referencing algebra as a dependency, I ran into the same, above problem:

$pwd
/Users/Kevin/Workspace/Personal/algebra
$cd ../algebra_sandbox/
$cat build.sbt
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies += "org.spire-math" %% "algebra" % "0.3.1"
scalaVersion := "2.11.8"
$sbt console
scala> import algebra._
import algebra._

scala> import algebra.std._
<console>:14: error: object std is not a member of package algebra
       import algebra.std._
                      ^