Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Last active August 29, 2015 14:17
Show Gist options
  • Save gigiigig/dde0e0c6d7b2c7bed35e to your computer and use it in GitHub Desktop.
Save gigiigig/dde0e0c6d7b2c7bed35e to your computer and use it in GitHub Desktop.
Dependent Types experiments
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scalaz._
import Scalaz._
object console extends App {
trait Apart[F]{
type T
type W[X]
def apply(f: F): W[T]
}
object Apart {
def apply[F](implicit apart: Apart[F]) = apart
type Aux[FA, F[_], A] = Apart[FA]{ type T = A; type W[X] = F[X] }
implicit def mk[F[_], A] = new Apart[F[A]]{
type T = A
type W[X] = F[X]
def apply(f: F[A]): W[T] = f
}
}
def mapZero[Thing, F[_], A](thing: Thing)
(implicit apart: Apart.Aux[Thing, F, A],
f: Functor[F],
m: Monoid[A]): F[A] =
f.map(apart(thing))(_ => m.zero)
//val res = Apart[List[Int]].apply(List(1,2,3))
//implicit def a[A]: Apart[List[A]] = Apart.mk[List, A]
implicitly[Apart[List[Int]] =:= Apart.Aux[List[Int], List, Int]]
//val res = mapZero(List[String]("1,2,4"))
//println(res)
// simle test
trait Apart[F] {
type T
def apply(f: F): T
}
object Apart {
def apply[F](implicit a: Apart[F]) = a
implicit def mki[V]: Apart[Option[V]] = new Apart[Option[V]] {
type T = V
def apply(g: Option[V]): V = {
g.get
}
}
}
def foo[X](t: X)(implicit a: Apart[X]): a.T = {
a.apply(t)
}
//val res = foo(Option(3))
val res: String = foo(Option(3))
println(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment