LMU ☀️ CMSI 585
PROGRAMMING LANGUAGE FOUNDATIONS
Practice Problems

Here are some long-form exercises for you to work on to improve understanding and retention of course content. Short-form recall questions, designed to be part of your spaced-repetition learning regimen, can be found at the bottom of each page of course notes.

Computer Science Foundations

  1. Though machines can use language, process information, and reason at speeds that far exceed our own, there is one thing we can do that machines cannot. What is it?
  2. List five major contributions from Claude Shannon’s 1948 paper A Mathematical Theory of Communication.
  3. Write the number $3385228$ in Egyptian numerals.
  4. Name two algorithms written on the ancient Babylonian clay tablets, and two from the Rhind Mathematical Papyrus.
  5. What did Kurt Gödel actually prove that made him so famous?
  6. Name four major contributions of Alan Turing’s On Computable Numbers paper.
  7. What distinguishes computation from automation? Give examples of each.
  8. Name two contributions of Ada Lovelace’s Notes piece of 1843.
  9. Implement the sum of even squares function in at least five languages not covered in the course notes.
  10. What is logic and what is it concerned with?

Theories of Computation

  1. Name the four major theories of computation and their primary focus.
  2. What finiteness assumptions do we make about models of computation. Why are they necessary?
  3. Give a Turing Machine, both as a table and as a state diagram, that writes three asterisk symbols immediately to the left of the input and three asterisks immediately to the right of the input.
  4. Express the Church-Turing thesis in your own words. Why is it a thesis and not a law or theory?
  5. In class, we saw a demonstration of the undecidability of the halting problem using JavaScript. Reconstruct the argument using pictures.
  6. What are $\textsf{P}$ and $\textsf{NP}$ in computational complexity theory? Give example problems from each complexity class.

Logic

  1. Define deduction, induction, and abduction in your own words.
  2. Which of the following denote the sentence “All psychology majors like almond butter” (where $Px$ means $x$ is a psychology major, $a$ is almond butter, and $Lxy$ means $x$ likes $y$)? Select all that apply.
    • $\exists x. (Px \land Lxa)$
    • $\neg \exists x. (Px \land \neg Lxa)$
    • $\exists x. (Px \supset Lxa)$
    • $\forall x. (Px \supset Lxa)$
    • $\forall x. (Px \land Lxa)$
  3. Which of the following denote the sentence “Pablo or Queen found the key” (where $p$ is Pablo, $q$ is Queen, and $Fxy$ means $x$ found $y$, and $k$ is the key)? Select all that apply.
    • $Fpk \lor Fqk$
    • $F(p \lor q)k$
  4. Which of the following denote the sentence “Alice knows the program has never run” (where $a$ is Alice, $p$ is the program, and $Rx$ means $x$ is running? Select all that apply.
    • $\mathscr{K}_a\mathbf{H}\neg R p$
    • $\neg \mathscr{K}_a\mathbf{P}\neg R p$
    • $\mathscr{K}_a\lozenge R p$
    • $\mathscr{K}_a\neg \mathbf{F}\Box \neg R p$
    • $\mathscr{K}_a\neg\mathbf{P} R p$
  5. Which is the correct logic formulation of the statement “Caitlin once knew that every citizen of Moruxiliamisqia is not permitted to worship the queen of Zaunover.” (For this problem, you will have to guess what the variable and predicate abbreviations mean.)
    • $Kc(\mathbf{P}c\Box\forall p. Cmp\supset W(qz))$
    • $\mathbf{P}\mathscr{K}_c(\forall p.Cmp\supset \neg\mathscr{P}Wp(qz))$
    • $\mathbf{P}\mathscr{K}_c(\exists p.Cmp\land \mathscr{O}p\neg W(qz))$
    • $\mathbf{P}\mathscr{K}_c(\exists p.Cmp\land \mathscr{O}p\neg Wpq)$
    • $\mathbf{P}\mathscr{K}_c(\forall p.Cmp\supset \neg\lozenge\neg Wpq)$
  6. Translate the following sentences into logical notation.
    1. If you don't leave now, you will not win the prize.
    2. Ani or her sisters might have been late.
    3. Some dogs like cats who live in the capital of Turkey.
    4. The person who won the race prefers orange juice to tea.
    5. $3$ will never be greater than $7$.
    6. Not every odd number is greater than its own square.
    7. Something evil caused all evil things except itself.
    8. Some day, it will be possible that all players will have the same score.
    9. War is peace, freedom is slavery, and ignorance is strength.
    10. All that was once true will someday necessarily be forever false.
    11. 75% of Creation Museum attendees believe Jesus rode dinosaurs to church.
    12. Your best friend must help you out of every jam.
    13. It is not okay to lie.
    14. You are allowed to not believe that everyone is telling the truth.
    15. If you know what is on the test, you will probably get a good score.
    16. Juliet once believed that Socrates was real, but no longer does.
    17. Everyone who attended Lollapalooza in 2025 was more likely to have a good time if they took public transportation or carpooled.
    18. No one knows if Goldbach’s Conjecture is true.
    19. Tanya wrote her book about logic swiftly and eloquently.
    20. The set of all rational numbers less than $\sqrt{2}$ has no greatest element.
    21. Romeo doesn’t know that he is doomed.
  7. Give three ambiguous English sentences that could be interpreted as either $A \supset \Box B$ or $\Box (A \supset B)$, such that one of the formulae is false in our world and the other true. Then give renderings of each formula in English that would not be ambiguous.
  8. For each of the following, identify whether these longhand (non-single-letter) parts of formulas are (1) most likely a function, (2) most likely a predicate, or (3) pretty much 50-50 ambiguous.
    1. $\mathit{even}$
    2. $\mathit{oldestSister}$
    3. $\mathit{isSisterOf}$
    4. $\mathit{plus}$
    5. $\mathit{lessThan}$
    6. $\mathit{hasSiblings}$
    7. $\mathit{square}$
    8. $\mathit{french}$
  9. For each of the following, identify whether it is a term or a formula or neither.
    1. $\mathit{Romeo}$
    2. $\mathit{Likes}$
    3. $\mathit{Likes}\,(\mathit{Romeo}, \mathit{Juliet})$
    4. $\mathit{WeddingOfficiator}\,(\mathit{Romeo}, \mathit{Juliet})$
    5. $\exists c. \Box \textbf{G} (Ac)$
    6. $\iota c. \Box \textbf{G} (Ac)$
  10. A ________________ can be thought of as a predicate with no arguments.
  11. A ________________ can be thought of as a function with no arguments.
  12. Why is it that in predicate logic, you actually do not need functions?
  13. For each of the following, rewrite in its fully parenthesized form. Do not let the spacing fool you. Stick to the precedence and associativity rules.
    1. $\neg A \lor B \supset C \land D \equiv E \land \neg F$
    2. $A \lor B \lor B \lor \forall x. \exists y. A\,x \lor B$
    3. $A \supset B \supset (\exists x. \Box \lozenge C(f\;g\;x)) \lor \forall y. \neg D\,y$
    4. $\exists x. P x \;\land\; \exists y. Q y$
  14. For each of the following, circle the free occurrences of variables. Do not let the spacing fool you. Stick to the precedence and associativity rules.
    1. $\forall x. P\,x \lor Q\,y \;\supset\; \exists y. P\,y \land Q\,x$
    2. $\forall x. \exists y. P\,x\,y \lor Q\,y$
    3. $\neg \forall x. P\,x \;\land Q\,x$
    4. $Q\,R\,t \supset (\iota t. P\,t) \gt t$
    5. $\mathscr{K}_xHx \lor x = t$
  15. Evaluate each of the following. As always, be careful about parentheses and variable renaming:
    1. $(P\,x)[x \mapsto y]$
    2. $(P\,x)[x \mapsto f\,x]$
    3. $(\forall x. P\,x\,y)[x \mapsto y]$
    4. $(\forall y. P\,x\,y)[x \mapsto y]$
    5. $(x \land \exists x. P\,x)[x \mapsto y]$
    6. $(\forall x. \exists y. P\,x\,y)[x \mapsto y]$
    7. $(\forall x. \exists y. P\,(f\,x\,y))[x \mapsto f\,y]$
    8. $(\forall f. \exists y. P\,(f\,x\,y))[x \mapsto f\,y]$
  16. Is it the case that, when substituting $t$ for $x$ in $A$, then variable renaming is necessary if and only if $t$ is not free for $x$ in $A$? Why or why not?
  17. Write judgments for the following:
    1. Truth is necessarily true.
    2. Assuming that Mercury is in retrograde, it is possible that you will have a bad day.
    3. Something is good assuming not everything is bad.
    4. Assuming $x$ has type integer and $y$ has type natural, then $\frac{x}{y+1}$ has type rational.
  18. What is this logical argument stating, in plain English? $$ \frac{\forall P.\,P \supset \lozenge \mathscr{K}P}{\Box \mathscr{K}P} $$
  19. Distinguish the two forms of description operators, the $\iota$ of Church and the $\varepsilon$ of Hilbert.
  20. Is the Catuṣkoṭi bivalent? Why or why not?
  21. How does a classical logic handle, or not handle, the case of a wish grantee saying to a Genie “I wish this wish not be granted“? How is it handled in a multi-valued logic?
  22. What operators appear in predicate logic but not propositional logic?
  23. Translate each of the eight syllogistic forms into predicate logic.
  24. Why might the commutativity of conjunction, a pillar of classical logic, be controversial in non-classical contexts?
  25. What are the three main things you cannot do in intuitionistic logic that you can do in classical logic?
  26. Argue why $\mathscr{O}$ and $\mathscr{P}$ are duals in (classical) deontic logic.
  27. What is the difference between epistemic and doxastic logic?
  28. Are the (classical) temporal operators $\textbf{F}$ and $\textbf{G}$ duals of each other? Why or why not?
  29. In what sense is a formal system just a part of a logical system? What is it missing that a logical system provides?
  30. Given the interpretation $\{p: \textrm{false}, q: \textrm{true}, r: \textrm{false}\}$, compute the truth value of the following formulas:
    1. $p \lor q$
    2. $p \land q$
    3. $p \supset q$
    4. $\neg p$
    5. $\neg \neg \neg \neg r$
    6. $(p \lor q) \land r \equiv (\neg p \land q) \land \neg r$
    7. $\neg p \lor q \equiv \neg q \supset \neg p$
    8. $r \lor p \equiv p \supset r \land \neg r \lor T \land (r \equiv p)$
  31. Complete the following truth table for propositional logic.
    $A$$B$$\;\;\neg A\;\;$$\;\;\neg B\;\;$$A \land B$$A \lor B$$A \supset B$$A \equiv B$
    truetrue      
    truefalse      
    falsetrue      
    falsefalse      
  32. Complete the following truth table for propositional logic, where $A \uparrow B$ means $A$ NAND $B$, $A \downarrow B$ means $A$ NOR $B$, and $A \oplus B$ means $A$ XOR $B$, and $A \subset B$ means $B$ materially implies $A$.
    $A$$B$$A \uparrow B$$A \downarrow B$$A \oplus B$$A \subset B$
    truetrue    
    truefalse    
    falsetrue    
    falsefalse    
  33. Complete the following truth table for $K_3$.
    $A$$B$$\;\;\neg A\;\;$$\;\;\neg B\;\;$$A \land B$$A \lor B$$A \supset B$$A \equiv B$
    truetrue      
    truefalse      
    trueother      
    falsetrue      
    falsefalse      
    falseother      
    othertrue      
    otherfalse      
    otherother      
  34. Complete the following truth table for $Ł_3$.
    $A$$B$$\;\;\neg A\;\;$$\;\;\neg B\;\;$$A \land B$$A \lor B$$A \supset B$$A \equiv B$
    truetrue      
    truefalse      
    trueother      
    falsetrue      
    falsefalse      
    falseother      
    othertrue      
    otherfalse      
    otherother      
  35. For each of propositional logic, first order predicate logic, and higher order logic, give three formulae: one that is valid (a tautology), one that is satisfiable but not valid (a contingency), and one that is unsatisfiable (a contradiction).
  36. Describe the different notions of consistency in logic. Include at least (1) consistency with respect to negation and (2) absolute consistency.
  37. How do soundness and consistency differ? Can you have a logic that is sound but inconsistent? Unsound but consistent?
  38. Suppose that you had a classical, bivalent logistic system powerful enough to express statements about the provability of its own formulas, for example, “This formula is not provable” or equivalently “I am not provable.” Show that such a system, if consistent, must be incomplete, and if complete, must be inconsistent.

Mathematics

  1. Summarize the Foundational Crisis of Mathematics in a short 3-5 sentence paragraph.
  2. For each of the three schools of thought—Logicism, Formalism, and Intuitionism—summarize their main ideas in a short paragraph. Include the big players in each.
  3. Simplify each of the following as much as possible without introducing any roundoff errors. Your simplified expressions must still be exact:
    1. $3 \uparrow \uparrow 2$
    2. $2 \uparrow \uparrow \uparrow 3$
    3. $790912853^{3892359} \;\mathtt{mod}\; 90277$
    4. $(3 + 2i - 8k) \times (8i + 2j -k)$   —quaternion multiplication
    5. $\log_8{2^{333333333}}$
  4. What is $4 \uparrow\uparrow 3$ exactly? (Compute with Python or JavaScript, don’t try this by hand or on a calculator.)
  5. What is $5 \uparrow\uparrow 2$ exactly?
  6. $3\uparrow\uparrow\uparrow 3$ is:
    • $(3\uparrow\uparrow 3)\uparrow 3$
    • Larger than the number of cubic nanometers in the observable universe
    • $3\uparrow\uparrow (3\uparrow 3)$
    • $7625597484987$
    • $3^{7625597484987}$
  7. Let $S = \{a,b,c\}$. Which if the following are true (according to the notation used in the course notes)?
    • The elements of $S^*$ can be enumerated like so: $\{(),a,b,c,(a,a),(a,b),(a,c),(b,a),(b,b) \ldots\}$
    • $\{ w\in S^* \mid \exists x\in S^*. w=ax\} = S^* \setminus \varnothing$
    • $S \times S$ has $3!$ (three factorial) elements
    • $S^1 = S$
    • $(a,b,b,a) \in S^* \cup S^4$
  8. Let $A = \{1, 2, 3\}$ and $R \subseteq A \times A = \{ (2,2), (2,3), (3,2), (1,1)\}$. Which of the following are true?
    • $R$ is reflexive
    • $R$ is irreflexive
    • $R$ is symmetric
    • $R$ is asymmetric
    • $R$ is antisymmetric

Set Theory

  1. What is the reason for the restriction to pairwise disjoint sets in Zermelo’s original formulation of the Axiom of Choice?
    Without the restriction, $C$ could accidentally hit two elements of some $A$ that it shares with a different member of $\mathfrak{A}$, and the exactly one guarantee can fail outright. To see this, note that no such $C$ exists for $\{\{1,2\},\{2,3\},\{1,3\}\}$.
  2. Mark each of the following as true or false:
    1. $\varnothing \in \varnothing$
    2. $\varnothing \in \{ \varnothing \}$
    3. $\varnothing \subseteq \varnothing$
    4. $\varnothing \subseteq \{ \varnothing \}$
    5. $\{ x, y \} \subseteq \mathcal{P}(\{ x, y, \{x,y\} \})$
    6. $\mathcal{P}(\varnothing) = \{ \varnothing \}$
    7. $\{ x, y, z\}^3 - \{ s \;\mid\; |s| \leq 3 \} \neq \varnothing$
    8. $\bigcup \{ \mathbb{N}, \mathbb{B}, \mathbb{Q} \} - \mathbb{B} = \mathbb{Q}$
    9. $(9, 3, F) \in \mathbb{Z} \times \mathbb{B} \times \mathbb{R} \wedge (1,2)\!\downarrow\!1 = 2$
    10. $|\mathcal{P}(\{a,b,c\}) - \mathcal{P}(\{a,b\})| = 5$
  3. Show that the modern phrasing of the Axiom of Choice, which states the existence of a choice function that selects one element from each set does not require pairwise disjoint sets.
  4. Is the intersection of two partial orders a partial order? If so, prove it. If not, give a counterexample.

Type Theory

  1. Given the function $f = \lambda x_{\textsf{Nat}}. 2x$, evaluate:
    1. $f^3 2$
    2. $f^{-1} 64$
    3. $|f^*|$
    4. $\textsf{let}\;g=\lambda x_{\textsf{Nat}}. x\,4 \;\textsf{in}\; f[2\mapsto g(f)](2)$
    5. $(\lambda x_{\textsf{Nat}}. \textsf{if}\;x \lt 5\;\textsf{then}\;f^2\;\textsf{else}\;f^{10})3$
  2. Given $f = \lambda x_{\textsf{Int}}. x^2$, which of the following are true?
    • $f^3(5) = 390625$
    • $f^0(3) = 1$
    • $f(f(x)) = f^2(x)$
    • $f(x)^2 = f^2(x)$
    • $f^{-1}(100) = -10$
  3. Let $f = \lambda x_{\textsf{Int}}. 2x + 1$ and $g = \lambda x_{\textsf{Int}}. \lambda y. 3xy$. Evaluate each of the following:
    1. $f^5(20)$
    2. $f \circ f$
    3. $g(f[3\mapsto 5](3) - f[3\mapsto 5](2))$
    4. $(\lambda w. (f^{-1}(71), w(5), w(1)))(f \circ (g\:1)) $
  4. Is this a valid type definition? Why or why not?
    $\dfrac{x: \textsf{Ghost}}{x: \textsf{Ghost}}$
  5. Explain how a violation of strict positivity in an inductive type definition fails to define a consistent type.
  6. Give the number of inhabitants of each of the following types:
    1. $\textsf{Nat}$
    2. $\textsf{Bool}$
    3. $\textsf{Unit}$
    4. $\textsf{Void}$
    5. $\textsf{Nat} + \textsf{Bool}$
    6. $\textsf{Nat} + \textsf{Unit}$
    7. $\textsf{Nat} + \textsf{Void}$
    8. $\textsf{Bool} + \textsf{Unit}$
    9. $\textsf{Bool} + \textsf{Void}$
    10. $\textsf{Unit} + \textsf{Void}$
    11. $\textsf{Nat} \times \textsf{Bool}$
    12. $\textsf{Nat} \times \textsf{Unit}$
    13. $\textsf{Nat} \times \textsf{Void}$
    14. $\textsf{Bool} \times \textsf{Unit}$
    15. $\textsf{Bool} \times \textsf{Void}$
    16. $\textsf{Unit} \times \textsf{Void}$
  7. Give the number of inhabitants of each of the following types:
    1. $\textsf{Nat} \to \textsf{Nat}$
    2. $\textsf{Nat} \to \textsf{Bool}$
    3. $\textsf{Nat} \to \textsf{Unit}$
    4. $\textsf{Nat} \to \textsf{Void}$
    5. $\textsf{Bool} \to \textsf{Nat}$
    6. $\textsf{Bool} \to \textsf{Bool}$
    7. $\textsf{Bool} \to \textsf{Unit}$
    8. $\textsf{Bool} \to \textsf{Void}$
    9. $\textsf{Unit} \to \textsf{Nat}$
    10. $\textsf{Unit} \to \textsf{Bool}$
    11. $\textsf{Unit} \to \textsf{Unit}$
    12. $\textsf{Unit} \to \textsf{Void}$
    13. $\textsf{Void} \to \textsf{Nat}$
    14. $\textsf{Void} \to \textsf{Bool}$
    15. $\textsf{Void} \to \textsf{Unit}$
    16. $\textsf{Void} \to \textsf{Void}$
  8. Give the number of inhabitants of each of the following types:
    1. $(\textsf{Nat} \to \textsf{Nat}) \to \textsf{Nat}$
    2. $(\textsf{Nat} \to \textsf{Bool}) \to \textsf{Nat}$
    3. $(\textsf{Nat} \to \textsf{Unit}) \to \textsf{Nat}$
    4. $(\textsf{Nat} \to \textsf{Void}) \to \textsf{Nat}$
    5. $\textsf{Nat} \to (\textsf{Nat} \to \textsf{Nat})$
    6. $\textsf{Nat} \to (\textsf{Bool} \to \textsf{Nat})$
    7. $\textsf{Nat} \to (\textsf{Unit} \to \textsf{Nat})$
    8. $\textsf{Nat} \to (\textsf{Void} \to \textsf{Nat})$
    9. $((\textsf{Nat} \to \textsf{Nat}) \to \textsf{Nat}) \to \textsf{Nat}$
  9. What was the first thing Haskell Curry noticed that is now part of the Curry-Howard Correspondence? What was the first thing William Howard noticed?
  10. Show the correspondence between logical conjunction and product types in type theory.
  11. Show the correspondence between logical disjunction and sum types in type theory.
  12. Show the correspondence between logical material implication and function types in type theory.
  13. Show the correspondence between logical universal quantification and dependent function types in type theory.
  14. Show the correspondence between logical existential quantification and dependent pair types in type theory.
  15. Show the correspondence between the logical falsum and the empty type ($\textsf{Void}$) in type theory.
  16. Show the correspondence between the logical verum (truth) and the unit type ($\textsf{Unit}$) in type theory.

Lambda Calculus

  1. Circle the free variable occurrences in the following lambda expression:

    $λx.(λx.\;x\;y)\;(λz.\;(x\;λw.\;w)\;(z\;λp.\;p)\;x)\;z$

  2. Reduce each of the following expressions as far as you can go, or, if they cannot be reduced, say what the problem is. Assume that $\textsf{plus}$ and $\textsf{square}$ are the usual arithmetic operators.
    1. $(\lambda f. \lambda g. g\,f)\;\textsf{plus}\;3\;\textsf{square}$
    2. $(\lambda f. \lambda g. g\;f) (\textsf{plus}\;3) (\lambda h. \lambda x. h(h\;x)) 5$
    3. $(\lambda x. (x\,x))(\lambda x. (x\,x))$
    4. $(\lambda x. \neg(x\,x))(\lambda x. \neg(x\,x))$
    5. $(\lambda x. \lambda y. \textsf{plus}\; x\; y)(\textsf{sqrt}\; y)$
    6. $(\lambda g. \lambda f. g^3(f\;x))\;\textsf{square}\;(\lambda x. x)$
  3. Which of the following lambda-expressions is abbreviated by let y = 3 * x in sqrt(21 + y)?
    • λy. λx. (times 3 (sqrt (plus 21 y)))
    • λx. λy. times 3 (sqrt (plus 21 y))
    • (λy. (sqrt (plus 21 y))) (times 3 x)
    • sqrt(21 + 3 * x)
    • (λy. (times 3 x)) (sqrt (plus 21 y))

Language Theory

  1. What language does the following generative grammar define?
      s → s "a" s "b" s
        | s "b" s "a" s
        | 𝜀
    
    • $\{ a^nb^n \mid n \geq 0 \} \cup \{ b^na^n \mid n \geq 0 \}$
    • $\{ w \in \{a,b\}^* \mid w \textrm{ has the same number of $a$'s and $b$'s} \}$
    • $\{ a,b \}^*$
    • Palindromes over $\{ a,b \}$
  2. What language does the following generative grammar define?
      s → x*
      x → "a" e | "b" x x
      e → ("a" y | "b" x)?
      y → "b" e | "a" y y
    
    • Palindromes over $a$ and $b$
    • Arbitrary sequences of $a$'s and $b$'s
    • The language of all strings with more $a$'s than $b$'s
    • The language of all strings with equal number of $a$'s and $b$'s
  3. Give generative grammars for the following mini-languages:
    1. Odd-length palindromes of strings made from Unicode letters only
    2. All strings over the alphabet ${a, b, c, d, e}$ where the symbols are in decreasing alphabetic order
    3. $\{ 0^i1^j2^k \mid i=j \vee j=k \}$
    4. $\{ w \in \{0,1\}^* \mid w \textrm{ does not contain the substring 000} \}$
    5. $\{ w \in \{a,b\}^* \mid w \textrm{ has twice as many $a$'s as $b$'s} \}$
    6. $\{ a^nb^na^nb^n \mid n \geq 0 \}$
  4. What does it mean for a grammar to be ambiguous?
    • A parser for the grammar does not need to backtrack
    • A string in the language accepted by the grammar can be derived multiple ways
    • A string in the language accepted by the grammar has two parse trees
    • The grammar has multiple ways to represent characters

Programming Language Elements

  1. ...
  2. ...

Syntax

  1. Distinguish the terms grammar and syntax.
  2. Why do grammars make a distinction between lexical and phrase categories?
    • To make tokenization faster
    • To simplify retargetability
    • To simplify semantic analysis, because certain simple categories can be resolved earlier
    • To reduce the number of times whitespace and comments must explicitly appear in the grammar
  3. For the expression next(page).footer[2], give (1) the sequence of Unicode Characters, and (2) the token sequence. Assume any reasonable mainstream programming language with function calls, member access, and array indexing.
  4. Which of the following does not capture the idea that “a block is a sequence of one or more statements, each terminated by a semicolon”?
    • Block = (Statement ";")+
    • Block = NonEmptyListOf ";"?
    • Block = Statement (";" Statement)* ";"
  5. A grammar has the rule Exp4 -> Exp5 ("+" Exp5)*. Which of the following best describes the + operator?
    • It is left associative
    • It is right associative
    • It is non-associative
    • Its associativity is indeterminate
  6. Give a grammar fragment for a programming language syntax in which the logical operators "&&" and "||" have the same precedence but do not associate with each other. That is we want to make "A&&B||C" a syntax error, but "A||B||C" should be ok.
  7. In Lisp and its dialects, most of the arithmetic operators are defined to take two or more arguments, rather than strictly two. Thus (* 2 3 4 5) evaluates to 120, and (– 16 9 4) evaluates to 3. Show that parentheses are necessary to disambiguate arithmetic expressions in Lisp (in other words, give an example of an expression whose meaning is unclear when parentheses are removed). Why then, does the author Michael L. Scott say in his book that “Issues of precedence and associativity do not arise with prefix or postfix notation?” Reword this claim to make explicit the hidden assumption.
    Scott is assuming all operators have a fixed arity. What he meant to say was: “Issues of precedence and associativity do not arise with prefix or postfix notation assuming all operators have fixed arity.”
  8. Draw the concrete syntax tree for the expression 8 * (13 + 5), assuming a grammar with variables named Expression, Term, Factor, Primary, and intlit.
          Expression
              |
            Term
          /   |   \
      Term    *    Factor
        |       /    |     \
     Factor   (  Expression  )
        |         /    |  \
    Primary Expression +  Term
        |         |         |
     intlit     Term      Factor
                  |         |
               Factor    Primary
                  |         |
               Primary    intlit
                  |
               intlit
    
  9. Draw the abstract syntax tree for the expression 8 * (13 + 5).
        *
      /   \
    8      +
          /  \
        13    5
    
  10. Draw the abstract syntax tree for the expression f(x).a[2]
  11. Give the abstract syntax tree for the following C fragment:
    if (x) while (0) f(x); else if (y <- x) break;
    
  12. Give the abstract syntax tree for the following C++ expression
    (a = 3) >= m >= ! & 4 * ~ 6 || y %= 7 ^ 6 & p
    
  13. Give the abstract syntax for the following C fragment:
    int f(int x, int y) {
       return y,x?a.p[5]+=7&!y<x<y+++x||+x|*&y-~--y*x^y:0;
    }
    
  14. Give the abstract syntax for the following C fragment:
    printf("%08X %#16.7e\n",x---a.p[9]^x|*&p&1,*((float*)&x));
    
  15. Give the abstract syntax tree for the following C expression:
    p <<* a*7 &- 3 &x,1
    
  16. Give the abstract syntax tree for the following Perl fragment (die and split are both list operators):
    die "\n">>1,~\$$x^split @a,3|x**$;=>%p&&&p;
    
  17. For the following complete JavaScript program, draw the AST, using the level of detail we used during class. You can use my JavaScript AST explorer to guide you and check your work, but remember, the drawing you need to produce for full credit will be far less verbose than the explorer’s output (which is based on a third-party parser).
    import x from "x"
    console.log(93.8 * {x} << x.r[z])
    
  18. Give an analytic grammar (you may use either the analytic grammar notation from class or an Ohm grammar) for a little language with integer-valued variables, assignment statements, while statements, read and write statements, and the usual arithmetic, relational operators, such that type checking between integers and booleans is done in the syntax. Hint: you will need separate sets of variables for arithmetic and logical expressions.
    Program   =  Block
    Block     =  (Stmt ";")+
    Stmt      =  Id "=" Exp
              | read Id ("," Id)*
              | write Exp ("," Exp)*
              | while BoolExp loop Block end
    Exp       =  Exp1 (("+" | "-") Exp1)*
    Exp1      =  Exp2 (("*" | "/") Exp2)*
    Exp2      =  "-"? Exp3
    Exp3      =  intlit | id | "(" Exp ")"
    BoolExp   =  BoolExp1 (or BoolExp1)*
    BoolExp1  =  BoolExp2 (and BoolExp2)*
    BoolExp2  =  Exp (("<" | "<=" | "==" | "!=" | ">=" | ">") Exp)?
              | BoolExp3
    BoolExp3  =  not? BoolExp4
    BoolExp4  =  true | false | "(" BoolExp ")"
    
  19. Here is an abstract syntax for a small language:
      n: Nml
      i: Ide
      e: Exp = n | i | e + e
      s: Stm = i := e | print e
      b: Block = s+
      p: Pro = program b
    

    and here is a purported alternative syntax for the language:

      n: Nml
      i: Ide
      e: Exp = n | i | e + e
      s: Stm = i := e | print e | s s
      p: Pro = program s
    
    Which of the following statements are true?
    • The alternative syntax can lead to deeper (taller) syntax trees
    • The alternative syntax is not an alternative at all, because it defines a completely different language
    • The semantic rules can be somewhat simpler in the alternative syntax, since we won't have to use "..." or those "1 to n" notations anywhere for statements
    • The semantic rules for the alternative syntax will be much more complex
  20. For the programming language fragment described as follows:

    The lowest precedence operators are the binary unless, if while, and until operators, which are non-associative. The next lowest precedence operators are the logical binary operators, which are left associative amongst themselves, but do not associate with each other, meaning that one while can write A and B and C, one may not write A and B or C. The next lowest precedence operators are the relational operators (<, <=, ==, !=, >=, and >), which are non-associative. Next in precedence come the left associative shift operators (<< and >>) Next come the left multiplicative operators (*, /, and %) Next come the left associative additive operators (+ and -), followed by the right associative exponentiation operator (**), followed by the non-associative unary operators. The unary operators - and not are prefix, while ! (for factorial) is postfix. Because these are non-associative, one cannot write - - 2 or -5! or 3!!!. The most primitive expressions are identifiers, numeric literals, string literals, ranges (of the form [e1 .. e2] for expressions e1 and e2), function calls, and of course, parenthesized expressions.

    Assume the existence of variables id, numlit, stringlit, and Call. Provide, for this language fragment, the following:
    1. Syntax diagrams
    2. An analytic grammar (you may use either the analytic grammar notation from class or an Ohm grammar) for the language fragment
    3. An abstract syntax specification, using one of the two tree grammar forms found in the notes
    4. A sample program illustrating the majority of the language constructs
    5. An abstract syntax tree for your sample program
  21. For the programming language described as follows:

    Programs are made up of a possibly empty sequence of function declarations, followed by a single expression. Each function declaration is of the form $f = (p_1, \ldots, p_m) \Rightarrow e$ where $f$ is the function name (an identifier), each $p_i$ is a parameter (also identifiers) and the result (the “body”) is an expression. Expressions can be numeric literals, string literals, identifiers, function calls, or can be made up of other expressions with the usual binary arithmetic operators (plus, minus, times, divide, remainder) and a unary prefix negation and a unary postfix factorial (!). There’s a conditional expression with the syntax x ? y : z. Parentheses are used, as in most other languages, to group subexpressions. Numeric literals are non-empty sequences of decimal digits with an optional fractional part and an optional exponent part. String literals delimited with double quotes with the escape sequences \', \", \n, \\, and \u{$hhhhhh$} where $hhhhhh$ is a sequence of one-to-six hexadecimal digits. Identifiers are non-empty sequences of letters, decimal digits, underscores, and dollar signs, beginning with a letter or dollar sign. Function calls are formed with an identifier followed by a parenthesized, comma-separated list of expressions.

    Provide the following:
    1. Syntax diagrams
    2. An analytic grammar (you may use either the analytic grammar notation from class or an Ohm grammar) for the language fragment
    3. An abstract syntax specification, using one of the two tree grammar forms found in the notes
    4. The abstract syntax tree for the following program:
      gcd = (x, y) => y ? gcd(y, x % y) : x
      cube = (x) => x * x * x
      "The answer is" + cube(gcd(30, 4!)) + " 😦😦  "
      
  22. For the programming language described as follows:

    Programs are made up of a non-empty sequence of function declarations, terminated by semicolons, followed by a single expression. Each function declaration starts with the keyword fun followed by the function's name (an identifier), then a parenthesized list of zero or more parameters (also identifiers) separated by commas, then an equals sign, then the body which is a single expression. Expressions can be numeric literals, string literals, identifiers, function calls, or can be made up of other expressions with the usual binary arithmetic operators (plus, minus, times, divide) and a unary prefix negation and a unary postfix factorial (!). There is also an infix binary operator called then which indicates that both of its expressions be evaluated in order from left to right, with the value of the right expression being the value of the entire then-expression. There's a conditional expression that looks just like the one in Java, C, and Perl (with the question mark and colon). Factorial has the highest precedence, followed by negation, the multiplicative operators, the additive operators, conditional and finally the then operator. Parentheses are used, as in most other languages, to group subexpressions. Numeric literals are non-empty sequences of decimal digits with an optional fractional part and an optional exponent part. String literals are as in C. Identifiers are those non-empty sequences of letters, decimal digits, underscores, at-signs, and dollar signs, beginning with a letter or dollar sign, that are not also reserved words. Function calls are as in C, with the arguments in a comma-separated list of expressions bracketed by parentheses. There are no comments in this language, and whitespace can be used liberally between tokens.

    Provide the following:

    1. A complete set of syntax diagrams
    2. An analytic grammar (you may use either the analytic grammar notation from class or an Ohm grammar) for the language fragment
    3. An abstract syntax specification, using one of the two tree grammar forms found in the notes
    4. An example program in this language that shows off everything described above.
    5. A program in this language that consists of the declaration of the GCD function followed by a call to this function with the arguments 99 and 66.
    6. The abstract syntax tree for the GCD program.

Semantics

  1. What is the debate surrounding the terms context-sensitive symtax and static semantics all about?
  2. Give a concrete operational semantics for the statement x = 7 - p * gcd(2, hypot(p, q)) / -1;, assuming a stack-based interpreter with the following stack-based instructions: PUSH <num>, LOAD <var>, STORE <var>, ADD, SUB, MUL, DIV, NEG, HYPOT, and CALL <fun>.
  3. When giving an operational semantics for concurrent computation
    • Natural semantics is not used much if at all because the computation steps are too coarse grained
    • SOS is not used much if at all because the computation steps are too fine grained
    • Concrete operational semantics is okay, provided one had a good enough instruction set
    • SOS is commonly used
    • Natural semantics is always just fine
  4. Denotational semantics
    • provides mathematical descriptions of programming languages independent of their operational behavior
    • requires domains, not sets
    • uses inference rules
    • is closer to small-step than big-step operational semantics
    • Uses ⟂ a lot
  5. Problems from the Nielsen and Nielsen book: 1.1, 1.2, 1.8, 2.3, 2.4, 2.6 (second part only), 2.11 (but skip the proof), 2.17, and 2.33.
  6. Problems from the Slonneger and Kurtz book: 1.4.3, 5.1.1, 5.1.2, 5.1.3, 5.2.3, 5.2.7 (a), 8.5.1, 8.5.9, 8.6.3 (a) and (c), and 8.6.6, 9.1.5, 9.2.6, 9.3.1 (a,b), 9.3.9, and 10.1.2.
  7. What is this natural semantics rule defining? $$ \dfrac{e_1,m \Longrightarrow a \;\;\;\; e_2,m \Longrightarrow b \;\;\;\; x\geq a \;\;\;\; x\lt b}{[\![e_1 \lozenge e_2]\!],m \Longrightarrow x}$$
    • Nothing, it is utter nonsense, $x$ is not defined
    • Nothing, there is a syntax error in the semantic rule
    • The diamond operator produces a random number between its left operand inclusive and its right operand exclusive
    • The static semantics of a TypeScript union type
    • The diamond operator is a modal operator meaning that $b$ is possible if $a$ is
  8. Which of the following rules precisely capture the semantics of a for-loop that evaluates its upper bound each time through the loop (as opposed to only once at the beginning, as we saw in class)? (Note: we want the for loop to treat both the lower and upper bounds of its range as inclusive, not exclusive.)
    • $\dfrac{e_1,m_x \Downarrow x \;\;\; e_2,m_x \Downarrow y \;\;\; (b,m_i,o_i \Downarrow m_{i+1},o_{i+1})_{i=x}^y}{[\![\textrm{for }j\textrm{ in }e_1...e_2\textrm{ do }b]\!], m_x,o_x \Downarrow m_{y+1},o_{y+1}}$
    • $\dfrac{e_1,m_x \Downarrow x \;\;\; e_2,m_x \Downarrow y \;\;\; (b,m_i[i/j],o_i \Downarrow m_{i+1},o_{i+1})_{i=x}^y}{[\![\textrm{for }j\textrm{ in }e_1...e_2\textrm{ do }b]\!], m_x,o_x \Downarrow m_{y+1},o_{y+1}}$
    • $\dfrac{e_1,m_x \Downarrow x \;\;\; e_2,m_x \Downarrow y \;\;\; (b,m_i[i/j],o_i \Downarrow m_{i+1},o_{i+1})_{i=x}^y}{[\![\textrm{for }j\textrm{ in }e_1...e_2\textrm{ do }b]\!], m_x,o_x \Downarrow m_{y},o_{y}}$
    • $\dfrac{e_1,m_x \Downarrow x \;\;\;\; (e_2,m_i \Downarrow y \;\;\;\; b,m_i[i/j],o_i \Downarrow m_{i+1},o_{i+1})_{i=x}^y}{[\![\textrm{for }j\textrm{ in }e_1...e_2\textrm{ do }b]\!], m_x,o_x \Downarrow m_{y+1},o_{y+1}}$
    • $\dfrac{e_1,m \Downarrow x}{[\![\textrm{for }j\textrm{ in }e_1...e_2\textrm{ do }b]\!], m,o \Downarrow [\![\textrm{block }(j=x, \textrm{ while }j\leq e_2\textrm{ do block}(b, j=j+1))]\!],m,o}$
  9. Which of the following are correct ways to give the denotational semantics of a repeat statement? (Focus!)
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!](m,o)=\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }\mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!](m',o') \end{array}$
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!](m,o)=\textrm{fix }\lambda w.\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }w(m',o') \end{array}$
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!]=\textrm{fix }\lambda w.\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }w(m',o') \end{array}$
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!]=\textrm{fix }\lambda w.\lambda (m,o).\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }w(m',o') \end{array}$
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!]=\textit{loop}\textrm{ where }\textit{loop}(m,o) =\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }\mathit{loop}(m',o') \end{array}$
    • $\begin{array}{l} \mathscr{S}[\![\textrm{repeat }b\textrm{ until }e]\!](m,o)=\textit{loop}\textrm{ where }\textit{loop} = \lambda (m,o).\\ \quad\textrm{let }(m',o')=\mathscr{B}\,b(m,o)\textrm{ in}\\ \quad\quad\textrm{if }\mathscr{E}e(m',o')\textrm{ then }(m',o')\\ \quad\quad \textrm{else }\mathit{loop}(m',o') \end{array}$
  10. What is this denotational semantics rule saying? $$\begin{array}{l} \mathscr{S}[\![e \gg b]\!](m,o) = \\ \quad\quad \textrm{if }\mathscr{E}e\,m\textrm{ then }\mathscr{B}\,b(m,o)\textrm{ else }\bot \end{array}$$
    • This executes block $b$ $e$ times
    • This evaluates $e$, stores the result in memory $m$, then tries to run $b$
    • This is a “guarded block” statement: it executes block $b$ only if $e$ is true, otherwise it crashes
    • This is a “guarded block” statement: it executes block $b$ only if $e$ is true, otherwise it does nothing
    • This compares $e$ to $b$ in a short-circuit fashion
  11. What programming language construct is defined by these SOS rules?
    $\dfrac{e_1,m \rightarrow e_1',m}{[\![e_1 \triangleright e_2]\!],m \rightarrow [\![e_1' \triangleright e_2]\!],m}$
    $\dfrac{e_2,m \rightarrow e_2',m}{[\![n \triangleright e_2]\!],m \rightarrow [\![n \triangleright e_2']\!],m}$
    $\dfrac{f \in \mathbb{R}\rightarrow \mathbb{R}}{[\![n \triangleright f]\!],m \rightarrow f(n)}$
    • This is function application, with the argument on the left and the function on the right
    • An expression returning a random number between $0$ and $n$
    • A function call on a random number
    • It’s nonsense. Ewwwwwwww.
    • It invokes a function only if the guarded expression $e_1$ is true
  12. Which of the following would appear in an SOS for a typical if-else statement in a typical programming language. Choose the correct set of rules. (Hint: there are more than one and less than five.)
    • $\dfrac{e,m \longrightarrow e',m } {[\![\mathtt{if}\;e\;b_1\;b_2]\!],m,o \longrightarrow [\![\mathtt{if}\;e'\;b_1\;b_2]\!],m,o}$
    • $\dfrac{} {[\![\mathtt{if}\;\mathtt{true}\;b_1\;b_2]\!],m,o \longrightarrow b_1,m,o}$
    • $\dfrac{} {[\![\mathtt{if}\;\mathtt{false}\;b_1\;b_2]\!],m,o \longrightarrow b_2,m,o}$
    • $\dfrac{ b_1,m,o \longrightarrow b_1',m',o' } {[\![\mathtt{if}\;e\;b_1\;b_2]\!],m,o \longrightarrow [\![\mathtt{if}\;e\;b_1'\;b_2]\!],m',o'}$
    • $\dfrac{ b_2,m,o \longrightarrow b_2',m',o' } {[\![\mathtt{if}\;e\;b_1\;b_2]\!],m,o \longrightarrow [\![\mathtt{if}\;e\;b_1\;b_2']\!],m',o'}$