OCaml Weekly News

Previous Week Up Next Week


Here is the latest OCaml Weekly News, for the week of May 05 to 12, 2020.

Table of Contents

Looking for "lovely, idiomatic" examples of Ocaml used for shell-scripting in the manner of Perl/Python (but esp. Perl)

Continuing this thread, Chet Murthy said and Aaron L. Zeng replied

  • needs to be Ocaml code, not an interpreter. I mean, if I’m not going to write it in Ocaml, I might as well write in Perl, yes?

I think shexp might deserve another look. It's not an interpreter for a sexp-based shell language, as its name might unfortunately deceivingly suggest. It's really a DSL for constructing shell pipelines using a 'a Process.t monad. The s-expression part is advertising that you can debug and trace the actions performed using s-expressions.

The second-most-important part of Perl/Bash scripting is string-handling. And it’s certainly the part of Ocaml that’s most painful when writing scripts. Let’s stipulate that there are nice libraries to make this easy. I’m an Ocaml bigot, I have to believe this anyway grin . This library doesn’t seem to use 'em, nor choose/promote a particular set of such libraries.

I've found Base plus Re to be sufficient for most of my string-manipulation needs. It's never going to be as concise as Perl's built-in "magic" support for regexps, but you gain explicitness and clarity, which is part of the benefit of OCaml anyway.

Chet Murthy said and Donn Cave replied

It’s not as trivial in Ocaml, for many complicated reasons that boil down to “gee, string-handling is a PITA”.

Really? hadn't noticed. Ha ha.

I could never really get urge for Perl, but I use its ancestor awk a lot, and I'm trying out some awk-like simple string functions, like

let strlen = String.length
let sub s i n = let b = strlen s
     in if i < b
         then let n = min n (b - i)
         in String.sub s i n
    else ""
(* substring to end of line *)
let substr a i = if i < strlen a
     then String.sub a i ((strlen a) - i)
     else ""
let matchre t s = try
     Str.search_forward t s 0
     with | Not_found -> -1


So "open Awk" gets me a handful of more basic variations on common string functions, with less elaborate parameters, no normal exceptions, etc. Including a line by line file processing function. I have just newly started on this and haven't used it extensively, but it seems fairly promising. No wacky syntax or hyper intelligent string processing, no packages, just a few dozen lines of cheater functions.

"Awk" is a misnomer, in that there's little correspondence between this and awk, it was just what inspired me to try it.

Raphaël Proust said

I don't think it's lovely and I have no idea if it is idiomatic, but I made a few scripts of my own in OCaml using the same library that other mentioned: bos

  • typepass uses xdotool to type passwords from the password password manager
  • conn wraps wpa_supplicant, dhcpcd, ip, and other network management CLI
  • laptop-status fetches status information for laptops (e.g., battery level) and prints it in a nicely formatted form
  • bakelite increases or decreases screen brightness

Vasile Rotaru also said

Gabriel Radanne also said

I have no particular opinion about the rest, but at least on the regex side, this might be of interest: https://github.com/paurkedal/ppx_regexp

If that's still not good enough, I would be very interested by suggestions on how to make it more convenient. :)

OCamlUser proposed

I'm not sure about idiomatic, but I do have a utop config that I use to do some one-off scripting in OCaml that uses shexp

#use "topfind"
#warnings "+a"
#require "ppx_jane,core"
#require "shexp.process"
#require "lambdasoup"
module List' = List
open Shexp_process
open Shexp_process.Infix
open Core

module Html = struct
    include Soup

    let of_string = parse

let read_lines cmd =
    eval (call cmd |- read_all)

let wget url =
    read_lines ["wget"; "-O"; "-"; url]

let chrome_curl url =
    read_lines ["curl"; "-k"; "-sA"; "Chrome"; "-L"; url; "-o"; "-"]

let split_lines = String.split ~on:'\n'
let filter_lines substring = List.filter ~f:String.(is_substring ~substring)
let to_html = Html.of_string
let find_html pat html = Html.(html $$ pat)

let (%) = Fn.compose

Then a simple script called shexp in my path:

utop -init ~/bin/ocaml-shexp-config

I add little helper functions as I come upon them. I find it's much easier to transition to a file, or full program when I need it. Example program:

utop # read_lines ["sensors"] |> split_lines |> filter_lines "Core 0";;
- : string list =
["Core 0:        +63.0°C  (high = +84.0°C, crit = +100.0°C)"]

Anton Kochkov said

Not exactly OCaml, but can be made with the OCaml syntax as well - see BATSH.

Bikal Lem also said

I just found this - https://github.com/ShamoX/cash. @Chet_Murthy This may be the closest to ocaml shell scripting experience re perl.

Are there learning materials for OCaml for those with no programming experience?

Continuing this threaad, Luc_ML said

Before studying more complex books, it's a good idea to first get an overview.

OCaml for the Skeptical / OCaml in a Nutshell : the title is funny; its main advantage is that it covers most OCaml concepts in 21 short sections where you can experiment by yourself on simple but essential things.

The books/courses already mentioned are nice. You can also consider this one that offers many examples/exercises and also a good overview: Developing Applications With Objective Caml.

LE LANGAGE CAML mentioned by @nojb is an excellent book. Written in Caml Light, it's easy to turn it by yourself into OCaml. It offers a great chance to learn how to do a lot of things in pure Caml with only stdlib and a simple syntax extension system (use camlp5 (i.e. the "genuine camlp4") that is fine for that. It works out of the box to deal with streams and it's a chance to understand what is a LL(1)/recursive descent parser).

Dune meeting notes

Jérémie Dimino announced

I just wanted to publicise that we are now publishing the notes from our Dune meetings on the wiki:


These meetings happen via video-conference every two weeks. If you are interested in following the development of Dune more closely, this is good place to look at.

OCaml 4.11.0, first alpha release

octachron announced

The set of new features for the future version 4.11.0 of OCaml has been frozen. In the next few months, the OCaml compiler team is focusing on bug hunting and fixing.

For this release cycle, we have decided to test publishing regularly alpha versions of OCaml 4.11.0 in order to help fellow hackers join us early in our bug hunting and opam ecosystem fixing fun. Once the opam ecosystem is in shape, these alpha releases will morph into the usual beta and release candidate releases.

If you find any bugs, please report them here: https://github.com/ocaml/ocaml/issues

The compiler can be installed as an OPAM switch with one of the following commands

opam switch create ocaml-variants.4.11.0+alpha1 --repositories=default,beta=git+https://github.com/ocaml/ocaml-beta-repository.git


opam switch create ocaml-variants.4.11.0+alpha1+VARIANT --repositories=default,beta=git+https://github.com/ocaml/ocaml-beta-repository.git

where you replace VARIANT with one of these: afl, flambda, fp, fp+flambda

The source code for the alpha is also available at these addresses:


If you are interested by the ongoing list of new features and fixed bugs, the updated change log for OCaml 4.11.0 is available at:


OCaml Users and Developers Meeting 2020

Ivan Gotovchits announced

Due to the multiple requests and since ICFP will be now officially held online with a significantly reduced fee, we decided to extend the submission deadline till the end of this month. We are hoping to attract a larger and more diverse audience this year, given that the new format is more accessible both travel-wise and financially.

Please, share the news widely!

Important Dates (updated)

  • Talk proposal submission deadline: May 29th, 2020, AoE
  • Author Notification: July 17th, 2020
  • OCaml Workshop: August 28th, 2020

VSCode Platform Plugin 0.5.0

Rudi Grinberg announced

This release contains a couple of major improvements:

  • Syntax highlighting is vastly improved. There's now highlighting for many more filetypes, and the core highlighting for OCaml is far more accurate.
  • There's integration with package managers such as opam and esy. One may now explicitly use them to explicitly select the sandbox that contains the lsp server and related tools.

Under the hood, the entire plugin was rewritten from typescript to OCaml (bucklescript). This should hopefully make contribution more accessible to OCaml hackers.

I'd like to thank @rustykey, @mnxn, @prometheansacrifice, and @imbsky for their contributions to this release. Their help is the reason for this vastly improved version of the plugin.

As usual, the plugin is available directly using vscode's extension market place. I'll leave a link to the plugin here to avoid confusion with the many other OCaml plugins available.

Please report any issues on the bug tracker

Other OCaml News

From the ocamlcore planet blog

Here are links from many OCaml blogs aggregated at OCaml Planet.


If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.