juliet
juliet

Reputation: 193

Modelling of C++ concepts with Scala traits

There are some examples of using Scala traits like C++ concepts and Haskell type classes in paper «Type Classes as Objects and Implicits». I try to write something like InputIterator concept and find function in Scala:

concept InputIterator<typename Iter> {
  typename value_type;
  value_type operator*(Iter);
  ...
};

template<typename Iter, typename V>
requires InputIterator<Iter> && EqualityComparable<Iter::value_type, V>
Iter find(Iter first, Iter last, V v) { 
    while (first < last && *first != v)
        ++first;
    return first;
}

I'm not sure I understand traits correctly. But still... There is InputIterator trait written in Scala (or more exactly — it's simplified analogue with methods used in find function):

trait InputIterator[Iter] {
    type value_type

    def <(a: Iter, b: Iter): Boolean
    def ++(it: Iter): Unit
    def *(it: Iter): value_type
}

EqualityComparable is clear:

trait EqualityComparable[S, T] {
    def ==(s: S, t: T): Boolean
    def !=(s: S, t: T): Boolean = !(s == t)
}

But what should we do with find? I would like to write something like this:

def find[Iter, V](first: Iter, last: Iter, x: V)(implicit iterator: InputIterator[Iter], 
    cmp: EqualityComparable[iterator.value_type, V]): Iter =
{
    while (iterator.<(first, last) && cmp.!=(iterator.*(first), x))
        iterator.++(first)
    first
}

But it causes an error «illegal dependent method type». And I don't know how to «extract» abstract type value_type other way. So as a result I have got this code:

trait EqualityComparable[S, T] {
    def ==(s: S, t: T): Boolean
    def !=(s: S, t: T): Boolean = !(s == t)
}

trait InputIterator[Iter] {
    type value_type

    def <(a: Iter, b: Iter): Boolean
    def ++(it: Iter): Unit
    def *(it: Iter): value_type
}

trait VTInputIterator[Iter, VT] extends InputIterator[Iter] {
    type value_type = VT
}

class ArrayListIterator[T](a: ArrayList[T], i: Int) {
    val arr: ArrayList[T] = a
    var ind: Int = i

    def curr(): T = arr.get(ind)
    def ++(): Unit = { ind += 1 }

    override def toString() = "[" + ind.toString() + "]"
}

class InputIterArrList[T] extends VTInputIterator[ArrayListIterator[T], T]{ 
    def <(a: ArrayListIterator[T], b: ArrayListIterator[T]) = {
      if (a.arr == b.arr) a.ind < b.ind
      else throw new IllegalArgumentException()
    }

    def ++(it: ArrayListIterator[T]): Unit = it.++()
    def *(it: ArrayListIterator[T]) = it.curr()
}

object TestInputIterator extends Application{   

    def find[Iter, VT, V](first: Iter, last: Iter, x: V)(implicit iterator: VTInputIterator[Iter, VT], 
        cmp: EqualityComparable[VT, V]): Iter = 
    {
      while (iterator.<(first, last) && cmp.!=(iterator.*(first), x))
        iterator.++(first)
      first
    }

    implicit object EqIntInt extends EqualityComparable[Int, Int] {
      def ==(a: Int, b: Int): Boolean = { a == b }
    }

    implicit object inputIterArrListInt extends InputIterArrList[Int]{}

    val len = 10;
    var arr: ArrayList[Int] = new ArrayList(len);
    for (i: Int <- 1 to len)
      arr.add(i)
    var arrFirst = new ArrayListIterator(arr, 0)
    var arrLast = new ArrayListIterator(arr, len)
    var r = find(arrFirst, arrLast, 7)
    println(r)
}

Instead of abstract type we used type parameter VT in def find[Iter, VT, V].

So the question is: how it can be done better? And is it possible to use abstract type value_type without additional type parameter VT?

Upvotes: 4

Views: 344

Answers (1)

Jesper Nordenberg
Jesper Nordenberg

Reputation: 2104

Change the signature of find to:

def find[Iter, V, II <: InputIterator[Iter]](first: Iter, last: Iter, x: V)(
  implicit iterator: II, cmp: EqualityComparable[II#value_type, V]): Iter

That's probably what you want to express.

Note that your Scala code is really not the same as the C++ code. In C++ find uses Iter::value_type but in Scala you use InputIterator[Iter]#value_type.

Upvotes: 4

Related Questions