-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
61 lines (41 loc) · 2.95 KB
/
index.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
<html xmlns:svg='http://www.w3.org/2000/svg' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head><meta content='application/xhtml+xml;charset=utf-8' http-equiv='Content-type' /><title>Tuple map</title></head>
<body>
<h1 id='tuple_map'>Tuple map</h1>
<p>This exercise implements a “function” to map over a tuple:</p>
<pre><code>let (a, b, c) = Tuple.map g (d, e, f)</code></pre>
<p>Such a function can’t be implemented in plain OCaml of course; we use Camlp4 to rewrite this to</p>
<pre><code>let (a, b, c) = (g d, g e, g f)</code></pre>
<p>If the tuple is not a syntactic tuple, we have to specify its arity. (Camlp4 works with syntax trees, so has no idea of the type of an expression.) In that case let’s use the syntax</p>
<pre><code>let x = Tuple.map 3 g y</code></pre>
<p>to indicate that y is a 3-element tuple.</p>
<h2 id='filters'>Filters</h2>
<p>This expansion is implemented using a Camlp4 filter. To implement a filter we just provide a function of type <code>Ast.str_item ->
Ast.str_item</code>; we match the AST fragment for the syntax above and replace it with the rewritten code.</p>
<p>To walk the tree we extend the <code>Ast.map</code> object.</p>
<h2 id='other_stuff'>Other stuff</h2>
<p>Unfortunately, Camlp4 filters don’t work in the toplevel. So once you have built and installed the code, you can try it by running e.g.</p>
<pre><code>camlp4of -I `ocamlfind query tuple_map` tuple_map.cmo \
-str "Tuple.map f (1, 2, 3)"</code></pre>
<p>or use the <code>syntax_camlp4o</code> and <code>pkg_tuple_map</code> tags in your ocamlbuild <code>_tags</code> file.</p>
<h2 id='tasks'>Tasks</h2>
<ol>
<li>
<p>We have provided the boilerplate for registering a filter and walking the AST, but the actual rewrite is not implemented. You need to match <code>Tuple.map</code> expressions and replace them with the appropriate substitution. Do just the case where the tuple is a syntactic tuple.</p>
<p>See the <code>Ast</code> functions <code>list_of_expr</code> and <code>exCom_of_list</code> (see the library signatures documentation) for converting between <code>Ast.ExCom</code> lists (that is, comma separated expressions) and ordinary lists. Also see the quotations page and the <code>tup</code> antiquotation in particular.</p>
</li>
<li>
<p>Do the case where the arity of the tuple is given as an argument.</p>
</li>
<li>
<p>Lots of other functions from the <code>List</code> module might be useful over tuples instead. Pick a few and implement them.</p>
</li>
<li>
<p>Some but not all of the functions in <code>List</code> still make sense if the elements of the tuple or tuples involved have different types. Implement one of these, if you have not already, and make sure that it actually works on such tuples.</p>
</li>
</ol>
</body></html>