0

I'm trying to simulate global and local variables in Coq, but I don't even know how to start. Is there anybody that can give me a hint or some pieces of advice? I read a lot of documentation about this programming language, but I can't figure it out. Thank you in advance!

  • Do you want to prove something about a program written in a language with global variables? Or do you want to write a program in Coq's language? (If it is the latter, what program is it that you want to write, and why does it have to use global variables?) – larsr Dec 19 '20 at 12:08

1 Answers1

2

It depends on what you mean by "local" and "global". Variables in Coq work differently from most programming languages, because they cannot be modified. The closest thing to a global variable is a top-level definition, and the closest thing to a local variable would be a local definition:

Definition i_am_a_global : nat := 42.

Definition my_function (my_parameter : nat) : nat :=
  (* Function parameters are always local *)
  let my_local := my_parameter + my_parameter in
  my_local + i_am_a_global.
Arthur Azevedo De Amorim
  • 23,012
  • 3
  • 33
  • 39
  • I see your point and I understand what you explained to me (really appreciated, btw). The fact is, I am trying to "re-write" some functionalities from C++ in Coq and this one was a challenging one to me because I learned how to use variables in Coq, but not to create some sort of new type (scope) that can be either global or local. – geeky90846 Dec 17 '20 at 22:17
  • @geeky90846 Maybe you could include in your question a code snippet in C++ that you would like to translate to Coq. – Arthur Azevedo De Amorim Dec 18 '20 at 13:39
  • Let's consider this program: // Global variable declaration: int g; int main () { // Local variable declaration: int a, b; // actual initialization a = 10; b = 20; g = a + b; cout << g; return 0; } – geeky90846 Dec 19 '20 at 20:55
  • @geeky90846 I am not sure which feature exactly you would like to translate. Usually in Coq you cannot produce output from a program. And there isn't a way of declaring a variable first and only defining it later... – Arthur Azevedo De Amorim Dec 20 '20 at 01:56
  • Alright, I think I got it! Thanks for helping me out! – geeky90846 Dec 21 '20 at 07:32