frege - jugsaxony.org · this is a key distinction between frege and other jvm languages! even java...
TRANSCRIPT
![Page 1: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/1.jpg)
Fregepurely functional programming
on the JVM
JUG Saxony 2016
![Page 2: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/2.jpg)
Dierk König canoo
mittie
![Page 3: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/3.jpg)
Dreaming of code
![Page 4: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/4.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/5.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/6.jpg)
Using functionsa=1
b=2
1
1 2
![Page 7: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/7.jpg)
Using functionsa=1
b=2
1
1 2
2 1
swap(a,b)=(b,a)
![Page 8: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/8.jpg)
Let’s just program without
assignments or statements!
![Page 9: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/9.jpg)
Developer Discipline
Pure Functional Language
![Page 10: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/10.jpg)
Online REPL try.frege-lang.org
![Page 11: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/11.jpg)
Define a Functionfrege>timesab=a*b
frege>times23
6
frege>:typetimes
Numα=>α->α->α
![Page 12: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/12.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/13.jpg)
Reference a Functionfrege>twotimes=times2
frege>twotimes3
6
frege>:ttwotimes
Int->Int
![Page 14: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/14.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/15.jpg)
Function Compositionfrege>sixx=twotimes(threetimesx)
frege>sixx=(twotimes.threetimes)x
frege>six=twotimes.threetimes
frege>six2
12
![Page 16: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/16.jpg)
Function Compositionfrege>sixx=twotimes(threetimesx)
frege>sixx=(twotimes.threetimes)x
frege>six=twotimes.threetimes
frege>six2
12
f(g(x))
(f ° g) x
f ° g
![Page 17: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/17.jpg)
Pure FunctionsJava
Tfoo(Pair<T,U>p){…}
Frege
foo::(α,β)->α
What could possibly happen?
What could possibly happen?
![Page 18: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/18.jpg)
Pure FunctionsJava
Tfoo(Pair<T,U>p){…}
Frege
foo::(α,β)->α
Everything! State changes,
file or db access, missile launch,…
a is returned
![Page 19: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/19.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/20.jpg)
Is my method pure?
Let the type system find out!
![Page 21: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/21.jpg)
Java Interoperability
Do not mix OO and FP,
combine them!
![Page 22: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/22.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/23.jpg)
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 - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/24.jpg)
allows calling Java but never unprotected!
is explicit about effects just like Haskell
Frege
Prerequisite to safe concurrency and deterministic parallelism!
![Page 25: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/25.jpg)
Mutable I/O
Mutable
Mutable
Keep the mess out!
Pure Computation
Pure Computation
Pure Computation
![Page 26: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/26.jpg)
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 27: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/27.jpg)
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 for clarity.
![Page 28: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/28.jpg)
Pure Transactions
![Page 29: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/29.jpg)
Type inference FTW
![Page 30: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/30.jpg)
Fizzbuzzhttp://c2.com/cgi/wiki?FizzBuzzTest
https://dierk.gitbooks.io/fregegoodness/ chapter 8 „FizzBuzz“
![Page 31: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/31.jpg)
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 32: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/32.jpg)
Fizzbuzz Logicalfizzes=cycle["","","fizz"]buzzes=cycle["","","","","buzz"]pattern=zipWith(++)fizzesbuzzesnumbers=mapshow[1..]fizzbuzz=zipWithmaxpatternnumbers
main_=for(take100fizzbuzz)println
![Page 33: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/33.jpg)
Fizzbuzz ComparisonImperative Logical
Conditionals 4 0
Operators 7 1
Nesting level 3 0
Sequencing sensitive transparent
Maintainability - - - +
Incremental development - +++
![Page 34: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/34.jpg)
Unique in FregeGlobal type inference (requires purity)Purity by default effects are explicit in the type systemType-safe concurrency & parallelismLaziness by defaultValues are always immutable Guarantees extend into Java calls
![Page 35: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/35.jpg)
Why FregeRobustness under parallel execution Robustness under composition Robustness under incrementsRobustness under refactoring
Enables local and equational reasoning
Best way to learn FP
![Page 36: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/36.jpg)
Why FP mattersEnabling incremental development www.canoo.com/blog/fp1 Brush up computational fundamentals
„An investment in knowledge always pays the best interest.“
—Benjamin Franklin
![Page 37: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/37.jpg)
Why Frege
it is just a pleasure to work with
![Page 38: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/38.jpg)
How?http://www.frege-lang.org@fregelangstackoverflow „frege“ tagedX FP101 MOOC
![Page 39: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/39.jpg)
Dierk König canoo
mittie
Please give feedback!
![Page 40: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/40.jpg)
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) Yes, we have STM.
![Page 41: Frege - jugsaxony.org · This is a key distinction between Frege and other JVM languages! even Java can be pure. allows calling Java but never unprotected! is explicit about e"ects](https://reader034.vdocuments.us/reader034/viewer/2022050310/5f725bd9b7711e376d4c5982/html5/thumbnails/41.jpg)
Unique in FregeGlobal type inference (requires purity)Purity by default effects are explicit in the type systemType-safe concurrency & parallelismLaziness by defaultValues are always immutable Guarantees extend into Java calls