Playing with Nat without case classes - scala

I just create a definition in scala for Naturals, and also a PLUS operation:
abstract class Nat {
def +(other:Nat):Nat = this match {
case Zero => other
case Succ(x) => x + Succ(other)
}
}
object Zero extends Nat {
override def toString = "Zero"
}
And for the definition of Succ, i tryit not to use a Case class, for learning purposes. My first approach was:
class Succ(x: Nat) extends Nat {
override def toString = "Succ(" + x.toString + ")"
}
object Succ {
def apply(x: Nat) = new Succ(x)
def unapply(s: Succ) = Some(s.x)
}
But the compiler throws me an error
Error:( , ) value x is not a member of Succ
def unapply(s: Succ) = Some(s.x)
^
I make an explicit method for get X, and it works
class Succ(x: Nat) extends Nat {
def getX = x
override def toString = "Succ(" + x.toString + ")"
}
object Succ {
def apply(x: Nat) = new Succ(x)
def unapply(s: Succ) = Some(s.getX)
}
Why?

Constructor arguments are only visible inside the class. If you want to make it a field, you'll have to say so:
class Succ(val x: Nat) extends Nat { … }
// ^^^

Related

Scala - not a case class nor does it have method .unapply

I am quite new to Scala and got a few unresolved problems with the following code:
object exprs{
println("Welcome to the Scala worksheet")
def show(e: Expr): String = e match {
case Number(x) => x.toString
case Sum(l, r) => show(l) + " + " + show(r)
}
show(Sum(Number(1), Number(44)))
}
trait Expr {
def isNumber: Boolean
def isSum: Boolean
def numValue: Int
def leftOp: Expr
def rightOp: Expr
def eval: Int = this match {
case Number(n) => n
case Sum(e1, e2) => e1.eval + e2.eval
}
}
class Number(n: Int) extends Expr {
override def isNumber: Boolean = true
override def isSum: Boolean = false
override def numValue: Int = n
override def leftOp: Expr = throw new Error("Number.leftOp")
override def rightOp: Expr = throw new Error("Number.rightOp")
}
class Sum(e1: Expr, e2: Expr) extends Expr {
override def isNumber: Boolean = false
override def isSum: Boolean = true
override def numValue: Int = e1.eval + e2.eval
override def leftOp: Expr = e1
override def rightOp: Expr = e2
}
I get the following errors:
Error: object Number is not a case class, nor does it have an unapply/unapplySeq member
Error: not found: value Sum
How to resolve them? Thanks in advance
In Scala case class are like class with extra goodies + some other properties.
For a normal class,
class A(i: Int, s: String)
You can not create its instance like this,
val a = A(5, "five") // this will not work
You will have to use new to create new instance.
val a = new A(5, "five")
Now lets say we have case class,
case class B(i: Int, s: String)
We can create a new instance of B like this,
val b = B(5, "five")
The reason this works with case class is because case class have an auto-created companion objects with them, which provides several utilities including an apply and unapply method.
So, this usage val b = B(5, "five") is actually val b = B.apply(5, "five"). And here B is not the class B but the companion object B which is actually provieds apply method.
Similarly Scala pattern matching uses the unapply (unapplySeq for SeqLike patterns) methods provided by companion object. And hence normal class instances do not work with pattern matching.
Lets say you wanted to defined a class and not a case class for some specific reason but still want to use them with pattern-matching etc, you can provide its companion object with the required methods by yourselves.
class C(val i: Int, val s: String) {
}
object C {
def apply(i: Int, s: String) = new C(i, s)
def unapply(c: C) = Some((c.i, c.s))
}
// now you can use any of the following to create instances,
val c1 = new C(5, "five")
val c2 = C.apply(5, "five")
val c3 = C(5, "five")
// you can also use pattern matching,
c1 match {
case C(i, s) => println(s"C with i = $i and s = $s")
}
c2 match {
case C(i, s) => println(s"C with i = $i and s = $s")
}
Also, as you are new to learning Scala you should read http://danielwestheide.com/scala/neophytes.html which is probably the best resource for any Scala beginner.

Scala - Pattern Matching MatchError

I'm playing with pattern matching, but I have some difficult to understand what's wrong with this piece of code:
class Expr {
case class Number(v : Int) extends Expr
case class Sum(a : Expr, b : Expr) extends Expr
def show(e: Expr): String = {
e match {
case Number(a) => a.toString()
case Sum(a, b) => "(" + show(a) + "+" + show(b) + ")"
}
}
override def toString() = show(this)
}
class Number(v : Int) extends Expr
class Sum(a : Expr, b : Expr) extends Expr
object Number {
def apply(v : Int) = new Number(v)
}
object Sum extends Expr {
def apply(a : Expr, b : Expr) = new Sum(a, b)
}
object ExpressionProblem {
def main(args: Array[String]) {
val p = Sum( Number(3), Number(4))
println( p )
}
}
When I try to execute println it throws a MatchError.
Exception in thread "main" scala.MatchError: an instance of class week4.Expr$Sum
at week4.Expr.show(ExpressionProblem.scala:8)
at week4.Expr.toString(ExpressionProblem.scala:14)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at scala.Console$.println(Console.scala:148)
at scala.Predef$.println(Predef.scala:315)
at week4.ExpressionProblem$.main(ExpressionProblem.scala:31)
at week4.ExpressionProblem.main(ExpressionProblem.scala)
you're sending an instance of your Sum class, but trying to match an instance of Sum case class. Remove classes and make case classes visible to your code:
trait Expr {
def show(e: Expr): String = e match {
case Number(a) => a.toString()
case Sum(a, b) => "(" + show(a) + "+" + show(b) + ")"
}
override def toString() = show(this)
}
case class Number(v : Int) extends Expr
case class Sum(a : Expr, b : Expr) extends Expr
object ExpressionNoProblem {
def main(args: Array[String]) {
val p = Sum(Number(3), Number(4))
println(p)
}
}

How to create object/singleton of generic type in Scala?

In the code shown below, how can I convert EmptyTree to object (Singleton) ?
trait Tree[T] {
def contains(num: T): Boolean
def inc( num: T ): Tree[T]
}
class EmptyTree[T <% Ordered[T] ] extends Tree[T] {
def contains(num:T):Boolean = false
def inc(num:T):Tree[T] = {
new DataTree(num, new EmptyTree, new EmptyTree)
}
override def toString = "."
}
class DataTree[T <% Ordered[T] ](val x:T, val left:Tree[T], val right:Tree[T]) extends Tree[T] {
def contains(num:T):Boolean = {
if( num < x ) left.contains(x)
else if ( num > x ) right.contains(x)
else true
}
def inc(num:T):Tree[T] = {
if(num < x ) new DataTree(x, left.inc(num), right)
else if ( num > x ) new DataTree(x, left, right.inc(num))
else this
}
override def toString = "{" + left + x + right + "}"
}
val t = new DataTree(20, new EmptyTree[Int], new EmptyTree[Int])
//> t : greeting.Test.DataTree[Int] = {.20.}
val p = t.inc(10) //> p : greeting.Test.Tree[Int] = {{.10.}20.}
val a = p.inc(30) //> a : greeting.Test.Tree[Int] = {{.10.}20{.30.}}
val s = a.inc(5) //> s : greeting.Test.Tree[Int] = {{{.5.}10.}20{.30.}}
val m = s.inc(11) //> m : greeting.Test.Tree[Int] = {{{.5.}10{.11.}}20{.30.}}
Let me detalize Alexey's answer. Here is full implementation with some code style improvements:
First define your trait with aknowledgment of its covariance:
trait Tree[+T] {
def contains[U >: T : Ordering](num: U): Boolean
def inc[U >: T : Ordering](num: U): Tree[U]
}
Next define your subtype-of-all-trees object
case object EmptyTree extends Tree[Nothing] {
def contains[U >: Nothing : Ordering](num: U): Boolean = false
def inc[U >: Nothing : Ordering](num: U): Tree[U] =
DataTree(num, EmptyTree, EmptyTree)
override def toString = "."
}
Now change your general case implementation:
case class DataTree[T: Ordering](x: T, left: Tree[T], right: Tree[T]) extends Tree[T] {
import Ordering.Implicits._
def contains[U >: T : Ordering](num: U): Boolean =
if (num < x) left.contains(x)
else if (num > x) right.contains(x)
else true
def inc[U >: T : Ordering](num: U): Tree[U] =
if (num < x) DataTree(x, left.inc(num), right)
else if (num > x) DataTree(x, left, right.inc(num))
else this
override def toString = "{" + left + x + right + "}"
}
You could be a little bit frustrated since I replaced your Ordered with Ordering, but you should know that view bounds are deprecated
You have to fix the generic argument because that's the only time you can provide it:
scala> trait A[T]
defined trait A
scala> object B extends A[Int]
defined object B
Obviously you want to reuse EmptyTree for all types of T, so instead of defining A[SOMETYPE] for each type just use bottom type Nothing:
scala> object B extends A[Nothing]
defined object B
This object can be used with any tree.
That's exactly how Option[T] is implemented in Scala. Here is how None is defined:
case object None extends Option[Nothing]
If keeping generics, also there is an option to add empty factory - like it's done for Map and Vector. Off course, with such an implementation it will not be a unique instance object for every creation, but when using inc method, it will not produce new objects, it will just reference itself.
object DataTree {
def empty[T <% Ordered[T]] = new Tree[T] {
def contains(num: T):Boolean = false
def inc(num: T): Tree[T] = {
new DataTree(num, this, this)
}
override def toString = "."
}
}
So you can instantiate it as following:
val t = new DataTree(20, DataTree.empty[Int], DataTree.empty[Int])

scala's spire framework : I am unable to operate on a group

I try to use spire, a math framework, but I have an error message:
import spire.algebra._
import spire.implicits._
trait AbGroup[A] extends Group[A]
final class Rationnel_Quadratique(val n1: Int = 2)(val coef: (Int, Int)) {
override def toString = {
coef match {
case (c, i) =>
s"$c + $i√$n"
}
}
def a() = coef._1
def b() = coef._2
def n() = n1
}
object Rationnel_Quadratique {
def apply(coef: (Int, Int),n: Int = 2)= {
new Rationnel_Quadratique(n)(coef)
}
}
object AbGroup {
implicit object RQAbGroup extends AbGroup[Rationnel_Quadratique] {
def +(a: Rationnel_Quadratique, b: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique(coef=(a.a() + b.a(), a.b() + b.b()))
def inverse(a: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique((-a.a(), -a.b()))
def id: Rationnel_Quadratique = Rationnel_Quadratique((0, 0))
}
}
object euler66_2 extends App {
val c = Rationnel_Quadratique((1, 2))
val d = Rationnel_Quadratique((3, 4))
val e = c + d
println(e)
}
the program is expected to add 1+2√2 and 3+4√2, but instead I have this error:
could not find implicit value for evidence parameter of type spire.algebra.AdditiveSemigroup[Rationnel_Quadratique]
val e = c + d
^
I think there is something essential I have missed (usage of implicits?)
It looks like you are not using Spire correctly.
Spire already has an AbGroup type, so you should be using that instead of redefining your own. Here's an example using a simple type I created called X.
import spire.implicits._
import spire.algebra._
case class X(n: BigInt)
object X {
implicit object XAbGroup extends AbGroup[X] {
def id: X = X(BigInt(0))
def op(lhs: X, rhs: X): X = X(lhs.n + rhs.n)
def inverse(lhs: X): X = X(-lhs.n)
}
}
def test(a: X, b: X): X = a |+| b
Note that with groups (as well as semigroups and monoids) you'd use |+| rather than +. To get plus, you'll want to define something with an AdditiveSemigroup (e.g. Semiring, or Ring, or Field or something).
You'll also use .inverse and |-| instead of unary and binary - if that makes sense.
Looking at your code, I am also not sure your actual number type is right. What will happen if I want to add two numbers with different values for n?
Anyway, hope this clears things up for you a bit.
EDIT: Since it seems like you're also getting hung up on Scala syntax, let me try to sketch a few designs that might work. First, there's always a more general solution:
import spire.implicits._
import spire.algebra._
import spire.math._
case class RQ(m: Map[Natural, SafeLong]) {
override def toString: String = m.map {
case (k, v) => if (k == 1) s"$v" else s"$v√$k" }.mkString(" + ")
}
object RQ {
implicit def abgroup[R <: Radical](implicit r: R): AbGroup[RQ] =
new AbGroup[RQ] {
def id: RQ = RQ(Map.empty)
def op(lhs: RQ, rhs: RQ): RQ = RQ(lhs.m + rhs.m)
def inverse(lhs: RQ): RQ = RQ(-lhs.m)
}
}
object Test {
def main(args: Array[String]) {
implicit val radical = _2
val x = RQ(Map(Natural(1) -> 1, Natural(2) -> 2))
val y = RQ(Map(Natural(1) -> 3, Natural(2) -> 4))
println(x)
println(y)
println(x |+| y)
}
}
This allows you to add different roots together without problem, at the cost of some indirection. You could also stick more closely to your design with something like this:
import spire.implicits._
import spire.algebra._
abstract class Radical(val n: Int) { override def toString: String = n.toString }
case object _2 extends Radical(2)
case object _3 extends Radical(3)
case class RQ[R <: Radical](a: Int, b: Int)(implicit r: R) {
override def toString: String = s"$a + $b√$r"
}
object RQ {
implicit def abgroup[R <: Radical](implicit r: R): AbGroup[RQ[R]] =
new AbGroup[RQ[R]] {
def id: RQ[R] = RQ[R](0, 0)
def op(lhs: RQ[R], rhs: RQ[R]): RQ[R] = RQ[R](lhs.a + rhs.a, lhs.b + rhs.b)
def inverse(lhs: RQ[R]): RQ[R] = RQ[R](-lhs.a, -lhs.b)
}
}
object Test {
def main(args: Array[String]) {
implicit val radical = _2
val x = RQ[_2.type](1, 2)
val y = RQ[_2.type](3, 4)
println(x)
println(y)
println(x |+| y)
}
}
This approach creates a fake type to represent whatever radical you are using (e.g. √2) and parameterizes QR on that type. This way you can be sure that no one will try to do additions that are invalid.
Hopefully one of these approaches will work for you.

Object orientation in Scala

From Martin Odersky's Scala course I have the following Exercise (this a video exercise in which answer is given):
"
Provide an implementation of the abstract class Nat that represents
non-negative integers
Do not use standard numerical classes in this implementation.
Rather, implement a sub-object and a sub-class.
One for the number zero, the other for strictly prositive numbers.
"
Here is the code :
abstract class Nat {
def isZero : scala.Boolean
def predecessor : Nat
def successor = new Succ(this)
def + (that : Nat) : Nat
def - (that : Nat) : Nat
}
object Zero extends Nat {
def isZero = true
def predecessor = throw new Error("0.predecessor")
def + (that: Nat) = that
def - (that: Nat) = if(that.isZero) this else throw new Error("negative number")
}
class Succ(n : Nat) extends Nat {
def isZero = false
def predecessor = n
def +(that : Nat) = new Succ(n + that)
def -(that: Nat) = n - that.predecessor
}
Within a Scala worksheet I have :
object NatTests {
new Successor(Zero).+(new Successor(Zero))
}
Which returns a new Sucessor. I don't think I'm understanding this code fully as I should be able to add non zero objects without extending the code ? If so, how is this accomplished ?
You are able to add non zero numbers / objects without extending any of the classes Nat, Zero, or Succ. When you use an object natObj of type Nat and construct a new object new Succ(natObject) that new object represents a number that is one higher than the number that natObj represents.
Maybe being able to view the objects, makes this a bit clearer:
abstract class Nat {
def isZero : Boolean
def predecessor : Nat
def successor = new Succ(this)
def + (that : Nat) : Nat
def - (that : Nat) : Nat
}
object Zero extends Nat {
def isZero = true
def predecessor = throw new Error("0.predecessor")
def + (that: Nat) = that
def - (that: Nat) = if(that.isZero) this else throw new Error("negative number")
override def toString = "0 => Zero"
}
class Succ(n : Nat) extends Nat {
def isZero = false
def predecessor = n
def + (that : Nat) = new Succ(n + that)
def - (that: Nat) = if (that.isZero) this else n - that.predecessor
override def toString = {
def findNumber(nat: Nat): Int =
if (nat.isZero) 0
else 1 + findNumber(nat.predecessor)
val number = findNumber(this)
String.valueOf(number) + " => " +
((1 to number) fold ("Zero")) ( (s,_) => "Succ(" + s + ")")
}
}
Now your Scala worksheet will show you the number an object represents and its internal structure:
object NatTests extends App {
val nat0 = Zero
val nat1 = new Succ(Zero)
val nat2 = new Succ(nat1) // or new Succ(new Succ(Zero))
val nat3 = new Succ(nat2) // or new Succ(new Succ(new Succ(Zero)))
println(nat0) //> 0 => Zero
println(nat1) //> 1 => Succ(Zero)
println(nat2) //> 2 => Succ(Succ(Zero))
println(nat3) //> 3 => Succ(Succ(Succ(Zero)))
println(nat2 + nat2) //> 4 => Succ(Succ(Succ(Succ(Zero))))
println(nat3 + nat2) //> 5 => Succ(Succ(Succ(Succ(Succ(Zero)))))
}
You're most likely expected to implement them recursively
I'm not sure of the exact syntax, but it'd be something like this
def +(that : Nat) = (this.predecessor + that.successor)
def -(that: Nat) = if (that.isZero) this else (this.predecessor - that.predecessor)
Also since your objects are immutable, there's no real reason to create a new one each time.
That's how I would write it:
sealed trait Nat {
def isZero : scala.Boolean
def predecessor : Nat
def successor = Succ(this)
def + (that : Nat) : Nat
def - (that : Nat) : Nat =
if (that.isZero) this else this.predecessor - that.predecessor
}
case object Zero extends Nat {
def isZero = true
def predecessor = sys.error("predecessor of zero")
def + (that: Nat) = that
}
case class Succ(predecessor : Nat) extends Nat {
def isZero = false
def +(that : Nat) = this.predecessor + that.successor
}