functional and concurrent programming - university of · pdf filefunctional and concurrent...

64
Functional and Concurrent Programming Simon Thompson [email protected] CO545 Lecture 1

Upload: phungkien

Post on 11-Mar-2018

243 views

Category:

Documents


2 download

TRANSCRIPT

Functional and Concurrent Programming

Simon [email protected]

CO545 Lecture 1

CO545: functional and concurrent programming

CO545: functional and concurrent programming

Lectures22 lectures: introduction, functional programming,concurrent programming,

going further

CO545: functional and concurrent programming

Classes10 seminars:

from next week.11 terminal sessions:

from this week

Lectures22 lectures: introduction, functional programming,concurrent programming,

going further

CO545: functional and concurrent programming

Classes10 seminars:

from next week.11 terminal sessions:

from this week

Lectures22 lectures: introduction, functional programming,concurrent programming,

going further

LecturersSimon Thompson

[email protected] Dominic Orchard [email protected]

CO545: functional and concurrent programming

Classes10 seminars:

from next week.11 terminal sessions:

from this week

ResourcesMoodle for

slides, lecture recordings, programs, class and seminar resources

Lectures22 lectures: introduction, functional programming,concurrent programming,

going further

LecturersSimon Thompson

[email protected] Dominic Orchard [email protected]

What will I learn?

What will I learn?

Functional ideasValues, names, evaluation,

structured types, lists, higher-order functions,

recursion, PBT.

What will I learn?

Concurrent ideasProcesses and messages,process ids and spawn,

asynchrony and mailboxes,fail-safe and exits, …

Functional ideasValues, names, evaluation,

structured types, lists, higher-order functions,

recursion, PBT.

What will I learn?

Concurrent ideasProcesses and messages,process ids and spawn,

asynchrony and mailboxes,fail-safe and exits, …

Functional ideasValues, names, evaluation,

structured types, lists, higher-order functions,

recursion, PBT.

Put it into practiceUsing these ideas in practice in the Erlang

programminglanguage.

What will I learn?

Concurrent ideasProcesses and messages,process ids and spawn,

asynchrony and mailboxes,fail-safe and exits, …

ContextHaskell for “lazy”, typed

programming.Other concurrency models; scaling up Erlang systems.

Functional ideasValues, names, evaluation,

structured types, lists, higher-order functions,

recursion, PBT.

Put it into practiceUsing these ideas in practice in the Erlang

programminglanguage.

How will I learn?

Four assessments: two functional, two concurrent.

How will I be assessed?

Four assessments: two functional, two concurrent.

Four assessments: two in-class, two take-home.

How will I be assessed?

Programming means making this …

Programming

http://www.cpu-world.com/forum/viewtopic.php?t=19888

Programming

Intel Xeon Phi 'Knight's Corner': 1+ teraFLOPS with double precision; 62 x86 processor cores and a 512 bit GDDR5 memory controller

… do this

… do this

… do this

A# .NET, A# (Axiom), A-0 System, A+, A++, ABAP, ABC, ABC ALGOL, ABLE, ABSET, ABSYS, ACC, Accent, Ace DASL, ACL2, ACT-III, Action!, ActionScript, Ada, Adenine, Agda, Agilent VEE, Agora, AIMMS, , ALF, ALGOL 58, ALGOL 60, ALGOL 68, ALGOL W, Alice, Alma-0, AmbientTalk, Amiga E, AMOS, AMPL, APL, App Inventor for Android's visual block language, AppleScript, Arc, ARexx, Argus, AspectJ, Assembly language, ATS, Ateji PX, AutoHotkey, Autocoder, AutoIt, AutoLISP / Visual LISP, Averest, AWK, Axum, B, Babbage, Bash, BASIC, bc, BCPL, BeanShell, Batch (Windows/Dos), Bertrand, BETA, Bigwig, Bistro, BitC, BLISS, Blue, Bon, Boo, Boomerang, Bourne shell, bash, ksh, BREW, BPEL, C, C--, C++, C#, C/AL, Caché ObjectScript, C Shell, Caml, Candle, Cayenne, CDuce, Cecil, Cel, Cesil, Ceylon, CFEngine, CFML, Cg, Ch, Chapel, CHAIN, Charity, Charm, Chef, CHILL, CHIP-8, chomski, ChucK, CICS, Cilk, CL, Claire, Clarion, Clean, Clipper, CLIST, Clojure, CLU, CMS-2, COBOL, Cobra, CODE, CoffeeScript, Cola, ColdC, ColdFusion, COMAL, Combined Programming Language, COMIT, Common Intermediate Language, Common Lisp, COMPASS, Component Pascal, Constraint Handling Rules, Converge, Cool, Coq, Coral 66, Corn, CorVision, COWSEL, CPL, csh, CSP, Csound, CUDA, Curl, Curry, Cyclone, Cython, D, DASL, DASL, Dart, DataFlex, Datalog, DATATRIEVE, dBase, dc, DCL, Deesel, Delphi, DCL, DinkC, DIBOL, Dog, Draco, DRAKON, Dylan, DYNAMO, E, E#, Ease, Easy PL/I, Easy Programming Language, EASYTRIEVE PLUS, ECMAScript, Edinburgh IMP, EGL, Eiffel, ELAN, Elixir, Elm, Emacs Lisp, Emerald, Epigram, EPL, Erlang, es, Escapade, Escher, ESPOL, Esterel, Etoys, Euclid, Euler, Euphoria, EusLisp Robot Programming Language, CMS EXEC, EXEC 2, Executable UML, F, F#, Factor, Falcon, Fancy, Fantom, FAUST, Felix, Ferite, FFP, Fjölnir, FL, Flavors, Flex, FLOW-MATIC, FOCAL, FOCUS, FOIL, FORMAC, @Formula, Forth, Fortran, Fortress, FoxBase, FoxPro, FP, FPr, Franz Lisp, F-Script, FSProg, G, Google Apps Script, Game Maker Language, GameMonkey Script, GAMS, GAP, G-code, Genie, GDL, Gibiane, GJ, GEORGE, GLSL, GNU E, GM, Go, Go!, Gödel, Godiva, GOM (Good Old Mad), Goo, Gosu, GOTRAN, GPSS, GraphTalk, GRASS, Groovy, Hack (programming language), HAL/S, Hamilton C shell, Harbour, Hartmann pipelines, Haskell, Haxe, High Level Assembly, HLSL, Hop, Hope, Hugo, Hume, HyperTalk, IBM Basic assembly language, IBM HAScript, IBM Informix-4GL, IBM RPG, ICI, Icon, Id, IDL, Idris, IMP, Inform, Io, Ioke, IPL, IPTSCRAE, ISLISP, ISPF, ISWIM, J, J#, J++, JADE, Jako, JAL, Janus, JASS, Java, JavaScript, JCL, JEAN, Join Java, JOSS, Joule, JOVIAL, Joy, JScript, JScript .NET, JavaFX Script, Julia, Jython, K, Kaleidoscope, Karel, Karel++, KEE, Kixtart, KIF, Kojo, Kotlin, KRC, KRL, KUKA, KRYPTON, ksh, L, L# .NET, LabVIEW, Ladder, Lagoona, LANSA, Lasso, LaTeX, Lava, LC-3, Leda, Legoscript, LIL, LilyPond, Limbo, Limnor, LINC, Lingo, Linoleum, LIS, LISA, Lisaac, Lisp, Lite-C, Lithe, Little b, Logo, Logtalk, LPC, LSE, LSL, LiveCode, LiveScript, Lua, Lucid, Lustre, LYaPAS, Lynx, M2001, M4, Machine code, MAD, MAD/I, Magik, Magma, make, Maple, MAPPER, MARK-IV, Mary, MASM Microsoft Assembly x86, Mathematica, MATLAB, Maxima, Macsyma, Max, MaxScript, Maya (MEL), MDL, Mercury, Mesa, Metacard, Metafont, MetaL, Microcode, MicroScript, MIIS, MillScript, MIMIC, Mirah, Miranda, MIVA Script, ML, Moby, Model 204, Modelica, Modula, Modula-2, Modula-3, Mohol, MOO, Mortran, Mouse, MPD, CIL, MSL, MUMPS, NASM, NATURAL, Napier88, Neko, Nemerle, nesC, NESL, Net.Data, NetLogo, NetRexx, NewLISP, NEWP, Newspeak, NewtonScript, NGL, Nial, Nice, Nickle, NPL, Not eXactly C, Not Quite C, NSIS, Nu, NWScript, NXT-G, o:XML, Oak, Oberon, Obix, OBJ2, Object Lisp, ObjectLOGO, Object REXX, Object Pascal, Objective-C, Objective-J, Obliq, Obol, OCaml, occam, occam-π, Octave, OmniMark, Onyx, Opa, Opal, OpenCL, OpenEdge ABL, OPL, OPS5, OptimJ, Orc, ORCA/Modula-2, Oriel, Orwell, Oxygene, Oz, P#, ParaSail (programming language), PARI/GP, Pascal, Pawn, PCASTL, PCF, PEARL, PeopleCode, Perl, PDL, PHP, Phrogram, Pico, Picolisp, Pict, Pike, PIKT, PILOT, Pipelines, Pizza, PL-11, PL/0, PL/B, PL/C, PL/I, PL/M, PL/P, PL/SQL, PL360, PLANC, Plankalkül, Planner, PLEX, PLEXIL, Plus, POP-11, PostScript, PortablE, Powerhouse, PowerBuilder, PowerShell, PPL, Processing, Processing.js, Prograph, PROIV, Prolog, PROMAL, Promela, PROSE modeling language, PROTEL, ProvideX, Pro*C, Pure, Python, Q (equational programming language), Q (programming language from Kx Systems), Qalb, Qi, QtScript, QuakeC, QPL, R, R++, Racket, RAPID, Rapira, Ratfiv, Ratfor, rc, REBOL, Red, Redcode, REFAL, Reia, Revolution, rex, REXX, Rlab, RobotC, ROOP, RPG, RPL, RSL, RTL/2, Ruby, RuneScript, Rust, S, S2, S3, S-Lang, S-PLUS, SA-C, SabreTalk, SAIL, SALSA, SAM76, SAS, SASL, Sather, Sawzall, SBL, Scala, Scheme, Scilab, Scratch, Script.NET, Sed, Seed7, Self, SenseTalk, SequenceL, SETL, Shift Script, SIMPOL, Shakespeare, SIGNAL, SiMPLE, SIMSCRIPT, Simula, Simulink, SISAL, SLIP, SMALL, Smalltalk, Small Basic, SML, Snap!, SNOBOL, SPITBOL, Snowball, SOL, Span, SPARK, SPIN, SP/k, SPS, Squeak, Squirrel, SR, S/SL, Stackless Python, Starlogo, Strand, Stata, Stateflow, Subtext, SuperCollider, SuperTalk, Swift (Apple programming language), Swift (parallel scripting language), SYMPL, SyncCharts, SystemVerilog, T, TACL, TACPOL, TADS, TAL, Tcl, Tea, TECO, TELCOMP, TeX, TEX, TIE, Timber, TMG, Tom, TOM, Topspeed, TPU, Trac, TTM, T-SQL, TTCN, Turing, TUTOR, TXL, TypeScript, Turbo C++, Ubercode, UCSD Pascal, Umple, Unicon, Uniface, UNITY, Unix shell, UnrealScript, Vala, VBA, VBScript, Verilog, VHDL, Visual Basic, Visual Basic .NET, Visual DataFlex, Visual DialogScript, Visual Fortran, Visual FoxPro, Visual J++, Visual J#, Visual Objects, Visual Prolog, VSXu, Vvvv, WATFIV, WATFOR, WebDNA, WebQL, Windows PowerShell, Winbatch, Wolfram, Wyvern, X++, X#, X10, XBL, XC, XMOS architecture, xHarbour, XL, Xojo, XOTcl, XPL, XPL0, XQuery, XSB, XSLT, XPath, Xtend, Yorick, YQL, Z notation, Zeno, ZOPL, ZPL.

A list of programming languages fetched from Wikipedia in CSV format – https://github.com/jamhall/programming-languages-csv

Hardware

Low-level but …

… fiendishly complicated.

The Turing machine

http://www.math.uri.edu/~kulenm/mth381pr/comput/fig.jpg

The von Neumann model

By Kapooht - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=25789639

Computation is …

… performed in a sequence of steps

… through changing values stored in memory

The von Neumann model

object-oriented

object-based

structured

procedural

Scaling up the von Neumann model

assembler

machine code

object-oriented

object-based

structured

procedural

Scaling up the von Neumann model

assembler

machine code

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Instructions to store and fetch values from memory …

… and to perform arithmetical operations on these values and registers.

Linear sequence of code, with (un)conditional jumps.

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Instructions to store and fetch values from memory …

… and to perform arithmetical operations on these values and registers.

Linear sequence of code, with (un)conditional jumps.

Plus symbolic names, library routines.

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Symbolic variables, simple control structures …

… reuse of sections of code, using global variables.

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Symbolic variables, simple control structures …

… reuse of sections of code, w/parameters, return values, scopes.

Defined composite data types.

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Group together variables with the procedures and functions that operate on them.

object-oriented

object-based

structured

procedural

Levels of abstraction

assembler

machine code

Group together variables with the procedures and functions that operate on them.

Inheritance allows objects to extend / modify other objects.

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

object-oriented

object-based

structured

procedural

Examples

assembler

machine code 010010…

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

SPARC, 86x, …

010010…

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

BASIC

SPARC, 86x, …

010010…

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

Pascal, Modula 2

BASIC

SPARC, 86x, …

010010…

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

JavaScript, Haxe

Pascal, Modula 2

BASIC

SPARC, 86x, …

010010…

object-oriented

object-based

structured

procedural

Examples

assembler

machine code

Java, C#, C++

JavaScript, Haxe

Pascal, Modula 2

BASIC

SPARC, 86x, …

010010…

The von Neumann model

By Kapooht - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=25789639

Calculation

http://www.wikihow.com/Evaluate-an-Algebraic-Expression

Calculation

Computation = calculation.

Programming = defining your own functions

f x y = 4*x + 3*y

? f 2 6 ——> 4*2 + 3*6 ——> 8 + 18 ——> 26

Calculation

No variables / storage locations.

Just values of expressions … … and names for values.

f x y = 4*x + 3*y

? f 2 6 ——> 4*2 + 3*6 ——> 8 + 18 ——> 26

Abstraction hiding the VNM

No variables / storage locations.

Just values of expressions … … and names for values.

Abstraction hiding the VNM

No variables / storage locations.

Just values of expressions … … and names for values. Von Neumann

Storage allocation, modification, data structure mutation

Abstraction hiding the VNM

No variables / storage locations.

Just values of expressions … … and names for values.

CalculationImmutable data

Named values and subexpressions

Von NeumannStorage allocation, modification, data structure mutation

The variety of functional languages

LISPEarliest language with funs.Symbolic computing: eval.

Weak types.AI applications

ErlangWeakly typed, strict.

Concurrency at heart.Side effects controlled.

Use? telecoms, WhatsApp, …

HaskellLeading “lazy” language: calculate on demand.

Strongly typed. Side effects only via “monads”

OCamlStrongly typed but “strict”.Side effects but controlled.

Systems programming e.g XenBasis of F# (in .NET fwd)

The variety of functional languages

LISPEarliest language with funs.Symbolic computing: eval.

Weak types.AI applications

ErlangWeakly typed, strict.

Concurrency at heart.Side effects controlled.

Use? telecoms, WhatsApp, …

HaskellLeading “lazy” language: calculate on demand.

Strongly typed. Side effects only via “monads”

OCamlStrongly typed but “strict”.Side effects but controlled.

Systems programming e.g XenBasis of F# (in .NET fwd)

Interpreter

programsource

resultsprogram

Compiler

programsource

programtarget

Compiler

front endprogram

sourceprogramtargetback

end IR

Compiler

front endprogram

sourceprogramtargetback

end IRoptimiser

IR

Compiler

front endprogram

sourceprogramtargetback

end IRoptimiser

IR

Uncover the structure in the text

Lexical analysis: find the words.Parsing: find the structure.Analysis: find aspects of the meaning.Generate an intermediate representation.

Compiler

front endprogram

sourceprogramtargetback

end IRoptimiser

IR

Uncover the structure in the text

Lexical analysis: find the words.Parsing: find the structure.Analysis: find aspects of the meaning.Generate an intermediate representation.

IR: e.g. SSA formatOptimisation = Analysis + Transformation Optimiser will combine many small optimisations e.g … … data flow: float out calculations from loops.

Compiler

front endprogram

sourceprogramtargetback

end IRoptimiser

IR

Uncover the structure in the text

Lexical analysis: find the words.Parsing: find the structure.Analysis: find aspects of the meaning.Generate an intermediate representation.

Generate instructions for the target machine.Allocating registers to symbolic variables.Scheduling instructions.

IR: e.g. SSA formatOptimisation = Analysis + Transformation Optimiser will combine many small optimisations e.g … … data flow: float out calculations from loops.

Concurrency

+

Concurrency

+

Concurrency

+Threads = multiple modifiers + one memory

Concurrency

+Threads = multiple modifiers + one memory

Processes = multiple modifiers + their own memory

Fight over scarce resources

http://www.dailymail.co.uk/news/article-2852585/Mayhem-Black-Friday-begins-Shoppers-clash-supermarkets-trying-grab-bargains-Boots-Game-Curry-s-PC-world-websites-crash-thousands-start-hunt-Christmas-deals.html

Message-passing concurrency

Each process has its own memory …

… garbage collected separately, too.

Communication is via message passing …

… no shared memory at all.

Concurrency is a design artefact …

… but maps onto hardware parallelism.

Don’t forget …

Lecture tomorrow 10:00 in Woolf Lecture Theatre …

… and terminal sessions start this week.