Skip to content

Instantly share code, notes, and snippets.

@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
@paulp
paulp / sbt-new
Created January 2, 2017 17:57
a less messy sbt new
#!/usr/bin/env bash
#
# Intercepting the output of 'sbt new' directly fails
# because it asks interactive questions so we can't process
# line by line. An apparent OSX-specific bug in egrep
# fails on lookbehind clauses so we use ag. Capturing
# the interactive output with script succeeds, but script
# uses \r\n to terminate lines so we can't match the
# filename with '.*' or we get
#
@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] {
@paulp
paulp / sbt-with-coursier.sh
Last active March 30, 2017 07:58
sbt from zero to shell in 39s.
#!/usr/bin/env bash -x
#
# Invoke sbt starting with no local jars, using coursier.
shopt -s globstar
set -euo pipefail
cpof() {
find "$@" -iname '*.jar' -o -iname '*.zip' | paste -sd: -
}
@viktorklang
viktorklang / restart_audio.sh
Created December 11, 2016 08:05
Emergency audio reset on OS X / macOS when faced with loss of audio
#Use at your own risk. No warranties expressed or implied. YMMV. Drive responsibly. Eat healthy.
#for ZSH, I typically put these in my .zshrc
function restart_audio() {
command sudo killall coreaudiod &&
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist &&
sudo launchctl load /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist &&
echo 'Audio daemon restarted'
}
@paulp
paulp / global.sbt
Last active October 16, 2018 19:09
continuous compilation of the sbt build
// These lines go in ~/.sbt/0.13/global.sbt
watchSources ++= (
(baseDirectory.value * "*.sbt").get
++ (baseDirectory.value / "project" * "*.scala").get
++ (baseDirectory.value / "project" * "*.sbt").get
)
addCommandAlias("rtu", "; reload ; test:update")
addCommandAlias("rtc", "; reload ; test:compile")
addCommandAlias("ru", "; reload ; update")
import scala.collection.immutable._
case class Foo(s: String)
// what do you expect?
implicit val ordering = Ordering[String]
val tree = TreeMap.empty ++ (1 to 100).map { i => Foo(i.toString) -> i }
tree.getClass
// compared to
package demo
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.{Global, Phase}
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
class DemoPlugin(val global: Global) extends Plugin {
import global._
override def init(options: List[String], error: String => Unit): Boolean = true

Coherence Domains in Scala

Miles probably already thought of this idea and found some problem with it, but I'm going to go ahead and propose it anyway so that he has an easy way of telling me it's a bad idea. :-)

In order to solve the problem of typeclass coherence in a system which supports local instances (Scala), I propose that we introduce a tagging type – which I call a coherence domain – on all implicit values, regardless of their declaration scope. This tagging type could be encoded as a type member on the type of the implicit value itself, but that is awkward, exposes some details of the machinery in user-facing APIs, and also requires a macro to fully implement. This proposal requires a small, backwards-compatible change to the type system, an unambiguous and consistent change to the syntax (also backwards-compatible), and a minor revision to the implicit resolution rules.

Domains

Declaration