Haskell/YAHT/Getting started

From Wikibooks, open books for an open world
< Haskell‎ | YAHT
Jump to: navigation, search
Haskell
Haskellwiki logo.png
Yet Another Haskell Tutorial
Preamble
Introduction
Getting Started
Language Basics (Solutions)
Type Basics (Solutions)
IO (Solutions)
Modules (Solutions)
Advanced Language (Solutions)
Advanced Types (Solutions)
Monads (Solutions)
Advanced IO
Recursion
Complexity

There are three well known Haskell systems: Hugs, GHC and NHC. Hugs is exclusively an interpreter, meaning that you cannot compile stand-alone programs with it, but can test and debug programs in an interactive environment. GHC is both an interpreter (like Hugs) and a compiler which will produce stand-alone programs. NHC is exclusively a compiler. Which you use is entirely up to you. I've tried to make a list of some of the differences in the following list but of course this is far from exhaustive:

  • Hugs - very fast to load files, slow to run them; implements almost all of Haskell 98 (the standard) and most extensions; built-in support for module browsing; cannot create stand-alones; written in C; works on almost every platform; built-in graphics library.
  • GHC - interactive environment is slower than Hugs to load, but allows function definitions in the environment (in Hugs you have to put them in a file); implements all of Haskell 98 and extensions; good support for interfacing with other languages; in a sense the "de facto" standard. It also allows compiled objects to be loaded and tested.
  • NHC - less used and no interactive environment, but produces smaller and often faster executables than does GHC; supports Haskell98 and some extensions.

I, personally, have all of them installed and use them for different purposes. I tend to use GHC to compile (primarily because I'm most familiar with it) and the Hugs interactive environment, since it is much faster. As such, this is what I would suggest. However, that is a fair amount to download and install, so if you had to go with just one, I'd get GHC, since it contains both a compiler and interactive environment.

Following is a description of how to download and install each of these as of the time this tutorial was written. It may have changed -- see the Haskell website for up-to-date information.

Hugs[edit]

Hugs supports almost all of the Haskell 98 standard (it lacks some of the libraries), as well as a number of advanced/experimental extensions, including: multi-parameter type classes, extensible records, rank-2 polymorphism, existentials, scoped type variables, and restricted type synonyms.

Where to get it[edit]

The official Hugs web page is at [1].

If you go there, there is a link titled "downloading" which will send you to the download page. From that page, you can download the appropriate version of Hugs for your computer.

Installation procedures[edit]

Once you've downloaded Hugs, installation differs depending on your platform, however, installation for Hugs is more or less identical to installation for any program on your platform.

  • For Windows when you click on the "msi" file to download,

simply choose "Run This Program" and the installation will begin automatically. From there, just follow the on-screen instructions.

  • For RPMs use whatever RPM installation program you know best.
  • For source first gunzip the file, then untar it. Presumably if you're using a system which isn't otherwise supported, you know enough about your system to be able to run configure scripts and make things by hand.

How to run it[edit]

On Unix machines, the Hugs interpreter is usually started with a command line of the form: hugs [option | file] ...

On Windows , Hugs may be started by selecting it from the start menu or by double clicking on a file with the .hs or .lhs extension. (This manual assumes that Hugs has already been successfully installed on your system.)

Hugs uses options to set system parameters. These options are distinguished by a leading + or - and are used to customize the behavior of the interpreter. When Hugs starts, the interpreter performs the following tasks:

  • Options in the environment are processed. The variable HUGSFLAGS holds these options. On Windows 95/NT, the registry is also queried for Hugs option settings.
  • Command line options are processed.
  • 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.
  • The prelude file is loaded. The interpreter will look for the prelude file on the path specified by the -P option. If the prelude, located in the file Prelude.hs, 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.
  • Program files specified on the command line are loaded. The effect of a command hugs f1 ... fn is the same as starting up Hugs with the hugs command and then typing :load f1 ... fn. 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.

The environment variables and command line options used by Hugs are described in the following sections.

Program options[edit]

To list all of the options would take too much space. The most important option at this point is "+98" or "-98". When you start hugs with "+98" it is in Haskell 98 mode, which turns off all extensions. When you start in "-98", you are in Hugs mode and all extensions are turned on. If you've downloaded someone else's code and you're having trouble loading it, first make sure you have the "98" flag set properly.

Further information on the Hugs options is in the manual: http://cvs.haskell.org/Hugs/pages/hugsman/started.html


How to get help[edit]

To get Hugs specific help, go to the Hugs web page. To get general Haskell help, go to the Haskell web page.

Glasgow Haskell Compiler[edit]

The Glasgow Haskell Compiler (GHC) is a robust, fully-featured, optimizing compiler and interactive environment for Haskell 98; GHC compiles Haskell to either native code or C. It implements numerous experimental language extensions to Haskell 98; for example: concurrency, a foreign language interface, multi-parameter type classes, scoped type variables, existential and universal quantification, unboxed types, exceptions, weak pointers, and so on. GHC comes with a generational garbage collector, and a space and time profiler.

Where to get it[edit]

Go to the official GHC web page GHC to download the latest release. The current version as of the writing of this tutorial is 5.04.2 and can be downloaded off of the GHC download page (follow the "Download" link). From that page, you can download the appropriate version of GHC for your computer.

Installation procedures[edit]

Once you've downloaded GHC, installation differs depending on your platform; however, installation for GHC is more or less identical to installation for any program on your platform.

  • For Windows when you click on the "msi" file to download, simply choose "Run This Program" and the installation will begin automatically. From there, just follow the on-screen instructions.
  • For RPMs use whatever RPM installation program you know best.
  • For source first gunzip the file, then untar it. Presumably if you're using a system which isn't otherwise supported, you know enough about your system to be able to run configure scripts and make things by hand.

For a more detailed description of the installation procedure, look at the GHC users manual under "Installing GHC".

How to run the compiler[edit]

Running the compiler is fairly easy. Assuming that you have a program written with a main function in a file called Main.hs, you can compile it simply by writing:

Example:

% ghc --make Main.hs -o main

The "--make" option tells GHC that this is a program and not just a library and you want to build it and all modules it depends on. "Main.hs" stipulates the name of the file to compile; and the "-o main" means that you want to put the output in a file called "main".

Note

In Windows, you should say "-o main.exe" to tell Windows that this is an executable file.

You can then run the program by simply typing "./main" at the prompt.

How to run the interpreter[edit]

GHCi is invoked with the command "ghci" or "ghc --interactive". One or more modules or filenames can also be specified on the command line; this instructs GHCi to load the specified modules or filenames (and all the modules they depend on), just as if you had said :load modules at the GHCi prompt.

To get help within the interpreter, type ":?" at the prompt. To quit, type ":q". To interrupt a running computation, press control-c.

Program options[edit]

To list all of the options would take too much space. The most important option at this point is "-fglasgow-exts". When you start GHCi without "-fglasgow-exts" it is in Haskell 98 mode, which turns off all extensions. When you start with "-fglasgow-exts", all extensions are turned on. If you've downloaded someone else's code and you're having trouble loading it, first make sure you have this flag set properly.

Further information on the GHC and GHCi options are in the manual off of the GHC web page.

How to get help[edit]

To get GHC(i) specific help, go to the GHC web page. To get general Haskell help, go to the Haskell web page.

NHC[edit]

About NHC...

Where to get it[edit]

Installation procedures[edit]

How to run it[edit]

Program options[edit]

How to get help[edit]

Editors[edit]

With a good text editor, programming is fun. Of course, you can get along with a simplistic editor capable of just cut-n-paste, but a good editor is capable of doing most of the chores for you, letting you concentrate on what you are writing. With respect to programming in Haskell, a good text editor should have as many as possible of the following features:

  • Syntax highlighting for source files
  • Indentation of source files
  • Interaction with Haskell interpreter (be it Hugs or GHCi)
  • Computer-aided code navigation
  • Code completion

At the time of writing, several options were available: Emacs/XEmacs support Haskell via haskell-mode and accompanying Elisp code (available from [2]), and ... . %todo: what else?

What's else available?...

(X)Emacs seem to do the best job, having all the features listed above. Indentation is aware about Haskell's 2-dimensional layout rules (see the section on Layout, very smart and have to be seen in action to be believed. You can quickly jump to the definition of chosen function with the help of "Definitions" menu, and name of the currently edited function is always displayed in the modeline. %todo: more?