You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_06/lab.md
+44-40Lines changed: 44 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,42 +10,35 @@ Secondly we will start playing with the metaprogramming side of Julia, mainly co
10
10
11
11
These topics will be extended in the next [lecture](@ref macro_lecture)/[lab](@ref macro_lab), where we are going use metaprogramming to manipulate code with macros.
12
12
13
-
We will be again a little getting ahead of ourselves as we are going to use quite a few macros, which will be properly explained in the next lecture as well, however for now the important thing to know is that macro is just a special function, that accepts as an argument Julia code, which it modifies.
13
+
We will be again a little getting ahead of ourselves as we are going to use quite a few macros, which will be properly explained in the next lecture as well, however for now the important thing to know is that a macro is just a special function, that accepts as an argument Julia code, which it can modify.
14
14
15
-
## Introspection intro
16
-
Let's start with the topic of code inspection, e.g. we may ask the following: What happens when I execute this
17
-
```julia
18
-
[i for i in1:10]
19
-
```
20
-
We may be tempted to think, that this is some function call, for which we can call the `@which` macro to infer what is being called under the hood, however when we do this Julia will warn us that this is something "more complex"
15
+
## Quick reminder of introspection tooling
16
+
Let's start with the topic of code inspection, e.g. we may ask the following: What happens when Julia evaluates `[i for i in 1:10]`?
17
+
- parsing
21
18
```@repl lab06_intro
22
19
using InteractiveUtils #hide
23
-
@which [i for i in 1:10]
20
+
:([i for i in 1:10]) |> dump
24
21
```
25
-
and that we should rather call the `Meta.@lower` macro, which spits out the so called lowered code of the expression.
22
+
- lowering
26
23
```@repl lab06_intro
27
24
Meta.@lower [i for i in 1:10]
28
25
```
29
-
Now we see that the result is constructed by first creating the range, then a generator, which is subsequently collected. That is the reason, why simple `@which` macro cannot help.
30
-
31
-
So why it is not a call, let's look at how the code is parsed
Rewrite the `polynomial` function from the last [lab](@ref horner) using recursion and find the length of the coefficients, at which inlining of the recursive calls stops occuring.
170
+
Rewrite the `polynomial` function from the last [lab](@ref horner) using recursion and find the length of the coefficients, at which inlining of the recursive calls stops occurring.
176
171
177
172
```julia
178
173
functionpolynomial(a, x)
@@ -195,7 +190,7 @@ end
195
190
<summary class = "solution-header">Solution:</summary><p>
@@ -205,20 +200,23 @@ a = Tuple(ones(Int, 21)) # everything less than 22 gets inlined
205
200
x = 2
206
201
polynomial(a,x) == evalpoly(x,a) # compare with built-in function
207
202
208
-
209
203
@code_lowered polynomial(a,x) # cannot be seen here as optimizations are not applied
210
-
@code_typedpolynomial(a,x)
211
204
@code_llvm polynomial(a,x) # seen here too, but code_typed is a better option
205
+
nothing #hide
206
+
```
207
+
208
+
```@repl lab06_intro
209
+
@code_typed polynomial(a,x)
212
210
```
213
211
214
212
```@raw html
215
213
</p></details>
216
214
```
217
215
218
-
## Let's do some metaprogramming
216
+
## AST manipulation: The first steps to metaprogramming
219
217
Julia is so called homoiconic language, as it allows the language to reason about its code. This capability is inspired by years of development in other languages such as Lisp, Clojure or Prolog.
220
218
221
-
There are two easy ways to extract/construct the code structure [^2]
219
+
There are two easy ways to extract/construct the code structure [^3]
222
220
- parsing code stored in string with internal `Meta.parse`
223
221
```@repl lab06_meta
224
222
code_parse = Meta.parse("x = 2") # for single line expressions (additional spaces are ignored)
@@ -230,7 +228,7 @@ begin
230
228
end
231
229
""") # for multiline expressions
232
230
```
233
-
- constructing an expression using `quote ... end` or simple `:()` syntax
231
+
- constructing an expression using `quote ... end` or simple `:()` syntax
234
232
```@repl lab06_meta
235
233
code_expr = :(x = 2) # for single line expressions (additional spaces are ignored)
236
234
code_expr_block = quote
@@ -326,7 +324,7 @@ s = string(ex)
326
324
Think of some corner cases, that the method may not handle properly.
327
325
328
326
**HINTS**:
329
-
- Use `Meta.parse` in combination with `replace_i`**only** for checking of correctness.
327
+
- Use `Meta.parse` in combination with `replace_i`**ONLY** for checking of correctness.
330
328
- You can use the `replace` function.
331
329
332
330
```@raw html
@@ -345,7 +343,7 @@ does not work in this simple case, because it will replace "i" inside the `sin(z
345
343
</p></details>
346
344
```
347
345
348
-
If the exercises so far did not feel very useful let's focus on one, that is actually useful as a part of the [`IntervalArithmetics.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) pkg.
346
+
If the exercises so far did not feel very useful let's focus on one, that is similar to a part of the [`IntervalArithmetics.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) pkg.
This kind of manipulation is at the core of some pkgs, such as aforementioned [`IntervalArithmetics.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) where every number is replaced with a narrow interval in order to find some bounds on the result of a computation.
398
396
399
397
400
-
[^2]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
398
+
[^3]: Once you understand the recursive structure of expressions, the AST can be constructed manually like any other type.
401
399
---
402
400
403
401
## Resources
402
+
- Julia's manual on [metaprogramming](https://docs.julialang.org/en/v1/manual/metaprogramming/)
403
+
- David P. Sanders' [workshop @ JuliaCon 2021](https://www.youtube.com/watch?v=2QLhw6LVaq0)
404
+
- Steven Johnson's [keynote talk @ JuliaCon 2019](https://www.youtube.com/watch?v=mSgXWpvQEHE)
405
+
- Andy Ferris's [workshop @ JuliaCon 2018](https://www.youtube.com/watch?v=SeqAQHKLNj4)
406
+
-[From Macros to DSL](https://github.com/johnmyleswhite/julia_tutorials) by John Myles White
407
+
- Notes on [JuliaCompilerPlugin](https://hackmd.io/bVhb97Q4QTWeBQw8Rq4IFw?both#Julia-Compiler-Plugin-Project)
0 commit comments