Skip to content

Instantly share code, notes, and snippets.

View mandubian's full-sized avatar

Pascal Voitot mandubian

View GitHub Profile
trait X[A <: AnyKind, F[_ <: AnyKind]] { type B = F[A] }
// OK
val i5: X[Option, ({ type l[X[_]] = X[Int] })#l]#B = Some(5)
/////////////////////////////////////////////////////////////
// KO
val i1: X[Option, List]#B = Some(5)
// Error Before Patch
trait Monoid[M <: AnyKind] {
type ->[_<:AnyKind, _<:AnyKind]
type I <: AnyKind
def unit: ->[I, M]
def mult(a: I -> M, b: I -> M): I -> M
}
object Monoid {
type Aux[M <: AnyKind, M0[_<:AnyKind, _<:AnyKind], I0 <: AnyKind] = Monoid[M] { type ->[a <: AnyKind, b <: AnyKind] = M0[a, b]; type I = I0 }
@mandubian
mandubian / multi-blocks.scala
Last active January 26, 2017 07:18
Function definition with multiple implicit blocks & Interleaving
object Test extends App {
implicit val b: String = "toto"
implicit val c: Float = 1.0F
trait Foo[A] {
type B
def AtoB(a: A): B
}
@mandubian
mandubian / cat_functor_rec.scala
Last active February 14, 2017 20:24
Implementation of Kind-Polymorphism Category, Functor & Rec for generic (un)folding
// WARNING... NOT FOR THE FAINT HEARTED
// Scala Conversion of haskell code in http://blog.functorial.com/posts/2012-02-02-Polykinded-Folds.html
// to study more samples of kind polymorphism in Scala
//
// It provides the implementation of Kind-Polymorphism Category, Functor & Rec for generic folding
// HASKELL CODE
// class Category hom where
// ident :: hom a a
// compose :: hom a b -> hom b c -> hom a c
@mandubian
mandubian / kp-monoid.scala
Last active September 30, 2019 21:39
"Monad is a monoid in the category of endofunctors" evidence using trivial sample of Scala Kind-Polymorphism PoC
/** Showing with Kind-Polymorphism the evidence that Monad is a monoid in the category of endofunctors */
object Test extends App {
/** Monoid (https://en.wikipedia.org/wiki/Monoid_(category_theory))
* In category theory, a monoid (or monoid object) (M, μ, η) in a monoidal category (C, ⊗, I)
* is an object M together with two morphisms
*
* μ: M ⊗ M → M called multiplication,
* η: I → M called unit
*
@mandubian
mandubian / typeable.scala
Created December 31, 2016 14:09
Draft implementation of Kind-Polymorphic Typeclass for type-safe type representation & cast operation
import scala.language.higherKinds
import scala.reflect.ClassTag
import scala.collection.{ GenMap, GenTraversable }
/** Draft implmementation of Polykinded Safe Type Representation & Cast */
object Test extends App {
/** Representation of the type of a value
* T is the type constructor of the type of the value (For example Int => T = Int, List[Int] => T = List)
* TAbs (for T abstracted) is a well-formed monomorphic type derived from T (For example List => TAbs = List[Any])
@mandubian
mandubian / ScopedTypeclass.scala
Last active February 6, 2017 19:49
Avoid classic Scala implicit collisions (example +/* monoid) by managing your typeclass instances scoped in a kind-polymorphic data structure
import scala.language.higherKinds
object Test extends App {
// PKList or the Any-Order heterogenous list
sealed trait PKList[Args <: AnyKind]
trait PKNil[Args <: AnyKind] extends PKList[Args]
sealed trait PKCons[Args <: AnyKind, HA, TA <: PKList[Args]] extends PKList[Args] {
package papersplease2
import freek._
import cats.free.Free
import cats.{~>, Id}
import cats.data.Xor
case class Person(name: String)
case class Picture(person: Person)
case class Passedport(person: Person)
@mandubian
mandubian / ASample.scala
Last active January 20, 2017 17:30
Encoding Category Theory Laws (à-la-scalaz/cats) using kind-polymorphic List and compare/manipulate laws of structure
/**
* In classic Scala implementation of hierarchies of Category theory laws (scalaz/cats), the dependency between
* different laws is encoded using inheritance (https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/Monad.scala#L14)
*
* This approach creates well-known issues: for example, a Monad is able to _derive_ an Applicative.
* So sometimes, you don't have the Applicative you want but the one derived from the Monad or it doesn't
* compile because you have 2 Applicatives in your implicit scope.
*
* Alois Cochard has proposed a new model to represent that in (scato project)[https://github.com/aloiscochard/scato] or
* (scalaz/8.0.x)[https://github.com/scalaz/scalaz/blob/series/8.0.x/base/src/main/scala/typeclass/Monad.scala] branch.
@mandubian
mandubian / any-order-ubiklist.scala
Created November 30, 2016 00:08
UbikList or abstracting heterogenous list to anykind
// Basic shapeless-style HList
sealed trait HList
sealed trait HNil extends HList {
def ::[H](h : H) = Test.::(h, this)
}
final case object HNil extends HNil
final case class ::[+H, +T <: HList](head : H, tail : T) extends HList