
Quicksort in Idris - mpweiher
https://github.com/bmsherman/blog/wiki/Quicksort-in-Idris
======
majewsky
I have some experience with Haskell [1], but I'm absolutely lost here. Can
someone explain all this dependent-type syntax that's going on there, or would
that be out of scope for a single comment?

[1] The biggest Haskell program I wrote until now was a spellchecker as the
final project for a university course.

~~~
danidiaz
: is used for type annotations instead of :: like in Haskell.

The double asterisk is syntactic sugar for a "dependent pair"
[http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html...](http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html#dependent-pairs) Basically, it is a
pair in which the _type_ of the second element depends on the _value_ of the
first element.

Here it is being used to pack the result list with a proof that the list
satisfies the required properties. The _type_ of the proof must refer to the
list, hence the need for dependent types.

The stuff within braces {} are _implicit arguments_ [http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html...](http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html?highlight=braces#implicit-
arguments) that are not given explicitly, but are inferred from the type of
other arguments.

 _Type_ is commonly referred as the _kind_ * in Haskell, the kind of types
that have values.

Unlike in vanilla Haskell, the types of data constructors are explicitly
stated [http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html...](http://docs.idris-
lang.org/en/latest/tutorial/typesfuns.html?highlight=data#vectors) In Haskell
the "types" of data constructors are _kinds_ , and you need to enable the
_XKindSignatures_ extension to give them explicitly.
[https://downloads.haskell.org/~ghc/latest/docs/html/users_gu...](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html?highlight=#extension-
KindSignatures)

------
ainar-g
This is rather off-topic, but I want to learn Idris or any other purely
functional programming language one day to the point where I would be able to
read any program, but most programs written in those languages are so terse
and abstract, that I constantly find myself lost in them. The fact that most
of those languages permit programmers to define custom operators doesn't help.

I really wish someone would create something like
[https://cdecl.org/](https://cdecl.org/) but for programs in
Haskell/Idris/etc.

EDIT: On the article itself, I wonder how much time does it take to compile
the resulting program?

~~~
akhilcacharya
I'm in the same boat. I'd really love to see something that's a little bit
more beginner friendly than Hoogle.

It's honestly why I think avoiding operator overloading in Java/Go etc was a
great idea.

~~~
timjver
Isn't operator overloading something else than the ability to define custom
operators?

~~~
btilly
What people do in practice with operator overloading is turn regular operators
into custom operators that do something different. Your ability to customize
behavior is not as extreme as being able to make any custom operators that you
want. But the tradeoffs are definitely related.

~~~
seanmcdirmid
C# supports operator overloading but not custom operators. That means vectors
get to use the plus operator for addition, but no Scalaesque ~~/ operators.

C# is not a language that is generally criticized for operator soup like the
ones that support custom operators.

~~~
ainar-g
Counter-examples:

* C++ uses bitshift operators for IO. The (in?)famous
    
    
        std::cout << "hello world" << std::endl;
    

Which is _such an improvement_ over

    
    
        puts("hello world");
    

* Ruby on Rails components sometimes redefines `=` as a hash merge. E.g. this
    
    
        config.action_mailer.default_options = {from: 'no-reply@example.com'}
    

actually _adds_ `from` to the default options instead of overriding it
completely.

------
RcouF1uZ4gsC
What's interesting is that an algorithm is presented as correct, but which
blows up to quadratic behavior given the right input, and since since
quicksort uses than tail recursion, could potentially cause a stack overflow.
Thus you have a correct, safe algorithm in theory but not in practice.

~~~
AzzieElbab
Could you explain about tail-rec causing stack overflow?

~~~
garmaine
Looks like a typo. Quick sort is binary recursive so tail call optimization
wouldn’t fix it.

------
AstralStorm
Why oh why all the functional programmers I know use sExps. ;)

You didn't guess the right expansion. I meant shortened expressions. Say a
function called forallPerm.

There are million better names most of which start by saying _what it does_.
Even foldAllPermutations would be better (if you know what a fold is).

If you're writing in M-expressions do it consistently. If it is logic proof,
it is not functional. There I'd expect some lemma names. In plain English.

The datatype called LTEL takes the cake.

~~~
gmfawcett
> If it is logic proof, it is not functional.

Logic proofs _are_ functional programs; at least, they are isomorphic to them,
by Curry-Howard.

[https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspon...](https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence)

