FunHop: Understanding Referential Transparency

Table of contents
Reading Time: 3 minutes

In this episode of FunHop, we would try to look at what is Referential Transparency, what is substitution model and how being immutable really helps.

Referential transparency means that we can exchange the expression by its value or a value by the expression and nothing changes. Pure functions are referentially transparent. Any function f(a)=b is a pure function if for the same “a” we end up getting the same “b” without any internal or external state change.

For example the + function is a pure function
2 + 3 and

val c= 2
c + 3

would always yield the same result. There are no side effects either.

Now, let us look at Referential Transparency.

Loot at the code block below

Now, the value of c remains 9 even if we substitue the expression with the value of x which is 7. Taking it a step further, lets look at the following code

As you would agree, the results would remain the same.
Hence, if the expression can be replaced by its value and vice versa AND nothing changes then it is called Referential Transparency. This model of substituting values for expressions is called Substitution Model.

Substitution model gives us a way to naturally reason code. You would observe that substitution model lends itself easily to immutability. Only when objects are immutable, we can substitute them without any concerns on the results. Ok, let us look at a more concrete example

For example, let us refer to a money class which is represented like this

Aha! some weirdness here, the add method mutates the money object and returns the mutated object. Now, for the following block of code

The output is
Your sum1 is Money(40)
Your sum2 is Money(50)

Now going by the Referential Transparency and Substitution model, let us replace money2 by the expression for money2 which is money1.add(20)
Hence our code becomes

Now the output becomes
Your sum1 is Money(60)
Your sum2 is Money(90)

Weird Huh? 😉

This is different than what we got first. This shows that money2 expression is NOT referentially transparent. We cannot substitute the expression with its values and get the same result.

Ok, now let us change the code a little and have GoodMoney instead which DOES NOT mutate the object.

Now let us start the same cycle with GoodMoney

The output is
Your sum1 is GoodMoney(40)
Your sum2 is GoodMoney(40)

And now let us do the substitution,

What do you expect the output to be?
Of course it is the same
Your sum1 is GoodMoney(40)
Your sum2 is GoodMoney(40)

Hence, as you would observe that substitution when it is referentially transparent, saves us the headache of mapping state transitions, which is not easy. Immutability lends itself directly to referential transparency and substitution model. The evaluated expression when doing substitution is purely local and requires only local reasoning. If the operations on your object return another object and are immutable then you are good.

The code is present on the Knoldus GitHub Account. Till we meet again, write immutable code.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

1 thought on “FunHop: Understanding Referential Transparency4 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading