|
|
|
|
|
by nfoz
1472 days ago
|
|
With interface-typing, is there a right way to use the type system to declare "this function takes a (statically-certifiable) positive integer", not because it would fail to output a value for other numbers, but because its output would be incorrect or meaningless? In scientific programming I've often wanted that, to push asserting particular constraints on values to the caller and/or the type system (whether that's compile-time or runtime type checks). But those constrained types would always match the interface of their unconstrained forms. |
|
Needless to say, this code would be very verbose and ugly - what you would actually want is dependent typing, which solves this problem at the type system level.
Here is the implementation for Nat in Idris, for example:
With dependent typing, you can specify constraints on the values of function parameters, and have the compiler prove that only such values can be passed to those functions. For example, the following function only takes non-zero natural numbers, since the S constructor used in the parameter implies that the element can not be Z: