6 ms·
>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here i
by im3w1l 13y ago
>What this means is that existing sort functions such as mergeSort and quickSort would need to be rewritten to sort values of the new type. In constrast, here is the type of mergeSort in Haskell mergeSort :: Ord a => [a] -> [a]
Er, you could define mergesort using templates in c++ and it would work pretty much the same.
- DannoHung 13y agoHow do you express the Ordinality constraint in C++?
- GeneralMayhem 13y agoMultiple inheritance from a purely virtual class. In Java, they're called interfaces; Comparable is the analogue to Ord. The advantage that Haskell's typeclasses have over either Java or C++ is that the types aren't closed. I can define a new typeclass and tell the compiler that a type you defined is a member of that class at any point. In Java, if you don't define that your type is Comparable, there's nothing that I, as a consumer of your type, can do about it.
- chongli 13y agoYeah, though you will want to be the owner of either the type class or the type itself. Instances which are neither are called orphan instances and they are generally frowned upon. Luckily, it's trivial to create a newtype wrapper which gives you a whole new set of hooks for writing instances of a type.
- dllthomas 13y agoOrphan instances in libraries are frowned upon. In an application they are more palatable, though a newtype might still be a better choice, depending.
- GeneralMayhem 13y agoThat makes sense, but again, in Java you can't do it if you're just the owner of the typeclass, and in order to make a wrapper you'd have to make a subclass, which mucks with all kinds of other things.
- Peaker 13y agoThe advantage you mention is one of many (over OOP interfaces), I'll repeat it as (A). (A) Ability to instantiate old types with new classes (B) Conditional instantiation: "Maybe a" is an instance of "Show" iff "a" is an instance of "Show" (C) Can do return-type polymorphism, or polymorphism on any part of the signature (D) Objects do not need to pay for the interface vtable pointer. Large array of some type-class instance can be much smaller than an array of an OOP interface subclass. (E) When comparing two Ord instance values, there is only one Ord dictionary, rather than 2 to choose from. No false dilemma.
- GeneralMayhem 13y ago(C) is covered by generics in most cases. The others, yes.
- judk 13y agoGenerics aren't ad-hoc polymorphic. Type classes are.
- judk 13y agoE) you do get a dilemma if you add two elements in a sorted structure , lexically in two different files, where two different Ord instances arr in scope, respectively. this function-is-instance problem is dual to OO's -object-is-instance problem.
- Peaker 13y agoA homogenous list is guaranteed to only have one instance unless you enable incoherent instances (a known bad extension) or confuse the compiler with orphan instances (though I've never encountered this and think it should be detectable at link time anyway). One type -> at most one instance for any class, in Haskell.
- cozmorado 13y agoConcepts will accomplish this when they finally make it into the standard.
- pbsd 13y agoConcepts will certainly be nice once they arrive in the standard, but it is already possible today with some trickery. Example checking whether an std::vector<T>'s type is comparable with `operator<`: http://coliru.stacked-crooked.com/a/2ea46e0afd894d3d http://coliru.stacked-crooked.com/a/2ea46e0afd894d3d The error you get if you try to sort a vector with an unordered type is relatively easy to read: main.cpp: In instantiation of 'void merge_sort(std::vector<_RealType>&) [with T = type]': main.cpp:45:17: required from here main.cpp:30:5: error: static assertion failed: Element type is not sortable static_assert( concept::is_ord<T>(), "Element type is not sortable" ); ^
- Rusky 13y agoThe existence of an operator< or the existence of a template specialization. Not as nice, and incapable of runtime polymorphism, but that's typically how it's done. Concepts would have been more explicit about it but they're also compile-time only. Interfaces or pure virtual classes are not the same because they're closed.
- GFK_of_xmaspast 13y agoYeah, I don't get why that's superior to something like std::sort : http://en.cppreference.com/w/cpp/algorithm/sort http://en.cppreference.com/w/cpp/algorithm/sort (or a similar approach however you like).
- acomar 13y agoThe difference is entirely in the types. A sort function in Haskell has type: Ord a => [a] -> [a] You therefore know that the implementation of the function can only make use of functions provided by Ord, and manipulation of the list structure. The function cannot inspect the values in the list except via comparison. This is an incredibly powerful guarantee. It's also really useful when you go to implement the function because this level of parametricity guarantees that there are only so many valid implementations and only a couple of them are even close to correct.
- millstone 13y agoAll true. On the other hand, std::sort in C++ works on RandomAccessIterator, which means that it can sort vectors, C arrays, deques, strings, your custom container type, etc. The Haskell implementation can only sort lists. Furthermore, the Haskell type signature fails to express that the input and output have the same size. But std::sort necessarily preserves its size, because you pass it a range, and a range cannot access its underlying container to change its size (a limitation leading to bizarre APIs like std::unique).
- Peaker 13y agoYou can define a mutable std::sort like function in Haskell: mutableSort :: (MArray arr elem m, Ord elem, Ix ix) => arr ix elem -> m () This can work on mutable arrays in the IO, ST, or other mutable-array supporting monads. It also has a generalized index type (though newer array libraries threw that part away, so it's not considered a good idea anymore).