lainchan / λ

OCaml Thread

St. IGNUcius · 2022-10-12 17:02
I've been an OCaml developer for a while, and I couldn't imagine using anything else. The strong type system, first-class modules, Generalized Algebraic Data Types, and performance keep me using OCaml for just about everything. Unfortunately not many businesses use OCaml, most likely because FP in general is still fairly niche, but also because OCaml itself is often overlooked, in favor of Haskell. Haskell is a good language, I will concede that, but its far from pragmatic. That is where OCaml fits in. It brings high performance functional programming to everyone. Currently OCaml is version 4.14, but version 5.0 is in the works and promises to drastically change the language's backend with even more performance, and asynchronous programming using effect handlers. You should really consider learning this language, and using it for your next projects! I would love to discuss OCaml with the skeptics, or if there are any OCaml lainons it would be a pleasure to hear what you're working on!
Zero Cool · 2022-10-12 17:05
What about metaprogramming facilities?
DEBUG-CONTENTS · 2022-10-12 17:08
>>33036 ppx provides pretty much everything metaprogramming you'd ever need. It's widely used and very well documented. http://ocamlverse.net/content/metaprogramming.html
Hiro Protagonist · 2022-10-12 18:27
>>33035 I made a database in OCaml :)
Drano · 2022-10-12 21:09
>>33035 Between Haskell and OCaml, which one has a more active community? I used to be a SML programmer, but sadly, the language and its community is that and I'm not into doing another language that the community is so dead you can hardly find any good library.
Void · 2022-10-12 22:44
>>33040 Haskell has more users, and a larger community. But OCaml's community is still quite large, and extremely active. OCaml has libraries for everything you'll need, and if you know SML you'll feel right at home!
Rick Deckard · 2022-10-13 00:00
>>33041 What are OCaml's strongest points? What is it really good for? Since I know some other languages, I want to use it exclusively for that which OCaml seems to be the best option.
Lady 3Jane.. · 2022-10-13 11:54
>>33042 Systems programming and high performance computing. But there is also a case to make for web development. Personally I use it for everything, frontend, backend, systems, ai, etc. But I think it's strongest in systems.
BloodCat · 2022-10-13 16:07
>>33042 Pretty much anything. It's fast enough for most stuff (used for high-performance trading, as high level as Python and as fast as Java/Go). You can go very far in mathematical abstractions, and you can also do systems programming, at the same time. You can build everything with bash/Makefiles but you also have more advanced package management/build tools. There are libraries for most things. You can compile to JS if you want to do frontend stuff. On a more philosophical level, OCaml and its community value independence very heavily. Most modern programming communities are becoming/have become social media overrun by people that don't want to contribute code but just to be popular, influence how people work, create drama. OCaml is the opposite, different groups of people with different views and different ways to do things, that know how to work together when it would be beneficial to do so, and that know when to leave each other alone because we all have our preferences/reasons. It reminds me of when I was in college, and I could not go to a class for two weeks in a row, and the next week show up (having caught up of course), follow the course, ask questions to the professor and that wouldn't be a problem. No arbitrary rules about being present just to be present, just people going on about their lives, ready to help each other when/if needed. With all that put together, OCaml is a breath of fresh air compared to everything else. To answer your question, the original purpose of ML languages is to write compilers. For that, it is one of the best options, with other MLs (and self-hosting, but you can't pick your language when you self-host). But I'd say it's good for most stuff. I would even argue that it's better suited than most languages for most stuff (ignoring libraries availability, just talking about the language). The usual proof of that is that other language that are at the same level (GC'd high-level languages) are trying to become like OCaml. Java and C# are adding/have added type inference, pattern matching, records, sum types. Python, JS and Ruby are adding types, trying to be faster. Go added generics. C++ added optional, and with C they're trying to improve compilation times (OCaml, thanks to its Pascal heritage, compiles really fast). As for OCaml's flaw, the main one (for most people) is that the people behind the language take the time to design features well, and there is not as much manpower as in other languages. Just today the first beta for OCaml 5.0 was released. It will include multicore support. Work on multicore was started in 2014. On the other hand, having played a bit with it, I'm glad they took the time to do things well. If you're more of a move fast break things person, this might not be for you.
Cat Mother · 2022-10-13 17:55
>>33048 ML was mostly made for expressing math I thought?
Liet Kynes · 2022-10-13 19:05
>>33051 I think you're right: >We now proceed to ML as it existed in the interactive LCF theorem proving system, where it served as the metalanguage (i.e., proof scripting language) and command language. We refer to this original version of the language as LCF/ML From the linked PDF, "The History of Standard ML".
t · 2022-10-13 19:41
>>33052 I think being so mathematically inclined, also lends itself to Parser, Lexer, Compiler stuff though. So it makes sense that it's so powerful for language research.
stack · 2022-10-14 14:41
>>33035 OP here. I'm currently writing a Docker wrapper for OCaml, basically to allow you to programmatically manipulate docker containers.
Sage · 2022-10-15 01:30
>>33053 >>33048 I'm not really sure that OCaml and the ML family in general are fit for compilers. For simple interpreters they are wonderful, along with say modeling an abstract machine at one layer. But every ML (haskell, ocaml, and SML) I've seen has been very hacky, but the limited nature of their sum types and inability to share code between layers and making uniform abstractions..
Sage · 2022-10-15 01:35
33057 I should have included https://lawrencecpaulson.github.io/2022/10/05/Standard_ML.html Gives an interesting perspective of LCF from a PolyML perspective. It is a shame that SML died the way it did, it had a few properly interactive compilers that would be better for development than what we have now with OCaml, Haskell and it's derivatives
Bob Arctor · 2022-10-15 03:06
>>33058 What parts of SML and it's compilers make it better than OCaml or Haskell?
stack · 2022-10-15 12:20
>>33058 I'm still sad that SML is dead. It was really the only programming language I really loved. Neither OCaml and any other functional language got close to how much fun I had with SML.
Drano · 2022-10-15 12:30
>>33060 I once wrote a small-ish compiler for a subset of SML in Haskell. It was a true joy.
Davis · 2022-10-15 15:11
>>33035 Why are GADT's important? I hear a lot of talk about them, but not much practical use.
par22 · 2022-10-15 16:43
>>33057 >I'm not really sure that OCaml and the ML family in general are fit for compilers Were you the one who gave the reasoning in the "Compilers" thread? https://lainchan.org/%CE%BB/res/32311.html#32402 >>33062 One practical example of GADT is in the implementation of a type-safe URL router. https://github.com/anuragsoni/routes
par22 · 2022-10-15 16:49
>>33051 There's a book for learning that using OCaml: Handbook of Practical Logic and Automated Reasoning by John Harrison.
Hopper · 2022-10-16 03:35
>>33059 It was a standard, so you had many interesting extensions of SML and some interesting implementations. Variants like poly ML, and poplog's SML seem like quite nice developer experiences. It ultimately comes down to the philosophy of interactive computing, that you don't find in Haskell or OCaml. I believe if those compilers caught on more, we could see ML culture accept more dynamic behavior and attempt to type whole systems, rather than just have very rigid models of certain layers. >>33063 Yes, however my arguments are more numerous than the one I posted there. In that thread I was simply explaining the limitations of ML style sum types. Many attempts were had at working within the current ML's systems to solve it but most lead to hacks and more error prone compiler choices. I think there are cultural issues that need to be shed to try to move forward, see my response to 33059 for 1 more example. I think we could get much more flexible, safe, and adaptive systems from the ML crowed if the culture issues were addressed and better compilers and environments were had.
over · 2022-10-22 03:11
>>33035 I like OCaml, but the ORMs/DB stuff kind of is like kicking dead whales down the beach. Caqti in particular is not very good. If your model has more than 4 fields you need to do some gross tuple spamming.
par22 · 2022-10-25 02:30
>>33109 What kind of database are you using? Why not use postgresql-ocaml, sqlite3-ocaml, etc. directly?
John Doe · 2022-10-25 05:13
For ocaml programmers ITT: > which stdlib do you use? > which ppx's? > if you do web stuff, reason or js_of_ocaml I've always had a soft spot for OCaml, but writing module-generic code requires so much ceremony that it makes me go back to Haskell. Any advice?
Tomohisa "Tom" Kanezaki · 2022-10-25 14:28
>>33142 I switch between the standard library and Jane Street. I only really use JSON ppx and the equality witness PPX by Eduardo to allow simple generic monads. Js of OCaml is a lot better than Reason in my experience. Can you dive into what issues you have with module-generic code?
Bob Arctor · 2022-10-26 07:02
>>33146 This could totally be my own ignorance hindering me, and I understand that OCaml's module system is likely more expressive than Haskell's typeclasses, but something about having to write List.map , Array.map , MyCoolSequenceType.map , etc. seems needlessly verbose. I think the solution to this is modular implicits, which seem a long ways away from being a thing.
list · 2022-10-26 14:48
>>33154 You can always let open List in if you want to locally open the module and avoid using the identifier.
John Titor · 2022-10-26 14:59
>>33154 >>33155 You could also use module typing to infer it, for example: module type Mappable = sig val map : 'a t -> ('a -> 'b) -> 'b t end let identity_map (module M : Mappable) lst = M.map lst (fun t -> t) Unfortunatley I am not sure how we can generalize this/infer the module type of lst, but I think you can probably do it. https://v2.ocaml.org/manual/firstclassmodules.html Good resource for learning more about the module system.
car · 2022-11-03 22:15
>>33035 New OCaml book just released. The second edition to the legendary Real World OCaml by Yaron, Anil, & Jason. If anyone is interested in getting into OCaml, this book will turn you into a great programmer (if it's anything like the first edition).
Linda Lee · 2022-11-03 22:28
>>33264 >legendary rwo theres literally nothing in there distinguishable from any other language's main book
list · 2022-11-03 22:31
>>33265 Well it's legendary to the OCaml community. Not only is it the only OCaml book worth really digging into (outside the Unix programming book), but it's written by Yaron Minsky. THE OCaml dude. Without him the language would be a lot more primordial.
over · 2022-11-03 22:33
>>33266 this is why im saging ur soykaf "real world" just means "how do use json ooga booga" the async library they introduce in that book is terrible
Case · 2022-11-04 00:38
>>33267 Ok, and? Teaching people to use OCaml to deal with real world problems is not a bad thing. Not sure what you're trying to convey with your argument.
Nobody important · 2022-11-04 19:49
>>33035 Opium is such a great framework. Purely functional, fast, easily extensible, I've been writing my startup in it for awhile. Excited to see how it turns out :D
par22 · 2022-11-04 19:57
>>33273 What do you use for URL routing? Do you use a library for URL routing with Opium? Do your web applications connect to a relational database? If so, which database are you using, and how do you connect to it using OCaml?
Jens Hansen · 2022-11-04 23:24
>>33274 MySQL with the ocaml-mysql library, I route just with the standard routing App.get "/my_route" my_handler The one thing I like to do is compose my own type of middleware: module type Filter = sig val filter : (Opium.Request.t -> Opium.Response.t Lwt.t) -> Opium.Request.t -> Opium.Response.t Lwt.t end So I can chain them together via: App.get "/" @@ middleware_one @@ middleware_two @@ handler Overall it's really good, going to be open sourcing my startup in a month or two, so folks can see how I'm handling things.
par22 · 2022-11-05 12:30
>>33275 I want to like Opium, but some of the bug reports make it look like a toy project. For example: "file_upload broken: example adds newline before EOF on upload" https://github.com/rgrinberg/opium/issues/258 Not fixed after more than 20 months ... File uploads are such a basic feature of web frameworks, but Opium couldn't get it right.
Unnamed · 2022-11-05 12:52
>>33278 Yeah it is fairly barebones. If you're looking for a deeper framework, Sihl or Dream are both very good.
The Oracle · 2022-11-05 12:57
>>33278 Rudi also has a weird thing where he doesn't close issues. That issue in particular is already fixed. I've made a few PRs to his work and the issues they address are still open lol.
par22 · 2022-11-05 19:23
>>33281 >That issue in particular is already fixed. Since which commit? There has been 10 commits since bug #258 was filed. None of them appear to fix the problem. Are you sure that the problem has been fixed?
John Doe · 2022-11-05 22:06
>>33284 My bad. It's fixed downstream in Sihl not in base Opium. Sorry, I was using the wrong pin on my system. But it's fixed on Sihl (along with many other things in Opium), so if you want to use Opium with a little more tooling Sihl is a great option.
Nobody important · 2022-11-11 13:50
>>33035 What are GADT's and how can I use them in OCaml?
William Holtzer · 2022-11-18 11:30
>>33278 That bug is from the bad library being used for multipart. You can pull in https://github.com/dinosaure/multipart_form instead if you want to use multiparts.
Dozer · 2022-11-25 18:48
>>33141 ppx_rapper actually fixed all of the issues I had with Caqti, it's literally unusable for me without it.
overriding · 2022-12-17 17:56
Since Ocaml dropped Algebraic Effects in 5.0.0 trunk I wanted to learn about it, since I never learned how to use MOnadic IO when Haskell got popular, so I am not missing the fad train today. I like the capabilities aspect, I think having syntactic handles representing them is nice, and should make static analysis of of code purity possible, if not easy. Hello World let main env = let stdout = Eio.Stdenv.stdout env in Eio.Flow.copy_string "Hello World!\n" stdout ;; Eio_main.run (fun env -> main env) TCP based networking ping pong test #require "eio" #require "eio_main" open Eio.Std let rec run_client ~net ~addr = Switch.run @@ fun sw -> let msg = "Hello from client" in let flow = Eio.Net.connect ~sw net addr in traceln "SENT: %S" msg; Eio.Flow.copy_string msg flow let rec run_server socket = Switch.run @@ fun sw -> Eio.Net.accept_fork ~sw socket (fun flow addr -> let from_client = Eio.Buf_read.of_flow flow ~max_size:1024 in traceln "RECV: %S" (Eio.Buf_read.line from_client)) ~on_error:(traceln "Error handling connection: %a" Fmt.exn) let () = Eio_main.run @@ fun env -> Switch.run @@ fun sw -> let net = Eio.Stdenv.net env in let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8080) in let server = Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:5 addr in let run = Seq.forever (fun a -> Fiber.both (fun () -> run_server server) (fun () -> run_client ~net ~addr)) in run (); ()
Naomi Nascimento · 2022-12-18 17:11
>>33758 Yeah it's really nice. Sucks most of the codebases I work in are all v4.12.0. I'll have to think of a good side project to build with these new features.
Dujour · 2023-04-26 06:04
>>33035 How fast is fast is OCaml truly? I've seen some benchmarks where it lags behind especially in AIO.
Emmanuel · 2023-04-28 06:08
oh yes and the idea that a language tutorial makes someone into "a good programmer" is the most fugkbrain garbage i'd heard all month.
The Fin · 2023-05-01 06:48
>>35204 Really good argument. You stick with your toy Standard ML, I'll keep building systems that you can't understand. What makes OCaml a 3000 hour language? Do you not understand basic category theory?
Turing · 2023-05-01 18:43
>>35204 > He's angry at camels
Incognito · 2023-05-10 01:30
compared to haskell it lacks syntactic sugar