Nextjournal / Mar 05 2020
OCaml Environment
This notebook describes and creates the default OCaml environment in Nextjournal. Check out the showcase if you want to see what the environment contains. To see how it’s built, see setup.
Showcase
#use "topfind"
first allows easy package loading via the #require
directive.
#use "topfind"
#require "jupyter.notebook"
1.0s
OCaml 4.08.0
OCaml
Jupyter_notebook.display "text/html" "<b>Hello, world!</b>"
0.0s
OCaml 4.08.0
OCaml
Hello, world!
- : Jupyter_notebook.display_id = <abstr>
The Archimedes package provides plot capabilities.
#require "jupyter-archimedes"
0.5s
OCaml 4.08.0
OCaml
let vp = A.init ~w:560. ~h:260. ["jupyter"] in
A.Axes.box vp ;
A.set_color vp A.Color.red ;
A.fx vp sin 0.0 10.0 ;
A.close vp
1.0s
OCaml 4.08.0
OCaml
- : unit = ()
let rec walk acc p i =
if i = 0 then List.rev acc else begin
let q = if Random.bool () then succ p else pred p in
walk (q :: acc) q (pred i)
end in
let vp = A.init ~w:560. ~h:260. ["jupyter"] in
A.Axes.box vp ;
A.set_color vp A.Color.red ;
A.List.y vp ~style:`Lines (List.map float @@ walk [] 0 1000) ; (* Plot a random walk of 1000 points. *)
A.close vp
0.7s
OCaml 4.08.0
OCaml
- : unit = ()
Setup
Download and verify OPAM.
file="opam-${OPAM_VERSION}-x86_64-linux"
url="https://github.com/ocaml/opam/releases/download/${OPAM_VERSION}/${file}"
curl https://keybase.io/altgr/pgp_keys.asc | gpg --import
cd /tmp
wget -q --show-progress --progress=bar:force $url $url.asc
gpg --verify $file.asc
cp $file /results/
4.1s
OCaml (Bash)
Install OPAM, then use it to install OCaml.
cp opam-2.0.6-x86_64-linux /usr/local/bin/opam
chmod a+x /usr/local/bin/opam
mkdir -p $OPAMROOT
# environment setup, install default version of the compiler
opam init -c $OCAML_VERSION -ay --disable-sandboxing
opam config env
ln -s /opt/ocaml/$OCAML_VERSION /opt/ocaml/default
220.2s
OCaml (Bash)
Check install, version.
which ocaml
ocaml -version
0.6s
OCaml (Bash)
Install Reason.
opam install -y \
'ocamlfind>=1.8.1' 'ocaml-compiler-libs>=0.12.1' \
'reason>=3.5.2' 'yojson>=1.7.0'
76.9s
OCaml (Bash)
Was Reason successfully installed?
opam info reason
0.6s
OCaml (Bash)
Install one dep for OCaml's Jupyter library.
apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends \
libgmp-dev
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale
6.7s
OCaml (Bash)
opam install -y jupyter 'jupyter-archimedes>=2.3.2' 'archimedes>=0.4.18'
jupyter kernelspec install \
--name ocaml-reason "$(opam config var share)/jupyter"
149.1s
OCaml (Bash)
Install any additional compiler versions.
for ver in $OCAML_XV; do
opam switch create $ver || true
done
216.5s
OCaml (Bash)
opam switch set $OCAML_VERSION
opam switch
0.5s
OCaml (Bash)
jupyter kernelspec list
du -hsx /
2.5s
OCaml (Bash)