Live data from Hacker News

Comparing OCaml and Standard ML

adam.chlipala.net

21–30 of 71 posts

Re: Comparing OCaml and Standard ML

#21
post #16

Earlier quoted context omitted.

I'm curious about what you find particularly confusing about OCaml. I'm a (relative) newcomer to the language, but I find it extremely readable, even if it's a bit clunky at times.

What does this code do? if a > 0 then print_endline "a > 0" ; match a with | 0 -> print_endline "impossible" | 2 -> match b with | 0 -> print_endline "2, 0" | _ -> print_endline "2, not 0" | 3 -> print_endline "3, something" | _ -> print_endline "something, something" ; print_endline "done"

As someone without too much OCaml experience, how do you disambiguate this?

Re: Comparing OCaml and Standard ML

#22
post #21
post #16

Earlier quoted context omitted.

What does this code do? if a > 0 then print_endline "a > 0" ; match a with | 0 -> print_endline "impossible" | 2 -> match b with | 0 -> print_endline "2, 0" | _ -> print_endline "2, not 0" | 3 -> print_endline "3, something" | _ -> print_endline "something, something" ; print_endline "done"

As someone without too much OCaml experience, how do you disambiguate this?

1) Ignore indentation (I intentionally wrote it to deceive, but similar examples could easily appear in real code). 2) `match` cases always bind to the innermost `match` statement. 3) Statement separators (`;`) are tricky.

The correct indentation of the above code is:

  if a > 0 then
    print_endline "a > 0" ;
  match a with
    | 0 -> print_endline "impossible"
    | 2 ->
      match b with
        | 0 -> print_endline "2, 0"
        | _ -> print_endline "2, not 0"
        | 3 -> print_endline "3, something"
        | _ -> print_endline "something, something"
            ;
            print_endline "done"
To have the same meaning as the indentation implies, you would need to add `(...)` or `begin ... end` at the appropriate places.

Re: Comparing OCaml and Standard ML

#23
post #22
post #21

Earlier quoted context omitted.

As someone without too much OCaml experience, how do you disambiguate this?

1) Ignore indentation (I intentionally wrote it to deceive, but similar examples could easily appear in real code). 2) `match` cases always bind to the innermost `match` statement. 3) Statement separators (`;`) are tricky. The correct indentation of the above code is: if a > 0 then print_endline "a > 0" ; match a with | 0 -> print_endline "impossible" | 2 -> match b with | 0 -> print_endline "2, 0" | _ -> print_endli…

I figured the indentation was a lie, but didn't realize you would have to introduce something like begin...end to make it work the way it appears.

Re: Comparing OCaml and Standard ML

#24
post #22
post #21

Earlier quoted context omitted.

As someone without too much OCaml experience, how do you disambiguate this?

1) Ignore indentation (I intentionally wrote it to deceive, but similar examples could easily appear in real code). 2) `match` cases always bind to the innermost `match` statement. 3) Statement separators (`;`) are tricky. The correct indentation of the above code is: if a > 0 then print_endline "a > 0" ; match a with | 0 -> print_endline "impossible" | 2 -> match b with | 0 -> print_endline "2, 0" | _ -> print_endli…

What would be the corresponding SML? I see how the way SML binds ';' would help here, but would it change anything about the last two badly indented matching? And it seems to provide you with the equivalent footgun if you attempt to do multiple side-effect operations in a match branch, which in my experience you want to do more often than you need to separate matches with ';'.

Re: Comparing OCaml and Standard ML

#25
post #3

Small update: recent versions of OCaml have immutable strings too. It also miss mention of package manager (OCaml has Opam, which is awesome, I don't know about SML). And ocamlbuild is missing from the build tools section.

My only reservation with Ocaml is that there does not exist a package management system for windows or even cygwin.

I cannot understand why OPAM takes so much to work on Cygwin. I like SML but it still does not have a good set of libraries.

I understand that these projects are sort of resources but supporting cygwin well should be easier than mingw or VS.

Re: Comparing OCaml and Standard ML

#26
post #21
post #16

Earlier quoted context omitted.

What does this code do? if a > 0 then print_endline "a > 0" ; match a with | 0 -> print_endline "impossible" | 2 -> match b with | 0 -> print_endline "2, 0" | _ -> print_endline "2, not 0" | 3 -> print_endline "3, something" | _ -> print_endline "something, something" ; print_endline "done"

As someone without too much OCaml experience, how do you disambiguate this?

There are two things to remember. The first is the match binding thing that tomp mentions in his reply.

The second one is that in OCaml, semicolon is a separator, and not a terminator. In contrast, in C/C++, semicolon is a terminator. If you have an expression, you end it with a ";" just because.

This is not the case for OCaml. In OCaml, semicolon is used to separate two sequential expressions where only one expression is expected. Thus,

    ; 
is evaluated in sequence, and can be used in a place where only one is expected. For example, if statements have the following syntax:

    if  then  else 
Now if you wanted to do two things (instead of one) in the "then" block, you would simply write

    if  then ; else 
Notice that under these rules

    if  then ; else  
makes no sense. Separators are not terminators. We are used to thinking of ";" as terminators because of C/C++.

Re: Comparing OCaml and Standard ML

#27

Earlier quoted context omitted.

I really liked SML and wanted to use it in anger after reading "ML for the working programmer" and "Purely functional data structures" (excellent functional programming books by the way) but just couldn't due to the lack of libraries as you say. For me the object system on Ocaml was just too much. Tis a pity.

> For me the object system on Ocaml was just too much What do you mean by that ? The current consensus about the object system in OCaml is "don't use it", even if there might be cases where it will describe your system better. The standard library (both the real stdlib and Jane Street's Core library) don't use objects so you can pretty much ignore this part of the language if you want to.

Since that is the 'current consensus' as you say, it seems like the ocaml community converged on the opinion I formed 10 years ago. But it wasn't necessarily the consensus then.

Re: Comparing OCaml and Standard ML

#28
post #15
post #8

ML's syntax is indeed less confusing than OCaml's. And that's the reason I'm working on a compiler front-end replacement for OCaml inspired by Clojure, Haskell, Ruby and Julia. It will be for OCaml what Elixir is for Erlang. I'm finishing the parser right now and will next implement the language primitives as a library in the language itself.

Do you share the development anywhere online? Are you using Menhir? I could maybe help you if you're having any troubles with the parser, I've been working on many interesting and hard (LA)LR parser issues in the past few months. What will the syntax be like, any existing language, maybe like Julia?

I don't have the code publicly available yet, but all the development will happen openly on GitHub. I estimate to prepare a minimal working compiler in the next weeks.

I've been thinking a lot about how to perform the parsing. I believe it's important to have a complete metaprogramming system (with code macros and quasiquotations) with all the basic language primitives defined in the language itself. So standard parsing with Menhir may not be ideal for this purpose. Instead, I've ported a simple top-down operator parsers (also known as Pratt parser) to OCaml, as described for example here[1]. This is the technique used by Douglas Crockford for the JSLint parser[2].

The core of the language can be seen as a simple compiler toolkit, that parses generic expressions and produces native OCaml AST. So for example even things like assignment (`=`) or function definitions are regular macros. This is one of the reasons I named the language "Meta".

There's another interessting language that inspired me a lot called Magpie[3] which uses identical approach (described in detail here [4]).

The main goal of the language is simplicity. OCaml is a very powerful language but programming in it requires some writing effort. I want the freedom and natural expressiveness of Clojure or Python combined with type security guarantees and modularity of OCaml.

I'm developing a large system for my startup in OCaml right now and plan to incrementally port it to Meta, which I think is important for dogfooding experiments, for support and commitment.

The syntax will look a lot like Julia (although I considered to just adopt s-expressions, languages like Elixir and Julia showed as that it's possible to be homoiconic (in some restricted sense) and still use regular syntax). I am still in research and only have defined basic language constructs like variable bindings, function definitions, type annotations, pattern matching and macros. If you are interested I can show you some examples.

What do you think about it? It would be nice to hear some early feedback.

[1]: http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-e...

[2]: http://javascript.crockford.com/tdop/tdop.html

[3]: http://magpie.stuffwithstuff.com/

[4]: http://journal.stuffwithstuff.com/2011/02/13/extending-synta...

Re: Comparing OCaml and Standard ML

#29
post #2

Purity is almost used in a pejorative sense throughout the page. If it wasn't for poor library support, I would prefer using SML to OCaml. SML is syntactically simpler and both SML/NJ and MLton have a lot to offer that I haven't yet found with OCaml. I think one of the biggest mistakes in the life cycle of SML was premature specification. As a result of the language definition, SML has stagnated for almost 20 years.…

I agree completely with your library statements.

> I think one of the biggest mistakes in the life cycle of SML was premature specification.

I disagree. There were some earlier Successor ML efforts ~10 years ago (see: http://successor-ml.org/index.php?title=Main_Page ), but as I understand it, there was agreement on many of the small issues but larger challenges reaching consensus on bigger changes. You can see some notes from Bob Harper at the ML Workshop in 2013 on how we're moving forward currently:

http://www.cs.cmu.edu/~rwh/talks/mlw13.pdf

Re: Comparing OCaml and Standard ML

#30
post #8

ML's syntax is indeed less confusing than OCaml's. And that's the reason I'm working on a compiler front-end replacement for OCaml inspired by Clojure, Haskell, Ruby and Julia. It will be for OCaml what Elixir is for Erlang. I'm finishing the parser right now and will next implement the language primitives as a library in the language itself.

I'm curious about what you find particularly confusing about OCaml. I'm a (relative) newcomer to the language, but I find it extremely readable, even if it's a bit clunky at times.

Good OCaml code is one of most readable in the category of static languages, I agree. But it's less "writable" than readable in my opinion. Of course you can get used to the syntax, but for me expressiveness and clarity of comprehension comes from both: simplicity of writing and reading.

Often I feel like there is a barrier between your thoughts and their execution which does not exist in Python or Ruby for example. When I switch from OCaml to Python it's like a breath of fresh air.

In conclusion I think common tasks must be syntactically abstractable. For example, I find working with standard data structures in OCaml annoying, as if I were programming in C. There are tons of syntax extensions for OCaml that try to fix these defects, but it only supports my claims.

Post reply on HN