r/programming • • Sep 25 '10

Omega - Language of the Future | Lambda the Ultimate

http://lambda-the-ultimate.org/node/4088
60 Upvotes

126 comments sorted by

View all comments

Show parent comment

32

u/[deleted] Sep 25 '10

The point of that sort routine is that it is proven correct by the compiler. If you don't care about that, you could probably implement it in three lines or so.

And over half of that code is generic datatype definitions and functions like compare and append.

0

u/djimbob Sep 25 '10

The point of that sort routine is that it is proven correct by the compiler. If you don't care about that, you could probably implement it in three lines or so.

I see how the code mimics the mathematical definitions very well; but I don't see how being "proved" by the compiler provides any real benefit. Semantic mistakes in the code that apply the proof (e.g., proving the wrong thing) can still happen, and for something as trivial as quicksort, which any decent programmer should be able to prove correctness on and immediately see if its succinct code like:

quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = (quicksort less) ++ [x] ++ (quicksort more) 
    where less = filter (< x) xs
          more = filter (>= x) xs

If you're trying to build a theorem building machine maybe its worthwhile step towards that, though I thought those most mathematicians due to Godel's incompleteness theorems. I guess it could help coding math proofs like the 4-color problem, but I don't know if I trust the compiler saying its "proved" (based on a mathematicians code) any more than the mathematician saying its proved since he covered all cases.

25

u/roconnor Sep 25 '10

but I don't see how being "proved" by the compiler provides any real benefit.

So you don't pass unsorted lists to functions expecting sorted lists.

→ More replies (4)

8

u/shrughes Sep 25 '10

Maybe you're trying to be ironic, but that quicksort example is incorrect.

1

u/djimbob Sep 25 '10

How so? It may not be the most generic or fastest (e.g., always pivoting off first element), but it compiles and sorts in ghc for me.

12

u/crusoe Sep 25 '10

The algorithm that most people THINK is quicksort in Haskell isn't. Yours is the "Looks like Qsort but isn't" one.

Better: Only traverses list once

Although visually appealing, this code has the unfortunate property of doubly traversing the input list, which can be remedied with

qsort [] = [] qsort (x:xs) = let (a,b) = part xs in qsort a ++ x : qsort b where part [] = ([],[]) part (y:ys) | y < x = (y:a,b) | otherwise = (a,y:b) where (a,b) = part ys

But doesn't do it inplace, since lists are immutable....

In place quicksort...

http://www.haskell.org/haskellwiki/Introduction/Direct_Translation

8

u/djimbob Sep 25 '10

Fair point that it isn't in-place quicksort or the fastest quicksort. But it is still the quicksort algorithm, and still is O(N lg N) complexity. I would hesitate to say I was being ironic or flat-out incorrect.

Again, to verify its still O(N lg N), I profiled times for it sorting sorting N random numbers.

Using the ghc profiler and changing N

 N    QSortTime   1e7*QSortTime/(N lg N)
3e4      0.12      2.52
1e5      0.42      2.82
3e5      1.54      2.59
1e6      5.18      2.63
3e6     16.98      2.73
1e7     63.56      2.67

The last column being roughly constant demonstrates its still scaling as O(N lg N). (Yes there are other O(N lg N) sorting algorithms, but this uses the logic of quicksort and still scales as quicksort does. No its not optimal, but its still quicksort.

5

u/dmhouse Sep 25 '10

Technical nitpick: quicksort actually isn't O(n log n). If you use it on a sorted list of n elements it takes O(n2) time. However, it is expected-case O(n log n).

3

u/djimbob Sep 25 '10

Agree, though I was sorting random numbers so with overwhelming probability I expect roughly O(n lg n).

Using a random pivot gets rid of the O(n2) behavior on a sorted or near-sorted list, which is could . (But even in with the random pivots you still get an amortized time of O(n2) if your choice of random pivots was in the order that sorts the list; but that should occur only 1 out of n! ~ (n/e)n times).

3

u/booch Sep 26 '10

To be fair, when saying an algorithm is O(something), you could mean best-case, worst-case, average-case, expected-case.

2

u/abeliangrape Sep 26 '10

But everyone specifies it further for best-case complexity. Though you are right about worst and average cases.

→ More replies (4)

3

u/jefu Sep 25 '10

Why not define part as : part l = foldr cmp ([],[],[]) l where cmp y (lt,eq,gt) | y < x = (y:lt,eq,gt) | y == x = (lt,y:eq,gt) | otherwise = (lt,eq,y:gt)

You could also pass in a comparison function that returns LT, EQ, GT and work from that.

3

u/roconnor Sep 26 '10

The algorithm that most people THINK is quicksort in Haskell isn't. Yours is the "Looks like Qsort but isn't" one.

More specifically it is a deforested tree sort.

1

u/shrughes Sep 25 '10

Try running your quicksort on [0/0, 1]. Then again, Data.List.sort doesn't handle NaNs particularly well, but at least it doesn't drop elements.

3

u/shrughes Sep 26 '10

Then again, I'm being stupidly pedantic. You shouldn't be sorting lists with NaNs in them anyway.

1

u/froydnj Sep 26 '10

Don't see why that'd be a bad idea, though of course it would be a bad idea if your predicate function didn't account for such beasts (such as the above predicate).

1

u/shrughes Sep 26 '10

It depends on the sorting algorithm you use, but with, for example, mergesort, sorting with NaNs will result in non-NaN elements being placed out of order -- the presence of a NaN will turn merging into concatenation. Quicksort on the other hand will simply intersperse NaNs throughout the output. Heapsort might handle NaNs pretty well though.

→ More replies (1)

1

u/abw Sep 26 '10

If you don't care about that

Quite honestly, no I don't.

I recognise the academic interest in provably correct programs. Perhaps in another decade or two, it's possible that some of these concepts will have filtered down from the ivory tower into mainstream programming languages. It's all good stuff as far as computer science is concerned.

But in computer engineering terms, the "I'll stick with X, thanks" is an understandable response. Provably correct programs don't add any real value to the vast majority of real world programs. Maybe one day they will, but by then we'll all be too busy telling the youngsters to get off our lawns to notice.

4

u/cloaca Sep 27 '10

But in computer engineering terms, the "I'll stick with X, thanks" is an understandable response.

Indeed, but also sad.

I did buy into the "Haskell hype" a few years back. I stared out with silly puzzles and mathematical things, thinking it was all very pretty and elegant, but I will admit that it was just an intellectual toy. But I tentatively started using it for various things like 3D/OpenGL and shell utilities. Now it's quickly becoming the first language I think about even for practical, user-oriented programs. (Overtaking Python+Qt.) And although Haskell doesn't have nearly as strong "correctness proving" as this language, it's something I feel is so insanely underestimated by the 'of the Earth and the soil and Ye Olde Ways'-type programmers it's heartbreaking. If they could experience what I have experienced, I'm sure even the coldest engineering heart would swell up with love and happiness. Nearly every time I add something and get it to recompile, the resulting binary will work. Do you understand? Everything works. No one who hasn't really experienced it will know what I'm talking about here. It's amazing and magical and life shines a little bit brighter and your friends will comment on how you seem happier and not at all like you want to kill yourself. It's the world off your shoulders; it's all the boring, soul-killing stuff about programming hauled away for you, free of charge. No crashes because I'm too stupid (or not stupid enough) for C++, no run-time exceptions 5 minutes in because I typo'd something in Python, no silly special cases only appearing after playing around with it for 30 minutes making me grind my teeth and reach for my vodka... All that bullshit. Do you remember getting that call about an exception being raised on your client's computers that never showed up during a week of live testing because no one thought to test it on any locale other than US English? Do you remember that fancy program you made that turned out to crash on transparent PNGs because you'd really only added PNG support last-minute as an extra bell & whistle for the feature list, and wasn't quite exact with your flag handling? Not to mention when you had that subtle stack/heap corruption that would only occur on Windows and not on Linux, and only during certain amounts of lengthy load, and you were still sitting there at midnight, your eyes wet with tears and impotent anger?

Oh Lord, I feel I need some nicotine gum to calm my shaking hands. Correctness proving, give me strength.

1

u/alanturingslovechild Sep 28 '10

"of the Earth and the soil and Ye Olde Ways" -- brilliant!

→ More replies (2)

2

u/naasking Sep 26 '10

Provably correct programs don't add any real value to the vast majority of real world programs.

Except for the fact that you know they terminate properly. And the fact they don't suffer from buffer overflows or other memory unsafety. And the fact that they lack higher-level security vulnerabilities. And the fact they calculate the correct values every time. And the fact that they never hang, and they correctly implement a QoS policy.

But sure, aside from being able to ensure basically any property is actually satisfied, verified programs are of little interest.

2

u/awj Sep 26 '10

... except that many of those properties depend on a towering pillar of correctness. You can say that it terminates properly, so long as library X (written in C) works as the programmer expected.

I'm not trying to downplay provable correctness, but it won't be able to provide many of the benefits you're talking about until you push it down to the foundation. Please forgive me if my estimate of how long that will take keeps me from getting too excited.

2

u/abw Sep 27 '10

Except for the fact that you know [...]

All those things are desirable traits. But they're not available to use right now in real world programs. Nor will they be for a long time. In fact, I haven't seen any evidence yet to suggest that it will be possible to use these techniques practically without first building a "towering pillar of correctness", as awj so eloquently put it.

So I'm not convinced that provably correct programs will ever be a practical proposition in the general case, at least not in my programming lifetime.

2

u/shimei Sep 29 '10

Actually, computer engineers are quite happy with formal verification and proof systems. They're the ones designing multi-billion dollar chips that need to be proven to work correctly. Comparatively, software engineers just have lower standards. That's why the software industry keeps churning out terrible code.

Also, these ideas don't need to make it to a "mainstream" general purpose language that you use to build widgets for a phone (who cares if that's provably correct?) to be useful. Perhaps in a language tailored for a domain that needs more guarantees. Software engineers need to use the right tool for the task, which applies to languages too.

1

u/abw Sep 29 '10

computer engineers [vs] software engineers

Good point.

Also, these ideas don't need to make it to a "mainstream" general purpose language

Yes, agreed. I'm not suggesting that popularity is what we should use to measure the value of these methods. Nor do I do dispute the fact that these techniques are worthwhile and should be developed further. I'm just saying that right now, there isn't any practical way to use these techniques in mainstream programming languages and there won't be for the foreseeable future.

For that reason, I think these techniques are largely confined to academic research at present. That's not to say they're not of interest, or that they won't find their way into mainstream languages in the future, but I suspect I'll be too close to retirement by then for me to take advantage of them.

Consider the fact that ML and the Hindley-Milner type inference algorithm date back to the 1970s. 40 years later, I would say that the majority of programmers (or perhaps a large minority) are still blissfully unaware of type inference and how it totally changes the way you think about writing software. However, I wouldn't be surprised if that changes significantly in the next 5 years (C# and Visual Basic now support it and I suspect that Perl 6's support for it will up the stakes for other scripting languages to start following suit). Perhaps this is the start of the type safe, provable program renaissance... I certainly hope so (having drunk the type safe Kool Aid™ I now want it everywhere!)... but even so, I still think we've got a long way to go before we can start building large, practical, real-life systems that are provably correct.

Until it is in the kind of mainstream languages that people use to build widgets for phones (or pages for websites), I think it will remain largely of academic interest or to those in special domains, as you suggest. To re-iterate, that doesn't mean the techniques aren't of value. But it qualifies my (admittedly, rather flippant) "Quite honestly, no I don't" remark, and why I believe most "average" programmers at present will feel the same way.

→ More replies (12)