0

So, I was wondering about how Haskell's lists are implemented. I looked it up, and found this:

data [] a = [] | a : [a]

So I get that you can write lists like this if you want to:

a:b:c:[] -- instead of [a, b, c]

But my question is: How is the list syntax that is usually used (the [a, b, c] syntax) implemented?

Edit: I want to know the implementation, so if anyone could point me to the right standard library file, that would be much appreciated.

2
  • I'm not entirely clear on what you're asking. Do you mean to ask about the compiler internals? Jan 8, 2020 at 15:24
  • 2
    Can you clarify what kind of answer you are looking for? The answer is already in your question: it is syntactic sugar. Are you looking for the exact file where it is implemented? If yes, which of the hundreds of versions of dozens of Haskell Interpreters and compilers are you talking about? Jan 8, 2020 at 15:25

1 Answer 1

3

The fact that you are asking for a library suggests that you are misunderstanding when this happens. The text that you use to write code is for humans. It's not what the machine is executing. The step that converts the code you are looking at into something that is usable by a computer is called Compilation.*

The term 'syntactical sugar' refers to a language feature that is equivalent to something that is more fundamental. In other words, the compiler will convert both the 'sugar' style, and the fundamental style to the exact same thing. In the executing application, where an application library is relevant, there's nothing more to do because there would be no way to distinguish between these two approaches.

*There's also a concept of 'interpreted' implementations of a language. The distinction doesn't change anything about how we understand syntactical sugar.

2
  • So what you're saying is that the Haskell compiler automatically converts [1, 2, 3] into 1:2:3:[] and it's not implemented in the standard library? Jan 8, 2020 at 19:08
  • Basically, yes. You can think of it that way. Whether that's exactly what happens in the compiler would depend on the specifics of the implementation of the compiler. Think of it this way, I can represent the number (decimal) 10 as (roman numeral) X, (hexidecimal) x0A, or (scientific notation) 1.0E1. They all mean the same thing but in memory they would (typically) be represented as (binary) 1010.
    – JimmyJames
    Jan 8, 2020 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.