Functional and Concurrent Programming
Simon Thompson s.j[email protected]
CO545 Lecture 1 CO545: functional and concurrent programming CO545: functional and concurrent programming
Lectures 22 lectures: introduction, functional programming, concurrent programming, going further CO545: functional and concurrent programming
Lectures Classes 22 lectures: introduction, 11 two-hour functional programming, terminal sessions: concurrent programming, from this week going further CO545: functional and concurrent programming
Lectures Classes 22 lectures: introduction, 11 two-hour functional programming, terminal sessions: concurrent programming, from this week going further
Resources Moodle for slides, lecture recordings, programs, class and seminar resources CO545: functional and concurrent programming
Lectures Classes 22 lectures: introduction, 11 two-hour functional programming, terminal sessions: concurrent programming, from this week going further
Resources Lecturers Moodle for Simon Thompson slides, lecture recordings, [email protected] programs, class and Dominic Orchard seminar resources d[email protected] What will I learn? What will I learn?
Functional ideas Values, names, evaluation, structured types, lists, higher-order functions, recursion, PBT. What will I learn?
Functional ideas Concurrent ideas Values, names, evaluation, Processes and messages, structured types, lists, process ids and spawn, higher-order functions, asynchrony and mailboxes, recursion, PBT. fail-safe and exits, … What will I learn?
Functional ideas Concurrent ideas Values, names, evaluation, Processes and messages, structured types, lists, process ids and spawn, higher-order functions, asynchrony and mailboxes, recursion, PBT. fail-safe and exits, …
Put it into practice Using these ideas in practice in the Erlang programming language. What will I learn?
Functional ideas Concurrent ideas Values, names, evaluation, Processes and messages, structured types, lists, process ids and spawn, higher-order functions, asynchrony and mailboxes, recursion, PBT. fail-safe and exits, …
Put it into practice Context Using these ideas in Haskell for “lazy”, typed practice in the Erlang programming. programming Other concurrency models; language. scaling up Erlang systems. How will I learn? Lectures … interaction, Q&A, … Terminal sessions … supervisor Lecturers, anon Q&A, drop-in, … How will I learn? How will I be assessed?
Four assessments: two functional, two concurrent.
How will I be assessed?
Four assessments: two functional, two concurrent.
Four assessments: a “driving test”, an in-class test, two take-homes. 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 The von Neumann model
Computation is … … performed in a sequence of steps … through changing values stored in memory Scaling up the von Neumann model
object-oriented object-based structured procedural assembler machine code Scaling up the von Neumann model
object-oriented object-based structured procedural assembler machine code Levels of abstraction
Instructions to store and fetch object-oriented values from memory … object-based … and to perform arithmetical operations on these values and structured registers. procedural Linear sequence of code, with assembler (un)conditional jumps. machine code Examples
object-oriented object-based structured procedural assembler machine 010010… code Levels of abstraction
Instructions to store and fetch object-oriented values from memory … object-based … and to perform arithmetical operations on these values and structured registers. procedural Linear sequence of code, with assembler (un)conditional jumps. machine Plus symbolic names, library code routines. Examples
object-oriented object-based structured procedural assembler SPARC, 86x, … machine 010010… code Levels of abstraction
object-oriented
Symbolic variables, simple control object-based structures … structured
… reuse of sections of code, procedural using global variables. assembler machine code Examples
object-oriented object-based structured procedural BASIC assembler SPARC, 86x, … machine 010010… code Levels of abstraction
object-oriented
Symbolic variables, simple control object-based structures … structured
… reuse of sections of code, w/ procedural parameters, return values, scopes. assembler Defined composite data types. machine code Examples
object-oriented object-based
Pascal, Modula 2 structured procedural BASIC assembler SPARC, 86x, … machine 010010… code Levels of abstraction
object-oriented object-based Group together variables with the procedures and functions structured that operate on them. procedural assembler machine code Examples
object-oriented JavaScript, Haxe object-based
Pascal, Modula 2 structured procedural BASIC assembler SPARC, 86x, … machine 010010… code Levels of abstraction
object-oriented object-based Group together variables with the procedures and functions structured that operate on them. procedural Inheritance allows objects to assembler extend / modify other objects. machine code Examples
Java, C#, C++ object-oriented JavaScript, Haxe object-based
Pascal, Modula 2 structured procedural BASIC assembler SPARC, 86x, … machine 010010… code 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
f x y = 4*x + 3*y Computation = calculation.
Programming = defining your ? f 2 6 ——> 4*2 + 3*6 own functions ——> 8 + 18 ——> 26 Calculation
f x y = 4*x + 3*y No variables / storage locations.
Just values of expressions … ? f 2 6 ——> 4*2 + 3*6 … and names for values. ——> 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
Calculation Immutable data Named values and No variables / storage locations. subexpressions
Just values of expressions … … and names for values. Von Neumann Storage allocation, modification, data structure mutation The variety of functional languages
Haskell LISP Leading “lazy” language: Earliest language with funs. calculate on demand. Symbolic computing: eval. Strongly typed. Weak types. Side effects only via “monads” AI applications
OCaml Erlang Strongly typed but “strict”. Weakly typed, strict. Side effects but controlled. Concurrency at heart. Systems programming e.g Xen Side effects controlled. Basis of F# (in .NET fwd) Use? telecoms, WhatsApp, … The variety of functional languages
Haskell LISP Leading “lazy” language: Earliest language with funs. calculate on demand. Symbolic computing: eval. Strongly typed. Weak types. Side effects only via “monads” AI applications
OCaml Erlang Strongly typed but “strict”. Weakly typed, strict. Side effects but controlled. Concurrency at heart. Systems programming e.g Xen Side effects controlled. Basis of F# (in .NET fwd) Use? telecoms, WhatsApp, … Interpreter
source program program results Compiler
source target program program Compiler
source front IR back target program end end program Compiler
source IR IR target front optimiser back program end end program Compiler
source IR IR target front optimiser back program end end program
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 IR: e.g. SSA format Optimisation = Analysis + Transformation Optimiser will combine many small optimisations e.g … … data flow: float out calculations from loops.
source IR IR target front optimiser back program end end program
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 IR: e.g. SSA format Optimisation = Analysis + Transformation Optimiser will combine many small optimisations e.g … … data flow: float out calculations from loops.
source IR IR target front optimiser back program end end program
Uncover the structure in the text Generate instructions for the target machine. Lexical analysis: find the words. Allocating registers to symbolic variables. Parsing: find the structure. Scheduling instructions. Analysis: find aspects of the meaning. Generate an intermediate representation. 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. You know Java … now here’s Erlang
The basis of Erlang is functional …
You know Java … now here’s Erlang
The basis of Erlang is functional … … which means that it works like a calculator …
You know Java … now here’s Erlang
The basis of Erlang is functional … … which means that it works like a calculator … … but with more complex values than numbers. Erlang is also concurrent
Erlang is also concurrent …
Erlang is also concurrent
Erlang is also concurrent … … allowing different things to go on at the same time …
Erlang is also concurrent
Erlang is also concurrent … … allowing different things to go on at the same time … … just like in the real world, and most computer systems. Why functional?
Functional is more high-level: functions become data.
Why functional?
Functional is more high-level: functions become data. With no side-effects (“immutable data”) algorithms clearer.
Why functional?
Functional is more high-level: functions become data. With no side-effects (“immutable data”) algorithms clearer. New architectures (many/multicore) and patterns e.g. map/reduce. Why concurrency?
Modularity: different “processes” in different processes.
Why concurrency?
Modularity: different “processes” in different processes. Robustness: “let it fail” and others sort it out.
Why concurrency?
Modularity: different “processes” in different processes. Robustness: “let it fail” and others sort it out. Concurrency (independence) vs parallelism (happen at same time). Pragmatic
At its core, Erlang is functional …
Pragmatic
At its core, Erlang is functional … … and it does allow some side-effects (e.g. communication) …
Pragmatic
At its core, Erlang is functional … … and it does allow some side-effects (e.g. communication) … … but not others (no Java-style variables). Pragmatic
Erlang has types …
Pragmatic
Erlang has types … … has a more free-wheeling approach to types than Java …
Pragmatic
Erlang has types … … has a more free-wheeling approach to types than Java … … but can use tools to check type correctness. Useful
Erlang was designed by Ericsson for building telecoms systems …
Useful
Erlang was designed by Ericsson for building telecoms systems … … that must be concurrent, high-availability, robust, fault-tolerant …
Useful
Erlang was designed by Ericsson for building telecoms systems … … that must be concurrent, high-availability, robust, fault-tolerant … Used today by WhatsApp!, VISA, PaddyPower, AlertLogic, æternity, … Useful
Even if what you’re going to do is program in Java …
Useful
Even if what you’re going to do is program in Java … … it’s really useful to have another perspective on how to program. Don’t forget …
Lecture on Tuesday, 10:00 here (in Grimond Lecture Theatre 1).
Terminal sessions start this week.