scala - Immutable Covariant Priority Queues with Higher-kinded Types -
i define different implementations of immutable covariant priority queues. first of all, of implementations provide following interface:
trait pqlike[t, this[_]] { def isempty : boolean def enqueue[e >: t](x : e)(implicit ord : ordering[e]) : this[e] def dequeue[t] : this[t] def first : t }
so this
stands implementation's type constructor , t
type of elements stored in queue. instance, if linearpriorityqueue[t]
implementation of priority queue, want method enqueue
linearpriorityqueue
return linearpriorityqueue
.
i define priorityqueue[t]
trait (or abstract class) implementations of priority queues can typed (in addition specific type) priorityqueue
s.
i tried this:
trait priorityqueue[+t] extends pqlike[t, _ <: pqlike[_,_]] trait linearpriorityqueue[+t] extends priorityqueue[t] pqlike[t,linearpriorityqueue] object epq extends linearpriorityqueue[nothing] { def isempty = true def first = throw new priorityqueueexception("first on empty queue") def dequeue = throw new priorityqueueexception("dequeue on empty queue") def enqueue[t](x : t) = npq(x,this) } case class npq[+t](x : t, q : linearpriorityqueue[t]) extends linearpriorityqueue[t] { def isempty = false def first = x def dequeue = q def enqueue[e >: t](y : e) = npq(y,this) // not real implementation }
but i'm getting type errors related kinds cannot fix.
thanks
edit: ok, have made progress. compiling , working expected, although don't know if solution simplified further (note have done renamings):
trait ispriorityqueue[+t, +repr[+x] <: ispriorityqueue[x,repr]] { def isempty : boolean def first : t def enqueue[e >: t](x : e)(implicit ord : ordering[e]) : repr[e] def dequeue : repr[t] } trait priorityqueue[+t] extends ispriorityqueue[t, priorityqueue] trait linearpriorityqueue[+t] extends priorityqueue[t] ispriorityqueue[t,linearpriorityqueue] object emptylpq extends linearpriorityqueue[nothing] { def isempty = true def first = throw new priorityqueueexception("first on empty queue") def dequeue = throw new priorityqueueexception("dequeue on empty queue") def enqueue[e](x : e)(implicit ord : ordering[e]) = nodelpq(x,this) } case class nodelpq[+t](hd : t, tl : linearpriorityqueue[t]) extends linearpriorityqueue[t] { def isempty = false def first = hd def dequeue = tl def enqueue[e >: t](x : e)(implicit ord : ordering[e]) = if(ord.compare(x,hd)<0) nodelpq(x,this) else nodelpq(hd,tl.enqueue(x)) }
i comments on code , possible simplifications. thinking maybe trait priorityqueue
defined using existential type (a priorityqueue
extends ispriorityqueue
representation) haven't been able express that.
edit: 1 have comments on solution?
Comments
Post a Comment