frege - consequently functional programming for the jvm

Post on 22-Jan-2018

1.224 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Fregekonsequent 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

time1time2time3

state1 state2 state3

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

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

Using functionsa  =  1  

b  =  2  

           

1

1 2

Using functionsa  =  1  

b  =  2  

           

1

1 2

2 1

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

The Key IdeaAssignments  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 Functionfrege>    times  a  b  =  a  *  b  

frege>    times  3  4  

12  

frege>  :type  times  

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

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

Reference a Functionfrege>  twotimes  =  times  2  

frege>  twotimes  3  

6    

frege>  :t  twotimes  

Int  -­‐>  Int

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

Function Compositionfrege>  twotimes  (threetimes  2)  

12  

frege>  sixtimes  =  twotimes  .  threetimes  

frege>  sixtimes  2  

frege>  :t  sixtimes  

Int  -­‐>  Int

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)

Pattern Matchingfrege>  times  0  (threetimes  2)  

0  

frege>  times  0  b  =  0

Pattern Matchingfrege>  times  0  (threetimes  2)  

0  

frege>  times  0  b  =  0

unnecessarily evaluated

shortcuttingpattern matching

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

0endless sequence

evaluation would never stop

Pattern matching and non-strict evaluation

to the rescue!

Pure FunctionsJava  

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

Frege  

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

What could possibly happen?

What could possibly happen?

Pure FunctionsJava  

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

Frege  

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

Everything! State changes,

file or db access, missile launch,…

a is returned

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

can  be  optimized

Java Interoperability do not mix OO and FP

combine them

Java -> FregeScripting:  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

Fregeallows  calling  Java      but  never  unprotected!  

is  explicit  about  effects      just  like  Haskell  

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…

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

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

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

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…

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

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

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

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

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

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

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

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

main  _      =  do                      for  (take  100  fizzbuzz)  println

Fizzbuzz ComparisonImperative Logical

Conditionals 4 0

Operators 7 1

Nesting level 3 0

Sequencing sensitive transparent

Maintainability - - - +

HistoryJava promise: „No more pointers!“

But NullPointerExceptions (?)

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

Frege in comparison

practical

safe

JavaGroovy

FregeHaskell

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

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

Enables local and equational reasoning

Best way to learn FP

How?http://www.frege-­‐lang.org@fregelangstackoverflow  „frege“  tagedX  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

top related