![Page 1: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/1.jpg)
FregePurely Functional Programming
on the JVM
![Page 2: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/2.jpg)
Dierk König Canoo
mittie
![Page 3: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/3.jpg)
![Page 4: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/4.jpg)
differentFrege is
![Page 5: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/5.jpg)
differentFrege is very
![Page 6: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/6.jpg)
You would not believe just how different it is!
![Page 7: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/7.jpg)
Online REPL http://try.frege-lang.org
![Page 8: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/8.jpg)
Define a Functionfrege> times a b = a * b
frege> times 3 4
12
frege> :type times
Num α => α -‐> α -‐> α
![Page 9: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/9.jpg)
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 10: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/10.jpg)
Reference a Functionfrege> twotimes = times 2
frege> twotimes 3
6
frege> :t twotimes
Int -‐> Int
![Page 11: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/11.jpg)
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 12: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/12.jpg)
Function Compositionfrege> twotimes (threetimes 2)
12
frege> sixtimes = twotimes . threetimes
frege> sixtimes 2
frege> :t sixtimes
Int -‐> Int
![Page 13: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/13.jpg)
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 14: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/14.jpg)
Pattern Matchingfrege> times 0 (threetimes 2)
0
frege> times 0 b = 0
![Page 15: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/15.jpg)
Pattern Matchingfrege> times 0 (threetimes 2)
0
frege> times 0 b = 0
unnecessarily evaluated
shortcuttingpattern matching
![Page 16: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/16.jpg)
Lazy Evaluationfrege> times 0 (length [1..])
0endless sequence
evaluation would never stop
Pattern matching and non-strict evaluation
to the rescue!
![Page 17: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/17.jpg)
Pure FunctionsJava
T foo(Pair<T,U> p) {…}
Frege
foo :: (α,β) -‐> α
What could possibly happen?
What could possibly happen?
![Page 18: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/18.jpg)
Pure FunctionsJava
T foo(Pair<T,U> p) {…}
Frege
foo :: (α,β) -‐> α
Everything! NPEs, state
changes, endless loops, missile
launch,…
a is returned or
system error
![Page 19: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/19.jpg)
Java Interoperability
![Page 20: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/20.jpg)
Java -> FregeScripting: JSR 223
Service: compile *.fr file put on javac classpath call static method
simple
![Page 21: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/21.jpg)
Frege -> Java!
!data Date = native java.util.Date where native new :: () -> IO (MutableIO Date) -- new Date() native toString :: Mutable s Date -> ST s String -- d.toString()
This is a key distinction between Frege and previous efforts to port Haskell to the JVM.
Declaring the messiness.
![Page 22: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/22.jpg)
Some Cool Stuff
![Page 23: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/23.jpg)
Zippingaddzip [] _ = []addzip _ [] = []
addzip (x:xs) (y:ys) = (x + y : addzip xs ys )
![Page 24: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/24.jpg)
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 25: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/25.jpg)
High Order FunctionszipWith f [] _ = []zipWith f _ [] = []
zipWith f (x:xs) (y:ys) = (f x y : zipWith xs ys )
!
!
![Page 26: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/26.jpg)
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 27: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/27.jpg)
Fibonacci!
fib = 0: 1: addzip fib (tail fib)
! use as take 60 fib
a new solution approach
fib 0:1 … tail 1 … zip 1 …
![Page 28: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/28.jpg)
Fibonacci!
fib = 0: 1: addzip fib (tail fib)
! use as take 60 fib
a new solution approach
fib 0 1:1 … tail 1 … zip 2 …
![Page 29: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/29.jpg)
Fibonacci!
fib = 0: 1: addzip fib (tail fib)
! use as take 60 fib
a new solution approach
fib 0 1 1:2 … tail 2 … zip 3 …
![Page 30: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/30.jpg)
Fibonacci!
fib = 0: 1: addzip fib (tail fib)
! use as take 60 fib
a new solution approach
fib 0 1 1 2:3 … tail 3 … zip 5 …
![Page 31: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/31.jpg)
List ComprehensionPythagorean triples: a2 + b2 = c2!
[ (m*m-‐n*n, 2*m*n, m*m+n*n)
| m <-‐ [2..], n <-‐ [1..m-‐1]
]
!
![Page 32: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/32.jpg)
List ComprehensionPythagorean triples: a2 + b2 = c2!
[ (m*m-‐n*n, 2*m*n, m*m+n*n)
| m <-‐ [2..], n <-‐ [1..m-‐1]
]
!endless production
triples
think „nested loop“
![Page 33: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/33.jpg)
QuickCheck-‐-‐ An AVL tree is balanced so that the height of the left and right subtree differ by at most 1
p_balance = forAll aTree (\tree -‐> abs tree.balance < 2)
! QuickCheck will create 500 different trees covering all corner cases in creation and
validate the invariant. (from Frege source code)
![Page 34: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/34.jpg)
Type SystemHindley-Milner more info for the programmer less work for the programmer more useful programs compile less bad programs compile
http://perl.plover.com/yak/typing/Endless recursion in merge sort detected by the type system.
![Page 35: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/35.jpg)
Mutable I/O
Mutable
Mutable
Keep the mess out!
Clean Computation
Clean Computation
Clean Computation
![Page 36: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/36.jpg)
Mutable I/O
Mutable
Mutable
Keep the mess out!
Clean Computation
Clean Computation
Clean Computation
Ok, these are Monads. Be brave. Think of them as contexts that the type system propagates and makes un-escapable.
![Page 37: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/37.jpg)
HistoryJava promise: „No more pointers!“!
But NullPointerExceptions (?)
![Page 38: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/38.jpg)
Frege is differentNo More But
state no state (unless declared)
statements expressions (+ „do“ notation)
interfaces type classes
classes & objects algebraic data types
inheritance polymorphism
null references Maybe
and the list goes on…
![Page 39: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/39.jpg)
Frege in comparison
useful
safe
JavaGroovy
FregeHaskell
![Page 40: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/40.jpg)
Frege in comparison
useful
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
![Page 41: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/41.jpg)
Why FP mattersThe intellectual challenge The aesthetical enjoymentType system and modularity benefits!
It may even be useful!
„An investment in knowledge always pays the best interest.“
—Benjamin Franklin
![Page 42: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/42.jpg)
How?http://www.frege-‐lang.org
stackoverflow „frege“ tag
https://github.com/Dierk/Real_World_Frege
Join the effort!
![Page 43: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/43.jpg)
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 44: Frege - purely functional programming on the JVM](https://reader038.vdocument.in/reader038/viewer/2022102823/547ce01d5806b5d13f8b47d4/html5/thumbnails/44.jpg)
Dierk König Canoo
Please fill th
e feedback
forms
mittie