- Concepts of Programming Languages

Metaprogramming

Instructor:

Learning Objectives

  • Combine parametric polymorphism and type bounds/subtyping relationships to let compiler execute algorithms
  • Limits of type inference

Example: Peano Arithmetic

  • Peano arithmetic: recursive definition of the natural numbers including 0

  • Base case 0, every other number is the successor of a Peano number

  • Ordinary recursive ADT

    1. {withdialog}
    2. enum Nat:
    3. case Zero
    4. case Succ(n: Nat)
    5. end Nat
    6. import Nat.*
    7. val _0 = Zero
    8. val _1 = Succ(_0)
    9. val _2 = Succ(_1)
    10. {exercise}
    11. // Exercise 1: Create an infinite stream of Peano numbers
    12. {solution}
    13. val peano =
    14. def peanoList(current: Nat) : LazyList[Nat] = current #:: peanoList(Succ(current))
    15. peanoList(Zero)
    16. end peano
    17. {endexercise}

Peano Arithmetic at Compile Time with Types

  • Base case is a class named _0

  • Every other number is a class Succ parameterized with another Peano number

    1. {withdialog}
    2. trait Nat
    3. class _0 extends Nat
    4. class Succ[A <: Nat] extends Nat
    5. type _1 = Succ[_0]
    6. type _2 = Succ[_1]
    7. // 1. Compile, what happens?
    8. // 2. Try comparing _0 == _0, _1 == _1, _0 < _1, what happens?
    9. // 3. Try instantiating _2, what happens?
    10. // 4. Try comparing new _0 == new _0, what happens?

Comparison

Operation < compares 2 natural numbers

  • 0 is smaller than a successor of any number, no number is smaller than 0
  • The successor of x is smaller than the successor of y if x is smaller than y
  • Ordinary code: function def <
  1. {withdialog}
  2. {dialogcode}
  3. enum Nat:
  4. case Zero
  5. case Succ(n: Nat)
  6. import Nat.*
  7. val _0 = Zero
  8. val _1 = Succ(_0)
  9. val _2 = Succ(_1)
  10. {enddialogcode}
  11. def <(a: Nat, b: Nat) : Boolean = (a, b) match
  12. case (Zero, Succ(_)) => true
  13. case (_, Zero) => false
  14. case (Succ(x), Succ(y)) => <(x, y)
  15. end <
  • <(_0, _1) evaluates to true
  • <(_1, _0) evaluates to false
  • As a type: interface trait <
  1. {withdialog}
  2. {dialogcode}
  3. // turn off worksheet mode
  4. trait Nat
  5. class _0 extends Nat
  6. class Succ[A <: Nat] extends Nat
  7. type _1 = Succ[_0]
  8. type _2 = Succ[_1]
  9. {enddialogcode}
  10. trait <[A <: Nat, B <: Nat]
  11. object < {
  12. given [B <: Nat] => <[_0, Succ[B]]
  13. given [A <: Nat, B <: Nat] => <[A, B] => <[Succ[A], Succ[B]]
  14. // helper method to summon an instance (a proof)
  15. def apply[A <: Nat, B <: Nat](using lt: <[A, B]) = lt
  16. }
  17. {dialogcode}
  18. object test:
  19. @main def main() =
  20. // 1. Try to compile val p = <[_0, _1], what happens?
  21. // 2. Try to compile val q = <[_1, _0], what happens?
  22. end main
  23. end test
  24. {enddialogcode}
  • Compiling <(_0, _1) succeeds
  • Compiling <(_1, _0) fails

Addition

Operation + takes 2 natural numbers and returns their sum, recursively

  • 0+0 is 0, 0+x is x, x+0 is x
  • Successor of x plus successor of y is the successor of the successor of x+y
  • Recursive function plus
  1. {withdialog}
  2. {dialogcode}
  3. enum Nat:
  4. case Zero
  5. case Succ(n: Nat)
  6. import Nat.*
  7. val _0 = Zero
  8. val _1 = Succ(_0)
  9. val _2 = Succ(_1)
  10. {enddialogcode}
  11. def plus(a: Nat, b: Nat) : Nat = (a, b) match
  12. case (Zero, Zero) => Zero
  13. case (Zero, x) => x
  14. case (x, Zero) => x
  15. case (Succ(x), Succ(y)) => Succ(Succ(plus(x, y)))
  16. end plus

Type plus checks additions

  1. trait plusP[A <: Nat, B <: Nat, R <: Nat]

given objects spawn checks

  1. {withdialog}
  2. {dialogcode}
  3. // turn off worksheet mode
  4. trait Nat
  5. class _0 extends Nat
  6. class Succ[A <: Nat] extends Nat
  7. type _1 = Succ[_0]
  8. type _2 = Succ[_1]
  9. trait plusP[A <: Nat, B <: Nat, R <: Nat]
  10. {enddialogcode}
  11. object plusP {
  12. given plusP[_0, _0, _0]
  13. given [B <: Nat] => plusP[_0, B, B]
  14. given [A <: Nat] => plusP[A, _0, A]
  15. given [A <: Nat, B <: Nat, S <: Nat] =>
  16. plusP[A, B, S] =>
  17. plusP[Succ[A], Succ[B], Succ[Succ[S]]]
  18. // public interface to check addition
  19. def apply[A <: Nat, B <: Nat, S <: Nat]
  20. (using p: plusP[A, B, S]): plusP[A, B, S] = p
  21. }
  22. {dialogcode}
  23. object test:
  24. @main def main() =
  25. // 1. Try to compile val p = plusP[_0, _1, _1], what happens?
  26. // 2. Try to compile val r = plusP[_1, _2, _2], what happens?
  27. // 3. Fix problems
  28. ???
  29. end main
  30. end test
  31. {enddialogcode}

Compute result

Addition: Compute Result

  • Prove/disprove conjectured result vs. compute result

Recursive function plus

Type plusP checks addition

  1. trait plusP[A <: Nat, B <: Nat, R <: Nat]

given objects spawn checks

  1. object plusP {
  2. given plusP[_0, _0, _0]
  3. given [B <: Nat] => plusP[_0, B, B]
  4. given [A <: Nat] => plusP[A, _0, A]
  5. given [A <: Nat, B <: Nat, S <: Nat]
  6. => plusP[A, B, S]
  7. => plusP[Succ[A], Succ[B], Succ[Succ[S]]]
  8. // public interface to check addition
  9. def apply[A <: Nat, B <: Nat, S <: Nat]
  10. (using p: plusP[A, B, S]) = p
  11. }

Type plus computes result

  1. trait plus[A <: Nat, B <: Nat] extends Nat:
  2. type Result <: Nat // deferred given
  3. end plus

given objects spawn computation

  1. {withdialog}
  2. {dialogcode}
  3. // turn off worksheet mode
  4. trait Nat
  5. class _0 extends Nat
  6. class Succ[A <: Nat] extends Nat
  7. type _1 = Succ[_0]
  8. type _2 = Succ[_1]
  9. trait plus[A <: Nat, B <: Nat] extends Nat:
  10. type Result <: Nat // deferred given
  11. end plus
  12. {enddialogcode}
  13. object plus {
  14. // base cases: 0+0=0, 0+B=0, A+0=A
  15. given plus[_0, _0] { type Result = _0 }
  16. given [B <: Nat] => plus[_0, B] { type Result = B }
  17. given [A <: Nat] => plus[A, _0] { type Result = A }
  18. // inductive case:
  19. // Succ[A] + Succ[B] = Succ[Succ[R]] if A + B = R
  20. given [A <: Nat, B <: Nat, R <: Nat]
  21. => (plus[A,B] { type Result = R })
  22. => plus[Succ[A], Succ[B]] { type Result = Succ[Succ[R]] }
  23. // public interface to compute addition
  24. def apply[A <: Nat, B <: Nat]
  25. (using p: plus[A, B]):
  26. (plus[A, B] { type Result = p.Result } ) = p
  27. }
  28. {dialogcode}
  29. object test:
  30. @main def main() =
  31. // 1. Try to compile val p = plus[_0, _1], what happens (hover over p to inspect type)?
  32. // 2. Try to compile val q = plus[_1, _2], what happens (hover over q to inspect type)?
  33. // 3. Try to compile val one: p.Result = new _2, what happens?
  34. // 4. Fix problems
  35. ???
  36. end main
  37. end test
  38. {enddialogcode}

Evaluate to BigInt

Traversal at runtime

  1. {withdialog}
  2. enum Nat:
  3. case Zero
  4. case Succ(n: Nat)
  5. import Nat.*
  6. val _0 = Zero
  7. val _1 = Succ(_0)
  8. val _2 = Succ(_1)
  9. def eval(n: Nat) : BigInt = n match
  10. case Zero => 0
  11. case Succ(i) => 1 + eval(i)
  12. end eval
  • Value computed at runtime

Type traversal at compile time

  1. {withdialog}
  2. trait Nat
  3. class _0 extends Nat
  4. class Succ[A <: Nat] extends Nat
  5. type _1 = Succ[_0]
  6. type _2 = Succ[_1]
  7. {dialogcode}
  8. trait plus[A <: Nat, B <: Nat] extends Nat:
  9. type Result <: Nat // deferred given
  10. end plus
  11. object plus {
  12. // base cases: 0+0=0, 0+B=0, A+0=A
  13. given plus[_0, _0] { type Result = _0 }
  14. given [B <: Nat] => plus[_0, B] { type Result = B }
  15. given [A <: Nat] => plus[A, _0] { type Result = A }
  16. // inductive case:
  17. // Succ[A] + Succ[B] = Succ[Succ[R]] if A + B = R
  18. given [A <: Nat, B <: Nat, R <: Nat]
  19. => (plus[A,B] { type Result = R })
  20. => plus[Succ[A], Succ[B]] { type Result = Succ[Succ[R]] }
  21. // public interface to compute addition
  22. def apply[A <: Nat, B <: Nat]
  23. (using p: plus[A, B]):
  24. (plus[A, B] { type Result = p.Result } ) = p
  25. }
  26. {enddialogcode}
  27. transparent inline def eval[N <: Nat]: BigInt =
  28. inline scala.compiletime.erasedValue[N] match
  29. case _: _0 => 0
  30. case _: Succ[i] => 1 + eval[i]
  31. end eval
  32. {dialogcode}
  33. object test:
  34. @main def main() =
  35. // 1. Try to compile val two = eval[_2], what happens (hover over two to inspect type)?
  36. // 2. Try to compile val _3 = plus[_1, _2]; val three = eval[_3.Result], what happens (hover over _3 and three to inspect types)?
  37. ???
  38. end main
  39. end test
  40. {enddialogcode}
  • Values computed in compiler
  • At runtime: lookup

Fibonacci Numbers

  1. {withdialog}
  2. {dialogcode}
  3. // deactivate worksheet
  4. trait Nat
  5. class _0 extends Nat
  6. class Succ[A <: Nat] extends Nat
  7. type _1 = Succ[_0]
  8. type _2 = Succ[_1]
  9. type _3 = Succ[_2]
  10. type _4 = Succ[_3]
  11. type _5 = Succ[_4]
  12. type _6 = Succ[_5]
  13. trait plus[A <: Nat, B <: Nat] extends Nat:
  14. type Result <: Nat // deferred given
  15. end plus
  16. object plus {
  17. // base cases: 0+0=0, 0+B=0, A+0=A
  18. given plus[_0, _0] { type Result = _0 }
  19. given [B <: Nat] => plus[_0, B] { type Result = B }
  20. given [A <: Nat] => plus[A, _0] { type Result = A }
  21. // inductive case:
  22. // Succ[A] + Succ[B] = Succ[Succ[R]] if A + B = R
  23. given [A <: Nat, B <: Nat, R <: Nat]
  24. => (plus[A,B] { type Result = R })
  25. => plus[Succ[A], Succ[B]] { type Result = Succ[Succ[R]] }
  26. // public interface to compute addition
  27. def apply[A <: Nat, B <: Nat]
  28. (using p: plus[A, B]):
  29. (plus[A, B] { type Result = p.Result } ) = p
  30. }
  31. transparent inline def eval[N <: Nat]: BigInt =
  32. inline scala.compiletime.erasedValue[N] match
  33. case _: _0 => 0
  34. case _: Succ[i] => 1 + eval[i]
  35. end eval
  36. {enddialogcode}
  37. trait fib[N <: Nat] extends Nat:
  38. type Result <: Nat
  39. end fib
  40. object fib {
  41. {exercise}
  42. // Exercise: implement base cases fib(0) = 0, fib(1) = 1
  43. {solution}
  44. given fib[_0] { type Result = _0 }
  45. given fib[_1] { type Result = _1 }
  46. {endexercise}
  47. {exercise}
  48. // Exercise: implement inductive case fib(Succ(Succ(N))) = fib(N) + fib(Succ(N))
  49. {solution}
  50. given [N <: Nat, R <: Nat]
  51. => (n: fib[N], sn: fib[Succ[N]], p: plus[n.Result, sn.Result] { type Result = R })
  52. => fib[Succ[Succ[N]]] { type Result = p.Result }
  53. {endexercise}
  54. // interface to get Fibonacci number
  55. def apply[N <: Nat](using p: fib[N]): (fib[N] { type Result = p.Result }) = p
  56. }
  57. {dialogcode}
  58. object test:
  59. @main def main() =
  60. val fib3 = fib[_3] // fib(3) = 2
  61. val fib4 = fib[_4] // fib(4) = 3
  62. val fib5 = fib[_5] // fib(5) = 5
  63. val fib6 = fib[_6] // fib(6) = 8
  64. val fib6Result = eval[fib6.Result]
  65. end main
  66. end test
  67. {enddialogcode}

Template Metaprogramming in C++

  • Uses compiler as interpreter with pure functional features

  • Evaluates templates to generate values and code

  • C++ templates can take values, not only types

  • Example: compile-time factorial

  1. template <int N>
  2. struct Factorial {
  3. static constexpr int value = N * Factorial<N-1>::value; // :: is static field access
  4. }
  5. // base case: template specialization
  6. template <>
  7. struct Factorial<0> {
  8. static constexpr int value = 0;
  9. }
  10. constexpr int result = Factorial<5>::value; // computes result at compile time

Type Constraints in C++

  • Modern (C++ 20) replaces some template metaprogramming with "concepts"

  • Concepts constrain type parameters, similar to type bounds in Scala

  • Example: a function that takes only numeric types

  1. #include <concepts>
  2. template <typename T>
  3. requires std::integral<T> || std::floating_point<T> // constrains type T
  4. T add(T a, T b) {
  5. return a + b;
  6. }

Summary

  • Metaprogramming uses compiler type inference to execute algorithms at compile time
  • Type inference performs proofs
  • Template expansion evaluates recursive template expressions
  • Shifts computation from runtime to compile-time (if no user input necessary)
  • Bloats compile time and size of binary

Scastie integration for live coding in slides

highlight.js consistent with Scastie (Solarized light theme)

Hidden modal dialogs

Copy code

Scastie embedded Scala editor

Modal dialogs