Zulip Chat Archive

Stream: new members

Topic: Are Global Variables OK?


James B Clifford (Jan 22 2023 at 20:44):

I'm new to functional programming and I come from a C# background. I just wrote my first program in F# and I ended up putting the meat of my program in a class with a bunch of mutable members. I know I just mentioned F# but I am trying to learn lean, and functional programming along the way. Anyway, my question is, if I am writing a program that operates on an object, like a graph, and all the operations on that graph return a new graph, should I keep a variable that points to the current graph in global scope?

Eric Wieser (Jan 22 2023 at 20:54):

What do you mean by a "global variable" in Lean? Are you using Lean3 or Lean4?

James B Clifford (Jan 22 2023 at 21:36):

lean4. In OOP global variables are discouraged. But they appear to be OK here. I'm just trying to verify , or ask If I'm missing something.

Patrick Massot (Jan 22 2023 at 21:40):

You missed the first question, which was very important.

Patrick Massot (Jan 22 2023 at 21:40):

You will have a lot to unlearn, but this is part of the fun of switching to another programming paradigm!

Gareth Ma (Jan 22 2023 at 21:43):

Showing maybe a code snippet explaining "In OOP global variables are discouraged. But they appear to be OK here" will help.

Patrick Massot (Jan 22 2023 at 21:44):

At least in a simplified form, the final answer is probably: global variables in your sense don't exist at all in Lean 4.

Eric Wieser (Jan 22 2023 at 21:56):

With the follow up answer of "variables (x : Nat) is not declaring a global variable, it's declaring function arguments"

James B Clifford (Jan 22 2023 at 22:11):

so you mean to tell me that there is no concept of scope in lean4?

Eric Wieser (Jan 22 2023 at 22:13):

There's no concept of global scope

Eric Wieser (Jan 22 2023 at 22:15):

The closest you can reasonably get is the docs4#StateM monad which lets you implicitly thread a piece of state through a call stack

Tomas Skrivan (Jan 23 2023 at 05:18):

I think global mutable variable is more like IO.Ref and it is as bad as in other programming languages. You should think twice before using it.


Last updated: Dec 20 2023 at 11:08 UTC