Computation Abstraction Going beyond programming language glue, or what we've missed from FP for so long in mainstream  googlewave.com!w+PgcakhgiA  sadache@twitter.

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Advertisements

CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Java: Chapter 1 Computer Systems Computer Programming II.
The Java Programming Language
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
C++ crash course Class 8 statements, sort, flight times program.
Reactive pattern matching for F# Part of “Variations in F#” research project Tomáš Petříček, Charles University in Prague
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programovací jazyky F# a OCaml Chapter 6. Sequence expressions and computation expressions (aka monads)
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Thinking reactive. Who is speaking? Software developer at Seavus Member of JugMK coffee - code - pizza - repeat.
1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Practical Programming in an Exception Free World
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
Exceptions In this lecture:
George Mason University
Exceptions: When things go wrong
6.001 SICP Variations on a Scheme
CS 4450: Principles of Programming Languages
Lecture 2: Data Types, Variables, Operators, and Expressions
Delegates and Events 14: Delegates and Events
Creating and Modifying Text part 2
Stack Data Structure, Reverse Polish Notation, Homework 7
ATS Application Programming: Java Programming
Functional Programming with Java
CSE 341: Programming Langs
CSE341: Programming Languages Section 1
Learning to Program in Python
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
null, true, and false are also reserved.
Exceptions with Functions
Java Programming Language
CS1100 Computational Engineering
CSE341: Programming Languages Section 1
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Chapter 1: Computer Systems
CMSC 202 Lesson 22 Templates I.
Fundamental Error Handling
Dynamic Scoping Lazy Evaluation
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
Focus of the Course Object-Oriented Software Development
Fundaments of Game Design
Records and Type Classes
Templates I CMSC 202.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
Chap 2. Identifiers, Keywords, and Types
Review of Previous Lesson
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exception Handling.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Lecture 6 - Recursion.
Records and Type Classes
Presentation transcript:

Computation Abstraction Going beyond programming language glue, or what we've missed from FP for so long in mainstream  googlewave.com!w+PgcakhgiA  sadache@twitter

Sadek Drobi Consultant (j2EE, Sadek Drobi Consultant (j2EE,.Net) Programming Languages Hobbyist Training: FP (F#, Scala), WOA Architecture Editor at InfoQ.com Maybe known for my FP interviews? http://www.infoq.com/bycategory/contentbycategory.action?authorName=Sadek-Drobi&ct=2 WWW.SadekDrobi.com sadache@twitter

What has been abstraction for us in mainstream? Hierarchical and Structural OAbstruaction

What do we know about Computation Abstraction in mainstream? This page is intentionally left blank.

What do we know about Computation Abstraction in mainstream? Did anyone mention Behavioral GOF Design Patterns?

What is Computation Abstraction? googlewave.com!w+PgcakhgiA sadache@twitter

It is all about Glue! googlewave.com!w+PgcakhgiA sadache@twitter

int GetInterstingNumber(string cityName){ IDictionary<string,int> zipCodes= new Dictionary<string,int>{             {"Paris",75}         }; IDictionary<int, int> population = new Dictionary<int, int>{             {75,100}         };          int GetInterstingNumber(string cityName){         var myCityCode= zipCodes[cityName];        return ( population[myCityCode] *100 ) / TOTAL_POPULATION ;  } void PrintIt(string[] args){        Console.WriteLine("Paris has "+getInterstingNumber("Paris")+                                      "% of Population"); googlewave.com!w+PgcakhgiA sadache@twitter

int GetInterstingNumber(string cityName){ IDictionary<string,int> zipCodes= new Dictionary<string,int>{             {"Paris",75}         }; IDictionary<int, int> population = new Dictionary<int, int>{             {75,100}         };          int GetInterstingNumber(string cityName){         var myCityCode= zipCodes[cityName];        return ( population[myCityCode] * 100) / TOTAL_POPULATION ;  } void PrintIt(string[] args){        Console.WriteLine(“Nancy has "+getInterstingNumber(“Nancy")+                                      "% of Population"); googlewave.com!w+PgcakhgiA sadache@twitter

Welcome in the REAL WORLD googlewave.com!w+PgcakhgiA sadache@twitter

int GetInterstingNumber(string cityName){ IDictionary<string,int> zipCodes= new Dictionary<string,int>{             {"Paris",75}         }; IDictionary<int, int> population = new Dictionary<int, int>{             {75,100}         };          int GetInterstingNumber(string cityName){         var myCityCode= zipCodes[cityName];        return ( population[myCityCode] * 100 ) / TOTAL_POPULATION ;  } void PrintIt(string[] args){        Console.WriteLine(“Nancy has "+getInterstingNumber(“Nancy")+                                      "% of Populatin"); googlewave.com!w+PgcakhgiA sadache@twitter

static int? GetInterstingNumber(string cityName){     int? myCityCode=null;     try       {          myCityCode = zipCodes[cityName];       }     catch(KeyNotFoundException e)       {          myCityCode = null;          return (population[myCityCode.Value] * 100 / TOTAL_POPULATION);     catch (KeyNotFoundException e){ return null;}     catch(/* .Value can produce an*/ InvalidOperationException e)             {                 return null;             } } googlewave.com!w+PgcakhgiA sadache@twitter

static int? GetInterstingNumber(string cityName){     int? myCityCode=null;     try       {          myCityCode = zipCodes[cityName];       }     catch(KeyNotFoundException e)       {          myCityCode = null;          return (population[myCityCode.Value] * 100 / TOTAL_POPULATION);     catch (KeyNotFoundException e){ return null;}     catch(/* .Value can produce an*/ InvalidOperationException e)             {                 return null;             } } googlewave.com!w+PgcakhgiA sadache@twitter

How does this default glue look like? googlewave.com!w+PgcakhgiA sadache@twitter

How does this only possible glue look like? Errors are represented through an Exception System that by default cascades them up the call stack Exceptions short circuit (interrupt execution until they are “catched”) Nulls by default produce errors that cascade up as exceptions These defaults can’t be overridden but can be interrupted using some language syntax googlewave.com!w+PgcakhgiA sadache@twitter

What's wrong with our only possible glue? googlewave.com!w+PgcakhgiA sadache@twitter

Mainly two types of problems: one PRACTICAL and one Conceptual googlewave.com!w+PgcakhgiA sadache@twitter

Practical Problem: static int? GetInterstingNumber(string cityName){ Noise that disperses the main algorithm declaration making readability a challenge static int? GetInterstingNumber(string cityName){     const int TOTAL_POPULATION=123;     int? myCityCode=null;     try       {          myCityCode = zipCodes[cityName];       }     catch(KeyNotFoundException e)       {          myCityCode = null;          return (population[myCityCode.Value] * 100 / TOTAL_POPULATION);     catch (KeyNotFoundException e){ return null;}     catch(/* .Value can produce an*/ InvalidOperationException e)             {                 return null;             } } The special case handling is duplicated in two different places googlewave.com!w+PgcakhgiA sadache@twitter

Conceptual Problem: static int? GetInterstingNumber(string cityName){ I can’t abstract it and say for instance: for any null you encounter in the algorithm stop and return null as a final answer. Conceptual Problem: static int? GetInterstingNumber(string cityName){     const int TOTAL_POPULATION=123;     int? myCityCode=null;     try       {          myCityCode = zipCodes[cityName];       }     catch(KeyNotFoundException e)       {          myCityCode = null;          return (population[myCityCode.Value] * 100 / TOTAL_POPULATION);     catch (KeyNotFoundException e){ return null;}     catch(/* .Value can produce an*/ InvalidOperationException e)             {                 return null;             } } … and since I can’t abstract the glue logic I can’t reuse it in another algorithm definition across my application/domain googlewave.com!w+PgcakhgiA sadache@twitter

Some languages continue kindly trying add more ways to approach these issues but they still share the same problems with the main glue “Nancy has“ + getInterstingNumber(“Nancy")??”NA”+"% of Population” googlewave.com!w+PgcakhgiA sadache@twitter

So how do we abstract “glue” or “computations”? or as some call it “overriding the semicolon ;” googlewave.com!w+PgcakhgiA sadache@twitter

We need computation abstraction tools! Functional Languages contain naturally our needed tools (functions, functors, monads,…) The good news is that some currently mainstream (C#) and other potentially mainstream (Scala, F#) programming languages have them in someway too! googlewave.com!w+PgcakhgiA sadache@twitter

Abstracting the glue with nulls (None) in F# would be: let getInterestingNumber (cities:Map<string,int>) (population:Map<int,int>) (cityName:string) :int Option=      maybe{ let! zipCode= cities.TryFind cityName             let! cityPopulation= population.TryFind zipCode             return cityPopulation * 100 / TOTAL_POPULATION } googlewave.com!w+PgcakhgiA sadache@twitter

The glue implementation with null (None) propagation looks like: module Maybe=     let succeed x = Some(x)     let fail = None     let bind p rest =         match p with             | None -> fail             | Some r -> rest r     let delay f = f()           type MaybeBuilder() =         member b.Return(x) = succeed x         member b.Bind(p, rest) = bind p rest         member b.Delay(f) = delay f         member b.Let(p,rest) = rest p             let maybe = MaybeBuilder() googlewave.com!w+PgcakhgiA sadache@twitter

What about a glue implementation that collects errors and goes on? let plusOneWithError ints= (Error "First Error",List.map ((+)1) ints) let plus2WithAnotherError ints=(Error “2nd Error",List.map ((+)2) ints) let twiceWithNoError ints= (NoError,List.map (( *)2) ints )      let answer= collectingErrors { let! l1= plusOneWithError [1;2;3]                  let! l2= plusTwoWithAnotherError l1                   let! l3= twiceWithNoError l2                              return l3 } val final : Error * int list = (ErrorList [Error "First Error"; Error "Second Error"], [8; 10; 12]) googlewave.com!w+PgcakhgiA sadache@twitter

In Scala you can Abstract Computation too: for{ i <- Some(1) val j = i +1 } yield j ) Evaluates to Some(2) googlewave.com!w+PgcakhgiA sadache@twitter

def map[B](f: A => B): Option[B]= o match{case None => None                             case Some(a) => Some f(a)}    def flatMap[B](f: A => Option[B]): Option[B]=                       o match{case None => None                               case Some(a) => f(a)}

What about C#, a current mainstream language? Heard of LinQ of course! IEnumerable<double> result=from i in Enumerable.Range(0,100) select 1.0/i; there is an error of division by zero there! IEnumerable<double> result=from i in Enumerable.Range(0,100) .IgnoringErrors() select 1.0/i; Non problem. Since we abstracted computation, we can use a more tolerant implementation of the glue! googlewave.com!w+PgcakhgiA sadache@twitter

What about C#, a current mainstream language? Implementing asynchronous programming glue using Computation Abstraction in the Reactive Programming Framework: // Create mouse drag var mouseDrag = from md in this.GetMouseDown() from mm in this.GetMouseMove() .Until(this.GetMouseUp()) select mm; // Subscribe var handler = mouseDrag.Subscribe( e => PublishMouseDrag(e.EventArgs.Location)); googlewave.com!w+PgcakhgiA sadache@twitter

In Scala Error propagating glue without exceptions: def throwError(i:Int):ThrowsError[Int]= Error("I don't like " +i+ "!"); for{i <- throwError(2) val j = i + 1} yield j ) Evaluates to Error(I don't like 2!) googlewave.com!w+PgcakhgiA sadache@twitter

case class ThrowsError[A] (e:Either[Error,A]){         def map[B](f: A => B): ThrowsError[B]= ThrowsError(e.right.map(f))         def flatMap[B](f: A => ThrowsError[B]): ThrowsError[B]= ThrowsError(e.right.flatMap(a=>f(a).e))   }

Asynchronous Worflows in F# let asynctask = async { let req = WebRequest.Create(url) let! response = req.GetResponseAsync() let stream = response.GetResponseStream() let streamreader = new System.IO.StreamReader(stream) return streamreader.ReadToEnd() } googlewave.com!w+PgcakhgiA sadache@twitter

What did I show so far? Defined glue for: Nulls Errors that propagate (better exceptions) Custom glue that doesn’t short circuit (collectErrors) Events and asynchronous programming A lot is already available (Lists, Streams, Channels, State, Enviorement…) Easily add your own (implementing map, bind(flatMap)) Combine them! googlewave.com!w+PgcakhgiA sadache@twitter

Reuse and combine Monad Transformers googlewave.com!w+PgcakhgiA sadache@twitter

Showed Computation Abstraction Functors, Applicative Functors, Monads, Comands Still there are others! Like Arrows for more control over your computation: googlewave.com!w+PgcakhgiA sadache@twitter

Q? Release Your Algorithms from plumping code Let them Express themselves! Q? googlewave.com!w+PgcakhgiA sadache@twitter