Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk König

159 views

Published on

FREGE - INTRODUCING PURELY FUNCTIONAL PROGRAMMING ON THE JVM

Frege is a Haskell for the JVM. In Frege you program with pure functions, immutable values,
and isolated effects only. This talk gives you a first impression of what this paradigm means to the programmer and how it makes your code robust under composition, allows refactoring without fear, and becomes safe for parallel execution.
This introduction leads you through the benefits that make Frege unique between the JVM languages. It is followed up by the Frege tutorial that provides more detail and examples.

Published in: Software
  • Be the first to comment

  • Be the first to like this

JDD2015: Frege - Introducing purely functional programming on the JVM - Dierk König

  1. 1. Frege Introduction to purely functional programming on the JVM
  2. 2. Dierk König Canoo mittie
  3. 3. Takeaway Frege is fundamentally different based on science and practical
  4. 4. 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 time1 time2 time3 state1 state2 state3
  5. 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 time1 time2 time3 state1 state2 state3
  6. 6. Using functions a = 1 b = 2 1 1 2
  7. 7. Using functions a = 1 b = 2 1 1 2 2 1 swap(a,b) = (b,a)
  8. 8. The Key Idea Assignments change state and introduce time. Statements do side effects. Let’s just remove them!
  9. 9. Pure Functions Java T foo(Pair<T,U> p) {…} Frege foo :: (α,β) -> α What could possibly happen? What could possibly happen?
  10. 10. Pure Functions Java T foo(Pair<T,U> p) {…} Frege foo :: (α,β) -> α Everything!
 State changes, 
 file or db access, missile launch,… a is returned
  11. 11. 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
  12. 12. Is my method pure? Let the type system find out!
  13. 13. Java Interoperability do not mix OO and FP combine them
  14. 14. Java -> Frege Scripting: JSR 223 Service:
 compile *.fr file
 call static method simple
  15. 15. 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
  16. 16. allows calling Java
 but never unprotected! is explicit about effects
 just like Haskell Frege
  17. 17. Type System Global 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…
  18. 18. Mutable
 I/O Mutable Mutable Keep the mess out! Pure Computation Pure Computation Pure Computation
  19. 19. 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
  20. 20. Java Java Java Service Based Design Frege Frege Frege A typical integration option: use Frege code for services
  21. 21. Fizzbuzz http://c2.com/cgi/wiki?FizzBuzzTest https://dierk.gitbooks.io/fregegoodness/
 chapter 8 „FizzBuzz“
  22. 22. 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);
 } } } }
  23. 23. Fizzbuzz Logical fizzes = cycle ["", "", "fizz"]
 buzzes = cycle ["", "", "", "", "buzz"]
 pattern = zipWith (++) fizzes buzzes fizzbuzz = zipWith (max . show) [1..] pattern main _ = do
 for (take 100 fizzbuzz) println
  24. 24. Fizzbuzz Comparison Imperative Logical Conditionals 4 0 Operators 7 1 Nesting level 3 0 Sequencing sensitive transparent Maintainability - - - +
  25. 25. History Java promise: „No more pointers!“ But NullPointerExceptions (?)
  26. 26. Frege is different No More But state no state (unless declared) statements expressions (+ „do“ notation) assignments definitions variables ST monad as „agent“ interfaces type classes classes & objects algebraic data types inheritance parametric polymorphism null references Maybe NullPointerExceptions Bottom, error
  27. 27. Frege in comparison practical safe Java Groovy Frege Haskell
  28. 28. Frege in comparison safe Java Groovy Frege Haskell 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
  29. 29. 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
  30. 30. How? http://www.frege-lang.org
 @fregelang
 stackoverflow „frege“ tag
 edX FP101 MOOC (start Oct 15th!)
  31. 31. 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
  32. 32. Dierk König Canoo mittie Join the tutorial tomorrow

×