who am i even? OCaml Programmers Wanted Dead or Alive Write software in your favorite language for real Systems Hiwi-jobs and Theses available http://www6.in.tum.de/Main/Weissmam only From Technische Universität München Introduction to Programming lecture with OCaml Then, Bachelor Thesis: OCaml Now, Master Thesis: OCaml 1
quick feature list Static Hindley-Milner type system with inference Powerful module system Fast in the average case Strict Easy to learn Quick native and byte code compilers Wide variety of platforms supported One single language (not 70 language extensions) Macro system Good backwards compatibility 3
from a haskell perspective Language with much gentler learning curve Module system: OCaml offers modules that can be parametrized with other modules →Functors 5
js_of_ocaml Take OCaml byte code, generate JavaScript code Surprisingly fast OCaml REPL in browser Alt-Ergo — a SMT solver in OCaml — running in browser Coupled with Ocsigen: OCaml on the front and backend, code sharing 6
projects using ocaml You might have heard some of these names 0install Pfft Flow Hack Haxe Rust (previously) CompCert Frama-C FFTW Unison MLDonkey Coq Alt-Ergo Opalang Grenchman OCaml is a popular language for writing compilers and verification tools. 9
this talk So we saw that it seems to be pretty cool. Why is not everyone all over it? Most difficult part: getting started. Let me lend you a hand and give you some guidance. 10
real world ocaml The best OCaml book ever published For more experienced developers Broad amount of topics covered: functional programming object orientation, runtime Uses Jane Street’s Core Standard library Freely available online 12
ocaml from the very beginning Introduction into functional programming More geared towards novices A bit like ”The Little Schemer” Companion book ”More OCaml” available 13
building code sucks Getting inter-project dependencies Getting inter-file dependencies Incrementally rebuilding Build times Compiler options In Haskell, this is all handled by Cabal 19
building ocaml code used to suck .ml/.mli: source files .o: Compiled C implementations .cmo/.cmi: Compiled modules .cma/.cmax: Compiled module archives, native archives .cmt/.cmti: Type information annotation ocamlc, ocamlc.opt ocamlopt, ocamlopt.opt ocamldep … Need to compile each module, specify them in the proper order. 20
ocamlfind The OCaml compiler knows no packages, just modules. Additional tool by Gerd Stolpman ocamlfind finds packages Wrapper around commandline tools, ocamlfind ocamlopt Adds a -package option Reads information from META file Universally established tool Package/dependency problem solved. 21
ocamlbuild Previously: no build system shipped. Cue: OMake, obuild, ocp-build, jenga, GNU Make. OCaml 3.12 came with ocamlbuild Ships with default rules to invoke compiler toolchain _tags file for customization Quite alright for small to medium projects Popular but not ubiquitous 22
enter oasis ”Architecture for building OCaml libraries and applications” _oasis file with definitions Can generate files for ocamlfind, ocamlbuild Supports other systems via plugin (in theory) ”One system to bind them” 24
unit testing Most popular library is OUnit. open OUnit2 let test_commutative test_ctx = assert_equal (1+2) (2+1) let suite = ”Example” >::: [ ”addition” >:: test_commutative; ] let _ = run_test_tt_main suite pa_ounit syntax extension for less overhead. 28
specification testing Let’s check whether OCaml integers are commutative open QCheck let test = mk_test ~name:”commutative” Arbitrary.(pair small_int small_int) (fun (a, b) -> a + b = b + a) let _ = ignore @@ run test The Kaputt package supports also enumeration-based testing 29
continuous integration ”It works on my machine” Better build it independently on Travis CI. Add this to your .travis.yml, ocaml-travisci-skeleton will take care of the rest (as long as your package is OPAMized). language: c install: wget https://…/.travis-opam.sh script: bash -ex .travis-opam.sh env: - OCAML_VERSION=4.02 - OCAML_VERSION=4.01 - OCAML_VERSION=4.00 - OCAML_VERSION=3.12 30
the need for distribution Do not reinvent the wheel, let’s use existing libraries! So, if OASIS is like Cabal, what is the equivalent to Hackage? There was a project called OASIS-DB, but it didn’t work out. 32
opam Lessons learned from OASIS-DB. OCaml PAckage Manager Manages building compiler and packages Uses community-maintained Git repo with build recipes Describes packages: Version, source tarball, hash Dependencies, conflicts Installation scripts & patches SAT solver to solve requests Does not enforce any build system, project structure Handles updates and removals 33
projects need documentation OCaml goes the Javadoc route: ocamldoc. Shipped with the compiler. (** Documentation of function @param arg1 What the param is for @param arg2 More documentation *) Best to be used on .mli (module interface) files. Uses custom documentation markup format. Future: OPAM-Doc to generate documentation of every installed package. 36
concurrency Multithreaded OCaml runtime not yet a thing. In the works, but no definite timeframe. Libraries to parallelize execution to multiple processes exist. Who cares about threads, right? Async evented I/O! Monadic concurrency libraries. 1. Lwt 2. Async 39
lwt in action let channels_history token ?count channel = let%lwt channel_id = id_of_channel token channel in endpoint ”channels.history” |> definitely_add ”token” token |> definitely_add ”channel” channel_id |> optionally_add ”count” @@ maybe string_of_int count |> query >|= function | ‘Json_response d -> d |> history_obj_of_yojson | #history_result as res -> res | _ -> ‘Unknown_error let%lwt is processed by a macro to work like bind. 40
jvm .NET has F# (basically OCaml for .NET), a statically typed ML. It’s a serious effort by Microsoft. JVM users have… Yeti? Scala? OCaml-Java generates JVM bytecode based on OCaml 4.01 faster than byte code slower than native code early effort 41
debugging ocamldebug exists Works on bytecode Can breakpoint on time and even travel back Tends to step into Stdlib often Can’t display abstract values Buggy Print debugging to the rescue? Nah… 43