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>hs2c</command></title>
104 <indexterm><primary><command>hs2c</command></primary>
107 <para>The <command>hs2c</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
121 <para>Actually <command>hs2c</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>glue-hsc takes input files as arguments, and flags that
139 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>--cc=PROG</literal></term>
153 <para>The C compiler to use (default:
154 <command>ghc</command>)</para>
159 <term><literal>--ld=PROG</literal></term>
161 <para>The linker to use (default:
162 <command>gcc</command>).</para>
167 <term><literal>--cflag=FLAG</literal></term>
169 <para>An extra flag to pass to the C compiler.</para>
174 <term><literal>--lflag=FLAG</literal></term>
176 <para>An extra flag to pass to the linker.</para>
181 <term><literal>--help</literal></term>
183 <para>Display a summary of the available flags.</para>
188 <para>The input file should end with .hsc. Output files get
189 names with the <literal>.hsc</literal> suffix replaced:</para>
195 <entry><literal>.hs</literal></entry>
196 <entry>Haskell file</entry>
199 <entry><literal>_hsc.h</literal></entry>
200 <entry>C header</entry>
203 <entry><literal>_hsc.c</literal></entry>
204 <entry>C file</entry>
210 <para>The C program is compiled using the Haskell compiler. This
211 provides the include path to <filename>HsFFI.h</filename> which
212 is automatically included into the C program.</para>
215 <sect2><title>Input syntax</title>
217 <para>All special processing is triggered by the
218 <literal>#</literal> character. To output a literal
219 <literal>#</literal>, write it twice: <literal>##</literal>.</para>
221 <para>Otherwise <literal>#</literal> is followed by optional
222 spaces and tabs, an alphanumeric key that describes the kind of
223 processing, and its arguments. Arguments look like C expressions
224 and extend up to the nearest unmatched <literal>)</literal>,
225 <literal>]</literal>, or <literal>}</literal>, or to the end of
226 line outside any <literal>() [] {} '' "" /* */</literal>. Any
227 character may be preceded by a backslash and will not be treated
230 <para>Meanings of specific keys:</para>
235 <term><literal>#include <file.h></literal></term>
236 <term><literal>#include "file.h"</literal></term>
238 <para>The specified file gets included into the C program,
239 the compiled Haskell file, and the C
240 header. <literal><HsFFI.h></literal> is included
241 automatically.</para>
246 <term><literal>#define name</literal></term>
247 <term><literal>#define name value</literal></term>
249 <para>Similar to <literal>#include</literal>. Note that
250 <literal>#includes</literal> and
251 <literal>#defines</literal> may be put in the same file
252 twice so they should not assume otherwise.</para>
257 <term><literal>#option opt</literal></term>
259 <para>The specified Haskell compiler command-line option
260 is placed in the <literal>{-# OPTIONS #-}</literal> pragma
261 at the top of the Haskell file (see <xref
262 linkend="source-file-options">). This is needed because
263 glue-hsc emits its own <literal>OPTIONS</literal> pragma,
264 and only one such pragma is interpreted by GHC.</para>
269 <term><literal>#def C_definition</literal></term>
271 <para>The definition (of a function, variable, struct or
272 typedef) is written to the C file, and its prototype or
273 extern declaration to the C header. Inline functions are
274 handled correctly. struct definitions and typedefs are
275 written to the C program too. The
276 <literal>inline</literal>, <literal>struct</literal> or
277 <literal>typedef</literal> keyword must come just after
278 <literal>def</literal>.</para>
283 <term><literal>#if condition</literal></term>
284 <term><literal>#ifdef name</literal></term>
285 <term><literal>#ifndef name</literal></term>
286 <term><literal>#elif condition</literal></term>
287 <term><literal>#else</literal></term>
288 <term><literal>#endif</literal></term>
290 <para>Conditional compilation directives are passed
291 unmodified to the C program, C file, and C header. Putting
292 them in the C program means that appropriate parts of the
293 Haskell file will be skipped.</para>
298 <term><literal>#const C_expression</literal></term>
300 <para>The expression must be convertible to
301 <literal>long</literal> or <literal>unsigned
302 long</literal>. Its value (literal or negated literal)
303 will be output.</para>
308 <term><literal>#const_str C_expression</literal></term>
310 <para>The expression must be convertible to const char
311 pointer. Its value (string literal) will be output.</para>
316 <term><literal>#type C_type</literal></term>
318 <para>A Haskell equivalent of the C numeric type will be
319 output. It will be one of
320 <literal>{Int,Word}{8,16,32,64}</literal>,
321 <literal>Float</literal>, <literal>Double</literal>,
322 <literal>LDouble</literal>.</para>
327 <term><literal>#peek struct_type, field</literal></term>
329 <para>A function that peeks a field of a C struct will be
330 output. It will have the type
331 <literal>Storable b => Ptr a -> IO b</literal>.
333 The intention is that <literal>#peek</literal> and
334 <literal>#poke</literal> can be used for implementing the
335 operations of class <literal>Storable</literal> for a
336 given C struct (see <xref linkend="sec-Storable">).</para>
341 <term><literal>#poke struct_type, field</literal></term>
343 <para>Similarly for poke. It will have the type
344 <literal>Storable b => Ptr a -> b -> IO ()</literal>.</para>
349 <term><literal>#ptr struct_type, field</literal></term>
351 <para>Makes a pointer to a field struct. It will have the type
352 <literal>Ptr a -> Ptr b</literal>.</para>
360 <title>Custom constructs</title>
362 <para><literal>#const</literal>, <literal>#type</literal>,
363 <literal>#peek</literal>, <literal>#poke</literal> and
364 <literal>#ptr</literal> are not hardwired into the
365 <command>hs2c</command>, but are defined in a C template that is
366 included in the C program: <filename>template-hsc.h</filename>.
367 Custom constructs and templates can be used too. Any
368 <literal>#</literal>-construct with unknown key is expected to
369 be handled by a C template.</para>
371 <para>A C template should define a macro or function with name
372 prefixed by <literal>hsc_</literal> that handles the construct
373 by emitting the expansion to stdout. See
374 <filename>template-hsc.h</filename> for examples.</para>
383 ;;; Local Variables: ***
385 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***