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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
|
<body bgcolor="#ffffff"> <i>The Hugs 98 User Manual</i><br> <a href="index.html">top</a> | <a href="basics.html">back</a> | <a href="commands.html">next</a> <br><hr>
<a name="started"></a><a name="sect4"></a>
<h2>4<tt> </tt>Starting Hugs</h2>
On Unix machines, the Hugs interpreter is usually started with a
command line of the form:
<tt><br>
hugs [option | file] ...<br>
</tt>On Windows 95/NT, Hugs may be started by selecting it from the
start menu or by double clicking on a file with the
<tt>.hs</tt> or <tt>.lhs</tt> extension.
(This manual assumes that Hugs has already been successfully
installed on your system.) <p>
Hugs uses <I>options</I> to set system parameters. These options are
distinguished by a leading <tt>+</tt> or <tt>-</tt> and are
used to customize the behaviour of the interpreter.
When Hugs starts, the interpreter
performs the following tasks:
<UL><LI>Options in the environment are processed. The variable
<tt>HUGSFLAGS</tt> holds these options. On Windows 95/NT, the registry is
also queried for Hugs option settings.
<LI>Command line options are processed.
<LI>Internal data structures are initialized. In
particular, the heap is initialized, and its size is
fixed at this point; if you want to run the interpreter with a heap
size other than the default, then this must be specified using
options on the command line, in the environment or in the registry.
<LI>The prelude file is loaded. The interpreter will
look for the prelude file on the path specified by
the <tt>-P</tt> option. If the prelude, located in the file
<tt>Prelude.hs</tt>, cannot be found in one of the path directories or
in the current directory, then Hugs will terminate;
Hugs will not run without the prelude file.
<LI>Program files specified on the command line are loaded. The
effect of a command <tt>hugs f1 ... fn</tt> is the same as starting up
Hugs with the <tt>hugs</tt> command and then typing <tt>:load f1 ... fn</tt>.
In particular, the interpreter will not terminate if a problem occurs
while it is trying to load one of the specified files, but it will
abort the attempted load command.
</UL>
The environment variables and command line options used by Hugs are
described in the following sections.<a name="secenv"></a><p>
<a name="sect4.1"></a>
<h3>4.1<tt> </tt>Environment options</h3>
Before options on the command line are processed, initial option
values are set from the environment. On Windows 95/NT, these
settings are added to the registry during setup.
On other systems, the initial settings are
determined by the <tt>HUGSFLAGS</tt> environment
variable. The syntax used in this case
is the same as on the command line: options are single letters,
preceeded by <tt>+</tt> or <tt>-</tt>, and sometimes followed by a value.
Option settings are separated by
spaces; option values containing spaces are encoded using
Haskell string syntax. The environment should be set up before
the interpreter is used so
that the search path is correctly defined to include the prelude.
The built-in defaults, however, may allow Hugs to be run without any
help from the environment on some systems.<p>
It is usually more convenient to save preferred option settings in
the environment rather than specifying them on the command line;
they will then be used
automatically each time the
interpreter is started. The method for setting these options depends on
the machine and operating system that you are using, and on the way that
the Hugs system was installed. The following examples show some typical
settings for Unix machines and PCs:
<UL><LI>The method for setting <tt>HUGSFLAGS</tt> on a Unix
machine depends on the choice of shell. For example, a C-shell user might
add something like the following to their <tt>.cshrc</tt> file:
<tt><br>
set HUGSFLAGS -P/usr/Hugs/lib:/usr/Hugs/libhugs -E"vi +%d %s"<br>
</tt>The <tt>P</tt> option is used to set the search path and the <tt>E</tt> is
used to set the editor. The string quotes are necessary for the
value of the <tt>E</tt> option becauses it contains spaces. The setting
for the path assumes that the system has been
installed in <tt>/usr/local/Hugs</tt> and will need to
be modified accordingly if a different directory was chosen.
The editor specified here is <tt>vi</tt>, which allows the
user to specify a startup line number by preceding it with
a <tt>+</tt> character. The settings are easily changed to accommodate
other editors.<p>
If you are installing Hugs for the benefit of several different
users, then you should probably use a script
file that sets appropriate values for the environment variables, and
then invokes the interpreter:
<tt><br>
#!/bin/sh<br>
HUGSFLAGS=/usr/Hugs/lib:/usr/Hugs/libhugs -E"vi +%d %s" +s<br>
export HUGSFLAGS<br>
exec /usr/local/bin/hugs $*<br>
</tt>One advantage of this approach is that individual users do not have
to worry about setting the environment variables themselves. In
addition to the <tt>E</tt> and <tt>P</tt> options, other options---such as
<tt>+s</tt> in this example---can be set.
It is easy for more advanced users to copy and customize a script like
this to suit their own needs.
<LI>Users of DOS or Windows 3.1 might add the following line
to <tt>autoexec.bat</tt>:
<tt><br>
set HUGSFLAGS=-P\hugs\lib;\hugs\libhugs -E"vi +%%d %%s"<br>
</tt>The setting for the path assumes that the system has been
installed in a top-level <tt>hugs</tt> directory, and will need to
be modified accordingly if a different directory was chosen.
In a similar way, the setting for the editor will only work if
you have installed the editor program, in this case <tt>vi</tt>, that it
refers to.
<LI>On Windows 95/NT, the setup program initializes the
environment, and this can be changed subsequently (on these systems only)
by using either the <tt>:set</tt> command or a registry editor. The
InstallShield script that performs the installation
initializes the path using the installation directory; other
directories can be added using <tt>-P</tt>. Installed options are stored
under the <tt>HKEY_LOCAL_MACHINE</tt> key; changes to these options
using <tt>:set</tt> are placed under <tt>HKEY_CURRENT_USER</tt> so that
different users do not alter each other's options.
</UL><p>
For completeness, we should also mention the other environment variables
that are used by Hugs:
<UL><LI>The <tt>SHELL</tt> variable on a Unix machine, or
the <tt>COMSPEC</tt> variable on a DOS machine, determines which
shell is used by the <tt>:!</tt> command.
<LI>The <tt>EDITOR</tt> variable is used to try and locate an editor
if no editor option has been set. Note, however, that
this variable does not normally provide the extra information that is
needed to be able to start the editor at a specific line in the input file.
</UL><a name="secoptions"></a><p>
<a name="sect4.2"></a>
<h3>4.2<tt> </tt>Options</h3>
The behaviour of the interpreter, particularly the read-eval-print loop,
can be customized using options. For example, you might use:
<tt><br>
hugs -i +g +h30K<br>
</tt>to start the interpreter with the <tt>i</tt> option (import chasing) disabled,
the <tt>g</tt> option (garbage collector messages) enabled, and with a heap
of thirty thousand cells. As this example suggests, many of the
options are toggles, meaning that they can either be switched on (by
preceding the option with a <tt>+</tt> character) or off (by using
a <tt>-</tt> character). Options may also be grouped together.
For example, <tt>hugs +stf -le</tt> is equivalent
to <tt>hugs +s +t +f -l -e</tt>.<p>
Option settings can be specified in a number of different
ways---the <tt>HUGSFLAGS</tt> environment variable, the Windows
registry, the command line, and the <tt>:set</tt> command---but the
same syntax is used in each case.
To avoid any confusion with filenames entered on the command line, option
settings must always begin with a leading <tt>+</tt> or <tt>-</tt> character.
However, in some cases---the <tt>h</tt>, <tt>p</tt>, <tt>r</tt>, <tt>P</tt>,
and <tt>E</tt> options---the choice is not significant.
With the exception of
the heap size option, <tt>h</tt>, all
options can be changed while the interpreter is running using
the <tt>:set</tt> command.
The same command can be used (without any arguments) to display a summary
of the available options and to inspect their current settings.<p>
The complete set of Hugs options is described in the sections below.
The only omission here is the <tt>-98</tt> and <tt>+98</tt> options that
are used to set the Haskell 98 compatability mode. These are discussed
in Section <a href="exts.html#exts">7</a>.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set search path</I> -Ppath</td></tr></table>
The <tt>-P</tt>path option changes the Hugs
search path to the specified path.
The search path is usually initialized in the environment and should
always include the directory containing the Hugs prelude and the
standard libraries.
When an unknown module is imported, Hugs searches for a file with the
same name as the module along this path. The current directory is
always searched before the path is used.
Directory names should be separated by colons or, on
Windows/DOS machines, by semicolons. Empty components in the path
refer to the prior value of the path. For example, setting the path to
<tt>dir:</tt> (<tt>dir;</tt> on Windows/DOS) would add <tt>dir</tt> to the
front of the current path. Within the path, <tt>{Hugs}</tt> refers
to the directory containing the Hugs libraries so one might use a path
such as <tt>{Hugs}/lib:{Hugs}/lib/hugs</tt>.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set editor </I> -Ecmd</td></tr></table>
A <tt>-E</tt>cmd option can be used to change the editor
string to the specified cmd while the interpreter
is running. The editor string is usually initialized
from the environment
when the interpreter starts running.<p>
Any occurrences of <tt>%d</tt> and <tt>%s</tt> in
the editor option are replaced by the start line
number and the name of the file to be edited, respectively, when the
editor is invoked. If specified, the line number parameter is used
to let the interpreter start the editor at the line where an error was
detected, or, in the case of the <tt>:find</tt> command,
where a specified variable was defined.<p>
Other editors can be selected.
For example, you can use the following value to
configure Hugs to use <tt>emacs</tt>:
<tt><br>
-E"emacs +%d %s"<br>
</tt>More commonly, <tt>emacsclient</tt> or <tt>gnuclient</tt> is used to avoid
starting a new <tt>emacs</tt> with every edit.<p>
On Windows/DOS, you can use <tt>-Eedit</tt> for the standard DOS editor,
or <tt>-Enotepad</tt> for the Windows notepad editor.
However, neither <tt>edit</tt> or <tt>notepad</tt> allow you
to specify a start line number, so you may prefer to install
a different editor.<p>
<table border=2 cellpadding=3>
<tr><td><I>Print statistics</I> +s,-s</td></tr></table>
Normally, Hugs just shows the result of evaluating each expression:
<tt><br>
Prelude> map (\x -> x*x) [1..10]<br>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]<br>
Prelude> [1..]<br>
[1, 2, 3, 4, {Interrupted!}<br>
Prelude><br>
</tt>With the <tt>+s</tt> option, the interpreter will also display statistics
about the total number of <I>reductions</I> and <I>cells</I>; the
former gives a measure of the work done, while the latter gives an
indication of the amount of memory used. For example:
<tt><br>
Prelude> :set +s<br>
Prelude> map (\x -> x*x) [1..10]<br>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]<br>
(248 reductions, 429 cells)<br>
Prelude> [1..]<br>
[1, 2, 3, 4, {Interrupted!}<br>
(18 reductions, 54 cells)<br>
Prelude><br>
</tt>Note that the statistics produced by <tt>+s</tt> are an extremely crude
measure of the behaviour of a program, and can easily be misinterpreted.
For example:
<UL><LI>The fact that one expression requires more reductions than another does
not necessarily mean that the first is slower; some reductions
require much more work than others, and it may be that the average cost
of reductions in the first expression is much lower than the average
for the second.
<LI>The cell count does not give any information about <I>residency</I>,
which is the number of cells that are being used at any given time. For
example, it does not distinguish between computations that run in constant
space and computations with residency proportional to the size of the
input.
</UL>
One reasonable use of the statistics produced by <tt>+s</tt> would be to
observe general trends in the behaviour of a single algorithm with
variations in its input.<p>
<table border=2 cellpadding=3>
<tr><td><I>Print type after evaluation</I> +t,-t</td></tr></table>
With the <tt>+t</tt> option, the interpreter will display both the result
and type of each expression entered at the Hugs prompt:
<tt><br>
Prelude> :set +t<br>
Prelude> map (\x -> x*x) [1..10]<br>
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] :: [Int]<br>
Prelude> not True<br>
False :: Bool<br>
Prelude> \x -> x<br>
<<function>> :: a -> a<br>
Prelude><br>
</tt>Note that the interpreter will not display the type of an expression
if its evaluation is interrupted or fails with a run-time error.
In addition, the interpreter will not print the type, <tt>IO ()</tt>, of
a program in the <tt>IO</tt> monad;
the interpreter treats these as a special case, giving the programmer more
control over the output that is produced.<p>
<table border=2 cellpadding=3>
<tr><td><I>Terminate on error</I> +f,-f</td></tr></table>
In normal use, the evaluation of an expression is abandoned completely
if a run-time error occurs, such as a failed pattern match or an attempt
to divide by zero. For example:
<tt><br>
Prelude> [1 `div` 0]<br>
[<br>
Program error: {primDivInt 1 0}<br>
<br>
Prelude> [1 `div` 0, 2]<br>
[<br>
Program error: {primDivInt 1 0}<br>
<br>
Prelude><br>
</tt>This is often useful during program development because it means that
errors are detected as soon as they occur.
However, technically speaking, the two expressions above have different
meanings; the first is a singleton list, while the second has two elements.
Unfortunately, the output produced by Hugs does not allow us to distinguish
between the values.<p>
The <tt>-f</tt> option can be used to make the Hugs printing option
a little more accurate; this should normally be combined
with <tt>-u</tt> because
the built-in printer is better than the user-defined <tt>show</tt> functions
at recovering from evaluation errors. With these settings, if the interpreter
encounters an irreducible subexpression, then it prints the expression
between a matching pair of braces and attempts to continue with the evaluation
of other parts of the original expression. For the examples above, we get:
<tt><br>
Prelude> :set -u -f<br>
Prelude> [1 `div` 0] -- value is [bottom]<br>
[{primDivInt 1 0}]<br>
Prelude> [1 `div` 0, 2]<br>
[{primDivInt 1 0}, 2] -- value is [bottom, 2]<br>
Prelude><br>
</tt>Reading an expression in braces as
bottom, the output produced here
shows the correct values, according to the semantics of
Haskell. Of course, it is not possible to
detect all occurrences of bottom like this, such as those produced by
a nonterminating computation:
<tt><br>
Prelude> last [1..]<br>
^C{Interrupted!} -- nothing printed until interrupted<br>
<br>
Prelude><br>
</tt>Note that the basic method of evaluation is the same with both
the <tt>+f</tt> and <tt>-f</tt> options; all that changes is the way that
the printing mechanism deals with certain kinds of runtime error.<p>
<table border=2 cellpadding=3>
<tr><td><I>Garbage collector notification</I> +g,-g</td></tr></table>
It is sometimes useful to monitor uses of the garbage collector, and to
determine how many cells are recovered with each collection.
If the <tt>+g</tt> option is set, then the interpreter will print a message
of the form <tt>{{Gc:num}}</tt> each time that the garbage collector is
invoked. The number after the colon indicates the total number of
cells that are recovered.<p>
As a simple application, we can use garbage collector messages to observe
that an attempt to sum an infinite list, although non-terminating, will
at least run in constant space:
<tt><br>
Prelude> :set +g<br>
Prelude> sum [1..]<br>
{{Gc:95763}}{{Gc:95760}}{{Gc:95760}}{{Gc:95760}}{Interrupted!}<br>
<br>
Prelude><br>
</tt>Garbage collector messages may be printed at almost any stage in a
computation (or indeed while loading, type checking or compiling a
file of definitions). For this reason, it is often best to turn
garbage collector messages off (using <tt>:set -g</tt>, for example)
if they are not required.<p>
<table border=2 cellpadding=3>
<tr><td><I>Literate modules</I> +l,-l,+e,-e</td></tr></table>
Like most programming languages, Hugs usually treats source file input
as a sequence of lines in which program text is the norm,
and comments play a secondary role. In Hugs, as in Haskell, comments
are introduced by the character sequences <tt>--</tt> and <tt>{- ... -}</tt>.<p>
An alternative approach, using an idea described by Knuth as "literate
programming," gives more emphasis to comments and documentation, with
additional characters needed to distinguish program text from comments.
Hugs supports a form of literate programming based on an idea due to
Richard Bird and originally implemented as part of the functional
programming language Orwell.<p>
In a Hugs literate module, program lines are marked by a <tt>></tt> character
in the first column; any other line is treated as a program comment.
This makes it particularly easy to write a document which is both an
executable Hugs module and, at the same time, without need for any
preprocessing, suitable for use with document preparation software such
as LaTeX.<p>
Hugs will treat any input file with a name ending in <tt>.hs</tt> as a normal
module and any input file with a name ending in <tt>.lhs</tt> as a literate
module. If the <tt>-l</tt> option is selected, then any other file loaded
into Hugs will be treated as a normal module. Conversely, if <tt>+l</tt> is
selected, then these files will be treated as literate modules.
The effect of using literate modules can be thought of as applying a
preprocessor to each input file that is loaded into Hugs. This
has a particularly simple definition in Hugs:
<tt><br>
illiterate :: String -> String<br>
illiterate cs = unlines [ " " ++ xs | ('>':xs) <- lines cs ]<br>
</tt>The system of literate modules that was used in Orwell is a
little more complicated than this and requires the programmer to adopt
two further conventions in an attempt to catch simple errors in literate
modules:
<UL><LI>Every input file must contain at least one line whose first
character is <tt>></tt>. This prevents modules with no
definitions (because the programmer has forgotten to use
the <tt>></tt> character to mark definitions) from being accepted.
<LI>Lines containing definitions must be separated from comment lines
by one or more blank lines (i.e., lines containing only space and
tab characters). This is useful for catching programs where the
leading <tt>></tt> character has been omitted from one or more lines in
the definition of a function. For example:
<tt> <br>
> map f [] = []<br>
map f (x:xs) = f x : map f xs<br>
</tt>would be treated as an error.
</UL>
Hugs will report on errors of this kind whenever the <tt>-e</tt> option is
enabled (the default setting).<p>
The Haskell Report defines a second style of literate programming in
which code is surrounded by <tt>\begin{code}</tt> and <tt>\end{code}</tt>.
See Appendix C of the Haskell Report for more information about
literate programming in Haskell. <p>
<table border=2 cellpadding=3>
<tr><td><I>Display dots while loading</I> +.,-.</td></tr></table>
As Hugs loads each file into the interpreter, it prints a short
sequence of messages to indicate progress through the various stages
of parsing the module, dependency analysis, type checking, and compilation.
With the default setting, <tt>-.</tt>, the interpreter prints the name of
each stage, backspacing over it to erase it from the screen when the
stage is complete. If you are fortunate enough to be using a
fast machine, you may not always see the individual words as they flash
past. After loading a file, your screen will typically look something
like this:
<tt><br>
Prelude> :l Array<br>
Reading file "/Hugs/lib/Array.hs":<br>
<br>
Hugs session for:<br>
/Hugs/lib/Prelude.hs<br>
/Hugs/lib/Array.hs<br>
Prelude><br>
</tt>On some systems, the use of backspace characters to erase a line may
not work properly---for example, if you try to run Hugs from
within <tt>emacs</tt>. In this case, you may prefer
to use the <tt>+.</tt> setting which prints a separate line for each stage,
with a row of dots to indicate progress:
<tt><br>
Prelude> :load Array<br>
Reading file "/Hugs/lib/Array.hs":<br>
Parsing....................................................<br>
Dependency analysis........................................<br>
Type checking..............................................<br>
Compiling..................................................<br>
<br>
Hugs session for:<br>
/Hugs/lib/Prelude.hs<br>
/Hugs/lib/Array.hs<br>
Prelude><br>
</tt>This setting can also be useful on very slow machines where the growing
line of dots provides confirmation that the interpreter is making progress
through the various stages involved in loading a file. You should note,
however, that the mechanisms used to display the rows of dots can add
a substantial overhead to the time that it takes to load files;
in one experiment, a particular program took nearly five times longer
to load when the <tt>+.</tt> option was used. In this case, users might
prefer to use the <tt>-q</tt> option described below.<p>
<table border=2 cellpadding=3>
<tr><td><I>Display nothing while loading</I> +q,-q</td></tr></table>
The <tt>+q</tt> (quiet) option supresses the messages used to indicate
progress while Hugs is loading files. If this option is turned off
using the <tt>-q</tt>, then the format of output messages is determined
by the current <tt>+.</tt> or <tt>-.</tt> setting.<p>
<table border=2 cellpadding=3>
<tr><td><I>List files loaded</I> +w,-w</td></tr></table>
By default, Hugs prints a complete list of all the files that have
been loaded into the system after every successful load or reload command.
The <tt>-w</tt> option can be used to turn this feature off. Note that
the <tt>:info</tt> command, without any arguments, can also be used to list
the names of currently loaded files.<p>
<table border=2 cellpadding=3>
<tr><td><I>Detailed kind errors</I> +k,-k</td></tr></table>
Haskell uses a system of kinds to ensure that type expressions are well-formed:
for example, to make sure that each type constructor is applied to the
appropriate number of arguments. For example, the following program:
<tt><br>
module Main where<br>
data Tree a = Leaf a | Tree a :^: Tree a<br>
type Example = Tree Int Bool<br>
</tt>will cause an error:
<tt><br>
ERROR "Main.hs" (line 3): Illegal type "Tree Int Bool" in<br>
constructor application<br>
</tt>The problem here is that <tt>Tree</tt> is a unary constructor
of kind <tt>* -> *</tt>, but the definition of <tt>Example</tt> uses
it as a binary constructor with at least two arguments, and hence
expecting a kind of the form <tt>(* -> * -> k)</tt>, for some kind <tt>k</tt>.<p>
By default, Hugs reports problems like this with a simple message like the
one shown above. However, if the <tt>+k</tt> option is selected, then the
interpreter will print a more detailed version of the error message,
including details about the kinds of the type expressions that are involved:
<tt><br>
ERROR "Main.hs" (line 3): Kind error in constructor application<br>
*** expression : Tree Int Bool<br>
*** constructor : Tree<br>
*** kind : * -> *<br>
*** does not match : * -> a -> b<br>
<br>
</tt>In addition, if the <tt>+k</tt> option is used, then Hugs will also
include information about kinds in the information produced by
the <tt>:info</tt> command:
<tt><br>
Prelude> :info Tree<br>
-- type constructor with kind * -> *<br>
data Tree a<br>
<br>
-- constructors:<br>
Leaf :: a -> Tree a<br>
(:^:) :: Tree a -> Tree a -> Tree a<br>
<br>
-- instances:<br>
instance Eval (Tree a)<br>
<br>
Prelude><br>
<br>
<p>
</tt><table border=2 cellpadding=3>
<tr><td><I>Use "show" to display results</I> +u,-u</td></tr></table>
In normal use, Hugs displays the value of each expression entered into the
interpreter by applying the standard prelude function:
<tt><br>
show :: Show a => a -> String<br>
</tt>to it and displaying the resulting string of characters. This approach works
well for any value whose type is an instance of the standard <tt>Show</tt> class;
for example, the prelude defines instances of <tt>Show</tt> for all of the
built-in datatypes. It is also easy for users to extend the class with new
datatypes, either by providing a handwritten instance declaration, or by
requesting an automatically derived instance as part of the datatype
definition, as in:
<tt><br>
data Rainbow = Red | Orange | Yellow | Green | Blue | Indigo | Violet<br>
deriving Show<br>
</tt>The advantage of using <tt>show</tt> is that it allows programmers to
display the results of evaluations in whatever form is most convenient
for users---which is not always the same as the way in which the values
are represented.<p>
This is probably all that most users will ever need. However, there are
some circumstances where it is not convenient, for example, for certain
kinds of debugging or for work with datatypes that are not instances
of <tt>Show</tt>. In these situations, the <tt>-u</tt> option can be used
to prevent the use of <tt>show</tt>. In its place, Hugs will use a
built-in printing mechanism that works for <I>all</I> datatypes, and
uses the representation of a value to determine what gets printed.
At any point, the default printing mechanism can be restored by
setting <tt>+u</tt>.<p>
<table border=2 cellpadding=3>
<tr><td><I>Import chasing</I> +i,-i</td></tr></table>
Import chasing is a simple, but flexible mechanism for dealing with
programs that involve multiple modules. It works in a natural
way, using the information in import statements at the beginning of
modules, and is particularly useful for large programs, or for
programs that use standard Hugs libraries.<p>
For example, consider a module <tt>Demo.hs</tt> that requires the
facilities provided by the <tt>STArray</tt> library.
This dependency might be reflected by including
the following import statement at the beginning of <tt>Demo.hs</tt>:
<tt><br>
import STArray<br>
</tt>Now, if we try to load this module into Hugs, then the system will
automatically search for
the <tt>STArray</tt> library and load it
into Hugs, before <tt>Demo.hs</tt> is loaded. In fact,
the <tt>STArray</tt> library module also begins with some import statements:
<tt><br>
import ST<br>
import Array<br>
</tt>So, Hugs will actually load the <tt>ST</tt> and <tt>Array</tt> libraries
first, then the <tt>STArray</tt> library, and only then will it try to
read the rest of <tt>Demo.hs</tt>:
<tt><br>
Prelude> :load Demo<br>
Reading file "Demo.hs":<br>
Reading file "/hugs/libhugs/STArray.hs":<br>
Reading file "/hugs/libhugs/ST.hs":<br>
Reading file "/hugs/lib/Array.hs":<br>
Reading file "/hugs/libhugs/STArray.hs":<br>
Reading file "Demo.hs":<br>
Demo><br>
</tt>Initially, the interpreter reads only the first part of any module
loaded into the system, upto and including any import statements.
Only one module is allowed in each file; files with no module
declaration are assumed to declare the <tt>Main</tt> module.
If there are no imports, or if the modules specified as imports
have already been loaded, then the system carries on and loads the
module as normal.
On the other hand, if the module includes import statements for modules that
have not already been loaded, then the interpreter postpones the task of
reading the current module until all of the specified imports have
been successfully loaded. This explains
why <tt>Demo.hs</tt> and <tt>STArray.hs</tt> are read twice in the example above;
first to determine which imports are required, and then to read in the
rest of the file once the necessary imports have been loaded.<p>
The list of directories and filenames that Hugs tries in an attempt
to locate the source for a module <tt>Mod</tt> named in an import statement
can be specified by:
<tt><br>
[ (dir,"Mod"++suf) | dir <- [d] ++ path ++ [""],<br>
suf <- ["", ".hs", ".lhs"]]<br>
</tt>The search starts in the directory <tt>d</tt> where the file containing
the import statement was found, then tries each of the directories in
the current path (as defined by the <tt>-P</tt> option),
represented here by <tt>path</tt>, and ends with <tt>""</tt>, which gives
a search relative to the current directory.
The fact that the search starts in <tt>d</tt> is particularly important
because it means that you can load a multi-file program into Hugs without
having to change to the directory where its source code is located.
For example, suppose that <tt>/tmp</tt> contains the
files, <tt>A.hs</tt>, <tt>B.hs</tt>, and <tt>C.hs</tt>,
that <tt>B</tt> imports <tt>A</tt>, and that <tt>C</tt> imports <tt>B</tt>.
Now, regardless of the current working directory, you can load the
whole program with the command <tt>:load /tmp/C</tt>; the
import in <tt>C</tt> will be taken as a reference to <tt>/tmp/B.hs</tt>,
while the import in that file will be taken as a reference
to <tt>/tmp/A.hs</tt>.<p>
Import chasing is often very useful, but you should also be aware of its
limitations:
<UL><LI>Mutually recursive modules are not supported;
if <tt>A</tt> imports <tt>B</tt>, then <tt>B</tt> must not import <tt>A</tt>,
either directly or indirectly through another one of its imports.
<LI>Import chasing assumes a direct mapping from module names to
the names
of the files that they are stored in. If <tt>A</tt> imports <tt>B</tt>, then
the code for <tt>B</tt> must be in a file called
either <tt>B</tt>, <tt>B.hs</tt>, or <tt>B.lhs</tt>, and must be located in
one of the directories specified above.<p>
On rare occasions, it is useful to specify a particular pathname as the
target for an import statement; Hugs allows string literals to be used
as module identifiers for this purpose:
<tt><br>
import "../TypeChecker/Types.hs"<br>
</tt>Note, however, that this is a nonstandard feature of Hugs, and that it is
not valid Haskell syntax. You should also be aware that Hugs uses the
names of files in deciding whether a particular import has already
been loaded, so you should avoid situations where a single file is referred
to by more than one name. For example, you should not assume that Hugs
will be able to determine whether <tt>Demo.hs</tt> and <tt>./Demo.hs</tt> are
references to the same file.
</UL><p>
Import chasing is usually enabled by default (setting <tt>+i</tt>), but it can
also be disabled using the <tt>-i</tt> option.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set heap size</I> -hsize</td></tr></table>
A <tt>-h</tt>size option can be used to request
a particular heap size for the interpreter---the total number of
cells that are available at any one time---when Hugs is
first loaded. The request will only be honoured if it falls within a
certain range, which depends on the machine, and the version of Hugs that
is used. The size parameter may include
a <tt>K</tt> or <tt>k</tt> suffix, which acts as a multiplier by 1,000.
For example, either of the following commands:
<tt><br>
hugs -h25000<br>
hugs -h25K<br>
</tt>will usually start the Hugs interpreter with a heap of 25,000 cells.
Cells are generally 8 bytes wide (except on the 16 bit Hugs running on
DOS) and Hugs allocates a single heap.
Note that the heap is used to hold an intermediate (parsed) form of
each module while it is being read, type checked
and compiled. It follows that, the larger the module, the larger
the heap required to enable that module to be loaded into Hugs. In practice,
most large programs are written (and loaded) as a number of separate modules
which means that this does not usually cause problems.<p>
Unlike all of the other options described here, the heap
size setting cannot be changed from within the interpreter using
a <tt>:set</tt> command.
However, on Window 95/NT, changing the heap size with <tt>:set</tt> will
affect the next running of Hugs since it saves all options in the
registry. <p>
<table border=2 cellpadding=3>
<tr><td><I>Set prompt</I> -pstring</td></tr></table>
A <tt>-p</tt>str option can be used to change the
prompt to the specified string, str:
<tt><br>
Prelude> :set -p"Hugs> "<br>
Hugs> :set -p"? "<br>
?<br>
</tt>Note that you will need to use quotes around the prompt string if you
want to include spaces or special characters. Any <tt>%s</tt> in the
prompt will be replaced by the current module name.
The default prompt is <tt>"%s> "</tt>.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set repeat string</I> -rstring</td></tr></table>
Hugs allows the user to recall the last expression entered into the
interpreter by typing the characters <tt>$$</tt> as part of the next
expression:
<tt><br>
Prelude> map (1+) [1..10]<br>
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]<br>
Prelude> filter even $$<br>
[2, 4, 6, 8, 10]<br>
Prelude><br>
</tt>A <tt>-r</tt>str option can be used to change the
repeat string---the symbol used to recall the last
expression---to str. For example,
users of Standard ML might be more comfortable using:
<tt><br>
Prelude> :set -rit<br>
Prelude> 6 * 7<br>
42<br>
Prelude> it + it<br>
84<br>
Prelude> <br>
</tt>Another reason to change the repeat string is to avoid clashes with uses
of the same symbol in a particular program; for example, if <tt>$$</tt> is
defined as an operator in a program.<p>
Note that the repeat string must be a valid Haskell identifier or
symbol, although it will always be parsed as an identifier. If the
repeat string is set to a value that is neither an identifier or symbol
(for example, <tt>-r0</tt>), then the repeat last expression facility
will be disabled.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set preprocessor string</I> -Fcmd</td></tr></table>
Although it is not needed very often, there are sometimes applications
where it useful to arrange for input files to be preprocessed before
they are passed to the Hugs interpreter. This might be used,
for example, to make use of filters to support conditional compilation,
language extensions, literate programming systems, or format conversion.
The <tt>-F</tt> option can be used to set a particular command
string <tt>cmd</tt> as the name for a preprocessor. If set, then for
any source file, say <tt>file.hs</tt>, that the user tries to load
into Hugs, the interpreter will use the output from the
command <tt>cmd file.hs</tt> instead of the contents of
the file <tt>file.hs</tt> itself.<p>
Note that the <tt>-F</tt> option is system dependent, and is not supported
on all platforms.<p>
<table border=2 cellpadding=3>
<tr><td><I>Set constraint cutoff limit</I> -cnum</td></tr></table>
The <tt>-c</tt> parameter controls the complexity of constraint satisfaction
searches in the Hugs type checker. This is a technical mechanism to ensure
that type checking terminates, and can safely be ignored by most users.
However, in programs that make significant use of complex type class
hierarchies, it may sometimes be necessary to increase the setting for
the <tt>-c</tt> option to enable the Hugs type checker to explore a larger
search space.<p>
The usual default for this setting is 40, which corresponds to the
command line option <tt>-c40</tt>, and we have not yet seen any examples
of valid Hugs programs that are rejected with this setting. (It is
possible to construct artificial programs that do require higher values,
but such examples are pathological and they do not seem to appear in
practice.) There is no practical benefit in choosing a lower value for
this parameter. Note that, if the value used is too low, then Hugs
will not be able to load some standard files and libraries, including
the prelude.<p>
<hr><i>The Hugs 98 User Manual</i><br><a href="index.html">top</a> | <a href="basics.html">back</a> | <a href="commands.html">next</a> <br><font size=2>May 22, 1999</font>
|