Guest User

Untitled

a guest
Jan 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. trait SemiringElement {
  2. /**
  3. * The element type
  4. */
  5. type E
  6. /**
  7. * The type returned by the the addition and multiplication operators
  8. */
  9. type R <: SemiringElement
  10. val value: E
  11.  
  12. def +(that: R): R
  13.  
  14. def *(that: R): R
  15.  
  16. override def toString = value.toString
  17. }
  18.  
  19. case class BooleanSemiringElement(init: Boolean) extends SemiringElement {
  20. type E = Boolean
  21. type R = BooleanSemiringElement
  22. val value = init
  23.  
  24. def +(that: BooleanSemiringElement#R) = BooleanSemiringElement(value || that.value)
  25.  
  26. def *(that: BooleanSemiringElement#R) = BooleanSemiringElement(value && that.value)
  27. }
  28.  
  29. trait Semiring {
  30. type E <: SemiringElement
  31. /**
  32. * The addition identity
  33. */
  34. val zero: E
  35. /**
  36. * The multiplication identity
  37. */
  38. val one: E
  39. }
  40.  
  41. object BooleanSemiring extends Semiring {
  42. type E = BooleanSemiringElement
  43.  
  44. val zero = BooleanSemiringElement(false)
  45. val one = BooleanSemiringElement(true)
  46. }
  47.  
  48. class ElementMap(s: Semiring) {
  49. val m = mutable.Map[String, SemiringElement]()
  50. }
  51.  
  52. val x = new ElementMap(BooleanSemiring)
  53.  
  54. scala> val x = new ElementMap(BooleanSemiring)
  55. x: ElementMap = ElementMap@46cf97b
  56. scala> x.m
  57. res2: scala.collection.mutable.Map[String,SemiringElement] = Map()
  58. scala> x.m("one") = BooleanSemiring.one
  59. scala> x.m("one") + BooleanSemiring.one
  60. <console>:12: error: type mismatch;
  61. found : BooleanSemiring.one.type (with underlying type BooleanSemiring.BooleanSemiringElement)
  62. required: _1.R where val _1: SemiringElement
  63. x.m("one") + BooleanSemiring.one
  64. ^
  65.  
  66. class ElementMap[BooleanSemiring]...
  67.  
  68. class ElementMap(s: Semiring) {
  69. val m = mutable.Map[String, s.E]()
  70. }
  71.  
  72. scala> class ElementMap[S <: Semiring](s: S) {
  73. | val m = collection.mutable.Map[String, S#E]()
  74. | }
  75. defined class ElementMap
  76.  
  77. scala> val x = new ElementMap(BooleanSemiring)
  78. x: ElementMap[BooleanSemiring.type] = ElementMap@6544c984
  79.  
  80. scala> x.m("one") = BooleanSemiring.one
  81.  
  82. scala> x.m("one") + BooleanSemiring.one
  83. res0: BooleanSemiringElement = true
  84.  
  85. scala> class ElementMap[SE <: SemiringElement](s: Semiring { type E = SE }) {
  86. | val m = collection.mutable.Map[String, SE]()
  87. | }
  88. defined class ElementMap
  89.  
  90. scala> val x = new ElementMap(BooleanSemiring)
  91. x: ElementMap[BooleanSemiringElement] = ElementMap@4cf353e5
  92.  
  93. scala> x.m("one") = BooleanSemiring.one
  94.  
  95. scala> x.m("one") + BooleanSemiring.one
  96. res1: BooleanSemiringElement = true
Add Comment
Please, Sign In to add comment