raz / site / public / tour / generics.html

176 lines · 11.5 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Generic modules - Raz, Set, Go</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Generic modules - Raz, Set, Go">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
22<li><a href="hello.html">Hello, world</a></li>
23<li><a href="modules.html">Modules</a></li>
24<li><a href="integers.html">Integers</a></li>
25<li><a href="values.html">Values and conversions</a></li>
26<li><a href="procedures.html">Procedures and several results</a></li>
27</ol>
28<h3>Set</h3><ol start='6'>
29<li><a href="slices.html">Arrays, slices and strings</a></li>
30<li><a href="records.html">Records and pointers</a></li>
31<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
32<li><a href="memory.html">Memory and allocators</a></li>
33</ol>
34<h3>Go</h3><ol start='10'>
35<li><a href="errors.html">Errors</a></li>
36<li><a href="defer.html">Defer</a></li>
37<li><a href="checks.html">When it goes wrong</a></li>
38<li><a href="generics.html" aria-current="page">Generic modules</a></li>
39<li><a href="c.html">Talking to C</a></li>
40<li><a href="threads.html">Threads</a></li>
41</ol></aside><article><p class="kicker">Go &middot; lesson 13 of 15</p><h1>Generic modules</h1>
42<p>A module may take types, and the procedures it needs on them, as parameters. <code>Biggest.raz</code>:</p>
43<div class="file"><span>Biggest.raz</span></div><pre class="code"><code><span class="kw">module</span> Biggest(T; less: <span class="kw">proc</span>(a, b: T): <span class="ty">BOOL</span>);
44
45<span class="comment">(* the biggest of s, which mustn&#x27;t be empty *)</span>
46<span class="kw">proc</span> max*(s: []T): T;
47<span class="kw">var</span> i: <span class="ty">INT</span>; m: T;
48<span class="kw">begin</span>
49 m := s[<span class="number">0</span>];
50 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span>
51 <span class="kw">if</span> less(m, s[i]) <span class="kw">then</span> m := s[i]; <span class="kw">end</span>;
52 <span class="kw">end</span>;
53 <span class="kw">return</span> m;
54<span class="kw">end</span>;
55
56<span class="kw">end</span> Biggest.</code></pre>
57<p>and a program that uses it:</p>
58<div class="file"><span>Generics.raz</span></div><pre class="code"><code><span class="kw">module</span> Generics;
59<span class="kw">import</span> Out, Ints := Biggest(<span class="ty">INT</span>, before),
60 Words := Biggest([]<span class="kw">const</span> <span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before);
61
62<span class="kw">proc</span> before(a, b: <span class="ty">INT</span>): <span class="ty">BOOL</span>;
63<span class="kw">begin</span>
64 <span class="kw">return</span> a &lt; b;
65<span class="kw">end</span>;
66
67<span class="kw">proc</span> shorter(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">BOOL</span>;
68<span class="kw">begin</span>
69 <span class="kw">return</span> <span class="ty">LEN</span>(a) &lt; <span class="ty">LEN</span>(b);
70<span class="kw">end</span>;
71
72<span class="kw">proc</span> main*;
73<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
74<span class="kw">begin</span>
75 a := [<span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">5</span>];
76 Out.int(Ints.max(a)); Out.ln;
77 Out.str(Words.max([<span class="string">&quot;a&quot;</span>, <span class="string">&quot;tree&quot;</span>, <span class="string">&quot;is&quot;</span>])); Out.ln;
78 Order.sort(a);
79 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
80 Out.ln;
81<span class="kw">end</span>;
82
83<span class="kw">end</span> Generics.</code></pre>
84<pre class="output"><code>5
85tree
861 1 3 4 5
87</code></pre>
88<p>Each import with its own arguments makes an instance: the module compiled again, with <code>T</code> standing for the type given, and <code>less</code> calling the procedure given. The arguments may name what the importing module declares later, as <code>before</code> is here. There is no run-time cost for being generic, and no boxing: <code>Ints.max</code> is exactly the code you would write by hand for <code>INT</code>.</p>
89<h2>Writing your own</h2>
90<p>A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.</p>
91<div class="file"><span>Stk.raz</span></div><pre class="code"><code><span class="kw">module</span> Stk(T);
92<span class="kw">import</span> Mem;
93<span class="kw">import</span> SYSTEM;
94
95<span class="kw">type</span>
96 Stack* = <span class="kw">record</span>
97 items: ^T; <span class="comment">(* an array of cap Ts *)</span>
98 cap*, len*: <span class="ty">INT</span>;
99 a: ^Mem.Allocator;
100 <span class="kw">end</span>;
101
102<span class="kw">proc</span> init*(s: ^Stack; a: ^Mem.Allocator);
103<span class="kw">begin</span>
104 s^.a := a;
105 s^.items := <span class="ty">nil</span>;
106 s^.cap := <span class="number">0</span>;
107 s^.len := <span class="number">0</span>;
108<span class="kw">end</span>;
109
110<span class="comment">(* v on top; false if the allocator has no room *)</span>
111<span class="kw">proc</span> push*(s: ^Stack; v: T): <span class="ty">BOOL</span>;
112<span class="kw">var</span> bigger: ^T; i, cap: <span class="ty">INT</span>; src, dst: ^T;
113<span class="kw">begin</span>
114 <span class="kw">if</span> s^.len = s^.cap <span class="kw">then</span>
115 <span class="kw">if</span> s^.cap = <span class="number">0</span> <span class="kw">then</span> cap := <span class="number">4</span>; <span class="kw">else</span> cap := s^.cap * <span class="number">2</span>; <span class="kw">end</span>;
116 bigger := ^T(Mem.alloc(s^.a, cap * <span class="ty">SIZE</span>(T), <span class="number">8</span>));
117 <span class="kw">if</span> bigger = <span class="ty">nil</span> <span class="kw">then</span> <span class="kw">return</span> <span class="ty">false</span>; <span class="kw">end</span>;
118 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> s^.len - <span class="number">1</span> <span class="kw">do</span>
119 src := ^T(<span class="ty">INT</span>(s^.items) + i * <span class="ty">SIZE</span>(T));
120 dst := ^T(<span class="ty">INT</span>(bigger) + i * <span class="ty">SIZE</span>(T));
121 dst^ := src^;
122 <span class="kw">end</span>;
123 <span class="kw">if</span> s^.items # <span class="ty">nil</span> <span class="kw">then</span> Mem.free(s^.a, ^<span class="ty">U8</span>(s^.items)); <span class="kw">end</span>;
124 s^.items := bigger;
125 s^.cap := cap;
126 <span class="kw">end</span>;
127 dst := ^T(<span class="ty">INT</span>(s^.items) + s^.len * <span class="ty">SIZE</span>(T));
128 dst^ := v;
129 <span class="ty">INC</span>(s^.len);
130 <span class="kw">return</span> <span class="ty">true</span>;
131<span class="kw">end</span>;
132
133<span class="comment">(* the top value, taken off; false if empty *)</span>
134<span class="kw">proc</span> pop*(s: ^Stack): (T, <span class="ty">BOOL</span>);
135<span class="kw">var</span> z: T; p: ^T;
136<span class="kw">begin</span>
137 <span class="kw">if</span> s^.len = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> z, <span class="ty">false</span>; <span class="kw">end</span>;
138 <span class="ty">DEC</span>(s^.len);
139 p := ^T(<span class="ty">INT</span>(s^.items) + s^.len * <span class="ty">SIZE</span>(T));
140 <span class="kw">return</span> p^, <span class="ty">true</span>;
141<span class="kw">end</span>;
142
143<span class="kw">end</span> Stk.</code></pre>
144<div class="file"><span>Uses.raz</span></div><pre class="code"><code><span class="kw">module</span> Uses;
145<span class="kw">import</span> Out, Heap, Nums := Stk(<span class="ty">INT</span>), Pairs := Stk(Pair);
146<span class="kw">import</span> SYSTEM;
147
148<span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">INT</span>; <span class="kw">end</span>;
149
150<span class="kw">proc</span> main*;
151<span class="kw">var</span> n: Nums.Stack; p: Pairs.Stack; i, v: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>; pr: Pair;
152<span class="kw">begin</span>
153 Nums.init(SYSTEM.<span class="ty">ADR</span>(n), Heap.heap);
154 Pairs.init(SYSTEM.<span class="ty">ADR</span>(p), Heap.heap);
155 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Nums.push(SYSTEM.<span class="ty">ADR</span>(n), i * i); <span class="kw">end</span>;
156 _ := Pairs.push(SYSTEM.<span class="ty">ADR</span>(p), {a: <span class="number">3</span>, b: <span class="number">4</span>});
157 v, ok := Nums.pop(SYSTEM.<span class="ty">ADR</span>(n));
158 Out.int(v); Out.str(<span class="string">&quot; of &quot;</span>); Out.int(n.len + <span class="number">1</span>); Out.ln;
159 pr, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p));
160 Out.int(pr.a * pr.b); Out.ln;
161 _, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p));
162 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">&quot;empty\n&quot;</span>); <span class="kw">end</span>;
163<span class="kw">end</span>;
164
165<span class="kw">end</span> Uses.</code></pre>
166<pre class="output"><code>100 of 10
16712
168empty
169</code></pre>
170<p>Two instances (<code>Nums</code> and <code>Pairs</code>) are two separate modules, compiled with their own <code>T</code>. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.</p>
171<h2>The library's generics</h2>
172<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules. A <code>Map</code> takes its key hash and equality as procedure arguments: <code>import Count := Map([]const U8, INT, Str.hash, Str.eq);</code>. The advanced tour uses each of them in a real program.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">&larr; When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C &rarr;</a></div></article></div>
173</main>
174<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
175</body>
176</html>