Live data from Hacker News

Idris, a language that will change the way you think about programming (2015)

crufter.com

21–30 of 101 posts

Re: Idris, a language that will change the way you think about programming (2015)

#21
post #20
post #17

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.

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

You're gonna have to read the article to find out.

Re: Idris, a language that will change the way you think about programming (2015)

#22
post #19

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.

You can do this in Julia now, with many of the affordances you'd expect from Matlab.

    function train_model(X::Array{Float64,d}, y::Array{Float64,1})

Re: Idris, a language that will change the way you think about programming (2015)

#23
post #20
post #17

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.

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

A length n vector of a's, appended to by a length m vector of a's, results in a length (n + m) vector of a's. This is a safety guarantee that is statically checked at compile time rather than dynamically checked at run time.

Re: Idris, a language that will change the way you think about programming (2015)

#24
post #20
post #17

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.

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

You have two parameters - a vector of size "m", and a vector of size "n". The function signature then expects to get back a vector of size "m + n".

Contrast that with a language with method signatures that can only return types like "Vector" or "List", without any information that is dependent on the incoming parameters.

So then, the logic is checked - if the method returns a vector that is anything other than the size of the two incoming parameter vector sizes added together, it won't compile.

In other words, even more behavior that would normally be a runtime bug is checked at compile time, which is a good thing if you are working in a domain where correctness is important and want to avoid runtime bugs.

Re: Idris, a language that will change the way you think about programming (2015)

#25
post #20
post #17

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.

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

Basically the length of the vector is encoded at the type level. Lets you do all sorts of cool stuff.

You can have number systems modulo n, which are only compatible with other numbers modulo the same n because they are distinct types.

It's just another level of abstraction that pretty much nothing else has.

Re: Idris, a language that will change the way you think about programming (2015)

#26
post #20
post #17

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.

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

    app : Vect n a -> Vect m a -> Vect (n + m) a
is a type signature for a function, `app`, which should append a vector (`Vect`) of length `n` to a vector of length `m`.

What's cool about Idris and other dependently-typed languages is that the length of the resulting vector, `n + m`, can be tracked in the type. This can prevent a whole slew of errors.

Re: Idris, a language that will change the way you think about programming (2015)

#27
post #15

Earlier quoted context omitted.

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()

> There's nothing about braces that ensures visual cues to nested code. A machine can look at the braces and re-indent the code, so that you don't have to look at the braces. All while you rest assured that the meaning didn't change: I just popped it into Vim, selected all and hit =: int func() { while dosomething() { dosomething() dosomething() doanotherthing() } dosomething() } Some semicolons are expected so thing…

The braces didn't do anything for a human until indentations were added, whether by human or machine. If you lifted those braces out, you'd still know that the intent was (although in C it would be wrong :)

The only thing I miss about braces is being able to jump to the end of a block easily. In python and similar that's a bit harder to do with an editor.

Re: Idris, a language that will change the way you think about programming (2015)

#28
post #20
post #17

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.

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

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.

Re: Idris, a language that will change the way you think about programming (2015)

#29
post #20
post #17

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.

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

I'm not an expert in Idris, but I will try to answer this as best as I can.

That's the Haskell type notation (Idris and Haskell are similar). Here's an example:

  plus5 :: Int -> Int
  plus5 n = n + 5
The first line is the type declaration of the `plus5` function. You can read it as "plus5 takes an Int as argument and returns another Int". The second line is the function declaration. The equivalent in Python would be:

  def plus5(n):
      return n + 5
The syntax is the same when functions have more arguments:

  add :: Int -> Int -> Int
  add a b = a + b
This means that add takes two integers as arguments and returns another integer. This syntax might look strange (you could be asking yourself "How do I distinguish between the argument and return types?") but that's because in Haskell functions are curried[1].

Haskell (and Idris as well) also has parametric polymorphism, so you can define functions which operate on generic types. For example, the identity function:

  id :: a -> a
  id x = x
You can read this as "`id` is a function that takes an argument of type a and returns another argument of type a (so the return type and argument type must be the same)". This means the id function will work for any type of argument (String, Float, Int, etc.). Note that of course this is not dynamic typing, Haskell and Idris are statically typed and therefore all the types are checked at compile-time.

Now, let's move on to Idris. One of the reasons there's a lot of excitement over Idris is because it has dependent types, i.e., you can declare types that depend on values and these are checked at compile-time.

The example GP gave was this:

  app : Vect n a -> Vect m a -> Vect (n + m) a
`app` here stands for append. This is the function that appends two vectors (lists of static length) together. In Idris, `Vect x y` is a vector of size x and type y, so

   Vect 5 Int
is a vector of 5 integers. So, essentially, `app` is a function that takes two vectors as arguments, `Vect n a` and `Vect m a` (this means they can be of different sizes but their content must be of the same type). It returns a `Vect (n + m) a`.

Think about it for a minute. That is amazing! We can be sure that, at compile-time, just by looking at the function signature, our append function, given two vectors of size n and m, will _always_ return a vector of size (n + m).

If we accidentally implement a function that does adhere to the type specification (for example, it appends the first argument to itself, returning a Vect of size (n + n) instead of (n + m)), the code will not compile and the compiler will tell us where is the bug.

[1]: http://learnyouahaskell.com/higher-order-functions#curried-f...

Re: Idris, a language that will change the way you think about programming (2015)

#30
post #15
post #9

"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

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()

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.
Post reply on HN