SlideShare a Scribd company logo
Purely functional
programming -
the red pill
JavaOne 2015
Dierk König
Canoo
mittie
Takeaway
Frege is fundamentally different
based on science
made for practical use
Why do we care?
a	=	1	
b	=	2	
c	=	b	
b	=	a	
a	=	c
1
1 2
1 2
2
1
1
22
1 2
time1
time2
time3
state1 state2 state3
Operational Reasoning
a	=	1	
b	=	2	
c	=	b	
b	=	a	
a	=	c
1
1 2
1 2 2
1 1 2
2 1 2
time1
time2
time3
state1 state2 state3
We need a debugger!
Using functions
a	=	1	
b	=	2	
						
1
1 2
Using functions
a	=	1	
b	=	2	
						
1
1 2
2 1
swap(a,b)	=	(b,a)
The Key Idea
Assignments change state and
introduce time.

Statements do side effects.
Let’s just program without
assignments or statements!
Developer

Discipline
Pure 

Functional

Language
Online REPL
try.frege-lang.org
Define a Function
frege>		times	a	b	=	a	*	b	
frege>		times	3	4	
12	
frege>	:type	times	
Num	α	=>	α	->	α	->	α
Define a Function
frege>		times	a	b	=	a	*	b	
frege>	(times	3)	4	
12	
frege>	:type	times	
Num	α	=>	α	->(α	->	α)
no types declared
function appl.
left associative
tell the inferred
type
typeclass
only 1
parameter!
return type is
a function!
thumb: „two params
of same numeric type
returning that type“
no comma
Reference a Function
frege>	twotimes	=	times	2	
frege>	twotimes	3	
6		
frege>	:t	twotimes	
Int	->	Int
Reference a Function
frege>	twotimes	=	times	2	
frege>	twotimes	3	
6	
frege>	:t	twotimes	
Int	->	Int
No second
argument!
„Currying“, „schönfinkeling“,
or „partial function
application“.
Concept invented by
Gottlob Frege.
inferred types
are more specific
Function Composition
frege>	twotimes	(threetimes	2)	
12	
frege>	sixtimes	=	twotimes	.	threetimes	
frege>	sixtimes	2	
frege>	:t	sixtimes	
Int	->	Int
Function Composition
frege>	twotimes	(threetimes	2)	
12	
frege>	sixtimes	=	twotimes	.	threetimes	
frege>	sixtimes	2	
frege>	:t	sixtimes	
Int	->	Int
f(g(x))
more about this later
(f ° g) (x)
Pattern Matching
frege>	times	0	(threetimes	2)	
0	
frege>	times	0	b	=	0
Pattern Matching
frege>	times	0	(threetimes	2)	
0	
frege>	times	0	b	=	0
unnecessarily evaluated
shortcuttingpattern matching
Lazy Evaluation
frege>	times	0	(length	[1..])	
0
endless sequence
evaluation would never stop
Pattern matching and
non-strict evaluation
to the rescue!
Pure Functions
Java	
T	foo(Pair<T,U>	p)	{…}	
Frege	
foo	::	(α,β)	->	α
What could
possibly happen?
What could
possibly happen?
Pure Functions
Java	
T	foo(Pair<T,U>	p)	{…}	
Frege	
foo	::	(α,β)	->	α
Everything!

State changes, 

file or db access,
missile launch,…
a is returned
can be cached (memoized)

can be evaluated lazily

can be evaluated in advance

can be evaluated concurrently

can be eliminated 

in common subexpressions
can be optimized
Pure Functions
Is my method pure?
Let the type system find out!
Magic (?)
Think	of	a	generic	function	
f	::	[a]	->	[a]	
and	any	specific	function	
g	::	a	->	b
Magic (?)
[a] [a]
[b] [b]
f
f
map g map g
Magic (?)
[1,2,3] [3,2,1]
[2,4,6] [6,4,2]
reverse
reverse
map (*2) map (*2)
Commutative square of natural transformations.

Robust Refactoring
Changing the order of operations



Requires purity.

100% safe if the type system 

can detect natural transformations
Applied Category Theory
credits: Phil Wadler, Tech Mesh 2012 - Faith, Evolution, and Programming Languages
QuickCheck
import	Test.QuickCheck

f	=	reverse

g	=	(*2)		
commutativity	=	property	(xs	->	

	map	g	(f	xs)	==	f	(map	g	xs)	)
Java Interoperability
do not mix OO and FP
combine them
Java -> Frege
Scripting: JSR 223
Service:

compile *.fr file

call static method
simple
pure native encode java.net.URLEncoder.encode :: String -> String
encode “Dierk König“


native millis java.lang.System.currentTimeMillis :: () -> IO Long
millis ()
millis ()
past = millis () - 1000 

Does not compile!
Frege -> Java
This is a key distinction between Frege and

other JVM languages!
even Java can be pure
allows calling Java

but never unprotected!
is explicit about effects

just like Haskell
Frege
Type System
Global type inference and thus 

more safety and less work 

for the programmer
You don’t need to specify any types at all!
But sometimes you do anyway…
Mutable

I/O
Mutable
Mutable
Keep the mess out!
Pure Computation
Pure Computation
Pure Computation
Mutable

I/O
Mutable
Mutable
Keep the mess out!
Pure Computation
Pure Computation
Pure Computation
Ok, these are Monads. Be brave. Think of them as contexts
that the type system propagates and makes un-escapable.
Thread-
safe by
design!
Checked
by
compiler
Java
Java
Java
Service Based Design
Frege
Frege
Frege
A typical integration option: use Frege code for services
Some Cool Stuff
Zipping
addzip	[]	_		=	[]

addzip	_		[]	=	[]		
addzip	(x:xs)	(y:ys)	=	

							(x	+	y	:	addzip	xs	ys	)
Zipping
addzip	[]	_		=	[]

addzip	_		[]	=	[]		
addzip	(x:xs)	(y:ys)	=	

							(x	+	y	:	addzip	xs	ys	)



use as
addzip	[1,2,3]		
							[1,2,3]		
				==	[2,4,6]
Pattern matching
feels like Prolog
Why only for the (+) function?
We could be more general…
High Order Functions
zipWith	f	[]	_		=	[]

zipWith	f	_		[]	=	[]		
zipWith	f	(x:xs)	(y:ys)	=	

							(f	x	y	:	zipWith	xs	ys	)
High Order Functions
zipWith	f	[]	_		=	[]

zipWith	f	_		[]	=	[]		
zipWith	f	(x:xs)	(y:ys)	=	

							(f	x	y	:	zipWith	xs	ys	)
use as
zipWith	(+)	[1,2,3]		
												[1,2,3]		
									==	[2,4,6]	
and, yes we can now define
	 addzip	=			 	
	 	 	 zipWith	(+)			
invented by Gottlob Frege
Fizzbuzz
http://c2.com/cgi/wiki?FizzBuzzTest
https://dierk.gitbooks.io/fregegoodness/

chapter 8 „FizzBuzz“
Fizzbuzz Imperative
public	class	FizzBuzz{

		public	static	void	main(String[]	args){

				for(int	i=	1;	i	<=	100;	i++){

						if(i	%	15	==	0{		

								System.out.println(„FizzBuzz");

						}else	if(i	%	3	==	0){

								System.out.println("Fizz");

						}else	if(i	%	5	==	0){

								System.out.println("Buzz");

						}else{

								System.out.println(i);

}	}	}	}
Fizzbuzz Logical
fizzes			=	cycle			["",	"",	"fizz"]

buzzes			=	cycle			["",	"",	"",	"",	"buzz"]

pattern		=	zipWith	(++)	fizzes	buzzes

numbers		=	map	show	[1..]	
fizzbuzz	=	zipWith	max	pattern	numbers		
main	_			=	do

											for	(take	100	fizzbuzz)	println
Fizzbuzz Comparison
Imperative Logical
Conditionals 4 0
Operators 7 1
Nesting level 3 0
Sequencing sensitive transparent
Maintainability - - - +
List Comprehension
Pythagorean triples: a2 + b2 = c2
pyth	n	=	[		
	(x,y,z)		
	|	x	<-	[1..n],	y	<-	[1..n],	z	<-	[1..n],		
	x*x	+	y*y	==	z*z	
	]
List Comprehension
Pythagorean triples: a2 + b2 = c2
pyth	n	=	[		
	(x,y,z)		
	|	x	<-	[1..n],	y	<-	[1..n],	z	<-	[1..n],		
	x*x	+	y*y	==	z*z	
	]	
select from
where
„brute force“ or „executable specification“.
A more efficient solution:
List Comprehension
Pythagorean triples: a2 + b2 = c2
[	(m*m-n*n,	2*m*n,	m*m+n*n)		
|	m	<-	[2..],	n	<-	[1..m-1]		
]	
endless production think „nested loop“
„select“
functions
dynamic
„from“
empty
„where“
History
Java promise: „No more pointers!“
But NullPointerExceptions (?)
Frege is different
No More But
state no state (unless declared)
statements expressions (+ „do“ notation)
assignments definitions
variables ST monad as „agent“
interfaces type classes
classes & objects algebraic data types
inheritance parametric polymorphism
null references Maybe
NullPointerExceptions Bottom, error
Frege in comparison
practical
robust
Java
Groovy
Frege
Haskell
Frege in comparison
Java
Groovy
Frege
Haskell
concept by 

Simon Peyton-Jones
Frege makes the Haskell spirit
accessible to the Java programmer
and provides a new level of safety.
apply logic
run computers
practical
robust
Unique in Frege
Explicit effects

Purity = no effects

Guarantees extend into Java calls

Laziness enforces purity and

immutable values (no assignments)

Global type inference 

requires a purely functional language

(only expressions, no statements)
Why Frege
Robustness under parallel execution

Robustness under composition

Robustness under increments

Robustness under refactoring
Enables local and equational reasoning
Best way to learn FP
Why FP matters
Enabling incremental development

www.canoo.com/blog/fp1



Brush up computational fundamentals
„An investment in knowledge 

always pays the best interest.“
—Benjamin Franklin
Why Frege
it is just a pleasure to work with
How?
http://www.frege-lang.org

@fregelang

stackoverflow „frege“ tag

edX FP101 MOOC (still possible to join!)
Gottlob 

Frege
"As I think about acts of integrity and grace, 

I realise that there is nothing in my knowledge
that compares with Frege’s dedication to truth…
It was almost superhuman.“ —Bertrand Russel
"Not many people managed to create a revolution
in thought. Frege did.“ —Graham Priest
Lecture on Gottlob Frege:
http://www.youtube.com/watch?v=foITiYYu2bc
Dierk König
Canoo
mittie
Please vote at the machine!

More Related Content

PDF
FregeFX - JavaFX with Frege, a Haskell for the JVM
PDF
Frege - consequently functional programming for the JVM
PDF
JDD2015: Frege - how to program with pure functions - Dierk König
PPTX
Virtual function
ODP
Groovy Ast Transformations (greach)
PPTX
virtual function
PDF
Android antipatterns
PDF
Booting into functional programming
FregeFX - JavaFX with Frege, a Haskell for the JVM
Frege - consequently functional programming for the JVM
JDD2015: Frege - how to program with pure functions - Dierk König
Virtual function
Groovy Ast Transformations (greach)
virtual function
Android antipatterns
Booting into functional programming

What's hot (20)

ODP
Ast transformation
KEY
Mirah Talk for Boulder Ruby Group
PDF
Functional go
PDF
Introduction to functional programming (In Arabic)
PDF
(3) cpp procedural programming
PDF
Why Java Sucks and C# Rocks (Final)
PDF
Ruby Functional Programming
PDF
Handling inline assembly in Clang and LLVM
PPTX
Virtual function complete By Abdul Wahab (moon sheikh)
PPTX
Functional programming with Ruby - can make you look smart
PPTX
Clean Code Principles
PDF
A quick and fast intro to Kotlin
PDF
C# / Java Language Comparison
PPTX
Dev Concepts: Functional Programming
PPT
C++ programming
PPTX
Pure virtual function and abstract class
KEY
Scala: functional programming for the imperative mind
PDF
Clean Lambdas & Streams in Java8
PDF
Introduction to functional programming
PDF
Pure functions and immutable objects @dev nexus 2021
Ast transformation
Mirah Talk for Boulder Ruby Group
Functional go
Introduction to functional programming (In Arabic)
(3) cpp procedural programming
Why Java Sucks and C# Rocks (Final)
Ruby Functional Programming
Handling inline assembly in Clang and LLVM
Virtual function complete By Abdul Wahab (moon sheikh)
Functional programming with Ruby - can make you look smart
Clean Code Principles
A quick and fast intro to Kotlin
C# / Java Language Comparison
Dev Concepts: Functional Programming
C++ programming
Pure virtual function and abstract class
Scala: functional programming for the imperative mind
Clean Lambdas & Streams in Java8
Introduction to functional programming
Pure functions and immutable objects @dev nexus 2021
Ad

Viewers also liked (17)

PDF
JavaOne 2008: Designing GUIs 101
PDF
FregeDay: Design and Implementation of the language (Ingo Wechsung)
PDF
Software Transactional Memory (STM) in Frege
PDF
Quick into to Software Transactional Memory in Frege
PDF
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
PDF
J Ruby Whirlwind Tour
ODP
Solr Presentation5
PDF
Os Harkins
PDF
Os Ellistutorial
PDF
Os Keysholistic
PDF
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
PPT
Os Napier
PDF
Os Keyshacks
PDF
Os Raysmith
PDF
OCaml Labs introduction at OCaml Consortium 2012
PDF
Os Peytonjones
PDF
From object oriented to functional domain modeling
JavaOne 2008: Designing GUIs 101
FregeDay: Design and Implementation of the language (Ingo Wechsung)
Software Transactional Memory (STM) in Frege
Quick into to Software Transactional Memory in Frege
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
J Ruby Whirlwind Tour
Solr Presentation5
Os Harkins
Os Ellistutorial
Os Keysholistic
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
Os Napier
Os Keyshacks
Os Raysmith
OCaml Labs introduction at OCaml Consortium 2012
Os Peytonjones
From object oriented to functional domain modeling
Ad

Similar to Frege Tutorial at JavaOne 2015 (20)

PDF
JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...
PDF
Google Interview Questions By Scholarhat
PPTX
Iwsm2014 on automatically collectable metrics for software maintainability ...
PPTX
Introduction to julia
ODP
Clojure
PPTX
Clojure And Swing
PPT
Unit I Advanced Java Programming Course
PPTX
brief introduction to core java programming.pptx
PPT
00_Introduction to Java.ppt
PDF
Writing Macros
PPT
Java basic tutorial by sanjeevini india
PPT
Java basic tutorial by sanjeevini india
PDF
Get into Functional Programming with Clojure
PPT
Java if and else
PPTX
Things about Functional JavaScript
PPTX
COSCUP: Introduction to Julia
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PDF
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
PDF
Neal Ford Emergent Design And Evolutionary Architecture
PDF
Clean Code JavaScript
JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...
Google Interview Questions By Scholarhat
Iwsm2014 on automatically collectable metrics for software maintainability ...
Introduction to julia
Clojure
Clojure And Swing
Unit I Advanced Java Programming Course
brief introduction to core java programming.pptx
00_Introduction to Java.ppt
Writing Macros
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Get into Functional Programming with Clojure
Java if and else
Things about Functional JavaScript
COSCUP: Introduction to Julia
If You Think You Can Stay Away from Functional Programming, You Are Wrong
(chapter 5) A Concise and Practical Introduction to Programming Algorithms in...
Neal Ford Emergent Design And Evolutionary Architecture
Clean Code JavaScript

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Modernizing your data center with Dell and AMD
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Monthly Chronicles - July 2025
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Modernizing your data center with Dell and AMD
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Frege Tutorial at JavaOne 2015