concepts of programming languages
play

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

Faculty of Science Information and Computing Sciences 1 Concepts of programming languages Lecture 9 Wouter Swierstra Faculty of Science Information and Computing Sciences 2 Talks advice language. Remember its the first time for


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

  2. Faculty of Science Information and Computing Sciences 2 Talks – advice language. Remember – it’s the first time for most people in the audience to read code in this language. of key points. ▶ Focus on what makes your language different or interesting. ▶ Giving small examples can really help give a feel for a new ▶ Go slow. Take the time to talk us through these examples. ▶ Don’t try to cover too much ground: instead try to convey a handful

  3. Faculty of Science Information and Computing Sciences 3 Last time Lisp/Sheme family. existing ones. languages in Racket or define our own Racket-dialects. ▶ Racket is a strict, dynamically typed functional language of the ▶ The syntax and language features are fairly minimalisticy. ▶ But this makes it very easy to manipulate code as data . ▶ Macros let us define new programming constructs in terms of ▶ By customizing the parser even further, we can embed other

  4. Faculty of Science Information and Computing Sciences 4 Metaprogramming in Racket > ( eval '( + (* 1 2) 3)) 6 associated value. ▶ Quoting can turn any piece of code into data ▶ Using eval we can interpret a quoted expression and compute the

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

  6. Faculty of Science Information and Computing Sciences 6 Two separate aspects We can tease apart two separate issues: These ideas pop up over and over again in different languages. Many dynamically typed languages borrow ideas from the Lisp-family of languages. ▶ How to inspect a piece of code? (Reflection or quotation) ▶ How to generate or run new code? (Metaprogramming)

  7. Faculty of Science Information and Computing Sciences 7 Javascript: object reflection Objects in Javascript are little more than a map from its attributes and methods to the associated values. Given any object o , we can iterate over all its methods and attributes: for ( var key in o) { console.log(key + " -> " + o[key]); } This lets us reflect and inspect the object structure.

  8. ▶ Illegal syntax: eval("2#2k-sa[2da") ▶ Reference to unbound variables eval("3 * z") ▶ Type errors: eval("1.toUpperCase()") ▶ Dynamic execution errors: eval("1/0") Faculty of Science Information and Computing Sciences 8 Javascript: eval As we saw previously, we can evaluate any string corresponding to a program fragment: var x = 3; var y = 7; var z = eval("x * y") Note that eval is incredibly unsafe! Question: In what ways can this fail?

  9. Faculty of Science Information and Computing Sciences 8 Javascript: eval As we saw previously, we can evaluate any string corresponding to a program fragment: var x = 3; var y = 7; var z = eval("x * y") Note that eval is incredibly unsafe! Question: In what ways can this fail? ▶ Illegal syntax: eval("2#2k-sa[2da") ▶ Reference to unbound variables eval("3 * z") ▶ Type errors: eval("1.toUpperCase()") ▶ Dynamic execution errors: eval("1/0")

  10. Faculty of Science Information and Computing Sciences 9 C# reflection In C# there are limited ways in which we can reflect the type of a piece of data: int i = 42; System.Type type = i.GetType(); System.Console.WriteLine(type); This prints System.Int32 . Similarly to Javascript, we can request the attributes and functions of a class and iterate over these. But what about generating new code?

  11. Faculty of Science Information and Computing Sciences 10 Metaprogramming in C# There is no way to generate new C# ASTs… But you can emit ‘machine code’ instructions dynamically. All .NET languages, including C#, F#, and Visual Basic, are compiled to the Common Intermediate Language (CIL). This is stack-based assembly language that is either executed natively or run on a virtual machine.

  12. Faculty of Science Information and Computing Sciences 11 Example: Emitting CIL instructions A simple call to WriteLine : Console.WriteLine("Hello World"); Becomes…

  13. Faculty of Science .GetMethod( } generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Call, methodInfo); new Type[]{ typeof (string)}, null ); BindingFlags.Public | BindingFlags.Static, null , "WriteLine", With another 100 lines of boilerplate… Information and Computing Sciences generator.Emit(OpCodes.Ldstr, text); ILGenerator generator = builder.GetILGenerator(); { , string text) private void EmitCode(MethodBuilder builder 12 MethodInfo methodInfo = typeof (System.Console)

  14. Faculty of Science Information and Computing Sciences 13 Metaprogramming in C# We can generate new bytecode and inspect existing classes… But it is a good example of a different style of metaprogramming where the generating language (C#) and generated language (CIL) are different. ▶ this doesn’t scale very well to larger examples; ▶ emitting CIL codes is essentially untyped and unsafe.

  15. Faculty of Science Information and Computing Sciences 14 Metaprogramming in other languages More advanced systems – such as Scala’s Lightweight modular staging will be presented after the Christmas break. What about metaprogramming in a strongly typed language such as Haskell?

  16. Faculty of Science Information and Computing Sciences 15 Template Haskell There is a language extension, Template Haskell , that provides metaprogramming support for Haskell. This defines support for quotating code into data and splicing generated code back into your program.

  17. Faculty of Science Information and Computing Sciences 16 Template Haskell import Language.Haskell.TH three :: Int three = 1 + 2 threeQ :: ExpQ threeQ = [| 1 + 2 |] Rather than Racket’s quote function, we can enclose expressions in quotation brackets [| ... |] . This turns code into a quoted expression, ExprQ .

  18. Faculty of Science Information and Computing Sciences 17 Unquoting > three 3 > $threeQ 3 We can splice an expression e back into our program by writing $e (note the lack of space between $ and e ) This runs the associated metaprogram and replaces the occurrence $e with its result.

  19. Faculty of Science Information and Computing Sciences 18 Inspecting code What happens when we quote an expression? > runQ [| 1 + 2 |] InfixE (Just (LitE (IntegerL 1))) (VarE GHC.Num.+) (Just (LitE (IntegerL 2))) Template Haskell defines a data type Exp corresponding to Haskell expressions. The type ExpQ is a synonym for Q Exp – a value of type Exp in the quotation monad Q . The runQ function returns the quoted expression associated with ExpQ .

  20. Faculty of Science Information and Computing Sciences 18 Inspecting code What happens when we quote an expression? > runQ [| 1 + 2 |] InfixE (Just (LitE (IntegerL 1))) (VarE GHC.Num.+) (Just (LitE (IntegerL 2))) Template Haskell defines a data type Exp corresponding to Haskell expressions. The type ExpQ is a synonym for Q Exp – a value of type Exp in the quotation monad Q . The runQ function returns the quoted expression associated with ExpQ .

  21. Faculty of Science | AppE Exp Exp -- application Not to mention unboxed tuples, record updates, record construction, ... | CondE Exp Exp Exp -- if-then-else | LetE [Dec] Exp -- let | TupE [Exp] -- tuples | LamE [Pat] Exp -- lambdas | ParensE Exp -- parentheses -- literals such as 5 or 'c' Information and Computing Sciences | LitE Lit -- constructors | ConE Name VarE Name -- variables data Exp = The Exp data type 19 lists, list comprehensions,…

  22. Faculty of Science Information and Computing Sciences 20 Template Haskell: programming with expressions incr : Int -> Int incr x = x + 1 incrE : Exp -> Exp incrE e = AppE (VarE 'incr) e) new expression by passing its argument to incr . 'incr ▶ The first incr function increments a number; ▶ The second takes a quoted expression as argument and builds a ▶ We can ‘quote’ variable names with the prefix quotation mark

  23. Faculty of Science Information and Computing Sciences 21 Template Haskell: programming with expressions -- Let x = [| 1 + 2 |] x : Exp x = InfixE (Just (LitE (IntegerL 1)))... y : Int y = $(incrE x) Question: What is the result of evaulating y ? But this might go wrong…

  24. Faculty of Science Information and Computing Sciences 21 Template Haskell: programming with expressions -- Let x = [| 1 + 2 |] x : Exp x = InfixE (Just (LitE (IntegerL 1)))... y : Int y = $(incrE x) Question: What is the result of evaulating y ? But this might go wrong…

  25. Faculty of Science Information and Computing Sciences 22 Typing Template Haskell What happens when we create ill-typed expressions? -- Let x = [| "Hello world" |] x : Exp x = LitE (StringL "Hello World") y : Int y = $(incrE x) Question: What is the result of this program? We get a type error: No instance for (Num [Char]) arising from a use of ‘incr’ In the expression: incr "Hello World"...

Recommend


More recommend