« Return to Thread: [scala] higher-kinded types

[scala] Re: higher-kinded types

by Meredith Gregory :: Rate this Message:

Reply to Author | View in Thread

All,

The following code works without going through the M-P-O construction. It enjoys approximately the same brevity as a Haskell type class.

// smallest expression of monad i can find
trait MBrace[C[_] <: MBrace[C,A],A] {
  def nest( a : A ) : C[A]
  def flatten[T <: C[C[A]]]( bsq : T ) : C[A]
}

// one of the simplest witnesses of monad i can find
class MBraceSeq[A]( a_ : A* ) extends Seq[A] with MBrace[MBraceSeq,A] {
  override def nest( a : A ) = new MBraceSeq[A]( a )
  override def flatten[T <: MBraceSeq[MBraceSeq[A]]]( bsq : T ) : MBraceSeq[A] = {
    (new MBraceSeq[A]( ) /: bsq)( {
      ( acc : MBraceSeq[A], e : MBraceSeq[A] ) => ( acc ++ e ).asInstanceOf[MBraceSeq[A]]
    } )
  }
  override def length = a_.length
  override def elements = a_.elements
  override def apply( n : Int ) = a_.apply( n )
}

Best wishes,

--greg

On Wed, Jun 24, 2009 at 3:49 PM, Meredith Gregory <lgreg.meredith@...> wrote:
All,

Am i correct in concluding that the solution in The Moors-Piessens-Odersky paper only works with collections that have been clever enough to have used type members rather that type parameters? Or, is there a trick to making the vast majority of the collections types that are parametrically typed look as if they have type members? (See example below.)

Best wishes,

--greg

// Paraphrasing the basic Moors-Piessens-Odersky construction
trait TypeCtor1 { type E }
trait Brace[A] extends TypeCtor1 {
  type C <: TypeCtor1
  def nest( a : A ) : C{type E = A}
  def flatten( bsq : C{type E=C{type E=A}} ) : C{type E=A}
}

// Now, how to make a version of BraceList since List is parametrically typed?


--
L.G. Meredith
Managing Partner
Biosimilarity LLC
1219 NW 83rd St
Seattle, WA 98117

+1 206.650.3740

http://biosimilarity.blogspot.com



--
L.G. Meredith
Managing Partner
Biosimilarity LLC
1219 NW 83rd St
Seattle, WA 98117

+1 206.650.3740

http://biosimilarity.blogspot.com

 « Return to Thread: [scala] higher-kinded types