I think I know what refertially transparent and pure mean. However here's a question about the two properties and how they differ.
As regards how referential transparency and/or purity are enforced in a language, I don't know much. (Not even this helped me understand.) I mean, I might know (kind of) how Haskell can deal with IO though being purely functional (see this), and I understand that I can't write impure functions because the type system just doesn't let me (or, better, it does let me in a controlled way, as I have to write unsafe
explicitly).
But in C++, like in many other languages, functions are normally non pure nor referentially transparent.
So on the one hand I have Haskell which is constructed as a pure language, where every function is pure. On the other hand I have C++ which has no way to enforce purity (or does it?¹).²
But would it be possible, in the future, for the C++ language to provide a pure
/whatever attribute that one could attach to a function so that the compiler would have to verify that the function is indeed pure (or compile-time fail otherwise)?
(¹) This question popped up in my mind when I first knew of [[gnu:pure]]
and [[gnu:const]]
. My understanding is that those (non-portable) attributes are for giving more guarantees to the compiler, so that it can optimize more stuff, not for telling it to check if the function is truly pure. After all this example seems to compile and run just fine.
(²) But I also remember another, very old language, which is not pure like Haskell, but gives you a PURE
attribute to tell that a functions must be pure, and the compiler checked it: Fortran.