SlideShare una empresa de Scribd logo
1 de 60
Descargar para leer sin conexión
Frege
konsequent funktionale Programmierung
für die JVM
Dierk König
Canoo
mittie
Dierk König
Canoo
mittie
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2
2
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2 2
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2 2
1
1
2
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2 2
1 1 2
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
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2 2
1 1 2
2 1 2
Why do we care?
a	
  =	
  1	
  
b	
  =	
  2	
  
c	
  =	
  b	
  
b	
  =	
  a	
  
a	
  =	
  c
1
1 2
1 2 2
1 1 2
2 1 2
state1 state2 state3
Why do we care?
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
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
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
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	
  remove	
  them!
differentFrege is
differentFrege is very
You would not believe
just how different it is!
Online REPL
http://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
Pure Functions
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
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
Frege
allows	
  calling	
  Java

	
  	
  	
  but	
  never	
  unprotected!	
  
is	
  explicit	
  about	
  effects

	
  	
  	
  just	
  like	
  Haskell	
  
Type System
Ubiquitous static typing allows

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
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	
  
fizzbuzz	
  =	
  zipWith	
  (max	
  .	
  show)	
  pattern	
  [1..]	
  	
  
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 - - - +
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
safe
Java
Groovy
Frege
Haskell
Frege in comparison
safe
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
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
How?
http://www.frege-­‐lang.org

@fregelang

stackoverflow	
  „frege“	
  tag

edX	
  FP101	
  MOOC	
  (start	
  Oct	
  15th!)	
  
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
Dierk König
Canoo
mittie
Please fill the feedback forms

Más contenido relacionado

La actualidad más candente

AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 

La actualidad más candente (20)

Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Ast transformation
Ast transformationAst transformation
Ast transformation
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVM
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekito
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 

Destacado

Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholistic
oscon2007
 
Solr Presentation5
Solr Presentation5Solr Presentation5
Solr Presentation5
oscon2007
 
Os Keyshacks
Os KeyshacksOs Keyshacks
Os Keyshacks
oscon2007
 
J Ruby Whirlwind Tour
J Ruby Whirlwind TourJ Ruby Whirlwind Tour
J Ruby Whirlwind Tour
oscon2007
 
Os Ellistutorial
Os EllistutorialOs Ellistutorial
Os Ellistutorial
oscon2007
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjones
oscon2007
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012
Anil Madhavapeddy
 

Destacado (16)

Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
Quick into to Software Transactional Memory in Frege
Quick into to Software Transactional Memory in FregeQuick into to Software Transactional Memory in Frege
Quick into to Software Transactional Memory in Frege
 
Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholistic
 
Solr Presentation5
Solr Presentation5Solr Presentation5
Solr Presentation5
 
Os Keyshacks
Os KeyshacksOs Keyshacks
Os Keyshacks
 
Os Harkins
Os HarkinsOs Harkins
Os Harkins
 
Os Napier
Os NapierOs Napier
Os Napier
 
J Ruby Whirlwind Tour
J Ruby Whirlwind TourJ Ruby Whirlwind Tour
J Ruby Whirlwind Tour
 
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss) FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
 
Os Ellistutorial
Os EllistutorialOs Ellistutorial
Os Ellistutorial
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
Os Raysmith
Os RaysmithOs Raysmith
Os Raysmith
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjones
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 

Similar a Frege - consequently functional programming for the JVM

Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
Jan Kronquist
 
Deterministic simulation testing
Deterministic simulation testingDeterministic simulation testing
Deterministic simulation testing
FoundationDB
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
seanmcq
 

Similar a Frege - consequently functional programming for the JVM (20)

JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...
JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...
JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk...
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
COSCUP: Introduction to Julia
COSCUP: Introduction to JuliaCOSCUP: Introduction to Julia
COSCUP: Introduction to Julia
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Deterministic simulation testing
Deterministic simulation testingDeterministic simulation testing
Deterministic simulation testing
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on Workshop
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
 
Clojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMClojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVM
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Python 1
Python 1Python 1
Python 1
 
NTU ML TENSORFLOW
NTU ML TENSORFLOWNTU ML TENSORFLOW
NTU ML TENSORFLOW
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Frege - consequently functional programming for the JVM

  • 4. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c
  • 5. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1
  • 6. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2
  • 7. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2
  • 8. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2
  • 9. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2 1 1 2
  • 10. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2 1 1 2
  • 11. 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
  • 12. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2 1 1 2 2 1 2
  • 13. Why do we care? a  =  1   b  =  2   c  =  b   b  =  a   a  =  c 1 1 2 1 2 2 1 1 2 2 1 2 state1 state2 state3
  • 14. Why do we care? 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
  • 15. 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
  • 16. 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
  • 17. Using functions a  =  1   b  =  2               1 1 2
  • 18. Using functions a  =  1   b  =  2               1 1 2 2 1 swap(a,b)  =  (b,a)
  • 19. The Key Idea Assignments  change  state  and   introduce  time.   Statements  do  side  effects.   Let’s  just  remove  them!
  • 22. You would not believe just how different it is!
  • 24. Define a Function frege>    times  a  b  =  a  *  b   frege>    times  3  4   12   frege>  :type  times   Num  α  =>  α  -­‐>  α  -­‐>  α
  • 25. 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
  • 26. Reference a Function frege>  twotimes  =  times  2   frege>  twotimes  3   6     frege>  :t  twotimes   Int  -­‐>  Int
  • 27. 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
  • 28. Function Composition frege>  twotimes  (threetimes  2)   12   frege>  sixtimes  =  twotimes  .  threetimes   frege>  sixtimes  2   frege>  :t  sixtimes   Int  -­‐>  Int
  • 29. 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)
  • 30. Pattern Matching frege>  times  0  (threetimes  2)   0   frege>  times  0  b  =  0
  • 31. Pattern Matching frege>  times  0  (threetimes  2)   0   frege>  times  0  b  =  0 unnecessarily evaluated shortcuttingpattern matching
  • 32. Lazy Evaluation frege>  times  0  (length  [1..])   0 endless sequence evaluation would never stop Pattern matching and non-strict evaluation to the rescue!
  • 33. Pure Functions Java   T  foo(Pair<T,U>  p)  {…}   Frege   foo  ::  (α,β)  -­‐>  α What could possibly happen? What could possibly happen?
  • 34. Pure Functions Java   T  foo(Pair<T,U>  p)  {…}   Frege   foo  ::  (α,β)  -­‐>  α Everything!
 State changes, 
 file or db access, missile launch,… a is returned
  • 35. Pure Functions 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
  • 36. Java Interoperability do not mix OO and FP combine them
  • 37. Java -> Frege Scripting:  JSR  223   Service:
       compile  *.fr  file
       call  static  method simple
  • 38. 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
  • 39. Frege allows  calling  Java
      but  never  unprotected!   is  explicit  about  effects
      just  like  Haskell  
  • 40. Type System Ubiquitous static typing allows
 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…
  • 41. Mutable
 I/O Mutable Mutable Keep the mess out! Pure Computation Pure Computation Pure Computation
  • 42. 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
  • 44. Zipping addzip  []  _    =  []
 addzip  _    []  =  []     addzip  (x:xs)  (y:ys)  =  
              (x  +  y  :  addzip  xs  ys  )
  • 45. 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…
  • 46. High Order Functions zipWith  f  []  _    =  []
 zipWith  f  _    []  =  []     zipWith  f  (x:xs)  (y:ys)  =  
              (f  x  y  :  zipWith  xs  ys  )  
  • 47. 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
  • 49. 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);
 }  }  }  }
  • 50. Fizzbuzz Logical fizzes      =  cycle      ["",  "",  "fizz"]
 buzzes      =  cycle      ["",  "",  "",  "",  "buzz"]
 pattern    =  zipWith  (++)  fizzes  buzzes   fizzbuzz  =  zipWith  (max  .  show)  pattern  [1..]     main  _      =  do
                      for  (take  100  fizzbuzz)  println
  • 51. Fizzbuzz Comparison Imperative Logical Conditionals 4 0 Operators 7 1 Nesting level 3 0 Sequencing sensitive transparent Maintainability - - - +
  • 52. History Java promise: „No more pointers!“ But NullPointerExceptions (?)
  • 53. 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
  • 55. Frege in comparison safe 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
  • 56. 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
  • 58. 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