Questions tagged [referential-transparency]

A property of a function, variable, or expression whereby the expression can be replaced by its (evaluated) value without affecting the behavior of the program.

Overview

Referential transparency is a property of some computer programs and programming languages. The property is present if a function, variable or expression in the program is immune to "side effects".

The immunity to side-effects exists when the function, variable or expression can be replaced with its (evaluated) value, without affecting the behavior of the program.

See also

78 questions
334
votes
15 answers

What is referential transparency?

What does the term referential transparency mean? I've heard it described as "it means you can replace equals with equals" but this seems like an inadequate explanation.
Claudiu
  • 224,032
  • 165
  • 485
  • 680
76
votes
7 answers

Purity vs Referential transparency

The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for…
46
votes
8 answers

Is Haskell really a purely functional language considering unsafePerformIO?

Haskell is generally referenced as an example of a purely functional language. How can this be justified given the existence of System.IO.Unsafe.unsafePerformIO ? Edit: I thought with "purely functional" it was meant that it is impossible to…
39
votes
8 answers

Is Haskell truly pure (is any language that deals with input and output outside the system)?

After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard…
39
votes
9 answers

Are there any purely functional Schemes or Lisps?

I've played around with a few functional programming languages and really enjoy the s-expr syntax used by Lisps (Scheme in particular). I also see the advantages of working in a purely functional language. Therefore: Are there any purely functional…
35
votes
5 answers

Does Haskell have variables?

I've frequently heard claims that Haskell doesn't have variables; in particular, this answer claims that it doesn't, and it was upvoted at least nine times and accepted. So does it have variables or not, and why? This question also appears to apply…
cjs
  • 25,752
  • 9
  • 89
  • 101
25
votes
4 answers

How do functional languages model side-effects?

Since side-effects break referential transparency, don't they go against the point of functional languages?
25
votes
2 answers

Zipper like data structure with more than one cursor

The Zipper data structure is great when one wants to traverse a tree and keep the current position, but what data structure one should use if they want to track more then one position? Let me explain with examples: Someone on the #haskell channel…
21
votes
3 answers

Why are getArgs and getProgName IO actions?

I'm a complete newbie currently trying to learn Haskell with "Learn You a Haskell for Great Good". I've reach the section explaining how to work with command line arguments, and something is bugging me. From my understanding (and haskell.org's…
icecrime
  • 74,451
  • 13
  • 99
  • 111
20
votes
2 answers

How to catch (and ignore) a call to the error function?

I'm surprised I couldn't find an answer to this anywhere. I'm writing a roguelike and I'm using the ncurses library from hackage, which is a pretty good wrapper around the ncurses library. Now ncurses has this quirk where if you try to write the…
David McHealy
  • 2,471
  • 18
  • 34
18
votes
7 answers

Type inference interferes with referential transparency

What is the precise promise/guarantee the Haskell language provides with respect to referential transparency? At least the Haskell report does not mention this notion. Consider the expression (7^7^7`mod`5`mod`2) And I want to know whether or not…
false
  • 10,264
  • 13
  • 101
  • 209
18
votes
3 answers

Sampling sequences of random numbers in Haskell

I need small lists of gaussian random numbers for a simulation and so I tried the following: import System.Random seed = 10101 gen = mkStdGen seed boxMuller mu sigma (r1,r2) = mu + sigma * sqrt (-2 * log r1) * cos (2 * pi * r2) This is just the…
17
votes
2 answers

Optimization of Function Calls in Haskell

Not sure what exactly to google for this question, so I'll post it directly to SO: Variables in Haskell are immutable Pure functions should result in same values for same arguments From these two points it's possible to deduce that if you call…
user500944
17
votes
1 answer

Referential transparency with polymorphism in Haskell

Say I have a function: f :: Int -> (Rational, Integer) f b = ((toRational b)+1,(toInteger b)+1) I want to abstract away the (+1) like so: f :: Int -> (Rational, Integer) f b = (h (toRational b) ,h (toInteger b)) where h = (+1) This wont…
16
votes
2 answers

"inject" progress logging/tracing in haskell computation?

I'm picking a specific task to illustrate what I was talking about Let's say I wanted to find the sum of all the factors of a large number, naively -- by checking every number below it if it was a factor, then adding them together. In an imperative…
Justin L.
  • 13,510
  • 5
  • 48
  • 83
1
2 3 4 5 6