concepts of programming languages

Concepts of programming languages Lecture 10 Wouter Swierstra - PowerPoint PPT Presentation

Faculty of Science Information and Computing Sciences 1 Concepts of programming languages Lecture 10 Wouter Swierstra Faculty of Science Information and Computing Sciences 2 Last time How do other (typed) languages support


  1. Faculty of Science Information and Computing Sciences 1 Concepts of programming languages Lecture 10 Wouter Swierstra

  2. Faculty of Science Information and Computing Sciences 2 Last time ▶ How do other (typed) languages support metaprogramming ? ▶ Case studies : when do people use reflection? ▶ Embedding DSLs using quasiquotation .

  3. Faculty of Science Information and Computing Sciences 3 This time Quasiquotation and DSLs Concurrency and parallelism ▶ Why is this important? ▶ What are these two concepts? ▶ How does Erlang address these challenges?

  4. Faculty of Science Information and Computing Sciences 4 Quasiquotation As a last example, I want to briefly mention quasiquotation . We’ve seen how to embed domain specific languages in Haskell using deep/shallow embeddings. When we do so, we are constrained by Haskell’s syntax and static semantics. Racket shows how to use macros to write custom language dialects. Haskell’s quasiquotation framework borrows these ideas.

  5. Faculty of Science Information and Computing Sciences 5 Quasiquotation: example Suppose I’m writing a Haskell library for manipulating and generating C code. But working with C ASTs directly is pretty painful: add n = Func (DeclSpec [ ] [ ] (Tint Nothing)) (Id "add") DeclRoot (Args [Arg (Just (Id "x")) ... What I’d like to do is embed (a fragment of) C in my Haskell library.

  6. Faculty of Science Information and Computing Sciences 6 Using quasiquotation The quasiquoter allows me to do just that: int add (int x ) { } |] The cfun quasiquoter tells me how to turn a string into a suitable Exp . add n = [cfun | return x + $int : n$;

  7. Faculty of Science quotePat ... |] it will run the parser associated with the quasiquoter myQQ to Whenever the Haskell parser encounters a quasiquotation [ myQQ | :: String -> Q [Dec] } quoteDec quoteType :: String -> Q Type, :: String -> Q Pat, :: String -> Q Exp, Information and Computing Sciences QuasiQuoter { quoteExp data QuasiQuoter = patterns, types and declarations: A quasiquoter is nothing more than a series of parsers for expressions, Defining quasiquoters 7 generate the quoted expression/pattern/type/declaration.

  8. Faculty of Science = (\a -> beautiful hello example = [ml | And call it as follows: LitE (StringL a)), ... } { quoteExp Information and Computing Sciences ml = QuasiQuoter ml :: QuasiQuoter We can define a quasiquoter: As a simple example, suppose we want to have multi-line string literals. Multiline strings 8 world|] example : String

  9. Faculty of Science Information and Computing Sciences 9 Quasiquoting The quasiquoting mechanism allows you to embed arbitrary syntax within your Haskell program. And still use Template Haskell’s quotation and splicing to mix your object language with Haskell code. This is a mix of the embedded and stand-alone approaches to domain specific languages that we saw over the last few weeks.

  10. Faculty of Science Information and Computing Sciences 10 Parallelism and concurrency

  11. Faculty of Science Information and Computing Sciences 11 Modern server software is demanding to develop and operate: It must be available at all times and in all locations; it must reply within milliseconds to user requests; it must respond quickly to capacity demands; it must process a lot of data and even more traffic; it must adapt quickly to changing product needs; and, in many cases, it must accommodate a large engineering organization, its many engineers the proverbial cooks in a big, messy kitchen. Marius Eriksen, Twitter Principle Engineer, Functional at Scale , CACM December 2016

  12. Faculty of Science Information and Computing Sciences 12 Concurrency & parallelism A parallel program is a program that uses multiplicity of computer hardware to perform a computation more quickly A concurrent program has multiple threads of control . Conceptually, these threads execute ‘at the same’ time, interleaving their effects. These notions are not the same! Programs may be both parallel and concurrent, one of the two, or neither.

  13. Faculty of Science Information and Computing Sciences 13 Determinism and non-determinism A program that will return a single result is said to be deterministic . When different runs of the program may yield different results, the program is non-deterministic . Concurrent programs are necessarily non-deterministic – depending on how threads are interleaved, they may compute different results. Concurrent and non-deterministic programs are much harder to test and verify.

  14. Faculty of Science Information and Computing Sciences 14 Why care about concurrency and parallelism? Computers are not getting much faster: instead they have more and more cores. To exploit this hardware we need parallelism . Programs no longer run in isolation. They are inter-connected – to different machines, to users, to subsystems. To structure such interconnected programs, we need concurrency .

  15. Faculty of Science Information and Computing Sciences 15 Why concurrency? First, scale implies concurrency. For example, a search engine may split its index into many small pieces (or shards) so the entire corpus can fit in main memory. To satisfy queries efficiently, all shards must be queried concurrently. Second, communication between servers is asynchronous and must be handled concurrently for efficiency and safety. Marius Eriksen, Twitter Principle Engineer, Functional at Scale , CACM December 2016

  16. Faculty of Science Information and Computing Sciences 16 Concurrency models Traditionally, concurrency is done through spawning or forking new threads. There are numerous synchronization primitives to co-ordinate between threads: locks, mutexes, semaphores, … If you’ve taken the course on Concurrency, you will have seen how to write algorithms using them.

  17. Faculty of Science Information and Computing Sciences 17 Concurrency models Programming with concurrency is really hard. In particular, once you have state that is mutable and shared between threads, it becomes almost impossible to reason about your program. How can both threads reach the critical section simultaneously?

  18. Faculty of Science Information and Computing Sciences 18 a=0; Thread one a = a + 1; if (a == 1) { critical_section(); } Thread two a = a + 1; if (a == 1) { critical_section(); } Question What can go wrong?

  19. Faculty of Science Information and Computing Sciences 19 Atomicity Assignments are not atomic operations. Instead they read in the current value of a , increment that value, and store it again. This can cause all kinds of undesirable interaction! Check out The Deadlock Empire (https://deadlockempire.github.io/) or our Concurrency course. Programming with locks and threads directly is really hard .

  20. Faculty of Science Information and Computing Sciences 20

  21. Faculty of Science Information and Computing Sciences 21 Beyond locks and threads Locks and threads are at the heart of any concurrent program. More recently, languages have started to offer different abstractions , often built on top of simple locks and threads: These all represent different tools, specifically designed for different problems. ▶ actor model/message passing ▶ software transactional memory ▶ exploiting data parallelism ▶ executing code on GPUs

  22. Faculty of Science Information and Computing Sciences 22 Erlang Originally developed in 1986 at Ericsson, it aimed to improve the software running on telephony switches. Specifically designed for concurrent programming with many different processes that might fail at any time. Open source release together with OTP (Open Telecom Platform) containing: ▶ Erlang compiler and interpreter; ▶ communication protocol between servers; ▶ a static analysis tool (Dialyzer); ▶ a distributed database server (Mnesia); ▶ …

  23. ▶ Social media: ▶ WhatsApp ▶ Grindr ▶ Betting websites ▶ William Hill ▶ Bet365 ▶ Telecoms ▶ TMobile ▶ AT&T ▶ Much, much more.. Faculty of Science Information and Computing Sciences 23 Does anyone use Erlang?

  24. Faculty of Science Information and Computing Sciences 23 Does anyone use Erlang? ▶ Social media: ▶ WhatsApp ▶ Grindr ▶ Betting websites ▶ William Hill ▶ Bet365 ▶ Telecoms ▶ TMobile ▶ AT&T ▶ Much, much more..

  25. Faculty of Science Information and Computing Sciences 24 Erlang for Haskell programmers Erlang is: ▶ dynamically typed; ▶ strict rather than lazy; ▶ impure (there are no restrictions on effects and assignments); ▶ the syntax is familiar, but slightly different.

  26. Faculty of Science Information and Computing Sciences 25 Quicksort qsort([]) -> []; qsort([X|XS]) -> qsort([Y || Y <- Xs, Y < X]) ++ [X] ++ qsort([Y || Y <- Xs, Y >= X]). finished with a period. lower-case letters are atoms . ▶ No ‘equations’ but ‘arrows’ when pattern matching. ▶ Clauses are separated with semi-colons; a function definition is ▶ All Erlang variables start with capital letters; names starting with

  27. Faculty of Science Information and Computing Sciences 26 Modules To compile the code, we need to include some module information -module (myModuleName) -compile(export_all) qsort([]) -> ... This declares the module myModuleName and exports all its declarations.

Recommend


More recommend