r/AskComputerScience • u/thekeyofPhysCrowSta • Oct 14 '25
Is there a term for "almost pure" functions?
For example, a function that reads an external file is not pure, but if the file contents is constant, we can pretend it's pure. Or, a function that modifies an external lookup table has a side effect and is not pure, but if the lookup table is only used to cache the results of that function, then it behaves as if it's pure.
33
Upvotes
21
u/Cogwheel Oct 14 '25
Observational purity, I believe.
6
u/disposepriority Oct 14 '25 edited Oct 14 '25
The term is functions, I believe.
I/O can't be pure (and don't have to be), which is why the functions that handle it should only do IO after which you're back to purity.
Some more context:
a function updateCache(k, v) is pure for me, because it only operates on its subject, it's not a side effect.
so would a function that reads from its primary subject
Because given the same THING it will always do the same thing ;) in the pure line, while the impure line has no such guarantee
You could argue that the cache could be affecting something else, but the update cache method is still pure, it's whoever is using it (if) that isn't (unless they are receiving it as a parameter, in which case you're back to the pure example, if they receive the same thing from the cache they do the same thing)
3
u/dmbergey Oct 14 '25
I say "morally pure". Examples include reading env vars (for most programs that choose not to support / specify behavior of changing vars while running) or global consts that can't quite be computed at compile time, but are initialized and then left alone.
As long as we want only two categories, pure and IO, we need to make these choices at the margin. We almost always accept that memory allocation / page faults are not IO, pragmatically.
5
u/spacepopstar Oct 14 '25
There can not be “almost pure” because that assumes the dangerous things that drove people to think about purity.
Someone can unplug your filesystem, and now the file contents are both constant and unavailable.
Someone else can modify the lookup table, and now your cache has de-synched with your table.
Once your program interacts with a system outside of the control of your program, there are a bunch of conditions that will bite you if you “pretend they are pure”. Computational purity is kind of like saying “here is the undeniably math-y part of the program, where the hardware is irrelevant to the results”
2
u/Most_Double_3559 Oct 14 '25
Are you looking for the term "hermetic" maybe?
For example:
https://bazel.build/basics/hermeticity
2
u/[deleted] Oct 14 '25
There are some similar concepts that come up when discussing the I/O monad in Haskell. It's referred to as "referentially transparent" or "encapsulated impurity" because it's not truly pure but we can generally think of it as pure when writing Haskell.
2
u/ameriCANCERvative Oct 14 '25
I like how this comes off like a question posed during a cult gathering.
Yes we can agree some functions are sinful and impure, but what about those functions that have sinned only partially? What about the functions that sin predictably, such that we can determine when they will sin and thus be able to prevent them from sinning in the first place? Surely they deserve a spot in heaven, no?
Can we maybe come up with another term for them? My mother may have acted sinfully but she’s a good person on the whole, I promise. Just don’t take her near a casino and everything is fine.
1
u/Just-Hedgehog-Days Oct 14 '25
what's the purpose of purity for you? Can you're function be treated that way?
1
u/Temporary_Pie2733 Oct 14 '25
I don’t think that’s a useful distinction. If the file is truly constant, make it a module and let I/O issues be independent of your code. And why have an external cache instead of a closure?
1
u/martionfjohansen Oct 15 '25
I have used the term "virtual pure" in my work.
1
u/ci2e Oct 15 '25
You're looking for parametric functions.
If you annotate every function type with the kinds of effect they can produce, pure functions have no effects, whereas your read_file function has a filesystem effect.
When you write your code in such a way that you pass the parts that may produce side effects using a higher order function, the signature becomes parametric in the effect, similar to generic functions.
So your setup has the filling shape:
do_stuff(read_file) { logic goes here }
Do_stuff is pure exactly iff read_file is pure. The signature would be like this:
do_stuff:: eff x (path -> contents) -> eff x void
Here, x is a generic effect Parameter. Only after instantiating do_stuff with the concrete read file implementation the effect becomes a concrete impure one.
1
u/[deleted] Oct 15 '25
If
1
u/chisui Oct 15 '25 edited Oct 15 '25
Purity depends on the context since no fuction that's actually executed is ever truely pure.
The list goes on. Even the most pure function will at least heat up your CPU.
I would say the core characteristic of a pure function that we as programmers are interrested in is refential transparency. Or more pragmatically: A function is pure if you can replace its invocation with its result without causing side effects. What counts as a side effect depends on the context.
Issues arise if you have different assumptions of purity come together.
These different notions of purity can also be modeled in typesystems to prevent errors. For example using effect systems.
0
u/iOSCaleb Oct 14 '25
Impure or not pure.
0
u/Bubbly_Safety8791 Oct 14 '25
Idempotent could capture some of what you’re after.