ANU: The Australian National University
_____________________________________________________________________
[ANU] [FEIT] [DCS] [Jim Grundy] [COMP8033] [ML] [HOL] [Exercises] [Lectures] [Assignments]
_____________________________________________________________________

Exercise 1: Getting Started with ML

Setting Your Path

All the programs you will need to run on the HPC lab computers for this course are located in the one directory:

/home/dcs/jdg659/pub/bin

You'll be needing to run files from this directory quite often, so you should add it to your UNIX path. If you haven't modified the setup of your HPC lab account, then you can do this by adding the following line to your .cshrc file.

setenv PATH ${PATH}:/home/dcs/jdg659/pub/bin
Any new shells you start will now have their path set correctly.

Starting ML (without HOL or Emacs)

ML is a powerful programming language. The HOL theorem prover is essentially ML plus a great many definitions. However, you may wish to use ML on its own. To start ML just type the command `mosml' at the UNIX prompt. This should start the Moscow ML system.

Try starting ML now. If you succeed, you should see the following:

> mosml
Moscow ML version 1.43 (April 1998)
Enter `quit();' to quit.
-
Note: `>' is the UNIX prompt and `-' is the ML prompt.

Evaluating Expressions

ML code can be interpreted or compiled. When you see the `-' prompt you can type an expression (terminated by `;'), the interpreter will then evaluate it and print the result. Try evaluating some simple expressions like these:

Notice that the interpreter also prints the type along with the value of the result. Functions in ML are first-class objects; that is, they are also values. Try evaluating the expression rev. What was its type?

Making Definitions

You can bind a value to a name in ML by typing

val name = expression;
The expression will be evaluated and the result bound to the name. You can then use the name in other expressions. For example:
- val one = 1;
> val one = 1 : int
- one + one;
> val it = 2 : int

You are free to free to redefine names, for example, you could now say:

- val one = 2;
> val one = 2 : int
- one + one;
> val it = 4 : int

Notice that if you just evaluate an expression without binding it to a name, then ML will automatically bind it to the name it for you. Each evaluation redefines it, so it is always the value of the last expression evaluated. Check this for yourself by evaluating some expression and then evaluating it.

Functions

ML allows you to write function valued expressions (effectively, lambda-abstractions). A function that takes some value x and returns (x + 1) is written as follows:

fn x => x + 1
Use the val keyword to bind this function to the name inc, then evaluate inc 0.

Another, more usual, way to define functions is as follows:

fun name x = expression;
Think of this as a short-hand notation for the definition below:
val name = fn x => expression;
Try redefining inc in this style.

Quitting ML

To exit ML just type `quit();' or C-d at the `-' prompt.

_____________________________________________________________________

[ANU] [FEIT] [DCS] [Jim Grundy] [COMP8033] [ML] [HOL] [Exercises] [Lectures] [Assignments]
_____________________________________________________________________
Feedback & Queries: Jim.Grundy@anu.edu.au
Date Last Modified: Wed 22 Mar 2000
Universal Resource Locator: http://cs.anu.edu.au/student/comp8033/ex01.html
Copyright © 2000 The Australian National University
All rights reserved