6 ms·
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,
by a0 12y ago
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.
- mercurial 12y agoI'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.
- tomp 12y agoWhat 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"
- p4bl0 12y agoAny decent programming editor will be able to indent that properly and you will see the problem. Also, I agree that it is a bit ugly but it's not that complicated to understand how the match syntax works. Adding a "begin" and an "end" in this code is simple enough :).
- tomp 12y agoIt's not just the match syntax; it's also the `;` expression separator. > Any decent programming editor will be able to indent that properly and you will see the problem. This seems like a weak excuse. In particular, I could turn it around and say, "any decent programming language should be writable without an editor". Also, the issue isn't just reading, it's writing too - it's much harder to foresee/plan all the `begin`/`end`, while you're writing a line of code, that will make the lines that follow work as intended.
- p4bl0 12y ago> This seems like a weak excuse. (…) Fair enough, but for the ';', as others explained meanwhile, I think it's pretty simple to understand, it's just that we are not used to it because of C syntax. EDIT: actually, you are right about ';', it is confusing when I think about it: it does not have the same behavior in a branch of a `match` and in the branch of an `if`… I wonder how I never had problem with that before.
- pjmlp 12y agoIt depends on the learning path of each one. In the Pascal family of languages, ';' is a separator as well.
- deleted 12y ago[deleted]
- tel 12y agoAs someone without too much OCaml experience, how do you disambiguate this?
- tomp 12y ago1) 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.
- tel 12y agoI 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.
- mercurial 12y agoWhat 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 ';'.
- deleted 12y ago[deleted]
- tomp 12y agoNot in my version of OCaml (OCamlWinPlus v1.9RC4, OCaml version 4.01.0): # if false then print_endline "false" ; match () with () -> print_endline "match";; match - : unit = ()
- jdh30 12y agoOCaml will give you compiler warnings on the incomplete and redundant match cases. F# will silently execute a semantically different version of your code. I prefer OCaml. Indentation sensitivity was one of F#'s design flaws.
- a0 12y agoGood 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.
- adultSwim 12y agoLearning Python was certainly easier for me. I could really hit the ground running. OCaml had a much steeper learning curve. It took me a while to get used to the compiler and the different style of programming. However, I'm a professional. Some of the powerful tools I use take a serious investment to learn. Here it paid off well. Now I find that Python programs are easy to start but OCaml programs are easy to finish. The compiler is an invaluable helper that I miss when using other languages. Some syntax is clunkier than others but to me having good types (and a good compiler) makes or breaks a language. I can get my work done in anything but this is an area where the language really helps me out.
- tomp 12y agoDo 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?
- a0 12y agoI 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-expression-parsing-made-easy/ http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-e... [2]: http://javascript.crockford.com/tdop/tdop.html http://javascript.crockford.com/tdop/tdop.html [3]: http://magpie.stuffwithstuff.com/ http://magpie.stuffwithstuff.com/ [4]: http://journal.stuffwithstuff.com/2011/02/13/extending-syntax-from-within-a-language/ http://journal.stuffwithstuff.com/2011/02/13/extending-synta...