Link transparency in OCaml

I am trying to align the definition of referential transparency with how OCaml handles polymorphic types and side effects. I read http://www.csc.villanova.edu/~dmatusze/resources/ocaml/ocaml.html that

It is said that a definition has referential transparency if its meaning is independent of the context in which it is located. Functions in OCaml have referential transparency, that is, changing the context (other variables and other functions) does not change the meaning of any functions that you have already defined. This fact can be crucial when you are debugging a program because you are likely to override functions quite often.

But as I understand things, this cannot be true in OCaml, because you can perform a whole bunch of side effects (for example, write to files and perform other calculations) before returning everything that was served to this function.

You could have a function f : string -> stringto f "a"not equal f "a". We can discard some secondary expressions in the function body that are completely invisible in the type description f.

As an example, it can be defined fto return the first line of some file. There may be a function somewhere in the context fthat changes, which affects what the first line returns f. Or, even worse, some functions in the context may delete the file on which it depends f, which will make fundefined.

, OCaml - ?

+3
2

Ocaml , .

, , , , .

( ), , , OCaml is a purely functional language OCaml claims to be stateless. Omissions

, .

, Ocaml, .

+6

, , "" - , . ,

# 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

, f ( y), .

+1

All Articles