2 <title>Other Haskell utility programs</title>
3 <indexterm><primary>utilities, Haskell</primary></indexterm>
5 <para>This section describes other program(s) which we distribute,
6 that help with the Great Haskell Programming Task.</para>
8 <!-- comment: hstags doesn't work anymore
11 <title>Emacs `TAGS' for Haskell: <command>hstags</command></title>
12 <indexterm><primary><command>hstags</command></primary></indexterm>
13 <indexterm><primary>TAGS for Haskell</primary></indexterm>
15 <para>`Tags' is a facility for indexing the definitions of
16 programming-language things in a multi-file program, and then
17 using that index to jump around among these definitions.</para>
19 <para>Rather than scratch your head, saying “Now where did
20 we define `foo'?”, you just do (in Emacs) <Literal>M-. foo
21 RET</Literal>, and You're There! Some people go wild over this
24 <para>GHC comes with a program <command>hstags</command>, which
25 build Emacs-able TAGS files. The invocation syntax is:</para>
28 hstags [GHC-options] file [files...]
31 <para>The best thing is just to feed it your GHC command-line
32 flags. A good Makefile entry might be:</para>
37 hstags $(GHC_FLAGS) *.lhs
40 <para>The only flags of its own are: <Option>-v</Option> to be
41 verbose; <Option>-a</Option> to <Emphasis>APPEND</Emphasis> to the
42 TAGS file, rather than write to it.</para>
44 <para>Shortcomings: (1) Instance declarations don't get into
45 the TAGS file (but the definitions inside them do); as instances
46 aren't named, this is probably just as well.
47 (2) Data-constructor definitions don't get in. Go for the
48 corresponding type constructor instead.</para>
50 <para>Actually, GHC also comes with <command>etags</command>
51 [for C], and <Command>perltags</Command> [for You
52 Know What]. And—I cannot tell a lie—there is
53 Denis Howe's <Command>fptags</Command> [for Haskell,
54 etc.] in the <Filename>ghc/CONTRIB</Filename>
55 section…)</para>
61 <title>“Yacc for Haskell”: <command>happy</command></title>
63 <indexterm><primary>Happy</primary></indexterm>
64 <indexterm><primary>Yacc for Haskell</primary></indexterm>
65 <indexterm><primary>parser generator for Haskell</primary></indexterm>
67 <para>Andy Gill and Simon Marlow have written a parser-generator
69 <Command>happy</Command>.<IndexTerm><Primary>happy parser
70 generator</Primary></IndexTerm> <command>Happy</command> is to
71 Haskell what <command>Yacc</command> is to C.</para>
73 <para>You can get <Command>happy</Command> from <ulink
74 url="http://www.haskell.org/happy/">the Happy
75 Homepage</ulink>.</para>
77 <para><command>Happy</command> is at its shining best when
78 compiled by GHC.</para>
82 <!-- we don't distribute this anymore
84 <title>Pretty-printing Haskell: <Command>pphs</Command></title>
85 <indexterm><primary>pphs</primary></indexterm>
86 <indexterm><primary>pretty-printing Haskell code</primary></indexterm>
88 <para>Andrew Preece has written
89 <Command>pphs</Command>,<IndexTerm><Primary>pphs</Primary></IndexTerm><IndexTerm><Primary>pretty-printing
90 Haskell</Primary></IndexTerm> a utility to pretty-print Haskell
91 code in LaTeX documents. Keywords in bolds, variables in
92 italics—that sort of thing. It is good at lining up program
93 clauses and equals signs, things that are very tiresome to do by
96 <para>The code is distributed with GHC in
97 <Filename>ghc/CONTRIB/pphs</Filename>.</para>
102 <title>Writing Haskell interfaces to C code:
103 <command>hsc2hs</command></title>
104 <indexterm><primary><command>hsc2hs</command></primary>
107 <para>The <command>hsc2hs</command> command can be used to automate
108 some parts of the process of writing Haskell bindings to C code.
109 It reads an almost-Haskell source with embedded special
110 constructs, and outputs a real Haskell file with these constructs
111 processed, based on information taken from some C headers. The
112 extra constructs deal with accessing C data from Haskell.</para>
114 <para>It may also output a C file which contains additional C
115 functions to be linked into the program, together with a C header
116 that gets included into the C code to which the Haskell module
117 will be compiled (when compiled via C) and into the C file. These
118 two files are created when the <literal>#def</literal> construct
119 is used (see below).</para>
121 <para>Actually <command>hsc2hs</command> does not output the Haskell
122 file directly. It creates a C program that includes the headers,
123 gets automatically compiled and run. That program outputs the
126 <para>In the following, “Haskell file” is the main
127 output (usually a <literal>.hs</literal> file), “compiled
128 Haskell file” is the Haskell file after
129 <command>ghc</command> has compiled it to C (i.e. a
130 <literal>.hc</literal> file), “C program” is the
131 program that outputs the Haskell file, “C file” is the
132 optionally generated C file, and “C header” is its
136 <title>Command line syntax</title>
138 <para><command>hsc2hs</command> takes input files as arguments,
139 and flags that modify its behavior:</para>
143 <term><literal>-t FILE</literal> or
144 <literal>--template=FILE</literal></term>
146 <para>The template file (see below).</para>
151 <term><literal>-c PROG</literal> or
152 <literal>--cc=PROG</literal></term>
154 <para>The C compiler to use (default:
155 <command>ghc</command>)</para>
160 <term><literal>-l PROG</literal> or
161 <literal>--ld=PROG</literal></term>
163 <para>The linker to use (default:
164 <command>gcc</command>).</para>
169 <term><literal>-C FLAG</literal> or
170 <literal>--cflag=FLAG</literal></term>
172 <para>An extra flag to pass to the C compiler.</para>
177 <term><literal>-I DIR</literal></term>
179 <para>Passed to the C compiler.</para>
184 <term><literal>-L FLAG</literal> or
185 <literal>--lflag=FLAG</literal></term>
187 <para>An extra flag to pass to the linker.</para>
192 <term><literal>-i FILE</literal> or
193 <literal>--include=FILE</literal></term>
195 <para>As if the appropriate <literal>#include</literal>
196 directive was placed in the source.</para>
201 <term><literal>-D NAME[=VALUE]</literal> or
202 <literal>--define=NAME[=VALUE]</literal></term>
204 <para>As if the appropriate <literal>#define</literal>
205 directive was placed in the source.</para>
210 <term><literal>-o FILE</literal> or
211 <literal>--output=FILE</literal></term>
213 <para>Name of the Haskell file.</para>
218 <term><literal>--help</literal></term>
220 <para>Display a summary of the available flags.</para>
225 <term><literal>--version</literal></term>
227 <para>Output version information.</para>
232 <para>The input file should end with .hsc. Output files by
233 default get names with the <literal>.hsc</literal> suffix
240 <entry><literal>.hs</literal></entry>
241 <entry>Haskell file</entry>
244 <entry><literal>_hsc.h</literal></entry>
245 <entry>C header</entry>
248 <entry><literal>_hsc.c</literal></entry>
249 <entry>C file</entry>
255 <para>The C program is compiled using the Haskell compiler. This
256 provides the include path to <filename>HsFFI.h</filename> which
257 is automatically included into the C program.</para>
260 <sect2><title>Input syntax</title>
262 <para>All special processing is triggered by
263 the <literal>#</literal> operator. To output
264 a literal <literal>#</literal>, write it twice:
265 <literal>##</literal>. Inside string literals and comments
266 <literal>#</literal> characters are not processed.</para>
268 <para>A <literal>#</literal> is followed by optional
269 spaces and tabs, an alphanumeric keyword that describes
270 the kind of processing, and its arguments. Arguments look
271 like C expressions separated by commas (they are not
272 written inside parens). They extend up to the nearest
273 unmatched <literal>)</literal>, <literal>]</literal> or
274 <literal>}</literal>, or to the end of line if it occurs outside
275 any <literal>() [] {} '' "" /**/</literal> and is not preceded
276 by a backslash. Backslash-newline pairs are stripped.</para>
278 <para>In addition <literal>#{stuff}</literal> is equivalent
279 to <literal>#stuff</literal> except that it's self-delimited
280 and thus needs not to be placed at the end of line or in some
283 <para>Meanings of specific keywords:</para>
288 <term><literal>#include <file.h></literal></term>
289 <term><literal>#include "file.h"</literal></term>
291 <para>The specified file gets included into the C program,
292 the compiled Haskell file, and the C header.
293 <literal><HsFFI.h></literal> is included
294 automatically.</para>
299 <term><literal>#define name</literal></term>
300 <term><literal>#define name value</literal></term>
301 <term><literal>#undef name</literal></term>
303 <para>Similar to <literal>#include</literal>. Note that
304 <literal>#includes</literal> and
305 <literal>#defines</literal> may be put in the same file
306 twice so they should not assume otherwise.</para>
311 <term><literal>#let name parameters = "definition"</literal></term>
313 <para>Defines a macro to be applied to the Haskell
314 source. Parameter names are comma-separated, not
315 inside parens. Such macro is invoked as other
316 <literal>#</literal>-constructs, starting with
317 <literal>#name</literal>. The definition will be
318 put in the C program inside parens as arguments of
319 <literal>printf</literal>. To refer to a parameter,
320 close the quote, put a parameter name and open the
321 quote again, to let C string literals concatenate.
322 Or use <literal>printf</literal>'s format directives.
323 Values of arguments must be given as strings, unless the
324 macro stringifies them itself using the C preprocessor's
325 <literal>#parameter</literal> syntax.</para>
330 <term><literal>#def C_definition</literal></term>
332 <para>The definition (of a function, variable, struct or
333 typedef) is written to the C file, and its prototype or
334 extern declaration to the C header. Inline functions are
335 handled correctly. struct definitions and typedefs are
336 written to the C program too. The
337 <literal>inline</literal>, <literal>struct</literal> or
338 <literal>typedef</literal> keyword must come just after
339 <literal>def</literal>.</para>
344 <term><literal>#if condition</literal></term>
345 <term><literal>#ifdef name</literal></term>
346 <term><literal>#ifndef name</literal></term>
347 <term><literal>#elif condition</literal></term>
348 <term><literal>#else</literal></term>
349 <term><literal>#endif</literal></term>
350 <term><literal>#error message</literal></term>
351 <term><literal>#warning message</literal></term>
353 <para>Conditional compilation directives are passed
354 unmodified to the C program, C file, and C header. Putting
355 them in the C program means that appropriate parts of the
356 Haskell file will be skipped.</para>
361 <term><literal>#const C_expression</literal></term>
363 <para>The expression must be convertible to
364 <literal>long</literal> or <literal>unsigned
365 long</literal>. Its value (literal or negated literal)
366 will be output.</para>
371 <term><literal>#const_str C_expression</literal></term>
373 <para>The expression must be convertible to const char
374 pointer. Its value (string literal) will be output.</para>
379 <term><literal>#type C_type</literal></term>
381 <para>A Haskell equivalent of the C numeric type will be
382 output. It will be one of
383 <literal>{Int,Word}{8,16,32,64}</literal>,
384 <literal>Float</literal>, <literal>Double</literal>,
385 <literal>LDouble</literal>.</para>
390 <term><literal>#peek struct_type, field</literal></term>
392 <para>A function that peeks a field of a C struct will be
393 output. It will have the type
394 <literal>Storable b => Ptr a -> IO b</literal>.
396 The intention is that <literal>#peek</literal> and
397 <literal>#poke</literal> can be used for implementing the
398 operations of class <literal>Storable</literal> for a
399 given C struct (see <xref linkend="sec-Storable">).</para>
404 <term><literal>#poke struct_type, field</literal></term>
406 <para>Similarly for poke. It will have the type
407 <literal>Storable b => Ptr a -> b -> IO ()</literal>.</para>
412 <term><literal>#ptr struct_type, field</literal></term>
414 <para>Makes a pointer to a field struct. It will have the type
415 <literal>Ptr a -> Ptr b</literal>.</para>
420 <term><literal>#enum type, constructor, value, value, ...</literal></term>
422 <para>A shortcut for multiple definitions which use
423 <literal>#const</literal>. Each <literal>value</literal>
424 is a name of a C integer constant, e.g. enumeration value.
425 The name will be translated to Haskell by making each
426 letter following an underscore uppercase, making all the rest
427 lowercase, and removing underscores. You can supply a different
428 translation by writing <literal>hs_name = c_value</literal>
429 instead of a <literal>value</literal>, in which case
430 <literal>c_value</literal> may be an arbitrary expression.
431 The <literal>hs_name</literal> will be defined as having the
432 specified <literal>type</literal>. Its definition is the specified
433 <literal>constructor</literal> (which in fact may be an expression
434 or be empty) applied to the appropriate integer value. You can
435 have multiple <literal>#enum</literal> definitions with the same
436 <literal>type</literal>; this construct does not emit the type
437 definition itself.</para>
445 <title>Custom constructs</title>
447 <para><literal>#const</literal>, <literal>#type</literal>,
448 <literal>#peek</literal>, <literal>#poke</literal> and
449 <literal>#ptr</literal> are not hardwired into the
450 <command>hsc2hs</command>, but are defined in a C template that is
451 included in the C program: <filename>template-hsc.h</filename>.
452 Custom constructs and templates can be used too. Any
453 <literal>#</literal>-construct with unknown key is expected to
454 be handled by a C template.</para>
456 <para>A C template should define a macro or function with name
457 prefixed by <literal>hsc_</literal> that handles the construct
458 by emitting the expansion to stdout. See
459 <filename>template-hsc.h</filename> for examples.</para>
461 <para>Such macros can also be defined directly in the
462 source. They are useful for making a <literal>#let</literal>-like
463 macro whose expansion uses other <literal>#let</literal> macros.
464 Plain <literal>#let</literal> prepends <literal>hsc_</literal>
465 to the macro name and wraps the defininition in a
466 <literal>printf</literal> call.</para>
475 ;;; Local Variables: ***
477 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***