source: CIVL/doc/manual/part-semantics.tex@ a054a5b

1.23 2.0 main test-branch
Last change on this file since a054a5b was 9f53b6c, checked in by Stephen Siegel <siegel@…>, 12 years ago

Adding the parts of the manual.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@644 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 28.6 KB
RevLine 
[9f53b6c]1\part{Semantics}
2\label{part:semantics}
3
4\chapter{CIVL Model Syntax}
5
6\section{Notation and terminology}
7\label{sec:notation}
8
9Let $\B=\{\true,\false\}$ (the set of boolean values). Let
10$\N=\{0,1,2,\ldots\}$ (the set of natural numbers).
11
12Given a node $u$ in a tree, we let $\ancestors(u)$ denote the set of
13all ancestors of $u$, including $u$. We let $\descendants(u)$ denote
14the set of all descendants of $u$, including $u$.
15
16For any set $S$, let $S^*$ denote the set of all finite sequences of
17elements of $S$. The length of a sequence $\xi\in S^*$ is denoted
18$\len(\xi)$.
19
20% This is a lie:
21% The elements of the sequence are indexed from $0$ to
22% $\len(\xi)-1$.
23
24\section{Definition of Context}
25\label{sec:context}
26
27\begin{definition}
28 A \emph{CIVL type system} is a tuple comprising the following
29 components:
30 \begin{enumerate}
31 \item a set $\Type$ (the set of \emph{types}),
32 \item a type $\bool\in\Type$ (the \emph{boolean type}),
33 \item a type $\proc\in\Type$ (the \emph{process-reference type}),
34 \item a set $\Var$ (the set of all \emph{typed variables}),
35 \item a function $\vtype\colon \Var\ra\Type$ (which gives the
36 type of each variable),
37 \item a set $\Val$ (the set of all \emph{values}),
38 \item a function which assigns to each $t\in\Type$ a subset
39 $\Val_t\subseteq\Val$ (the set of values of type $t$)
40 and which satisfies $\Val_{\bool}=\B$ and $\Val_{\proc}=\N$,
41 \item a function which assigns to each $t\in\Type$ a value
42 $\default_t\in\Val_t$.
43 \end{enumerate}
44\end{definition}
45
46The default value will be used to give an initial value to any
47variable. It could represent something like ``an undefined value of
48type $t$'' or a reasonable initial value ($0$ for integers, etc.),
49depending on the language one is modeling.
50
51\begin{definition}
52 Given a CIVL type system, a \emph{valuation} in that system is a
53 function $\eta\colon\Var\ra\Val$ with the property that for any
54 $v\in\Var$, $\eta(v)\in\Val_{\vtype(v)}$.
55\end{definition}
56
57Given a CIVL type system, we let $\Eval$ denote the set of all
58valuations in that system.
59
60\begin{definition}
61 Given a CIVL type system, A \emph{CIVL expression system} for that
62 type system is a tuple comprising the following components:
63 \begin{enumerate}
64 \item a set $\Expr$ (the set of all \emph{typed expressions} over
65 $\Var$),
66 \item a function $\etype\colon\Expr\ra\Type$ (giving the type
67 of each expression),
68 \item a function
69 $\eval\colon\Expr\times\Eval\ra\Val$ (the \emph{evaluation
70 function}), satisyfing
71 \begin{itemize}
72 \item for any $e\in\Expr$ and $\eta\in\Eval$,
73 $\eval(e,\eta)\in\Val_{\etype(e)}$,
74 \end{itemize}
75 \item a function which associates to any $V\subseteq\Var$, a
76 subset $\Expr(V)\subseteq\Expr$ (the set of
77 \emph{expressions which involve only variables in $V$}) satisfying
78 the following:
79 \begin{itemize}
80 \item for any $V\subseteq\Var$ and $\eta,\eta'\in\Eval$, if
81 $\eta(v)=\eta'(v)$ for all $v\in V$, then for any $e\in\Expr(V)$,
82 $\eval(e,\eta)=\eval(e,\eta')$
83 \end{itemize}
84 \end{enumerate}
85\end{definition}
86
87\begin{definition}
88 A \emph{CIVL context} is a CIVL type system together with
89 a CIVL expression system for that type system.
90\end{definition}
91
92\begin{figure}
93 \notationtable
94 \caption{Table of Notation Used to Define CIVL Model Syntax}
95 \label{fig:notation}
96\end{figure}
97
98\section{Lexical scopes}
99\label{sec:scopes}
100
101\begin{definition}
102 Given a CIVL context $\mathcal{C}$, a \emph{lexical scope system}
103 over $\mathcal{C}$ is a tuple $(\Sigma,\rootscope,\sparent,\vars)$
104 consisting of
105 \begin{enumerate}
106 \item a set $\Sigma$ (the set of \emph{static scopes}),
107 \item a scope $\rootscope\in\Sigma$ (the \emph{root scope}),
108 \item a function
109 $\sparent\colon\Sigma\setminus\{\rootscope\}\rightarrow
110 \Sigma$ such that
111 \[\{(\sparent(\sigma),\sigma)\mid \sigma\in\Sigma\setminus\{\rootscope\}\}\]
112 gives $\Sigma$ the structure of a rooted tree with root $\rootscope$,
113 \item a function $\vars\colon\Sigma\rightarrow 2^{\Var}$
114 (specifying the variables \emph{declared} in each scope) satisfying
115 \begin{itemize}
116 \item $\sigma\neq\tau\implies \vars(\sigma)\cap \vars(\tau)=\emptyset$.
117 \end{itemize}
118 \end{enumerate}
119\end{definition}
120
121\begin{definition}
122 Given a CIVL context and scope $\sigma\in\Sigma$,
123 the set of \emph{visible variables} in $\sigma$
124 is $\bigcup_{\sigma'\in\ancestors(\sigma)}\vars(\sigma')$.
125\end{definition}
126
127One way this notion will be used: expressions used in a scope $\sigma$
128can only involve variables visible in $\sigma$.
129
130\section{Functions}
131\label{sec:functions}
132
133Fix a CIVL context $\mathcal{C}$ and lexical scope system
134\[\Lambda=(\Sigma,\rootscope,\sparent,\vars)\] over $\mathcal{C}$.
135
136We introduce a new type symbol $\void$, as in C, to use as the return
137type for a function that does not return a value. Let
138$\Type'=\Type\cup\{\void\}$.
139
140\begin{definition}
141 A \emph{function prototype} for $\Lambda$ is a tuple
142 $(\sigma, t, \xi)$ consisting of
143 \begin{enumerate}
144 \item a scope $\sigma\in\Sigma\setminus\{\rootscope\}$
145 (the \emph{function scope}),
146 \item a type $t\in\Type'$ (the \emph{return type}),
147 \item a finite sequence $\xi=v_1v_2\cdots v_n\in\vars(\sigma)^*$
148 consisting of variables declared in the function scope
149 (the \emph{formal parameters}).
150 \end{enumerate}
151\end{definition}
152
153\begin{definition}
154 A \emph{CIVL function prototype system} consists of
155 \begin{enumerate}
156 \item a set $\Func$ (the \emph{function symbols}),
157 \item a function which assigns to each $f\in\Func$ a
158 function prototype, denoted
159 \[
160 (\fscope(f), \returntype(f), \params(f)),
161 \]
162 and satisfying
163 \begin{itemize}
164 \item for any $\sigma\in\Sigma$, there is at most
165 one $f\in\Func$ such that $\sigma=\fscope(f)$, and
166 \end{itemize}
167 \item a \emph{root function} $f_0$ with $\fscope(f_0)=\rootscope$
168 and which is the only function with root scope.
169 \end{enumerate}
170\end{definition}
171
172
173\begin{definition}
174 Given a CIVL function prototype system, and function symbol
175 $f\in\Func\setminus\{f_0\}$, the \emph{declaration scope} of $f$ is
176 the scope $\sigma=\sparent(\fscope(f))$. We also write \emph{$f$ is
177 declared in $\sigma$.}
178\end{definition}
179
180Note the root function $f_0$ has no declaration scope.
181
182Just as every scope has a set of visible variables, there is also
183a set of visible functions:
184\begin{definition}
185 The functions \emph{visible at scope $\sigma$} are
186 those declared in $\sigma$ or an ancestor of $\sigma$.
187\end{definition}
188We will see that the variables and functions visible at $\sigma$ are
189the only variables and functions that can be referred to by statements
190and expressions used within $\sigma$.
191
192Note that only certain scopes are function scopes. There can be
193additional scopes (intuitively corresponding to block scopes in a
194source program). Every scope, however, must ``belong to'' exactly one
195function. The precise definition is as follows:
196\begin{definition}
197 \label{def:func}
198 Given a CIVL function prototype system, define
199 \[
200 \func \colon \Sigma \ra \Func
201 \]
202 by
203 \[
204 \func(\sigma)=
205 \begin{cases}
206 f & \text{if $\sigma=\fscope(f)$ for some $f\in\Func$}\\
207 \func(\sparent(\sigma)) & \text{otherwise.}
208 \end{cases}
209 \]
210 We say \emph{$\sigma$ belongs to $f$} when $\func(\sigma)=f$.
211\end{definition}
212Note that the recursion in Definition \ref{def:func} must stop as the
213root scope belongs to the root function and the scopes form a tree.
214
215
216\section{Statements}
217
218Fix a CIVL function prototype system. A \emph{CIVL statement} is
219defined to be a tuple of one of the forms described below.
220In each case, we give any restritions on the components of the tuple
221and a brief intuition on the statement's semantics. The precise
222semantics will be described in \S\ref{sec:semantics}.
223
224\begin{enumerate}
225\item $\langle\code{parassign},V_1,V_2,\psi\rangle$
226 \begin{itemize}
227 \item $V_1,V_2\subseteq\Var$
228 \item $\psi\colon V_2\ra\Expr(V_1)$ satisfying
229 $\etype(\psi(v))=\vtype(v)$ for all $v\in V_2$
230 \item \emph{meaning}: parallel assignment, i.e., the assignment of new
231 values to any or all of the variables in $V_2$. For each variable
232 in $V_2$ an expression is given which will be evaluated in the old
233 state to compute the new value for that variable. $V_1$ contains
234 all the variables that may be used in those expressions. Hence
235 $V_1$ is the ``read set'' and $V_2$ is the ``write set'' for the
236 parallel assignment.
237 \end{itemize}
238\item $\langle\code{assign},v,e\rangle$
239 \begin{itemize}
240 \item $v\in\Var$, $e\in\Expr$, $\etype(e)=\vtype(v)$
241 \item \emph{meaning}: simple assignment: evaluate an expression $e$ and
242 assign result to variable $v$. It is a special case of
243 \code{parassign} but is provided for convenience.
244 \end{itemize}
245\item $\langle\code{call},y,f,e_1,\ldots,e_n\rangle$
246 \begin{itemize}
247 \item $y\in\Var$, $f\in\Func$, $e_1,\ldots,e_n\in\Expr$
248 \item $n=\numparams(f)$
249 \item $\etype(e_i)=\vtype(v_i)$, where $\params(f)=v_1\cdots v_n$
250 \item $\returntype(f)=\vtype(y)$
251 \item \emph{meaning}: evaluate expressions $e_1,\ldots,e_n$; push
252 frame on call stack and move control to guarded transition system
253 (see \S\ref{sec:gts}) for function $f$; when $f$ returns, pop
254 stack and store returned result in $y$
255 \end{itemize}
256\item $\langle\code{call},f,e_1,\ldots,e_n\rangle$
257 \begin{itemize}
258 \item $f\in\Func$, $e_1,\ldots,e_n\in\Expr$
259 \item $n=\numparams(f)$
260 \item $\etype(e_i)=\vtype(v_i)$, where $\params(f)=v_1\cdots v_n$
261 \item \emph{meaning}: like above, but return type may be \code{void}
262 or returned value could just be ignored
263 \end{itemize}
264\item $\langle\code{fork},p,f,e_1,\ldots,e_n\rangle$
265 \begin{itemize}
266 \item $p\in\Var$, $f\in\Func$, $e_1,\ldots,e_n\in\Expr$
267 \item $n=\numparams(f)$
268 \item $\etype(e_i)=\vtype(v_i)$, where $\params(f)=v_1\cdots v_n$
269 \item $\returntype(f)=\void$
270 \item $\vtype(p)=\proc$
271 \item \emph{meaning}: evaluate expressions $e_1,\ldots,e_n$;
272 create new process and invoke function $f$ in it;
273 return, immediately, a reference to the new process and store
274 that reference in $p$
275 \end{itemize}
276\item $\langle\code{join},e\rangle$
277 \begin{itemize}
278 \item $e\in\Expr$
279 \item $\etype(e)=\proc$
280 \item \emph{meaning}: evaluate $e$ and wait for the referenced process to terminate
281 \end{itemize}
282\item $\langle\code{return},e\rangle$
283 \begin{itemize}
284 \item $e\in\Expr$
285 \item \emph{meaning}: evaluate $e$, pop the call stack and return
286 control, along with the value, to caller
287 \end{itemize}
288\item $\langle\code{return}\rangle$
289 \begin{itemize}
290 \item \emph{meaning}: pop the call stack and return control to caller;
291 only to be used in functions returning \code{void}
292 \end{itemize}
293\item $\langle\code{write},e\rangle$
294 \begin{itemize}
295 \item $e\in\Expr$
296 \item \emph{meaning}: evaluate $e$ and send result to output
297 \end{itemize}
298\item $\langle\code{noop}\rangle$
299 \begin{itemize}
300 \item \emph{meaning}: does nothing
301 \end{itemize}
302\item $\langle\code{assert}, e\rangle$
303 \begin{itemize}
304 \item $e\in\Expr$, $\vtype(e)=\bool$
305 \item \emph{meaning}: evaluate $e$; if result is false, stop
306 execution and report error
307 \end{itemize}
308\item $\langle\code{assume}, e\rangle$
309 \begin{itemize}
310 \item $e\in\Expr$, $\vtype(e)=\bool$
311 \item \emph{meaning}: assume $e$ holds (i.e., if $e$ does not hold,
312 the execution sequence is not a real execution)
313 \end{itemize}
314\end{enumerate}
315
316\section{Remarks}
317
318The system described is sufficiently general to model pointers. There
319can be (one or more) pointer types and corresponding values. The
320parallel assignment statement can be used to model statements like
321C's \texttt{*p=e;}. In the worst case (if no information is known
322about where \texttt{p} could point), one can let $V_2=\Var$.
323Similarly, expressions that involve \texttt{*p} as a right-hand
324side subexpression can always take $V_1=\Var$.
325
326Heaps can also be modeled. A heap type may be defined and a variable
327of that type declared. Expressions to modify and read from the heap
328can be defined, as can pointers into the heap.
329
330
331\section{Transition system representation of functions}
332\label{sec:gts}
333
334\begin{definition}
335 Given a CIVL function prototype system and $f\in\Func$,
336 a \emph{guarded transition system} for $f$
337 is a tuple $(\Loc,\lscope,\start, T)$, where
338 \begin{itemize}
339 \item $\Loc$ is a set (the set of \emph{locations}),
340 \item $\lscope\colon\Loc\ra\Sigma$ is a function which associates
341 to each $l\in\Loc$ a scope $\lscope(l)\in\Sigma$ belonging to $f$,
342 \item $\start\in\Loc$ (the \emph{start location}),
343 \item $T$ is a set of \emph{guarded transitions}, each of which has
344 the form $\langle l,g,s,l'\rangle$, where
345 \begin{itemize}
346 \item $l,l'\in\Loc$ (the \emph{source} and \emph{target}
347 locations)
348 \item $g\in\Expr(V)$, where $V$ is the set of variables visible at
349 $\lscope(l)$, and $\etype(g)=\bool$ ($g$ is called the
350 \emph{guard}),
351 \item $s$ is a statment all of whose constituent variables,
352 expressions, and function symbols are visible at $\lscope(l)$.
353 \end{itemize}
354 \end{itemize}
355 Furthermore, if the guarded transition system contains a statement
356 of the form $\langle\code{return}\rangle$ then
357 $\returntype(f)=\void$. If it contains a statement of the form
358 $\langle\code{return},e\rangle$ then $\etype(e)=\returntype(f)$.
359\end{definition}
360
361\begin{definition}
362 Given a CIVL prototype system, a \emph{CIVL model} $M$ for that
363 system assigns, to each $f\in\Func$, a guarded transition system
364 \[(\Loc_f,\lscope_f, \start_f,T_f)\] for $f$. Moreover, if $f\neq f'$
365 then
366 $\Loc_f\cap\Loc_{f'}=\emptyset$.
367\end{definition}
368
369\begin{definition}
370 Given a CIVL model $M$, let $\Loc=\bigcup_{f\in\Func}\Loc_f$.
371\end{definition}
372
373\chapter{CIVL Model Semantics}
374\label{sec:semantics}
375
376\section{State}
377\label{sec:state}
378
379Fix a CIVL model $M$. Recall that a valuation is a type-respecting
380function from $\Var$ to $\Val$. Given a subset $V\subseteq\Var$ of
381variables, we define a \emph{valuation on $V$} to be a type-respecting
382function from $V$ to $\Val$. Let $\Eval(V)$ denote the set of all
383valuations on $V$. Note that $\Eval(\Var)=\Eval$.
384
385\begin{definition}
386 \label{def:state}
387 A \emph{state} of a CIVL model $M$ is a tuple
388 \[
389 s=(\Delta, \droot, \dparent, \static, \deval, P, \stack),
390 \]
391 where
392 \begin{enumerate}
393 \item $\Delta$ is a finite set (the set of \emph{dynamic scopes} in
394 $s$),
395 \item $\droot\in\Delta$ (the \emph{root dynamic scope}),
396 \item $\dparent\colon \Delta\setminus\{\droot\}\ra\Delta$
397 is a function such that the set
398 \[
399 \{(\dparent(\delta),\delta)\mid \delta\in
400 \Delta\setminus\{\droot\}\}
401 \]
402 gives $\Delta$ the structure of a rooted tree with root $\droot$,
403 \item $\static\colon\Delta\ra\Sigma$,
404 \item $\static(\droot)=\rootscope$ and $\droot$ is the only
405 $\delta\in\Delta$ satisfying $\static(\delta)=\rootscope$,
406 \item $\static(\dparent(\delta))=\sparent(\static(\delta))$ for any
407 $\delta\in\Delta$,
408 \item $\deval$ is a function that assigns to each $\delta\in\Delta$
409 a valuation $\deval(\delta)\in\Eval(\vars(\static(\delta)))$,
410 \item $P$ (the set of \emph{process IDs} in $s$)
411 is a finite subset of $\Val_{\proc}$, and
412 \item $\stack\colon P\ra \Frame^*$, where
413 \[
414 \Frame=\{(\delta,l)\in\Delta\times\Loc\mid\lscope(l)=\static(\delta)\}.
415 \]
416 \end{enumerate}
417 Let $\State$ denote the set of all states of $M$.
418\end{definition}
419
420% say entrance and exit from scopes does not have to be "structured".
421
422Remarks:
423\begin{enumerate}
424\item We will also refer to dynamic scopes as \emph{dyscopes}.
425\item The elements of $\Delta$ contain no intrinsic information.
426 Instead, all of the information concerning dyscopes is encoded
427 in the functions that take elements of $\Delta$ as arguments. Hence
428 we might just as well call the elements of $\Delta$ ``dynamic scope
429 IDs'' (just as we call the elements of $P$ ``process IDs''). One
430 could use natural numbers for the dyscopes, just as one does
431 for processes.
432\item The reason for using some form of ID for dyscopes and
433 processes, rather than just incorporating the data in the
434 appropriate part of the state, is that both kinds of objects may be
435 shared. There can be several components of the state that refer to
436 the same dyscope $d$: e.g., $d$ could have several children,
437 each of which has a parent reference to $d$, as well as a reference
438 from a frame. A process can be referred to by any number of
439 variables of type $\proc$.
440\item If $\sigma=\static(\delta)$, we say that \emph{$\delta$ is an
441 instance of $\sigma$} or \emph{$\sigma$ is the static scope
442 associated to $\delta$}. In general, a static scope can have any
443 number (including 0) of dynamic instances. The exception is the root
444 scope $\rootscope$, which must have exactly one instance ($\droot$).
445\item A valuation $\deval(\delta)$ assigns a value to each variable in
446 the static scope associated to $\delta$. The function $\deval$
447 thereby encodes the value of all variables ``in scope'' in state
448 $s$.
449\item The sequence $\stack(p)$ encodes the state of the call stack of
450 process $p$. The elements of the sequence are called
451 \emph{activation frames}. The first frame in the sequence, i.e.,
452 the frame in position $0$, is the bottom of the stack; the last
453 frame is the top of the stack.
454\item Each frame refers to a dyscope $\delta$ and a
455 location in the static scope associated to $\delta$.
456\end{enumerate}
457
458
459\begin{definition}
460 A dyscope $\delta\in\Delta$ is a \emph{function node}
461 if $\static(\delta)$ is the function scope of some function.
462\end{definition}
463
464\begin{definition}
465 Given any $\delta\in\Delta$, $\fnode(\delta)\in\Delta$ is defined as
466 follows: if $\delta$ is a function node, then
467 $\fnode(\delta)=\delta$, else
468 $\fnode(\delta)=\fnode(\dparent(\delta))$. We call $\fnode(\delta)$
469 the \emph{function node associated to $\delta$}.
470\end{definition}
471
472The relation $\{(\delta,\delta')\mid \fnode(\delta)=\fnode(\delta')\}$
473is an equivalence relation $\sim$ on $\Delta$. Let
474$\bar{\Delta}=\Delta/\sim$ and write $[\delta]$ for the equivalence
475class containing $\delta$.
476
477The set of activation frames in a state $s$ may be identified with the
478set
479\[
480AF(s)=\bigcup_{p\in P}\{p\}\times\{0,\ldots,\len(\stack(p))-1\}
481\]
482Namely, $(p,i)$ corresponds to the $i^{\text{th}}$ entry in the call
483stack $\stack(p)$ (where the elements of the stacks are indexed from
484$0$).
485
486Define $\Psi\colon AF(s)\ra \bar{\Delta}$ as follows: given $(p,i)$,
487let $(\delta,l)$ be the corresponding frame; set $\Psi(p,i)=[\delta]$.
488
489% \begin{definition}
490% A state $s$ is \emph{well-formed} if all of the following hold:
491% \begin{enumerate}
492% \item for any $\delta\in\Delta$, at most one child of $\delta$ is not
493% a function node,
494% \item the function $\Psi$ is a one-to-one correspondence,
495% \item any $\delta$ occurring in the top frame of a call stack
496% is a leaf node.
497% \end{enumerate}
498% \end{definition}
499
500
501\section{Jump protocol}
502\label{sec:jump}
503
504% can only jump if \delta is a leaf node.
505% also no other frame can point to a dyscope between \delta
506% and the dyscope corresponding to the function scope
507
508% in any state, a "region" of the dyscope tree can have at
509% most one (exactly one?) stack frame pointing into it.
510
511% a region in a chain of nodes starting from a dyanmic scope corresonding
512% to a function scopes and leading down until you reach another
513% function scope.
514
515% whenever you have more than one child in the dynamic tree, all but
516% 0 or 1 children must be a function scope
517
518% every dyscope is owned by at most one frame
519
520% can a frame point only to a leaf node? No, but all of its children
521% must be function nodes
522
523% to find out which frame owns which dyscopes:
524
525% approach 1: start from a frame. frame owns the node it points to.
526% move up parents until you hit a function scope and stop.
527% that is that frame's region. No other frame can point into its
528% region. Proof: true in initial state, invariant under
529% call and fork.
530
531% invariant: every leaf node in the dyscope tree is pointed
532% to by the top frame of the call stack of some process
533
534% Define a \emph{well-formed state}:
535
536% every dyscope node has at most one child which is not a function node
537
538% 1-1 correspondence between leaf nodes and top frames of stacks.
539
540% Define regions in the dyscope tree. (each region contains one
541% function node)
542
543\newcommand{\lub}{\sigma_{\textsf{lub}}}
544
545\begin{figure}[t]
546 \begin{algorithm}[H]
547 \Procedure{$\textsf{\textup{jump}}(s\colon\State, p\colon\Val_{\proc},
548 l'\colon\Loc)\colon\State$}{%
549 let $(\Delta, \droot, \dparent, \static, \deval, P, \stack)=s$\;
550 let $\delta$ be the dyscope of the last frame on $\stack(p)$\;
551 let $\sigma=\static(\delta)$\;
552 let $\sigma'=\lscope(l')$\;
553 let $\lub$ be the least upper bound of $\sigma$ and
554 $\sigma'$ in the tree $\Sigma$\;
555 let $m$ be the minimum integer such that
556 $\sparent^m(\sigma)=\lub$\;
557 let $\delta_{\textsf{lub}}=\dparent^m(\delta)$\;
558 let $n$ be the minimum integer such that
559 $\sparent^{n}(\sigma')=\lub$\;
560 let $\delta_0,\ldots,\delta_{n-1}$ be $n$ distinct objects not in $\Delta$\;
561 let
562 \( \displaystyle
563 \Delta'=\Delta
564 \cup \{ \delta_0,\ldots,\delta_{n-1} \}
565 %\setminus \{ \dparent^j(\delta)\mid 0\leq j<m\}
566 \)\;
567 define $\dparent'\colon\Delta'\setminus\{\droot\}\ra\Delta'$ by
568 \(
569 \dparent'(\delta')=
570 \begin{cases}
571 \dparent(\delta') & \text{if $\delta'\in\Delta$}\\
572 \delta_{i+1} & \text{if $\delta'=\delta_{i}$ for some $0\leq i<n-1$}\\
573 \delta_{\textsf{lub}} & \text{if $n\geq 1$ and $\delta'=\delta_{n-1}$}
574 \end{cases}
575 \)\;
576 define $\static'\colon\Delta'\ra\Sigma$ by
577 \(
578 \static'(\delta')=
579 \begin{cases}
580 \static(\delta') & \text{if $\delta'\in\Delta$}\\
581 \sparent^i(\sigma') & \text{if $\delta'=\delta_i$ for some $0\leq i<n$}
582 \end{cases}
583 \)\;
584 for $\delta'\in\Delta'$ and $v\in\vars(\static(\delta'))$,
585 let
586 \(
587 \deval'(\delta')(v) =
588 \begin{cases}
589 \deval(\delta')(v) & \text{if $\delta'\in\Delta$}\\
590 \default_t & \text{otherwise}
591 \end{cases}
592 \)\;
593 define $\stack'$ to be the same as $\stack$ except that
594 the last frame on $\stack'(p)$ is replaced with
595 $(\delta_0,l')$ if $n\geq 1$, or with $(\delta_{\textsf{lub}},l')$
596 if $n=0$\;
597 let $s'(\Delta',\droot,\dparent',\static',\deval',P,\stack')$\;
598 return the result of removing all unreachable dyscopes from $s'$\;
599 }
600 \end{algorithm}
601 \caption{Jump protocol: how the state changes when control moves
602 to a new location within a function. The procedure may only
603 be called if $\func(\sigma)=\func(\sigma')$, i.e., the jump
604 is contained within one function.}
605 \label{fig:jump}
606\end{figure}
607
608When control moves from one location to another within a function's
609transition system, dyscopes may be added, because you can jump out of
610scope nests and into new scope nests. The motivating idea is that you
611have to move up the dyscope tree every time you move past a right
612curly brace (i.e., leave a scope) and then push on a new scope for
613each left curly brace you move past. So there is a sequence of upward
614moves followed by a sequence of pushes to get to the new
615location. (And either or both sequences could be empty.) At the end,
616if any dyscopes become unreachable, they are removed from the state.
617
618Note however, that we do not assume that scopes are associated to
619locations in a nice lexical pattern (or any pattern at all). The
620protocol described here works for any arbitrary assignment of scopes
621to locations.
622
623The precise protocol is described in Figure \ref{fig:jump}. The
624algorithm shown there takes as input a well-formed state, a process
625ID, and a location $l'$ for the function that $p$ is currently in.
626Say the current dyscope for $p$ is $\delta$, and $l'$ is in
627static scope $\sigma'$. Let $\sigma=\static(\delta)$. Hence the
628current static scope is $\sigma$ and the new static scope will be $\sigma'$.
629
630First, you have to find the \emph{least upper bound} $\lub$ of
631$\sigma$ and $\sigma'$ in the static scope tree. (Hence $\lub$ is a
632common anecestor of $\sigma$ and $\sigma'$, and if $\sigma''$ is any
633common ancestor of $\sigma$ and $\sigma'$ then $\sigma''$ is an
634ancestor or equal to $\lub$.) Note that the least upper bound must
635exist since the function scope is a common ancestor of $\sigma$ and
636$\sigma'$. There is a chain of static scopes from $\sigma$ up to
637$\lub$ and a corresponding chain
638$\delta,\dparent(\delta),\ldots,\dparent^m(\delta)$ in the dynamic
639scope tree. Let $\delta_{\textsf{lub}}=\dparent^m(\delta)$. This
640will become the least upper bound of $\delta$ and the new dynamic
641scope corresponding to $\sigma'$ that will be added to the state.
642
643Next you add new dyscopes corresponding to the chain of static
644scopes leading from $\lub$ down to $\sigma'$. The variables in the
645new scopes are assigned the default values for their respective types.
646The $\dparent$, $\static$, and $\deval$ maps are adjusted
647accordingly. Finally, the stack is modified by replacing the last
648activation frame with a frame referring to the (possibly) new dynamic
649scope and new location $l'$.
650
651This protocol is executed every time control moves from one location
652to another.
653
654Note that in CIVL all jumps stay within a function. There is no
655way to jump from one function to another (without returning).
656
657A small variation is the protocol for moving to the start location of
658a function when the function is first pushed on the stack. Since the
659start location is not necessarily in the function scope (it may be a
660proper descendant of that scope), you have to execute the second half
661of the protocol to push a sequence of scopes from the function scope
662to the scope of the start location.
663
664\section{Initial State}
665
666The \emph{initial state} for $M$ is obtained by creating one process
667(let $P=\{0\}$) and having it call the root function $f_0$.
668Hence start with the state $s$ in which $P=\{0\}$, $\ldots$.
669The initial state is $\textsf{jump}(s,0,\start_{f_0})$.
670
671\section{Transitions}
672
673The transitions follow the usual ``interleaving'' semantics. Given a
674state $\sigma$, one defines the set of enabled transitions in $\sigma$
675as follows. Let $p\in P$. Look at the last frame $(d,l)$ of
676$\stack(p)$ (i.e., the top of the call stack), assuming the stack is
677nonempty. Look at all guarded transitions emanating from $l$. For each
678such transition, evaluate the guard using the valuation formed by
679taking the union of the valuations of all ancestors of $d$ (including
680$d$). In other words, follow the standard ``lexical scoping'' protocol
681to determine the value of any variable that could occur at this point.
682Those transitions whose guard evaluates to $\emph{true}$ are enabled.
683
684For each enabled transition, a new state is generated by executing
685the transition's statement. For the most part, the semantics are obvious,
686but there are a few details that are a bit subtle.
687
688\section{Calls and Spawns}
689
690Both calls and forks of a function $f$ entail the creation of a new
691frame. First, a new dyscope $d$ corresponding to $\fscope(f)$ is
692created. To find out where to stick that new scope in the dynamic
693scope tree, proceed as follows: begin in the dyscope for the
694process invoking the fork or call and start moving up its parent
695sequence until you reach the first dyscope $d'$ whose associated
696static scope is the defining scope of $f$. (You must reach such a
697scope, or else $f$ would not be visible, and the model would have a
698syntax error!) Insert $d$ right under $d'$, i.e.,
699$\dparent(d)=d'$. This preserves the required correspondence between
700static scopes and dyscopes. Now you move to the start location,
701using the jump protocol, which may involve the creation of additional
702dyscopes under $d$. The new frame references the last dynamic
703scope you created, and the location is the start location of $f$.
704Variables are given their initial values in all the newly created
705dyscopes (however that is done).
706
707All of that is the same whether the statement is a fork or call. The
708only difference is what happens next: for a call, the new frame is
709pushed onto the calling process' call stack. For a fork, a new process
710is ``created'', i.e., you pick a natural number not in $P$ and
711add it to $P$, and push the frame onto the new stack. To be totally
712deterministic, you could pick the least natural number not in $P$.
713
714\section{Garbage collection}
715
716In a state $s$, a dyscope is unreachable if there is no path
717from a frame in a call stack to that dyscope (following the
718$\dparent$ edges). You can remove all unreachable dyscopes.
719
720If a process has terminated (has empty stack) and \emph{there are no
721references to that process} in the state, you can just remove the process
722from the state.
723
724In any state, you can renumber the processes (and update the
725references accordingly) however you want, to get rid of gaps, put them
726in a canonic order, etc.
727
Note: See TracBrowser for help on using the repository browser.