r/ProgrammingLanguages • u/Erythrina_ • Jun 17 '26
Question about side effects in functional programming
One of the things I noticed using REPLs of functional languages is that you can write a ton of pure functional code, and then as soon as you hit enter to evaluate it, printing the result back to you is a side effect.
There are advantages to having code that is guaranteed to be side effect free, but I've been playing around with the idea of having a language with an imperative shell (with procedures, mutable vars, database and network operations, etc.) that can call into a language core that's guaranteed to be pure functional for certain kinds of operations. It can make for a simpler approach to side effects than a whole pure functional language but provide guarantees that other kinds of impure languages can't.
My question for people who are interested in functional programming: is this a useful distinction? Would that make for a language you might be interested in?
34
u/initial-algebra Jun 17 '26
That is basically already how Haskell works. The "imperative shell" is
IO. I guess the main difference would be that you don't support e.g.unsafePerformIO, which could be a valid design choice.6
u/deaddyfreddy Jun 17 '26
it's (usually) the IDE that generates it, not the code itself
6
u/Inconstant_Moo 🧿 Pipefish Jun 18 '26
I did that, it's called Pipefish. Hi!
People have said that Haskell's monads are kind of what you're talking about, but the crucial difference between monads and the functional-core/imperative-shell pattern is that I understand the second one.
The way Pipefish works is that you have pure functions which can return values but can't affect state, and then commands which can affect state but which can only return
OKto show that they've succeeded or an error to say why they failed. Commands can call functions; functions can't call commands.Functional core/imperative shell.
Pipefish and the lambda calculus.
This is indeed a very pleasant way to program, which is why it's already the world's most popular language paradigm. (Yes, really. Think of Excel formulas and SQL queries. Everyone loves functional programming because it's easy --- except people who've seen Haskell and think that it's difficult.)
Re your question about the REPL, what the REPL does is put a human being in the position of the caller of a function. Because we are made out of meat, the only way for a function to return a value to us is to show it to our meat eyes, and so it does look a lot like posting the same value to the terminal (an imperative command).
As u/deaddyfreddy points out, it's technically the environment that's showing you the value, not the service itself, and so e.g. whether it renders as a string or a literal is an environment setting rather than something the code does --- the code is returning a value to you just like it would if you yourself were a function: it doesn't know that you're not.
19
u/Coding-Kitten Jun 17 '26
Isn't this already how languages like Haskell work?
90% of the code is fully pure, & then the last 10% is IO mondads that fmap & traverse pure functions over monadic IO values it gets out of files/databases/networks.
6
u/sciolizer Jun 17 '26
In a classic type-and-effect system, functions with side effects can call functions without, but not vice versa, which formally distinguishes your "inside" from your "outside".
However, these days it's more popular to embed effects into monads, like Haskell and PureScript do, so that you can leverage System Fω's built-in polymorphism as polymorphism-of-effects.
5
u/brucejbell sard Jun 17 '26
At some point, it becomes reasonable to answer "isn't that a side effect?" with "no".
Consider running your pure function under a debugger, where you can set a breakpoint to trigger a script, which then launches the missiles or some other kind of irreversible effect.
Does that mean that every place you can set a breakpoint is a potential side effect? No, we have the intuitive notion that does not count. Pure functions also take time, allocate memory, burn power etc. but likewise we have mostly decided that these aren't the droids we're looking for.
The question your comment raises for me is: how far can you take this?
One practical problem with purely functional programming is that printf-style debugging doesn't work because the printf-equivalent is impure. Does that mean purely functional programming is necessarily less expressive in this way?
What if you treat logging as "not really an effect" like debugging or memory allocation. Can you do that? Will the purity police come and arrest you?
I think it's fine as long as what goes in the log, stays in the log. As long as information can't get from the log to your program, it's no different from that debugger case.
And sure, it's possible for logs to fill up storage and crash your program that way, or for your program to inspect the log files and get weird feedback that way. But we don't count that as proper side effect any more than we worry constantly about out-of-memory errors.
Anyway, you can push this farther. Why not add an explicit performance monitoring subsystem to your logging system? This adds state to your logger, but if there is no way for the pure code you're instrumenting to access it, it's still morally equivalent to the debugger example.
This kind of instrumentation is especially flexible if you can use the full power of the language to specify it!
3
u/fl00pz Jun 17 '26
Along with Haskell, you can look at Dafny to see how they separate pure functions from side-effecting methods.
1
u/Norphesius Jun 17 '26
People are mentioning the IO monad in Haskell, but for something thats maybe a bit closer to what you're talking about, OCaml is primarily functional, but has "escape hatches" to let you create mutable variables and other more procedural operations for convenience.
1
u/koflerdavid Jun 19 '26
IMHO, you have the following possibilites:
((...))in bash. You will notice that this device will appear all over the place and soon long for a way to reduce the verboseness, or to make it implicit.2
u/lgastako Jun 17 '26
In addition to Haskell you may way to check out Elm. It basically provides a runtime that does all the effects and you write only pure code which calls into the runtime and vice versa.
2
u/thmprover Jun 18 '26
Everyone mentions the IO monad, but did you know you can can implement it in "impure" functional languages like Standard ML? This was described in Andrew D Gordon's dissertation Functional Programming and Input/Output, specifically the last chapter contains the relevant code. For an expedited discussion, see blog post.
1
u/azhder Jun 17 '26
In functional languages it isn't the side-effect-free code that is put into a shell, but the mutable world. Monads are like wrappers around the mutable stuff, like a white blood cell surrounding the toxin and not letting it touch and "dirty up" the pure functional code. You give your pure code (like a function) to the monad and it uses your code to manipulate the outside world and change it.
1
u/dcbst Jun 17 '26
Have you considered SPARK Ada? It's not so much about avoiding side effects, but defining the expected side effects and mathematically proving the absence of undefined effects.
1
u/catbrane Jun 17 '26
I think it's worth pointing out that Haskell's monads are also purely functional. They are all implemented entirely in Haskell (with a tiny amount of sugar), so they have to be, heh. They just let you express ideas in an imperative way.
You're right that it's all about the printing process. A monadic Haskell program is a lazy function from a list of input events to a list of output events, with production of the output events driving computation. It's a thin skin over a repl.
1
u/Vegetable_Bank4981 Jun 17 '26
The problem is how do you do “guaranteed to be pure” in actual concrete practice in the compiler?
Everyone wants this. It is basically the frontier of type system research right now. But it makes inference undecideable, or you accept godawful ergonomics and uselessly broad fn signatures on everything.
Read the 2014 Leijen paper on Koka. Their reasoning for row polymorphism and why it can’t be described by a set of side effects instead will tell you what you need.
Otherwise you do it with the typed monad like haskell but this forces lazy evaluation and a type system God Himself barely understands.
OR you just implement two languages with a manually typed ffi boundary. Anyone could do this, many have, it isn’t that useful in practice.