Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Brave New OCaml World

Marek Kubica
February 26, 2015

Brave New OCaml World

It's 2015 and time to revisit OCaml. Turns out, it is pretty good now. But getting started is tough so let me lend you a hand.

Marek Kubica

February 26, 2015
Tweet

More Decks by Marek Kubica

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. from a python perspective Stronger correctnes assurances due to type

    checking Faster execution Pattern matching Functionally focussed community 4
  4. from a haskell perspective Language with much gentler learning curve

    Module system: OCaml offers modules that can be parametrized with other modules →Functors 5
  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
  6. opam repository contributors 04.2012 07.2012 11.2012 02.2013 05.2013 09.2013 12.2013

    03.2014 06.2014 10.2014 01.2015 0 50 100 150 200 250 300 Time Contributors 8
  7. 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
  8. 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
  9. 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
  10. 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
  11. communities Learning in isolation is difficult #ocaml on freenode ocaml_beginners

    mailing list caml-list ocaml tag on Stack Overflow 14
  12. editor selection Emacs ocaml-mode Tuareg Vim OCaml syntax Operator replacement

    Eclipse OCaml Development Tools Merlin Helper that understands OCaml code. Offers sensible completion, type inference, renaming, definition location. Vim Emacs Sublime Text 16
  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
  14. 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
  15. 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
  16. 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
  17. too much config! So you need to write Configuration script

    (think ./configure) META _tags If we just had a declarative tool to generate this cruft… 23
  18. 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
  19. _oasis _oasis files are easy to write OASISFormat: 0.4 Name:

    pkg Version: 0.12.0 Synopsis: Example library for LambdaDays Authors: Marek Kubica License: LGPL-3 with OCaml linking exception Plugins: META (0.4), DevFiles (0.4) BuildTools: ocamlbuild BuildDepends: cohttp.lwt, yojson Library pkg Path: src BuildDepends: lwt.ppx Modules: Pkg ByteOpt: -safe-string NativeOpt: -safe-string CompiledObject: best 25
  20. different kinds of testing Assertion-based testing (xUnit) Property-based testing (QuickCheck)

    Enumeration-based testing (SmallCheck) Continuous integration (Travis CI) 27
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. opam stats: packages 11.2012 02.2013 05.2013 09.2013 12.2013 03.2014 06.2014

    10.2014 01.2015 0 500 1,000 1,500 2,000 2,500 3,000 3,500 Time Packages Total Unique 34
  27. 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
  28. ocamloscope ”Signature goes in, function comes out — can’t explain

    that.” API search for OCaml, like Hoogle/Hayoo. Searches top 100 OPAM packages. 37
  29. 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
  30. 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
  31. 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
  32. 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
  33. profiling ocamlprof and gprof exist Work on byte/native code respectively

    Not right granularity Call count display incredibly inconvenient Buggy 44