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: hasktags documentation losely based on that for hstags -->
10 <sect1 id ="hasktags">
11 <title>Ctags and Etags for Haskell: <command>hasktags</command></title>
12 <indexterm><primary><command>hasktags</command></primary></indexterm>
13 <indexterm><primary>CTAGS for Haskell</primary></indexterm>
15 <para><command>hasktags</command> is a very simple Haskell program that produces ctags "tags" and etags "TAGS" files for Haskell programs.</para>
17 <para>When loaded into an editor such an NEdit, Vim, or Emacs, this allows one to easily navigate around a multi-file program, finding definitions of functions, types, and constructors.</para>
19 <para>Invocation Syntax:</para>
25 <para>This will read all the files listed in <option>files</option> and produce a ctags "tags" file and an etags "TAGS" file in the current directory.</para>
27 <para>Example usage</para>
30 find -name \*.\*hs | xargs hasktags
33 <para>This will find all haskell source files in the current directory and below, and create tags files indexing them in the current directory.</para>
35 <para><command>hasktags</command> is a simple program that uses simple
36 parsing rules to find definitions of functions, constructors, and types. It isn't guranteed to find everything, and will sometimes create false index entries, but it usually gets the job done fairly well. In particular, at present, functions are only indexed if a type signature is given for them.</para>
38 <para>Before hasktags, there used to be <command>fptags</command> and <command>hstags</command>, which did essentially the same job, however neither of these seem to be maintained any more.</para>
41 <title>Using tags with your editor</title>
43 <para>With NEdit, load the "tags" file using "File/Load Tags File". Use "Ctrl-D" to search for a tag.</para>
45 <para>With XEmacs, load the "TAGS" file using "visit-tags-table". Use "M-." to search for a tag.</para>
52 <!-- comment: hstags doesn't work anymore
55 <title>Emacs `TAGS' for Haskell: <command>hstags</command></title>
56 <indexterm><primary><command>hstags</command></primary></indexterm>
57 <indexterm><primary>TAGS for Haskell</primary></indexterm>
59 <para>`Tags' is a facility for indexing the definitions of
60 programming-language things in a multi-file program, and then
61 using that index to jump around among these definitions.</para>
63 <para>Rather than scratch your head, saying “Now where did
64 we define `foo'?”, you just do (in Emacs) <Literal>M-. foo
65 RET</Literal>, and You're There! Some people go wild over this
68 <para>GHC comes with a program <command>hstags</command>, which
69 build Emacs-able TAGS files. The invocation syntax is:</para>
72 hstags [GHC-options] file [files...]
75 <para>The best thing is just to feed it your GHC command-line
76 flags. A good Makefile entry might be:</para>
81 hstags $(GHC_FLAGS) *.lhs
84 <para>The only flags of its own are: <Option>-v</Option> to be
85 verbose; <Option>-a</Option> to <Emphasis>APPEND</Emphasis> to the
86 TAGS file, rather than write to it.</para>
88 <para>Shortcomings: (1) Instance declarations don't get into
89 the TAGS file (but the definitions inside them do); as instances
90 aren't named, this is probably just as well.
91 (2) Data-constructor definitions don't get in. Go for the
92 corresponding type constructor instead.</para>
94 <para>Actually, GHC also comes with <command>etags</command>
95 [for C], and <Command>perltags</Command> [for You
96 Know What]. And—I cannot tell a lie—there is
97 Denis Howe's <Command>fptags</Command> [for Haskell,
98 etc.] in the <Filename>ghc/CONTRIB</Filename>
99 section…)</para>
105 <title>“Yacc for Haskell”: <command>happy</command></title>
107 <indexterm><primary>Happy</primary></indexterm>
108 <indexterm><primary>Yacc for Haskell</primary></indexterm>
109 <indexterm><primary>parser generator for Haskell</primary></indexterm>
111 <para>Andy Gill and Simon Marlow have written a parser-generator
113 <Command>happy</Command>.<IndexTerm><Primary>happy parser
114 generator</Primary></IndexTerm> <command>Happy</command> is to
115 Haskell what <command>Yacc</command> is to C.</para>
117 <para>You can get <Command>happy</Command> from <ulink
118 url="http://www.haskell.org/happy/">the Happy
119 Homepage</ulink>.</para>
121 <para><command>Happy</command> is at its shining best when
122 compiled by GHC.</para>
126 <!-- we don't distribute this anymore
128 <title>Pretty-printing Haskell: <Command>pphs</Command></title>
129 <indexterm><primary>pphs</primary></indexterm>
130 <indexterm><primary>pretty-printing Haskell code</primary></indexterm>
132 <para>Andrew Preece has written
133 <Command>pphs</Command>,<IndexTerm><Primary>pphs</Primary></IndexTerm><IndexTerm><Primary>pretty-printing
134 Haskell</Primary></IndexTerm> a utility to pretty-print Haskell
135 code in LaTeX documents. Keywords in bolds, variables in
136 italics—that sort of thing. It is good at lining up program
137 clauses and equals signs, things that are very tiresome to do by
140 <para>The code is distributed with GHC in
141 <Filename>ghc/CONTRIB/pphs</Filename>.</para>
146 <title>Writing Haskell interfaces to C code:
147 <command>hsc2hs</command></title>
148 <indexterm><primary><command>hsc2hs</command></primary>
151 <para>The <command>hsc2hs</command> command can be used to automate
152 some parts of the process of writing Haskell bindings to C code.
153 It reads an almost-Haskell source with embedded special
154 constructs, and outputs a real Haskell file with these constructs
155 processed, based on information taken from some C headers. The
156 extra constructs deal with accessing C data from Haskell.</para>
158 <para>It may also output a C file which contains additional C
159 functions to be linked into the program, together with a C header
160 that gets included into the C code to which the Haskell module
161 will be compiled (when compiled via C) and into the C file. These
162 two files are created when the <literal>#def</literal> construct
163 is used (see below).</para>
165 <para>Actually <command>hsc2hs</command> does not output the Haskell
166 file directly. It creates a C program that includes the headers,
167 gets automatically compiled and run. That program outputs the
170 <para>In the following, “Haskell file” is the main
171 output (usually a <literal>.hs</literal> file), “compiled
172 Haskell file” is the Haskell file after
173 <command>ghc</command> has compiled it to C (i.e. a
174 <literal>.hc</literal> file), “C program” is the
175 program that outputs the Haskell file, “C file” is the
176 optionally generated C file, and “C header” is its
180 <title>Command line syntax</title>
182 <para><command>hsc2hs</command> takes input files as arguments,
183 and flags that modify its behavior:</para>
187 <term><literal>-t FILE</literal> or
188 <literal>--template=FILE</literal></term>
190 <para>The template file (see below).</para>
195 <term><literal>-c PROG</literal> or
196 <literal>--cc=PROG</literal></term>
198 <para>The C compiler to use (default:
199 <command>ghc</command>)</para>
204 <term><literal>-l PROG</literal> or
205 <literal>--ld=PROG</literal></term>
207 <para>The linker to use (default:
208 <command>gcc</command>).</para>
213 <term><literal>-C FLAG</literal> or
214 <literal>--cflag=FLAG</literal></term>
216 <para>An extra flag to pass to the C compiler.</para>
221 <term><literal>-I DIR</literal></term>
223 <para>Passed to the C compiler.</para>
228 <term><literal>-L FLAG</literal> or
229 <literal>--lflag=FLAG</literal></term>
231 <para>An extra flag to pass to the linker.</para>
236 <term><literal>-i FILE</literal> or
237 <literal>--include=FILE</literal></term>
239 <para>As if the appropriate <literal>#include</literal>
240 directive was placed in the source.</para>
245 <term><literal>-D NAME[=VALUE]</literal> or
246 <literal>--define=NAME[=VALUE]</literal></term>
248 <para>As if the appropriate <literal>#define</literal>
249 directive was placed in the source.</para>
254 <term><literal>-o FILE</literal> or
255 <literal>--output=FILE</literal></term>
257 <para>Name of the Haskell file.</para>
262 <term><literal>--help</literal></term>
264 <para>Display a summary of the available flags.</para>
269 <term><literal>--version</literal></term>
271 <para>Output version information.</para>
276 <term><literal>--no-compile</literal></term>
278 <para>Stop after writing out the intermediate C program to disk.
279 The file name for the intermediate C program is the input file name
280 with <literal>.hsc</literal> replaced with <literal>_hsc_make.c</literal>.</para>
285 <para>The input file should end with .hsc. Output files by
286 default get names with the <literal>.hsc</literal> suffix
293 <entry><literal>.hs</literal></entry>
294 <entry>Haskell file</entry>
297 <entry><literal>_hsc.h</literal></entry>
298 <entry>C header</entry>
301 <entry><literal>_hsc.c</literal></entry>
302 <entry>C file</entry>
308 <para>The C program is compiled using the Haskell compiler. This
309 provides the include path to <filename>HsFFI.h</filename> which
310 is automatically included into the C program.</para>
313 <sect2><title>Input syntax</title>
315 <para>All special processing is triggered by
316 the <literal>#</literal> operator. To output
317 a literal <literal>#</literal>, write it twice:
318 <literal>##</literal>. Inside string literals and comments
319 <literal>#</literal> characters are not processed.</para>
321 <para>A <literal>#</literal> is followed by optional
322 spaces and tabs, an alphanumeric keyword that describes
323 the kind of processing, and its arguments. Arguments look
324 like C expressions separated by commas (they are not
325 written inside parens). They extend up to the nearest
326 unmatched <literal>)</literal>, <literal>]</literal> or
327 <literal>}</literal>, or to the end of line if it occurs outside
328 any <literal>() [] {} '' "" /**/</literal> and is not preceded
329 by a backslash. Backslash-newline pairs are stripped.</para>
331 <para>In addition <literal>#{stuff}</literal> is equivalent
332 to <literal>#stuff</literal> except that it's self-delimited
333 and thus needs not to be placed at the end of line or in some
336 <para>Meanings of specific keywords:</para>
341 <term><literal>#include <file.h></literal></term>
342 <term><literal>#include "file.h"</literal></term>
344 <para>The specified file gets included into the C program,
345 the compiled Haskell file, and the C header.
346 <literal><HsFFI.h></literal> is included
347 automatically.</para>
352 <term><literal>#define name</literal></term>
353 <term><literal>#define name value</literal></term>
354 <term><literal>#undef name</literal></term>
356 <para>Similar to <literal>#include</literal>. Note that
357 <literal>#includes</literal> and
358 <literal>#defines</literal> may be put in the same file
359 twice so they should not assume otherwise.</para>
364 <term><literal>#let name parameters = "definition"</literal></term>
366 <para>Defines a macro to be applied to the Haskell
367 source. Parameter names are comma-separated, not
368 inside parens. Such macro is invoked as other
369 <literal>#</literal>-constructs, starting with
370 <literal>#name</literal>. The definition will be
371 put in the C program inside parens as arguments of
372 <literal>printf</literal>. To refer to a parameter,
373 close the quote, put a parameter name and open the
374 quote again, to let C string literals concatenate.
375 Or use <literal>printf</literal>'s format directives.
376 Values of arguments must be given as strings, unless the
377 macro stringifies them itself using the C preprocessor's
378 <literal>#parameter</literal> syntax.</para>
383 <term><literal>#def C_definition</literal></term>
385 <para>The definition (of a function, variable, struct or
386 typedef) is written to the C file, and its prototype or
387 extern declaration to the C header. Inline functions are
388 handled correctly. struct definitions and typedefs are
389 written to the C program too. The
390 <literal>inline</literal>, <literal>struct</literal> or
391 <literal>typedef</literal> keyword must come just after
392 <literal>def</literal>.</para>
397 <term><literal>#if condition</literal></term>
398 <term><literal>#ifdef name</literal></term>
399 <term><literal>#ifndef name</literal></term>
400 <term><literal>#elif condition</literal></term>
401 <term><literal>#else</literal></term>
402 <term><literal>#endif</literal></term>
403 <term><literal>#error message</literal></term>
404 <term><literal>#warning message</literal></term>
406 <para>Conditional compilation directives are passed
407 unmodified to the C program, C file, and C header. Putting
408 them in the C program means that appropriate parts of the
409 Haskell file will be skipped.</para>
414 <term><literal>#const C_expression</literal></term>
416 <para>The expression must be convertible to
417 <literal>long</literal> or <literal>unsigned
418 long</literal>. Its value (literal or negated literal)
419 will be output.</para>
424 <term><literal>#const_str C_expression</literal></term>
426 <para>The expression must be convertible to const char
427 pointer. Its value (string literal) will be output.</para>
432 <term><literal>#type C_type</literal></term>
434 <para>A Haskell equivalent of the C numeric type will be
435 output. It will be one of
436 <literal>{Int,Word}{8,16,32,64}</literal>,
437 <literal>Float</literal>, <literal>Double</literal>,
438 <literal>LDouble</literal>.</para>
443 <term><literal>#peek struct_type, field</literal></term>
445 <para>A function that peeks a field of a C struct will be
446 output. It will have the type
447 <literal>Storable b => Ptr a -> IO b</literal>.
449 The intention is that <literal>#peek</literal> and
450 <literal>#poke</literal> can be used for implementing the
451 operations of class <literal>Storable</literal> for a
452 given C struct (see <xref linkend="sec-Storable">).</para>
457 <term><literal>#poke struct_type, field</literal></term>
459 <para>Similarly for poke. It will have the type
460 <literal>Storable b => Ptr a -> b -> IO ()</literal>.</para>
465 <term><literal>#ptr struct_type, field</literal></term>
467 <para>Makes a pointer to a field struct. It will have the type
468 <literal>Ptr a -> Ptr b</literal>.</para>
473 <term><literal>#enum type, constructor, value, value, ...</literal></term>
475 <para>A shortcut for multiple definitions which use
476 <literal>#const</literal>. Each <literal>value</literal>
477 is a name of a C integer constant, e.g. enumeration value.
478 The name will be translated to Haskell by making each
479 letter following an underscore uppercase, making all the rest
480 lowercase, and removing underscores. You can supply a different
481 translation by writing <literal>hs_name = c_value</literal>
482 instead of a <literal>value</literal>, in which case
483 <literal>c_value</literal> may be an arbitrary expression.
484 The <literal>hs_name</literal> will be defined as having the
485 specified <literal>type</literal>. Its definition is the specified
486 <literal>constructor</literal> (which in fact may be an expression
487 or be empty) applied to the appropriate integer value. You can
488 have multiple <literal>#enum</literal> definitions with the same
489 <literal>type</literal>; this construct does not emit the type
490 definition itself.</para>
498 <title>Custom constructs</title>
500 <para><literal>#const</literal>, <literal>#type</literal>,
501 <literal>#peek</literal>, <literal>#poke</literal> and
502 <literal>#ptr</literal> are not hardwired into the
503 <command>hsc2hs</command>, but are defined in a C template that is
504 included in the C program: <filename>template-hsc.h</filename>.
505 Custom constructs and templates can be used too. Any
506 <literal>#</literal>-construct with unknown key is expected to
507 be handled by a C template.</para>
509 <para>A C template should define a macro or function with name
510 prefixed by <literal>hsc_</literal> that handles the construct
511 by emitting the expansion to stdout. See
512 <filename>template-hsc.h</filename> for examples.</para>
514 <para>Such macros can also be defined directly in the
515 source. They are useful for making a <literal>#let</literal>-like
516 macro whose expansion uses other <literal>#let</literal> macros.
517 Plain <literal>#let</literal> prepends <literal>hsc_</literal>
518 to the macro name and wraps the defininition in a
519 <literal>printf</literal> call.</para>
528 ;;; Local Variables: ***
530 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***