Skip to content

Instantly share code, notes, and snippets.

Miles Sabin milessabin

View GitHub Profile
View gist:1ed8da29f314df2942c2dfd5f5a573e6
import scala.language.higherKinds
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Traverse[F[_]] {
implicit val F: Functor[F]
}
View gist:b92b9bcc4298e0077ba033de49531bc0
tapply: (a -> b) -> a -> b
tapply ab a = ab a
foo: (tapply List Nat) --OK
foo = [1, 2, 3]
mkPair: a -> b -> (a, b)
mkPair a b = (a, b)
bar: (mkPair Nat String) -- Not OK
@milessabin
milessabin / gist:6c9c060cf5159563b722d49ce9ee103e
Last active Jul 26, 2017
Induction benchmark: vanilla scalac vs. scalac + proof of concept inductive solver
View gist:6c9c060cf5159563b722d49ce9ee103e
// Compiled with ./build/pack/bin/scalac -J-Xss4M -J-Xmx4G test/files/pos/inductive-implicits.scala
// HList scalac scalac +
// Size inductive heuristics
// 50 6 4
// 100 15 5
// 150 36 6
// 200 68 6
// 250 114 7
// 300 179 9
View gist:2cc749c84e504876cb6e9507993fa7c4
scala> def stuff(xs: ::[Int]): Int = xs.head
stuff: (xs: ::[Int])Int
scala> stuff(Nil)
<console>:13: error: type mismatch;
found : scala.collection.immutable.Nil.type
required: ::[Int]
stuff(Nil)
^
View gist:55939412017e2da20bcb9047240364f8
scala> def narrow[T <: Singleton](t: T): T {} = t
narrow: [T <: Singleton](t: T)T
scala> narrow(23)
res1: 23 = 23
scala> var x = narrow(42)
x: 42 = 42
scala>
View gist:9005671ea17add0f5054f01995e54025
### Keybase proof
I hereby claim:
* I am milessabin on github.
* I am milessabin (https://keybase.io/milessabin) on keybase.
* I have a public key whose fingerprint is 87D4 DCA4 07A5 556C 0BAD D01F 420D 4BEE 9FA4 7A44
To claim this, I am signing this object:
View sip23.scala
object Test {
def one[T <: 1](t: T): T = t
final val o = one(1)
o: 1
def narrow[T <: Singleton](t: T): T {} = t
final val fn0 = narrow(23)
fn0: 23
val n0 = narrow(23)
@milessabin
milessabin / singleton-only.scala
Created Jun 6, 2016
Scala type which can only be extended by an object, not by a non-abstract type ...
View singleton-only.scala
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
View pws.scala
import scala.language.higherKinds
object cats {
trait Show[A] {
def apply(x: A): String
}
object Show {
implicit def stringShow: Show[String] = new Show[String] {
def apply(x: String): String = x
@milessabin
milessabin / blah.scala
Created May 19, 2016
Doing something useful with bare singleton types and no macros ...
View blah.scala
scala> trait Label[T] { val value: Boolean }
defined trait Label
scala> def mkInstance(s: String, v: Boolean): Label[s.type] = new Label[s.type] { val value = v }
mkInstance: (s: String, v: Boolean)Label[s.type]
scala> implicit def lTrue = mkInstance("True", true)
lTrue: Label[String("True")]
scala> implicit def lFalse = mkInstance("False", false)
You can’t perform that action at this time.