Skip to content

Instantly share code, notes, and snippets.

@arnolddevos
Created February 5, 2016 02:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnolddevos/09e5f51ce1436f590c7d to your computer and use it in GitHub Desktop.
Save arnolddevos/09e5f51ce1436f590c7d to your computer and use it in GitHub Desktop.
Dependent Types Example
// Example from Odersky et al http://infoscience.epfl.ch/record/215280/files/paper.pdf
object DT {
trait Key { type Value }
class Setting(val str: String) extends Key
val sort = new Setting("sort") { type Value = String }
val width = new Setting("width") { type Value = Int }
val params = HMap.empty.add(width)(120).add(sort)("time")
val x: Option[Int] = params.get(width)
val y: Option[String] = params.get(sort)
trait HMap { self =>
def get(key: Key): Option[key.Value]
def add(key: Key)(value: key.Value) = new HMap {
def get(k: Key) =
if (k == key) Some(value.asInstanceOf[k.Value])
else self.get(k)
}
}
object HMap {
def empty = new HMap { def get(k: Key) = None }
}
}
// Example rewritten without path dependent types but with equivalent type parameters
object TP {
trait Key[Value] {}
class Setting[Value](val str: String) extends Key[Value]
val sort = new Setting[String]("sort")
val width = new Setting[Int]("width")
val params = HMap.empty.add(width)(120).add(sort)("time")
val x: Option[Int] = params.get(width)
val y: Option[String] = params.get(sort)
trait HMap { self =>
def get[Value](key: Key[Value]): Option[Value]
def add[Value](key: Key[Value])(value: Value) = new HMap {
def get[X](k: Key[X]) =
if (k == key) Some(value.asInstanceOf[X])
else self.get(k)
}
}
object HMap {
def empty = new HMap { def get[Value](k: Key[Value]) = None }
}
}
object Run extends App {
println(s"DT: x=${DT.x} y=${DT.y}")
println(s"TP: x=${TP.x} y=${TP.y}")
}
// DT: x=Some(120) y=Some(time)
// TP: x=Some(120) y=Some(time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment