I'm trying to reconcile the definition of referential transparency with how OCaml handles polymorphic types and side-effects. I read on https://web.archive.org/web/20120729232358/http://www.csc.villanova.edu/~dmatusze/resources/ocaml/ocaml.html that
A definition is said to have referential transparency if its meaning does not depend on the context it is in. Functions in OCaml have referential transparency, that is, changing the context (other variables and other functions) does not change the meaning of any functions you have already defined. This fact can be crucial when you are debugging a program, because you are likely to be redefining functions fairly frequently.
But the way I understand things, this can't be true in OCaml because it is possible to perform a whole bunch of side-effects (like writing to files and performing other calculations) before returning whatever was fed into the function.
You could potentially have a function f : string -> string
so that f "a"
does not equal f "a"
. We can drop in some side-effecting expressions into the body of the function that are completely invisible in the type description of f
.
As an example f
could be defined to return the first line of some file. There could be a function somewhere in the context of f
that is changed which affects what first line f
returns. Or worse, some function in the context could delete the file that f
depends on which would make f
undefined.
So is OCaml referentially transparent or am I missing something?
Ocaml isn't referentially transparent, exactly as you have explained.
Maybe Matuszek wants to emphasize the functional aspects of Ocaml, but in my opinion he is misleading or flat out wrong.
Section Expressions (but not statements) for example says that OCaml is a purely functional language
and OCaml claims to be stateless
. Section Omissions says
Loops, also, have been omitted, but they are not terribly useful in a purely functional language, anyway.
which is funny since loops wouldn't have been added to Ocaml if they weren't useful.