Download Purely functional programming for the JVM Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Frege
Purely functional programming
for the JVM
Language Day, HdM Stuttgart 2015
Dierk König
Canoo
mittie
Why do we care?
a=1
1
b=2
1
2
time1
c=b
1
2
time2
b=a
1
time3
a=c
state1
2
1
2
state2
state3
Operational Reasoning
a=1
1
b=2
1
2
time1
c=b
1
2
time2
b=a
1
1
2
We need a debugger!
a=c
2
1
2
time3
state1
state2
2
state3
Using functions
a=1
1
b=2
1
2
Using functions
a=1
1
b=2
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>timesab=a*b
frege>times34
12
frege>:typetimes
Numα=>α->α->α
Define a Function
frege>timesab=a*b
no types declared
frege>(times3)4
12
function appl.
left associative
no comma
frege>:typetimes
Numα=>α->(α->α)
typeclass
only 1
return type is
parameter!
a function!
tell the inferred
type
thumb: „two params
of same numeric type
returning that type“
Reference a Function
frege>twotimes=times2
frege>twotimes3
6
frege>:ttwotimes
Int->Int
Reference a Function
frege>twotimes=times2
frege>twotimes3
No second
argument!
„Currying“, „schönfinkeling“,
or „partial function
6
frege>:ttwotimes
Int->Int
application“.
Concept invented by
Gottlob Frege.
inferred types
are more specific
Function Composition
frege>twotimes(threetimes2)
12
frege>sixtimes=twotimes.threetimes
frege>sixtimes2
frege>:tsixtimes
Int->Int
Function Composition
frege>twotimes(threetimes2)
12
f(g(x))
more about this later
frege>sixtimes=twotimes.threetimes
frege>sixtimes2
frege>:tsixtimes
Int->Int
(f
°
g) (x)
Pattern Matching
frege>times0(threetimes2)
unnecessarily evaluated
0
frege>times0b=0
pattern matching
shortcutting
Pure Functions
Java
Tfoo(Pair<T,U>p){…}
What could
possibly happen?
Frege
foo::(α,β)->α
What could
possibly happen?
Pure Functions
Java
Tfoo(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
Is my method pure?
Let the type system find out!
Java Interoperability
do not mix OO and FP
combine them
Java -> Frege
Scripting: JSR 223
Service:
compile *.fr file
call static method
simple
Frege -> Java
pure native encode java.net.URLEncoder.encode :: String -> String
encode “Dierk König“
even Java can be pure
native millis java.lang.System.currentTimeMillis :: () -> IO Long
millis ()
millis ()
past = millis () - 1000 Does not compile!
This is a key distinction between Frege and
other JVM languages!
Frege
allows calling Java
but never unprotected!
is explicit about effects
just like Haskell
Type System
Global 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…
Keep the mess out!
Mutable
Pure Computation
Mutable
I/O
Pure Computation
Mutable
Pure Computation
Keep the mess out!
Mutable
Pure Computation
Mutable
I/O
Threadsafe by
Pure Computation
design!
Checked
by
Mutable
Pure Computation
compiler
Ok, these are Monads. Be brave. Think of them as contexts
that the type system propagates and makes un-escapable.
Fizzbuzz
http://c2.com/cgi/wiki?FizzBuzzTest
https://dierk.gitbooks.io/fregegoodness/
chapter 8 „FizzBuzz“
Fizzbuzz Imperative
publicclassFizzBuzz{
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);
}}}}
Fizzbuzz Logical
fizzes=cycle["","","fizz"]
buzzes=cycle["","","","","buzz"]
pattern=zipWith(++)fizzesbuzzes
numbers=mapshow[1..]
fizzbuzz=zipWithmaxpatternnumbers
main_=do
for(take100fizzbuzz)println
Fizzbuzz Comparison
Imperative
Logical
Conditionals
4
0
Operators
7
1
Nesting level
3
0
Sequencing
sensitive
transparent
---
+
Maintainability
Frege in comparison
practical
Groovy
Java
Frege
Haskell
robust
Frege in comparison
practical
run computers
Frege makes the Haskell spirit
Groovy
Java
accessible to the Java programmer
and provides a new level of safety.
Frege
Haskell
concept by Simon Peyton-Jones
robust
apply logic
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 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!)
Dierk König
Canoo
mittie
Related documents