Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package reproduction
  2.  
  3. import scala.reflect.Manifest
  4. import scala.lms.common._
  5.  
  6. trait Reproducing
  7. extends BooleanOps with OrderingOps with PrimitiveOps with ArrayOps with StringOps with NumericOps with TupleOps with StructOps
  8. with Equal with Variables with While with IfThenElse with TupledFunctions
  9. with LiftVariables with LiftBoolean with LiftPrimitives with LiftString {
  10.  
  11. trait Gen[S] {
  12. def k1(k : S => Rep[Int]): Rep[Int]
  13. def k2(s : S, k: (Rep[Int] => Rep[Int])) : Rep[Int]
  14. }
  15.  
  16. sealed trait HidesS
  17. case class G[S](gen : Gen[S]) extends HidesS
  18.  
  19. /*
  20.  
  21. Box[Int](x).start() where x is some integer compiles down to the following:
  22.  
  23. class reproducingTest extends ((Int)=>(Int)) {
  24. def apply(x0:Int): Int = {
  25. var x1: Int = 0
  26. var x2: Int = 0 // why is this declared outside the loop? assuming this is the i.
  27. val x10 = while ({val x3 = x2
  28. val x4 = x3 < 10
  29. x4}) {
  30. var x6: Int = 0
  31. x6 = 1
  32. x2 = 1 // x2 doesn't seem to be updated
  33. ()
  34. }
  35. 5
  36. }
  37. }
  38.  
  39. */
  40.  
  41. class Box(hidden_s : HidesS) {
  42. def start() : Rep[Int] = {
  43. val G(gen) = hidden_s
  44. gen.k1(s => {
  45. var counter = 0
  46. while (counter < 10){
  47. gen.k2(s, (a : Rep[Int]) => {
  48. counter = a // counter is updated by index conceptually
  49. counter
  50. })
  51. ()
  52. }
  53. 4
  54. })
  55. 5
  56. }
  57. }
  58. object Box {
  59. def apply(element : Rep[Int]) : Box = {
  60.  
  61. class GenImpl extends Gen[Rep[Int]] {
  62. def k1(k : Rep[Int] => Rep[Int]) : Rep[Int] = {
  63. var i = 0 // creates state
  64. k(i)
  65. }
  66. def k2(s : Rep[Int], k : (Rep[Int] => Rep[Int])) : Rep[Int] = {
  67. var index = s
  68. index = index + 1 // updates state
  69. k(index)
  70. }
  71. }
  72.  
  73. new Box(G(new GenImpl))
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement