Skip to content

Instantly share code, notes, and snippets.

@akabe
akabe / bucklescript_headless_chrome.ml
Last active January 11, 2018 04:45
A short example of bindings of `chrome-launcher` and `chrome-remote-interface` in OCaml BuckleScript
(* This is a short example of bindings of `chrome-launcher` and `chrome-remote-interface`
(for node libraries manipulating headless-mode Google Chrome) in OCaml BuckleScript (https://bucklescript.github.io/).
Usage:
$ npm install -g chrome-launcher chrome-remote-interface
$ bsc -bs-main bucklescript_headless_chrome.ml
Headless browsers (such as PhantomJS, Chrome, Firefox) are useful for, e.g.,
- integration tests of JavaScript products on a real browser, or
- Web scraping for pages containing complex JavaScript.
@akabe
akabe / install_opam.sh
Last active October 12, 2018 02:43
OPAM/OCaml scripts for CI
#!/usr/bin/env sh -xeu
##
## Usage:
## curl -sL https://gist.githubusercontent.com/akabe/24979afbf95c4cf4393f589cda997e1b/raw/install_opam.sh | sh -xeu
##
## Variables:
## - $OPAM_PREFIX Path to install opam
## - $OPAM_VERSION Version of OPAM to be installed
## - $OCAML_VERSION Version of OCaml to be installed
@akabe
akabe / centos-zmq.sh
Created July 28, 2017 03:41
Install zeromq-devel on CentOS7
#!/bin/sh -xeu
sudo yum install -y epel-release
sudo yum install -y zeromq-devel
@akabe
akabe / extensible_record_by_lens.ml
Last active September 4, 2019 01:43
Extensible record by Lens trick in OCaml
(** The Lens trick: getter and setter for fields *)
type ('s, 'v) lens =
{
get: 's -> 'v;
set: 'v -> 's -> 's;
}
let id_lens = {
get = (fun x -> x);
set = (fun v _ -> v);
@akabe
akabe / SafeList.scala
Last active June 24, 2016 00:48
A simple example of type-safe head, tail, and zip of List by Peano-style type-level natural number via phantom type in Scala
object SafeList {
trait Z // phantom type "zero" (corresponding to 0)
trait S[N] // phantom type "successor" (correspoding to n => n+1)
class SList[N, E] private[SafeList] (private[SafeList] val list: List[E])
// SList[N, E] is a list of elements of type E, and length N.
def empty[E] = new SList[Z, E](Nil)
def cons[N, E] (x: E, xs: SList[N, E]) = new SList[S[N], E](x :: xs.list)
@akabe
akabe / subtyping2.ml
Created November 19, 2015 11:39
A subtyping encoding by phantom types
open Format
(** The types of the source language
(Hindley-Milner + subtyping + bounded polymorphism) *)
module SL =
struct
type 'a typ =
| Base of 'a (** base type *)
| Var of string (** type variable *)
| Arrow of 'a typ * 'a typ (** function type *)
@akabe
akabe / subtyping1.ml
Last active November 22, 2015 06:51
A subtyping encoding by phantom types [Fluet and Pucella, JFP 2006]
open Format
(** The types of the source language
(Hindley-Milner + subtyping + bounded polymorphism) *)
module SL =
struct
type 'a typ =
| Base of 'a (** base type *)
| Var of string (** type variable *)
| Arrow of 'a typ * 'a typ (** function type *)
@akabe
akabe / generative_phantom_type_first_class_polymorphism.ml
Created October 29, 2015 07:19
Static size checking for lists (loaded from files) by generative phantom types (first-class-polymorphism version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in
@akabe
akabe / generative_phantom_type_GADT.ml
Created October 29, 2015 07:06
Static size checking for lists (loaded from files) by generative phantom types (GADT version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in
@akabe
akabe / generative_phantom_type_first_class_module.ml
Created October 29, 2015 06:58
Static size checking for lists (loaded from files) by generative phantom types (first-class-module version)
(* http://akabe.github.io/2015/10/GenerativePhantomTypes *)
#load "str.cma";;
(** Loads a list of integers from a file of a given path (delimiters = spaces,
tabs, or line feeds). *)
let load_list fname =
let re = Str.regexp "[ \t]+" in
let oc = open_in fname in
let rec aux acc = try aux (input_line oc :: acc) with End_of_file -> acc in