summaryrefslogtreecommitdiff
path: root/doc/transfer-reference.html
blob: d96269db56d2e9d681050dc286076af353cfcbd3 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net">
<TITLE>Transfer language reference</TITLE>
</HEAD><BODY BGCOLOR="white" TEXT="black">
<P ALIGN="center"><CENTER><H1>Transfer language reference</H1>
<FONT SIZE="4">
<I>Author: Björn Bringert &lt;bringert@cs.chalmers.se&gt;</I><BR>
Last update: Tue Dec  6 14:26:07 2005
</FONT></CENTER>

<P></P>
<HR NOSHADE SIZE=1>
<P></P>
    <UL>
    <LI><A HREF="#toc1">Layout syntax</A>
    <LI><A HREF="#toc2">Imports</A>
    <LI><A HREF="#toc3">Function declarations</A>
    <LI><A HREF="#toc4">Data type declarations</A>
    <LI><A HREF="#toc5">Lambda expressions</A>
    <LI><A HREF="#toc6">Local definitions</A>
    <LI><A HREF="#toc7">Types</A>
      <UL>
      <LI><A HREF="#function_types">Function types</A>
      <LI><A HREF="#toc9">Basic types</A>
      <LI><A HREF="#toc10">Records</A>
      <LI><A HREF="#toc11">Tuples</A>
      <LI><A HREF="#toc12">Lists</A>
      </UL>
    <LI><A HREF="#toc13">Pattern matching</A>
      <UL>
      <LI><A HREF="#toc14">Constructor patterns</A>
      <LI><A HREF="#toc15">Variable patterns</A>
      <LI><A HREF="#toc16">Wildcard patterns</A>
      <LI><A HREF="#toc17">Record patterns</A>
      <LI><A HREF="#toc18">Disjunctive patterns</A>
      <LI><A HREF="#toc19">List patterns</A>
      <LI><A HREF="#toc20">Tuple patterns</A>
      <LI><A HREF="#toc21">String literal patterns</A>
      <LI><A HREF="#toc22">Integer literal patterns</A>
      </UL>
    <LI><A HREF="#toc23">Meta variables</A>
    <LI><A HREF="#toc24">Type classes</A>
    <LI><A HREF="#toc25">Operators</A>
    <LI><A HREF="#toc26">Compositional functions</A>
    <LI><A HREF="#toc27">do notation</A>
    </UL>

<P></P>
<HR NOSHADE SIZE=1>
<P></P>
<P>
This document describes the features of the Transfer language.
See the <A HREF="transfer-tutorial.html">Transfer tutorial</A>
for an example of a Transfer program, and how to compile and use 
Transfer programs.
</P>
<P>
Transfer is a dependently typed functional programming language 
with eager evaluation.
</P>
<A NAME="toc1"></A>
<H2>Layout syntax</H2>
<P>
Transfer uses layout syntax, where the indentation of a piece of code
determines which syntactic block it belongs to.
</P>
<P>
To give the block structure of a piece of code without using layout
syntax, you can enclose the block in curly braces (<CODE>{ }</CODE>) and 
separate the parts of the blocks with semicolons (<CODE>;</CODE>).
</P>
<P>
For example, this case expression:
</P>
<PRE>
  case x of
         p1 -&gt; e1
         p2 -&gt; e2
</PRE>
<P></P>
<P>
is equivalent to this one:
</P>
<PRE>
  case x of {
         p1 -&gt; e1 ;
         p2 -&gt; e2 
  }
</PRE>
<P></P>
<P>
Here the layout is insignificant, as the structure is given with
braces and semicolons. Thus the above is equivalent to:
</P>
<PRE>
  case x of { p1 -&gt; e1 ; p2 -&gt; e2 }
</PRE>
<P></P>
<A NAME="toc2"></A>
<H2>Imports</H2>
<P>
A Transfer module start with some imports. Most modules will have to
import the prelude, which contains definitons used by most programs:
</P>
<PRE>
  import prelude
</PRE>
<P></P>
<A NAME="toc3"></A>
<H2>Function declarations</H2>
<P>
Functions need to be given a type and a definition. The type is given
by a typing judgement on the form:
</P>
<PRE>
  f : T
</PRE>
<P></P>
<P>
where <CODE>f</CODE> is the function's name, and <CODE>T</CODE> its type. See 
<A HREF="#function_types">Function types</A> for a how the types of functions
are written.
</P>
<P>
The definition of the function is the given as a sequence of pattern
equations. The first equation whose patterns match the function arguments
is used when the function is called. Pattern equations are on the form:
</P>
<PRE>
  f p1 ... p1n = exp
  ...
  f qn1 ... qnm = exp
</PRE>
<P></P>
<A NAME="toc4"></A>
<H2>Data type declarations</H2>
<P>
Transfer supports Generalized Algebraic Datatypes.
They are declared thusly:
</P>
<PRE>
  data D : T where 
       c1 : Tc1
       ...
       cn : Tcn
</PRE>
<P></P>
<P>
Here <CODE>D</CODE> is the name of the data type, <CODE>T</CODE> is the type of the type
constructor, <CODE>c1</CODE> to <CODE>cn</CODE> are the data constructor names, and
<CODE>Tc1</CODE> to <CODE>Tcn</CODE> are their types. 
</P>
<A NAME="toc5"></A>
<H2>Lambda expressions</H2>
<P>
<I>Lambda expressions</I> are terms which express functions, without
giving names to them. For example:
</P>
<PRE>
  \x -&gt; x + 1
</PRE>
<P></P>
<P>
is the function which takes an argument, and returns the value of the 
argument + 1.
</P>
<A NAME="toc6"></A>
<H2>Local definitions</H2>
<P>
To give local definition to some names, use:
</P>
<PRE>
  let x1 : T1 = exp1
      ...
      xn : Tn = expn
   in exp
</PRE>
<P></P>
<A NAME="toc7"></A>
<H2>Types</H2>
<A NAME="function_types"></A>
<H3>Function types</H3>
<P>
Functions types are of the form:
</P>
<PRE>
  A -&gt; B
</PRE>
<P></P>
<P>
This is the type of functions which take an argument of type 
<CODE>A</CODE> and returns a result of type <CODE>B</CODE>.
</P>
<P>
To write functions which take more than one argument, we use <I>currying</I>.
A function which takes n arguments is a function which takes 1 
argument and returns a function which takes n-1 arguments. Thus,
</P>
<PRE>
  A -&gt; (B -&gt; C)
</PRE>
<P></P>
<P>
or, equivalently, since <CODE>-&gt;</CODE> associates to the right:
</P>
<PRE>
  A -&gt; B -&gt; C
</PRE>
<P></P>
<P>
is the type of functions which take 2 arguments, the first of type 
<CODE>A</CODE> and the second of type <CODE>B</CODE>. This arrangement lets us do
<I>partial application</I> of function to fewer arguments than the function 
is declared to take, returning a new function which takes the rest 
of the arguments.
</P>
<H4>Dependent function types</H4>
<P>
In a function type, the value of an argument can be used later 
in the type. Such dependent function types are written:
</P>
<PRE>
  (x1 : T1) -&gt; ... -&gt; (xn : Tn) -&gt; T
</PRE>
<P></P>
<P>
Here, <CODE>x1</CODE> can be used in <CODE>T2</CODE> to <CODE>Tn</CODE>, <CODE>x1</CODE> can be used 
in <CODE>T2</CODE> to <CODE>Tn</CODE>
</P>
<A NAME="toc9"></A>
<H3>Basic types</H3>
<H4>Integers</H4>
<P>
The type of integers is called <CODE>Integer</CODE>. 
standard decmial integer literals are used to represent values of this type.
</P>
<H4>Floating-point numbers</H4>
<P>
The only currently supported floating-point type is <CODE>Double</CODE>, which supports
IEEE-754 double-precision floating-point numbers. Double literals are written
in decimal notation, e.g. <CODE>123.456</CODE>.
</P>
<H4>Strings</H4>
<P>
There is a primitive <CODE>String</CODE> type. This might be replaced by a list of 
characters representation in the future. String literals are written 
with double quotes, e.g. <CODE>"this is a string"</CODE>.
</P>
<H4>Booleans</H4>
<P>
Booleans are not a built-in type, though some features of the Transfer language
depend on them.
</P>
<PRE>
  data Bool : Type where
          True : Bool
          False : Bool
</PRE>
<P></P>
<P>
In addition to normal pattern matching on booleans, you can use the built-in
if-expression:
</P>
<PRE>
  if exp1 then exp2 else exp3
</PRE>
<P></P>
<P>
where <CODE>exp1</CODE> must be an expression of type <CODE>Bool</CODE>.
</P>
<A NAME="toc10"></A>
<H3>Records</H3>
<P>
Record types are created by using a <CODE>sig</CODE> expression:
</P>
<PRE>
  sig { p1 : T1; ... ; pn : Tn }
</PRE>
<P></P>
<P>
Here, <CODE>p1</CODE> to <CODE>pn</CODE> are the field labels and <CODE>T1</CODE> to <CODE>Tn</CODE> are their types.
</P>
<P>
Record values are constructed using <CODE>rec</CODE> expressions:
</P>
<PRE>
  rec { p1 = exp1; ... ; pn = expn }
</PRE>
<P></P>
<P>
The curly braces and semicolons are simply explicit layout syntax, so 
the record type and record expression above can also be written as:
</P>
<PRE>
  sig p1 : T1
      pn : Tn
</PRE>
<P></P>
<PRE>
  rec p1 = exp1
      pn = expn
</PRE>
<P></P>
<H4>Record subtyping</H4>
<P>
A record of some type R1 can be used as a record of any type R2
such that for every field <CODE>p1 : T1</CODE> in R2, <CODE>p1 : T1</CODE> is also a 
field of T1.
</P>
<A NAME="toc11"></A>
<H3>Tuples</H3>
<P>
Tuples on the form:
</P>
<PRE>
  (exp1, ..., expn)
</PRE>
<P></P>
<P>
are syntactic sugar for records with fields <CODE>p1</CODE> to <CODE>pn</CODE>. The expression
above is equivalent to:
</P>
<PRE>
  rec { p1 = exp1; ... ; pn = expn }
</PRE>
<P></P>
<A NAME="toc12"></A>
<H3>Lists</H3>
<P>
The <CODE>List</CODE> type is not built-in, though there is some special syntax for it.
The list type is declared as:
</P>
<PRE>
  data List : Type -&gt; Type where 
  	Nil : (A:Type) -&gt; List A
          Cons : (A:Type) -&gt; A -&gt; List A -&gt; List A
</PRE>
<P></P>
<P>
The empty lists can be written as <CODE>[]</CODE>. There is a operator <CODE>::</CODE> which can 
be used instead of <CODE>Cons</CODE>. These are just syntactic sugar for expressions
using <CODE>Nil</CODE> and <CODE>Cons</CODE>, with the type arguments hidden.
</P>
<A NAME="toc13"></A>
<H2>Pattern matching</H2>
<P>
Pattern matching is done in pattern equations and by using the 
<CODE>case</CODE> construct:
</P>
<PRE>
  case exp of
       p1 | guard1 -&gt; rhs1
       ...
       pn | guardn -&gt; rhsn
</PRE>
<P></P>
<P>
<CODE>guard1</CODE> to <CODE>guardn</CODE> are boolean expressions. Case arms can also be written 
without guards, such as:
</P>
<PRE>
       pk -&gt; rhsk
</PRE>
<P></P>
<P>
This is the same as writing:
</P>
<PRE>
       pk | True -&gt; rhsk
</PRE>
<P></P>
<P>
The syntax of patterns are decribed below.
</P>
<A NAME="toc14"></A>
<H3>Constructor patterns</H3>
<P>
Constructor patterns are written as:
</P>
<PRE>
  C p1 ... pn
</PRE>
<P></P>
<P>
where <CODE>C</CODE> is a data constructor which takes <CODE>n</CODE> arguments.
If the value to be matched is the constructor <CODE>C</CODE> applied to 
arguments <CODE>v1</CODE> to <CODE>vn</CODE>, then <CODE>v1</CODE> to <CODE>vn</CODE> will be matched
against <CODE>p1</CODE> to <CODE>pn</CODE>.
</P>
<A NAME="toc15"></A>
<H3>Variable patterns</H3>
<P>
A variable pattern is a single identifier:
</P>
<PRE>
  x
</PRE>
<P></P>
<P>
A variable pattern matches any value, and binds the variable name to the
value. A variable may not occur more than once in a pattern.
</P>
<A NAME="toc16"></A>
<H3>Wildcard patterns</H3>
<P>
Wildcard patterns are written as with a single underscore:
</P>
<PRE>
  _
</PRE>
<P></P>
<P>
Wildcard patterns match all values and bind no variables.
</P>
<A NAME="toc17"></A>
<H3>Record patterns</H3>
<P>
Record patterns match record values:
</P>
<PRE>
  rec { l1 = p1; ... ; ln = pn }
</PRE>
<P></P>
<P>
A record value matches a record pattern, if the record value has all the 
fields <CODE>l1</CODE> to <CODE>ln</CODE>, and their values match <CODE>p1</CODE> to <CODE>pn</CODE>.
</P>
<P>
Note that a record value may have more fields than the record pattern and 
they will still match.
</P>
<A NAME="toc18"></A>
<H3>Disjunctive patterns</H3>
<P>
It is possible to write a pattern on the form:
</P>
<PRE>
  p1 || ... || pn
</PRE>
<P></P>
<P>
A value will match this pattern if it matches any of the patterns <CODE>p1</CODE> to <CODE>pn</CODE>.
FIXME: talk about how this is expanded
</P>
<A NAME="toc19"></A>
<H3>List patterns</H3>
<A NAME="toc20"></A>
<H3>Tuple patterns</H3>
<P>
Tuples patterns on the form:
</P>
<PRE>
  (p1, ... , pn)
</PRE>
<P></P>
<P>
are syntactic sugar for record patterns, in the same way as tuple expressions.
</P>
<A NAME="toc21"></A>
<H3>String literal patterns</H3>
<P>
String literals can be used as patterns.
</P>
<A NAME="toc22"></A>
<H3>Integer literal patterns</H3>
<P>
Integer literals can be used as patterns.
</P>
<A NAME="toc23"></A>
<H2>Meta variables</H2>
<A NAME="toc24"></A>
<H2>Type classes</H2>
<A NAME="toc25"></A>
<H2>Operators</H2>
<A NAME="toc26"></A>
<H2>Compositional functions</H2>
<A NAME="toc27"></A>
<H2>do notation</H2>

<!-- html code generated by txt2tags 2.0 (http://txt2tags.sf.net) -->
<!-- cmdline: txt2tags darcs.txt transfer-reference.txt transfer-tutorial.txt transfer.txt -->
</BODY></HTML>