Velocerアプリを開く

Velocer › フラッシュカード › University of Illinois Urbana-Champaign › CS 421: Programming Languages and Compilers · UIUC

CS 421: Programming Languages and Compilers · UIUC

Programming languages and compilers review cards. went through the lecture topics week by week. · 作成者: siebel_sprout · カード 131 枚

University of Illinois Urbana-Champaign

このデッキを学習する 埋め込む

  1. What argument-passing strategy does OCaml use?Call-by-value.
  2. What kind of language is OCaml in terms of function application?A higher-order applicative language.
  3. How does OCaml handle memory management?Garbage collection is done automatically.
  4. What kind of data types can the programmer define in OCaml?Algebraic data types.
  5. What typing feature does OCaml provide?Type inference.
  6. Why is OCaml well suited to language-processing tasks?It is well suited to tasks such as parsers and compilers.
  7. What is an environment in programming language semantics?A mapping from identifiers to values, behaving like a partial function. It is usually stored as a list or stack searched left to right, with the first match winning.
  8. How is an environment typically stored and searched?As a list or stack that is searched left to right, where the first matching binding wins.
  9. What does let x = e;; do to the environment?It evaluates e to a value v in the current environment, then extends the environment with the binding x ↦ v.
  10. What does the environment extension operator ρ1 + ρ2 mean?It keeps every binding of ρ1 plus the bindings of ρ2 that are not rebound in ρ1. New bindings hide old ones, but old values are not changed.
  11. When a new binding hides an old one, is the old value destroyed?No. The new binding hides the old one, but the old value is not changed or removed.
  12. Does evaluating a bare expression (not a declaration) change the environment?No. A bare expression does not change the environment.
  13. What is the difference between let x = e;; and let x = e1 in e2?let x = e;; extends the current environment permanently, while let x = e1 in e2 is a local binding: e1 is evaluated to v and e2 is evaluated in the environment extended with x ↦ v.
  14. How is let x = e1 in e2 evaluated?Evaluate e1 to a value v, then evaluate e2 in the environment {x ↦ v} + ρ.
  15. In the environment {x ↦ v} + ρ, which binding wins if ρ already binds x?The new binding x ↦ v wins, because new bindings hide old ones.
  16. What does it mean that an environment behaves like a partial function?It maps some identifiers to values but may be undefined for others, so not every identifier necessarily has a binding.
  17. What is a local binding in the context of let x = e1 in e2?A binding whose scope is limited to the evaluation of e2. It extends the environment only while evaluating e2.
  18. What is the lambda term fun n -> n + 2?A nameless (anonymous) function that adds 2 to its argument.
  19. How does let plus_two n = n + 2 relate to a lambda term?It is syntactic sugar for let plus_two = fun n -> n + 2.
  20. In fun v -> e, what is the scope of v?Just e — the variable is bound only within the function body.
  21. What type does let add_three x y z = x + y + z have?int -> int -> int -> int.
  22. What lambda expression is let add_three x y z = x + y + z sugar for?fun x -> fun y -> fun z -> x + y + z.
  23. What is partial application (sectioning)?Supplying fewer arguments than a curried function expects, yielding a function of the remaining arguments; e.g. add_three 5 4 returns an int -> int.
  24. What is the difference between a curried and an uncurried function?Curried functions take arguments one at a time (e.g. add_three); uncurried ones take a single tuple (e.g. add_triple (u,v,w)).
  25. What type does let thrice f x = f (f (f x)) have?('a -> 'a) -> 'a -> 'a.
  26. What three things does a closure package together?The function's parameter pattern, its body, and the environment in effect when the function was defined.
  27. How is a closure written notationally?As a pair of the function and its environment, e.g. ⟨(n,m) → n+m, ρ⟩.
  28. For a recursive function, what must its closure's environment include?The function itself, so recursive calls can be resolved.
  29. In evaluation, what does a constant (including primitive operators) evaluate to?It evaluates to itself.
  30. How is a variable evaluated?It is looked up in the environment ρ.
  31. How is a tuple evaluated?Each component is evaluated (right to left in OCaml), then the tuple value is formed.
  32. How are primitive operations evaluated?Evaluate the arguments, then apply the operation.
  33. What does a function expression evaluate to?Its closure.
  34. How is an if-then-else expression evaluated?Evaluate the condition b; then evaluate only the chosen branch.
  35. What types can a tuple hold?A tuple can hold values of mixed types, e.g. (5, "hi", 3.2) : int * string * float. Tuples can also nest.
  36. What does the pattern let (a,b,c) = s do?It destructures the tuple s, binding its components to a, b, and c respectively.
  37. What does the wildcard pattern _ do?It matches anything and binds nothing.
  38. Can patterns be nested?Yes, patterns may nest, allowing destructuring of nested tuples.
  39. How does match choose which clause to use?It tries clauses top to bottom and uses the first one that fits.
  40. What is the scope of variables bound in a match clause?Each clause's variables are scoped only to that clause.
  41. What is the type of the tuple (5, "hi", 3.2)?int * string * float.
  42. What is the purpose of pattern matching on tuples?Patterns destructure tuples, extracting their components into named variables or ignoring them with _.
  43. What are the two possible forms of a list in OCaml?A list is either [] (empty) or x :: xs, where x is the head and xs is the tail.
  44. What does [x] mean in terms of cons?[x] is shorthand for x :: [].
  45. What does it mean that lists are homogeneous?All elements of a list must share the same type.
  46. What is structural recursion?Recursion that recurses on the same-typed pieces of a data structure, with base cases ending the recursion.
  47. Give the structural recursion definition of length.length [] = 0 and length (a::bs) = 1 + length bs.
  48. Why is poor_rev (reverse using @) quadratic?Because each use of @ takes time proportional to the length of its left argument, and it is applied repeatedly.
  49. What is forward recursion?A recursion pattern that first recurses on all components, then builds the result from the partial results.
  50. What is the type of map f?('a -> 'b) -> 'a list -> 'b list.
  51. What does map f do to a list?It applies f to every element of the list, producing a new list of results.
  52. Write the expansion of fold_right f [x1;...;xn] b.f x1 (f x2 (... (f xn b))).
  53. What recursion pattern can always be written with fold_right?Any forward primitive recursion over lists.
  54. Write the expansion of fold_left f a [x1;...;xn].f (... (f (f a x1) x2) ...) xn.
  55. What recursion pattern can always be written with fold_left?Any tail primitive recursion.
  56. What is primitive recursion?A recursion that only recurses on immediate subcomponents of the structure.
  57. What does it mean for a subexpression to be available?It could be the next thing evaluated. Code under a lambda (fun x -> ...) is not available.
  58. What is a tail position?An available subexpression whose value, if computed, becomes the value of the whole expression.
  59. What is a tail call?A function call that appears in tail position.
  60. When is a function considered tail recursive?When all of its recursive calls are tail calls.
  61. Why are tail-recursive functions special for compilation?They can be compiled into loops, so they don't grow the stack.
  62. What do tail-recursive functions usually need to avoid stack growth?An accumulator, and often an auxiliary function.
  63. Is code inside a lambda expression available?No, code under a lambda (fun x -> ...) is not available.
  64. What distinguishes a tail call from other function calls?A tail call is specifically a call in tail position, meaning its result becomes the value of the whole expression.
  65. What is the relationship between tail position and the value of the whole expression?A tail-position expression's computed value becomes the value of the entire expression.
  66. What is a continuation?A function representing "the rest of the computation"; it receives a result instead of the result being returned.
  67. How does direct style differ from continuation passing style (CPS)?Direct style returns results to the caller, while CPS passes every result forward to a continuation, and continuations are only called in tail position.
  68. What are common uses of continuations?Non-local control flow (jumps, exceptions, green threads), converting non-tail calls into tail calls, and as an intermediate form in compilers.
  69. What are the trade-offs of CPS?CPS makes evaluation order explicit and names every intermediate value, at the cost of building closures on the heap.
  70. What is the general syntax for defining a variant type?type name = C1 [of ty1] | ... | Cn [of tyn], where each Ci is a constructor.
  71. What is a constant constructor?A constructor that takes no argument, e.g. Monday in type weekday = Monday | ... | Sunday.
  72. What are constructors the basis for?Pattern matching.
  73. How is an enumeration type defined and used?As a variant with only constant constructors, e.g. type weekday = Monday | ... | Sunday; functions over it use match.
  74. What characterizes a disjoint union type?Each alternative is tagged by a distinct constructor, e.g. DriversLicense of int | SocialSecurity of int | Name of string.
  75. How is the option type defined and what does it represent?type 'a option = Some of 'a | None; it represents failure or absence, encoding partial functions and often replacing exceptions.
  76. How are functions over recursive types written?They recurse on each recursive component, e.g. sum_tree recurses on the subtrees of a Node.
  77. How do map and fold generalize to trees?They follow the tree structure, e.g. ibtreeMap and ibtreeFoldRight leafFun nodeFun.
  78. How are mutually recursive types declared?With and, e.g. 'a tree and 'a treeList.
  79. What do mutually recursive types require of functions over them?Mutually recursive functions, such as fringe and list_fringe.
  80. Why do types matter?For data abstraction, type checking, code generation, and data layout.
  81. What is the difference between type declarations and type inference?Type declarations state types explicitly, while type inference computes a type from context. Fully static inference was introduced by Robin Milner in ML.
  82. Who introduced fully static type inference, and in which language?Robin Milner introduced fully static type inference in ML. Languages like OCaml, SML, and Haskell use it.
  83. What does the type judgment Γ ⊢ e : τ mean?In typing environment Γ, expression e has type τ. Γ maps variables to at most one type each.
  84. How is the symbol ⊢ read in a type judgment?⊢ is read as 'entails' or 'shows'.
  85. What are the typing axioms for integer constants and boolean constants?Integer constants have type int; true and false have type bool.
  86. What is the variable typing rule?If Γ maps variable x to type τ, then Γ ⊢ x : τ. Formally, Γ ⊢ x : Γ(x).
  87. What is the typing rule for binary operators like + and *?If both arguments have type int, the result has type int. For example, if Γ ⊢ e1 : int and Γ ⊢ e2 : int, then Γ ⊢ e1 + e2 : int.
  88. What is the typing rule for relations such as < and =?Relations take two int arguments and produce a bool. For example, if Γ ⊢ e1 : int and Γ ⊢ e2 : int, then Γ ⊢ e1 < e2 : bool.
  89. What is the typing rule for if-then-else expressions?The condition must have type bool, and both branches must have the same type τ, which is also the type of the whole expression.
  90. What is the typing rule for function application?If e1 has type τ1 → τ2 and e2 has type τ1, then e1 e2 has type τ2.
  91. What is the typing rule for fun expressions (lambda abstractions)?If extending Γ with x : τ1 gives e type τ2, then fun x -> e has type τ1 → τ2.
  92. How do let and let rec typing rules differ?Both extend the environment with the bound variable's type. For let rec, the bound name is also in scope in its own definition.
  93. What is a type derivation?A type derivation is a proof tree built from typing rules. It shows step-by-step why an expression has a certain type.
  94. What is a monomorphic type? Give examples.A type without universal quantifiers, such as base types, type variables, and compound types like 'a -> 'b, int * string, or bool list.
  95. How does a polymorphic type differ from a monomorphic type?A polymorphic type adds universal quantifiers, e.g. ∀α.τ, allowing it to work for all instantiations of the quantified variables.
  96. What are the free type variables of a type, and what is Vars('a -> (int -> 'b) -> 'a)?Free type variables are those not bound by a quantifier. Vars('a -> (int -> 'b) -> 'a) = {'a, 'b}.
  97. What question does type checking ask?Whether a given expression e has a given type τ, typically answered by constructing a derivation.
  98. What question does type inference ask?Whether an expression e has some type and, if so, which type (or it reports an error).
  99. Outline the main steps of type inference.Assign the whole expression a fresh type variable; decompose by typing rules generating constraints; solve constraints with substitutions, applying and composing them; apply the final substitution to the original variable.
  100. How is the variable case handled in type inference?Instantiate all quantified variables of Γ(v) with fresh type variables, then unify the result with the expected type.
  101. What does the GEN (generalization) operation do in let/let rec inference?It generalizes the inferred type of the bound expression over type variables not free in the environment, yielding let-polymorphism.
  102. What is let-polymorphism?The polymorphism obtained by generalizing a let-bound expression's type over variables not free in the surrounding environment.
  103. In inference, how are constraints handled across subproblems?Solve the first subproblem's constraints with a substitution, apply that substitution to the next subproblem, compose the substitutions, and apply the final one to the original type variable.
  104. What is a fresh type variable used for in inference?It stands for an as-yet-unknown type, later resolved by unification and substitution.
  105. What is the difference between type checking and type inference?Type checking verifies an expression against a given type; type inference determines an unknown type (or reports an error) without being given one.
  106. What is the unification problem?Given equations between terms built from constructors and variables, find a substitution that makes every pair equal.
  107. What does the Delete rule do in the unification algorithm?If the two terms in a pair are identical, drop that pair.
  108. What does the Decompose rule do in unification?When both sides are the same constructor with the same arity, replace the pair with the pairs of corresponding arguments.
  109. What does the Orient rule do in unification?If one side is a variable and the other is not, flip the equation so the variable is on the left.
  110. What is the occurs check in unification?It verifies that a variable does not occur inside the term it is being equated to before applying the substitution.
  111. What does the Eliminate rule do in unification?If s = x with x a variable not occurring in t, apply {x -> t} to the remaining equations and compose with that substitution.
  112. What substitution results from an empty set of equations?The identity substitution.
  113. What are the two failure cases in unification?Different constructors (decompose fails), or a variable occurring inside the term it must equal (occurs check fails).
  114. Give an example of an occurs-check failure.x = g(x), since x occurs inside g(x).
  115. Why does unifying f(a) = g(b) fail?The constructors f and g differ, so the Decompose rule cannot apply.
  116. What kinds of terms does unification operate on?Terms built from constructors and variables.
  117. In the Decompose rule, what must match for it to apply?The constructor must be the same and the arity must be the same.
  118. What is a substitution in unification?A mapping from variables to terms that, when applied, makes the equations hold.
  119. What are the main phases of a compiler described in this section?Strings are converted to tokens (lexing), tokens to abstract syntax trees (parsing), then the result is interpreted or translated to instructions.
  120. What is the difference between syntax and semantics?Syntax determines which symbol strings are meaningful; semantics gives them meaning.
  121. What does lexing do, and how are tokens specified?Lexing turns characters into tokens, which are specified by regular expressions.
  122. What does parsing do, and how is it specified?Parsing turns tokens into an abstract syntax tree (AST), specified by BNF grammars.
  123. In regular expressions, what does concatenation xy mean?It combines strings from L(x) and L(y): e.g., L(x)={a,ab} and L(y)={c,d} gives {ac,ad,abc,abd}.
  124. What does alternation x∨y represent in regular expressions?The union of the languages of x and y.
  125. What does the Kleene star x* denote?Zero or more copies of strings from x, including the empty string.
  126. What does ε represent in regular expressions?The language containing only the empty string, {""}.
  127. What does ∅ represent in regular expressions?The empty set of strings.
  128. What form do rules take in a right regular grammar?Rules of the form A::=aB or A::=a (or ε), where nonterminals act as states and rules as edges.
  129. What class of languages do right regular grammars describe?Exactly the regular languages.
  130. How do right regular grammars map to automata?They map directly to nondeterministic finite automata: nonterminals are states and rules are edges.
  131. What are the three audiences grammars serve?Designers write them, implementers accept programs by them, and users follow them.

Velocerは無料です。間隔反復と、あらゆる文書から作成できる自分だけのカードをご利用いただけます。

PDF to flashcards · Lecture to flashcards · Practice exam · Syllabus study plan · Anki import

University of Illinois Urbana-Champaignのほかのデッキ