File: pragmas.html

package info (click to toggle)
haskell-doc 19991028-1
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 1,672 kB
  • ctags: 620
  • sloc: haskell: 2,123; makefile: 158; sh: 31
file content (87 lines) | stat: -rw-r--r-- 4,685 bytes parent folder | download
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

<title>The Haskell 98 Report: Pragmas</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Report</i><br> <a href="index.html">top</a> | <a href="derived.html">back</a> | <a href="haskell.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><hr>
<a name="pragmas"></a><a name="sectE"></a>
<h2>E<tt>&nbsp;&nbsp;</tt>Compiler Pragmas</h2>
<p>
Some compiler implementations support compiler <I>pragmas</I>, which are
used to give additional instructions or hints to the compiler, but which
do not form part of the Haskell  language proper and do not change a
program's semantics.  This
section summarizes this existing practice.  An implementation is not
required to respect any pragma, but the pragma should be ignored if an
implementation is not prepared to handle it.  
Lexically, pragmas appear as comments, except that the enclosing
syntax is <tt>{-#</tt> <tt>#-}</tt>.<p>
<a name="sectE.1"></a>
<h3>E.1<tt>&nbsp;&nbsp;</tt>Inlining</h3>

<table cellspacing=0 cellspacing=0>
<tr><td width=100>
decl </td><td width=20>  <tt>-&gt;</tt> </td><td width=250> <tt>{-#</tt> <tt>inline</tt> [digit] qvars <tt>#-}
</tt></td></tr><tr><td>
decl </td><td>  <tt>-&gt;</tt> </td><td> <tt>{-#</tt> <tt>notInline</tt> qvars <tt>#-}
</tt></td></tr></table>
The optional digit represents the level of optimization at which the
inlining is to occur.  If omitted, it is assumed to be 0.  A compiler
may use a numeric optimization level setting in which increasing level
numbers indicate increasing amounts of optimization.  Trivial
inlinings that have no 
impact on compilation time or code size should have an optimization
level of 0; more complex inlinings that may lead to slow compilation
or large executables should be associated with higher optimization levels.<p>
Compilers will often automatically inline simple expressions.  This
may be prevented by the <tt>notInline</tt> pragma.<p>
<a name="sectE.2"></a>
<h3>E.2<tt>&nbsp;&nbsp;</tt>Specialization</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
decl </td><td width=20>  <tt>-&gt;</tt> </td><td width=250> <tt>{-#</tt> <tt>specialize</tt> spec<sub>1</sub> <tt>,</tt> ... <tt>,</tt> spec<sub>k</sub> <tt>#-}</tt> </td><td> (k&gt;=1)
</td></tr><tr><td>
spec </td><td>  <tt>-&gt;</tt> </td><td> vars :: type
</td></tr></table>
Specialization is used to avoid inefficiencies involved in dispatching
overloaded functions.  For example, in
<tt><br>

<br>
factorial&nbsp;::&nbsp;Num&nbsp;a&nbsp;=&gt;&nbsp;a&nbsp;-&gt;&nbsp;a<br>
factorial&nbsp;0&nbsp;=&nbsp;0<br>
factorial&nbsp;n&nbsp;=&nbsp;n&nbsp;*&nbsp;factorial&nbsp;(n-1)<br>
{-#&nbsp;specialize&nbsp;factorial&nbsp;::&nbsp;Int&nbsp;-&gt;&nbsp;Int,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factorial&nbsp;::&nbsp;Integer&nbsp;-&gt;&nbsp;Integer&nbsp;#-}<br>

<br>

</tt>calls to <tt>factorial</tt> in which the compiler can detect that the
parameter is either <tt>Int</tt> or <tt>Integer</tt> will
use specialized versions of <tt>factorial</tt> which do not involve
overloaded numeric operations.<p>
<a name="sectE.3"></a>
<h3>E.3<tt>&nbsp;&nbsp;</tt>Optimization</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
decl </td><td width=20>  <tt>-&gt;</tt> </td><td width=250> optdecl
</td></tr><tr><td>
exp<sup>0</sup> </td><td>  <tt>-&gt;</tt> </td><td> optdecl exp<sup>0</sup>
</td></tr><tr><td>
optdecl </td><td>  <tt>-&gt;</tt> </td><td> <tt>{-#</tt> <tt>optimize</tt> optd<sub>1</sub> <tt>,</tt> ... <tt>,</tt> optd<sub>k</sub> <tt>#-}</tt> </td><td> (k &gt;= 1)
</td></tr><tr><td>
optd </td><td>  <tt>-&gt;</tt> </td><td> digit
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>speed</tt> digit
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>space</tt> digit 
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>compilationSpeed</tt> digit
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>debug</tt> digit
</td></tr></table>
The <tt>optimize</tt> pragma
provides  explicit control over the optimization levels
of the compiler.  If used as a declaration, this applies to all values
defined in the declaration group (and recursively to any nested
values).  Used as an expression, it applies only to the prefixed
expression.  If no attribute is named, the <tt>speed</tt> attribute is
assumed.<p>
<hr><i>The Haskell 98 Report</i><br><a href="index.html">top</a> | <a href="derived.html">back</a> | <a href="haskell.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><font size=2>1 February, 1999</font>