This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Forall3[X[_, _, _]] { | |
def apply[U, V, R]: X[U, V, R] | |
} | |
type ZipWith[A[_]] = { | |
type Apply[U, V, R] = ((U, V) => R) => (A[U], A[V]) => A[R] | |
} | |
type Result[T] = (A[T], A[T], Forall3[ZipWith[A]#Apply]) forSome { type A[_] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//library code | |
//references: http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf | |
abstract class SubjectObserver { | |
type S <: Subject | |
type O <: Observer | |
abstract class Subject { this: S => | |
private var observers: List[O] = List() | |
def subscribe(obs: O) = | |
observers = obs :: observers | |
def publish = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Test extends App { | |
sealed trait Nat | |
case class Zero private (dummy: Zero) extends Nat | |
case class Succ[T <: Nat] private (dummy: Succ[T]) extends Nat | |
sealed abstract class Vec[L <: Nat, +T] | |
case object VNil extends Vec[Zero, Nothing] | |
case class VCons[L <: Nat, +T](head: T, tail: Vec[L, T]) extends Vec[Succ[L], T] | |
def fromList[T, R](l: List[T])(k: (Vec[A, T] forSome {type A <: Nat}) => R): R = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Test extends App { | |
object Vec { | |
def fromList[A](l: List[A]): Vec[_, A] = Vec(l) | |
} | |
case class Vec[Tag1, A] private (private val l: List[A]) { | |
def toList = l | |
def zipWith[Tag2, B, C](v: Vec[Tag2, B])(f: (A, B) => C)(implicit ev: Tag1 =:= Tag2) = | |
Vec[Tag1, C](l.zip(v.l).map{case (a, b) => f(a, b)}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ResumptionMonad extends App { | |
sealed abstract class Resumption[A] { | |
def map[B](k: A => B) = flatMap(k andThen Val.apply) | |
def flatMap[B](k: A => Resumption[B]): Resumption[B] = { | |
def loop(x: Resumption[A]): Resumption[B] = | |
x match { | |
case Val(value) => k(value) | |
case Wrap(value) => Wrap(loop(value)) | |
} | |
loop(this) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object OperationalMonad extends App { | |
class Operational { | |
type instr[A] <: { def run: A } | |
sealed abstract class Program[A] { | |
def map[B](k: A => B) = flatMap(k andThen Lift.apply) | |
def flatMap[B](k: A => Program[B]): Program[B] = { | |
Bind(this, k) | |
} | |
def run: A | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._ | |
import Scalaz._ | |
object FreeMonad extends App { | |
class FreeMonad[F[_]: Monad] { | |
sealed abstract class Free[A] | |
case class Val[A](value: A) extends Free[A] | |
case class Wrap[A](value: F[Free[A]]) extends Free[A] | |
implicit val freeMonad = new Monad[Free] { | |
def pure[A](x: => A) = Val(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package logic | |
object PropAst { | |
type Id = String | |
sealed abstract class Term | |
case class Const(name: Id) extends Term { | |
override def toString = name | |
} | |
case class Var(name: Id) extends Term { | |
override def toString = name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vec(list: List[Double]) { | |
case class Value(list: List[Double]) | |
val value = Value(list) | |
def sameLength(x: Vec#Value): Option[Value] = { | |
if (x.list.length == list.length) Some(Value(x.list)) | |
else None | |
} | |
def innerProd(x: Value): Double = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Seq a = Nil | Cons a (Seq (a,a)) | |
-- 多相再帰の型推論は完全ではないので、 | |
-- このように型アノテーションをコメントアウトすると | |
-- コンパイルできない | |
-- len :: Seq a -> Int | |
len Nil = 0 | |
len (Cons x s) = 1 + 2 * (len s) | |
pair f (x,y) = (f x, f y) |