Frege - consequently functional programming for the JVM

60
Frege konsequent funktionale Programmierung für die JVM

Transcript of Frege - consequently functional programming for the JVM

Page 1: Frege - consequently functional programming for the JVM

Fregekonsequent funktionale Programmierung

für die JVM

Page 2: Frege - consequently functional programming for the JVM

Dierk König Canoo

mittie

Page 3: Frege - consequently functional programming for the JVM

Dierk König Canoo

mittie

Page 4: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

Page 5: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

Page 6: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

Page 7: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2

2

Page 8: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2 2

Page 9: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2 2

1

1

2

Page 10: Frege - consequently functional programming for the JVM

Why do we care?a  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2 2

1 1 2

Page 11: Frege - consequently functional programming for the JVM

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

Page 12: Frege - consequently functional programming for the JVM

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

Page 13: Frege - consequently functional programming for the JVM

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

Page 14: Frege - consequently functional programming for the JVM

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

time1time2time3

state1 state2 state3

Page 15: Frege - consequently functional programming for the JVM

Operational Reasoninga  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2 2

1 1 2

2 1 2

time1time2time3

state1 state2 state3

Page 16: Frege - consequently functional programming for the JVM

Operational Reasoninga  =  1  

b  =  2  

c  =  b  

b  =  a  

a  =  c

1

1 2

1 2 2

1 1 2

2 1 2

time1time2time3

state1 state2 state3

Page 17: Frege - consequently functional programming for the JVM

Using functionsa  =  1  

b  =  2  

           

1

1 2

Page 18: Frege - consequently functional programming for the JVM

Using functionsa  =  1  

b  =  2  

           

1

1 2

2 1

swap(a,b)  =  (b,a)

Page 19: Frege - consequently functional programming for the JVM

The Key IdeaAssignments  change  state  and  introduce  time.  

Statements  do  side  effects.  

Let’s  just  remove  them!

Page 20: Frege - consequently functional programming for the JVM

differentFrege is

Page 21: Frege - consequently functional programming for the JVM

differentFrege is very

Page 22: Frege - consequently functional programming for the JVM

You would not believe just how different it is!

Page 23: Frege - consequently functional programming for the JVM

Online REPL http://try.frege-lang.org

Page 24: Frege - consequently functional programming for the JVM

Define a Functionfrege>    times  a  b  =  a  *  b  

frege>    times  3  4  

12  

frege>  :type  times  

Num  α  =>  α  -­‐>  α  -­‐>  α

Page 25: Frege - consequently functional programming for the JVM

Define a Functionfrege>    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

typeclassonly 1

parameter!return type is a function!

thumb: „two params of same numeric type returning that type“

no comma

Page 26: Frege - consequently functional programming for the JVM

Reference a Functionfrege>  twotimes  =  times  2  

frege>  twotimes  3  

6    

frege>  :t  twotimes  

Int  -­‐>  Int

Page 27: Frege - consequently functional programming for the JVM

Reference a Functionfrege>  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

Page 28: Frege - consequently functional programming for the JVM

Function Compositionfrege>  twotimes  (threetimes  2)  

12  

frege>  sixtimes  =  twotimes  .  threetimes  

frege>  sixtimes  2  

frege>  :t  sixtimes  

Int  -­‐>  Int

Page 29: Frege - consequently functional programming for the JVM

Function Compositionfrege>  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)

Page 30: Frege - consequently functional programming for the JVM

Pattern Matchingfrege>  times  0  (threetimes  2)  

0  

frege>  times  0  b  =  0

Page 31: Frege - consequently functional programming for the JVM

Pattern Matchingfrege>  times  0  (threetimes  2)  

0  

frege>  times  0  b  =  0

unnecessarily evaluated

shortcuttingpattern matching

Page 32: Frege - consequently functional programming for the JVM

Lazy Evaluationfrege>  times  0  (length  [1..])  

0endless sequence

evaluation would never stop

Pattern matching and non-strict evaluation

to the rescue!

Page 33: Frege - consequently functional programming for the JVM

Pure FunctionsJava  

T  foo(Pair<T,U>  p)  {…}  

Frege  

foo  ::  (α,β)  -­‐>  α

What could possibly happen?

What could possibly happen?

Page 34: Frege - consequently functional programming for the JVM

Pure FunctionsJava  

T  foo(Pair<T,U>  p)  {…}  

Frege  

foo  ::  (α,β)  -­‐>  α

Everything! State changes,

file or db access, missile launch,…

a is returned

Page 35: Frege - consequently functional programming for the JVM

Pure Functionscan  be  cached  (memoized)can  be  evaluated  lazilycan  be  evaluated  in  advancecan  be  evaluated  concurrentlycan  be  eliminated          in  common  subexpressions  

can  be  optimized

Page 36: Frege - consequently functional programming for the JVM

Java Interoperability do not mix OO and FP

combine them

Page 37: Frege - consequently functional programming for the JVM

Java -> FregeScripting:  JSR  223  

Service:       compile  *.fr  file       call  static  method

simple

Page 38: Frege - consequently functional programming for the JVM

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

Page 39: Frege - consequently functional programming for the JVM

Fregeallows  calling  Java      but  never  unprotected!  

is  explicit  about  effects      just  like  Haskell  

Page 40: Frege - consequently functional programming for the JVM

Type SystemUbiquitous static typing allowsglobal 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…

Page 41: Frege - consequently functional programming for the JVM

Mutable I/O

Mutable

Mutable

Keep the mess out!

Pure Computation

Pure Computation

Pure Computation

Page 42: Frege - consequently functional programming for the JVM

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

Page 43: Frege - consequently functional programming for the JVM

Some Cool Stuff

Page 44: Frege - consequently functional programming for the JVM

Zippingaddzip  []  _    =  []addzip  _    []  =  []    

addzip  (x:xs)  (y:ys)  =                (x  +  y  :  addzip  xs  ys  )

Page 45: Frege - consequently functional programming for the JVM

Zippingaddzip  []  _    =  []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…

Page 46: Frege - consequently functional programming for the JVM

High Order FunctionszipWith  f  []  _    =  []zipWith  f  _    []  =  []    

zipWith  f  (x:xs)  (y:ys)  =                (f  x  y  :  zipWith  xs  ys  )  

Page 47: Frege - consequently functional programming for the JVM

High Order FunctionszipWith  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

Page 48: Frege - consequently functional programming for the JVM

Fizzbuzzhttp://c2.com/cgi/wiki?FizzBuzzTest

https://dierk.gitbooks.io/fregegoodness/ chapter 8 „FizzBuzz“

Page 49: Frege - consequently functional programming for the JVM

Fizzbuzz Imperativepublic  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);}  }  }  }

Page 50: Frege - consequently functional programming for the JVM

Fizzbuzz Logicalfizzes      =  cycle      ["",  "",  "fizz"]buzzes      =  cycle      ["",  "",  "",  "",  "buzz"]pattern    =  zipWith  (++)  fizzes  buzzes  

fizzbuzz  =  zipWith  (max  .  show)  pattern  [1..]    

main  _      =  do                      for  (take  100  fizzbuzz)  println

Page 51: Frege - consequently functional programming for the JVM

Fizzbuzz ComparisonImperative Logical

Conditionals 4 0

Operators 7 1

Nesting level 3 0

Sequencing sensitive transparent

Maintainability - - - +

Page 52: Frege - consequently functional programming for the JVM

HistoryJava promise: „No more pointers!“

But NullPointerExceptions (?)

Page 53: Frege - consequently functional programming for the JVM

Frege is differentNo More But

state no state (unless declared)statements expressions (+ „do“ notation)assignments definitionsvariables ST monad as „agent“interfaces type classesclasses & objects algebraic data typesinheritance parametric polymorphismnull references MaybeNullPointerExceptions Bottom, error

Page 54: Frege - consequently functional programming for the JVM

Frege in comparison

practical

safe

JavaGroovy

FregeHaskell

Page 55: Frege - consequently functional programming for the JVM

Frege in comparison

safe

JavaGroovy

FregeHaskell

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

Page 56: Frege - consequently functional programming for the JVM

Why FregeRobustness under parallel execution Robustness under composition Robustness under incrementsRobustness under refactoring

Enables local and equational reasoning

Best way to learn FP

Page 57: Frege - consequently functional programming for the JVM

How?http://www.frege-­‐lang.org@fregelangstackoverflow  „frege“  tagedX  FP101  MOOC  (start  Oct  15th!)  

Page 58: Frege - consequently functional programming for the JVM

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

Page 59: Frege - consequently functional programming for the JVM

Dierk König Canoo

mittie

Page 60: Frege - consequently functional programming for the JVM

Dierk König Canoo

mittie

Please fill the feedback forms