Therese Hardin

Concepts and Semantics of Programming Languages 1


Скачать книгу

:= v2].

      For example, (Mem[r1:= v1 ][r2:= v2])(r) = v2.

      The function valeur_ref computes the value stored at a given location. If nothing has previously been written at this location, the function returns a special value (None), indicating the absence of a known value (i.e. a value resulting from initialization or computation).

      The following function writes a value into the memory:

      Python def write_mem(mem,a,v): if len(mem) == 0: return [(a,v)] else: a1,v1 = mem[0] if a == a1: return [(a1,v)] + mem[1:] else: return [(a1,v1)] + write_mem(mem[1:],a,v)OCaml let rec write_mem mem a v = match mem with | [] -> [(a, v)] | (a1, v1) :: t -> if a = a1 then (a1, v) :: t else (a1, v1) :: (write_mem t a v) val write_mem : (’a * ’b) list -> ’a -> ’b -> (’a * ’b) list

      A state is defined as a pair (Env, Mem) ∈ E × M such that any reference in the domain of Mem is accessible from a binding in Env. A reference is said to be accessible if its value can be read or written from an identifier contained in Env by a series of operations of reading, writing, or reference manipulation.

      Given an environment Env, the set of identifiers X is partitioned into two subsets: image which contains the identifiers bound to a reference, and image, which contains the others:

image

      The value associated with an identifier x in image is a reference Env(x) = r where a value Mem(r) is stored, which can be modified by writing. Identifiers of image are generally called mutable variables.

      The value of an expression is computed according to an evaluation environment and a memory, i.e. in a given state. This computation is defined by the evaluation semantics of the expression.

      2.2.1. Syntax

e ::= k Integer constant (k ∈ ℤ)
| x Identifier (xX)
| e1 + e2 Addition (e1, e2Exp1)
| !x Dereferencing (xX)

      Thus, an expression eExp1 is either an integer constant k ℤ, an identifier x ∈ X, an expression obtained by applying an addition operator to two expressions in Exp1 or an expression of the form !x denoting the value stored in the memory at the location bound to the mutable variable x. Thus, this is an inductive definition of the set Exp1. Note that Exp1 does not include an assignment construct. This is a deliberate choice. This point will be discussed in greater detail in section 2.3 by means of an extension of Exp1.

      NOTE. – The symbol + used in defining the syntax of expressions does not denote the integer addition operator. It could be replaced by any other symbol (for example ⊠). Its meaning will be assigned by the evaluation semantics. The same is true of the constant symbols: for example, the symbol 4 may be interpreted as a natural integer, a relative integer or a character.

      EXAMPLE 2.1.– !x + y is an expression of Exp1 in the same way as (x + 2) + 3. Parentheses are used here to structure the expression, they are part of the so-called concrete syntax and will disappear in the AST.

      Using Python, we define the following classes to represent the constructors of the set Exp1:

      Python class Cstel: def __init__(self,cste): self.cste = cste class Var1: def __init__(self,symb): self.symb = symb class Plusl: def __init__(self,exp1,exp2): self.exp1 = exp1 self.exp2 = exp2 class Bang1: def __init__(self,symb): self.symb = symb

      For example, the expression e1 = !x + y defined in example 2.1 is written as:

      Python ex_expl = Plusl(Bangl(“x”),Varl(“y”))

      Using OCaml, the type of arithmetic expressions is defined directly as:

      OCaml type ’a exp1 = Cstel of int | Var1 of ’a | Plusl of ’a expl * ’a expl | Bangl of ’a

      Values of this type are thus obtained using either the Cste1 constructor applied to an integer value, in which case they correspond to a constant expression, or using the Var1 constructor applied to a value of type ’a, corresponding to the type used to represent identifiers (the type ’a exp1 is thus polymorphic, as it depends on another type), or by applying the Plus1 constructor to two values of the type ’a expl, or by applying the Bang1 constructor to a value of type ’a. For example, the expression e1 = !x + y is written as:

      OCaml let ex_exp1 = Plus1 (Bang1 (“x”), Var1 (“y”)) val ex_exp1 : string exp1

      2.2.2.