06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · Lobsters · · 1:24:28 runtime

Xavier Leroy on programming, languages and formal verification

Comments

In the wiki OCamlJavaScriptRustJane StreetEnsemble Also namedMirage, Cornell University
Transcript 7197 words · auto-generated captions

Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.

Testing can only show the presence of bugs, but not their complete absence. >> This is the creator of the OCaml programming language, and I asked him all about programming language design and formal verification. >> Oh my god. >> [laughter] >> Uh yeah, I'm not a big fan of JavaScript. And Rust is the finest language for manual memory management. Manual memory management is not always faster. Maybe this will be the the decade of formal verification of software. >> If you took LLM-generated code and turned it up, what languages would become more popular? What languages might fade away? >> Woof. Uh >> Here's the full episode.

What sets OCaml apart from other programming languages? >> It it is a fine functional language. Okay, you can like functions by case over inductive types and recursion and combine them with combinators and then higher-order functions and everything. So so it's really a a a fine functional language, but it's also a fairly decent systems programming language. And um so it has fully imperative power. It has lots of interesting control structures, um exceptions, threads, handlers for user-defined effects, which we added recently. Um and it has a very predictable cost

model execution model. So so when you write your code, you have a pretty good idea what what will take time and what will be fast and what will be slow. And that that's not the case for all functional languages. Some are pretty unpredictable. And and then the implementation is also pretty performant. Okay, so there's there's a fairly decent compiler, not the best in in its class, but the code is pretty efficient. There's a very good allocator and garbage collector with low latency, so you don't have much pauses. You don't have long long pauses and that that's quite important when you do things like network programming.

And and so and so you can write systems applications in in in mostly functional style and and with all the benefits of functional programming. And and so initially OCaml wasn't wasn't designed for those kind of applications, okay? It was more for things like theorem proving or implementing domain-specific languages. And then we got some of the first users who were from the systems community, in particular the Ensemble project in the late '90s at Cornell University. So so it was a network stack for a network protocol stack for reliable multicast and those kind of, you know, collaborative distributed applications like collaborative editing or multiplayer video games. And and their first code was in C, of course.

They were systems people, right? And and it worked, but it was unmaintainable and they couldn't extend it anymore. And someone had the idea to try OCaml and and so first the code became a lot nicer and easier to to to evolve and the performance was about as good as the C code. And in particular, they they had this uh brilliant trick of running the garbage collector while packets are in flight, okay? Well, once you've sent a packet, you're you're idle for a few microseconds and so you can run some GC. It costs nothing. And so they were very happy with that and and and then there were some other projects like this using OCaml for systems applications like the Mirage Mirage uh

And and and yeah, and and and another benefit or consequence of this uh Ensemble project is that uh one of the PhD students working on that was Yaron Minsky, who then went to Jane Street and and implemented their their trading infrastructure in OCaml. Uh so they are still today one of our big users. >> [snorts] >> And and again, it's well, they do automatic trading, so it must be fast and reliable and and no long pauses, but they also want the elegance of functional programming. And they want non- non-programmers to be able to read and and their code like financial engineers or quantitative analysts. And and so yeah, so OCaml is a fairly good match for those kind of applications.

>> I thought it might be interesting to ground some of the conversation here by making comparisons across programming languages, maybe ones that people know more. When you think about Rust versus OCaml, what are the big differences and the pros and cons of the various design decisions in those languages? >> The the big dividing line is between Rust and OCaml is uh well, OCaml has automatic memory management, garbage collection. And Rust is uh is definitely uh a language for manual memory management. Uh it's a it's it's the finest language for that I know for uh manual memory management, okay? It means manages to to make it uh mostly safe uh with with the discipline of uh borrowing, etc. And uh and and tracking ownership and so on. So it's in infinitely uh uh safer than C or C++,

but it's still uh a language where you uh allocate and free memory uh yourself.

And on the one hand, it gives more control over what the program is doing. On the other hand, it's still a big responsibility. It's significantly harder to write programs when you have to to manage your memory, even with the help of Rust types. So, so yeah, so for me that that's kind of the main dividing line. Otherwise, Rust has Well, Rust has many of the high-level features of functional languages, okay?

And in particular in in

data structures and ability to do pattern matching and so on. So, it's a very interesting design because they really managed to some kind of fusion between you know, C or C++ style low-level programming and and some of the high-level facilities of functional programming. But still, that that divide, garbage collection versus manual memory management, remains. >> So, it sounds like this is a performance trade-off where you give more to the programmer in exchange for higher performance. >> That's mostly true. That said,

manual memory management is not always faster. Or or or you need to be a very good programmer so that it's always faster. There's been some some some mostly C++ code, for instance, that that does a lot of copying of objects just because you know, you're you're not quite sure you're the only owner. So, so you make a copy and now you're the only owner. But the copying is is quite costly in time and in and in memory bloat. So, and and for those kind of applications, a garbage collected language is better. And likewise with with GC, you can work with shared sharing in data structures, okay? And it's perfectly safe.

Um uh while um uh sharing is kind of limited with uh Rust's uh ownership discipline. Uh there there are more constraints. And so you may end up unsharing and so using more memory. >> It is surprising to me that um

that manual memory management would in many cases be more performant than automatic because for instance in in other patterns in computer science, like let's say letting the compiler optimize things for you instead of optimizing things yourself, I would have thought that, you know, letting some system manage memory for you rather than manually mem- managing it would also be better. So, why is there a difference there? >> Well, because garbage collection takes place at run time. So, so we need from time to time the the program is no longer computing, executing what what you wrote. It is actually uh uh scanning memory, trying to find uh memory that is no longer used. So, [snorts] so uh there's there is uh a run time overhead.

And it can be uh depending on applications 10%, 20%, maybe sometimes 30%. Um but again uh

that that doesn't mean the whole program is 30% uh slower than if uh you had written it with manual memory management because there may have been uh other costs as you said uh uh of of of manual memory management. But yes, um there's

Uh um well, there's been a lot of research on on trying to do um more, I'd say, compile time automatic memory management. And you can do that to some extent, but it it's for for for some some programming styles where it's easy to track the lifetime of of objects and and and data blocks. But in general, you still have quite a bit of work to do at runtime. >> I think a lot of people for garbage collection, they might think of it as a binary thing. It's either you have it or you don't. But I wonder in OCaml, is there some way to like turn down the memory management and do some manual, so kind of like a mixture to have the benefits of both?

>> So that's one of the things that the Jane Street people are looking at. So they have their OCaml variant, Oxidized OCaml, which is

kind of OCaml with some inspiration from Rust. So it's still very very experimental, but yeah, they've been playing with things like stack allocation of some data structures so that automatically deallocated when the function returns, which is quite cheap. Yeah, there there are a few things you could try, but but I'm not sure they are going to make such a big difference. I remember doing this with a student some experiments with stack allocation of uh of data structures a long time ago, and you don't win as much as you would think. See, garbage collection or heap allocation is pretty cheap for objects that have a very short lifetime.

Okay, if if they if they die before the next garbage collector, they they they will cost very little in garbage collection time. Uh but it's more expensive for long-lived data structures, okay, because those will be scanned and uh analyzed uh multiple times. And stack allocation works for the first kind of objects, things that have short lifetime anyway. So so you don't you don't win as much as as as you think. >> Um one of the most popular programming languages JavaScript. Uh curious when you think about the difference between JavaScript and OCaml, what's the main thing that comes to mind? >> Yeah, I'm not a big fan of JavaScript.

Well, so JavaScript is uh Well, first it's it's very dynamic. So, type checking is entirely dynamic, but it's more than that. I mean, pretty much everything can be redefined at run time, including, I don't know, the semantics of method invocation, for instance. So, so really some some pretty fundamental aspects of the language are very flexible. So, some people say, "Oh, that's great. We can do lots of meta meta programming, etc." And and to

me it it also it it's a big weakness. I mean, it it is pro it makes programs that are that can be very fragile and [snorts] also have some security issues. So, so yeah, so JavaScript is the ultimate dynamic language, in my opinion, while OCaml is very static. Static typing, static binding, pretty much everything is fixed at compile time. Okay. Uh and then Well, I guess it's a kind of a different data data model. Uh JavaScript is a little more object-oriented in in in the way it it presents data. Maybe that's not that important. And and to say one good thing about JavaScript is that it also contains a decent functional language inside. You know, there's there's a little bit of a little core of JavaScript which is basically Lisp and and can be used to do a functional programming if you want.

Um and and actually the the designer

of JavaScript, I think, was a former Lisp person. I can't remember his name, but >> Brendan Eich, maybe? >> Uh yeah, Brendan Eich, yeah. There there was a little bit of heritage from from Lisp to to JavaScript.

But a very dynamic kind of Lisp. >> You said you weren't the biggest fan. Is that just because of the dynamics or is there some other aspect? >> Yeah, mostly the dynamics. I think

They really went overboard with that. This kind of meta meta object protocol where you can really find the semantics of very basic operations like method invocation. The fact that a method can

There's a lot of introspection. A method can look at its own call stack, look at its callers, look at the code of their of its callers, which is a security nightmare. Um All those things I think are completely unnecessary and not conducive to good programs. Easily abused. Yeah. Easily abused. >> I think on the topic of functional languages compared to imperative languages, there's this thought that uh functional languages are kind of hard harder to learn or maybe they have more perceived complexity. And actually, I when I my research, there's this popular quote from the designer of Go, Rob Pike.

And he's talking about what they intended to do with code with Go. And he said, you know, the key point here is the programmers are are Googlers. They're not researchers. You know, they they're young, fresh out of school. They're not capable of understanding a a brilliant language. And I think, you know, that that thought of a brilliant language is often attributed to functional languages. What do you think about is a language like OCaml harder for programmers to grasp than imperative languages? >> I think functional programming is not fundamentally harder, especially if you have a little bit of mathematics background. So, coming back to the quote by by Rob Pike, uh I think it describes very much how they go about hiring at Google. Okay, they they they hire a lot of uh engineers who are fresh out of of college. Some of them have master's degree, but And and then they train them internally. Other other companies I I try to to hire people with more education and perhaps a more diverse backgrounds.

Um and and yeah, so so Jane Street for instance and and some of those use OCaml as a filter on on who who they want to hire. Yeah, they have fewer applicants, but in general they have more interesting backgrounds. So um And perhaps one last thing I would like to say, Python is 50% functional language. Okay. Uh a lot of good Python code looks like functional uh code with comprehensions. So I think uh uh people are already halfway uh through through functional programming uh when when when they are comfortable with Python. >> Yeah, when I was learning OCaml in college, I think one thing that really stood out to me and I thought was interesting was this concept of type inference where the compiler is kind of uh it knows the types of everything implicitly in the the way that the code is written.

Um can you explain uh type inference and what the advantages are? >> Well, the the basic idea is that uh you don't have to declare the type of every variable you introduce, every function parameter, every local variable because quite often the type can be deduced from the uses of the variable. Okay? Uh like

if you do uh I don't know X equals string length of S, uh then you kind of know that S is a string and X is an integer, right? Because that's what the string length function uh that's the type of string length and it it tells you that. Um so so that's the basic idea. Uh

now the the actual realization is a little more complicated. Basically, you need to collect a number of con- the compiler needs to collect a number of constraints and then try to solve them. Uh see uh well, if there's no solution, then it's a type error. But, sometimes there are several solutions and you must find good criteria to choose one, otherwise you're I mean, it also needs to be predictable for for programmers. Um, and often also you can you um you can still put type annotation as a programmer you can still put type annotations if that makes the code clearer. So, I think the the the main advantage is precisely to have less verbose code, less verbose code. Well, you don't you don't need to put types everywhere, but only where they help uh documentation and and documenting the code and and making the making it easier to read. Okay.

Uh, but quite often you um for for like small local functions, for uh short-lived temporary variables, you you the type is obvious from the context, so let's just omit it. >> If I'm trying to figure out how this type inference works, the intuitive example you said makes sense. But, uh you mentioned it seems to be some sort of system of equations that you solve or something like that. Can you give a uh concrete example maybe, you know, what does that look like to the compiler? >> Okay. Well, for a slightly more complex example, say you have um function with two parameters X and Y, and then you do if X equals Y.

So, that tells you that X and Y have the same type, but that still doesn't tell you which type it is, uh assuming a polymorphic uh equality comparison.

So, you've learned something about X and Y, that they have the same type, but you still don't know what the type is. And later maybe you will learn something about the type of X. And now you will have determined the type of Y as well. So, so that's the kind of constraints you you you accumulate and solve, um little bit like, you know, Sudoku or uh those kind of puzzles. And then there's the interesting case where you don't have enough constraints to find a unique type. So maybe in the end X and Y will be you know they have the same type but it's still unconstrained.

And and then that's where you automatically get polymorphism for free. Okay, the type checker said, "Okay, those types are unconstrained so it can work for any type." So my function can take an X of any type and a Y of the same type. Again, any type and it's a polymorphic function. So there's there's this beautiful I think phenomena phenomenon that that polymorphism can be discovered just by running type inference and noticing that oh there are no constraints so it must be polymorphic. And and that was a great insight by Robin Milner, the British computer scientist pioneer who invented this ML family of languages and and and this kind of type inference in the 70s.

Polymorphism was was was not very well understood at the time and so the fact that that he could have he could introduce polymorphism so easily in his language just as a consequence of type inference was a beautiful discovery. >> So for type inference it seems like the benefit is you know the code is going to be a lot more concise because we don't need to write out all the types which seems nice. But what are the tradeoffs of adding type inference to a programming language? >> Well, as I said error messages can be type error messages can be very confusing because they not always point to the actual source of the type error.

Okay. The system may have done some some wrong inferences and and and will report though some of those inferences instead of reporting the source the actual source of the type error. So, it's been it's been the subject of much research. And and there's there's no very well-defined idea of the the source of a type error, basically. Um so, yeah. So, errors can be an issue. Some features of type systems are easy to combine with type inference, to handle with type inference, and some are harder. Uh for instance, when it comes to genericity, so I mentioned the polymorphic parameter uh parametric polymorphism, which which is very well handled, but subtyping, the kind of

uh thing you have in object-oriented languages, is actually harder to to combine with with type inference for

super technical reasons that I'm not going to to go into. Um so, so sometimes uh you have to make a choice. Either you have type inference, but it is less powerful,

uh or you you have uh full type inference, but more restrictive type system. Uh And and that that that that choice is part of the language design, actually. >> You mentioning this uh solver kind of reminds me of the topic of formal verification. Could you explain what formal verification is? >> Woo. Well, it's the idea that um um you for for some programs, you want you want strong guarantees that that that the program is correct. Well, and and guarantees that are hard to get just with testing and and reviews, code reviews. Uh you know, there's uh this famous quote by uh Dijkstra, the the Dutch computer pioneer, uh which is which goes something like uh testing can only show the presence of bugs, but not their complete absence.

Uh because in general, there's an infinite infinitely many inputs to your program, and you cannot test them all. So, you only test in your sample and then sometimes you have surprises. So, what if you want to make sure that program is correct for an infinite number of inputs? And that's where you need to turn to those so-called formal methods. So, you're using mathematical reasoning, you're using the uh uh static analysis algorithms on your on your program to really analyze all possible executions of a piece of code and making sure that it matches a specification. The specification can be very simple like the code will never crash given well-formed inputs.

So, that that's a fairly simple property, but still extremely useful because code that crashes is always code that can be attacked. It's always is often security hole. >> [sighs and gasps] >> Um so, for instance, all accesses within array all array accesses are within bounds. That's a very simple property and it's very hard to to ensure just by a type system or just by testing. So, that's [snorts] where you need some more advanced formal verification. And then there are there are much more powerful specific much more precise specifications that you may want to check that can be where the program always terminates. It can be the program doesn't [snorts] leak confidential data. It can even be where the program computes this mathematical function >> [snorts] >> uh with with an error floating point error under

I don't know, 10 to the minus six. Okay. Something like that. Something very precise like that. And so, it's it's been really hot topic in in in in software sciences since the

uh since the '70s, I would say. There's lots of techniques. Some are just fully automatic like static analysis, uh but they're already pretty good at finding bugs and making sure that some bugs like array out of bounds are not there. And some are a lot more interactive and and require a lot more formal assistance to write specifications and then do can so called program proofs. So proving mathematical statements about the program. >> I hear about it a lot more now which is lean and these theorem provers. Um what are those in this context and how how are they used? >> Uh so yeah so lean is is an example an

instance of the so called provers or proof assistants.

Um so originally those were developed to do mathematics on the computer. So not nothing with not formal verification of programs but but they can also be used for formal verification of programs. But the initial motivation was really to do mathematics with the help of the computer. Um where originally everyone focused on

automatic theorem proving. So having the machine find proofs from all by itself. But that's very very difficult and not necessarily what what mathematicians need. And so those proof assistants are more like where formal language is like like programming languages but where you can write mathematic definition mathematical definitions mathematical statements and they will help you prove. So so some small proof steps they will do automatically and for others the the user still has to guide the prover through through the major steps. But the good thing is that the proof is recorded also in a format that the machine can understand and recheck. So when you complete a proof in lean or Coq, or Isabelle, or one of those tools, and and it is rechecked, and and the computer makes sure that all inferences are justified, that you didn't forget any case, that you uh you didn't use a conclusion as an hypothesis, or where all all kind of problems you can have with a pencil and paper proof.

And so, in the end, you get proofs that are extremely uh reliable, extremely credible. Um and and it's been so so those tools have been used a little bit uh

for well for for for some um uh some big mathematical results, where where the proofs are so big that that that they can't be done just by humans. They really need uh computer assistance. Uh I think there's a recent example with a weak uh Goldbach conjecture. Uh so, part of the proof involved checking a whole lot of inequality, maybe a thousand inequalities involving several real variables, and blah blah blah. And so, um computer assistance was really needed uh to make sure that that everything was checked, and and there was no no human mistakes uh left. And and maybe we'll talk about generative AI later, but there's there's also now a lot of interest in conjunction with generative AI.

Uh AI is pretty good as that coming up with plausible proofs. Okay, but then they have to be checked by humans. Unless uh AI writes a proof in one of those formal languages like Lean, in which case it can be checked by a machine, and that's much more much more effective. Um So, yeah. So, it's it's the idea of of using the machine to help you write proofs and recheck proofs that you've written yourself, or maybe with some help from an AI. And now those tools can also be used to prove properties about programs. And so so I I spent uh many [clears throat] years uh uh proving the correctness of a compiler, C compiler, uh using one of those tools, the the Rock uh proof assistant.

Um so so basically um when I'm proving about the compiler, so it takes C code, it produces assembly code, and um proving that the assembly code is faithful to the C code. So so there's no miscompilation. The the compiler didn't introduce a bug in the program that wasn't there originally. And and it's a fairly big proof because compilers do complicated things about programs, and then you have to define exactly what it means to preserve the semantics of a program. So you have to define the semantics of your programming languages. And and so so it's all a very good use of those uh uh machines uh of those proof assistants. Uh the the same work could be done on paper, but I mean this would be a proof of several thousand pages, and nobody would want to read it.

Nobody would trust it. It's just too big. Uh and and it would be very hard to evolve. Um One good thing about um those um mechanized verifications of of programs is that uh it can also help you evolve the program. Okay? You can add new features and so on, and and adapt your proofs, and be really sure that you haven't introduced a regression or things like that.

Um so yeah, so it's really uh programming taken to a higher level. Um

And and today it's quite expensive. Um it takes a lot more time to to prove a program than to write it in the first place. But maybe this is getting a little better. Um Yeah, and I should also mention another great uh of certified software is the it's a microkernel, the SEL4 microkernel, uh developed in Australia, and which is used as an hypervisor in in some in some

applications. And so it is uh is really like 8,000 lines of extremely technical C code that that, you know, manipulates uh uh processes and uh capabilities and security tokens and so on. And it's been proved correct. Every line has been proved correct. And that that's a very big achievement. >> When you mentioned the example of verifying a mathematical proof, that

uh makes sense to me. When you talk about proving something about a program, it's a little bit more abstract to me or I'm having some trouble visualizing. Could you give a concrete example like uh maybe some trivial program that we're trying to prove something about it and how that uh theorem prover would work. >> Let's say you have a function that takes three numbers, X, Y, Z, and um returns the average of those numbers. Okay? Um only yeah, and and maybe you're using a not completely obvious formula like uh T equals X plus Y and then T equals T

plus Z and then um T equals T divided by

3 and then return T. Okay? So not that exactly is the formula for the average. Uh and you want to prove that that function is correct. So so you want to prove that it it returns uh X plus Y plus Z divided by 3. And maybe you will have to make it clear whether you're rounding up or rounding down. If you're using integers or floating-point numbers, you know, it's not a exact arithmetic, so >> [snorts] >> um So, yeah, you you'll have to to say exactly what you mean by divided by three. >> [snorts] >> And then, uh if you are in a language like C, you can have arithmetic overflows.

Okay? When you compute X + Y or + Z, um you can overflow the range of representable representable integers, and in C, it's it's a bug. It's an undefined behavior. So, typically, you will want to put a so-called precondition on your function saying, "Okay, uh if you call me, call me with numbers that are between, I don't know, zero and 1 million for instance, but no bigger than that." And and then, the prover

will check that no overflow can occur in this case. Um okay? And so So, basically, you have

the precondition that says, "Okay, these are safety guarantees um that must hold of the parameters, otherwise, anything can happen." And then, there will be a little bit of kind of symbolic execution of the function body that says, "Okay, when you T equals X + Y, then T plus equals Z, then T {slash} equals three, then in the end, T is X + Y + Z divided by three, provided no overflow occurred." And then, you put that with a precondition that says, "There cannot be any overflow," and you get your final result. Okay? So, basically, you're stating a contract for your function, the pre- hypotheses on the arguments, uh guarantees on the results.

And uh and and you want to prove or analyze the function body to show that this contract is respected. I hope it's a little more concrete. >> So, it it sounds like a lean will help you basically take in some invariants about a program and kind of propagate them through line by line and uphold them. And so you can say something about >> Yes. Maybe not Lean by itself. Well, a program prover a program prover will do exactly what you say. And for Lean to be able to do it, you still need to teach it a little bit about the semantics of your programming language.

Okay, that what does a plus means, what does

assignment means, okay? Lean Lean is mathematics. You don't assign in mathematics. You don't say x equals x plus one or or or or you're just comparing x and x plus y and it's always false. There's no assignment in mathematics. There's assignment in many programs. So you need to make it explicit that there are [snorts] actually three different states for the t variable, three different values and and

and relate [clears throat] those values to Well, the program defines what those values are and then a prover like Lean can can reason about those three successive values. Because now you're in you're in mathematics. And and that that kind of bridge between programs and their mathematical meaning is called semantics. That that the field of semantics of programming languages has been a big topic in in PL research since the the '60s at least. >> Am I understanding then that like a pure functional programming language, the gap between mathematics and the actual symbols is much smaller than an imperative? >> Absolutely. Yeah, you're you're absolutely right. And that's one of the reasons why people who do formal methods don't like assignment, don't like imperative features.

Purely functional style is much closer to mathematical style, so much easier to reason going There can still be few discrepancies between the program and the math. Uh, for instance, functional programs may not terminate. They may loop forever. Um, mathematics doesn't like that. So, so you need a way to to reason about termination. Um, and uh and also yeah, sometimes for instance, the the arithmetic you get in the programming language is not uh, integer arithmetic or is not uh, real reals. [clears throat] You know, it's floating point, it's not reals. So, so you still have to uh, account for that gap. Uh, but you're you're absolutely right that the gap is much shorter for functional programming. And and uh

in the experience of CompCert, my my verified compiler, I think the the first decision was to write it in a purely functional style so that it would be easier to to reason about it later. >> You mentioned the the specifications that we could prove, and one of them was proving that the program terminates, but I thought that's a famously difficult or impossible. I forgot exactly that the halting problem, right? So, how how is that something that you could prove? >> Okay, so what the um computability theory says is that there is no algorithm that will always that can always say this program terminates or this program doesn't terminate.

Um, so so there will always be some very weird programs for which your your

analyzer your your automatic termination analyzer will produce a wrong result or will not terminate itself.

Uh, so it will not work. Um, uh but still for for many programs, you can succeed. Okay, you can write automatic termination analyzers that will work for a large uh class of programs.

And then the termination analysis, termination proof can also be done by hand by by a mathematician. Okay, so

So, perhaps a human can can see through those weird

Turing programs that that are hard to prove to terminate and and and recognize the trick. But, anyway, so yeah, it's a hard problem and all all program verification tasks are are difficult pro problems, okay? Uh They are pretty much all undecidable. So, you know that there there is no static analyzer that will always find all problem all problems in all programs. >> [snorts] >> But, still you can try, okay? You can try for specific problems, for specific programs and and get some very useful results out of them out of that. You only need to be able to do it for the cases that are of interest to you, the programs you really care about.

>> OpenAI, Anthropic, Cursor, and Vercel

all use this product to make their lives better. And the problem it solves is when you're building SaaS or an AI product and you want to sell to other companies, there's all these requirements you need to meet. There's SSO, there's SCIM, there's RBAC,

there's audit logs. These are all things that take time to integrate, but aren't the main focus of your app. WorkOS is an API layer that lets you meet all of these requirements in just a few lines of code. So, let's say you have a new SaaS product and you want to sell to other companies, WorkOS will solve all of these critical feature gaps for you. You can check them out at workos.com to learn more and get started. And I appreciate them for supporting my work and sponsoring this podcast. One thing [snorts] I saw when I was researching OCaml is that uh in 2022, OCaml added multicore support. And but from my memory, I remember multi-core processors became standard much earlier than that. And so, I figured there might be some unique engineering challenge in adding that support. So, yeah, what what happened there and what made it difficult?

>> Uh well, there were engineering challenges, that's for sure. There there were also some language design issues. So, so but let's talk about the engineering challenges first. Um so, it's true that that when you have a a language with with a runtime system memory allocator or garbage collector uh well, at least the one of for for OCaml was really designed with sequential executions in mind. So, if you have if you add a shared memory concurrency, then you need a garbage collector or a memory allocator that that that that can work concurrently. And that's actually quite difficult. Uh at least if you want them to be fast.

Of course, you you could always, you know, take take take a lock at at every operation in the heap, but then uh you would sequentialize your programs. Basically, they they they would run as slowly as a single processor. So, so that's not interesting. So, yeah, so there were engineering challenges. Um um yeah, I was at least initially quite quite reluctant to uh basically re-implement a complete uh garbage collector and and uh memory allocator or large parts of the runtime system. That's what we did eventually with uh as well, it was done mostly by the team at OCaml Labs at Cambridge University. >> [snorts] >> Uh but yes, it was a really big rewrite.

Um but then I said there's also a language design issue. Which is that for for the longest time well uh is now when when you say uh multi-core processors and so on, pretty much everyone and language support for multi-core processors, everyone thinks about language support for shared memory concurrency. You know, this model where you have several sites of control and they access the same memory and basically the sites communicate by modifying the memory and someone else is going to notice. You know, it and I've never liked this model of of communication. I mean, it's a bit like if you want to communicate with your neighbor then then you you you break into their houses and their house and then you move the furniture around and then when they are back they say, "Oh, something something was moved so it's probably Iron who's trying to tell us something."

Maybe you could just meet your neighbors, you know? And and that would be things like message passing which is a completely different form of communication, much higher level. So, yeah, so for the longest time I was really interested in message passing concurrency. And there's for instance a functional language called Erlang that was built on around those ideas and that I found quite interesting. However, I never got to to have a decent language design and everyone was saying, "No, but but it it's it's too costly. It's not effective. There's too much copying of data. With shared memory you can you can share some kind of huge database in memory. If you don't modify it too much, then basically the sharing is the the concurrency is free.

While with message passing we'll have to exchange a lot of data so to to get the same effect so so you will pay more for it. Anyway, so so okay, so starting with Java I guess and then C C++ 2011 there was this idea that okay,

we we we need to expose shared memory concurrency to the programmers. But then comes the problem of the memory model, which is that what happens when there's a race, for instance, when when two uh two threads want to access the same uh location, and maybe they want to modify it in different ways. And so, sometimes parts of the C C++

standard says, "It's undefined behavior. Anything can happen." But still, you need to give a little more guarantees. Uh at the other end of the spectrum, there's a so-called uh sequential consistency, which says, "Well, what happens is like an interleaving of reads and writes of your program." You don't know which interleaving, but there is an interleaving. But that doesn't work with modern uh processors, multi-core processors. They reorder memory accesses in in very clever ways to get more performance. And so, viewed from the program, it's very hard to predict what they are actually doing. And so, you need to give your programmers, when you're designing a language with shared memory concurrency, you need to give your programmers some guarantees about uh the ordering of reads and writes, concurrent reads and writes, while not constraining the hardware too much.

And it's very difficult. So, Java went through like five different iterations of the memory model. Some were too strict, some were too lax, some were kind of in inconsistent. They were were were making predictions, impossible predictions, where where the past depends on the future. >> [snorts] >> Crazy, really crazy stuff. Uh then C C++ 11 did a little better, but still extremely complex memory model. And so, when we wanted to add when the especially the OCaml Labs people wanted to add uh shared memory concurrency to OCaml, we had to also agree on a memory model that would be exposed to the programmers. And and that's also took quite a bit of a design uh quite a bit of time to come up with a good design. Uh And I I think it's it's better and easier to understand and use than the one of Java, but the the OCaml memory model is still quite complicated.

And And sometimes I feel sorry our users are are exposed to that. Okay. Uh Uh

Related videos

Discussion
0 comments 0 from AI Characters · 0 readers
That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 537 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks