Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up and running with OCaml #1165

Merged
merged 14 commits into from
Nov 9, 2020
Merged
Binary file added site/img/vscode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<h1 class="span9">OCaml is an industrial strength programming language supporting functional, imperative and object-oriented styles</h1>
<div class="span3">
<div>
<a class="btn" href="/docs/install.html">Install OCaml</a>
<a class="btn" href="/learn/tutorials/up_and_running.html">Install OCaml</a>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions site/learn/index.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ OCaml est un langage générique de programmation, de puissance industrielle, qu
<section class="span4 condensed">
<h1 class="ruled"><a href="tutorials/">Tutoriaux</a> &amp; <a href="faq.html">FAQ</a></h1>
<ul>
<li><a href="tutorials/up_and_running.html">Opérationnel avec OCaml</a></li>
<li><a href="tutorials/basics.fr.html">Les bases syntaxiques</a></li>
<li><a href="tutorials/structure_of_ocaml_programs.fr.html">Les constructions de base</a></li>
<li><a href="tutorials/modules.fr.html">Les modules</a></li>
Expand Down
1 change: 1 addition & 0 deletions site/learn/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<section class="span4 condensed">
<h1 class="ruled"><a href="tutorials/">Tutorials</a> &amp; <a href="faq.html">FAQ</a></h1>
<ul>
<li><a href="tutorials/up_and_running.html">Up and Running</a></li>
<li><a href="tutorials/basics.html">Basics</a></li>
<li><a href="tutorials/structure_of_ocaml_programs.html">Structure of OCaml Programs</a></li>
<li><a href="tutorials/modules.html">Modules</a></li>
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.de.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

((! get begin_two_columns !))

* [Mit OCaml in Betrieb](up_and_running.html)
* [Die Grundlagen](basics.de.html) Sollten Sie ein Anfänger in OCaml oder
funktionaler Programmierung sein, lesen Sie bitte dieses Kapitel
zuerst.
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

((! get begin_two_columns !))

* [Opérationnel avec OCaml](up_and_running.html)
* [Notions de base](basics.fr.html)
— Si vous débutez OCaml ou la programmation fonctionnelle, lisez
ceci d'abord.
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.it.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

((! get begin_two_columns !))

* [Funzionante con OCaml](up_and_running.html)
* [Le basi](basics.it.html) — Se sei nuovo di OCaml o della
programmazione funzionale, leggi questo per primo.
* [La struttura dei programmi OCaml](structure_of_ocaml_programs.it.html)
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### はじめに

* [Up and Running](up_and_running.html)
* [はじめの一歩](basics.ja.html) — 基本中の基本
* [OCamlプログラムの構造](structure_of_ocaml_programs.ja.html)
* [モジュール](modules.ja.html)
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

((! get begin_two_columns !))

* [Up and Running](up_and_running.html)
* [기본사항](basics.ko.html)
* [OCaml 프로그램의 구조](structure_of_ocaml_programs.ko.html)
* 모듈(Modules) - [en](modules.html)
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tracker there to request or offer new tutorials. Thanks!

### Getting Started

* [Up and Running](up_and_running.html)
* [Basics](basics.html)
* [Structure of OCaml Programs](structure_of_ocaml_programs.html)
* [Modules](modules.html)
Expand Down
1 change: 1 addition & 0 deletions site/learn/tutorials/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ OCaml是一种快速、简洁、而强大的应用程序开发语言--我想

((! get begin_two_columns !))

* [Up and Running](up_and_running.html)
* [基础](basics.zh.html) — 如果你是OCaml或函数式语言的初学者请先阅读本节。
* [Ocaml程序的结构](structure_of_ocaml_programs.zh.html)
* [模块](modules.zh.html)
Expand Down
230 changes: 230 additions & 0 deletions site/learn/tutorials/up_and_running.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<!-- ((! set title Up and Running with OCaml !)) ((! set learn !)) -->

*Table of contents*

# Up and Running with OCaml

This page will help you install OCaml, the Dune build system, and support for
your favourite text editor or IDE. These instructions work on Windows, Unix
systems like Linux, and MacOS.

## Installing OCaml

There are two procedures: one for Unix-like systems, and one for Windows.

### For Linux and MacOS

We will install OCaml using OPAM, the OCaml package manager. OPAM will also be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We will install OCaml using OPAM, the OCaml package manager. OPAM will also be
We will install OCaml using opam, the OCaml package manager. opam will also be

opam should be lower case

used when we wish to install third-party OCaml libraries.

**For MacOS**

```
# Homebrew
brew install gpatch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this gpatch step necessary? Just brew install opam should be sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the MacOS instructions from here:

https://opam.ocaml.org/doc/Install.html

So if they are out of date, we need to fix them there too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have asked for an update here:

ocaml/opam#3433

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have removed the instruction on the assumption your patch will be approved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR for the opam install page:

ocaml/opam#4421

brew install opam

# MacPort
port install opam
```

**For Linux** the preferred way is to use your system's package manager on
Linux (e.g `apt-get install opam` or similar). [Details of all installation
methods.](https://opam.ocaml.org/doc/Install.html)

Then, we install an OCaml compiler:

```
# environment setup
opam init
eval `opam env`

# install given version of the compiler
opam switch create 4.11.1
eval `opam env`
```

Now, OCaml is up and running:

```
$ which ocaml
/Users/frank/.opam/4.11.1/bin/ocaml

$ ocaml -version
The OCaml toplevel, version 4.11.1
```

**For either Linux or MacOS** as an alternative, a binary distribution of OPAM is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**For either Linux or MacOS** as an alternative, a binary distribution of OPAM is
**For either Linux or macOS** as an alternative, a binary distribution of opam is

available:

```
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
```

### For Windows

We use the [OCaml for Windows](https://fdopen.github.io/opam-repository-mingw/)
installer which comes in 32bit and 64bit versions. This installer gives you
OPAM and OCaml installations all in one go. It is used from within a Cygwin
environment, but the executables produced have no dependency on Cygwin at all.

## The OCaml top level

OCaml comes with two compilers: for native code, and for byte code. We shall
use one of those in a moment. But first, let's use OCaml's top level (sometimes
known as a REPL in other languages):

```
$ ocaml
OCaml version 4.11.1

# 1 + 2 * 3;;
- : int = 7

```

We typed the phrase `1 + 2 * 3` and then signalled to OCaml that we had
finished by typing `;;` followed by the Enter key. OCaml calculated the
result, `7` and its type `int` and showed them to us. We exit by running the
built-in `exit` function with exit code 0:

```
$ ocaml
OCaml version 4.11.1

# 1 + 2 * 3;;
- : int = 7
# exit 0;;
$
```

There are two ways to improve your experience with the OCaml top level: you can
install the popular [`rlwrap`](https://github.com/hanslub42/rlwrap) on your
system and invoke `rlwrap ocaml` instead of `ocaml` to get line-editing
facilities inside the OCaml top level, or you can install the alternative top
level `utop` using OPAM:

```
$ opam install utop
```

We run it by typing `utop` instead of `ocaml`. You can read more about
[utop](https://github.com/ocaml-community/utop).

## Installing the Dune build system

Dune is a build system for OCaml. It takes care of all the low level details of
OCaml compilation. We install it with OPAM:

```
$ opam install dune
The following actions will be performed:
- install dune 2.7.1

<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><>
[default] https://opam.ocaml.org/2.0.7/archives/dune.2.7.1+opam.tar.gz
downloaded

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><>
-> installed dune.2.7.1
Done.
```

## A first project

Let's begin the simplest project with Dune and OCaml. We create a new directory
and ask `dune` to initialise a new project:

```
$ mkdir helloworld
$ cd helloworld/
$ dune init exe helloworld
Success: initialized executable component named helloworld
```

Building our program is as simple as typing `dune build`:

```
$ dune build
Info: Creating file dune-project with this contents:
| (lang dune 2.7)
Done: 8/11 (jobs: 1)
```

When we change our program, we type `dune build` again to make a new
executable. We can run the executable with `dune exec` (it's called
`helloworld.exe` even when we're not using Windows):

```
$ dune exec ./helloworld.exe
Hello, World!
```

Let's look at the contents of our new directory. Dune has added the
`helloworld.ml` file, which is our OCaml program. It has also added our `dune`
file, which tells dune how to build the program, and a `_build` subdirectory,
which is dune's working space.

```
$ ls
_build dune helloworld.ml
```

The `helloworld.exe` executable is stored inside the `_build` structure, so
it's easier to run with `dune exec`. To ship the executable, we can just copy
it from inside `_build` to somewhere else.

Here is the contents of the automatically-generated `dune` file. When we want
to add components to your project, such as third-party libraries, we edit this
file.

```
(executable
(name helloworld))
```

## Editor support for OCaml

For **Visual Studio Code**, and other editors support the Language Server
Protocol, the OCaml language server can by installed with OPAM:

```
$ opam pin add ocaml-lsp-server https://github.com/ocaml/ocaml-lsp.git
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pin is no longer necessary, as lsp has been released into opam

$ opam install ocaml-lsp-server
```

Now, we install the OCaml Platform Visual Studio Code extension from the Visual
Studio Marketplace.

Upon first loading an OCaml source file, you may be prompted to select the
toolchain in use: pick OCaml the version of OCaml you are using, e.g. 4.11.1
from the list. Now, help is available by hovering over symbols in your program:

![Visual Studio Code](/img/vscode.png "")

**On Windows**, we must launch Visual Studio Code from within the Cygwin window,
rather than by clicking on its icon (otherwise, the language server will not be
found):

```
$ /cygdrive/c/Users/Frank\ Smith/AppData/Local/Programs/Microsoft\ VS\ Code/Code.exe
```

**For Vim and Emacs**, install the [Merlin](https://github.com/ocaml/merlin)
system using OPAM:

```
$ opam install merlin
```

The installation procedure will print instructions on how to link Merlin with
your editor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worth it to mention opam user-setup rather than refer the reader to some unspecified documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation is what opam install merlin prints out - I don't paste it into the tutorial, but I'm not sure how it's unspecified?

Is user-setup up to date? It looks not to have been updated in a while. Is it widely used? Do the merlin developers recommend it? I'm looking for consensus when writing this tutorial, above all.

Do you suggest just mentioning it, or making it the default instruction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reader cannot infer how involved is the remaining installation procedure with just a "Follow the printed instructions". And user-setup, it is still one of the option cited by merlin post-install and is mentioned in the merlin quick setup section. Moreover, it has the advantage to be editor independent which means that we can give the reader the information that the quick setup can be as quick as

opam user-setup install

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. What's the Windows status?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to deal with this in a followup PR, as I suspect noone is entirely sure what the opam user-setup status is. CCing @emillon who has been investigating the Windows support of Platform tools recently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opam user-setup seems to start on windows, but displays some error messages and tries to detect ocamltop, emacs, vim, gedit and sublime3. I don't think it does something windows specific at the moment.


**On Windows**, when using Vim, the default cygwin Vim will not work with
Merlin. You will need install Vim separately. In addition to the usual
instructions printed when installing Merlin, you may need to set the PATH in
Vim:

```
let $PATH .= ";".substitute(system('opam config var bin'),'\n$','','''')
```