Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@milessabin
milessabin / gist:cadd73b7756fe4097ca0
Last active September 16, 2019 13:44
A new approach to encoding dependently-typed chained implicits, using singleton types ...
object Demo {
// A couple of type classes with type members ...
trait Foo[T] {
type A
}
object Foo {
implicit val fooIS = new Foo[Int] { type A = String }
}
/*
* Copyright (c) 2019 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
def tupleType(tpes: List[Type]): Type = {
val PairType = typeOf[_ *: _] match {
case Type.IsAppliedType(tp) => tp.tycon
}
@tailrec
def nestedPairs(tpes: List[Type], acc: Type): Type = tpes match {
case Nil => acc
case hd :: tail => nestedPairs(tail, Type.AppliedType(PairType, List(hd, acc)))
}
object Utils {
type Id[t] = t
type Const[c] = [t] => c
}
import Utils._
class Instances[F[_[_]], T[_]]
trait Functor[F[_]]
@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 ...
@milessabin
milessabin / gist:9042788
Created February 17, 2014 00:42
Boilerplate free conversion from case classes to shapeless records via LabelledGeneric ... coming soon to shapeless 2.0.0.
scala> import shapeless._, record._
import shapeless._
import record._
scala> case class Person(name: String, address: String, age: Int)
defined class Person
scala> val joe = Person("Joe", "Brighton", 33)
joe: Person = Person(Joe,Brighton,33)
@milessabin
milessabin / LocalTests.scala
Last active October 6, 2018 18:26
Setup for local integration (ie. compile pos/neg/run) tests for lampepfl/dotty
// local/localtests/test/localtests/LocalTests.scala
package localtests
import java.nio.file._
import java.util.stream.{ Stream => JStream }
import org.junit.{ AfterClass, Test }
import scala.collection.JavaConverters._
import scala.concurrent.duration._
@milessabin
milessabin / typelevelcps.scala
Created May 29, 2018 10:13
Using type level continuation passing style to rewrite a whitebox macro (which relies on fundep materialization) as a blackbox macro
import scala.language.higherKinds
// Whitebox ...
trait Schema[T, R] {
def conv(t: T): R
}
object Schema {
// Whitebox macro: R is computed from T
implicit def mkSchema[T, R]: Schema[T, R] = ??? // macro ...