About Functional Programming(Robust Programming)AapoKyrölä, 23.3.2011Sulake
ContentsMotivation: some issues with OOP and JavaIntroduction to Functional ProgrammingFunctional Programming –style in Java
Motivation: Problems with Object-Oriented Programming and Java
Fundamental Design Problem of JavaNot well-typed => NullpointersProgrammer should handle two cases explicitly: userToBan is either null or not null.OR, compiler should not allow assigning NULL to a reference (like in C++  &obj).By proper typing, compiler would prevent null-pointers…
In Standard ML
OOP: criticismClass/Object structure cumbersome to change.a class hierarchy might be natural from some point of view, but for other (future) uses cumbersome.Problems with Inheritancemuch functionality hidden in superclasses.Hard to manage: if behavior of superclass is changed, how does it affect all subclasses?Especially in UI: subclass may depend on a not-intended behavior of a superclass.Delegation usually better than inheritance (Spring)better modularity with IOP  than with object hierarchies.API dependencies.Makes refactoring hard.
Object-relational mappingAlways hated…a) Intuitively, object should have an identityBut when you have many copies of the object…b) Queries with criteria-objects or example objects just stink.Select * from sale where date>? and date<? order by date limit 10;
ORM (continues)c) Even bigger problem with relationsIdentity of the objects in collection?Cumbersome to control when you want the collections loaded (all that hibernate session stuff).d) SQL is great, easy and powerful. Why switch away?  Declarative programming is powerful.
Side effectsWhen you call a method, it can change the state, even if it is named getX().At least previously in Habbo: effects on database object cache.Or : calling a collection method in hibernate object, user.getFriends().You need to know if users were loaded in order to reason what happens.
Who manages an object?Java does not have immutable objectsNeed to “wrap” them by hand (Collections.unmodifiableList()).Error only on run-time. How do you handle such error? Should be compile-time.Concurrency issuesWho manages multi-threaded access to an object? Read-only objects do not have this problem…Easy to get deadlocks.
Key to more robust code	Compiler and type-system should do more!Generics are even worse…End of rant.
Quick introduction to Functional Programming Languages
ContentsSome historyFunctional vs. imperative languagesDeclarative programming.Functions as values.High-order functions.Referential transparency.Power of the Type system in Standard ML.All examples in Standard ML.Next: how to program “functionally in Java”.
HistoryLambda calculus (30s-40s): mathematical theory for computing with functions (Church).LISP (50s): motivated by artificial intelligence. Introduced high level data structures such as lists and trees. Scheme, ClojureMeta-language: ML (70s): for writing proof checkers.Standard ML, OCaML, F#Haskell (80s-90s)
History (cont.)Functional programming has (so far) mostly been favored in the academiaMathematically consistent (no pointer-arithmetic).Clear semantics: define functions, not manipulate state.Focus on computation, not on memory addressing, low-level issues.Carnegie Mellon uses ML as teaching language on data structures and algorithmsMany other universities use Scheme or Haskell.Learn the data structures & algorithms, not fight with segfaults or nullpointers.
FP: new coming?Functional languages compatible with mainstream languages:Microsoft’s F#, based on OCaMLScala, Clojure for Java (hybrids)Python and other script languages adopt many functional ideas, such as list.map() and lambda-functions.Focus on correctness, not performanceEspecially important in finance.Compilers have improved.But,Completely different way of programming: hard to change.Performance (and memory consumption) has been a big issue.Lack of good tools; cumbersome to program “stateful” code such as UI.However, Scala/Clojure/F# will help bridging the gap as they are compatible with Java/C#.
What is Functional Programming?Imperative program executes sequence of commands to compute a value and modifies the state in the process:Functional program is declarativeand uses pattern matching: fun sumlist []    = 0         | (x::xs) = x + sumlist(xs);“sum of numbers in list is the value of head of the list plus the sum of the rest of the list. Sum of an empty list is zero.”Functions are usually defined recursively.
More function declarations> fun      factorial 1 = 1#        | factorial n = n * factorial(n-1);“factorial of n is n multiplied by the factorial of n-1, if n is larger than 1.Otherwise, factorial for 1 is 1.”fun gcd(a : int, 0) = a    | gcd(a : int, b : int) =         if (a < b) then gcd(a, b mod a) else gcd(b, a mod b)
More function declarationsMore pattern matching for types:
Functional programming (cont.)Imperative program executes commands, functional program evaluates expressions.Functions have no side-effects – there is no state.“Monads” used for input/output etc. (not part of this talk)Values are bound to symbols – but they cannot be changed!Definition of variable is different: it is not an address in memory.
Functional programming (cont.)Functions are values, and can be passed as parameters (anonymous or named):Example :(* Create new functions that return square of the original function *)fun sqrfun (f : real->real) = let      fun F2 (x:real) = f(x)*f(x) in F2 end;valdblsin = sqrfun(Math.sin);> dblsin(2.0);val it = 0.8268218104: real
FP (cont.): Functionalsmap> valmylist = [1,2,3,4,5,6,7,8,9,10];valmylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: int list> List.map(fn x=>x*x) mylist;val it = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]: int listfoldl / foldr> List.foldl(fn (x,n)=>x+n)  0 mylist;val it = 55: int> List.foldlop+ 0 mylist;val it = 55: int> vallistsummer= List.foldl op+ 0;vallistsummer = fn: int list -> int> listsummer(mylist);val it = 55: intfilter> valevenfilter =List.filter(fn (x)=> if (x mod 2 = 0) then true else false);valevenfilter = fn: int list -> int list	> evenfilter(mylist);val it = [2, 4, 6, 8, 10]: int listExtremely consistent and clear code!
Referential Transparency= expression can be safely replaced by its values= no side-effects.Enables reasoning about semantics of programs. Lazy evaluation.Automatic parallel evaluation: order of execution does not matter.Imperative programs are close to machine code, hard to read. Meaning of functional program is “as it is written”.
Type systemVery powerful feature of ML (and Haskell):use it for your benefit. Compiler can detect many errors that Java compiler could not.(You can ignore the type system and use wildcards extensively, but then you lose much of the power of Functional Programming).Parametric polymorphism and algebraic data typesinstead of inheritance.Example…
Type system: algebraic datatypesProduct types (records=structs) and sum-types (union of types, such as: user|pet|furniture)
ADT (cont.)
ADT (cont.)Let’s now add a new room object type, “wallitem”.But we forget to add a case to“blocks_coordinate”!
ADT (cont.)But compiler warns us about this!
ADT (cont.)Compiler also warns about redundant casestypical error.Consider this in Javaperhaps you have blocksCoordinate() for each room object class – example may not be best.but, maybe in Catalog code we have special price for each item for Moderators:This will be implemented as a if/case in Catalog code, not as a method in User object.Easy to forget cases when new user types are added.
Parametric Polymorphism	Close to C++ templatesunlike Java generics, first-class types (compile-time).Recursive types- definition of a tree with node type a’:datatype ’a tree = 		     Empty |			 Node of ’a tree * ’a * ’a tree
MiscellaniousIn practice, it is easy to very also inefficient code in SML (like it is with Java).Garbage collection.Standard ML has a sophisticated module system that neatly separates API and implementation.Lazy MLHaskell is lazily evaluated.Laziness is problematic for parallelism or reasoning about costs.OCaML brings objects to ML.	(but no-one uses them…)
Functional Style with JavaSome ideas
Functional Languages for JDKScalagaining popularity.compiles into Java bytecode, compatible with Java.parallel computation features.Erlang-type concurrent messaging style programming also supported.ClojureLISP dialect.
1) Pattern: Immutable objectsIf a “manager” returns an object, usually it should be immutable – and a copy:create special immutable classes:  ImmutableUser, ImmutableRoom, etc. => Makes ownership of the object clear.better to make own classes that do not have methods to modify the object than throw an exception!Collections.unmodifiableList(…)do not cache objects – only manager caches.makes thread-safe programming easier.Favor tiny carrier objects instead of huge monstrous objectsinstead of User, favor UserId, UserAppearance.make it clear by naming, that the object does not represent the “actual object”, buti s  a “view” of it.especially important with DAOs!! Should be evident what data has been loaded to the data-object.
2) Fail fastJava does not do much sanity checking in compilation time, so try to make incorrect code fail quickly in test time. Use runtime exceptions – and  you should not prepare to handle these kind of errors.
3) Instead of modifying an object, create new onesAvoids race conditions.Java GC is really fast, and likes short-lived objects.Particulary: avoid recycling objects. It is very error-prone!
4) Assertions and DeclarationsFunctional:  @MessageHandler(id=.., need_session=true), @ModeratorUse conservative default values for parameters!This is much better than expecting programmers to check all cases.Assertive: @NotNull, @NoSideEffect, @Slow etc.not great, but better than nothing.Intuitive naming: don’t use getXX, if method has a side-effect!Long method-names that explain what method does are good:user = loginUserWithPassword(user, pass);user = loginUserWithToke(token);instead of user = login(user,pass); user = login(token);
Do not switch assertions off!“… it is absurd to make elaborate security checks on debugging runs, when no trust is put into results, and then remove them in production runs, when an errorenous result could be expensive or disastrous. What would we think of a sailing enthusiast who wears his life-jacket when training on dry land but takes it off as soon as he goes to sea?”C.A.R Hoare (1989)
5) Poor man’s Closures	Closures = anonymous/lambda functionsexample from Groovy:Unfortunately Java (not even 7) will support Closures.BUT, anonymous classes can be used for the same purpose – although with much boilerplate.
Closure example
6) Compute, do not storeIf computing a value is reasonably light-weight, it is better to do that than store a precomputed value.Avoids many errors of forgetting to update all fields of an object.(example on next slide)
Example
Discussion
QuestionsDo you have experience on functional programming?Opinion on using Hibernate vs. using explicit queries?Where is Java going?why is it so difficult to do simple things, like a simple web service, with Java?Convention vs. Configuration?
ResourcesPolyML: http://www.polyml.org/Google: “ocaml jane street cmu”good video introduction to FPscala-lang.org

About Functional Programming

  • 1.
    About Functional Programming(RobustProgramming)AapoKyrölä, 23.3.2011Sulake
  • 2.
    ContentsMotivation: some issueswith OOP and JavaIntroduction to Functional ProgrammingFunctional Programming –style in Java
  • 3.
    Motivation: Problems withObject-Oriented Programming and Java
  • 4.
    Fundamental Design Problemof JavaNot well-typed => NullpointersProgrammer should handle two cases explicitly: userToBan is either null or not null.OR, compiler should not allow assigning NULL to a reference (like in C++ &obj).By proper typing, compiler would prevent null-pointers…
  • 5.
  • 6.
    OOP: criticismClass/Object structurecumbersome to change.a class hierarchy might be natural from some point of view, but for other (future) uses cumbersome.Problems with Inheritancemuch functionality hidden in superclasses.Hard to manage: if behavior of superclass is changed, how does it affect all subclasses?Especially in UI: subclass may depend on a not-intended behavior of a superclass.Delegation usually better than inheritance (Spring)better modularity with IOP than with object hierarchies.API dependencies.Makes refactoring hard.
  • 7.
    Object-relational mappingAlways hated…a)Intuitively, object should have an identityBut when you have many copies of the object…b) Queries with criteria-objects or example objects just stink.Select * from sale where date>? and date<? order by date limit 10;
  • 8.
    ORM (continues)c) Evenbigger problem with relationsIdentity of the objects in collection?Cumbersome to control when you want the collections loaded (all that hibernate session stuff).d) SQL is great, easy and powerful. Why switch away? Declarative programming is powerful.
  • 9.
    Side effectsWhen youcall a method, it can change the state, even if it is named getX().At least previously in Habbo: effects on database object cache.Or : calling a collection method in hibernate object, user.getFriends().You need to know if users were loaded in order to reason what happens.
  • 10.
    Who manages anobject?Java does not have immutable objectsNeed to “wrap” them by hand (Collections.unmodifiableList()).Error only on run-time. How do you handle such error? Should be compile-time.Concurrency issuesWho manages multi-threaded access to an object? Read-only objects do not have this problem…Easy to get deadlocks.
  • 11.
    Key to morerobust code Compiler and type-system should do more!Generics are even worse…End of rant.
  • 12.
    Quick introduction toFunctional Programming Languages
  • 13.
    ContentsSome historyFunctional vs.imperative languagesDeclarative programming.Functions as values.High-order functions.Referential transparency.Power of the Type system in Standard ML.All examples in Standard ML.Next: how to program “functionally in Java”.
  • 14.
    HistoryLambda calculus (30s-40s):mathematical theory for computing with functions (Church).LISP (50s): motivated by artificial intelligence. Introduced high level data structures such as lists and trees. Scheme, ClojureMeta-language: ML (70s): for writing proof checkers.Standard ML, OCaML, F#Haskell (80s-90s)
  • 15.
    History (cont.)Functional programminghas (so far) mostly been favored in the academiaMathematically consistent (no pointer-arithmetic).Clear semantics: define functions, not manipulate state.Focus on computation, not on memory addressing, low-level issues.Carnegie Mellon uses ML as teaching language on data structures and algorithmsMany other universities use Scheme or Haskell.Learn the data structures & algorithms, not fight with segfaults or nullpointers.
  • 16.
    FP: new coming?Functionallanguages compatible with mainstream languages:Microsoft’s F#, based on OCaMLScala, Clojure for Java (hybrids)Python and other script languages adopt many functional ideas, such as list.map() and lambda-functions.Focus on correctness, not performanceEspecially important in finance.Compilers have improved.But,Completely different way of programming: hard to change.Performance (and memory consumption) has been a big issue.Lack of good tools; cumbersome to program “stateful” code such as UI.However, Scala/Clojure/F# will help bridging the gap as they are compatible with Java/C#.
  • 17.
    What is FunctionalProgramming?Imperative program executes sequence of commands to compute a value and modifies the state in the process:Functional program is declarativeand uses pattern matching: fun sumlist [] = 0 | (x::xs) = x + sumlist(xs);“sum of numbers in list is the value of head of the list plus the sum of the rest of the list. Sum of an empty list is zero.”Functions are usually defined recursively.
  • 18.
    More function declarations>fun factorial 1 = 1# | factorial n = n * factorial(n-1);“factorial of n is n multiplied by the factorial of n-1, if n is larger than 1.Otherwise, factorial for 1 is 1.”fun gcd(a : int, 0) = a | gcd(a : int, b : int) = if (a < b) then gcd(a, b mod a) else gcd(b, a mod b)
  • 19.
    More function declarationsMorepattern matching for types:
  • 20.
    Functional programming (cont.)Imperativeprogram executes commands, functional program evaluates expressions.Functions have no side-effects – there is no state.“Monads” used for input/output etc. (not part of this talk)Values are bound to symbols – but they cannot be changed!Definition of variable is different: it is not an address in memory.
  • 21.
    Functional programming (cont.)Functionsare values, and can be passed as parameters (anonymous or named):Example :(* Create new functions that return square of the original function *)fun sqrfun (f : real->real) = let fun F2 (x:real) = f(x)*f(x) in F2 end;valdblsin = sqrfun(Math.sin);> dblsin(2.0);val it = 0.8268218104: real
  • 22.
    FP (cont.): Functionalsmap>valmylist = [1,2,3,4,5,6,7,8,9,10];valmylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: int list> List.map(fn x=>x*x) mylist;val it = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]: int listfoldl / foldr> List.foldl(fn (x,n)=>x+n) 0 mylist;val it = 55: int> List.foldlop+ 0 mylist;val it = 55: int> vallistsummer= List.foldl op+ 0;vallistsummer = fn: int list -> int> listsummer(mylist);val it = 55: intfilter> valevenfilter =List.filter(fn (x)=> if (x mod 2 = 0) then true else false);valevenfilter = fn: int list -> int list > evenfilter(mylist);val it = [2, 4, 6, 8, 10]: int listExtremely consistent and clear code!
  • 23.
    Referential Transparency= expressioncan be safely replaced by its values= no side-effects.Enables reasoning about semantics of programs. Lazy evaluation.Automatic parallel evaluation: order of execution does not matter.Imperative programs are close to machine code, hard to read. Meaning of functional program is “as it is written”.
  • 24.
    Type systemVery powerfulfeature of ML (and Haskell):use it for your benefit. Compiler can detect many errors that Java compiler could not.(You can ignore the type system and use wildcards extensively, but then you lose much of the power of Functional Programming).Parametric polymorphism and algebraic data typesinstead of inheritance.Example…
  • 25.
    Type system: algebraicdatatypesProduct types (records=structs) and sum-types (union of types, such as: user|pet|furniture)
  • 26.
  • 27.
    ADT (cont.)Let’s nowadd a new room object type, “wallitem”.But we forget to add a case to“blocks_coordinate”!
  • 28.
    ADT (cont.)But compilerwarns us about this!
  • 29.
    ADT (cont.)Compiler alsowarns about redundant casestypical error.Consider this in Javaperhaps you have blocksCoordinate() for each room object class – example may not be best.but, maybe in Catalog code we have special price for each item for Moderators:This will be implemented as a if/case in Catalog code, not as a method in User object.Easy to forget cases when new user types are added.
  • 30.
    Parametric Polymorphism Close toC++ templatesunlike Java generics, first-class types (compile-time).Recursive types- definition of a tree with node type a’:datatype ’a tree = Empty | Node of ’a tree * ’a * ’a tree
  • 31.
    MiscellaniousIn practice, itis easy to very also inefficient code in SML (like it is with Java).Garbage collection.Standard ML has a sophisticated module system that neatly separates API and implementation.Lazy MLHaskell is lazily evaluated.Laziness is problematic for parallelism or reasoning about costs.OCaML brings objects to ML. (but no-one uses them…)
  • 32.
    Functional Style withJavaSome ideas
  • 33.
    Functional Languages forJDKScalagaining popularity.compiles into Java bytecode, compatible with Java.parallel computation features.Erlang-type concurrent messaging style programming also supported.ClojureLISP dialect.
  • 34.
    1) Pattern: ImmutableobjectsIf a “manager” returns an object, usually it should be immutable – and a copy:create special immutable classes: ImmutableUser, ImmutableRoom, etc. => Makes ownership of the object clear.better to make own classes that do not have methods to modify the object than throw an exception!Collections.unmodifiableList(…)do not cache objects – only manager caches.makes thread-safe programming easier.Favor tiny carrier objects instead of huge monstrous objectsinstead of User, favor UserId, UserAppearance.make it clear by naming, that the object does not represent the “actual object”, buti s a “view” of it.especially important with DAOs!! Should be evident what data has been loaded to the data-object.
  • 35.
    2) Fail fastJavadoes not do much sanity checking in compilation time, so try to make incorrect code fail quickly in test time. Use runtime exceptions – and you should not prepare to handle these kind of errors.
  • 36.
    3) Instead ofmodifying an object, create new onesAvoids race conditions.Java GC is really fast, and likes short-lived objects.Particulary: avoid recycling objects. It is very error-prone!
  • 37.
    4) Assertions andDeclarationsFunctional: @MessageHandler(id=.., need_session=true), @ModeratorUse conservative default values for parameters!This is much better than expecting programmers to check all cases.Assertive: @NotNull, @NoSideEffect, @Slow etc.not great, but better than nothing.Intuitive naming: don’t use getXX, if method has a side-effect!Long method-names that explain what method does are good:user = loginUserWithPassword(user, pass);user = loginUserWithToke(token);instead of user = login(user,pass); user = login(token);
  • 38.
    Do not switchassertions off!“… it is absurd to make elaborate security checks on debugging runs, when no trust is put into results, and then remove them in production runs, when an errorenous result could be expensive or disastrous. What would we think of a sailing enthusiast who wears his life-jacket when training on dry land but takes it off as soon as he goes to sea?”C.A.R Hoare (1989)
  • 39.
    5) Poor man’sClosures Closures = anonymous/lambda functionsexample from Groovy:Unfortunately Java (not even 7) will support Closures.BUT, anonymous classes can be used for the same purpose – although with much boilerplate.
  • 40.
  • 41.
    6) Compute, donot storeIf computing a value is reasonably light-weight, it is better to do that than store a precomputed value.Avoids many errors of forgetting to update all fields of an object.(example on next slide)
  • 42.
  • 43.
  • 44.
    QuestionsDo you haveexperience on functional programming?Opinion on using Hibernate vs. using explicit queries?Where is Java going?why is it so difficult to do simple things, like a simple web service, with Java?Convention vs. Configuration?
  • 45.
    ResourcesPolyML: http://www.polyml.org/Google: “ocamljane street cmu”good video introduction to FPscala-lang.org

Editor's Notes

  • #2 Havent been here for two years speaking. Currently Ph.D. student in Carnegie Mellon: working on intersection of machine learning and distributed systems. Will study more parallel algorithms etc. further – this topic is very relevant for that. QUESTION: who has experience with Haskell, Scala, Standard ML, LISP?
  • #5 Start with some provocation. A fundamental design flaw in Java, which contributes to unrobust code tremendously.What is wrong with this?What if userToBan is null? Either: compiler should never let banUser to be called with NULL, or one should make a case analysis:
  • #6 In Standard ML: need to use type parameter “option” if type is nullable. In that case, each function must define cases for both non-null user (SOME) or null-user (NONE).If option is not set, value can never have null assignment.
  • #7 There is a lot of criticism about OOP, but it is what people learn in school. Especailly “old school” OOP that uses inheritance heavily is very hard to manage. Of course there are good sides of OOP as well .
  • #13 Disclaimers!
  • #14 Has anyone used Scala, is it used? Sorry that I am not very well aware.
  • #15 Point: functional languages have been there from the beginning. And by original computer scientist, functional programming was the proper way to study computation. Anyway, FP is not a new thing!
  • #16 QUESTION: does Univ of Helsinki or HUT/Aalto teach functional languages? Lambda-calculus theory?
  • #18 Note: this definition of sumlist is quite inefficient, since the recursion blows up. But we will see better version of this later. This is just to illustrate how functions are declarative.
  • #23 Familiar from python, scala
  • #29 This can be extremely useful.
  • #34 are we using these?Especially for payments etc., recommend trying.
  • #36 .. otherwise, the code gets bloated with wide try-catches and the session state can get screwed up. It is better to fail and close the session if a programmer bug is encountered.
  • #41 It is quite bloated, but of course the map/MapClosure, etc. must be done only once.