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"] inA.Axes.box vp ;A.set_color vp A.Color.red ;A.fx vp sin 0.0 10.0 ;A.close vp1.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 inlet vp = A.init ~w:560. ~h:260. ["jupyter"] inA.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 vp0.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 --importcd /tmpwget -q --show-progress --progress=bar:force $url $url.ascgpg --verify $file.asccp $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/opamchmod a+x /usr/local/bin/opammkdir -p $OPAMROOT# environment setup, install default version of the compileropam init -c $OCAML_VERSION -ay --disable-sandboxingopam config envln -s /opt/ocaml/$OCAML_VERSION /opt/ocaml/default220.2s
OCaml (Bash)
Check install, version.
which ocamlocaml -version0.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 reason0.6s
OCaml (Bash)
Install one dep for OCaml's Jupyter library.
apt-get -qq updateDEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends \ libgmp-devapt-get cleanrm -r /var/lib/apt/lists/* # Clear package list so it isn't stale6.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 || truedone216.5s
OCaml (Bash)
opam switch set $OCAML_VERSIONopam switch0.5s
OCaml (Bash)
jupyter kernelspec listdu -hsx /2.5s
OCaml (Bash)