1

I'm trying to come up with a syntax and semantics for a simple c-like program in coq. This is for a uni course I'm taking on the principles of programming languages.

I have checked the site for similar questions and only found two threads that got close to what I needed, but they were not really on par with what I am required to do.

Long story short, I currently have this definition for a class:

Inductive Object :=
| object : string -> list_variable -> Function -> Object.

Where list_variable is declared as:

Inductive list_variable : Type :=
| null
| cons (s : string) (l : list_variable).

and Function is declared as:

Inductive Function :=
| function : string -> list_variable -> Function.

All of this should be included in the following statement, which is what I need to use to build some test programs that should run without error:

Inductive Stmt :=
| var_declare : string -> Stmt 
| object_declare : Object -> string -> Stmt
| function_declare : function -> Stmt -> Stmt
| assignment : string -> Exp -> Stmt
| sequence : Stmt -> Stmt -> Stmt
| ifthenelse : Exp -> Stmt -> Stmt -> Stmt
| ifthen : Exp -> Stmt -> Stmt
| while : Exp -> Stmt -> Stmt.

Exp is simply an inductive type that has variables, numbers, arithmetic and boolean expressions.

My question is, how can I simulate a memory-like environment where I can define an object, create an instance for said object and be able to access at the very least the member variables for a specific instance?

I got some hints which resume to using two maps, one from variables (or objects) to memory addresses and the other one from memory addresses to values. So, it should be something of the form:

Definition Address := Object -> ObjAddress.
Definition ObjValues := Address -> Value (nat, bool or string)

In the end, I would like to have something among the lines of:

class example {member variables and methods};
example e1;
e1.variable=1.

Do you have any ideas as to how this could be coded into coq?

1 Answers1

1

A "c-like" (as you say) language is, I would think, a language where you have problems with aliasing, i.e. where you can have multiple references to the same memory location, and where references can have different types. But you don't seem to have type declarations for your variables. Perhaps you only have ints in your language?

C also has a stack and a heap and pointers that can point to either, and thus aliased references may become invalid when a function returns. Your language also does not seem to have pointers or malloc so your language probably doesn't have to deal with that.

But if you really do want to do that, then I suggest you look at how it is done in CompCert which has a beautiful way of modelling memory. The CompCert Memory Model, Version 2

But say that you don't want to deal with all this mess and aliasing (which I think is what you really ment), then you can just model memory as a function mem from variable name to value (nat).

larsr
  • 5,447
  • 19
  • 38
  • Yes, I do not really have to deal with complicated memory stuff, only a barebones c-like syntax. I don't plan to implement anything other than `int` for the time being. I'll definintely take a look at the document you referenced. Thanks a lot! – Radu Deleanu Jan 09 '21 at 17:47