Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jul 5, 2014
1 parent e12515a commit 3833aec
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/main/scala/remorse/Sequence.scala
Expand Up @@ -34,11 +34,11 @@ trait Sequence[S[_]] {
}
}

sealed trait ViewL[S[_], A]
case class EmptyL[S[_], A]() extends ViewL[S, A]
case class OnL[S[_], A](h: A, t: S[A]) extends ViewL[S, A]
sealed abstract class ViewL[S[_], A]
final case class EmptyL[S[_], A]() extends ViewL[S, A]
final case class OnL[S[_], A](h: A, t: S[A]) extends ViewL[S, A]

sealed trait ViewR[S[_], A]
case class EmptyR[S[_], A]() extends ViewR[S, A]
case class OnR[S[_], A](p: S[A], l: A) extends ViewR[S, A]
sealed abstract class ViewR[S[_], A]
final case class EmptyR[S[_], A]() extends ViewR[S, A]
final case class OnR[S[_], A](p: S[A], l: A) extends ViewR[S, A]

37 changes: 25 additions & 12 deletions src/main/scala/remorse/TConsList.scala
@@ -1,23 +1,36 @@
package remorse

sealed abstract class TConsList[C[_, _], X, Y]

object TConsList extends TConsListInstances {
}

sealed trait TConsList[C[_, _], X, Y]
case class CNil[C[_, _], X]() extends TConsList[C, X, X]
case class Cons[C[_, _], X, Y, Z](l: C[X, Y], r: TConsList[C, Y, Z]) extends TConsList[C, X, Z]
final case class CNil[C[_, _], X]() extends TConsList[C, X, X]
abstract case class Cons[C[_, _], X, Y, Z]() extends TConsList[C, X, Z] {
type Y
def l: C[X, Y]
def r: TConsList[C, Y, Z]
}

def cons[C0[_, _], X0, Y0, Z0](l0: C0[X0, Y0], r0: TConsList[C0, Y0, Z0]): TConsList[C0, X0, Z0] =
new Cons[C0, X0, Y0, Z0] {
type Y = Y0
def l = l0
def r = r0
}
}

trait TConsListInstances {
sealed abstract class TConsListInstances {
import TConsList._

def tsequence: TSequence[TConsList] =
val tsequence: TSequence[TConsList] =
new TSequence[TConsList] {
def empty[C[_, _], X]: TConsList[C, X, X] = CNil()
def singleton[C[_, _], X, Y](c: C[X, Y]): TConsList[C, X, Y] = Cons(c, CNil())
def addL[C[_, _], X, Y, Z](l: C[X, Y], r: TConsList[C, Y, Z]): TConsList[C, X, Z] = Cons(l, r)
def viewL[C[_, _], X, Y](l: TConsList[C, X, Y]): TViewL[TConsList, C, X, Y] =
override def empty[C[_, _], X]: TConsList[C, X, X] = CNil()
override def singleton[C[_, _], X, Y](c: C[X, Y]): TConsList[C, X, Y] = cons(c, CNil())
override def addL[C[_, _], X, Y, Z](l: C[X, Y], r: TConsList[C, Y, Z]): TConsList[C, X, Z] = cons(l, r)
override def viewL[C[_, _], X, Y](l: TConsList[C, X, Y]) = //: TViewL[TConsList, C, X, Y] =
l match {
case CNil() => TEmptyL()
case Cons(h, t) => TOnL(h, t)
case CNil() => TViewL.tEmptyL
case c @ Cons() => TViewL.tOnL(c.l, c.r)
}
}
}
76 changes: 58 additions & 18 deletions src/main/scala/remorse/TSequence.scala
@@ -1,53 +1,93 @@
package remorse

import scalaz._
import TViewR._, TViewL._

object TSequence extends TSequenceInstances {
}

trait TSequence[S[_[_, _], _, _]] {
abstract class TSequence[S[_[_, _], _, _]] {

def empty[C[_, _], X]: S[C, X, X]

def singleton[C[_, _], X, Y](a: C[X, Y]): S[C, X, Y]

def compose[C[_, _], X, Y, Z](l: S[C, X, Y], r: S[C, Y, Z]): S[C, X, Z] = viewL(l) match {
case TEmptyL() => r
case TOnL(h, t) => addL(h, compose(t, r))
case a @ TOnL() => addL(a.h, compose(a.t, r))
}

def addL[C[_, _], X, Y, Z](l: C[X, Y], r: S[C, Y, Z]): S[C, X, Z] = compose(singleton(l), r)

def addR[C[_, _], X, Y, Z](l: S[C, X, Y], r: C[Y, Z]): S[C, X, Z] = compose(l, singleton(r))

def viewL[C[_, _], X, Y](q: S[C, X, Y]): TViewL[S, C, X, Y] = viewR(q) match {
case TEmptyR() => TEmptyL()
case TOnR(p, l) => viewL(p) match {
case TEmptyL() => TOnL(l, empty)
case TOnL(h, t) => TOnL(h, addR(t, l))
case TEmptyR() => tEmptyL
case r @ TOnR() => viewL(r.p) match {
case TEmptyL() => TViewL.tOnL(r.l, empty)
case l @ TOnL() => TViewL.tOnL(l.h, addR(l.t, r.l))
}
}

def viewR[C[_, _], X, Y](q: S[C, X, Y]): TViewR[S, C, X, Y] = viewL(q) match {
case TEmptyL() => TEmptyR()
case TOnL(h, t) => viewR(t) match {
case TEmptyR() => TOnR(empty, h)
case TOnR(p, l) => TOnR(addL(h, p), l)
case TEmptyL() => tEmptyR
case l @ TOnL() => viewR(l.t) match {
case TEmptyR() => TViewR.tOnR(empty[C, X], l.h)
case r @ TOnR() => TViewR.tOnR(addL(l.h, r.p), r.l)
}
}
}

sealed trait TViewL[S[_[_, _], _, _], C[_, _], X, Y]
case class TEmptyL[S[_[_, _], _, _], C[_, _], X]() extends TViewL[S, C, X, X]
case class TOnL[S[_[_, _], _, _], C[_, _], X, Y, Z](h: C[X, Y], t: S[C, Y, Z]) extends TViewL[S, C, X, Z]
sealed abstract class TViewL[S[_[_, _], _, _], C[_, _], X, Y]

sealed trait TViewR[S[_[_, _], _, _], C[_, _], X, Y]
case class TEmptyR[S[_[_, _], _, _], C[_, _], X]() extends TViewR[S, C, X, X]
case class TOnR[S[_[_, _], _, _], C[_, _], X, Y, Z](p: S[C, X, Y], l: C[Y, Z]) extends TViewR[S, C, X, Z]
object TViewL {

trait TSequenceInstances {
final case class TEmptyL[S[_[_, _], _, _], C[_, _], X]() extends TViewL[S, C, X, X]

def category[S[_[_, _], _, _], C[_, _]](implicit S0: TSequence[S]): Category[({ type f[x, y] = S[C, x, y]})#f] =
def tEmptyL[S[_[_, _], _, _], C[_, _], X]: TViewL[S, C, X, X] =
TEmptyL[S, C, X]

abstract case class TOnL[S[_[_, _], _, _], C[_, _], X, Z]() extends TViewL[S, C, X, Z] {
type Y
def h: C[X, Y]
def t: S[C, Y, Z]
}

def tOnL[S0[_[_, _], _, _], C0[_, _], X0, Y0, Z0](h0: C0[X0, Y0], t0: S0[C0, Y0, Z0]): TViewL[S0, C0, X0, Z0] =
new TOnL[S0, C0, X0, Z0] {
type Y = Y0
def h = h0
def t = t0
}
}

sealed abstract class TViewR[S[_[_, _], _, _], C[_, _], X, Y]

object TViewR {

final case class TEmptyR[S[_[_, _], _, _], C[_, _], X]() extends TViewR[S, C, X, X]

def tEmptyR[S[_[_, _], _, _], C[_, _], X]: TViewR[S, C, X, X] =
TEmptyR[S, C, X]

abstract case class TOnR[S[_[_, _], _, _], C[_, _], X, Z]() extends TViewR[S, C, X, Z] {
type Y
def p: S[C, X, Y]
def l: C[Y, Z]
}

def tOnR[S0[_[_, _], _, _], C0[_, _], X0, Y0, Z0](p0: S0[C0, X0, Y0], l0: C0[Y0, Z0]): TViewR[S0, C0, X0, Z0] =
new TOnR[S0, C0, X0, Z0] {
type Y = Y0
def p = p0
def l = l0
}

}

sealed abstract class TSequenceInstances {

implicit def category[S[_[_, _], _, _], C[_, _]](implicit S0: TSequence[S]): Category[({ type f[x, y] = S[C, x, y]})#f] =
new Category[({ type f[x, y] = S[C, x, y]})#f] {
def id[X]: S[C, X, X] = S0.empty
def compose[X, Y, Z](r: S[C, Y, Z], l: S[C, X, Y]): S[C, X, Z] = S0.compose(l, r)
Expand Down

0 comments on commit 3833aec

Please sign in to comment.