← Back to context

Comment by hgoel

16 hours ago

I think it's about if there's a possibility of it being zero. Of course there's no way to tell at compile time that a value will definitely be zero.

So, in pseudocode

int div(int a, int b): return a / b;

Would probably be a compile time error, but

int div(int a, int b): return b == 0 ? ERR : (a /b);

Would not, or at least that's what I'd expect.

> Of course there's no way to tell at compile time that a value will definitely be zero.

Yes there is. Dependently typed languages like Idris can inspect terms at the value-level during compile time. Rather, instead of proving that the divisor will be zero, you must instead statically prove that the divisor cannot be zero; otherwise the code will not typecheck.

  • Okay,

    int integer_division(int a, int b) { if (b!=0) return a/b; raise(SIGFPE); }

    Great.

    • No. In this type of language, the typical division function does not check against zero. It has a precondition that requires the caller to ensure that the divisor is not zero. If the data the caller has is completely arbitrary, then yes, the caller must use an if statement or similar. If the caller knows something about its data and can be sure that the divisor is not zero, then it doesn't need to use an if statement. But it might need to convince the proof checker that it knows what it's doing.