4 ms·
I've experimented on a few systems to apply interval arithmetic to type inference, i.e. // starRating is an int<0,5>, meaning // integer between 0 and 5
by kerkeslager 2y ago
I've experimented on a few systems to apply interval arithmetic to type inference, i.e.
// starRating is an int<0,5>, meaning
// integer between 0 and 5 inclusive.
let starRating = console.readInt<0,5>("How many stars do you rate this movie?");
db.addStarRating(starRating);
// starRatingList is a list<int<0,5>>
let starRatingList = db.readStarRatings(movie);
// totalStarRatings is an int<0,5*starRatingList.length>
// this type can't be sized at compile time, but we can still check it!
let totalStarRatings = sum(starRatings);
// avgStarRating is a very difficult type, because we need to either have
// a fraction type that's slow, or some handling of rounding/truncating
// when we do division. So now we're dealing with 3 intervals: min/max,
// min/max precision, and min/max error.
let avgStarRating = totalStarRatings / starRatingList.length;