forked from scala-lms/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.scala
177 lines (130 loc) · 4.55 KB
/
start.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
Getting Started
===============
Outline:
<div id="tableofcontents"></div>
Staging and LMS
---------------
What is staging? The idea behind staging is to delay computation of
certain expressions, generating code to compute them later. The
benefit is *abstraction without regret*: using high-level programming
abstractions once, to structure generated code, instead of all the
time during execution.
Lightweight Modular Staging (LMS) is a staging technique driven by
types. In addition to the *staging* aspect, the technique is *lightweight*,
because it is purely library based. It is also *modular*, because
features can be mixed and matched and the framework is easy to
extend.
My First Multi-Stage Program
------------------------------
This tutorial is a [literate Scala file](https://github.com/scala-lms/tutorials/tree/master/src/test/scala/lms/tutorial/start.scala). We invite you to clone the
[GitHub repo](https://github.com/scala-lms/tutorials/) and play with the code as you follow along.
*/
package scala.lms.tutorial
import scala.lms.common._
class GettingStartedTest extends TutorialFunSuite {
val under = "dslapi"
/**
### Rep[T] vs T
In LMS, `Rep[T]` represents a delayed compuation of type `T`. Thus,
during staging, an expression of type `Rep[T]` becomes part of
the generated code, while an expression of bare type `T` becomes a
constant in the generated code. For core Scala features, adding
`Rep` types should be enough to build a program generator, as we
will see later.
*/
test("1") {
val snippet = new DslDriver[Int,Int] {
def snippet(x: Rep[Int]) = {
def compute(b: Boolean): Rep[Int] = {
// the if is executed in the first stage
if (b) 1 else x
}
compute(true)+compute(1==1)
}
}
assert(snippet.eval(0) === 2)
check("1", snippet.code)
}
/**
.. includecode:: ../../../../out/dslapi1.check.scala
Contrast the snippet above, where `b` is a `Boolean`, with the snippet
below, where `b` is a `Rep[Boolean]`. The expression `if (b) 1 else x`
is executed at staging time when `b` is a `Boolean`, while it is delayed
causing code to be generated for the `if` expression when `b` is a
`Rep[Boolean]` -- indeed, the actual value of `b` is not known at staging
time, but only when the generated code is executed.
*/
test("2") {
val snippet = new DslDriver[Int,Int] {
def snippet(x: Rep[Int]) = {
def compute(b: Rep[Boolean]): Rep[Int] = {
// the if is deferred to the second stage
if (b) 1 else x
}
compute(x==1)
}
}
assert(snippet.eval(2) === 2)
check("2", snippet.code)
}
/**
.. includecode:: ../../../../out/dslapi2.check.scala
### Rep[A => B] vs Rep[A]=>Rep[B]
In the previous snippets, we already notice some *abstraction without
regret*: the ``compute`` function exists only at staging time, and is
not part of the generated code -- more generally, we can freely use
abstractions to structure and compose the staged program, but these
abstractions are not part of the generated code when their type is a
bare ``T`` as opposed of a ``Rep[T]``. In the right snippet,
``compute`` has the type ``Rep[Boolean] => Rep[Int]``, not
``Rep[Boolean => Int]`` -- its type already tells us that the function
is known at staging time.
*/
test("power") {
val snippet = new DslDriver[Int,Int] {
def power(b: Rep[Int], x: Int): Rep[Int] =
if (x == 0) 1
else b * power(b, x-1)
def snippet(b: Rep[Int]) =
power(b, 3)
}
assert(snippet.eval(2) === 8)
check("power", snippet.code)
}
/**
.. includecode:: ../../../../out/dslapipower.check.scala
### Rep[Range] vs Range
Loops can be unrolled in the first stage, or be generated as loops in
the second stage, driven by the type of their condition.
*/
test("range1") {
val snippet = new DslDriver[Int,Unit] {
def snippet(x: Rep[Int]) = comment("for", verbose = false) {
for (i <- (0 until 3): Range) {
println(i)
}
}
}
check("range1", snippet.code)
}
/**
.. includecode:: ../../../../out/dslapirange1.check.scala for
*/
test("range2") {
val snippet = new DslDriver[Int,Unit] {
def snippet(x: Rep[Int]) = comment("for", verbose = false) {
for (i <- (0 until x): Rep[Range]) {
println(i)
}
}
}
check("range2", snippet.code)
}
/**
.. includecode:: ../../../../out/dslapirange2.check.scala for
What's next?
------------
Go back to the [tutorial index](index.html) or continue with the [Shonan Challenge](shonan.html).
*/
}