Frege purely functional programming on the JVM JUG Luzern 2016 - - PowerPoint PPT Presentation

frege
SMART_READER_LITE
LIVE PREVIEW

Frege purely functional programming on the JVM JUG Luzern 2016 - - PowerPoint PPT Presentation

Frege purely functional programming on the JVM JUG Luzern 2016 Dierk Knig canoo mittie Dreaming of code Why do we care? a = 1 1 1 2 2 b = 2 tj me 1 c = b 1 1 2 tj me 2 b = a 1 2 2 tj me 3 a = c 1 2 place 1 place 2


slide-1
SLIDE 1

Frege

purely functional programming


  • n the JVM


JUG Luzern 2016

slide-2
SLIDE 2

Dierk König canoo mittie

slide-3
SLIDE 3

Dreaming of code

slide-4
SLIDE 4

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 tjme1 tjme2 tjme3 place1 place2 place3

slide-5
SLIDE 5

Operational Reasoning

a = 1 b = 2 c = b b = a a = c 1 1 2 1 2 2 1 1 2 2 1 2 tjme1 tjme2 tjme3 place1 place2 place3 We need a debugger!

slide-6
SLIDE 6

Using functions

a = 1 b = 2 1 1 2

slide-7
SLIDE 7

Using functions

a = 1 b = 2 1 1 2 2 1 swap(a,b) = (b,a)

slide-8
SLIDE 8

Let’s just program without assignments or statements!

slide-9
SLIDE 9

Developer
 Discipline Pure 
 Functional
 Language

slide-10
SLIDE 10

Online REPL try.frege-lang.org

slide-11
SLIDE 11

Define a Function

frege> times a b = a * b frege> times 2 3 6 frege> :type times Num α => α -> α -> α

slide-12
SLIDE 12

Define a Function

frege> times a b = a * b frege>(times 2)3 6 frege> :type times Num α => α ->(α -> α)

no types declared function appl. left associative typeclass constraint

  • nly 1

parameter! return type is a function! thumb: „two params

  • f same numeric type

returning that type“ no comma

slide-13
SLIDE 13

Reference a Function

frege> twotimes = times 2 frege> twotimes 3 6 frege> :t twotimes Int -> Int

slide-14
SLIDE 14

Reference a Function

frege> twotimes x = times 2 x frege> twotimes 3 6 frege> :t twotimes Int -> Int

No second arg! „Currying“, „schönfinkeling“,

  • r „partial function

application“. Concept invented by Gottlob Frege. inferred types are more specific

slide-15
SLIDE 15

Function Composition

frege> six x = twotimes (threetimes x) frege> six x = (twotimes . threetimes)x frege> six = twotimes . threetimes frege> six 2 12

slide-16
SLIDE 16

Function Composition

frege> six x = twotimes (threetimes x) frege> six x = (twotimes . threetimes)x frege> six = twotimes . threetimes frege> six 2 12

f(g(x)) (f ° g) x f ° g

slide-17
SLIDE 17

Pure Functions

Java T foo(Pair<T,U> p) {…} Frege foo :: (α,β) -> α

What could possibly happen? What could possibly happen?

slide-18
SLIDE 18

Pure Functions

Java T foo(Pair<T,U> p) {…} Frege foo :: (α,β) -> α

Everything!
 State changes, 
 file or db access, missile launch,… a is returned

slide-19
SLIDE 19

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

slide-20
SLIDE 20

Is my method pure?

Let the type system find out!

slide-21
SLIDE 21

Java Interoperability Do not mix 
 OO and FP, combine them!

slide-22
SLIDE 22

Java -> Frege

Frege compiles Haskell to 
 Java source and byte code. Just call that. You can get help by using the :java command in the REPL.

slide-23
SLIDE 23

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


  • ther JVM languages!

even Java can be pure

slide-24
SLIDE 24

allows calling Java
 but never unprotected! is explicit about efgects
 just like Haskell

Frege

Prerequisite to safe concurrency and
 deterministic parallelism!

slide-25
SLIDE 25

Mutable
 I/O

Mutable Mutable

Keep the mess out!

Pure Computation Pure Computation Pure Computation

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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 for clarity.

slide-28
SLIDE 28

Pure Transactions

slide-29
SLIDE 29

Type inference FTW

slide-30
SLIDE 30

Fizzbuzz

http://c2.com/cgi/wiki?FizzBuzzTest https://dierk.gitbooks.io/fregegoodness/
 chapter 8 „FizzBuzz“

slide-31
SLIDE 31

Fizzbuzz Imperative

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

slide-32
SLIDE 32

Fizzbuzz Logical

fizzes = cycle ["", "", "fizz"]
 buzzes = cycle ["", "", "", "", "buzz"]
 pattern = zipWith (++) fizzes buzzes
 numbers = map show [1..]
 fizzbuzz = zipWith max pattern numbers main _ = for (take 100 fizzbuzz) println

slide-33
SLIDE 33

Fizzbuzz Comparison

Imperative Logical Conditionals 4 Operators 7 1 Nesting level 3 Sequencing sensitive transparent Maintainability

  • - -

+ Incremental development

  • +++
slide-34
SLIDE 34

Unique in Frege

Global type inference (requires purity)
 Purity by default
 effects are explicit in the type system
 Type-safe concurrency & parallelism
 Laziness by default
 Values are always immutable
 Guarantees extend into Java calls

slide-35
SLIDE 35

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

slide-36
SLIDE 36

Why FP matters

Enabling incremental development
 www.canoo.com/blog/fp1
 
 Brush up computational fundamentals

„An investment in knowledge 
 always pays the best interest.“ —Benjamin Franklin

slide-37
SLIDE 37

Why Frege

it is just a pleasure to work with

slide-38
SLIDE 38

How?

http://www.frege-lang.org
 @fregelang
 stackoverflow „frege“ tag
 edX FP101 MOOC

slide-39
SLIDE 39

Dierk König canoo mittie

Please give feedback!

slide-40
SLIDE 40

FGA

Language 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, STM is almost finished.

slide-41
SLIDE 41

Unique in Frege

Global type inference (requires purity)
 Purity by default
 effects are explicit in the type system
 Type-safe concurrency & parallelism
 Laziness by default
 Values are always immutable
 Guarantees extend into Java calls