Skip to main content
12 events
when toggle format what by license comment
May 2, 2016 at 20:35 comment added Andres F. @ZachLipton Of course, but I didn't want to complicate my answer. Assume that, in the context of this answer, this is a magical database that doesn't need to be configured and never fails to write. Or replace "database" with "logging to stdout and ignoring all output errors".
May 2, 2016 at 20:33 comment added Andres F. @ComicSansMS Indeed! I didn't want to complicate my answer with Haskell's additional type system features.
May 2, 2016 at 8:56 comment added ComicSansMS Since Haskell was mentioned: In Haskell adding a side-effect like that requires changing the signature of the function. (think of it like giving the original database as an additional input and getting the modified database as an additional return value of the function). It is actually possible to model side effects quite elegantly in the type system that way, it's just that today's mainstream languages don't care enough about side-effects and pureness to do this.
May 2, 2016 at 6:34 comment added sara @ZachLipton agree, writeToDatabase is almost guaranteed to be impure in more ways. it probably reads a connection string from disk or some cache and it sends a web request to the db and might throw exceptions and so on. and as we all know, impurity spreads and taints everything that touches it. no function calling an impure function can be considered pure.
May 2, 2016 at 2:38 comment added Zach Lipton I'd argue the second function could break rule #1 too. Depending on the language and the error handling practices of the database API in use, the function may well not return anything at all if the database is unavailable or the db write fails for some reason.
May 1, 2016 at 1:39 comment added Joshua Humph. Let us say that we have such a function that is pure except for side effects and is also guaranteed to have such behavior where your two samples are equivalent. (I had this case come up in fact so it's not hypothetical.) I think we're not quite done.
Apr 30, 2016 at 22:43 history edited Andres F. CC BY-SA 3.0
added 1 character in body
Apr 30, 2016 at 22:37 vote accept m0meni
Apr 30, 2016 at 22:37 vote accept m0meni
Apr 30, 2016 at 22:37
Apr 30, 2016 at 22:37 comment added Andres F. I agree it's confusing. I can only think of contrived examples. Say f(x) depends not only on x, but also on some external global variable y. Then, if f has the property of RT you can freely swap all its occurrences with its return value as long as you don't touch y. Yes, my example is dubious. But the important thing is: if f writes to the database (or writes to a log) it loses the property of RT: now it doesn't matter whether you leave global y untouched, you know the meaning of your program changes depending on whether you actually call f or simply use its return value.
Apr 30, 2016 at 22:32 comment added Ixrec Isn't "only depends on its input" redundant given referential transparency? Which would imply RT is synonymous with purity? (I'm getting more confused about this the more sources I look up)
Apr 30, 2016 at 22:21 history answered Andres F. CC BY-SA 3.0