Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@edwinb
edwinb / vc.idr
Created November 26, 2012 18:33
Type class resolution as a (partial) decision procedure
-- Idris type classes actually take any kind of value as a parameter,
-- we're not restricted to Sets or parameterised Sets.
-- So we actually have 'value classes'. It seems we can use type class
-- resolution to make rudimentary decision procedures, then:
using (xs : List a)
data Elem : a -> List a -> Set where
Here : Elem x (x :: xs)
There : Elem x xs -> Elem x (y :: xs)
@travisbrown
travisbrown / type-member-example.scala
Created December 10, 2012 10:55
Instantiating a trait with a type member in a macro
/** Instantiating a trait with a type member in a macro.
*
* Complete working example by Travis Brown for this Stack Overflow question:
* http://stackoverflow.com/q/13795490/334519
*/
import scala.language.existentials
import scala.language.experimental.macros
trait TypeBuilder { type fieldType }
@travisbrown
travisbrown / pseudo-singletons.scala
Created December 12, 2012 17:20
Constructing "singleton types" from compile-time literals with macros
/** Constructing "singleton types" from compile-time literals
*
* val x = Example.foo(42)
* val y: x.T = 42 // compiles
* val z: x.T = 43 // doesn't
*
*/
import scala.language.experimental.macros
import scala.reflect.macros.Context
/** Constructing "singleton types" from compile-time literals
*
* val x = Example.foo(42)
* val y: x.T = 42 // compiles
* val z: x.T = 43 // doesn't
*
*/
package s
import scala.language.experimental.macros
@puffnfresh
puffnfresh / Macros.scala
Last active December 11, 2015 01:58
Removing any2unit via a macro.
import language.experimental.{ macros => scalaMacros }
import reflect.macros.Context
package object macros {
def safe[A](expr: A) = macro safeImpl[A]
def safeImpl[A](c: Context)(expr: c.Expr[A]): c.Expr[A] = {
import c.universe._
def isIgnoredStatement(tree: Tree) = tree match {
import language.experimental.macros
import language.implicitConversions
import scala.reflect.macros.Context
import scala.reflect.runtime.universe.Tree
class ReflectiveClosure[A, B](val tree: Tree, fn: Function1[A, B]) extends Function1[A, B] {
def apply(x: A) = fn(x)
}
object ReflectiveClosure {
@paulp
paulp / test.scala
Created February 1, 2013 17:57
statically typed companion objects
package s
object Test {
// Observe that x.companion is statically typed such that foo is callable
def f1() = {
val x = new Foo
println(x) // Foo instance
println(x.companion) // Foo companion
println(x.companion.foo) // I'm foo!
13:29 ~/Projects/Kepler_introduce-member/sandbox (topic/introduce-member)$ cat Macros.scala
import scala.reflect.macros.Context
import language.experimental.macros
object Macros {
def impl(c: Context)(target: c.Tree, name: c.Tree, code: c.Tree) = {
import c.universe._
val Literal(Constant(targetType: Type)) = c.typeCheck(target)
val Literal(Constant(methodName: String)) = name
val Function(methodParams, methodBody) = code
@gseitz
gseitz / build.scala
Created February 19, 2013 22:52
SBT task for running all main classes
import sbt._
import Keys._
import Build.data
object build extends Build {
lazy val runAll = TaskKey[Unit]("run-all")
lazy val standardSettings = Seq(
runAllIn(Compile)
)
@puffnfresh
puffnfresh / Main.scala
Created March 6, 2013 02:44
Implementing a Single Abstract Method from a Scala lambda using macros (SAMBDAS!)
import MyFuncMacro.myfunc
object MyFuncExample {
def main(args: Array[String]) {
val x: MyFunc[Int, Int] = myfunc { a: Int => a }
println(x)
println(x(42))
}
}