|
|
|
|
|
by gnulinux
2590 days ago
|
|
Of course you can decide the type a function returns, to overkill this problem, just run any type inference algorithm powered by unification (e.g. hindley milner). Give undeclared variables the type NULL and you're all set since you can decide whether the function returns type NULL. Your friend was confused because you cannot decide whether a certain line in your program will be executed. Since this is equivalent to the halting problem. Let's prove this! Assume we have a blackbox B(P,x,N) decides whether line N of program P will be executed given input x. Here, we can solve the halting problem: ```pseudocode def Helper(sourcecode P, input x): [0] P(x)
[1] print "What Do We Say to the God of Death?"
def Halts(sourcecode P, input x): if B(Helper,(P,x),1):
return true
else:
return false
```This means, given programs like your interview question, compiler cannot decide -- in general -- whether the program will crash or work. Of course, it's not too hard to find "good enough" heuristics that'll catch some cases and/or restricting your language to make it "harder" to encode such programs. |
|