Question

I'm trying to reconcile the definition of referential transparency with how OCaml handles polymorphic types and side-effects. I read on 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?

Was it helpful?

Solution

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.

OTHER TIPS

Referential transparency means the context of the function doesn't affect the result, while a "pure" function is a function that depends only on its arguments and has no side-effects. For example in

# let y = 10;;
# let f x = (Printf.printf "%d+%d=%d\n" x y (x+y); x+y);;
val f : int -> int = <fun>
# f 5;;
5+10=15
- : int = 15
# let y = 3;;
val y : int = 3
# f 5;;
5+10=15
- : int = 15

we see that f is referentially transparent (because it doesn't change when you redefine y) but impure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top