by DonaldFisk · 9y ago · 0 comments
Dependent types do a bit more than that. Supposing you want to put a random pixel on a canvas C, but you don't know C's dimensions at compile time (perhaps because the user can change them at run time). The numerical value of the pixel's x and y coordinate ranges aren't known at compile time, but it is known that they're not negative and less than C.width and C.height.
C = new Canvas
...
// random(n) returns an integer of type range(0, n)
x = random(C.width) // x has type range(0, C.width)
y = random(C.height) // y has type range(0, C.height)
// drawPoint(w, j, i) takes args of type canvas, range(0, w.width), range(0, w.height).
drawPoint(C, x, y) // The types of x and y depend on the run-time value of C.
Using dependent typing would enable us to remove another source of bugs.I was under the impression that Adas integer ranges were checked at runtime, like contracts. Is that not the case?
A range is a signed integer value which ranges from a
First to a last Last. It is defined as
range First .. Last
When a value is assigned to an object with such a range
constraint, the value is checked for validity and
Constraint_Error exception is raised when the value is
not within First to Last.It's interesting to me how Ada takes the approach of blending types and run time contracts. In most languages it would be two different things - a function that takes int, then a contract or assert that handles the value.
; Constant 30 conflicts with its asserted type (INTEGER 50 100).
But in many other cases it'll generate a runtime check instead, because the static type checking is basically best-effort.