这次回顾Part B Exam。

课程主页:

https://www.coursera.org/learn/programming-languages-part-b/home

B站搬运:

https://www.bilibili.com/video/BV1tZ4y1D7

问题1

Check the box if the statement is true.

  • ML is dynamically typed.
  • ML is weakly typed.
  • Racket is dynamically typed.
    • 正确
  • Racket is weakly typed.

问题2

Check the box if the statement is true.

  • A “type system” that rejects every program is sound but useless.
    • 正确
  • A “type system” that rejects every program is complete but useless.
  • A “type system” that accepts every program is sound but useless.
  • A “type system” that accepts every program is complete but useless.
    • 正确

回顾定义:

  • sound
    • 从不接受一个在某些输入下会做X的程序
  • complete
    • 从来没有拒绝过一个在任意输入下都不会做X的程序程序

所以拒绝全部程序的类型系统是sound,接收全部程序的类型系统是complete。

问题3

For each of the following, check the box if and only if it is an accurate description of an advantage of (sound) static typing (for preventing the sort of things that ML’s type system prevents).

  • If you change the argument type of a function, the type-checker can give you a list of callers that no longer type-check as a result.
    • 正确
  • If you “comment out” a function and the program still type-checks, then you can be sure that no execution of the program will “try” to call the function.
    • 正确
  • If you add a function to a program and the program still type-checks, then you can be sure that the added function will always be called when the program is executed.
  • There is no reason to have exceptions in a programming language with static typing, so programmers do not have to worry about handling exceptions.

问题4

For the remaining questions, we will consider changing ML’s type system in the following way: We allow a function that takes a tuple argument t1 * t2 * ... tn to be called not only with a tuple of that exact type but also with wider tuples t1 * t2 * ... tn * t(n+1) * t(n+2) * .... The typing rule for such calls is that the first n parts of the tuple must still have the “right” type for the call and the “extra” parts each need to have some type, but any type is fine. The evaluation rule for such calls is that the tuple argument is fully evaluated (including the “extra” parts) and the extra parts are simply inaccessible (ignored) by the function called.

Note the only typing rule we change is the one for function calls.

We assume the goals of the ML type system (“what it aims to prevent”) are unchanged except that is okay to use these “too-wide tuples”.

Check a box below if and only if the corresponding statement is true.

  • ML without the change described above has a sound type system.
    • 选择
  • ML with the change described above has a sound type system.
    • 选择
  • ML without the change described above has a complete type system.
  • ML with the change described above has a complete type system.

回顾定义:

  • sound
    • 从不接受一个在某些输入下会做X的程序

改变前后都符合sound定义。

问题5

Check the box below only if the ML code does not type-check in regular ML and does type-check if we make the change to the type system described in Question 4. All the code below uses this function:

fun f1 (x,y) = x + y

选项:

val z1 = f1 (3,4,5)

选择。

val z2 = f1 (3,4,x)

不选择,因为x没定义。

val p1 = (7,9)
val z3 = f1 p1

不选,因为改变前后都能通过类型检查。

val p2 = (7,9,11)
val z4 = f1 p2

选择。

val z5 = if true then f1 (3,4) else f1 (5,6,7)

选择。

val z6 = f1 (if true then (3,4) else (5,6,7))

因为改变只涉及函数调用,而这里then else类型不匹配,所以不能选。

问题6

Which of the reasons below is not an advantage of making the type-system change described in Question 4.

  • If we had a function taking three arguments in a tuple, we could change it to take two arguments in a tuple without having to change any existing callers.
    • 不选
  • In some situations, it lets you build one tuple that you can use to call multiple functions even if those functions take tuples of different widths.
    • 不选
  • It makes it easier to convert between a multi-argument function using currying and one using tupling.
    • 选择,不能带来优势
  • It allows an expression like foo(bar(),baz(),print "about to call foo")to be equivalent to foo(bar(),let val x = baz() in (print "about to call foo"; x) end) while being shorter and probably easier to read.
    • 不选

问题7

Which of the reasons below is not a disadvantage of making the type-system change described in Question 4.

  • Passing too many arguments is often an error, but the type system will now not always prevent this error.
  • It makes type inference more difficult due to polymorphism. For example, if id has'a -> 'a, should id (3,4,5) return a triple or a pair?
  • It adds a special case to the language even though there are other places where a too-wide tuple could be allowed, for example in pattern-matching like let val (x,y) = (3,4,5) in ... end.
  • It is a bad idea for functions to ignore some of their arguments, and it is impossible for a function in regular ML to ignore an argument.
    • 可以忽略一些参数,所以不是缺点