Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

Program Verification Using the Spec# Programming System ETAPS Tutorial K. Rustan M. Leino, Microsoft Research, Redmond Rosemary Monahan, NUIM Maynooth.
Parallel Haskell Tutorial: The Par Monad Simon Marlow Ryan Newton Simon Peyton Jones.
Kathleen Fisher cs242 Reading: Tackling the Awkward Squad, Sections 1-2Tackling the Awkward Squad Real World Haskell, Chapter 7: I/OReal World Haskell.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Microsoft Research March 20, 2000 A Programming Language for Developing Interactive Web Services Claus Brabrand BRICS, University of Aarhus, Denmark.
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
static void Main() { int i = 0; if (i == 0) { int a = 5; int b = 15; if (a == 5) { int c = 3; int d = 99; }
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
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.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Tutorial 10 Programming with JavaScript
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
C# is a functional programming language Andrew Kennedy Microsoft Research Cambridge.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Javascript and the Web Whys and Hows of Javascript.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Type Safety and Enumerations. Type Checking and Type Errors  The type system defines data types and the operations on data types.  A type error occurs.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Object Oriented Programming … and other things you need to program in java.
Comp150PLD Reading: “Tackling the Awkward Squad,” Sections 1-2Tackling the Awkward Squad “Real World Haskell,” Chapter 7: I/OReal World Haskell Thanks.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Confessions of a used programming language salesperson (an appeal to purity) Erik Meijer.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Wel come To Seminar On C#.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Laziness and Infinite Datastructures
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Java Programming Language
Conditional Statements
Haskell Dealing with impurity 30-Nov-18.
Haskell Dealing with impurity 8-Apr-19.
Introduction to Programming
LCC 6310 Computation as an Expressive Medium
CSE 341 Lecture 11 b closures; scoping rules
Haskell Dealing with impurity 29-May-19.
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

What is a XXX Language? XXX = dynamic XXX = imperative XXX = OO

A language where functions are first-class citizens What is a Functional Language A language where functions are first-class citizens

EWD 1073

Easy like Sunday morning

Dan Meyer WTF went wrong?

Doing IO is a side-effect Console.WriteLine(“ha”); != var x = Console.ReadLine(); var y = x+x; != var y = Console.ReadLine() + Console.ReadLine()

DateTime.Now is not a mathematical function Doing IO is a side-effect let date = DateTime.Now() in (date, date)  ( DateTime.Now() , DateTime.Now() ) DateTime.Now is not a mathematical function

Functional Programming is just academic nonsense If I cannot even do IO Fundamentalist Functional Programming is just academic nonsense

Don’t eat The Yellow Snow!

C# Lazy evaluation and side-effects static bool LessThanThirty(int x) { Console.Write("{0}? Less than 30;",x); return x < 30; } static bool MoretHanTwenty(int x) { Console.Write("{0}? More than 20;",x); return x > 20; } var q0 = from x in new[]{ 1, 25, 40, 5, 23 } where LessThanThirty(x) select x; var q1 = from x in q0 where MoreThanTwenty(x) select x; foreach (var r in q1) Console.WriteLine("[{0}];",r); Order of side effects?

F# Lazy comprehensions let lessThanThirty(x:int)= begin Console.WriteLine ("{0} less than 30?", x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine ("{0} more than 20?", x); x > 20; let q0 = seq { for x in [1; 25; 40; 5; 23] do if lessThanThirty x then yield x } let q1 = seq { for x in q0 do if moreThanTwenty x for r in q1 do Console.WriteLine("> {0},", r) F# Lazy comprehensions

F# Eager comprehensions let lessThanThirty(x:int)= begin Console.WriteLine ("{0} less than 30?", x); x < 30; end let moreThanTwenty(x:int)= begin Console.WriteLine ("{0} more than 20?", x); x > 20; let q0 = [ for x in [1; 25; 40; 5; 23] do if lessThanThirty x then yield x ] let q1 = [ for x in q0 do if moreThanTwenty x for r in q1 do Console.WriteLine("> {0},", r) F# Eager comprehensions

Exceptions var xs = new[]{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; IEnumerable<int> q = null; try { q = from x in xs select 1/x; } catch { q = new int[]; } foreach(var z in q) Console.WriteLine(z): // throws here

Disposing resources Func<string> GetContents = null; using( var file = FileSystem.OpenTextFileReader(@"…")) { GetContents = () => file.ReadToEnd(); } // surprise! an exception Console.WriteLine(GetContents());

Variable Instantiation var DelayedActions = new List<Func<int>>(); var s = ""; for (var i = 4; i < 7; i++) { DelayedActions.Add(delegate(){ return i; }); } for (var k = 0; k < DelayedActions.Count; k++) s += DelayedActions[k](); Console.WriteLine(s); Prints 777

Variable Instantiation var DelayedActions = new List<Func<int>>(); var s = ""; for (var i = 4; i < 7; i++) { var j = i; DelayedActions.Add(delegate(){ return j; }); } for (var k = 0; k < DelayedActions.Count; k++) s += DelayedActions[k](); Console.WriteLine(s); Prints 456 C# Prints 666 JavaScript

Variable Instantiation var DelayedActions = new List<Func<int>>(); var s = ""; var j; for (var i = 4; i < 7; i++) { j = i; DelayedActions.Add(delegate(){ return j; }); } for (var k = 0; k < DelayedActions.Count; k++) s += DelayedActions[k](); Console.WriteLine(s); Prints 666

Object creation var o1 = new object(); //58225482, for instance Console.WriteLine(o1.GetHashCode()); var o2 = new object(); // 54267293, for instance Console.WriteLine(o2.GetHashCode()); Debug.Assert(o1 != 02);

Concurrency new_cell(X) -> spawn(fun() -> cell(X) end). cell(Value) -> receive {set, NewValue} -> cell(NewValue); {get, Pid} -> Pid!{return, Value}, cell(Value); {dispose} -> {} end. set_cell(Cell, NewValue) -> Cell!{set, NewValue}. get_cell(Cell) -> Cell!{get, self()}, {return, Value} -> Value dispose_cell(Cell) -> Cell!{dispose}.

Page 165: There is a subtle error in on_exit and keep_alive … Write your code such that race conditions cannot happen. … Fortunately, the OTP libraries have code for building servers, supervisor trees, and so on. These libraries have been well tested and should not suffer from any race conditions.

few interesting functions are idempotent *not* idempotent *not* pure few interesting functions are idempotent GET …/person/age… GET …/clock/seconds…

“Dishonest” types static T Identity<T>(this T me) { Not parametric! static T Identity<T>(this T me) { if(me.GetType() == typeof(Button)) throw new Exception(); var mini_me = me as string; if(mini_me != null) return “hello world”; Console.WriteLine(me.ToString()); return Activator.CreateInstance<T>(); }

HOT Languages To the rescue

Haskell lessThanThirty x = do { putStrLn $ (show x)++" less than 30?" ; return $ x < 30 } moreThanTwenty x = do { putStrLn $ (show x)++" more than 20?" ; return $ x > 20 q0 = [ x | x <- [1, 25, 50, 5, 23 ] , lessThanThirty x ] q1 = [ x | x <- q0 , moreThanTwenty 20 ] Couldn't match expected type `Bool' against inferred type `IO Bool' In a stmt of a list comprehension: lessThanThirty x In the expression: [x | x <- [1, 25, 50, ....], lessThanThirty x] In the definition of `q0': q0 = [x | x <- [1, 25, ....], lessThanThirty x Haskell

Forget about effects? Forget it! unsafePerformIO :: IO a -> a unsafeCast :: a -> b unsafeCast x = unsafePerformIO $ do{ writeIORef castref x ; readIORef castref } castref = unsafePerformIO $ do{ newIORef undefined }

Cover up effects

Remove all effects (cannot make language more powerful by removing features)

Don’t fight effects Embrace and extend

Mens sana in corpore sano

Mistake DateTime.Now() DateTime Type error !!!!!!!!!!!!!!!

Time changes, the clock does not Values vs Computation How to turn something mutable into something immutable? Time changes, the clock does not

It’s just a collection … 9:15 AM 9:16 AM 9:17 AM 9:19 AM 9:29 AM All problems in computer science can be solved by another level of indirection D. Wheeler

Mathematics is always right

But many ways to be impure Only one way to be pure, But many ways to be impure Multithreading  STM<T> Exceptions  Throws<T> Reflection  Mirror<T> Destructive updates  IO<T>

The “M” word M  IO Exception Animation Collection Monads M<A> A computation that produces a value of type A with side-effects described by M

Side-effecting computation that yields a value of type a data IO a putChar :: Char -> IO () getChar :: IO Char newIORef :: a -> IO (IORef a) readIORef :: IORef a -> IO a writeIORef :: IORef a -> a -> IO () forkIO :: IO a -> IO ThreadID

Fundamentalist Functional Can Normal Programmers Understand Fundamentalist Functional Programming?

LINQ Monads are the secret sauce behind LINQ Bind IEnumerable<S> SelectMany<T,S>( IEnumerable<T> src, Func<T, IEnumerable<S>> selector )

Java Throws clause void Foo(string s) throws Exception Explicit Effects

Abstract from evaluation order Make all effects explicit ==> Fundamentalist Functional Programming Abstract from evaluation order Make all effects explicit ==> Compiler can freely rearrange code Runtime can execute in code any order Call-by-name, or call-by-value version?

Call To Action Buy Graham Hutton’s book Nudge and nag MS to produce a fundamentalist functional language

Buy Bryan John & Don’s book Nudge and nag MS to produce a fundamentalist functional language

Thank You