, Idris, Agda Coq, , - . .
safeDivide : Nat -> (y:Nat) -> so (y /= 0) -> Nat
safeDivide x y p = div x y
main : IO ()
main =
print (show 1) -- compiles successfully
print (show (safeDivide 2 1 oh)) -- compiles successfully
-- print (show (safeDivide 2 0 oh)) -- throws an error at compile time
Dependent types of languages allow you to write evidence so that their type system can check whether your code will work as it should. By defining safeDividewith proof ( so (y /= 0), you guarantee that your program will not even compile if 0it ever extends to this function.
source
share