What Every C Programmer Should Know About Undefined Behavior #2/3
1–10 of 45 posts
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#2Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#3Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
I don't think what you're saying is intrinsic to FP. There are static analysis tools for non-functional languages. At the end of the day, everything is getting turned into an abstract syntax tree, so style should be irrelevant.
Do you mean pure (side-effect free) functional languages?
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#4Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#5Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
Can you elaborate on your question? I don't think what you're saying is intrinsic to FP. There are static analysis tools for non-functional languages. At the end of the day, everything is getting turned into an abstract syntax tree, so style should be irrelevant. Do you mean pure (side-effect free) functional languages?
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#6Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#7Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
In Haskell you happen to know about statelessness from the types, which is an additional bonus. But other statically type languages like Eiffel are also pretty well optimizable.
Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#8Kind of off topic, but I am curious: is this one of the advantages of functional programming - the notion that you can prove or disprove certain things about the code and therefore optimize the compiler, based on such proofs, to your heart's content?
In Haskell:
zip :: [a] -> [b] -> [(a, b)]
zip [] _ = []
zip _ [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
Note how each iteration needs to check both lists for emptiness.In a more advanced type system (or using more advanced type hackery in Haskell) you can have length-indexed lists. That is: lists that have 2 type parameters or "indexes" instead of 1. The 1 is usually the type of element inside the list, the extra 1 is a natural number indicating the length of the list.
So the function zip becomes something like:
zip :: List N a -> List N b -> List N (a, b)
zip [] [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
The compiler checks the code and verifies, at compile-time, that both the input lists and the resulting list are all of the same length. This also means that you only need one runtime emptiness check instead of 2. If the N is concretely known at compile-time, you may need 0 (though that case might be caught by inlining/loop unrolling optimizations anyway).Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#9 if (i = buffer)
... instead of if ((i = sizeof(buffer))
?Re: What Every C Programmer Should Know About Undefined Behavior #2/3
#10What would possess someone to write if (i = buffer) ... instead of if ((i = sizeof(buffer)) ?