Simon Peyton Jones on Functional Programming, Thinking in Types, Useless Languages
Comments
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
Why is the internet so insecure? Primarily, because all of our software infrastructure is written in unsafe languages. >> This is Simon Peyton Jones, co-creator of the functional programming language Haskell, and I asked him all about programming language design. >> If we rewrote all of our software infrastructure in Rust, things would be way, way better. I think that statically typed languages are huge boon for LLMs. I do large-scale
systematic refactorings fearlessly because the type system keeps me safe. In fact, >> Would you recommend people learn how to program today? >> I think people have a right to be worried in the sense that I think there's going to be considerable dislocation. >> Here's the full episode.
I wanted to start by asking you, what is functional programming in your words, and why does it matter to people who might use the other languages, the imperative languages? >> Functional programming is about programming with values instead of mutation. Um, so, uh, in a conventional
imperative programming, computation proceeds by, um, like if I want to add up the numbers between 1 and 100, um, I have a a mutable cell containing a running total, and then, as computation proceeds, I keep adding, you know, I I I I I keep adding to my running total. So, there's a cell that changes its value over time. Computation proceeds step by step. There's a program counter. Now, that's very unlike mathematics. If I say 3 + 4 * 7, you don't think of a mutable cell that changes its value over time. You think, well, 3 + 4 is 7, 7 * 7 is 49. That just The answer is 49. So, in a sense, it's more declarative than imperative. Um, another way to think of it is it's more like what a spreadsheet does. In a spreadsheet formula, if you put in a cell, you'd say, in A1, you put the formula equals A2 *
A3 + 7, well, then, that's just a formula that is evaluated and you expect to compute the values of A2 and A3 automatically before computing A1. It wouldn't make any sense to compute A1 from the old values of A2 and A3, right? So, and there's no notion of a program counter. There's no notion of do this and then do that. It wouldn't be sensible to say equals print X plus three. Because who knows when or even whether that cell will ever that formula will ever be evaluated. And then the surprise is that it is possible to do useful things in such a limited language. When I would learn programming, I learned it by writing machine code in which the fundamental mode of computation is registers and program counters, right?
And mutation of registers as you go along and memory. Like computation and mutation seem to be inextricably interwoven, right? That's how computation gets done. Um so, it was a surprise to me when I learned that well, um it is possible to do useful things with functional programs. I still remember that amazing revelation. Now, um uh In fact, one way to look at it is this. Right back in the late 1920s, early 1930s, Alan Turing was doing a PhD in Princeton under the supervision of Alonzo Church. Now, Alan Turing, as we all know, invented the Turing machine, which is fundamentally about mutation. Right? It has a little tape and you you can read things on the tape and then you write back new things onto the tape and it has an immutable program, right?
Now, Alonzo Church at the very same time was working on something he called the lambda calculus, in which there's no notion of mutation. This is it the lambda calculus is like the essence of functional programming. Programs execute just by reduction. Um now, so these were two different models of computation. You might ask, what can you compute with the Turing machine and what can can with a lambda term. And the astonishing thing that turned out, which is completely non-obvious, is that anything you compute with a lambda term, you can compute with a Turing machine, and anything you compute with a Turing machine, you can compute with lambda calculus.
So, that means that the two are identical from the point of view of their computational power. Big surprise to me. Okay, so they're equally powerful, and then then the question is, um you know, which which which might you want to choose? As I say, I started life from the my baseline of imperative programming, and of course that's the way you write programs, and functional programming is a kind of weird, anomalous, rather nerdy, academic thing. But, I became hooked on the idea that by excluding side effects by default, that is imperative programming has side effects by default, by excluding side effects by default, we could make programs that are easier to write, easier to maintain, easier to reason about.
And then the question is, um uh what are the practical obstacles? Is it slower? Is it less convenient? And so forth. So, in effect, I've devoted my research life to exploring that hypothesis. The wonderful thing about being a researcher is you can take something which at the time, this is back in 19 80 back in 1980, uh functional programming was very nerdy, geeky, academic, um but I was allowed to spend my professional life just saying, "What would happen if you took that one idea, programming with values, and just ran with it? Where would you get?" Um and I think we've got somebody somewhere pretty interesting. I don't want to say that it's better fundamentally, because after all, uh Haskell on that journey started with functional programming, then we learned how to mix functional imperative programming together in a way that we might talk about later, right? So, it's not really an either or, it's more of a both and in the end.
But, my uh position now is, as often
say, when the limestone of imperative
programming is worn away, the granite of functional programming will be revealed underneath. >> And why do you say that? >> Well, functional programming is the language of mathematics. Right, that's that's how we we fundamentally think about things when we want to be formal. Indeed, if we want to reason about and say what does this imperative program do, we have to wheel out a lot of mathematics and you know, whole triples and so forth. But that's all in the world of math, which is functional programming. Functional programming is just mathematics brought to life and made executable. So, you might wonder I suppose um you maybe saying, well, isn't imperative programming just enough?
And I think that there you have to have to um uh just look at the broad sweep of things. I think what's happened is that from the world of functional programming, lots of ideas have got adopted into the imperative arena. Like so they grew up over here in functional programming and then they have been adopted by the imperative programming things. So, you I think garbage collection, you might think lambdas, you might think language integrated query, you might think about lots about type systems, polymorphism and static typing, tons of that. So, at least it has been an intellectually interesting experiment that has been a very fertile laboratory which to grow up ideas that can infect the mainstream. Now, um my instincts now is that we should start from functional programming and do imperative programming where necessary.
I think probably the mainstream is we should start with imperative programming and sort of add bits of functional programming where possible. But and I'm not going to say that they're wrong to do that, just to say that I think there's a pretty interesting dialogue. Um now, you You to do have to think very differently about the act of programming, right? You do have to rewire your brain quite a bit. Um but just let me this is something we might want to come back to. I said easier to maintain. As programs get old, well just think of a 10-year-old program. Right? If you have um a piece of 10-year-old code written by somebody who's long since left your company that mutates some more or less global variables, then you might be a little bit a bit a little bit afraid about Oh, and there's other bits of code scattered about that also read or write those mutual those um global variables, then you might be a little cautious about making modifications, right? Lest Oh, if I put these two things if I do B and then A instead of A and then B, maybe, you know, the global variable that I read in read somewhere deep inside the code is now not have the value that it was expected to. So, reading and writing um shared variables that are visible, you know, outside the scope sort of somewhat global variables is a form of coupling between bits of code that is very invisible.
Right? Very invisible. So, functional programming forces that to become visible. It forces it into the open. And by the act of forcing it into the open, that strongly encourages a style of programming which you would don't have such interactions because they are painful to manage. Um so, um it encourages a uh
uh a style in which programs execute in modular box without interacting. Now, every imperative programmer would want to do that. But I want them to do that in a provably secure way. So, you can know that these programs don't interact rather than just say, "I've done my best." >> Then I guess on the on the flip side then, what are the downsides of functional programming language? >> Oh, well, it can be very tiresome and inconvenient, right? So, you're somewhere deep in the ball you know, a a a subroutine of a subroutine of a subroutine of a some soft software stack, and you want to reach out and I don't know, grab the time of day.
Oh dear. That's a side effect. But when in a pure functional language, the time of and it did there's a good reason because everybody else higher in the stack is assuming that if I call this function applied to, you know, seven, it will give me the same answer if I call it call it again applied to seven. But if it can interrogate the time of day, suddenly it might give a different answer. Right? So, this a simple a thing as look as asking for the time of day invalidates all set of assumptions and so is not by default allowed at all. And that can be a convenient because you all you say, "Look guys, all I want to do is get hold of the time of day. Don't give me grief."
And so every functional programming language including Haskell um provides you with a little trapdoor. Haskell's is called unsafePerformIO.
And it says, "Trust me. I'm going to do something that has side effects, but you can pretend there weren't any." All right. So, and it's called it is literally spelled u n s a f e perform i/o. It's the long name that has unsafe in the title so that you the programmer taking on the obligation to make sure that what you're doing is okay. Whereas in an imperative language, everything you do could be unsafePerformIO-ish. >> [laughter] >> I was watching one of your lectures and you did go over a little bit the history of functional programming languages and you mentioned somewhere that there was this idea of people building hardware for functional programming languages.
This just made me curious because I didn't I only know the Von Neumann architecture. Do you have any idea what that might look like and some high-level thoughts? >> Well, I did it. Um and now I think it's probably a bad idea. Um but back in 19 when was Backus's
Turing Award lecture? I I it was 1978 or there about. What was it called? Um >> Can programming be liberated from the von Neumann style? >> That's it. Now, he was saying two things. Firstly, he, the designer of Fortran, a quintessentially imperative language, the designer of Fortran was saying, "Guys, we should do functional programming." That was the That was what the talk was about. Um and um uh he introduced a language called FP for functional programming. It was a very particular, somewhat idiosyncratic functional programming language, but there was. So, that was rather amazing, right? This was 1977. Um he was saying, "Guys, give up on imperative programming. Just do functional programming. Here's how you do it."
But, he also said in the same lecture, um "We might imagine designing new hardware to execute this new kind of language." So, if you like, we've got Turing machines, they let give rise to register machines and microprocessors and so forth. Maybe lambda calculus, perhaps if we designed a machine from the ground up to do lambda calculus, it would look different. So, that was of course very exciting. And um uh similar kind of thinking led to an actual commercial machine called the Lisp machine. That was a piece of hardware that was designed by Symbolics to execute Lisp programs. Lisp is a mostly functional programming language. Um And so, at the time I was, you know, in my early 20s, we were thinking, "Oh, crumbs, what would a piece of hardware look like that could was primarily designed to execute functional programs?
Would it have a different kind of instruction set and architecture?" So, um the nearest we got to it actually was although it was a big strand of work then that was the um called the MIT data flow project. So, data flow languages, functional languages, they're definitely the same kind of category, right? And so, um the MIT had this great group led by Arvind called the MIT data flow group and they designed in the end a machine called monsoon. It was specifically designed to execute data flow programs, that is functional programs. It was based on uh you know a token store and matching and tokens flowing around and getting matched up and executing. You can imagine data flowing through a graph. When three arrives at a plus node and four arrives at the other input of the plus node, bam you can fire that plus node, right? And that hardware did all of that.
At a similar kind of time in England my um colleagues uh Thomas Clark, Joe Stoy and others um built something called the SKI machine, SKIM. There's a paper about this in the um uh FDC conference. And SKIM was designed to execute SK combinators. So at the time David Turner has written a lovely paper in which he described how to take lambda calculus programs and translate them into SK combinators. So you can read about that in my book. It's a very simple translation that you can give the whole translation in half a dozen lines. But um then you have this message of this message of S's and K's. And S and K reduction is very very easy.
Um so it's like the machine code of functional program. That's one way to think of it. >> What is a SK combinator just for context? >> There are three combinators, SK and I. So and they have simple reduction rules. Here they are. IX = X. KXY = X. SXYZ is X of Z applied
to Y of Z. End of story. That's all. So it's very simple, right? So now all I got to do is take to take my lambda translate it into a giant tree of SK combinators, right? S applied to K applied to I of um three and um S of Z of just a huge tree
of S's and K's. Right? Now simply apply the rewrite rules I gave you. And you have That's That is program execution. It's rather astonishing that that can execute arbitrarily complicated programs, but it can. Do you think that's rather amazing? Now, then I can take an arbitrary Haskell program, I can translate it into lambda terms, and I can translate those lambda terms into S and K, very simple transformation, and I can just write those Run those three rules, and it'll produce the output of the Haskell program. That's pretty amazing. And indeed, there is an implementation of Haskell that uses exactly this. It's called microHS, and my colleague Lennart Augustsson um has built it, and its execution mechanism is SK combinator reduction. And if you sit in microHS, you can ask it to show you the S's and K's that it produces. If we had a, you know, shared screen, I could probably show you. Um Uh So, long story short then, um uh the SKI machine was designed to take SK
trees, big trees of S's and K combinators, and just execute them directly. So, it was built in hardware, and it ran perfectly well, um reasonably fast. Now, why didn't this catch on? Why don't we have um functional programming machines? I mean, at the time, it was a big idea. We even had a conference called Functional Programming Computer Architecture, FPCA. It was right there in the title of the conference. But slowly, we became aware that what was really happening is we were doing at run time what we could do instead at compile time. And that is always a bad idea. Imagine an interpreter for, I don't know, um Pascal or something.
It'd be Or uh for Java. Like, we can compile Java to bytecode, and we could interpret the bytecode. Right? So, then we are We're doing something at run time. At run time, the interpreter is looking at the bytecode saying, dispatching to say which instruction, blah blah blah. right? Now, what does a JIT do? It takes a sequence of bytecode and says, "Oh, no, no. I'm going to take that sequence of bytecode and compile it to a sequence of machine instructions that, when executed, will do the same thing, right?" Much faster. Because it can take advantage of common of bytecode sequences, for example, to say, you know, if you swap and then swap, it's a no-op or something like that, right?
So, what In fact, it turned out is this whole SKI business, and any other And indeed dataflow machines also, was simply doing at runtime what you could do better at compile time. We were building an interpreter in hardware.
So, don't build the interpreter in hardware. Instead, build a compiler that translates your lambda term into a sequence of machine instructions that, when executed, will behave as if you had done this uh reduction business. And then you might think, well, machine instructions for what machine? Well, from a practical point of view, it was very hard to compete with Intel. Right? They were just spending Brazilians of man-years on making x86s go faster. And ARM likewise for ARMs. So, um we can leverage all of that work by compiling into their instruction set. But even if you say you are God, you are the chief executive in Intel, and can tell them to add new instructions or mechanisms to support functional programming, what would you add?
Not very much. Little bits to do with um uh garbage collection barriers, I think. And and such like, but nothing major.
So, in other words, I think the whole build hardware to execute functional programming directly turned out to be a mistake. Inspiring mistake, fun mistake, I had a great time, but a mistake. It's better to build a compiler. >> I would have thought there might be some unique opportunities for parallelism given the immutability in these functional programming languages. So, I Yeah, you described that that graph or that SK combinator graph. I imagine you
could, you know, execute that all in parallel assuming there's no connections across the graph. >> And indeed, that was the data flow machine. The MIT data flow machine was all based on that idea. It's saying just throw the whole graph into the token store and just run all the nodes that are runnable. Right? But that's incredibly fine grained. You've got to imagine this big tree of a million nodes and your processor is sort of wandering around over it doing little things in parallel. There's a lot of memory traffic going on there. And if this these these little, you know, threads have to, you know, are operating on the same bit of tree, then the synchronization costs, um so, very, very fine grained computer
parallel computation like this turns out to be impractical. Like or not impractical. You could do it, but it's very slow. Right? So, the data flow people, their history, if you look at the history of the MIT data flow project, they started with micro parallelism. Every individual
instruction was a separate parallel computation that might take place and the token store would match it up. Right? And then they built more and more compiler technology that grouped these things together into larger units. So, they expanded from micro threads of single instruction threads into 10 instruction threads or 100 instruction threads. Right? But even that never really caught on. So, you're right, though, that if I say in a Haskell program, E1 + E2, then I can do E1 and E2 in parallel. And GHC does support you in doing that, but you nowadays, rather than expecting the compiler to do that automatically for every sub-expression, you can spark a computation. You say E1 par E2, and that says do E1 and E2 in parallel. There's a project at Carnegie Mellon for a strict parallel strict language called ML that has a very good variant of parallel ML going in a similar way. So, essentially we've moved away from the the dream of automatically getting parallel at very, very fine grain to programmer we give programmer clues about where it's a good idea to do parallelism, but um uh and still the compiler will try not to fork to too tiny grains.
>> I was searching on YouTube and I've searched your name and this video came up and it said "Haskell is useless,
Simon Peyton Jones." >> [laughter] >> And it's it's a video from a long time ago. It's someone's got a low-quality and a handheld camera. There's group of researchers that are all talking. Butler Lampson is across the table and it's it's a fun little video. And in the video, you lay out this two-dimensional
graph where on on one dimension there is the useful versus useless axis
and then on the other dimension there is the safe versus dangerous. And then you're placing programming languages on this two-dimensional graph and I think you started by putting C as it's very
useful, but it's incredibly dangerous. And then you put Haskell on the the polar opposite side. It's You said it's useless, but it's very safe. What are the least safe and most useful languages and why do you place them there like C for instance? >> Well, yeah. So, um let's see. In C, you program by mutation. So, it's unsafe in the sense that any function can mutate any variable at any time. It has a lot, you you pass pointers around a lot and functions mutate the memory pointed to by those pointers. And moreover, typically they can mutate it anywhere. There's no array bounds checks or anything. So, it's kind of like super unsafe in the fact that this is demonstrated by the fact Can you imagine that that like all of these exploits that we get every day, right? Um that uh you know, MSRC is discovering in huge numbers, but but we've had you know, why is the internet so insecure?
Primarily because all of our software infrastructure is written in unsafe languages. If we I mean if we'd written all of our you know, internet software and operating systems in Haskell or maybe in OCaml or ML, 99% of all these exploits would be removed by construction. Like it's like we've built a boat out of
paper clips and we're surprised that it's leaky. I mean, you shouldn't build boats out of paper clips, right? Because they have holes in them. You should build it out of a secure substance, right? But then it's too late. So, we spend incredible amounts of human ingenuity and effort patching the holes in our boat built of paper clips. It's tragic. It's tragic
how much effort and ingenuity and money has been lost and waste of resources just because we wrote our um you know, computational infrastructure for the world in an insecure language. That's what I mean by unsafe. >> I mean, are there not vulnerabilities? Like if I wrote something in Haskell, I mean, there's got to be some new type of >> there are vulnerabilities. I just said 99%. I didn't say 100. If I write a Haskell program that says, "Receive message. If the message says, 'Tell me everything,' then spit out my
entire database in reply." No language can stop you doing that, right? But, if you look at the program and it doesn't have any such things, right? >> [laughter] >> So, I mean So, you know, nothing can prevent you against high-level attacks to insecure programs. Or, I mean, another example might be deadlock, right? Two services, no matter how securely written, if A waits for B and B waits for A, deadlock. Sorry. No and no language is going to stop you doing that. Um you might hope for some high-level verification tools. But it's like Surely, if you're trying to do something hard, like prove that that doesn't happen, you want to have a foundation that is, you know, in which you've got some bedrock to stand on. If you're standing on sand and trying to prove some advanced property, it's very, very difficult. But, good point. I'm not talking about 100% security. Absolutely not. But, uh how many how many of how many uh exploits are based on buffer overruns?
Or, you know, pointer manipulation that's gone wrong. If you couldn't have a buffer overrun, you couldn't do pointer manipulations gone wrong. Those Those exploits just wouldn't exist. >> So, I mean, C is maybe half a century old now. Um what about more modern versions of those lower-level languages like Rust? >> Much, much better. Much, much, much better, right? If we rewrote all of our software infrastructure in Rust, things would be way, way better. I I'm not actually even sure whether Rust has array bounds checks built in, but suppose it but it must have the ability >> to >> uh promise that you're not uh and actually not bound. I don't quite know quite know how, but if you compile all your code with that switched on, you're a way better situation. Way better.
So, yes, this is not just functional programming, but you did ask about why I thought C was an insecure, unsafe language. >> [laughter] >> If we imagine that graph, there's that the upper right quadrant, which is useful and safe. And you know, you described that as Nirvana in the in in the video. And I guess maybe over time, I mean C's you know, somewhat really unsafe, really useful. >> Yes, so C I mean Rust has moved along the axis from useful but very unsafe to stay useful and become safer. Now, Haskell started life as being very safe but useless. So, it's worth just you know, rehearsing that because in the first version of Haskell, there was no IO.
The only that a Haskell program was a function of type string to string. It's functional programming after all. It could do no IO. All it could do was take a string and produce a string. So, obviously that's not very useful. It's a little bit more useful. A language that is very very safe and completely useless is no-op that does nothing ever. Very safe but very useless. Haskell was a bit better, right? At least it lets you um apply a function to a string and get back another string. So, of course we then worked on how could we do IO in Haskell in a safe way.
That will lead to, you know, monads and stuff. But But so, just as uh uh the you know, we're moving from C horizontally to go safer and safer um but uh but staying useful. So, from Haskell we're moving sort of vertically to get more and more useful without stopping being safe. And Nirvana is when we sort of meet up, right? So, but the point is not to say one is better than the other, but just to say that we both seek things that are useful and safe. >> And when I think of functional languages um in I guess in the mainstream or you know, what people are kind of thinking about, I hear Haskell. I also hear OCaml. Uh how do these two programming languages compare if someone was trying to pick a functional language to work with?
>> So, OCaml is um a strict language that is called by value. So, when you say F applied to 3 + 4, it'll add 3 + 4 and then call F to get 7, right? In Haskell, if you say F of 3 + 4, it'll build a little suspension that says, "Well, if you ever need this 3 + 4, you can evaluate it, but maybe you'll never need it." And then it calls F. That's a lazy language. Right? Now, uh
so because OCaml is strict, it has a defined order of evaluation. So if you say F of 3 + 4 and a second parameter 8 + 9, it'll evaluate 3 + 4 and then 8 + 9 in that order. Right? So it makes sense to say you can say F of print hello, {comma} print goodbye. Right? Because you'll get hello and then goodbye printed. So once you have a defined order of evaluation, then it's pretty easy to incorporate IO. Right? Even though it's {quotes} a functional language, OCaml allows you to do IO without, you know, without saying unsafe before my IO or anything. In fact, it has a defined order of evaluation and it's perfectly kosher to do that.
As a sort of unintended consequence of being called by value, it was also by default impure. Now, good Haskell good OCaml programmers won't use many side effects, but OCaml doesn't prevent you. Haskell prevents you. Why does it prevent you? Because if we'd allowed you to say F of print hello, {comma} print goodbye, those would be thunks, right? Not yet evaluated. Right? So if F happened to evaluate its first argument and then its second, we'd print hello and then goodbye. If it evaluated its second and then its first, we'd print goodbye and then hello. If it didn't evaluate either, we wouldn't print either of them. That doesn't sound very good from a uh you know, if you want to control what IO is happening. Right? Laziness forced Haskell to stay pure. Strictness allowed OCaml, which grew out of the ML tradition by the way, ML is another functional language that predated Haskell.
It was always strict and it always had IO by default. Um it wasn't a goal, but that's just the way it worked out. At the time, nobody thought about it. It was just obvious. So, that's a big difference between OCaml and Haskell, right? Is that um Now then, since then they've both grown up. Haskell's gained sort of monads and I uh OCaml has gained lots of uh lots of fancy type systems. Some of them uh Haskell and OCaml have learned from each other. Um OCaml has gained an interesting effect system recently and a whole lot of new extensions. It's an absolute hotbed of innovation at the moment OCaml. So, I view OCaml and and Haskell as kind of um siblings.
Um brothers and sisters, right? We love each other, we learn from each other, um we compete with each other, all of the things that siblings do. But, they're not the same, right? So, siblings don't say, "I'm I'm just better than you. You shouldn't exist." We say, "Let's Let's enjoy our life together." And that's what it That's the way it is. >> You described the laziness and the strict and as a programmer I my initial
thought is I would like to control the order of execution. And when I say print X, I and then print Y, I I want it
to happen that order, otherwise it would be a little unintuitive. What is the main benefit of laziness? >> Why is laziness good? Well, um uh John Hughes did did this rather well way ago, 1980 something. He wrote a program called Why Functional Programming Matters. Um and one of its main thesis was that lazy evaluation lets you um uh compose programs in a particularly modular way. So, imagine a program that is I mean, his classic example is a program that plays chess, right? So, one thing you could do is you could imagine building a tree of all possible moves starting from the position we're at. It would be a very, very big tree.
You know, OCaml, it would be too big. Because first of all, I compute the tree, and then I'd start deciding what move to do. I couldn't possibly do that. But if the tree of all possible moves, you know, has more nodes in it than the number protons in the universe. So, let's not do that. Now, if instead you could build the tree Oh, having got this tree, supposing you had it, you could then walk over the tree saying, "Ah, let me do some mini-maxing and see Oh, this is looks like a good Let me go this way." I could explore the tree, right? Um Now then, so if you you know, OCaml, then you would have to put the tree generation and the tree exploration in one function.
Right? In Haskell, with lazy evaluation, you can generate an infinite tree, and then the explorer that prunes the tree and explores just the bits of it that is necessary is completely modularly separated. Right? I can build a different generator and a different pruner. They're just completely separate programs. So, lazy evaluation is very powerful glue that lets you glue together um
uh two programs that you'd like to be distinct. Strict evaluation forces you to merge them together. >> Yeah, this reminds me I mean um so, in Python, for instance, they have the idea of a generator where >> Yes. >> you can lazily retrieve things. >> Every strict language, every, you know, serious strict language has lazy evaluation in it. They're called iterators or generators. And so does OCaml. All right? But in Haskell, that's the default. Now, instead in Haskell, so this is a bit like Again, the two are converging on the middle, right? Haskell, lazy by default, but you can make things strict. You can put in exclamation marks to say, "Please evaluate this before the call."
All right? Or the IO monad know how say evaluate, but that's a a more brutal strict strict annotation. So, in Haskell you can make things stricter. In OCaml you can make things lazier.
All right? So, it really boils down to is what's the default. We both want to have a mixture of the two. And then it becomes a bit cultural as to which you prefer. Um I I don't know. Uh some people sometimes ask me, they say, "Well, if you were designing Haskell again, would you make it strict by default with really good support for laziness?" And I often say, "Well, I might." Uh yeah, that seemed attractive because frequently I found myself cursing laziness as an implementer. But, I strongly suspect that 10 years after that I'd be thinking, "Man, if only it was lazy by default."
Oh, when I say strict by default I would or definitely mean strict but pure. Like, no side effects. >> A few times in our conversation you've mentioned the word monad. And it seems like it allows us to do the side effects and um what is a monad and yeah, how does it help us do side effects and preserve ordering? >> One way to think about it that's very um easy to understand and use is just to imagine that the do notation is somehow built into Haskell. So, you can say do print X semicolon print Y.
And that has type IO unit. So, a value of type IO unit means I do some do some input output and return a value of type unit. An expression of type IO int is an expression that when you run it will do some IO and return an int. Okay? So, do let's you combine um
IO performing computations together. So, uh print three has type IO unit. It does some IO, namely printing three, um get char has type IO char. It does some IO, namely reading a character from standard input, and returns a character. All right? Notice that's different from just char. So, you know, {quotes} x {quote} that has type char. It's just a character pure, right? Get char, the IO performing operation, has type IO char. It does some input output, and returns a character. Okay. The do notation lets you combine together, um IO performing computations. So, if you could say do, and then you say x {left arrow} get char, semicolon, put char x, then that x {left arrow} get char, that says run the get char computation, and get me the character, call it x.
The put char x says run the put char computation to put x, and we combine them together with the do notation, and it combines two uh two computations to make one IO performing computation. Right? So, but these things are completely first class. That's what's new about monads compared to just make it into into C. x {left arrow} get char semicolon put char x. That's a computation whose type is IO unit. Right? Let me give it a name, so I can say let foo with type IO unit equals that do. Now, I can pass foo as an argument to something. I could put foo in a data structure. I could return foo as a result.
It's a value just as much as three
or plus.
In particular, for example, I could say, "Do foo semicolon foo."
That takes foo, uses it twice. Each time
do foo semicolon foo says, "Do foo." and then do foo again. Right? So, I'll read a character and put it and then read a character and put it again. So, these values of type IO T for some
type T are first-class values.
Right? In C, I can't take X colon equals three semicolon, you know, Y plus four and pass that as an argument to something and expect it to happen wherever it's used, right? It's not a first-class value. >> When I first was learning this this monad idea, it seems like you've you've said in a few places it's a way to do the side effects, but keeps the language pure. But, when I saw it, my first
thought was it's almost like this little
you know, place where we segregate the dirty things we want to do, I guess. But, then how does that keep the language pure? >> Oh. Because because I mean yeah, we are lying here, but look, if your function has type int to int, can it do any IO any IO? >> No. >> No. If it has type int to IO int, it could do arbitrary IO. So, yes. So, it's nice pure int to int function or dirty int to int function. So, you might say, perhaps you're about to say, that's a bit of a blunt instrument. Either completely pure or completely dirty, right? It gets you a awfully long way.
But, nevertheless, it would be cool if you could um say, "Oh, I am a int to um
effectful doing reading of files only int."
Right? So, you'd like to in the type you'd like to say what kind of effects can it have. Can it throw exceptions? Can it, you know, spawn new threads?
So, you'd like to enumerate the effects this computation could have, right? That would be cool. That's called an effect system. And there's, you know, Brazilian programming language papers about effect systems. And it turns out that you can indeed in the type system of Haskell and indeed OCaml is is rapidly becoming same. Uh you can express just not just all or nothing, does it do IO game over but rather which particular effects does it do? Including no effects at all, that's pure. Right? A good place to start is a library called Bluefin, which my um colleague Tom Ellis has designed. It's a very nice take on how to do effect systems in Haskell.
>> I guess the purity comes from that this
is this this dirty stuff is signaled through the type system. So >> Correct. >> Okay. >> Correct. So, we can do it, but here it is and be aware. Yep. And because you have to thread, you know, because the type system gets in your face, you know, you you were trying to um uh like map, say. No. Map says I apply a function to every element of the list. Well, um uh so, it has type A to B to list of A to list of B. If you were to apply an IO-performing function, so it the function you're applying has type like int to IO of char
you could map that over a list, but you just get a list of IO of chars. That hasn't done any IO yet, right? You want something that says, "Take a list of IO chars and perform those actions one at a time." So, I want a function that goes type from type list of IO char to IO of list of char. Right? And you might want to perform all those actions top to bottom, or maybe bottom to top. Who knows? That's what this function of type, you know, so yes, so you could do it in various ways. So, sometimes it gets in the way, right? You say, "Oh, you know, can't I just use map?"
>> [laughter] >> Well, in Haskell, no, sorry. You you're going to have to do a little bit more work to tell me in what sequence you want your effects to happen. Because by default, Haskell does not specify sequence. Um so, the, you know, adding, uh, you know, monads to control effects does get in your face a bit. And that's a that's the tax we pay. In effect, that's part of the big experiment that Haskell is doing is to say, "Suppose we up front say we can we're willing to pay that tax." You know, how many followers can we get?
Uh, and the more but I'm I will note that monads have infected quite a lot of other languages, like F Sharp is definitely a call by value impure language, and yet F Sharp had these workflows that were definitely monads,





