programming-languagesexpressionside-effectsreferential-transparency

If everything in a programming language is an expression, would it be refferentially transparent?


I read somewhere, although I could not find it after much searching, that there was some guy who proposed to create a new programming language, which would be completely referentially transparent where everything is an expression.

Some questions about referential transparency indicate that certain languages such as Ruby and Perl have almost everything as an expression.

Is this possible/does it exist?

If I understand this correctly, if a programming language is completely referentially transparent, doesn't this mean there would be no side effects at all, and is it possible to have a complete programming language without side effects?

And the main question of the title, if the goal were to create a completely referentially transparent programming language, would it go without saying that everything would be an expression?


Solution

  • If by expression we understand anything that returns a value, a computation that is not an expression must not return value, and by referential transparency can be safely removed. So yes, referential transparency requires that everything is an expression.

    But, everything being an expression does not imply that there can be no side effects. For example in the C language there are many expressions that have side effects: take any expression involving the increment and decrement operators ++ and --.

    There are a few programming languages with no side effects. Haskell is the most widely used. It uses a clever trick called monads to resolve situations that require modification of state. For example, to print "Hello" on the screen, Haskell doesn't modify the existing universe into one where the word appears on the screen; it creates and returns a new universe where the word is on the screen.