Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blaisorblade/19ef1abb28e20995e187 to your computer and use it in GitHub Desktop.
Save Blaisorblade/19ef1abb28e20995e187 to your computer and use it in GitHub Desktop.
Test encoding SML functors
// 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