#+title:  Emacs OCaml Integration
#+author: Howard X. Abrams
#+date:   2022-08-24
#+tags: emacs ocaml

A literate programming file for integrating the [[https://ocaml.org/][OCaml programming language]].
#+begin_src emacs-lisp :exports none
  ;;; ha-programming-ocaml --- integrating OCaml -*- lexical-binding: t; -*-
  ;;
  ;; © 2022-2023 Howard X. Abrams
  ;;   Licensed under a Creative Commons Attribution 4.0 International License.
  ;;   See http://creativecommons.org/licenses/by/4.0/
  ;;
  ;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
  ;; Maintainer: Howard X. Abrams
  ;; Created: August 24, 2022
  ;;
  ;; While obvious, GNU Emacs does not include this file or project.
  ;;
  ;; *NB:* Do not edit this file. Instead, edit the original literate file at:
  ;;            ~/other/hamacs/ha-programming-ocaml.org
  ;;       And tangle the file to recreate this one.
  ;;
  ;;; Code:
  #+end_src

* Introduction
After reading [[    https://batsov.com/articles/2022/08/19/learning-ocaml/][Batsov's OCaml overview essay]], I became intrigued with learning more about it. Like him, I’ll probably start learning OCaml using the following resources:
  * [[https://ocaml.org/docs/up-and-running][Getting Started]]: At the primary ocaml website.
  * [[https://dev.realworldocaml.org/][Real World OCaml]]: A free book about OCaml, that's considered one of the best starting points in the community.
  * [[https://cs3110.github.io/textbook/cover.html][Cornell University's course on OCaml]]: It's taught at the university, but the textbook and the video lectures are freely available online.
  * [[https://ocamlverse.github.io/][OCamlverse]]: An OCaml community wiki.

As these [[http://dev.realworldocaml.org/install.html][installation instructions state]], the primary binary to install is the /package manager/, like:
#+begin_src sh
  brew install opam
#+end_src

Initialize the opam package database by running:
#+begin_src sh
  opam init
#+end_src

The book suggests installing utop:
#+begin_src sh
  opam install core core_bench utop
#+end_src

The book also mentions that opam comes with a user-setup package to install Emacs configs. You can install it as follows, along with some related packages:
#+begin_src sh
  opam install user-setup tuareg ocamlformat merlin
  opam user-setup install
#+end_src
But I won’t be doing that, as I can configure it.
* Basics
I kicked off this code by following with [[https://batsov.com/articles/2022/08/23/setting-up-emacs-for-ocaml-development/][these instructions]] from Batsov.

Use the [[https://github.com/ocaml/tuareg][tuareg package]] for OCaml programming:
#+begin_src emacs-lisp
  (use-package tuareg
    :mode (((rx ".ocamlinit" eos) . tuareg-mode)))
#+end_src

Use [[https://github.com/ocaml/dune/blob/main/editor-integration/emacs/dune.el][dune.el]] to edit Dune project files:
#+begin_src emacs-lisp
  (use-package dune)
#+end_src

[[https://github.com/ocaml/merlin][Merlin]] provides advanced IDE features:
#+begin_src emacs-lisp
  (use-package merlin
    :config
    (add-hook 'tuareg-mode-hook #'merlin-mode)
    ;; we're using flycheck instead
    (setq merlin-error-after-save nil))

  (use-package merlin-eldoc
    :hook ((tuareg-mode) . merlin-eldoc-setup))
#+end_src

The flycheck package uses Merlin internally:
#+begin_src emacs-lisp
  (use-package flycheck-ocaml
    :config (flycheck-ocaml-setup))
#+end_src

If I get into [[https://opam.ocaml.org/blog/about-utop/][utop]], I can also setup some integration with it like this:
#+begin_src emacs-lisp
  (use-package utop
    :config
    (add-hook 'tuareg-mode-hook #'utop-minor-mode))
#+end_src
* Technical Artifacts                                :noexport:
Let's =provide= a name so we can =require= this file:
#+begin_src emacs-lisp :exports none
  (provide 'ha-programming-ocaml)
  ;;; ha-programming-ocaml.el ends here
  #+end_src

#+description: integrating OCaml
#+property:    header-args:sh :tangle no
#+property:    header-args:emacs-lisp  :tangle yes
#+property:    header-args    :results none :eval no-export :comments no mkdirp yes

#+options:     num:nil toc:t todo:nil tasks:nil tags:nil date:nil
#+options:     skip:nil author:nil email:nil creator:nil timestamp:nil
#+infojs_opt:  view:nil toc:t ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
