SlideShare a Scribd company logo
What Referential Transparency can
do for you
What exactly is referential
transparency?
Referential transparency is mostly
synonymous with purity, meaning
lack of side-effects
As functional programmers we want to avoid
side-effects.
But what exactly is a side-effect?
No mutability?
Always returns the same thing given the same
input?
Definition of a pure function
def sum(list: List[Int]): Int = {
launchNukes()
list.foldLeft(0)(_ + _)
}
Is this pure?
def sum(list: List[Int]): Int = {
var z = 0
for (i <- list) {
z += i
}
z
}
Is this pure?
Referential transparency just means
that exchanging a term for any other
term that refers to the same entity
does not change the program.
def sum(list: List[Int]): Int = {
var z = 0
for (i <- list) {
z += i
}
z
}
Is this referentially transparent?
sum(List(1,2,3)) + sum(List(1,2,3))
val x = sum(List(1,2,3))
x + x
Are these two programs equivalent?
- Testability! Pure functions are extremely easy to test
- Know when and where our effects occur.
- Both you and the compiler now have guarantees that your code can be
reasoned with, this makes refactoring a walk in the park.
Why is this useful?
- Memoization; If we can guarantee a function to always return the same
thing given the same input, the compiler can easily memoize the result.
- Parallelization; Without sharing state, we can easily run multiple functions in
parallel without having to worry about deadlocks or data races.
- Common Subexpression Elimination; The compiler can know exactly when
multiple expressions evaluate to the same value and can optimize this away
Potential optimizations
Effectful and non-effectful code
should be separated!
- C++’s constexpr
- Fortran’s PURE FUNCTION
- D’s pure
Examples
impure def launchNukes(): Unit = {
... //call impure code here
}
impure def main(): Unit = {
sum(List(1,2,3))
launchNukes()
}
Impurity annotations!
Then what about async?
async impure def launchNukes(): Unit = {
... //call impure code here
}
async impure def main(): Unit = {
sum(List(1,2,3))
await launchNukes()
}
Async annotations!
We’d need to add language feature
for every different kind of effect
Effects shouldn’t be “to the side”, they
should be first class values that we can
supply to our runtime.
Realization:
type Effect = () => Unit
type Effects = List[SideEffect]
def main(): Effects = List(
() => println(“Launching Nukes”),
() => launchNukes()
)
How should we model our effects?
type Effects = List[Any => Any]
def performSideEffects(effs: Effects): Unit = {
effs.foldLeft(())((acc, cur) => cur(acc))
}
def main(): Effects = List(
_ => localStorage.getItem("foo"),
foo => println(foo),
_ => new Date().getFullYear(),
year => println(year) //no access to foo here
)
Hmmmm...
trait Effect[A] {
def chain[B](f: A => Eff[B]): Eff[B]
}
def main(): Effect[Unit] =
localStorage.getItem("foo")
.chain(foo => putStrLn(foo))
Let’s try again!
trait IO[A] {
def flatMap[B](f: A => IO[B]): IO[B]
}
def main(): IO[Unit] = for {
foo <- localStorage.getItem("foo")
_ <- putStrLn(foo)
date <- Date.currentYear()
_ <- putStrLn(s"It's $date, $foo")
} yield ()
Expressed differently
We accidentally invented Monads!
class SimpleIO[A](unsafeRun: () => A) extends IO[A] {
def flatMap[B](f: A => SimpleIO[B]): SimpleIO[B] =
SimpleIO(() => f(unsafeRun()).unsafeRun())
}
object SimpleIO {
def pure(a: A): SimpleIO[A] =
SimpleIO(() => a)
}
Most simple implementation
Monads allow us to treat
computations as values that can be
passed around as first-class citizens
within the pure host language
Where can I find this?
- cats-effect IO
- Monix Task
- fs2
Eliminating only some side-effects can
only get us so far.
Only by fully embracing purity can we
achieve the safety we’re looking for.
Conclusion
Thank you for listening!
Twitter: @LukaJacobowitz

More Related Content

What's hot

The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.5.1 book - Part 17 of 180The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.5.1 book - Part 17 of 180
Mahmoud Samir Fayed
 
Domain Redesigned
Domain RedesignedDomain Redesigned
Domain Redesignedcmsparks
 
Source codes for alerts lovs and reports generation(database)
Source codes for alerts lovs and reports generation(database)Source codes for alerts lovs and reports generation(database)
Source codes for alerts lovs and reports generation(database)welcometofacebook
 
(Fun)ctional go
(Fun)ctional go(Fun)ctional go
(Fun)ctional go
BenJanecke
 
Programming a guide gui
Programming a guide guiProgramming a guide gui
Programming a guide guiMahmoud Hikmet
 
Java Programming Workshop
Java Programming WorkshopJava Programming Workshop
Java Programming Workshop
neosphere
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointer
Gem WeBlog
 
Python basic Program
Python basic ProgramPython basic Program
Python basic Program
nuripatidar
 
Throttle and Debounce Patterns in Web Apps
Throttle and Debounce Patterns in Web AppsThrottle and Debounce Patterns in Web Apps
Throttle and Debounce Patterns in Web Apps
Almir Filho
 
Beginning with vi text editor
Beginning with vi text editorBeginning with vi text editor
Beginning with vi text editor
Jose Pla
 
7. input and output functions
7. input and output functions7. input and output functions
7. input and output functions
Way2itech
 
3. control statement
3. control statement3. control statement
3. control statement
Shankar Gangaju
 
Cuarto Punto Parte A
Cuarto Punto Parte ACuarto Punto Parte A
Cuarto Punto Parte Agustavo206
 
Let it Flow - Introduction to Functional Reactive Programming
Let it Flow - Introduction to Functional Reactive ProgrammingLet it Flow - Introduction to Functional Reactive Programming
Let it Flow - Introduction to Functional Reactive Programming
Artur Skowroński
 
C language operator
C language operatorC language operator
C language operator
cprogram
 

What's hot (20)

The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.5.1 book - Part 17 of 180The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.5.1 book - Part 17 of 180
 
Loop Statements [5] M
Loop Statements [5] MLoop Statements [5] M
Loop Statements [5] M
 
Event handling
Event handlingEvent handling
Event handling
 
Domain Redesigned
Domain RedesignedDomain Redesigned
Domain Redesigned
 
Source codes for alerts lovs and reports generation(database)
Source codes for alerts lovs and reports generation(database)Source codes for alerts lovs and reports generation(database)
Source codes for alerts lovs and reports generation(database)
 
(Fun)ctional go
(Fun)ctional go(Fun)ctional go
(Fun)ctional go
 
Programming a guide gui
Programming a guide guiProgramming a guide gui
Programming a guide gui
 
Java Programming Workshop
Java Programming WorkshopJava Programming Workshop
Java Programming Workshop
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointer
 
Python basic Program
Python basic ProgramPython basic Program
Python basic Program
 
week-18x
week-18xweek-18x
week-18x
 
Throttle and Debounce Patterns in Web Apps
Throttle and Debounce Patterns in Web AppsThrottle and Debounce Patterns in Web Apps
Throttle and Debounce Patterns in Web Apps
 
Beginning with vi text editor
Beginning with vi text editorBeginning with vi text editor
Beginning with vi text editor
 
7. input and output functions
7. input and output functions7. input and output functions
7. input and output functions
 
3. control statement
3. control statement3. control statement
3. control statement
 
Cuarto Punto Parte A
Cuarto Punto Parte ACuarto Punto Parte A
Cuarto Punto Parte A
 
Let it Flow - Introduction to Functional Reactive Programming
Let it Flow - Introduction to Functional Reactive ProgrammingLet it Flow - Introduction to Functional Reactive Programming
Let it Flow - Introduction to Functional Reactive Programming
 
week-11x
week-11xweek-11x
week-11x
 
C language operator
C language operatorC language operator
C language operator
 

Similar to What Referential Transparency can do for you

ATS Programming
ATS ProgrammingATS Programming
ATS Programming
Zhiqiang Ren
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
Taisuke Oe
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
Simone Di Maulo
 
Parametricity
ParametricityParametricity
Parametricity
Shyamendra Solanki
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#Dmitri Nesteruk
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
DrRajeshreeKhande
 
What are monads?
What are monads?What are monads?
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
Damian Jureczko
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
Hang Zhao
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
Michal Bigos
 
ZIO Queue
ZIO QueueZIO Queue
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019
Daniel Pfeiffer
 
Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
Patrick Viafore
 
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
Python : Functions
Python : FunctionsPython : Functions
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
vikram mahendra
 

Similar to What Referential Transparency can do for you (20)

ATS Programming
ATS ProgrammingATS Programming
ATS Programming
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
Parametricity
ParametricityParametricity
Parametricity
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
 
What are monads?
What are monads?What are monads?
What are monads?
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019cats.effect.IO - Scala Vienna Meetup February 2019
cats.effect.IO - Scala Vienna Meetup February 2019
 
Tip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
 
Python programming
Python  programmingPython  programming
Python programming
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 

More from Luka Jacobowitz

Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020
Luka Jacobowitz
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
Luka Jacobowitz
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
Luka Jacobowitz
 
Up and Running with the Typelevel Stack
Up and Running with the Typelevel StackUp and Running with the Typelevel Stack
Up and Running with the Typelevel Stack
Luka Jacobowitz
 
Principled Error Handling - Scalapeño
Principled Error Handling - ScalapeñoPrincipled Error Handling - Scalapeño
Principled Error Handling - Scalapeño
Luka Jacobowitz
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
Luka Jacobowitz
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
Luka Jacobowitz
 
Advanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to FreeAdvanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to Free
Luka Jacobowitz
 
Traversals for all ocasions
Traversals for all ocasionsTraversals for all ocasions
Traversals for all ocasions
Luka Jacobowitz
 
Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
Luka Jacobowitz
 
Scala UA 2017
Scala UA 2017Scala UA 2017
Scala UA 2017
Luka Jacobowitz
 
Reactive Programming in the Browser feat. Scala.js and Rx
Reactive Programming in the Browser feat. Scala.js and RxReactive Programming in the Browser feat. Scala.js and Rx
Reactive Programming in the Browser feat. Scala.js and Rx
Luka Jacobowitz
 
Reactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScriptReactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScript
Luka Jacobowitz
 

More from Luka Jacobowitz (13)

Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Up and Running with the Typelevel Stack
Up and Running with the Typelevel StackUp and Running with the Typelevel Stack
Up and Running with the Typelevel Stack
 
Principled Error Handling - Scalapeño
Principled Error Handling - ScalapeñoPrincipled Error Handling - Scalapeño
Principled Error Handling - Scalapeño
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
Advanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to FreeAdvanced Tagless Final - Saying Farewell to Free
Advanced Tagless Final - Saying Farewell to Free
 
Traversals for all ocasions
Traversals for all ocasionsTraversals for all ocasions
Traversals for all ocasions
 
Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
 
Scala UA 2017
Scala UA 2017Scala UA 2017
Scala UA 2017
 
Reactive Programming in the Browser feat. Scala.js and Rx
Reactive Programming in the Browser feat. Scala.js and RxReactive Programming in the Browser feat. Scala.js and Rx
Reactive Programming in the Browser feat. Scala.js and Rx
 
Reactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScriptReactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScript
 

Recently uploaded

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 

Recently uploaded (20)

Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 

What Referential Transparency can do for you

  • 2. What exactly is referential transparency?
  • 3. Referential transparency is mostly synonymous with purity, meaning lack of side-effects
  • 4. As functional programmers we want to avoid side-effects. But what exactly is a side-effect?
  • 5. No mutability? Always returns the same thing given the same input? Definition of a pure function
  • 6. def sum(list: List[Int]): Int = { launchNukes() list.foldLeft(0)(_ + _) } Is this pure?
  • 7. def sum(list: List[Int]): Int = { var z = 0 for (i <- list) { z += i } z } Is this pure?
  • 8. Referential transparency just means that exchanging a term for any other term that refers to the same entity does not change the program.
  • 9. def sum(list: List[Int]): Int = { var z = 0 for (i <- list) { z += i } z } Is this referentially transparent?
  • 10. sum(List(1,2,3)) + sum(List(1,2,3)) val x = sum(List(1,2,3)) x + x Are these two programs equivalent?
  • 11. - Testability! Pure functions are extremely easy to test - Know when and where our effects occur. - Both you and the compiler now have guarantees that your code can be reasoned with, this makes refactoring a walk in the park. Why is this useful?
  • 12. - Memoization; If we can guarantee a function to always return the same thing given the same input, the compiler can easily memoize the result. - Parallelization; Without sharing state, we can easily run multiple functions in parallel without having to worry about deadlocks or data races. - Common Subexpression Elimination; The compiler can know exactly when multiple expressions evaluate to the same value and can optimize this away Potential optimizations
  • 13. Effectful and non-effectful code should be separated!
  • 14. - C++’s constexpr - Fortran’s PURE FUNCTION - D’s pure Examples
  • 15. impure def launchNukes(): Unit = { ... //call impure code here } impure def main(): Unit = { sum(List(1,2,3)) launchNukes() } Impurity annotations!
  • 16. Then what about async?
  • 17. async impure def launchNukes(): Unit = { ... //call impure code here } async impure def main(): Unit = { sum(List(1,2,3)) await launchNukes() } Async annotations!
  • 18. We’d need to add language feature for every different kind of effect
  • 19. Effects shouldn’t be “to the side”, they should be first class values that we can supply to our runtime. Realization:
  • 20. type Effect = () => Unit type Effects = List[SideEffect] def main(): Effects = List( () => println(“Launching Nukes”), () => launchNukes() ) How should we model our effects?
  • 21. type Effects = List[Any => Any] def performSideEffects(effs: Effects): Unit = { effs.foldLeft(())((acc, cur) => cur(acc)) } def main(): Effects = List( _ => localStorage.getItem("foo"), foo => println(foo), _ => new Date().getFullYear(), year => println(year) //no access to foo here ) Hmmmm...
  • 22. trait Effect[A] { def chain[B](f: A => Eff[B]): Eff[B] } def main(): Effect[Unit] = localStorage.getItem("foo") .chain(foo => putStrLn(foo)) Let’s try again!
  • 23. trait IO[A] { def flatMap[B](f: A => IO[B]): IO[B] } def main(): IO[Unit] = for { foo <- localStorage.getItem("foo") _ <- putStrLn(foo) date <- Date.currentYear() _ <- putStrLn(s"It's $date, $foo") } yield () Expressed differently
  • 25. class SimpleIO[A](unsafeRun: () => A) extends IO[A] { def flatMap[B](f: A => SimpleIO[B]): SimpleIO[B] = SimpleIO(() => f(unsafeRun()).unsafeRun()) } object SimpleIO { def pure(a: A): SimpleIO[A] = SimpleIO(() => a) } Most simple implementation
  • 26. Monads allow us to treat computations as values that can be passed around as first-class citizens within the pure host language
  • 27. Where can I find this? - cats-effect IO - Monix Task - fs2
  • 28. Eliminating only some side-effects can only get us so far. Only by fully embracing purity can we achieve the safety we’re looking for. Conclusion
  • 29. Thank you for listening! Twitter: @LukaJacobowitz