
Idris, a language that will change the way you think about programming (2015) - kenshiro_o
http://crufter.com/2015/01/01/idris-a-language-which-will-change-the-way-you-think-about-programming/
======
bojo

        app : Vect n a -> Vect m a -> Vect (n + m) a
    

That is pretty amazing if you ask me. I look forward to the day when we all
use languages which save programmers from themselves.

~~~
vosper
As a Python programmer who doesn't understand this notation - what am I
looking at, and what's amazing about it?

~~~
badsock
The "Vector m a" means that it's a vector (like a Python list) that can only
be m elements long, and those elements can only be of type a. E.g. "Vector 3
Int" will always be a Vector with three integers in it. If you try to treat it
like a vector of any other length it will refuse to compile (e.g. pass it to a
function that requires that it have 4 or more elements).

The whole line is the type declaration for a function (app) that takes a
Vector of length m, and a Vector of length n, and produces a Vector of length
(m+n). Which is to say, the compiler knows at compile time what length the
resulting Vector will be, and will fail to compile if the function you've
written doesn't - provably - always meet that requirement.

From a Haskeller's perspective it's amazing because container types are famous
loopholes for code that produces runtime errors. For instance, if you call the
"head" function (returns the first element) on an empty list it will halt the
program at run time with an error, despite the fact that it passed the type
check (because an empty list and a full one have the same type).

As someone who learned Haskell and subsequently have been writing a lot of
Python, I keep a mental tally of how many of my bugs (some of which took ages
to track down) would have been caught immediately by a type system like
Haskell or Idris'. I'd say it's well over half.

In Haskell those kind of errors are drastically reduced, but a few still slip
through. Languages like Idris can catch even more of them, in a clever way,
which is awesome.

------
solomatov
If you are interested in Idris, you might also be interested in agda, which is
another dependently typed programming language:
[https://en.wikipedia.org/wiki/Agda_(programming_language)](https://en.wikipedia.org/wiki/Agda_\(programming_language\))

~~~
nv-vn
Other interesting ones to look at are F* (a joint effort from Microsoft
Research and INRIA to create an ML-like dependently typed language) and ATS (a
low-level, fast dependently typed language meant to replace C). What's cool
about both of these is that they put emphasis on allowing imperative
programming despite being dependently typed and seeming very functional.

------
brudgers
Idris homepage: [http://www.idris-lang.org/](http://www.idris-lang.org/)

Github: [https://github.com/idris-hackers](https://github.com/idris-hackers)

------
nihils
This is actually perfect for a library on algebraic structures I've been
trying to make in Haskell. For example, how does one distinguish between
elements in the Dihedral group of order 10 vs. Dihedral group on order 16 when
obstensibly, they have the same representation. For now, I think Haskell
programmers use type-level arithmetic libraries, but this is a much better
solution.

~~~
tikhonj
Haskell is actively moving in the direction of adding dependent types too. I
believe phase 1[1] of the plan[2] is slated for GHC 8.0 (the upcoming
release), and I'm sure the rest of it will follow soon.

It's pretty exciting!

[1]:
[https://ghc.haskell.org/trac/ghc/wiki/DependentHaskell/Phase...](https://ghc.haskell.org/trac/ghc/wiki/DependentHaskell/Phase1)

[2]:
[https://ghc.haskell.org/trac/ghc/wiki/DependentHaskell](https://ghc.haskell.org/trac/ghc/wiki/DependentHaskell)

------
athesyn
Bit disappointed it makes the assumption the reader knows Haskell, that lost
me immediately.

~~~
skosch
You know what, I've found it immensely valuable to get acquainted with
Haskell, even though I've never used it (and likely never will). The concepts
are timelessly beautiful, simple to understand, and can feel enlightening to
run-of-the-mill imperative/OOP programmers.

What's more, it seems to me that Haskell syntax is the lingua franca when
discussing anything related to data types and functional programming these
days. Those ->'s are everywhere, it's useful to know what they stand for.

Just skim a few chapters of learnyouahaskell.com; you won't regret it.

~~~
enraged_camel
I'm more of a "learn by building things" type of learner. What kinds of things
can I build with Haskell?

For example, I got into Ruby via Rails, because Rails lets you quickly
prototype simple web apps. So I could go from "I wish I had an app that does
X" to actually building it, deploying it and sharing it with others. What
would a similar "learning flow" look like in Haskell? (doesn't have to be web-
based)

Put another way, when I come across a problem, how do I recognize it as the
type of problem that is best solved using Haskell, vs. an imperative language?

~~~
Ixiaus
Warning: highly biased opinion incoming.

I came from Python to Erlang / Scheme / Haskell and at this point I would
answer your question,

> What kind of things can I build with Haskell?

With: Everything.

We use Haskell in production at Plum for our REST APIs, job schedulers, web
applications, AWS service interfaces, a static site compiler, DB modeling,
command line utilities, etc...

We also use it for two CLI utilities that are cross-compiled for the ARM9 on
our IoT product.

I consider Haskell to be superior to any of the dynamically typed languages
when writing production-level code, it's cleaner, safer, easier to maintain,
easier to refactor, and much more fun IMHO.

[EDIT] I neglected the other part of your question, "What is the learning flow
like?"

Definitely a bit rougher than Python or Ruby, I will not lie, but don't be
discouraged. It simply means you need to do a bit more studying up-front first
before you can tinker without being caught at every turn by the straight-
jacket.

I would first go through Learn You a Haskell because it is pretty accessible
and introduces the language basics well enough. Then _study the type system_.
You must learn Haskell's type system and terminology before you can understand
more advanced code.

~~~
cheepin
> With: Everything.

This may be nitpicking but certain classes of programs cannot be realistically
built with Haskell. Anytime you need to tightly control latency (soft
realtime) won't really work since you have both a garbage collector and lazy
evaluation. Memory constrained systems are pretty tough as well since you
don't really get insight into allocation/deallocation, which also makes
structuring your data into a particular memory layout tricky compared to C(++)
for instance.

Not to say that Haskell isn't awesome. It should probably be used for more
systems. It just can't be the "hammer" to make every problem into a nail.

~~~
dllthomas
This depends on what you mean by "with". As you say, you can't sit down, bang
out some ordinary Haskell code, and expect GHC to give you an executable that
will do real-time as well as you can in C.

That said, it's perfectly possible to write a DSL that handles scheduling,
uses GHC's type machinery to track memory use and execution time, and have GHC
generate a program that will generate C code that meets _hard_ real-time
guarantees. In fact, someone wrote it, it's available on hackage
([https://hackage.haskell.org/package/atom](https://hackage.haskell.org/package/atom))
and my understanding is that it's used in production for control software in
the auto industry.

~~~
Ixiaus
This as well: [http://ivorylang.org/ivory-
introduction.html](http://ivorylang.org/ivory-introduction.html)

------
blt
I recently had some murky thoughts on my ideal Matlab replacement, and it
would have a feature like this. It would be huge for array-oriented
programming:

    
    
        func train_model(X: [n d], y: [n 1])
    

So many lines of code are spent verifying that the sizes of two function
arguments are compatible.

~~~
shele
Other example: Fixed size matrix multiplication in
[https://github.com/SimonDanisch/FixedSizeArrays.jl](https://github.com/SimonDanisch/FixedSizeArrays.jl)
depends on element type T and dimensions MxN and NxR:

    
    
        function *{T, M, N, R}(a::Mat{M, N, T}, b::Mat{N, R, T})

------
MichaelBurge
I haven't used Idris, but it sounds like someone I might want. I really want a
tool that lets me mix and match operational code with proofs. It's common now
to write a unit test while debugging something, less common to write a
randomized property tester, and rare to see a theorem prover being used.

It would be fantastic if I could casually throw in a proof while debugging a
hard problem.

------
farhanhubble
Thanks for the link. I was looking into liquid Haskell and refinement types
and thought that it's a great idea but rued the fact that it wasn't built into
the language. I am definitely going to try Idris for one of my projects.

------
muhuk
But does it allow specifying vectors that has no less than three and no more
than seven items? Or vectors with even number of items?

More importantly can we possibly implement church numerals in Haskell?
/rhetorical

~~~
dllthomas
_" But does it allow specifying vectors that has no less than three and no
more than seven items? Or vectors with even number of items?"_

Yes.

------
framp
Great article and really interesting language!

I wonder how Idris is going to be affected when Dependent types come to
Haskell as well (announced at last ICFP)

~~~
jonsterling
it's nice for the haskell folks that they'll be getting some sort of dependent
types, but suffice it to say that they will be of a very different sort than
the Idris ones.

Not to mention, there is hope of giving a semantics to Idris since it is based
on a fairly routine variant of type theory, whereas I don't think there is any
hope at all of understanding "which" type theory the Haskell folks shall have
implemented.

------
vzaliva
I would suggest to use Python or some other mainstream language instead of
Haskell in example to contrast to Idris. People who know Haskell most likely
be already familiar with dependent types and for people not familiar with
Haskell syntax could be confusing (as some comments indicate).

~~~
cantankerous
Many mainstream languages like Python don't even have a syntactic notion of
types. The best you could really do is say that Idris would get you some
typing reification that you might do in those languages at no cost, but it
would seriously sell short the power of dependent types like you see in Idris.

------
Ace17
I don't get it. Is it just about rediscovering std::array?

~~~
dllthomas
Can you append a std::array<int, x> and a std::array<int, y> to get a
std::array<int, x+y> with x and y chosen at _runtime_?

~~~
Ace17
Of course not. In my understanding, the article was about static type
checking, though.

~~~
dllthomas
It _is_! That's what is so cool about it! Idris lets you write an append that
will work for any X and Y _chosen at runtime_ , but will check that the result
must have length X + Y _at compile time_.

------
jonathonf
Does anyone know whether Idris is a response to Haskell's 'Foldable'
controversy?

It looks like it was released in 2012 on hackage but I don't know how far back
'Foldable' was envisioned.

~~~
tome
It is not.

------
ophelia
Is Idris related to Idris Elba, the British actor, by any chance?

~~~
andolanra
The story, if I recall correctly, is that the programmer Edwin Brady had a
previous project that was a proof engine, and lacking a suitable name for it,
named after an older British children's cartoon called Ivor the Engine.[^1]
When it came time to name a newer project, he decided to name it after another
character from the same show: Idris, the little red Welsh dragon. This,
incidentally, is why the Idris language's logo is a stylized red wing.

[^1]: Ivor the Proof Engine is here, although apparently not actively updated
any more: [https://github.com/edwinb/Ivor](https://github.com/edwinb/Ivor)

~~~
archgoon
Thank you, I had read the FAQ:

[http://docs.idris-lang.org/en/latest/faq/faq.html](http://docs.idris-
lang.org/en/latest/faq/faq.html)

but could not infer why a dragon from Ivor the engine had been chosen. That
the author had previously written a proof engine makes all the sense now. :)

------
systems
"Indentation significant syntax"

:(

in my opinion, one of the worst ideas to plague many new languages

brackets are really, seriously, honestly a better visual cue for grouping

~~~
a3n
There's nothing about braces that ensures visual cues to nested code.
Significant whitespace does precisely that. And eliminates brace placement
arguments.

    
    
      int func()
      {
      while dosomething()
      {
      dosomething()
      dosomething()
      doanotherthing()
      }
      dosomething()
      }
      
      def func():
          while dosomething():
              dosomething()
              dosomething()
              doanotherthing()
          dosomething()

~~~
spo81rty
I've never used a language where space indentation was used over curly
brackets. So question for those who have... When doing so, wouldn't copying
and pasting code around potentially cause a lot of accidental issues? Are
there some negatives and side effects of the indentation style? Just curious.

~~~
pekk
If you copy and paste code irrespective of bracket position, you will have the
same problems. It turns out not to be an issue.

A big positive of using indentation is that it enforces a visual indication of
scope vs. languages where culturally it is normal to find code with lots of
giant one-liners and braces on one line.

But overall, you just get used to it and don't notice it. Everyone who yells
about languages that use indentation is bikeshedding in the worst way.

