Skip to content

Latest commit

 

History

History
106 lines (85 loc) · 3.36 KB

README.md

File metadata and controls

106 lines (85 loc) · 3.36 KB

2. Hello World

(← Setup) (Parallelism and Granularity Control →)

Preliminaries

Make sure that you've already done the setup. If you're using Docker to run the tutorial, all commands below should be run within the container in directory ~/mpl-tutoral/02-hello/:

$ cd path/to/mpl-tutorial
$ ./start-container.sh
<container># cd 02-hello
<container># <enter commands here>

Write it

Our first program is a one-liner: the function print takes a string as argument and writes it to the terminal. Note that in SML, it is common to call a function without putting parentheses around its arguments (e.g. f x instead of f(x)).

mpl-tutorial/02-hello/hello.sml:

val _ = print "hello world\n"
Question: What does val _ = mean?
Normally, we use the syntax val ... = ... to introduce a new variable. For example, val x = 2+2.

But in this case, print doesn't return anything interesting, so we just write val _ = print ... which means "print the thing, but don't introduce a new variable for the result".

Compile and run it

To compile this file, pass it to mpl at the command-line. This produces an executable called hello. By default, mpl names the executable the same as the source file. We can tell it to use a different name with the -output flag.

<container># ls
README.md  hello-again.sml  hello-twice.mlb  hello.sml

<container># mpl hello.sml
<container># ./hello
hello world

<container># mpl -output foobar hello.sml
<container># ./foobar
hello world

Compiling multiple files as one program

.mlb Files. Typically, we don't write programs as just a single .sml file. Instead, we write multiple separate files and then compile them together as one program. To do this with MPL, we need to write an additional file that describes how to put the files together. This additional file is called an ML Basis File, and has the extension .mlb.

For example, take a look at hello-twice.mlb, which tells MPL to load three things: the SML basis library, and two files: hello.sml followed by hello-again.sml.

mpl-tutorial/02-hello/hello-twice.mlb:

$(SML_LIB)/basis/basis.mlb
hello.sml
hello-again.sml

We can pass an .mlb file directly to MPL to produce an executable, similar to before.

<container># mpl hello-twice.mlb
<container># ./hello-twice
hello world
hello again
Question: What is this $(SML_LIB)/basis/basis.mlb thing?
This loads the SML basis library, which is the standard library included in all SML distributions. It includes the definition of important functions such as print.

When we compile a .sml file by itself, the basis library is implicitly included for convenience. But when we use a .mlb, we have to be more explicit. This way, our .mlb file describes everything about our program. No hidden pieces!