Skip to content

Instantly share code, notes, and snippets.

@travisbrown
travisbrown / path-dependent-type-monad.scala
Created October 17, 2011 21:53
path-dependent-type-monad
object test {
trait M[X[_]] {
def map[A, B](a: X[A])(f: A => B): X[B]
}
trait Parsers {
trait Parser[Y] {
def map[B](f: Y => B): Parser[B] = null
}
}
@milessabin
milessabin / gist:1819087
Created February 13, 2012 18:59
Access to companion object via implicit resolution
// See this mailing list thread for context:
// https://groups.google.com/d/topic/scala-language/ImqJuGylyi8/discussion
case class Companion[-C, T](t : T)
trait Publish[C, T] { self : T =>
implicit val companion = Companion[C, T](this)
}
trait StaticKey {
@eparejatobes
eparejatobes / neo4jTypes.scala
Created February 17, 2012 17:48
neo4j fields and records with shapeless
import shapeless._
import shapeless.TypeOperators._
import shapeless.Record._
import shapeless.BasisConstraint._
import Neo4jTypesConstraints.OfNeo4jFieldsConstraint._
import Neo4jTypesConstraints._
import Neo4jTypes._
object Neo4jTypes {
@milessabin
milessabin / gist:1907932
Created February 25, 2012 11:20
Lifting Monoid over arbitrary arity type constructors
object MonoidExamples {
import shapeless._
import HList._
trait Monoid[T]
// Boilerplate ...
trait HListed[S, L <: HList]
implicit def hl1[S[_], A] = new HListed[S[A], A :: HNil] {}
implicit def hl2[S[_, _], A, B] = new HListed[S[A, B], A :: B :: HNil] {}
@milessabin
milessabin / gist:2031481
Created March 13, 2012 20:49
Impredicative types in Scala via shapeless
// See http://research.microsoft.com/en-us/um/people/simonpj/papers/boxy/boxy-icfp.pdf
object Impredicative extends App {
import shapeless._
import TypeOperators._
object head extends (List ~> Id) {
def default[T](l : List[T]) = l.head
}
def g(o : Option[List ~> Id]) = o match {
@milessabin
milessabin / gist:2336624
Created April 8, 2012 10:59
Name this idiom. Is it novel?
// K ~?> V witnesses the existence of a natural transformation between K and V
class ~?>[K[_], V[_]] {
class λ[KT, VT] // Witness K ~?> V at a particular K[T], V[T]
}
object ~?> {
// To instantiate the first-order witness we need a value of the
// higher-kinded witness, so we depend on it implicitly
implicit def witness[K[_], V[_], T](implicit rel : K ~?> V) = new rel.λ[K[T], V[T]]
@milessabin
milessabin / gist:2500326
Created April 26, 2012 15:22
Scala collection extension methods made easy (dependent types FTW!)
/*
* Copyright (c) 2012 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@milessabin
milessabin / gist:2659013
Created May 11, 2012 11:08
Proving equality of type constructors in Scala via an "arbitrary object" encoding of universal quantification.
// Universal quantification is encoded in terms of quantifier-free
// assertions about an "abitrary" type (cp. "all swans are white" vs.
// "the arbitrary swan is white". Inspired by Kit Fine's 1985 "Reasoning
// with Arbitrary Objects", http://philosophy.fas.nyu.edu/object/kitfine.
//
// Possibly also related to Oleg Kiselyov's "Interpreting types as
// abstract values", http://okmij.org/ftp/Computation/index.html#teval.
// What I wouldn't give for kind-polymorphism here ...
@kevinwright
kevinwright / ZeeingEvent.scala
Created July 10, 2012 12:19
Typesafe conversion from List[Any] to a case class, via shapeless
case class ZeeingEvent(
zid: String,
kind: String,
showId: String,
show_name: Option[String],
time: DateTime
) {
require (kind == "StartedZeeing" || kind == "EndedZeeing")
}
@travisbrown
travisbrown / ExtraStateParsers.scala
Created September 19, 2012 02:10
Threading extra state through a parser in Scala
import scala.util.parsing.combinator._
import scalaz._, Scalaz._
trait ExtraStateParsers[S] { this: Parsers =>
type ESP[A] = StateT[Parser, S, A]
protected implicit def monadInstance: Monad[Parser]
protected implicit def monadStateInstance =
implicitly[MonadState[({type F[T, +B] = StateT[Parser, T, B]})#F, S]]