07.01.2014 Views

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> <strong>Glorious</strong> <strong>Glasgow</strong> <strong>Haskell</strong><br />

<strong>Compilation</strong> <strong>System</strong> User’s <strong>Guide</strong>,<br />

Version 6.10.4 31 / 224<br />

:def[!] [name expr] :def is used to define new commands, or macros, in GHCi. <strong>The</strong> command :def name expr<br />

defines a new GHCi command :name, implemented by the <strong>Haskell</strong> expression expr, which must have type String -><br />

IO String. When :name args is typed at the prompt, GHCi will run the expression (name args), take the resulting<br />

String, and feed it back into GHCi as a new sequence of commands. Separate commands in the result must be separated<br />

by ‘\n’.<br />

That’s all a little confusing, so here’s a few examples. To start with, here’s a new GHCi command which doesn’t take any<br />

arguments or produce any results, it just outputs the current date & time:<br />

Prelude> let date _ = Time.getClockTime >>= print >> return ""<br />

Prelude> :def date date<br />

Prelude> :date<br />

Fri Mar 23 15:16:40 GMT 2001<br />

Here’s an example of a command that takes an argument. It’s a re-implementation of :cd:<br />

Prelude> let mycd d = Directory.setCurrentDirectory d >> return ""<br />

Prelude> :def mycd mycd<br />

Prelude> :mycd ..<br />

Or I could define a simple way to invoke “ghc --make Main” in the current directory:<br />

Prelude> :def make (\_ -> return ":! ghc --make Main")<br />

We can define a command that reads GHCi input from a file. This might be useful for creating a set of bindings that we<br />

want to repeatedly load into the GHCi session:<br />

Prelude> :def . readFile<br />

Prelude> :. cmds.ghci<br />

Notice that we named the command :., by analogy with the ‘.’ Unix shell command that does the same thing.<br />

Typing :def on its own lists the currently-defined macros. Attempting to redefine an existing command name results in<br />

an error unless the :def! form is used, in which case the old command with that name is silently overwritten.<br />

:delete * | num ... Delete one or more breakpoints by number (use :show breaks to see the number of each<br />

breakpoint). <strong>The</strong> * form deletes all the breakpoints.<br />

:edit [file] Opens an editor to edit the file file, or the most recently loaded module if file is omitted. <strong>The</strong> editor to<br />

invoke is taken from the EDITOR environment variable, or a default editor on your system if EDITOR is not set. You can<br />

change the editor using :set editor.<br />

:etags See :ctags.<br />

:force identifier ... Prints the value of identifier in the same way as :print. Unlike :print, :force<br />

evaluates each thunk that it encounters while traversing the value. This may cause exceptions or infinite loops, or further<br />

breakpoints (which are ignored, but displayed).<br />

:forward Move forward in the history. See Section 2.5.5. See also: :trace, :history, :back.<br />

:help , :?<br />

Displays a list of the available commands.<br />

: Repeat the previous command.<br />

:history [num] Display the history of evaluation steps. With a number, displays that many steps (default: 20). For use<br />

with :trace; see Section 2.5.5.<br />

:info name ... Displays information about the given name(s). For example, if name is a class, then the class methods and<br />

their types will be printed; if name is a type constructor, then its definition will be printed; if name is a function, then its<br />

type will be printed. If name has been loaded from a source file, then GHCi will also display the location of its definition<br />

in the source.<br />

For types and classes, GHCi also summarises instances that mention them. To avoid showing irrelevant information, an<br />

instance is shown only if (a) its head mentions name, and (b) all the other things mentioned in the instance are in scope<br />

(either qualified or otherwise) as a result of a :load or :module commands.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!