• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
9%
SHARE
TWEET

# Untitled

a guest Jul 24th, 2011 148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. package fundeps
2.
3. /**
4.  * So in Scala - we can enforce functional dependencies by making use of implicit object
5.  * object definitions - this tutorial is at the following URL:
6.  * Credit: Miles Sabin
7.  * http://www.chuusai.com/2011/07/16/fundeps-in-scala/
8.  */
9.
10. // Say we have a function that accepts three parameterised types, but rules as for that the third
11. // type can be dependant on the other types:
12. // Matrix x Matrix = Matrix
13. // Matrix x Vector = Vector
14. // Matrix x Int = Matrix
15. // Int x Matrix = Matrix
16.
17. // This is how we do it:
18.
19. // Dummy definitions
20. trait Matrix
21. trait Vector
22. trait MultDep[-A, -B, C] extends Function2[A, B, C]
23. // Note - A and B above need to be contravariant because....
24.
25. object Methods {
26.
27.   def mult[A, B, C](a : A, b : B)(implicit instance : MultDep[A, B, C]) : C =
28.     instance(a, b)
29.
30. }
31.
32. object Runner {
33.
34.   import Methods.mult
35.
36.   implicit object mmm extends MultDep[Matrix, Matrix, Matrix] {
37.     def apply(m1 : Matrix, m2 : Matrix) : Matrix = sys.error("TODO")
38.   }
39.
40.   implicit object mvv extends MultDep[Matrix, Vector, Vector] {
41.     def apply(m1 : Matrix, v2 : Vector) : Vector = sys.error("TODO")
42.   }
43.
44.   implicit object mim extends MultDep[Matrix, Int, Matrix] {
45.     def apply(m1 : Matrix, i2 : Int) : Matrix = sys.error("TODO")
46.   }
47.
48.   implicit object imm extends MultDep[Int, Matrix, Matrix] {
49.     def apply(i1 : Int, m2 : Matrix) : Matrix = sys.error("TODO")
50.   }
51.
52.   def main(args: Array[String]) {
53.     // Type annotations soley to verify that the correct result type has been inferred
54.     val r1 : Matrix = mult(new Matrix {}, new Matrix{}) // Compiles
55.     val r2 : Vector = mult(new Matrix {}, new Vector{}) // Compiles
56.     val r3 : Matrix = mult(new Matrix {}, 2)            // Compiles
57.     val r4 : Matrix = mult(2, new Matrix {})            // Compiles
58.
59.     // This next one doesn't compile ...
60.     //val r5 : Matrix = mult(new Matrix {}, new Vector{})
61.   }
62.
63. }
RAW Paste Data
Top