Created
February 5, 2016 02:01
-
-
Save arnolddevos/09e5f51ce1436f590c7d to your computer and use it in GitHub Desktop.
Dependent Types Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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