Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t...

37
Frege purely functional programming on the JVM GOTO Berlin 2015

Transcript of Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t...

Page 1: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Fregepurely functional programming

on the JVM

GOTO Berlin 2015

Page 2: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Dierk König canoo

mittie

Page 3: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Dreaming of code

Page 4: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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

time1time2time3

place1 place2 place3

Page 5: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Operational Reasoninga=1

b=2

c=b

b=a

a=c

1

1 2

1 2 2

1 1 2

2 1 2

time1time2time3

place1 place2 place3

We need a debugger!

Page 6: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Using functionsa=1

b=2

1

1 2

Page 7: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Using functionsa=1

b=2

1

1 2

2 1

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

Page 8: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Let’s just program without

assignments or statements!

Page 9: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Developer Discipline

Pure Functional Language

Page 10: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Online REPL try.frege-lang.org

Page 11: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Define a Functionfrege>timesab=a*b

frege>times23

6

frege>:typetimes

Numα=>α->α->α

Page 12: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Define a Functionfrege>timesab=a*b

frege>(times2)3

6

frege>:typetimes

Numα=>α->(α->α)

no types declared

function appl. left associative

typeclass constraint

only 1 parameter!

return type is a function!

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

no comma

Page 13: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Reference a Functionfrege>twotimes=times2

frege>twotimes3

6

frege>:ttwotimes

Int->Int

Page 14: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Reference a Functionfrege>twotimesx=times2x

frege>twotimes3

6

frege>:ttwotimes

Int->Int

No second arg!

„Currying“, „schönfinkeling“, or „partial function

application“. Concept invented by

Gottlob Frege.

inferred types are more specific

Page 15: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Function Compositionfrege>twotimes(threetimes2)

12

frege>sixtimes=twotimes.threetimes

frege>sixtimes2

frege>:tsixtimes

Int->Int

Page 16: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Function Compositionfrege>twotimes(threetimes2)

12

frege>sixtimes=twotimes.threetimes

frege>sixtimes2

frege>:tsixtimes

Int->Int

f(g(x))

(f ° g) x

Page 17: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Pure FunctionsJava

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

Frege

foo::(α,β)->α

What could possibly happen?

What could possibly happen?

Page 18: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Pure FunctionsJava

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

Frege

foo::(α,β)->α

Everything! State changes,

file or db access, missile launch,…

a is returned

Page 19: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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

Page 20: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Is my method pure?

Let the type system find out!

Page 21: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Java Interoperability

Do not mix OO and FP,

combine them!

Page 22: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Java -> FregeFrege compiles Haskell to Java source and byte code.

Just call that.

You can get help by using the :java command in the REPL.

Page 23: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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 24: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

allows calling Java but never unprotected!

is explicit about effects just like Haskell

Frege

Page 25: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Type SystemGlobal type inference

More safety and less work for the programmer

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

Page 26: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Mutable I/O

Mutable

Mutable

Keep the mess out!

Pure Computation

Pure Computation

Pure Computation

Page 27: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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 28: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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

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

Page 29: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Fizzbuzz ImperativepublicclassFizzBuzz{publicstaticvoidmain(String[]args){for(inti=1;i<=100;i++){if(i%15==0{System.out.println(„FizzBuzz");}elseif(i%3==0){System.out.println("Fizz");}elseif(i%5==0){System.out.println("Buzz");}else{System.out.println(i);}}}}

Page 30: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Fizzbuzz Logicalfizzes=cycle["","","fizz"]buzzes=cycle["","","","","buzz"]pattern=zipWith(++)fizzesbuzzesnumbers=mapshow[1..]fizzbuzz=zipWithmaxpatternnumbers

main_=for(take100fizzbuzz)println

Page 31: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Fizzbuzz ComparisonImperative Logical

Conditionals 4 0

Operators 7 1

Nesting level 3 0

Sequencing sensitive transparent

Maintainability - - - +

Incremental development - +++

Page 32: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Unique in FregeGlobal type inference requires a purely functional language (only expressions and parametric polymorphism) Purity by default effects are explicit in the type systemLaziness by defaultValues are always immutable Guarantees extend into Java calls

Page 33: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

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

Enables local and equational reasoning

Best way to learn FP

Page 34: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Why Frege

it is just a pleasure to work with

Page 35: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

How?http://www.frege-lang.org@fregelangstackoverflow „frege“ tagedX FP101 MOOC

Page 36: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

Dierk König canoo

mittie

Please give feedback!

Page 37: Frege - GOTO Blog · 2015-12-04 · frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int No second arg! „Currying“, „schönfinkeling“, or „partial

FGALanguage level is Haskell Report 2010.Yes, performance is roughly ~ Java.Yes, the compiler is reasonably fast.Yes, we have an Eclipse Plugin.Yes, Maven/Gradle/etc. integration.Yes, we have HAMT (aka HashMap). Yes, we have QuickCheck (+shrinking) No, but STM is in the works.