4 ms·
Interesting stuff. Can't wait to play with. One immediate reaction from reading about things like "@given(float)... def testFun(x): ... assume(not isnan(x))...
by e12e 12y ago
Interesting stuff. Can't wait to play with.
One immediate reaction from reading about things like "@given(float)...
def testFun(x): ... assume(not isnan(x))..." -- I immediately would prefer to decorate/document my functions (that could be test functions, but more generally my "real" functions) with these pre-/post- conditions and assumtions, and generate the tests, something like:
@assert(idempotent) #(inv(inv(x))==x)
def inv(x):
"Return -x"
@given(float):
@assume(not isnan(x))
return -x
Typing this out, I can see why they go in the tests (avoids going down the rabbithole of creating a partially typed version of python...) -- which is why @assume(float) isn't on top; it'd imply inv didn't work for integers. Maybe a weaker:
@assume(not isnan(x)) #implies x is number, but not nan
@idempotent # inv(inv(x)) == x
def inv(x):
return -x
Now it should be possible to infer that inv(x) should be checked with numbers, and that it is idempotent. Might be a weak test unless one can specfify a few other things - but maybe such pre/post "hints" could be combined to make better short tests? Eg that one could give a different expression to check against rather than the function itself: def test_inv(): assert(inv(x), lambda y: -y)?
At any rate, interesting framework.
- reverius42 12y agoI don't think that's what idempotent means. The only way f can be idempotent when f(f(x)) == x is if f(x) == x. (This implies that f is the identity function). The property that makes the function idempotent is that successive application produces the same result as initial application: f(f(x)) == f(x).
- e12e 12y agoYou're right, of course. I typed that on my cellphone, and were a little quick looking up alternatives for @double_application_turns_this_into_an_identity_function -- that made more sense, and was a little more succinct. Maybe @left_inverse would be more appropriate? It's been a while since I had to classify functions.
- jfarmer 12y agoIf you're curious, the math-y term for an operation that is its own inverse is involution. I don't know that I've ever heard the word in a programming context, but there it is! :)
- tmoertel 12y agoAn involution played a rather important role in this programming context: http://blog.moertel.com/posts/2013-12-14-great-old-timey-game-programming-hack.html http://blog.moertel.com/posts/2013-12-14-great-old-timey-gam...
- e12e 12y agoThank you! A quick look through my bookshelf reveals that out of four books on discrete mathematics, only one contain involution in the index: and there it is mentioned once in a supplementary exercise... I guess that explains why I couldn't seem to recall a term for "self-inverse" [ed: No, that's wrong to, lol. Lets just stick with involution].
- jfarmer 12y agoThe word "involution" finds more use in analysis and algebra, where self-inverses have more interesting (often geometric) properties. The only interesting property I can think of in the context of discrete mathematics would be that if S is a finite set and f:S → S is an involution then the parity of |S| is equal to the parity of the fixed points of f. That is, |S| ≡ |Fix(f)| (mod 2) where Fix(f) = {x in S : f(x) = x}.