Last active
August 29, 2015 14:06
-
-
Save Blaisorblade/19ef1abb28e20995e187 to your computer and use it in GitHub Desktop.
Test encoding SML functors
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
// http://stackoverflow.com/a/23019436/53974 | |
trait ORD { | |
type T | |
def leq(a: T, b: T): Boolean | |
} | |
// The most direct attempt runs into limitations of refinements. | |
//def F(X : ORD) = new { def eq(x : X.T, y : X.T) = X.leq(x, y) && X.leq(y, x) } | |
// Let's avoid refinements instead by declaring the signature we want. | |
trait EQ { | |
type T | |
def eq(x : T, y : T): Boolean | |
} | |
def F(X : ORD) = new EQ { type T = X.T; def eq(x : X.T, y : X.T) = X.leq(x, y) && X.leq(y, x) } | |
// The REPL shows that F is given the correct type: | |
// F: (X: ORD)EQ{type T = X.T} | |
// That is, EQ{type T = X.T} is the return type. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment