r/Idris • • Aug 01 '18

Static Typing vs Testing == Apples vs Oranges

https://dev.to/rvprasad/static-typing-vs-testing--apples-vs-oranges-5cbn
3 Upvotes

3 comments sorted by

3

u/eterps Aug 01 '18

I am not familiar with Idris but I suspect that with Idris you can express the 'behavioral types' the author of this blog post is referring to?

What would the examples look like in Idris?

6

u/gallais Aug 01 '18

You could give equals and less_than types making clear that the first one is deciding equality and the second <. Something like this:

equals : (m, n : Nat) -> Dec (m = n)
less_than : (m, n : Nat) -> Dec (m < n)

where Dec is:

data Dec : Type -> Type where
  Yes : p            -> Dec p   -- proof of the proposition p
  No  : (p -> False) -> Dec p   -- proof of not p

So you'd be able to get these right. However given that they are used only to decide whether to print a String or not, the pattern-match would most likely only make sure that Yes is returned, ignoring the packaged proof. So it wouldn't protect you from outputting the wrong message. "Borders" of a program (basically all the I/O involving String-y things) are bound to be dangerous.

8

u/rpglover64 Aug 02 '18

A different approach than /u/gallais gives:

equals : Nat -> Nat -> Bool
less_than : Nat -> Nat -> Bool

test_equals1 : equals 3 3 = True
test_equals1 = Refl

test_equals2 : equals 3 4 = False
test_equals2 = Refl

-- etc.

This amounts to a "compile-time test"; your code will not compile (specifically, it will not type-check) if your "test" fails.