views:

1035

answers:

9

I'm trying to figure out if Haskell uses dynamic or static scoping. I realize that, for example, if you define:

let x = 10

then define the function

let square x = x*x

You have 2 different "x's", and does that mean it is dynamically scoped? If not, what scoping does it use, and why?

Also, can Haskell variables have aliases (a different name for the same memory location/value)?

Thanks.

+6  A: 

Haskell use (broadly speaking) exactly the same lexical scoping as most other languages.

eg.

x = 10

Results in a value referenced through x in the global scope, whereas

square x = x * x

will result in x being lexically scoped to the function square. It may help if you think of the above form being a syntactic nicety for:

square = \ x -> x * x

As to your other question i'm not sure what you mean by aliasing

olliej
+7  A: 

There are some things wrong in your statements...

  • There are no mutable variables in Haskell just definitions (or immutable variables)
  • A variable memory location is a concept that do not exist in Haskell

In your example, x is not 10 in the function is just a argument to square, that can take any value (you can specify the type later) in this case 10 but just in this case.

Here is an example of aliases provided by Curt Sampson:

import Data.IORef

main :: IO ()
main = do x <- newIORef 0         -- write 0 into x
          readIORef x >>= print   -- x contains 0
          let y = x
          readIORef y >>= print   -- y contains 0
          writeIORef x 42         -- write 42 into x
          readIORef y >>= print   -- y contains 42
igorgue
Certainly Haskell has variables. See http://stackoverflow.com/questions/993124/does-haskell-have-variables/993126
Curt Sampson
no, it doesn't have variables!, "definitions" is the right term, or if you like calling them variables, then are immutable variables just like in Erlang. Still this is very different from the imperative concept of "variables".
igorgue
igorgue, if you believe that Haskell doesn't have variables, please post an answer to the question linked above telling us why it doesn't. Do include the "definition" of `x` in `f x = x * 2`.
Curt Sampson
As far as aliases go, I have provided a new answer to this question demonstrating aliasing in Haskell.
Curt Sampson
Edited my answer, thanks for your feedback
igorgue
I'm not sure what you mean by "variable memory location." If you're talking about mutable memory locations, you go on to give an example of exactly that in Haskell. If you're talking about "variables," that's a binding of a name to a value that varies over a range; memory locations are irrelevant. (You'll note that mathematical equations have variables, but no memory locations.)
Curt Sampson
Additionally, I don't the the question ever claimed that there are mutable variables in Haskell.
Curt Sampson
I meant that you don't think of a variable's memory location
igorgue
"A variable memory location is a concept that does not exist in Haskell"... uh, I don't see why variables in Haskell cannot live in a memory location. They simply are read-only. Now if you mean that memory locations are themselves variable, and Haskell does not support that, okay, but I don't know of ANY language that supports that. And I think your term of "definition" is bad. Are function parameters definitions? I think not. They are variables. They /vary/ between calls. Bone up on some math: http://en.wikipedia.org/wiki/Variable_%28mathematics%29
trinithis
+4  A: 

Answering only the second part of the question:

You can have several aliases for the same "memory location", but since they are all immutable, it does not matter most of the time.

Dumb example:

foo x y = x * y
bar z = foo z z

When within foo called from bar, both x and y are clearly the same value. But since you cannot modify either x or y, you will not even notice.

CesarB
+3  A: 

As the first part of the question is already answered by others, here is the second part:

I assume by aliasing you mean one name for another. As haskell is a functional language, and functions behave as normal identifiers in any case, you can do that like this:

y = x

which would define an alias y for the function x. Note that everything is a function. Even if it looks like a "variable", it's just a nullary function taking no arguments. Aliases for types look like this:

type Function = Double -> Double

which would define an alias Function for the type Double -> Double

Johannes Schaub - litb
"a nullary function taking no arguments" — I'd just point out that these comments don't hold true in the case of unlifted values.
Porges
+1  A: 

In your example, the global definition of x is shadowed by the local definition of x. In Haskell, a variable's scope is determined by a static reading of the source code - this is called lexical scope, but can get something similar to dynamic scoping with implicit parameters (but that can lead to some unexpected behavior (I've read; never tried 'em myself)).

ja
+1  A: 

To sum up the other answers concisely:

  1. lexical scope
  2. aliasing is as easy as x = 1; y = x but doesn't usually matter because things are immutable.

The let syntax you use in your example looks like it's at the interactive ghci> prompt. Everything in interactive mode occurs within the IO monad so things may appear more mutable there than normal.

Nathan Sanders
A: 

Well, as I think people have said already, Haskell doesn't have any variables as found in most other languages, it only has expressions. In your example let x = 10 x is an expression that always evaluates to 10. You can't actually change the value of x later on, though you can use the scoping rules to hide it by defining x to be another expression.

Magnus
I've tried them, and the behaviour is actually fairly clear, because you need to declare dynamically scoped variables explicitly in the type definition if you use them.
Curt Sampson
+1  A: 

Haskell uses static nested scopes. What is a bit confusing compared with other languages that have static nested scopes is that the scope of a name is a block which includes tests preceding its definition. For example

evens = 0 : map (+1) odds
odds  = map : (+1) evens

here the name 'odds' is in scope in the definition of 'evens', despite the surprising fact that 'odds' has not yet been defined. (The example defines two infinite lists of even and odd numbers.)

A dead language with a similar scoping rule was Modula-3. But Haskell is a bit trickier in that you can attempt to 'redefine' a variable within the same scope but instead you just introduce another recursion equation. This is a pitfall for people who learned ML or Scheme first:

let x = 2 * n
    x = x + 1   -- watch out!

This is perfectly good ML or Scheme let*, but Haskel has scheme letrec semantics, without the restriction to lambda values. No wonder this is tricky stuff!

Norman Ramsey
Err...I think you mean that the scope "includes other definitions following the definition," or something like that, don't you?
Curt Sampson
+1  A: 

Yes, Haskell has aliases. Try out this little program:

import Data.IORef

main :: IO ()
main = do x <- newIORef 0         -- write 0 into x
          readIORef x >>= print   -- x contains 0
          let y = x
          readIORef y >>= print   -- y contains 0
          writeIORef x 42         -- write 42 into x
          readIORef y >>= print   -- y contains 42
Curt Sampson